Shared SubtreesΒΆ
1) OverviewΒΆ
Consider the following situation:
A process wants to clone its own namespace, but still wants to access the CD that got mounted recently. Shared subtree semantics provide the necessary mechanism to accomplish the above.
It provides the necessary building blocks for features like per-user-namespace and versioned filesystem.
2) FeaturesΒΆ
Shared subtree provides four different flavors of mounts; struct vfsmount to be
precise:
A shared mount can be replicated to as many mountpoints and all the replicas continue to be exactly same.
Here is an example:
Letβs say /mnt has a mount that is shared:
# mount --make-shared /mnt
Note
mount(8) command now supports the --make-shared flag, so the sample βsmountβ program is no longer needed and has been removed.
# mount --bind /mnt /tmp
The above command replicates the mount at /mnt to the mountpoint /tmp and the contents of both the mounts remain identical.
#ls /mnt a b c #ls /tmp a b c
Now letβs say we mount a device at /tmp/a:
# mount /dev/sd0 /tmp/a # ls /tmp/a t1 t2 t3 # ls /mnt/a t1 t2 t3
Note that the mount has propagated to the mount at /mnt as well.
And the same is true even when /dev/sd0 is mounted on /mnt/a. The contents will be visible under /tmp/a too.
A slave mount is like a shared mount except that mount and umount events only propagate towards it.
All slave mounts have a master mount which is a shared.
Here is an example:
Letβs say /mnt has a mount which is shared:
# mount --make-shared /mnt
Letβs bind mount /mnt to /tmp:
# mount --bind /mnt /tmp
the new mount at /tmp becomes a shared mount and it is a replica of the mount at /mnt.
Now letβs make the mount at /tmp; a slave of /mnt:
# mount --make-slave /tmp
letβs mount /dev/sd0 on /mnt/a:
# mount /dev/sd0 /mnt/a # ls /mnt/a t1 t2 t3 # ls /tmp/a t1 t2 t3
Note the mount event has propagated to the mount at /tmp
However letβs see what happens if we mount something on the mount at /tmp:
# mount /dev/sd1 /tmp/b # ls /tmp/b s1 s2 s3 # ls /mnt/b
Note how the mount event has not propagated to the mount at /mnt
A private mount does not forward or receive propagation.
This is the mount we are familiar with. Its the default type.
An unbindable mount is, as the name suggests, an unbindable private mount.
letβs say we have a mount at /mnt and we make it unbindable:
# mount --make-unbindable /mnt
Letβs try to bind mount this mount somewhere else:
# mount --bind /mnt /tmp mount: wrong fs type, bad option, bad superblock on /mnt, or too many mounted file systems
Binding a unbindable mount is a invalid operation.
3) Setting mount statesΒΆ
The mount command (util-linux package) can be used to set mount states:
mount --make-shared mountpoint
mount --make-slave mountpoint
mount --make-private mountpoint
mount --make-unbindable mountpoint
4) Use casesΒΆ
A process wants to clone its own namespace, but still wants to access the CD that got mounted recently.
Solution:
The system administrator can make the mount at /cdrom shared:
mount --bind /cdrom /cdrom mount --make-shared /cdrom
Now any process that clones off a new namespace will have a mount at /cdrom which is a replica of the same mount in the parent namespace.
So when a CD is inserted and mounted at /cdrom that mount gets propagated to the other mount at /cdrom in all the other clone namespaces.
A process wants its mounts invisible to any other process, but still be able to see the other system mounts.
Solution:
To begin with, the administrator can mark the entire mount tree as shareable:
mount --make-rshared /
A new process can clone off a new namespace. And mark some part of its namespace as slave:
mount --make-rslave /myprivatetree
Hence forth any mounts within the /myprivatetree done by the process will not show up in any other namespace. However mounts done in the parent namespace under /myprivatetree still shows up in the processβs namespace.
Apart from the above semantics this feature provides the building blocks to solve the following problems:
Per-user namespace
The above semantics allows a way to share mounts across namespaces. But namespaces are associated with processes. If namespaces are made first class objects with user API to associate/disassociate a namespace with userid, then each user could have his/her own namespace and tailor it to his/her requirements. This needs to be supported in PAM.
Versioned files
If the entire mount tree is visible at multiple locations, then an underlying versioning file system can return different versions of the file depending on the path used to access that file.
An example is:
mount --make-shared / mount --rbind / /view/v1 mount --rbind / /view/v2 mount --rbind / /view/v3 mount --rbind / /view/v4
and if /usr has a versioning filesystem mounted, then that mount appears at /view/v1/usr, /view/v2/usr, /view/v3/usr and /view/v4/usr too
A user can request v3 version of the file /usr/fs/namespace.c by accessing /view/v3/usr/fs/namespace.c . The underlying versioning filesystem can then decipher that v3 version of the filesystem is being requested and return the corresponding inode.
5) Detailed semanticsΒΆ
The section below explains the detailed semantics of bind, rbind, move, mount, umount and clone-namespace operations.
Note
the word βvfsmountβ and the noun βmountβ have been used to mean the same thing, throughout this document.
Mount states
A propagation event is defined as event generated on a vfsmount that leads to mount or unmount actions in other vfsmounts.
A peer group is defined as a group of vfsmounts that propagate events to each other.
A given mount can be in one of the following states:
Shared mounts
A shared mount is defined as a vfsmount that belongs to a peer group.
For example:
mount --make-shared /mnt mount --bind /mnt /tmp
The mount at /mnt and that at /tmp are both shared and belong to the same peer group. Anything mounted or unmounted under /mnt or /tmp reflect in all the other mounts of its peer group.
Slave mounts
A slave mount is defined as a vfsmount that receives propagation events and does not forward propagation events.
A slave mount as the name implies has a master mount from which mount/unmount events are received. Events do not propagate from the slave mount to the master. Only a shared mount can be made a slave by executing the following command:
mount --make-slave mount
A shared mount that is made as a slave is no more shared unless modified to become shared.
Shared and Slave
A vfsmount can be both shared as well as slave. This state indicates that the mount is a slave of some vfsmount, and has its own peer group too. This vfsmount receives propagation events from its master vfsmount, and also forwards propagation events to its βpeer groupβ and to its slave vfsmounts.
Strictly speaking, the vfsmount is shared having its own peer group, and this peer-group is a slave of some other peer group.
Only a slave vfsmount can be made as βshared and slaveβ by either executing the following command:
mount --make-shared mount
or by moving the slave vfsmount under a shared vfsmount.
Private mount
A private mount is defined as vfsmount that does not receive or forward any propagation events.
Unbindable mount
A unbindable mount is defined as vfsmount that does not receive or forward any propagation events and cannot be bind mounted.
State diagram:
The state diagram below explains the state transition of a mount, in response to various commands:
----------------------------------------------------------------------- | |make-shared | make-slave | make-private |make-unbindab| --------------|------------|--------------|--------------|-------------| |shared |shared |*slave/private| private | unbindable | | | | | | | |-------------|------------|--------------|--------------|-------------| |slave |shared | **slave | private | unbindable | | |and slave | | | | |-------------|------------|--------------|--------------|-------------| |shared |shared | slave | private | unbindable | |and slave |and slave | | | | |-------------|------------|--------------|--------------|-------------| |private |shared | **private | private | unbindable | |-------------|------------|--------------|--------------|-------------| |unbindable |shared |**unbindable | private | unbindable | ------------------------------------------------------------------------ * if the shared mount is the only mount in its peer group, making it slave, makes it private automatically. Note that there is no master to which it can be slaved to. ** slaving a non-shared mount has no effect on the mount.
Apart from the commands listed below, the βmoveβ operation also changes the state of a mount depending on type of the destination mount. Its explained in section 5d.
Bind semantics
Consider the following command:
mount --bind A/a B/b
where βAβ is the source mount, βaβ is the dentry in the mount βAβ, βBβ is the destination mount and βbβ is the dentry in the destination mount.
The outcome depends on the type of mount of βAβ and βBβ. The table below contains quick reference:
-------------------------------------------------------------------------- | BIND MOUNT OPERATION | |************************************************************************| |source(A)->| shared | private | slave | unbindable | | dest(B) | | | | | | | | | | | | | v | | | | | |************************************************************************| | shared | shared | shared | shared & slave | invalid | | | | | | | |non-shared| shared | private | slave | invalid | **************************************************************************
Details:
βAβ is a shared mount and βBβ is a shared mount. A new mount βCβ which is clone of βAβ, is created. Its root dentry is βaβ . βCβ is mounted on mount βBβ at dentry βbβ. Also new mount βC1β, βC2β, βC3β ... are created and mounted at the dentry βbβ on all mounts where βBβ propagates to. A new propagation tree containing βC1β,..,βCnβ is created. This propagation tree is identical to the propagation tree of βBβ. And finally the peer-group of βCβ is merged with the peer group of βAβ.
βAβ is a private mount and βBβ is a shared mount. A new mount βCβ which is clone of βAβ, is created. Its root dentry is βaβ. βCβ is mounted on mount βBβ at dentry βbβ. Also new mount βC1β, βC2β, βC3β ... are created and mounted at the dentry βbβ on all mounts where βBβ propagates to. A new propagation tree is set containing all new mounts βCβ, βC1β, .., βCnβ with exactly the same configuration as the propagation tree for βBβ.
βAβ is a slave mount of mount βZβ and βBβ is a shared mount. A new mount βCβ which is clone of βAβ, is created. Its root dentry is βaβ . βCβ is mounted on mount βBβ at dentry βbβ. Also new mounts βC1β, βC2β, βC3β ... are created and mounted at the dentry βbβ on all mounts where βBβ propagates to. A new propagation tree containing the new mounts βCβ,βC1β,.. βCnβ is created. This propagation tree is identical to the propagation tree for βBβ. And finally the mount βCβ and its peer group is made the slave of mount βZβ. In other words, mount βCβ is in the state βslave and sharedβ.
βAβ is a unbindable mount and βBβ is a shared mount. This is a invalid operation.
βAβ is a private mount and βBβ is a non-shared(private or slave or unbindable) mount. A new mount βCβ which is clone of βAβ, is created. Its root dentry is βaβ. βCβ is mounted on mount βBβ at dentry βbβ.
βAβ is a shared mount and βBβ is a non-shared mount. A new mount βCβ which is a clone of βAβ is created. Its root dentry is βaβ. βCβ is mounted on mount βBβ at dentry βbβ. βCβ is made a member of the peer-group of βAβ.
βAβ is a slave mount of mount βZβ and βBβ is a non-shared mount. A new mount βCβ which is a clone of βAβ is created. Its root dentry is βaβ. βCβ is mounted on mount βBβ at dentry βbβ. Also βCβ is set as a slave mount of βZβ. In other words βAβ and βCβ are both slave mounts of βZβ. All mount/unmount events on βZβ propagates to βAβ and βCβ. But mount/unmount on βAβ do not propagate anywhere else. Similarly mount/unmount on βCβ do not propagate anywhere else.
βAβ is a unbindable mount and βBβ is a non-shared mount. This is a invalid operation. A unbindable mount cannot be bind mounted.
Rbind semantics
rbind is same as bind. Bind replicates the specified mount. Rbind replicates all the mounts in the tree belonging to the specified mount. Rbind mount is bind mount applied to all the mounts in the tree.
If the source tree that is rbind has some unbindable mounts, then the subtree under the unbindable mount is pruned in the new location.
eg:
letβs say we have the following mount tree:
A / \ B C / \ / \ D E F G
Letβs say all the mount except the mount C in the tree are of a type other than unbindable.
If this tree is rbound to say Z
We will have the following tree at the new location:
Z | A' / B' Note how the tree under C is pruned / \ in the new location. D' E'Move semantics
Consider the following command:
mount --move A B/b
where βAβ is the source mount, βBβ is the destination mount and βbβ is the dentry in the destination mount.
The outcome depends on the type of the mount of βAβ and βBβ. The table below is a quick reference:
--------------------------------------------------------------------------- | MOVE MOUNT OPERATION | |************************************************************************** | source(A)->| shared | private | slave | unbindable | | dest(B) | | | | | | | | | | | | | v | | | | | |************************************************************************** | shared | shared | shared |shared and slave| invalid | | | | | | | |non-shared| shared | private | slave | unbindable | ***************************************************************************
Note
moving a mount residing under a shared mount is invalid.
Details follow:
βAβ is a shared mount and βBβ is a shared mount. The mount βAβ is mounted on mount βBβ at dentry βbβ. Also new mounts βA1β, βA2β...βAnβ are created and mounted at dentry βbβ on all mounts that receive propagation from mount βBβ. A new propagation tree is created in the exact same configuration as that of βBβ. This new propagation tree contains all the new mounts βA1β, βA2β... βAnβ. And this new propagation tree is appended to the already existing propagation tree of βAβ.
βAβ is a private mount and βBβ is a shared mount. The mount βAβ is mounted on mount βBβ at dentry βbβ. Also new mount βA1β, βA2β... βAnβ are created and mounted at dentry βbβ on all mounts that receive propagation from mount βBβ. The mount βAβ becomes a shared mount and a propagation tree is created which is identical to that of βBβ. This new propagation tree contains all the new mounts βA1β, βA2β... βAnβ.
βAβ is a slave mount of mount βZβ and βBβ is a shared mount. The mount βAβ is mounted on mount βBβ at dentry βbβ. Also new mounts βA1β, βA2β... βAnβ are created and mounted at dentry βbβ on all mounts that receive propagation from mount βBβ. A new propagation tree is created in the exact same configuration as that of βBβ. This new propagation tree contains all the new mounts βA1β, βA2β... βAnβ. And this new propagation tree is appended to the already existing propagation tree of βAβ. Mount βAβ continues to be the slave mount of βZβ but it also becomes βsharedβ.
βAβ is a unbindable mount and βBβ is a shared mount. The operation is invalid. Because mounting anything on the shared mount βBβ can create new mounts that get mounted on the mounts that receive propagation from βBβ. And since the mount βAβ is unbindable, cloning it to mount at other mountpoints is not possible.
βAβ is a private mount and βBβ is a non-shared(private or slave or unbindable) mount. The mount βAβ is mounted on mount βBβ at dentry βbβ.
βAβ is a shared mount and βBβ is a non-shared mount. The mount βAβ is mounted on mount βBβ at dentry βbβ. Mount βAβ continues to be a shared mount.
βAβ is a slave mount of mount βZβ and βBβ is a non-shared mount. The mount βAβ is mounted on mount βBβ at dentry βbβ. Mount βAβ continues to be a slave mount of mount βZβ.
βAβ is a unbindable mount and βBβ is a non-shared mount. The mount βAβ is mounted on mount βBβ at dentry βbβ. Mount βAβ continues to be a unbindable mount.
Mount semantics
Consider the following command:
mount device B/b
βBβ is the destination mount and βbβ is the dentry in the destination mount.
The above operation is the same as bind operation with the exception that the source mount is always a private mount.
Unmount semantics
Consider the following command:
umount A
where βAβ is a mount mounted on mount βBβ at dentry βbβ.
If mount βBβ is shared, then all most-recently-mounted mounts at dentry βbβ on mounts that receive propagation from mount βBβ and does not have sub-mounts within them are unmounted.
Example: Letβs say βB1β, βB2β, βB3β are shared mounts that propagate to each other.
letβs say βA1β, βA2β, βA3β are first mounted at dentry βbβ on mount βB1β, βB2β and βB3β respectively.
letβs say βC1β, βC2β, βC3β are next mounted at the same dentry βbβ on mount βB1β, βB2β and βB3β respectively.
if βC1β is unmounted, all the mounts that are most-recently-mounted on βB1β and on the mounts that βB1β propagates-to are unmounted.
βB1β propagates to βB2β and βB3β. And the most recently mounted mount on βB2β at dentry βbβ is βC2β, and that of mount βB3β is βC3β.
So all βC1β, βC2β and βC3β should be unmounted.
If any of βC2β or βC3β has some child mounts, then that mount is not unmounted, but all other mounts are unmounted. However if βC1β is told to be unmounted and βC1β has some sub-mounts, the umount operation is failed entirely.
Clone Namespace
A cloned namespace contains all the mounts as that of the parent namespace.
Letβs say βAβ and βBβ are the corresponding mounts in the parent and the child namespace.
If βAβ is shared, then βBβ is also shared and βAβ and βBβ propagate to each other.
If βAβ is a slave mount of βZβ, then βBβ is also the slave mount of βZβ.
If βAβ is a private mount, then βBβ is a private mount too.
If βAβ is unbindable mount, then βBβ is a unbindable mount too.
6) QuizΒΆ
What is the result of the following command sequence?
mount --bind /mnt /mnt mount --make-shared /mnt mount --bind /mnt /tmp mount --move /tmp /mnt/1
what should be the contents of /mnt /mnt/1 /mnt/1/1 should be? Should they all be identical? or should /mnt and /mnt/1 be identical only?
What is the result of the following command sequence?
mount --make-rshared / mkdir -p /v/1 mount --rbind / /v/1
what should be the content of /v/1/v/1 be?
What is the result of the following command sequence?
mount --bind /mnt /mnt mount --make-shared /mnt mkdir -p /mnt/1/2/3 /mnt/1/test mount --bind /mnt/1 /tmp mount --make-slave /mnt mount --make-shared /mnt mount --bind /mnt/1/2 /tmp1 mount --make-slave /mnt
At this point we have the first mount at /tmp and its root dentry is 1. Letβs call this mount βAβ And then we have a second mount at /tmp1 with root dentry 2. Letβs call this mount βBβ Next we have a third mount at /mnt with root dentry mnt. Letβs call this mount βCβ
βBβ is the slave of βAβ and βCβ is a slave of βBβ A -> B -> C
at this point if we execute the following command:
mount --bind /bin /tmp/test
The mount is attempted on βAβ
will the mount propagate to βBβ and βCβ ?
what would be the contents of /mnt/1/test be?
7) FAQΒΆ
Why is bind mount needed? How is it different from symbolic links?
symbolic links can get stale if the destination mount gets unmounted or moved. Bind mounts continue to exist even if the other mount is unmounted or moved.
Why canβt the shared subtree be implemented using exportfs?
exportfs is a heavyweight way of accomplishing part of what shared subtree can do. I cannot imagine a way to implement the semantics of slave mount using exportfs?
Why is unbindable mount needed?
Letβs say we want to replicate the mount tree at multiple locations within the same subtree.
if one rbind mounts a tree within the same subtree βnβ times the number of mounts created is an exponential function of βnβ. Having unbindable mount can help prune the unneeded bind mounts. Here is an example.
- step 1:
letβs say the root tree has just two directories with one vfsmount:
root / \ tmp usr
And we want to replicate the tree at multiple mountpoints under /root/tmp
- step 2:
mount --make-shared /root mkdir -p /tmp/m1 mount --rbind /root /tmp/m1
the new tree now looks like this:
root / \ tmp usr / m1 / \ tmp usr / m1it has two vfsmounts
- step 3:
mkdir -p /tmp/m2 mount --rbind /root /tmp/m2
the new tree now looks like this:
root / \ tmp usr / \ m1 m2 / \ / \ tmp usr tmp usr / \ / m1 m2 m1 / \ / \ tmp usr tmp usr / / \ m1 m1 m2 / \ tmp usr / \ m1 m2 it has 6 vfsmounts- step 4:
mkdir -p /tmp/m3 mount --rbind /root /tmp/m3
I wonβt draw the tree..but it has 24 vfsmounts
at step i the number of vfsmounts is V[i] = i*V[i-1]. This is an exponential function. And this tree has way more mounts than what we really needed in the first place.
One could use a series of umount at each step to prune out the unneeded mounts. But there is a better solution. Unclonable mounts come in handy here.
- step 1:
letβs say the root tree has just two directories with one vfsmount:
root / \ tmp usr How do we set up the same tree at multiple locations under /root/tmp- step 2:
mount --bind /root/tmp /root/tmp mount --make-rshared /root mount --make-unbindable /root/tmp mkdir -p /tmp/m1 mount --rbind /root /tmp/m1
the new tree now looks like this:
root / \ tmp usr / m1 / \ tmp usr- step 3:
mkdir -p /tmp/m2 mount --rbind /root /tmp/m2
the new tree now looks like this:
root / \ tmp usr / \ m1 m2 / \ / \ tmp usr tmp usr- step 4:
mkdir -p /tmp/m3 mount --rbind /root /tmp/m3
the new tree now looks like this:
root / \ tmp usr / \ \ m1 m2 m3 / \ / \ / \ tmp usr tmp usr tmp usr
8) ImplementationΒΆ
Datastructure
Several new fields are introduced to
struct vfsmount:- ->mnt_share
Links together all the mount to/from which this vfsmount send/receives propagation events.
- ->mnt_slave_list
Links all the mounts to which this vfsmount propagates to.
- ->mnt_slave
Links together all the slaves that its master vfsmount propagates to.
- ->mnt_master
Points to the master vfsmount from which this vfsmount receives propagation.
- ->mnt_flags
Takes two more flags to indicate the propagation status of the vfsmount. MNT_SHARE indicates that the vfsmount is a shared vfsmount. MNT_UNCLONABLE indicates that the vfsmount cannot be replicated.
All the shared vfsmounts in a peer group form a cyclic list through ->mnt_share.
All vfsmounts with the same ->mnt_master form on a cyclic list anchored in ->mnt_master->mnt_slave_list and going through ->mnt_slave.
->mnt_master can point to arbitrary (and possibly different) members of master peer group. To find all immediate slaves of a peer group you need to go through _all_ ->mnt_slave_list of its members. Conceptually itβs just a single set - distribution among the individual lists does not affect propagation or the way propagation tree is modified by operations.
All vfsmounts in a peer group have the same ->mnt_master. If it is non-NULL, they form a contiguous (ordered) segment of slave list.
A example propagation tree looks as shown in the figure below.
Note
Though it looks like a forest, if we consider all the shared mounts as a conceptual entity called βpnodeβ, it becomes a tree.
A <--> B <--> C <---> D /|\ /| |\ / F G J K H I / E<-->K /|\ M L NIn the above figure A,B,C and D all are shared and propagate to each other. βAβ has got 3 slave mounts βEβ βFβ and βGβ βCβ has got 2 slave mounts βJβ and βKβ and βDβ has got two slave mounts βHβ and βIβ. βEβ is also shared with βKβ and they propagate to each other. And βKβ has 3 slaves βMβ, βLβ and βNβ
Aβs ->mnt_share links with the ->mnt_share of βBβ βCβ and βDβ
Aβs ->mnt_slave_list links with ->mnt_slave of βEβ, βKβ, βFβ and βGβ
Eβs ->mnt_share links with ->mnt_share of K
βEβ, βKβ, βFβ, βGβ have their ->mnt_master point to
struct vfsmountof βAββMβ, βLβ, βNβ have their ->mnt_master point to
struct vfsmountof βKβKβs ->mnt_slave_list links with ->mnt_slave of βMβ, βLβ and βNβ
Cβs ->mnt_slave_list links with ->mnt_slave of βJβ and βKβ
J and Kβs ->mnt_master points to
struct vfsmountof Cand finally Dβs ->mnt_slave_list links with ->mnt_slave of βHβ and βIβ
βHβ and βIβ have their ->mnt_master pointing to
struct vfsmountof βDβ.NOTE: The propagation tree is orthogonal to the mount tree.
Locking:
->mnt_share, ->mnt_slave, ->mnt_slave_list, ->mnt_master are protected by namespace_sem (exclusive for modifications, shared for reading).
Normally we have ->mnt_flags modifications serialized by vfsmount_lock. There are two exceptions:
do_add_mount()andclone_mnt(). The former modifies a vfsmount that has not been visible in any shared data structures yet. The latter holds namespace_sem and the only references to vfsmount are in lists that canβt be traversed without namespace_sem.Algorithm:
The crux of the implementation resides in rbind/move operation.
The overall algorithm breaks the operation into 3 phases: (look at
attach_recursive_mnt()andpropagate_mnt())Prepare phase.
For each mount in the source tree:
Create the necessary number of mount trees to be attached to each of the mounts that receive propagation from the destination mount.
Do not attach any of the trees to its destination. However note down its ->mnt_parent and ->mnt_mountpoint
Link all the new mounts to form a propagation tree that is identical to the propagation tree of the destination mount.
If this phase is successful, there should be βnβ new propagation trees; where βnβ is the number of mounts in the source tree. Go to the commit phase
Also there should be βmβ new mount trees, where βmβ is the number of mounts to which the destination mount propagates to.
If any memory allocations fail, go to the abort phase.
Commit phase.
Attach each of the mount trees to their corresponding destination mounts.
Abort phase.
Delete all the newly created trees.
Note
all the propagation related functionality resides in the file pnode.c
version 0.1 (created the initial document, Ram Pai linuxram@us.ibm.com)
version 0.2 (Incorporated comments from Al Viro)