sharedsubtree.rst 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===============
  3. Shared Subtrees
  4. ===============
  5. .. Contents:
  6. 1) Overview
  7. 2) Features
  8. 3) Setting mount states
  9. 4) Use-case
  10. 5) Detailed semantics
  11. 6) Quiz
  12. 7) FAQ
  13. 8) Implementation
  14. 1) Overview
  15. -----------
  16. Consider the following situation:
  17. A process wants to clone its own namespace, but still wants to access the CD
  18. that got mounted recently. Shared subtree semantics provide the necessary
  19. mechanism to accomplish the above.
  20. It provides the necessary building blocks for features like per-user-namespace
  21. and versioned filesystem.
  22. 2) Features
  23. -----------
  24. Shared subtree provides four different flavors of mounts; struct vfsmount to be
  25. precise
  26. a. shared mount
  27. b. slave mount
  28. c. private mount
  29. d. unbindable mount
  30. 2a) A shared mount can be replicated to as many mountpoints and all the
  31. replicas continue to be exactly same.
  32. Here is an example:
  33. Let's say /mnt has a mount that is shared::
  34. mount --make-shared /mnt
  35. Note: mount(8) command now supports the --make-shared flag,
  36. so the sample 'smount' program is no longer needed and has been
  37. removed.
  38. ::
  39. # mount --bind /mnt /tmp
  40. The above command replicates the mount at /mnt to the mountpoint /tmp
  41. and the contents of both the mounts remain identical.
  42. ::
  43. #ls /mnt
  44. a b c
  45. #ls /tmp
  46. a b c
  47. Now let's say we mount a device at /tmp/a::
  48. # mount /dev/sd0 /tmp/a
  49. #ls /tmp/a
  50. t1 t2 t3
  51. #ls /mnt/a
  52. t1 t2 t3
  53. Note that the mount has propagated to the mount at /mnt as well.
  54. And the same is true even when /dev/sd0 is mounted on /mnt/a. The
  55. contents will be visible under /tmp/a too.
  56. 2b) A slave mount is like a shared mount except that mount and umount events
  57. only propagate towards it.
  58. All slave mounts have a master mount which is a shared.
  59. Here is an example:
  60. Let's say /mnt has a mount which is shared.
  61. # mount --make-shared /mnt
  62. Let's bind mount /mnt to /tmp
  63. # mount --bind /mnt /tmp
  64. the new mount at /tmp becomes a shared mount and it is a replica of
  65. the mount at /mnt.
  66. Now let's make the mount at /tmp; a slave of /mnt
  67. # mount --make-slave /tmp
  68. let's mount /dev/sd0 on /mnt/a
  69. # mount /dev/sd0 /mnt/a
  70. #ls /mnt/a
  71. t1 t2 t3
  72. #ls /tmp/a
  73. t1 t2 t3
  74. Note the mount event has propagated to the mount at /tmp
  75. However let's see what happens if we mount something on the mount at /tmp
  76. # mount /dev/sd1 /tmp/b
  77. #ls /tmp/b
  78. s1 s2 s3
  79. #ls /mnt/b
  80. Note how the mount event has not propagated to the mount at
  81. /mnt
  82. 2c) A private mount does not forward or receive propagation.
  83. This is the mount we are familiar with. Its the default type.
  84. 2d) A unbindable mount is a unbindable private mount
  85. let's say we have a mount at /mnt and we make it unbindable::
  86. # mount --make-unbindable /mnt
  87. Let's try to bind mount this mount somewhere else::
  88. # mount --bind /mnt /tmp
  89. mount: wrong fs type, bad option, bad superblock on /mnt,
  90. or too many mounted file systems
  91. Binding a unbindable mount is a invalid operation.
  92. 3) Setting mount states
  93. The mount command (util-linux package) can be used to set mount
  94. states::
  95. mount --make-shared mountpoint
  96. mount --make-slave mountpoint
  97. mount --make-private mountpoint
  98. mount --make-unbindable mountpoint
  99. 4) Use cases
  100. ------------
  101. A) A process wants to clone its own namespace, but still wants to
  102. access the CD that got mounted recently.
  103. Solution:
  104. The system administrator can make the mount at /cdrom shared::
  105. mount --bind /cdrom /cdrom
  106. mount --make-shared /cdrom
  107. Now any process that clones off a new namespace will have a
  108. mount at /cdrom which is a replica of the same mount in the
  109. parent namespace.
  110. So when a CD is inserted and mounted at /cdrom that mount gets
  111. propagated to the other mount at /cdrom in all the other clone
  112. namespaces.
  113. B) A process wants its mounts invisible to any other process, but
  114. still be able to see the other system mounts.
  115. Solution:
  116. To begin with, the administrator can mark the entire mount tree
  117. as shareable::
  118. mount --make-rshared /
  119. A new process can clone off a new namespace. And mark some part
  120. of its namespace as slave::
  121. mount --make-rslave /myprivatetree
  122. Hence forth any mounts within the /myprivatetree done by the
  123. process will not show up in any other namespace. However mounts
  124. done in the parent namespace under /myprivatetree still shows
  125. up in the process's namespace.
  126. Apart from the above semantics this feature provides the
  127. building blocks to solve the following problems:
  128. C) Per-user namespace
  129. The above semantics allows a way to share mounts across
  130. namespaces. But namespaces are associated with processes. If
  131. namespaces are made first class objects with user API to
  132. associate/disassociate a namespace with userid, then each user
  133. could have his/her own namespace and tailor it to his/her
  134. requirements. This needs to be supported in PAM.
  135. D) Versioned files
  136. If the entire mount tree is visible at multiple locations, then
  137. an underlying versioning file system can return different
  138. versions of the file depending on the path used to access that
  139. file.
  140. An example is::
  141. mount --make-shared /
  142. mount --rbind / /view/v1
  143. mount --rbind / /view/v2
  144. mount --rbind / /view/v3
  145. mount --rbind / /view/v4
  146. and if /usr has a versioning filesystem mounted, then that
  147. mount appears at /view/v1/usr, /view/v2/usr, /view/v3/usr and
  148. /view/v4/usr too
  149. A user can request v3 version of the file /usr/fs/namespace.c
  150. by accessing /view/v3/usr/fs/namespace.c . The underlying
  151. versioning filesystem can then decipher that v3 version of the
  152. filesystem is being requested and return the corresponding
  153. inode.
  154. 5) Detailed semantics
  155. ---------------------
  156. The section below explains the detailed semantics of
  157. bind, rbind, move, mount, umount and clone-namespace operations.
  158. Note: the word 'vfsmount' and the noun 'mount' have been used
  159. to mean the same thing, throughout this document.
  160. 5a) Mount states
  161. A given mount can be in one of the following states
  162. 1) shared
  163. 2) slave
  164. 3) shared and slave
  165. 4) private
  166. 5) unbindable
  167. A 'propagation event' is defined as event generated on a vfsmount
  168. that leads to mount or unmount actions in other vfsmounts.
  169. A 'peer group' is defined as a group of vfsmounts that propagate
  170. events to each other.
  171. (1) Shared mounts
  172. A 'shared mount' is defined as a vfsmount that belongs to a
  173. 'peer group'.
  174. For example::
  175. mount --make-shared /mnt
  176. mount --bind /mnt /tmp
  177. The mount at /mnt and that at /tmp are both shared and belong
  178. to the same peer group. Anything mounted or unmounted under
  179. /mnt or /tmp reflect in all the other mounts of its peer
  180. group.
  181. (2) Slave mounts
  182. A 'slave mount' is defined as a vfsmount that receives
  183. propagation events and does not forward propagation events.
  184. A slave mount as the name implies has a master mount from which
  185. mount/unmount events are received. Events do not propagate from
  186. the slave mount to the master. Only a shared mount can be made
  187. a slave by executing the following command::
  188. mount --make-slave mount
  189. A shared mount that is made as a slave is no more shared unless
  190. modified to become shared.
  191. (3) Shared and Slave
  192. A vfsmount can be both shared as well as slave. This state
  193. indicates that the mount is a slave of some vfsmount, and
  194. has its own peer group too. This vfsmount receives propagation
  195. events from its master vfsmount, and also forwards propagation
  196. events to its 'peer group' and to its slave vfsmounts.
  197. Strictly speaking, the vfsmount is shared having its own
  198. peer group, and this peer-group is a slave of some other
  199. peer group.
  200. Only a slave vfsmount can be made as 'shared and slave' by
  201. either executing the following command::
  202. mount --make-shared mount
  203. or by moving the slave vfsmount under a shared vfsmount.
  204. (4) Private mount
  205. A 'private mount' is defined as vfsmount that does not
  206. receive or forward any propagation events.
  207. (5) Unbindable mount
  208. A 'unbindable mount' is defined as vfsmount that does not
  209. receive or forward any propagation events and cannot
  210. be bind mounted.
  211. State diagram:
  212. The state diagram below explains the state transition of a mount,
  213. in response to various commands::
  214. -----------------------------------------------------------------------
  215. | |make-shared | make-slave | make-private |make-unbindab|
  216. --------------|------------|--------------|--------------|-------------|
  217. |shared |shared |*slave/private| private | unbindable |
  218. | | | | | |
  219. |-------------|------------|--------------|--------------|-------------|
  220. |slave |shared | **slave | private | unbindable |
  221. | |and slave | | | |
  222. |-------------|------------|--------------|--------------|-------------|
  223. |shared |shared | slave | private | unbindable |
  224. |and slave |and slave | | | |
  225. |-------------|------------|--------------|--------------|-------------|
  226. |private |shared | **private | private | unbindable |
  227. |-------------|------------|--------------|--------------|-------------|
  228. |unbindable |shared |**unbindable | private | unbindable |
  229. ------------------------------------------------------------------------
  230. * if the shared mount is the only mount in its peer group, making it
  231. slave, makes it private automatically. Note that there is no master to
  232. which it can be slaved to.
  233. ** slaving a non-shared mount has no effect on the mount.
  234. Apart from the commands listed below, the 'move' operation also changes
  235. the state of a mount depending on type of the destination mount. Its
  236. explained in section 5d.
  237. 5b) Bind semantics
  238. Consider the following command::
  239. mount --bind A/a B/b
  240. where 'A' is the source mount, 'a' is the dentry in the mount 'A', 'B'
  241. is the destination mount and 'b' is the dentry in the destination mount.
  242. The outcome depends on the type of mount of 'A' and 'B'. The table
  243. below contains quick reference::
  244. --------------------------------------------------------------------------
  245. | BIND MOUNT OPERATION |
  246. |************************************************************************|
  247. |source(A)->| shared | private | slave | unbindable |
  248. | dest(B) | | | | |
  249. | | | | | | |
  250. | v | | | | |
  251. |************************************************************************|
  252. | shared | shared | shared | shared & slave | invalid |
  253. | | | | | |
  254. |non-shared| shared | private | slave | invalid |
  255. **************************************************************************
  256. Details:
  257. 1. 'A' is a shared mount and 'B' is a shared mount. A new mount 'C'
  258. which is clone of 'A', is created. Its root dentry is 'a' . 'C' is
  259. mounted on mount 'B' at dentry 'b'. Also new mount 'C1', 'C2', 'C3' ...
  260. are created and mounted at the dentry 'b' on all mounts where 'B'
  261. propagates to. A new propagation tree containing 'C1',..,'Cn' is
  262. created. This propagation tree is identical to the propagation tree of
  263. 'B'. And finally the peer-group of 'C' is merged with the peer group
  264. of 'A'.
  265. 2. 'A' is a private mount and 'B' is a shared mount. A new mount 'C'
  266. which is clone of 'A', is created. Its root dentry is 'a'. 'C' is
  267. mounted on mount 'B' at dentry 'b'. Also new mount 'C1', 'C2', 'C3' ...
  268. are created and mounted at the dentry 'b' on all mounts where 'B'
  269. propagates to. A new propagation tree is set containing all new mounts
  270. 'C', 'C1', .., 'Cn' with exactly the same configuration as the
  271. propagation tree for 'B'.
  272. 3. 'A' is a slave mount of mount 'Z' and 'B' is a shared mount. A new
  273. mount 'C' which is clone of 'A', is created. Its root dentry is 'a' .
  274. 'C' is mounted on mount 'B' at dentry 'b'. Also new mounts 'C1', 'C2',
  275. 'C3' ... are created and mounted at the dentry 'b' on all mounts where
  276. 'B' propagates to. A new propagation tree containing the new mounts
  277. 'C','C1',.. 'Cn' is created. This propagation tree is identical to the
  278. propagation tree for 'B'. And finally the mount 'C' and its peer group
  279. is made the slave of mount 'Z'. In other words, mount 'C' is in the
  280. state 'slave and shared'.
  281. 4. 'A' is a unbindable mount and 'B' is a shared mount. This is a
  282. invalid operation.
  283. 5. 'A' is a private mount and 'B' is a non-shared(private or slave or
  284. unbindable) mount. A new mount 'C' which is clone of 'A', is created.
  285. Its root dentry is 'a'. 'C' is mounted on mount 'B' at dentry 'b'.
  286. 6. 'A' is a shared mount and 'B' is a non-shared mount. A new mount 'C'
  287. which is a clone of 'A' is created. Its root dentry is 'a'. 'C' is
  288. mounted on mount 'B' at dentry 'b'. 'C' is made a member of the
  289. peer-group of 'A'.
  290. 7. 'A' is a slave mount of mount 'Z' and 'B' is a non-shared mount. A
  291. new mount 'C' which is a clone of 'A' is created. Its root dentry is
  292. 'a'. 'C' is mounted on mount 'B' at dentry 'b'. Also 'C' is set as a
  293. slave mount of 'Z'. In other words 'A' and 'C' are both slave mounts of
  294. 'Z'. All mount/unmount events on 'Z' propagates to 'A' and 'C'. But
  295. mount/unmount on 'A' do not propagate anywhere else. Similarly
  296. mount/unmount on 'C' do not propagate anywhere else.
  297. 8. 'A' is a unbindable mount and 'B' is a non-shared mount. This is a
  298. invalid operation. A unbindable mount cannot be bind mounted.
  299. 5c) Rbind semantics
  300. rbind is same as bind. Bind replicates the specified mount. Rbind
  301. replicates all the mounts in the tree belonging to the specified mount.
  302. Rbind mount is bind mount applied to all the mounts in the tree.
  303. If the source tree that is rbind has some unbindable mounts,
  304. then the subtree under the unbindable mount is pruned in the new
  305. location.
  306. eg:
  307. let's say we have the following mount tree::
  308. A
  309. / \
  310. B C
  311. / \ / \
  312. D E F G
  313. Let's say all the mount except the mount C in the tree are
  314. of a type other than unbindable.
  315. If this tree is rbound to say Z
  316. We will have the following tree at the new location::
  317. Z
  318. |
  319. A'
  320. /
  321. B' Note how the tree under C is pruned
  322. / \ in the new location.
  323. D' E'
  324. 5d) Move semantics
  325. Consider the following command
  326. mount --move A B/b
  327. where 'A' is the source mount, 'B' is the destination mount and 'b' is
  328. the dentry in the destination mount.
  329. The outcome depends on the type of the mount of 'A' and 'B'. The table
  330. below is a quick reference::
  331. ---------------------------------------------------------------------------
  332. | MOVE MOUNT OPERATION |
  333. |**************************************************************************
  334. | source(A)->| shared | private | slave | unbindable |
  335. | dest(B) | | | | |
  336. | | | | | | |
  337. | v | | | | |
  338. |**************************************************************************
  339. | shared | shared | shared |shared and slave| invalid |
  340. | | | | | |
  341. |non-shared| shared | private | slave | unbindable |
  342. ***************************************************************************
  343. .. Note:: moving a mount residing under a shared mount is invalid.
  344. Details follow:
  345. 1. 'A' is a shared mount and 'B' is a shared mount. The mount 'A' is
  346. mounted on mount 'B' at dentry 'b'. Also new mounts 'A1', 'A2'...'An'
  347. are created and mounted at dentry 'b' on all mounts that receive
  348. propagation from mount 'B'. A new propagation tree is created in the
  349. exact same configuration as that of 'B'. This new propagation tree
  350. contains all the new mounts 'A1', 'A2'... 'An'. And this new
  351. propagation tree is appended to the already existing propagation tree
  352. of 'A'.
  353. 2. 'A' is a private mount and 'B' is a shared mount. The mount 'A' is
  354. mounted on mount 'B' at dentry 'b'. Also new mount 'A1', 'A2'... 'An'
  355. are created and mounted at dentry 'b' on all mounts that receive
  356. propagation from mount 'B'. The mount 'A' becomes a shared mount and a
  357. propagation tree is created which is identical to that of
  358. 'B'. This new propagation tree contains all the new mounts 'A1',
  359. 'A2'... 'An'.
  360. 3. 'A' is a slave mount of mount 'Z' and 'B' is a shared mount. The
  361. mount 'A' is mounted on mount 'B' at dentry 'b'. Also new mounts 'A1',
  362. 'A2'... 'An' are created and mounted at dentry 'b' on all mounts that
  363. receive propagation from mount 'B'. A new propagation tree is created
  364. in the exact same configuration as that of 'B'. This new propagation
  365. tree contains all the new mounts 'A1', 'A2'... 'An'. And this new
  366. propagation tree is appended to the already existing propagation tree of
  367. 'A'. Mount 'A' continues to be the slave mount of 'Z' but it also
  368. becomes 'shared'.
  369. 4. 'A' is a unbindable mount and 'B' is a shared mount. The operation
  370. is invalid. Because mounting anything on the shared mount 'B' can
  371. create new mounts that get mounted on the mounts that receive
  372. propagation from 'B'. And since the mount 'A' is unbindable, cloning
  373. it to mount at other mountpoints is not possible.
  374. 5. 'A' is a private mount and 'B' is a non-shared(private or slave or
  375. unbindable) mount. The mount 'A' is mounted on mount 'B' at dentry 'b'.
  376. 6. 'A' is a shared mount and 'B' is a non-shared mount. The mount 'A'
  377. is mounted on mount 'B' at dentry 'b'. Mount 'A' continues to be a
  378. shared mount.
  379. 7. 'A' is a slave mount of mount 'Z' and 'B' is a non-shared mount.
  380. The mount 'A' is mounted on mount 'B' at dentry 'b'. Mount 'A'
  381. continues to be a slave mount of mount 'Z'.
  382. 8. 'A' is a unbindable mount and 'B' is a non-shared mount. The mount
  383. 'A' is mounted on mount 'B' at dentry 'b'. Mount 'A' continues to be a
  384. unbindable mount.
  385. 5e) Mount semantics
  386. Consider the following command::
  387. mount device B/b
  388. 'B' is the destination mount and 'b' is the dentry in the destination
  389. mount.
  390. The above operation is the same as bind operation with the exception
  391. that the source mount is always a private mount.
  392. 5f) Unmount semantics
  393. Consider the following command::
  394. umount A
  395. where 'A' is a mount mounted on mount 'B' at dentry 'b'.
  396. If mount 'B' is shared, then all most-recently-mounted mounts at dentry
  397. 'b' on mounts that receive propagation from mount 'B' and does not have
  398. sub-mounts within them are unmounted.
  399. Example: Let's say 'B1', 'B2', 'B3' are shared mounts that propagate to
  400. each other.
  401. let's say 'A1', 'A2', 'A3' are first mounted at dentry 'b' on mount
  402. 'B1', 'B2' and 'B3' respectively.
  403. let's say 'C1', 'C2', 'C3' are next mounted at the same dentry 'b' on
  404. mount 'B1', 'B2' and 'B3' respectively.
  405. if 'C1' is unmounted, all the mounts that are most-recently-mounted on
  406. 'B1' and on the mounts that 'B1' propagates-to are unmounted.
  407. 'B1' propagates to 'B2' and 'B3'. And the most recently mounted mount
  408. on 'B2' at dentry 'b' is 'C2', and that of mount 'B3' is 'C3'.
  409. So all 'C1', 'C2' and 'C3' should be unmounted.
  410. If any of 'C2' or 'C3' has some child mounts, then that mount is not
  411. unmounted, but all other mounts are unmounted. However if 'C1' is told
  412. to be unmounted and 'C1' has some sub-mounts, the umount operation is
  413. failed entirely.
  414. 5g) Clone Namespace
  415. A cloned namespace contains all the mounts as that of the parent
  416. namespace.
  417. Let's say 'A' and 'B' are the corresponding mounts in the parent and the
  418. child namespace.
  419. If 'A' is shared, then 'B' is also shared and 'A' and 'B' propagate to
  420. each other.
  421. If 'A' is a slave mount of 'Z', then 'B' is also the slave mount of
  422. 'Z'.
  423. If 'A' is a private mount, then 'B' is a private mount too.
  424. If 'A' is unbindable mount, then 'B' is a unbindable mount too.
  425. 6) Quiz
  426. A. What is the result of the following command sequence?
  427. ::
  428. mount --bind /mnt /mnt
  429. mount --make-shared /mnt
  430. mount --bind /mnt /tmp
  431. mount --move /tmp /mnt/1
  432. what should be the contents of /mnt /mnt/1 /mnt/1/1 should be?
  433. Should they all be identical? or should /mnt and /mnt/1 be
  434. identical only?
  435. B. What is the result of the following command sequence?
  436. ::
  437. mount --make-rshared /
  438. mkdir -p /v/1
  439. mount --rbind / /v/1
  440. what should be the content of /v/1/v/1 be?
  441. C. What is the result of the following command sequence?
  442. ::
  443. mount --bind /mnt /mnt
  444. mount --make-shared /mnt
  445. mkdir -p /mnt/1/2/3 /mnt/1/test
  446. mount --bind /mnt/1 /tmp
  447. mount --make-slave /mnt
  448. mount --make-shared /mnt
  449. mount --bind /mnt/1/2 /tmp1
  450. mount --make-slave /mnt
  451. At this point we have the first mount at /tmp and
  452. its root dentry is 1. Let's call this mount 'A'
  453. And then we have a second mount at /tmp1 with root
  454. dentry 2. Let's call this mount 'B'
  455. Next we have a third mount at /mnt with root dentry
  456. mnt. Let's call this mount 'C'
  457. 'B' is the slave of 'A' and 'C' is a slave of 'B'
  458. A -> B -> C
  459. at this point if we execute the following command
  460. mount --bind /bin /tmp/test
  461. The mount is attempted on 'A'
  462. will the mount propagate to 'B' and 'C' ?
  463. what would be the contents of
  464. /mnt/1/test be?
  465. 7) FAQ
  466. Q1. Why is bind mount needed? How is it different from symbolic links?
  467. symbolic links can get stale if the destination mount gets
  468. unmounted or moved. Bind mounts continue to exist even if the
  469. other mount is unmounted or moved.
  470. Q2. Why can't the shared subtree be implemented using exportfs?
  471. exportfs is a heavyweight way of accomplishing part of what
  472. shared subtree can do. I cannot imagine a way to implement the
  473. semantics of slave mount using exportfs?
  474. Q3 Why is unbindable mount needed?
  475. Let's say we want to replicate the mount tree at multiple
  476. locations within the same subtree.
  477. if one rbind mounts a tree within the same subtree 'n' times
  478. the number of mounts created is an exponential function of 'n'.
  479. Having unbindable mount can help prune the unneeded bind
  480. mounts. Here is an example.
  481. step 1:
  482. let's say the root tree has just two directories with
  483. one vfsmount::
  484. root
  485. / \
  486. tmp usr
  487. And we want to replicate the tree at multiple
  488. mountpoints under /root/tmp
  489. step 2:
  490. ::
  491. mount --make-shared /root
  492. mkdir -p /tmp/m1
  493. mount --rbind /root /tmp/m1
  494. the new tree now looks like this::
  495. root
  496. / \
  497. tmp usr
  498. /
  499. m1
  500. / \
  501. tmp usr
  502. /
  503. m1
  504. it has two vfsmounts
  505. step 3:
  506. ::
  507. mkdir -p /tmp/m2
  508. mount --rbind /root /tmp/m2
  509. the new tree now looks like this::
  510. root
  511. / \
  512. tmp usr
  513. / \
  514. m1 m2
  515. / \ / \
  516. tmp usr tmp usr
  517. / \ /
  518. m1 m2 m1
  519. / \ / \
  520. tmp usr tmp usr
  521. / / \
  522. m1 m1 m2
  523. / \
  524. tmp usr
  525. / \
  526. m1 m2
  527. it has 6 vfsmounts
  528. step 4:
  529. ::
  530. mkdir -p /tmp/m3
  531. mount --rbind /root /tmp/m3
  532. I won't draw the tree..but it has 24 vfsmounts
  533. at step i the number of vfsmounts is V[i] = i*V[i-1].
  534. This is an exponential function. And this tree has way more
  535. mounts than what we really needed in the first place.
  536. One could use a series of umount at each step to prune
  537. out the unneeded mounts. But there is a better solution.
  538. Unclonable mounts come in handy here.
  539. step 1:
  540. let's say the root tree has just two directories with
  541. one vfsmount::
  542. root
  543. / \
  544. tmp usr
  545. How do we set up the same tree at multiple locations under
  546. /root/tmp
  547. step 2:
  548. ::
  549. mount --bind /root/tmp /root/tmp
  550. mount --make-rshared /root
  551. mount --make-unbindable /root/tmp
  552. mkdir -p /tmp/m1
  553. mount --rbind /root /tmp/m1
  554. the new tree now looks like this::
  555. root
  556. / \
  557. tmp usr
  558. /
  559. m1
  560. / \
  561. tmp usr
  562. step 3:
  563. ::
  564. mkdir -p /tmp/m2
  565. mount --rbind /root /tmp/m2
  566. the new tree now looks like this::
  567. root
  568. / \
  569. tmp usr
  570. / \
  571. m1 m2
  572. / \ / \
  573. tmp usr tmp usr
  574. step 4:
  575. ::
  576. mkdir -p /tmp/m3
  577. mount --rbind /root /tmp/m3
  578. the new tree now looks like this::
  579. root
  580. / \
  581. tmp usr
  582. / \ \
  583. m1 m2 m3
  584. / \ / \ / \
  585. tmp usr tmp usr tmp usr
  586. 8) Implementation
  587. 8A) Datastructure
  588. 4 new fields are introduced to struct vfsmount:
  589. * ->mnt_share
  590. * ->mnt_slave_list
  591. * ->mnt_slave
  592. * ->mnt_master
  593. ->mnt_share
  594. links together all the mount to/from which this vfsmount
  595. send/receives propagation events.
  596. ->mnt_slave_list
  597. links all the mounts to which this vfsmount propagates
  598. to.
  599. ->mnt_slave
  600. links together all the slaves that its master vfsmount
  601. propagates to.
  602. ->mnt_master
  603. points to the master vfsmount from which this vfsmount
  604. receives propagation.
  605. ->mnt_flags
  606. takes two more flags to indicate the propagation status of
  607. the vfsmount. MNT_SHARE indicates that the vfsmount is a shared
  608. vfsmount. MNT_UNCLONABLE indicates that the vfsmount cannot be
  609. replicated.
  610. All the shared vfsmounts in a peer group form a cyclic list through
  611. ->mnt_share.
  612. All vfsmounts with the same ->mnt_master form on a cyclic list anchored
  613. in ->mnt_master->mnt_slave_list and going through ->mnt_slave.
  614. ->mnt_master can point to arbitrary (and possibly different) members
  615. of master peer group. To find all immediate slaves of a peer group
  616. you need to go through _all_ ->mnt_slave_list of its members.
  617. Conceptually it's just a single set - distribution among the
  618. individual lists does not affect propagation or the way propagation
  619. tree is modified by operations.
  620. All vfsmounts in a peer group have the same ->mnt_master. If it is
  621. non-NULL, they form a contiguous (ordered) segment of slave list.
  622. A example propagation tree looks as shown in the figure below.
  623. [ NOTE: Though it looks like a forest, if we consider all the shared
  624. mounts as a conceptual entity called 'pnode', it becomes a tree]::
  625. A <--> B <--> C <---> D
  626. /|\ /| |\
  627. / F G J K H I
  628. /
  629. E<-->K
  630. /|\
  631. M L N
  632. In the above figure A,B,C and D all are shared and propagate to each
  633. other. 'A' has got 3 slave mounts 'E' 'F' and 'G' 'C' has got 2 slave
  634. mounts 'J' and 'K' and 'D' has got two slave mounts 'H' and 'I'.
  635. 'E' is also shared with 'K' and they propagate to each other. And
  636. 'K' has 3 slaves 'M', 'L' and 'N'
  637. A's ->mnt_share links with the ->mnt_share of 'B' 'C' and 'D'
  638. A's ->mnt_slave_list links with ->mnt_slave of 'E', 'K', 'F' and 'G'
  639. E's ->mnt_share links with ->mnt_share of K
  640. 'E', 'K', 'F', 'G' have their ->mnt_master point to struct vfsmount of 'A'
  641. 'M', 'L', 'N' have their ->mnt_master point to struct vfsmount of 'K'
  642. K's ->mnt_slave_list links with ->mnt_slave of 'M', 'L' and 'N'
  643. C's ->mnt_slave_list links with ->mnt_slave of 'J' and 'K'
  644. J and K's ->mnt_master points to struct vfsmount of C
  645. and finally D's ->mnt_slave_list links with ->mnt_slave of 'H' and 'I'
  646. 'H' and 'I' have their ->mnt_master pointing to struct vfsmount of 'D'.
  647. NOTE: The propagation tree is orthogonal to the mount tree.
  648. 8B Locking:
  649. ->mnt_share, ->mnt_slave, ->mnt_slave_list, ->mnt_master are protected
  650. by namespace_sem (exclusive for modifications, shared for reading).
  651. Normally we have ->mnt_flags modifications serialized by vfsmount_lock.
  652. There are two exceptions: do_add_mount() and clone_mnt().
  653. The former modifies a vfsmount that has not been visible in any shared
  654. data structures yet.
  655. The latter holds namespace_sem and the only references to vfsmount
  656. are in lists that can't be traversed without namespace_sem.
  657. 8C Algorithm:
  658. The crux of the implementation resides in rbind/move operation.
  659. The overall algorithm breaks the operation into 3 phases: (look at
  660. attach_recursive_mnt() and propagate_mnt())
  661. 1. prepare phase.
  662. 2. commit phases.
  663. 3. abort phases.
  664. Prepare phase:
  665. for each mount in the source tree:
  666. a) Create the necessary number of mount trees to
  667. be attached to each of the mounts that receive
  668. propagation from the destination mount.
  669. b) Do not attach any of the trees to its destination.
  670. However note down its ->mnt_parent and ->mnt_mountpoint
  671. c) Link all the new mounts to form a propagation tree that
  672. is identical to the propagation tree of the destination
  673. mount.
  674. If this phase is successful, there should be 'n' new
  675. propagation trees; where 'n' is the number of mounts in the
  676. source tree. Go to the commit phase
  677. Also there should be 'm' new mount trees, where 'm' is
  678. the number of mounts to which the destination mount
  679. propagates to.
  680. if any memory allocations fail, go to the abort phase.
  681. Commit phase
  682. attach each of the mount trees to their corresponding
  683. destination mounts.
  684. Abort phase
  685. delete all the newly created trees.
  686. .. Note::
  687. all the propagation related functionality resides in the file pnode.c
  688. ------------------------------------------------------------------------
  689. version 0.1 (created the initial document, Ram Pai linuxram@us.ibm.com)
  690. version 0.2 (Incorporated comments from Al Viro)