sharedsubtree.txt 32 KB

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