sharedsubtree.txt 28 KB

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