sharedsubtree.rst 30 KB

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