4 * (C) Copyright IBM Corporation 2005.
5 * Released under GPL v2.
6 * Author : Ram Pai (linuxram@us.ibm.com)
9 #include <linux/namespace.h>
10 #include <linux/mount.h>
14 /* return the next shared peer mount of @p */
15 static inline struct vfsmount *next_peer(struct vfsmount *p)
17 return list_entry(p->mnt_share.next, struct vfsmount, mnt_share);
20 static int do_make_slave(struct vfsmount *mnt)
22 struct vfsmount *peer_mnt = mnt, *master = mnt->mnt_master;
23 struct vfsmount *slave_mnt;
26 * slave 'mnt' to a peer mount that has the
27 * same root dentry. If none is available than
28 * slave it to anything that is available.
30 while ((peer_mnt = next_peer(peer_mnt)) != mnt &&
31 peer_mnt->mnt_root != mnt->mnt_root) ;
33 if (peer_mnt == mnt) {
34 peer_mnt = next_peer(mnt);
38 list_del_init(&mnt->mnt_share);
44 list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
45 slave_mnt->mnt_master = master;
46 list_del(&mnt->mnt_slave);
47 list_add(&mnt->mnt_slave, &master->mnt_slave_list);
48 list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
49 INIT_LIST_HEAD(&mnt->mnt_slave_list);
51 struct list_head *p = &mnt->mnt_slave_list;
52 while (!list_empty(p)) {
53 slave_mnt = list_entry(p->next,
54 struct vfsmount, mnt_slave);
55 list_del_init(&slave_mnt->mnt_slave);
56 slave_mnt->mnt_master = NULL;
59 mnt->mnt_master = master;
60 CLEAR_MNT_SHARED(mnt);
61 INIT_LIST_HEAD(&mnt->mnt_slave_list);
65 void change_mnt_propagation(struct vfsmount *mnt, int type)
67 if (type == MS_SHARED) {
72 if (type != MS_SLAVE) {
73 list_del_init(&mnt->mnt_slave);
74 mnt->mnt_master = NULL;
79 * get the next mount in the propagation tree.
80 * @m: the mount seen last
81 * @origin: the original mount from where the tree walk initiated
83 static struct vfsmount *propagation_next(struct vfsmount *m,
84 struct vfsmount *origin)
93 * mount 'source_mnt' under the destination 'dest_mnt' at
94 * dentry 'dest_dentry'. And propagate that mount to
95 * all the peer and slave mounts of 'dest_mnt'.
96 * Link all the new mounts into a propagation tree headed at
97 * source_mnt. Also link all the new mounts using ->mnt_list
98 * headed at source_mnt's ->mnt_list
100 * @dest_mnt: destination mount.
101 * @dest_dentry: destination dentry.
102 * @source_mnt: source mount.
103 * @tree_list : list of heads of trees to be attached.
105 int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry,
106 struct vfsmount *source_mnt, struct list_head *tree_list)
108 struct vfsmount *m, *child;
110 struct vfsmount *prev_dest_mnt = dest_mnt;
111 struct vfsmount *prev_src_mnt = source_mnt;
113 LIST_HEAD(umount_list);
115 for (m = propagation_next(dest_mnt, dest_mnt); m;
116 m = propagation_next(m, dest_mnt)) {
117 int type = CL_PROPAGATION;
122 if (IS_MNT_SHARED(m))
123 type |= CL_MAKE_SHARED;
125 if (!(child = copy_tree(source_mnt, source_mnt->mnt_root,
128 list_splice(tree_list, tmp_list.prev);
132 if (is_subdir(dest_dentry, m->mnt_root)) {
133 mnt_set_mountpoint(m, dest_dentry, child);
134 list_add_tail(&child->mnt_hash, tree_list);
137 * This can happen if the parent mount was bind mounted
138 * on some subdirectory of a shared/slave mount.
140 list_add_tail(&child->mnt_hash, &tmp_list);
143 prev_src_mnt = child;
146 spin_lock(&vfsmount_lock);
147 while (!list_empty(&tmp_list)) {
148 child = list_entry(tmp_list.next, struct vfsmount, mnt_hash);
149 list_del_init(&child->mnt_hash);
150 umount_tree(child, 0, &umount_list);
152 spin_unlock(&vfsmount_lock);
153 release_mounts(&umount_list);
158 * return true if the refcount is greater than count
160 static inline int do_refcount_check(struct vfsmount *mnt, int count)
162 int mycount = atomic_read(&mnt->mnt_count);
163 return (mycount > count);
167 * check if the mount 'mnt' can be unmounted successfully.
168 * @mnt: the mount to be checked for unmount
169 * NOTE: unmounting 'mnt' would naturally propagate to all
170 * other mounts its parent propagates to.
171 * Check if any of these mounts that **do not have submounts**
172 * have more references than 'refcnt'. If so return busy.
174 int propagate_mount_busy(struct vfsmount *mnt, int refcnt)
176 struct vfsmount *m, *child;
177 struct vfsmount *parent = mnt->mnt_parent;
181 return do_refcount_check(mnt, refcnt);
184 * quickly check if the current mount can be unmounted.
185 * If not, we don't have to go checking for all other
188 if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
191 for (m = propagation_next(parent, parent); m;
192 m = propagation_next(m, parent)) {
193 child = __lookup_mnt(m, mnt->mnt_mountpoint, 0);
194 if (child && list_empty(&child->mnt_mounts) &&
195 (ret = do_refcount_check(child, 1)))
202 * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
203 * parent propagates to.
205 static void __propagate_umount(struct vfsmount *mnt)
207 struct vfsmount *parent = mnt->mnt_parent;
210 BUG_ON(parent == mnt);
212 for (m = propagation_next(parent, parent); m;
213 m = propagation_next(m, parent)) {
215 struct vfsmount *child = __lookup_mnt(m,
216 mnt->mnt_mountpoint, 0);
218 * umount the child only if the child has no
221 if (child && list_empty(&child->mnt_mounts)) {
222 list_del(&child->mnt_hash);
223 list_add_tail(&child->mnt_hash, &mnt->mnt_hash);
229 * collect all mounts that receive propagation from the mount in @list,
230 * and return these additional mounts in the same list.
231 * @list: the list of mounts to be unmounted.
233 int propagate_umount(struct list_head *list)
235 struct vfsmount *mnt;
237 list_for_each_entry(mnt, list, mnt_hash)
238 __propagate_umount(mnt);