sharedsubtree.txt 29 KB

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