sharedsubtree.txt 32 KB

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