xref: /linux/fs/pnode.c (revision 01b944fe1cd4e21a2a9ed51adbdbafe2d5e905ba)
107b20889SRam Pai /*
207b20889SRam Pai  *  linux/fs/pnode.c
307b20889SRam Pai  *
407b20889SRam Pai  * (C) Copyright IBM Corporation 2005.
507b20889SRam Pai  *	Released under GPL v2.
607b20889SRam Pai  *	Author : Ram Pai (linuxram@us.ibm.com)
707b20889SRam Pai  *
807b20889SRam Pai  */
96b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
1007b20889SRam Pai #include <linux/mount.h>
1107b20889SRam Pai #include <linux/fs.h>
12132c94e3SEric W. Biederman #include <linux/nsproxy.h>
136d59e7f5SAl Viro #include "internal.h"
1407b20889SRam Pai #include "pnode.h"
1507b20889SRam Pai 
1603e06e68SRam Pai /* return the next shared peer mount of @p */
17c937135dSAl Viro static inline struct mount *next_peer(struct mount *p)
1803e06e68SRam Pai {
196776db3dSAl Viro 	return list_entry(p->mnt_share.next, struct mount, mnt_share);
2003e06e68SRam Pai }
2103e06e68SRam Pai 
22c937135dSAl Viro static inline struct mount *first_slave(struct mount *p)
235afe0022SRam Pai {
246776db3dSAl Viro 	return list_entry(p->mnt_slave_list.next, struct mount, mnt_slave);
255afe0022SRam Pai }
265afe0022SRam Pai 
27c937135dSAl Viro static inline struct mount *next_slave(struct mount *p)
285afe0022SRam Pai {
296776db3dSAl Viro 	return list_entry(p->mnt_slave.next, struct mount, mnt_slave);
305afe0022SRam Pai }
315afe0022SRam Pai 
326fc7871fSAl Viro static struct mount *get_peer_under_root(struct mount *mnt,
3397e7e0f7SMiklos Szeredi 					 struct mnt_namespace *ns,
3497e7e0f7SMiklos Szeredi 					 const struct path *root)
3597e7e0f7SMiklos Szeredi {
366fc7871fSAl Viro 	struct mount *m = mnt;
3797e7e0f7SMiklos Szeredi 
3897e7e0f7SMiklos Szeredi 	do {
3997e7e0f7SMiklos Szeredi 		/* Check the namespace first for optimization */
40143c8c91SAl Viro 		if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root))
416fc7871fSAl Viro 			return m;
4297e7e0f7SMiklos Szeredi 
43c937135dSAl Viro 		m = next_peer(m);
446fc7871fSAl Viro 	} while (m != mnt);
4597e7e0f7SMiklos Szeredi 
4697e7e0f7SMiklos Szeredi 	return NULL;
4797e7e0f7SMiklos Szeredi }
4897e7e0f7SMiklos Szeredi 
4997e7e0f7SMiklos Szeredi /*
5097e7e0f7SMiklos Szeredi  * Get ID of closest dominating peer group having a representative
5197e7e0f7SMiklos Szeredi  * under the given root.
5297e7e0f7SMiklos Szeredi  *
5397e7e0f7SMiklos Szeredi  * Caller must hold namespace_sem
5497e7e0f7SMiklos Szeredi  */
556fc7871fSAl Viro int get_dominating_id(struct mount *mnt, const struct path *root)
5697e7e0f7SMiklos Szeredi {
576fc7871fSAl Viro 	struct mount *m;
5897e7e0f7SMiklos Szeredi 
5932301920SAl Viro 	for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
60143c8c91SAl Viro 		struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root);
6197e7e0f7SMiklos Szeredi 		if (d)
6215169fe7SAl Viro 			return d->mnt_group_id;
6397e7e0f7SMiklos Szeredi 	}
6497e7e0f7SMiklos Szeredi 
6597e7e0f7SMiklos Szeredi 	return 0;
6697e7e0f7SMiklos Szeredi }
6797e7e0f7SMiklos Szeredi 
686fc7871fSAl Viro static int do_make_slave(struct mount *mnt)
69a58b0eb8SRam Pai {
7032301920SAl Viro 	struct mount *peer_mnt = mnt, *master = mnt->mnt_master;
71d10e8defSAl Viro 	struct mount *slave_mnt;
72a58b0eb8SRam Pai 
73a58b0eb8SRam Pai 	/*
74a58b0eb8SRam Pai 	 * slave 'mnt' to a peer mount that has the
75796a6b52SAl Viro 	 * same root dentry. If none is available then
76a58b0eb8SRam Pai 	 * slave it to anything that is available.
77a58b0eb8SRam Pai 	 */
78c937135dSAl Viro 	while ((peer_mnt = next_peer(peer_mnt)) != mnt &&
796fc7871fSAl Viro 	       peer_mnt->mnt.mnt_root != mnt->mnt.mnt_root) ;
80a58b0eb8SRam Pai 
81a58b0eb8SRam Pai 	if (peer_mnt == mnt) {
82c937135dSAl Viro 		peer_mnt = next_peer(mnt);
83a58b0eb8SRam Pai 		if (peer_mnt == mnt)
84a58b0eb8SRam Pai 			peer_mnt = NULL;
85a58b0eb8SRam Pai 	}
865d477b60STakashi Iwai 	if (mnt->mnt_group_id && IS_MNT_SHARED(mnt) &&
875d477b60STakashi Iwai 	    list_empty(&mnt->mnt_share))
886fc7871fSAl Viro 		mnt_release_group_id(mnt);
89719f5d7fSMiklos Szeredi 
906776db3dSAl Viro 	list_del_init(&mnt->mnt_share);
9115169fe7SAl Viro 	mnt->mnt_group_id = 0;
92a58b0eb8SRam Pai 
93a58b0eb8SRam Pai 	if (peer_mnt)
94a58b0eb8SRam Pai 		master = peer_mnt;
95a58b0eb8SRam Pai 
96a58b0eb8SRam Pai 	if (master) {
976776db3dSAl Viro 		list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
9832301920SAl Viro 			slave_mnt->mnt_master = master;
996776db3dSAl Viro 		list_move(&mnt->mnt_slave, &master->mnt_slave_list);
1006776db3dSAl Viro 		list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
1016776db3dSAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
102a58b0eb8SRam Pai 	} else {
1036776db3dSAl Viro 		struct list_head *p = &mnt->mnt_slave_list;
104a58b0eb8SRam Pai 		while (!list_empty(p)) {
105b5e61818SPavel Emelianov                         slave_mnt = list_first_entry(p,
1066776db3dSAl Viro 					struct mount, mnt_slave);
1076776db3dSAl Viro 			list_del_init(&slave_mnt->mnt_slave);
108a58b0eb8SRam Pai 			slave_mnt->mnt_master = NULL;
109a58b0eb8SRam Pai 		}
110a58b0eb8SRam Pai 	}
11132301920SAl Viro 	mnt->mnt_master = master;
112fc7be130SAl Viro 	CLEAR_MNT_SHARED(mnt);
113a58b0eb8SRam Pai 	return 0;
114a58b0eb8SRam Pai }
115a58b0eb8SRam Pai 
11699b7db7bSNick Piggin /*
11799b7db7bSNick Piggin  * vfsmount lock must be held for write
11899b7db7bSNick Piggin  */
1190f0afb1dSAl Viro void change_mnt_propagation(struct mount *mnt, int type)
12007b20889SRam Pai {
12103e06e68SRam Pai 	if (type == MS_SHARED) {
122b90fa9aeSRam Pai 		set_mnt_shared(mnt);
123a58b0eb8SRam Pai 		return;
124a58b0eb8SRam Pai 	}
1256fc7871fSAl Viro 	do_make_slave(mnt);
126a58b0eb8SRam Pai 	if (type != MS_SLAVE) {
1276776db3dSAl Viro 		list_del_init(&mnt->mnt_slave);
128d10e8defSAl Viro 		mnt->mnt_master = NULL;
1299676f0c6SRam Pai 		if (type == MS_UNBINDABLE)
1300f0afb1dSAl Viro 			mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
1310b03cfb2SAndries E. Brouwer 		else
1320f0afb1dSAl Viro 			mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
13307b20889SRam Pai 	}
13403e06e68SRam Pai }
135b90fa9aeSRam Pai 
136b90fa9aeSRam Pai /*
137b90fa9aeSRam Pai  * get the next mount in the propagation tree.
138b90fa9aeSRam Pai  * @m: the mount seen last
139b90fa9aeSRam Pai  * @origin: the original mount from where the tree walk initiated
140796a6b52SAl Viro  *
141796a6b52SAl Viro  * Note that peer groups form contiguous segments of slave lists.
142796a6b52SAl Viro  * We rely on that in get_source() to be able to find out if
143796a6b52SAl Viro  * vfsmount found while iterating with propagation_next() is
144796a6b52SAl Viro  * a peer of one we'd found earlier.
145b90fa9aeSRam Pai  */
146c937135dSAl Viro static struct mount *propagation_next(struct mount *m,
147c937135dSAl Viro 					 struct mount *origin)
148b90fa9aeSRam Pai {
1495afe0022SRam Pai 	/* are there any slaves of this mount? */
150143c8c91SAl Viro 	if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
1515afe0022SRam Pai 		return first_slave(m);
1525afe0022SRam Pai 
1535afe0022SRam Pai 	while (1) {
15432301920SAl Viro 		struct mount *master = m->mnt_master;
1555afe0022SRam Pai 
15632301920SAl Viro 		if (master == origin->mnt_master) {
157c937135dSAl Viro 			struct mount *next = next_peer(m);
158c937135dSAl Viro 			return (next == origin) ? NULL : next;
1596776db3dSAl Viro 		} else if (m->mnt_slave.next != &master->mnt_slave_list)
1605afe0022SRam Pai 			return next_slave(m);
1615afe0022SRam Pai 
1625afe0022SRam Pai 		/* back at master */
1635afe0022SRam Pai 		m = master;
1645afe0022SRam Pai 	}
1655afe0022SRam Pai }
1665afe0022SRam Pai 
167f2ebb3a9SAl Viro static struct mount *next_group(struct mount *m, struct mount *origin)
1685afe0022SRam Pai {
169f2ebb3a9SAl Viro 	while (1) {
170f2ebb3a9SAl Viro 		while (1) {
171f2ebb3a9SAl Viro 			struct mount *next;
172f2ebb3a9SAl Viro 			if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
173f2ebb3a9SAl Viro 				return first_slave(m);
174f2ebb3a9SAl Viro 			next = next_peer(m);
175f2ebb3a9SAl Viro 			if (m->mnt_group_id == origin->mnt_group_id) {
176f2ebb3a9SAl Viro 				if (next == origin)
177f2ebb3a9SAl Viro 					return NULL;
178f2ebb3a9SAl Viro 			} else if (m->mnt_slave.next != &next->mnt_slave)
179f2ebb3a9SAl Viro 				break;
180f2ebb3a9SAl Viro 			m = next;
181f2ebb3a9SAl Viro 		}
182f2ebb3a9SAl Viro 		/* m is the last peer */
183f2ebb3a9SAl Viro 		while (1) {
184f2ebb3a9SAl Viro 			struct mount *master = m->mnt_master;
185f2ebb3a9SAl Viro 			if (m->mnt_slave.next != &master->mnt_slave_list)
186f2ebb3a9SAl Viro 				return next_slave(m);
187f2ebb3a9SAl Viro 			m = next_peer(master);
188f2ebb3a9SAl Viro 			if (master->mnt_group_id == origin->mnt_group_id)
189f2ebb3a9SAl Viro 				break;
190f2ebb3a9SAl Viro 			if (master->mnt_slave.next == &m->mnt_slave)
191f2ebb3a9SAl Viro 				break;
192f2ebb3a9SAl Viro 			m = master;
193f2ebb3a9SAl Viro 		}
194f2ebb3a9SAl Viro 		if (m == origin)
195f2ebb3a9SAl Viro 			return NULL;
196f2ebb3a9SAl Viro 	}
1975afe0022SRam Pai }
1985afe0022SRam Pai 
199f2ebb3a9SAl Viro /* all accesses are serialized by namespace_sem */
200f2ebb3a9SAl Viro static struct user_namespace *user_ns;
201f2ebb3a9SAl Viro static struct mount *last_dest, *last_source, *dest_master;
202f2ebb3a9SAl Viro static struct mountpoint *mp;
203f2ebb3a9SAl Viro static struct hlist_head *list;
204f2ebb3a9SAl Viro 
205f2ebb3a9SAl Viro static int propagate_one(struct mount *m)
206f2ebb3a9SAl Viro {
207f2ebb3a9SAl Viro 	struct mount *child;
208f2ebb3a9SAl Viro 	int type;
209f2ebb3a9SAl Viro 	/* skip ones added by this propagate_mnt() */
210f2ebb3a9SAl Viro 	if (IS_MNT_NEW(m))
211f2ebb3a9SAl Viro 		return 0;
212f2ebb3a9SAl Viro 	/* skip if mountpoint isn't covered by it */
213f2ebb3a9SAl Viro 	if (!is_subdir(mp->m_dentry, m->mnt.mnt_root))
214f2ebb3a9SAl Viro 		return 0;
215f2ebb3a9SAl Viro 	if (m->mnt_group_id == last_dest->mnt_group_id) {
216f2ebb3a9SAl Viro 		type = CL_MAKE_SHARED;
217f2ebb3a9SAl Viro 	} else {
218f2ebb3a9SAl Viro 		struct mount *n, *p;
219f2ebb3a9SAl Viro 		for (n = m; ; n = p) {
220f2ebb3a9SAl Viro 			p = n->mnt_master;
221f2ebb3a9SAl Viro 			if (p == dest_master || IS_MNT_MARKED(p)) {
222f2ebb3a9SAl Viro 				while (last_dest->mnt_master != p) {
223f2ebb3a9SAl Viro 					last_source = last_source->mnt_master;
224f2ebb3a9SAl Viro 					last_dest = last_source->mnt_parent;
225f2ebb3a9SAl Viro 				}
226f2ebb3a9SAl Viro 				if (n->mnt_group_id != last_dest->mnt_group_id) {
227f2ebb3a9SAl Viro 					last_source = last_source->mnt_master;
228f2ebb3a9SAl Viro 					last_dest = last_source->mnt_parent;
229f2ebb3a9SAl Viro 				}
230f2ebb3a9SAl Viro 				break;
231b90fa9aeSRam Pai 			}
232796a6b52SAl Viro 		}
233f2ebb3a9SAl Viro 		type = CL_SLAVE;
234796a6b52SAl Viro 		/* beginning of peer group among the slaves? */
235f2ebb3a9SAl Viro 		if (IS_MNT_SHARED(m))
236f2ebb3a9SAl Viro 			type |= CL_MAKE_SHARED;
237f2ebb3a9SAl Viro 	}
238f2ebb3a9SAl Viro 
239f2ebb3a9SAl Viro 	/* Notice when we are propagating across user namespaces */
240f2ebb3a9SAl Viro 	if (m->mnt_ns->user_ns != user_ns)
241f2ebb3a9SAl Viro 		type |= CL_UNPRIVILEGED;
242f2ebb3a9SAl Viro 	child = copy_tree(last_source, last_source->mnt.mnt_root, type);
243f2ebb3a9SAl Viro 	if (IS_ERR(child))
244f2ebb3a9SAl Viro 		return PTR_ERR(child);
2458486a788SEric W. Biederman 	child->mnt.mnt_flags &= ~MNT_LOCKED;
246f2ebb3a9SAl Viro 	mnt_set_mountpoint(m, mp, child);
247f2ebb3a9SAl Viro 	last_dest = m;
248f2ebb3a9SAl Viro 	last_source = child;
249f2ebb3a9SAl Viro 	if (m->mnt_master != dest_master) {
250f2ebb3a9SAl Viro 		read_seqlock_excl(&mount_lock);
251f2ebb3a9SAl Viro 		SET_MNT_MARK(m->mnt_master);
252f2ebb3a9SAl Viro 		read_sequnlock_excl(&mount_lock);
253f2ebb3a9SAl Viro 	}
254f2ebb3a9SAl Viro 	hlist_add_head(&child->mnt_hash, list);
255f2ebb3a9SAl Viro 	return 0;
256796a6b52SAl Viro }
257b90fa9aeSRam Pai 
258b90fa9aeSRam Pai /*
259b90fa9aeSRam Pai  * mount 'source_mnt' under the destination 'dest_mnt' at
260b90fa9aeSRam Pai  * dentry 'dest_dentry'. And propagate that mount to
261b90fa9aeSRam Pai  * all the peer and slave mounts of 'dest_mnt'.
262b90fa9aeSRam Pai  * Link all the new mounts into a propagation tree headed at
263b90fa9aeSRam Pai  * source_mnt. Also link all the new mounts using ->mnt_list
264b90fa9aeSRam Pai  * headed at source_mnt's ->mnt_list
265b90fa9aeSRam Pai  *
266b90fa9aeSRam Pai  * @dest_mnt: destination mount.
267b90fa9aeSRam Pai  * @dest_dentry: destination dentry.
268b90fa9aeSRam Pai  * @source_mnt: source mount.
269b90fa9aeSRam Pai  * @tree_list : list of heads of trees to be attached.
270b90fa9aeSRam Pai  */
27184d17192SAl Viro int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
27238129a13SAl Viro 		    struct mount *source_mnt, struct hlist_head *tree_list)
273b90fa9aeSRam Pai {
274f2ebb3a9SAl Viro 	struct mount *m, *n;
275b90fa9aeSRam Pai 	int ret = 0;
276b90fa9aeSRam Pai 
277f2ebb3a9SAl Viro 	/*
278f2ebb3a9SAl Viro 	 * we don't want to bother passing tons of arguments to
279f2ebb3a9SAl Viro 	 * propagate_one(); everything is serialized by namespace_sem,
280f2ebb3a9SAl Viro 	 * so globals will do just fine.
281f2ebb3a9SAl Viro 	 */
282f2ebb3a9SAl Viro 	user_ns = current->nsproxy->mnt_ns->user_ns;
283f2ebb3a9SAl Viro 	last_dest = dest_mnt;
284f2ebb3a9SAl Viro 	last_source = source_mnt;
285f2ebb3a9SAl Viro 	mp = dest_mp;
286f2ebb3a9SAl Viro 	list = tree_list;
287f2ebb3a9SAl Viro 	dest_master = dest_mnt->mnt_master;
288b90fa9aeSRam Pai 
289f2ebb3a9SAl Viro 	/* all peers of dest_mnt, except dest_mnt itself */
290f2ebb3a9SAl Viro 	for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
291f2ebb3a9SAl Viro 		ret = propagate_one(n);
292f2ebb3a9SAl Viro 		if (ret)
293b90fa9aeSRam Pai 			goto out;
294b90fa9aeSRam Pai 	}
295b90fa9aeSRam Pai 
296f2ebb3a9SAl Viro 	/* all slave groups */
297f2ebb3a9SAl Viro 	for (m = next_group(dest_mnt, dest_mnt); m;
298f2ebb3a9SAl Viro 			m = next_group(m, dest_mnt)) {
299f2ebb3a9SAl Viro 		/* everything in that slave group */
300f2ebb3a9SAl Viro 		n = m;
301f2ebb3a9SAl Viro 		do {
302f2ebb3a9SAl Viro 			ret = propagate_one(n);
303f2ebb3a9SAl Viro 			if (ret)
304f2ebb3a9SAl Viro 				goto out;
305f2ebb3a9SAl Viro 			n = next_peer(n);
306f2ebb3a9SAl Viro 		} while (n != m);
307b90fa9aeSRam Pai 	}
308b90fa9aeSRam Pai out:
309f2ebb3a9SAl Viro 	read_seqlock_excl(&mount_lock);
310f2ebb3a9SAl Viro 	hlist_for_each_entry(n, tree_list, mnt_hash) {
311f2ebb3a9SAl Viro 		m = n->mnt_parent;
312f2ebb3a9SAl Viro 		if (m->mnt_master != dest_mnt->mnt_master)
313f2ebb3a9SAl Viro 			CLEAR_MNT_MARK(m->mnt_master);
314b90fa9aeSRam Pai 	}
315f2ebb3a9SAl Viro 	read_sequnlock_excl(&mount_lock);
316b90fa9aeSRam Pai 	return ret;
317b90fa9aeSRam Pai }
318a05964f3SRam Pai 
319a05964f3SRam Pai /*
320a05964f3SRam Pai  * return true if the refcount is greater than count
321a05964f3SRam Pai  */
3221ab59738SAl Viro static inline int do_refcount_check(struct mount *mnt, int count)
323a05964f3SRam Pai {
324aba809cfSAl Viro 	return mnt_get_count(mnt) > count;
325a05964f3SRam Pai }
326a05964f3SRam Pai 
327a05964f3SRam Pai /*
328a05964f3SRam Pai  * check if the mount 'mnt' can be unmounted successfully.
329a05964f3SRam Pai  * @mnt: the mount to be checked for unmount
330a05964f3SRam Pai  * NOTE: unmounting 'mnt' would naturally propagate to all
331a05964f3SRam Pai  * other mounts its parent propagates to.
332a05964f3SRam Pai  * Check if any of these mounts that **do not have submounts**
333a05964f3SRam Pai  * have more references than 'refcnt'. If so return busy.
33499b7db7bSNick Piggin  *
335b3e19d92SNick Piggin  * vfsmount lock must be held for write
336a05964f3SRam Pai  */
3371ab59738SAl Viro int propagate_mount_busy(struct mount *mnt, int refcnt)
338a05964f3SRam Pai {
339c937135dSAl Viro 	struct mount *m, *child;
3400714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
341a05964f3SRam Pai 	int ret = 0;
342a05964f3SRam Pai 
3430714a533SAl Viro 	if (mnt == parent)
344a05964f3SRam Pai 		return do_refcount_check(mnt, refcnt);
345a05964f3SRam Pai 
346a05964f3SRam Pai 	/*
347a05964f3SRam Pai 	 * quickly check if the current mount can be unmounted.
348a05964f3SRam Pai 	 * If not, we don't have to go checking for all other
349a05964f3SRam Pai 	 * mounts
350a05964f3SRam Pai 	 */
3516b41d536SAl Viro 	if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
352a05964f3SRam Pai 		return 1;
353a05964f3SRam Pai 
354c937135dSAl Viro 	for (m = propagation_next(parent, parent); m;
355c937135dSAl Viro 	     		m = propagation_next(m, parent)) {
356474279dcSAl Viro 		child = __lookup_mnt_last(&m->mnt, mnt->mnt_mountpoint);
3576b41d536SAl Viro 		if (child && list_empty(&child->mnt_mounts) &&
3581ab59738SAl Viro 		    (ret = do_refcount_check(child, 1)))
359a05964f3SRam Pai 			break;
360a05964f3SRam Pai 	}
361a05964f3SRam Pai 	return ret;
362a05964f3SRam Pai }
363a05964f3SRam Pai 
364a05964f3SRam Pai /*
3655d88457eSEric W. Biederman  * Clear MNT_LOCKED when it can be shown to be safe.
3665d88457eSEric W. Biederman  *
3675d88457eSEric W. Biederman  * mount_lock lock must be held for write
3685d88457eSEric W. Biederman  */
3695d88457eSEric W. Biederman void propagate_mount_unlock(struct mount *mnt)
3705d88457eSEric W. Biederman {
3715d88457eSEric W. Biederman 	struct mount *parent = mnt->mnt_parent;
3725d88457eSEric W. Biederman 	struct mount *m, *child;
3735d88457eSEric W. Biederman 
3745d88457eSEric W. Biederman 	BUG_ON(parent == mnt);
3755d88457eSEric W. Biederman 
3765d88457eSEric W. Biederman 	for (m = propagation_next(parent, parent); m;
3775d88457eSEric W. Biederman 			m = propagation_next(m, parent)) {
3785d88457eSEric W. Biederman 		child = __lookup_mnt_last(&m->mnt, mnt->mnt_mountpoint);
3795d88457eSEric W. Biederman 		if (child)
3805d88457eSEric W. Biederman 			child->mnt.mnt_flags &= ~MNT_LOCKED;
3815d88457eSEric W. Biederman 	}
3825d88457eSEric W. Biederman }
3835d88457eSEric W. Biederman 
3845d88457eSEric W. Biederman /*
3850c56fe31SEric W. Biederman  * Mark all mounts that the MNT_LOCKED logic will allow to be unmounted.
3860c56fe31SEric W. Biederman  */
3870c56fe31SEric W. Biederman static void mark_umount_candidates(struct mount *mnt)
3880c56fe31SEric W. Biederman {
3890c56fe31SEric W. Biederman 	struct mount *parent = mnt->mnt_parent;
3900c56fe31SEric W. Biederman 	struct mount *m;
3910c56fe31SEric W. Biederman 
3920c56fe31SEric W. Biederman 	BUG_ON(parent == mnt);
3930c56fe31SEric W. Biederman 
3940c56fe31SEric W. Biederman 	for (m = propagation_next(parent, parent); m;
3950c56fe31SEric W. Biederman 			m = propagation_next(m, parent)) {
3960c56fe31SEric W. Biederman 		struct mount *child = __lookup_mnt_last(&m->mnt,
3970c56fe31SEric W. Biederman 						mnt->mnt_mountpoint);
3980c56fe31SEric W. Biederman 		if (child && (!IS_MNT_LOCKED(child) || IS_MNT_MARKED(m))) {
3990c56fe31SEric W. Biederman 			SET_MNT_MARK(child);
4000c56fe31SEric W. Biederman 		}
4010c56fe31SEric W. Biederman 	}
4020c56fe31SEric W. Biederman }
4030c56fe31SEric W. Biederman 
4040c56fe31SEric W. Biederman /*
405a05964f3SRam Pai  * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
406a05964f3SRam Pai  * parent propagates to.
407a05964f3SRam Pai  */
40861ef47b1SAl Viro static void __propagate_umount(struct mount *mnt)
409a05964f3SRam Pai {
4100714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
411c937135dSAl Viro 	struct mount *m;
412a05964f3SRam Pai 
4130714a533SAl Viro 	BUG_ON(parent == mnt);
414a05964f3SRam Pai 
415c937135dSAl Viro 	for (m = propagation_next(parent, parent); m;
416c937135dSAl Viro 			m = propagation_next(m, parent)) {
417a05964f3SRam Pai 
418474279dcSAl Viro 		struct mount *child = __lookup_mnt_last(&m->mnt,
419474279dcSAl Viro 						mnt->mnt_mountpoint);
420a05964f3SRam Pai 		/*
4210c56fe31SEric W. Biederman 		 * umount the child only if the child has no children
4220c56fe31SEric W. Biederman 		 * and the child is marked safe to unmount.
423a05964f3SRam Pai 		 */
4240c56fe31SEric W. Biederman 		if (!child || !IS_MNT_MARKED(child))
4250c56fe31SEric W. Biederman 			continue;
4260c56fe31SEric W. Biederman 		CLEAR_MNT_MARK(child);
4270c56fe31SEric W. Biederman 		if (list_empty(&child->mnt_mounts)) {
42888b368f2SAl Viro 			list_del_init(&child->mnt_child);
429590ce4bcSEric W. Biederman 			child->mnt.mnt_flags |= MNT_UMOUNT;
430c003b26fSEric W. Biederman 			list_move_tail(&child->mnt_list, &mnt->mnt_list);
43138129a13SAl Viro 		}
432a05964f3SRam Pai 	}
433a05964f3SRam Pai }
434a05964f3SRam Pai 
435a05964f3SRam Pai /*
436a05964f3SRam Pai  * collect all mounts that receive propagation from the mount in @list,
437a05964f3SRam Pai  * and return these additional mounts in the same list.
438a05964f3SRam Pai  * @list: the list of mounts to be unmounted.
43999b7db7bSNick Piggin  *
44099b7db7bSNick Piggin  * vfsmount lock must be held for write
441a05964f3SRam Pai  */
442c003b26fSEric W. Biederman int propagate_umount(struct list_head *list)
443a05964f3SRam Pai {
44461ef47b1SAl Viro 	struct mount *mnt;
445a05964f3SRam Pai 
4460c56fe31SEric W. Biederman 	list_for_each_entry_reverse(mnt, list, mnt_list)
4470c56fe31SEric W. Biederman 		mark_umount_candidates(mnt);
4480c56fe31SEric W. Biederman 
449c003b26fSEric W. Biederman 	list_for_each_entry(mnt, list, mnt_list)
450a05964f3SRam Pai 		__propagate_umount(mnt);
451a05964f3SRam Pai 	return 0;
452a05964f3SRam Pai }
453