xref: /linux-6.15/fs/pnode.c (revision d1ddc6f1)
159bd9dedSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
207b20889SRam Pai /*
307b20889SRam Pai  *  linux/fs/pnode.c
407b20889SRam Pai  *
507b20889SRam Pai  * (C) Copyright IBM Corporation 2005.
607b20889SRam Pai  *	Author : Ram Pai ([email protected])
707b20889SRam Pai  */
86b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
907b20889SRam Pai #include <linux/mount.h>
1007b20889SRam Pai #include <linux/fs.h>
11132c94e3SEric W. Biederman #include <linux/nsproxy.h>
12e262e32dSDavid Howells #include <uapi/linux/mount.h>
136d59e7f5SAl Viro #include "internal.h"
1407b20889SRam Pai #include "pnode.h"
1507b20889SRam Pai 
1603e06e68SRam Pai /* return the next shared peer mount of @p */
next_peer(struct mount * 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 
first_slave(struct mount * p)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 
last_slave(struct mount * p)27296990deSEric W. Biederman static inline struct mount *last_slave(struct mount *p)
28296990deSEric W. Biederman {
29296990deSEric W. Biederman 	return list_entry(p->mnt_slave_list.prev, struct mount, mnt_slave);
30296990deSEric W. Biederman }
31296990deSEric W. Biederman 
next_slave(struct mount * p)32c937135dSAl Viro static inline struct mount *next_slave(struct mount *p)
335afe0022SRam Pai {
346776db3dSAl Viro 	return list_entry(p->mnt_slave.next, struct mount, mnt_slave);
355afe0022SRam Pai }
365afe0022SRam Pai 
get_peer_under_root(struct mount * mnt,struct mnt_namespace * ns,const struct path * root)376fc7871fSAl Viro static struct mount *get_peer_under_root(struct mount *mnt,
3897e7e0f7SMiklos Szeredi 					 struct mnt_namespace *ns,
3997e7e0f7SMiklos Szeredi 					 const struct path *root)
4097e7e0f7SMiklos Szeredi {
416fc7871fSAl Viro 	struct mount *m = mnt;
4297e7e0f7SMiklos Szeredi 
4397e7e0f7SMiklos Szeredi 	do {
4497e7e0f7SMiklos Szeredi 		/* Check the namespace first for optimization */
45143c8c91SAl Viro 		if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root))
466fc7871fSAl Viro 			return m;
4797e7e0f7SMiklos Szeredi 
48c937135dSAl Viro 		m = next_peer(m);
496fc7871fSAl Viro 	} while (m != mnt);
5097e7e0f7SMiklos Szeredi 
5197e7e0f7SMiklos Szeredi 	return NULL;
5297e7e0f7SMiklos Szeredi }
5397e7e0f7SMiklos Szeredi 
5497e7e0f7SMiklos Szeredi /*
5597e7e0f7SMiklos Szeredi  * Get ID of closest dominating peer group having a representative
5697e7e0f7SMiklos Szeredi  * under the given root.
5797e7e0f7SMiklos Szeredi  *
5897e7e0f7SMiklos Szeredi  * Caller must hold namespace_sem
5997e7e0f7SMiklos Szeredi  */
get_dominating_id(struct mount * mnt,const struct path * root)606fc7871fSAl Viro int get_dominating_id(struct mount *mnt, const struct path *root)
6197e7e0f7SMiklos Szeredi {
626fc7871fSAl Viro 	struct mount *m;
6397e7e0f7SMiklos Szeredi 
6432301920SAl Viro 	for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
65143c8c91SAl Viro 		struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root);
6697e7e0f7SMiklos Szeredi 		if (d)
6715169fe7SAl Viro 			return d->mnt_group_id;
6897e7e0f7SMiklos Szeredi 	}
6997e7e0f7SMiklos Szeredi 
7097e7e0f7SMiklos Szeredi 	return 0;
7197e7e0f7SMiklos Szeredi }
7297e7e0f7SMiklos Szeredi 
do_make_slave(struct mount * mnt)736fc7871fSAl Viro static int do_make_slave(struct mount *mnt)
74a58b0eb8SRam Pai {
755235d448SAl Viro 	struct mount *master, *slave_mnt;
76a58b0eb8SRam Pai 
775235d448SAl Viro 	if (list_empty(&mnt->mnt_share)) {
785235d448SAl Viro 		if (IS_MNT_SHARED(mnt)) {
796fc7871fSAl Viro 			mnt_release_group_id(mnt);
805235d448SAl Viro 			CLEAR_MNT_SHARED(mnt);
815235d448SAl Viro 		}
825235d448SAl Viro 		master = mnt->mnt_master;
835235d448SAl Viro 		if (!master) {
846776db3dSAl Viro 			struct list_head *p = &mnt->mnt_slave_list;
85a58b0eb8SRam Pai 			while (!list_empty(p)) {
86b5e61818SPavel Emelianov 				slave_mnt = list_first_entry(p,
876776db3dSAl Viro 						struct mount, mnt_slave);
886776db3dSAl Viro 				list_del_init(&slave_mnt->mnt_slave);
89a58b0eb8SRam Pai 				slave_mnt->mnt_master = NULL;
90a58b0eb8SRam Pai 			}
915235d448SAl Viro 			return 0;
92a58b0eb8SRam Pai 		}
935235d448SAl Viro 	} else {
945235d448SAl Viro 		struct mount *m;
955235d448SAl Viro 		/*
965235d448SAl Viro 		 * slave 'mnt' to a peer mount that has the
975235d448SAl Viro 		 * same root dentry. If none is available then
985235d448SAl Viro 		 * slave it to anything that is available.
995235d448SAl Viro 		 */
1005235d448SAl Viro 		for (m = master = next_peer(mnt); m != mnt; m = next_peer(m)) {
1015235d448SAl Viro 			if (m->mnt.mnt_root == mnt->mnt.mnt_root) {
1025235d448SAl Viro 				master = m;
1035235d448SAl Viro 				break;
1045235d448SAl Viro 			}
1055235d448SAl Viro 		}
1065235d448SAl Viro 		list_del_init(&mnt->mnt_share);
1075235d448SAl Viro 		mnt->mnt_group_id = 0;
108fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
1095235d448SAl Viro 	}
1105235d448SAl Viro 	list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
1115235d448SAl Viro 		slave_mnt->mnt_master = master;
1125235d448SAl Viro 	list_move(&mnt->mnt_slave, &master->mnt_slave_list);
1135235d448SAl Viro 	list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
1145235d448SAl Viro 	INIT_LIST_HEAD(&mnt->mnt_slave_list);
1155235d448SAl Viro 	mnt->mnt_master = master;
116a58b0eb8SRam Pai 	return 0;
117a58b0eb8SRam Pai }
118a58b0eb8SRam Pai 
11999b7db7bSNick Piggin /*
12099b7db7bSNick Piggin  * vfsmount lock must be held for write
12199b7db7bSNick Piggin  */
change_mnt_propagation(struct mount * mnt,int type)1220f0afb1dSAl Viro void change_mnt_propagation(struct mount *mnt, int type)
12307b20889SRam Pai {
12403e06e68SRam Pai 	if (type == MS_SHARED) {
125b90fa9aeSRam Pai 		set_mnt_shared(mnt);
126a58b0eb8SRam Pai 		return;
127a58b0eb8SRam Pai 	}
1286fc7871fSAl Viro 	do_make_slave(mnt);
129a58b0eb8SRam Pai 	if (type != MS_SLAVE) {
1306776db3dSAl Viro 		list_del_init(&mnt->mnt_slave);
131d10e8defSAl Viro 		mnt->mnt_master = NULL;
1329676f0c6SRam Pai 		if (type == MS_UNBINDABLE)
1330f0afb1dSAl Viro 			mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
1340b03cfb2SAndries E. Brouwer 		else
1350f0afb1dSAl Viro 			mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
13607b20889SRam Pai 	}
13703e06e68SRam Pai }
138b90fa9aeSRam Pai 
139b90fa9aeSRam Pai /*
140b90fa9aeSRam Pai  * get the next mount in the propagation tree.
141b90fa9aeSRam Pai  * @m: the mount seen last
142b90fa9aeSRam Pai  * @origin: the original mount from where the tree walk initiated
143796a6b52SAl Viro  *
144796a6b52SAl Viro  * Note that peer groups form contiguous segments of slave lists.
145796a6b52SAl Viro  * We rely on that in get_source() to be able to find out if
146796a6b52SAl Viro  * vfsmount found while iterating with propagation_next() is
147796a6b52SAl Viro  * a peer of one we'd found earlier.
148b90fa9aeSRam Pai  */
propagation_next(struct mount * m,struct mount * origin)149c937135dSAl Viro static struct mount *propagation_next(struct mount *m,
150c937135dSAl Viro 					 struct mount *origin)
151b90fa9aeSRam Pai {
1525afe0022SRam Pai 	/* are there any slaves of this mount? */
153*d1ddc6f1SAl Viro 	if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
1545afe0022SRam Pai 		return first_slave(m);
1555afe0022SRam Pai 
1565afe0022SRam Pai 	while (1) {
15732301920SAl Viro 		struct mount *master = m->mnt_master;
1585afe0022SRam Pai 
15932301920SAl Viro 		if (master == origin->mnt_master) {
160c937135dSAl Viro 			struct mount *next = next_peer(m);
161c937135dSAl Viro 			return (next == origin) ? NULL : next;
1626776db3dSAl Viro 		} else if (m->mnt_slave.next != &master->mnt_slave_list)
1635afe0022SRam Pai 			return next_slave(m);
1645afe0022SRam Pai 
1655afe0022SRam Pai 		/* back at master */
1665afe0022SRam Pai 		m = master;
1675afe0022SRam Pai 	}
1685afe0022SRam Pai }
1695afe0022SRam Pai 
skip_propagation_subtree(struct mount * m,struct mount * origin)170296990deSEric W. Biederman static struct mount *skip_propagation_subtree(struct mount *m,
171296990deSEric W. Biederman 						struct mount *origin)
172296990deSEric W. Biederman {
173296990deSEric W. Biederman 	/*
174296990deSEric W. Biederman 	 * Advance m such that propagation_next will not return
175296990deSEric W. Biederman 	 * the slaves of m.
176296990deSEric W. Biederman 	 */
177*d1ddc6f1SAl Viro 	if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
178296990deSEric W. Biederman 		m = last_slave(m);
179296990deSEric W. Biederman 
180296990deSEric W. Biederman 	return m;
181296990deSEric W. Biederman }
182296990deSEric W. Biederman 
next_group(struct mount * m,struct mount * origin)183f2ebb3a9SAl Viro static struct mount *next_group(struct mount *m, struct mount *origin)
1845afe0022SRam Pai {
185f2ebb3a9SAl Viro 	while (1) {
186f2ebb3a9SAl Viro 		while (1) {
187f2ebb3a9SAl Viro 			struct mount *next;
188*d1ddc6f1SAl Viro 			if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
189f2ebb3a9SAl Viro 				return first_slave(m);
190f2ebb3a9SAl Viro 			next = next_peer(m);
191f2ebb3a9SAl Viro 			if (m->mnt_group_id == origin->mnt_group_id) {
192f2ebb3a9SAl Viro 				if (next == origin)
193f2ebb3a9SAl Viro 					return NULL;
194f2ebb3a9SAl Viro 			} else if (m->mnt_slave.next != &next->mnt_slave)
195f2ebb3a9SAl Viro 				break;
196f2ebb3a9SAl Viro 			m = next;
197f2ebb3a9SAl Viro 		}
198f2ebb3a9SAl Viro 		/* m is the last peer */
199f2ebb3a9SAl Viro 		while (1) {
200f2ebb3a9SAl Viro 			struct mount *master = m->mnt_master;
201f2ebb3a9SAl Viro 			if (m->mnt_slave.next != &master->mnt_slave_list)
202f2ebb3a9SAl Viro 				return next_slave(m);
203f2ebb3a9SAl Viro 			m = next_peer(master);
204f2ebb3a9SAl Viro 			if (master->mnt_group_id == origin->mnt_group_id)
205f2ebb3a9SAl Viro 				break;
206f2ebb3a9SAl Viro 			if (master->mnt_slave.next == &m->mnt_slave)
207f2ebb3a9SAl Viro 				break;
208f2ebb3a9SAl Viro 			m = master;
209f2ebb3a9SAl Viro 		}
210f2ebb3a9SAl Viro 		if (m == origin)
211f2ebb3a9SAl Viro 			return NULL;
212f2ebb3a9SAl Viro 	}
2135afe0022SRam Pai }
2145afe0022SRam Pai 
215f2ebb3a9SAl Viro /* all accesses are serialized by namespace_sem */
2165ec0811dSEric W. Biederman static struct mount *last_dest, *first_source, *last_source, *dest_master;
217f2ebb3a9SAl Viro static struct hlist_head *list;
218f2ebb3a9SAl Viro 
peers(const struct mount * m1,const struct mount * m2)2196ac39281SChristian Brauner static inline bool peers(const struct mount *m1, const struct mount *m2)
2207ae8fd03SMaxim Patlasov {
2217ae8fd03SMaxim Patlasov 	return m1->mnt_group_id == m2->mnt_group_id && m1->mnt_group_id;
2227ae8fd03SMaxim Patlasov }
2237ae8fd03SMaxim Patlasov 
propagate_one(struct mount * m,struct mountpoint * dest_mp)2244ea2a8d8SChristian Brauner static int propagate_one(struct mount *m, struct mountpoint *dest_mp)
225f2ebb3a9SAl Viro {
226f2ebb3a9SAl Viro 	struct mount *child;
227f2ebb3a9SAl Viro 	int type;
228f2ebb3a9SAl Viro 	/* skip ones added by this propagate_mnt() */
229*d1ddc6f1SAl Viro 	if (IS_MNT_NEW(m))
230f2ebb3a9SAl Viro 		return 0;
231*d1ddc6f1SAl Viro 	/* skip if mountpoint isn't visible in m */
2324ea2a8d8SChristian Brauner 	if (!is_subdir(dest_mp->m_dentry, m->mnt.mnt_root))
233f2ebb3a9SAl Viro 		return 0;
234*d1ddc6f1SAl Viro 	/* skip if m is in the anon_ns we are emptying */
235*d1ddc6f1SAl Viro 	if (m->mnt_ns->mntns_flags & MNTNS_PROPAGATING)
236*d1ddc6f1SAl Viro 		return 0;
237*d1ddc6f1SAl Viro 
2387ae8fd03SMaxim Patlasov 	if (peers(m, last_dest)) {
239f2ebb3a9SAl Viro 		type = CL_MAKE_SHARED;
240f2ebb3a9SAl Viro 	} else {
241f2ebb3a9SAl Viro 		struct mount *n, *p;
2425ec0811dSEric W. Biederman 		bool done;
243f2ebb3a9SAl Viro 		for (n = m; ; n = p) {
244f2ebb3a9SAl Viro 			p = n->mnt_master;
2455ec0811dSEric W. Biederman 			if (p == dest_master || IS_MNT_MARKED(p))
246f2ebb3a9SAl Viro 				break;
247b90fa9aeSRam Pai 		}
2485ec0811dSEric W. Biederman 		do {
2495ec0811dSEric W. Biederman 			struct mount *parent = last_source->mnt_parent;
25011933cf1SChristian Brauner 			if (peers(last_source, first_source))
2515ec0811dSEric W. Biederman 				break;
2525ec0811dSEric W. Biederman 			done = parent->mnt_master == p;
2535ec0811dSEric W. Biederman 			if (done && peers(n, parent))
2545ec0811dSEric W. Biederman 				break;
2555ec0811dSEric W. Biederman 			last_source = last_source->mnt_master;
2565ec0811dSEric W. Biederman 		} while (!done);
2575ec0811dSEric W. Biederman 
258f2ebb3a9SAl Viro 		type = CL_SLAVE;
259796a6b52SAl Viro 		/* beginning of peer group among the slaves? */
260f2ebb3a9SAl Viro 		if (IS_MNT_SHARED(m))
261f2ebb3a9SAl Viro 			type |= CL_MAKE_SHARED;
262f2ebb3a9SAl Viro 	}
263f2ebb3a9SAl Viro 
264f2ebb3a9SAl Viro 	child = copy_tree(last_source, last_source->mnt.mnt_root, type);
265f2ebb3a9SAl Viro 	if (IS_ERR(child))
266f2ebb3a9SAl Viro 		return PTR_ERR(child);
267f2ebb3a9SAl Viro 	read_seqlock_excl(&mount_lock);
2684ea2a8d8SChristian Brauner 	mnt_set_mountpoint(m, dest_mp, child);
269b0d3869cSAl Viro 	if (m->mnt_master != dest_master)
270f2ebb3a9SAl Viro 		SET_MNT_MARK(m->mnt_master);
271f2ebb3a9SAl Viro 	read_sequnlock_excl(&mount_lock);
272b0d3869cSAl Viro 	last_dest = m;
273b0d3869cSAl Viro 	last_source = child;
274f2ebb3a9SAl Viro 	hlist_add_head(&child->mnt_hash, list);
275d2921684SEric W. Biederman 	return count_mounts(m->mnt_ns, child);
276796a6b52SAl Viro }
277b90fa9aeSRam Pai 
278b90fa9aeSRam Pai /*
279b90fa9aeSRam Pai  * mount 'source_mnt' under the destination 'dest_mnt' at
280b90fa9aeSRam Pai  * dentry 'dest_dentry'. And propagate that mount to
281b90fa9aeSRam Pai  * all the peer and slave mounts of 'dest_mnt'.
282b90fa9aeSRam Pai  * Link all the new mounts into a propagation tree headed at
283b90fa9aeSRam Pai  * source_mnt. Also link all the new mounts using ->mnt_list
284b90fa9aeSRam Pai  * headed at source_mnt's ->mnt_list
285b90fa9aeSRam Pai  *
286b90fa9aeSRam Pai  * @dest_mnt: destination mount.
287b90fa9aeSRam Pai  * @dest_dentry: destination dentry.
288b90fa9aeSRam Pai  * @source_mnt: source mount.
289b90fa9aeSRam Pai  * @tree_list : list of heads of trees to be attached.
290b90fa9aeSRam Pai  */
propagate_mnt(struct mount * dest_mnt,struct mountpoint * dest_mp,struct mount * source_mnt,struct hlist_head * tree_list)29184d17192SAl Viro int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
29238129a13SAl Viro 		    struct mount *source_mnt, struct hlist_head *tree_list)
293b90fa9aeSRam Pai {
294f2ebb3a9SAl Viro 	struct mount *m, *n;
295b90fa9aeSRam Pai 	int ret = 0;
296b90fa9aeSRam Pai 
297f2ebb3a9SAl Viro 	/*
298f2ebb3a9SAl Viro 	 * we don't want to bother passing tons of arguments to
299f2ebb3a9SAl Viro 	 * propagate_one(); everything is serialized by namespace_sem,
300f2ebb3a9SAl Viro 	 * so globals will do just fine.
301f2ebb3a9SAl Viro 	 */
302f2ebb3a9SAl Viro 	last_dest = dest_mnt;
3035ec0811dSEric W. Biederman 	first_source = source_mnt;
304f2ebb3a9SAl Viro 	last_source = source_mnt;
305f2ebb3a9SAl Viro 	list = tree_list;
306f2ebb3a9SAl Viro 	dest_master = dest_mnt->mnt_master;
307b90fa9aeSRam Pai 
308f2ebb3a9SAl Viro 	/* all peers of dest_mnt, except dest_mnt itself */
309f2ebb3a9SAl Viro 	for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
3104ea2a8d8SChristian Brauner 		ret = propagate_one(n, dest_mp);
311f2ebb3a9SAl Viro 		if (ret)
312b90fa9aeSRam Pai 			goto out;
313b90fa9aeSRam Pai 	}
314b90fa9aeSRam Pai 
315f2ebb3a9SAl Viro 	/* all slave groups */
316f2ebb3a9SAl Viro 	for (m = next_group(dest_mnt, dest_mnt); m;
317f2ebb3a9SAl Viro 			m = next_group(m, dest_mnt)) {
318f2ebb3a9SAl Viro 		/* everything in that slave group */
319f2ebb3a9SAl Viro 		n = m;
320f2ebb3a9SAl Viro 		do {
3214ea2a8d8SChristian Brauner 			ret = propagate_one(n, dest_mp);
322f2ebb3a9SAl Viro 			if (ret)
323f2ebb3a9SAl Viro 				goto out;
324f2ebb3a9SAl Viro 			n = next_peer(n);
325f2ebb3a9SAl Viro 		} while (n != m);
326b90fa9aeSRam Pai 	}
327b90fa9aeSRam Pai out:
328f2ebb3a9SAl Viro 	read_seqlock_excl(&mount_lock);
329f2ebb3a9SAl Viro 	hlist_for_each_entry(n, tree_list, mnt_hash) {
330f2ebb3a9SAl Viro 		m = n->mnt_parent;
331f2ebb3a9SAl Viro 		if (m->mnt_master != dest_mnt->mnt_master)
332f2ebb3a9SAl Viro 			CLEAR_MNT_MARK(m->mnt_master);
333b90fa9aeSRam Pai 	}
334f2ebb3a9SAl Viro 	read_sequnlock_excl(&mount_lock);
335b90fa9aeSRam Pai 	return ret;
336b90fa9aeSRam Pai }
337a05964f3SRam Pai 
find_topper(struct mount * mnt)3381064f874SEric W. Biederman static struct mount *find_topper(struct mount *mnt)
3391064f874SEric W. Biederman {
3401064f874SEric W. Biederman 	/* If there is exactly one mount covering mnt completely return it. */
3411064f874SEric W. Biederman 	struct mount *child;
3421064f874SEric W. Biederman 
3431064f874SEric W. Biederman 	if (!list_is_singular(&mnt->mnt_mounts))
3441064f874SEric W. Biederman 		return NULL;
3451064f874SEric W. Biederman 
3461064f874SEric W. Biederman 	child = list_first_entry(&mnt->mnt_mounts, struct mount, mnt_child);
3471064f874SEric W. Biederman 	if (child->mnt_mountpoint != mnt->mnt.mnt_root)
3481064f874SEric W. Biederman 		return NULL;
3491064f874SEric W. Biederman 
3501064f874SEric W. Biederman 	return child;
3511064f874SEric W. Biederman }
3521064f874SEric W. Biederman 
353a05964f3SRam Pai /*
354a05964f3SRam Pai  * return true if the refcount is greater than count
355a05964f3SRam Pai  */
do_refcount_check(struct mount * mnt,int count)3561ab59738SAl Viro static inline int do_refcount_check(struct mount *mnt, int count)
357a05964f3SRam Pai {
358aba809cfSAl Viro 	return mnt_get_count(mnt) > count;
359a05964f3SRam Pai }
360a05964f3SRam Pai 
3616ac39281SChristian Brauner /**
3626ac39281SChristian Brauner  * propagation_would_overmount - check whether propagation from @from
3636ac39281SChristian Brauner  *                               would overmount @to
3646ac39281SChristian Brauner  * @from: shared mount
3656ac39281SChristian Brauner  * @to:   mount to check
3666ac39281SChristian Brauner  * @mp:   future mountpoint of @to on @from
3676ac39281SChristian Brauner  *
3686ac39281SChristian Brauner  * If @from propagates mounts to @to, @from and @to must either be peers
3696ac39281SChristian Brauner  * or one of the masters in the hierarchy of masters of @to must be a
3706ac39281SChristian Brauner  * peer of @from.
3716ac39281SChristian Brauner  *
3726ac39281SChristian Brauner  * If the root of the @to mount is equal to the future mountpoint @mp of
3736ac39281SChristian Brauner  * the @to mount on @from then @to will be overmounted by whatever is
3746ac39281SChristian Brauner  * propagated to it.
3756ac39281SChristian Brauner  *
3766ac39281SChristian Brauner  * Context: This function expects namespace_lock() to be held and that
3776ac39281SChristian Brauner  *          @mp is stable.
3786ac39281SChristian Brauner  * Return: If @from overmounts @to, true is returned, false if not.
3796ac39281SChristian Brauner  */
propagation_would_overmount(const struct mount * from,const struct mount * to,const struct mountpoint * mp)3806ac39281SChristian Brauner bool propagation_would_overmount(const struct mount *from,
3816ac39281SChristian Brauner 				 const struct mount *to,
3826ac39281SChristian Brauner 				 const struct mountpoint *mp)
3836ac39281SChristian Brauner {
3846ac39281SChristian Brauner 	if (!IS_MNT_SHARED(from))
3856ac39281SChristian Brauner 		return false;
3866ac39281SChristian Brauner 
3876ac39281SChristian Brauner 	if (to->mnt.mnt_root != mp->m_dentry)
3886ac39281SChristian Brauner 		return false;
3896ac39281SChristian Brauner 
3906ac39281SChristian Brauner 	for (const struct mount *m = to; m; m = m->mnt_master) {
3916ac39281SChristian Brauner 		if (peers(from, m))
3926ac39281SChristian Brauner 			return true;
3936ac39281SChristian Brauner 	}
3946ac39281SChristian Brauner 
3956ac39281SChristian Brauner 	return false;
3966ac39281SChristian Brauner }
3976ac39281SChristian Brauner 
398a05964f3SRam Pai /*
399a05964f3SRam Pai  * check if the mount 'mnt' can be unmounted successfully.
400a05964f3SRam Pai  * @mnt: the mount to be checked for unmount
401a05964f3SRam Pai  * NOTE: unmounting 'mnt' would naturally propagate to all
402a05964f3SRam Pai  * other mounts its parent propagates to.
403a05964f3SRam Pai  * Check if any of these mounts that **do not have submounts**
404a05964f3SRam Pai  * have more references than 'refcnt'. If so return busy.
40599b7db7bSNick Piggin  *
406b3e19d92SNick Piggin  * vfsmount lock must be held for write
407a05964f3SRam Pai  */
propagate_mount_busy(struct mount * mnt,int refcnt)4081ab59738SAl Viro int propagate_mount_busy(struct mount *mnt, int refcnt)
409a05964f3SRam Pai {
4101064f874SEric W. Biederman 	struct mount *m, *child, *topper;
4110714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
412a05964f3SRam Pai 
4130714a533SAl Viro 	if (mnt == parent)
414a05964f3SRam Pai 		return do_refcount_check(mnt, refcnt);
415a05964f3SRam Pai 
416a05964f3SRam Pai 	/*
417a05964f3SRam Pai 	 * quickly check if the current mount can be unmounted.
418a05964f3SRam Pai 	 * If not, we don't have to go checking for all other
419a05964f3SRam Pai 	 * mounts
420a05964f3SRam Pai 	 */
4216b41d536SAl Viro 	if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
422a05964f3SRam Pai 		return 1;
423a05964f3SRam Pai 
424c937135dSAl Viro 	for (m = propagation_next(parent, parent); m;
425c937135dSAl Viro 	     		m = propagation_next(m, parent)) {
4261064f874SEric W. Biederman 		int count = 1;
4271064f874SEric W. Biederman 		child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
4281064f874SEric W. Biederman 		if (!child)
4291064f874SEric W. Biederman 			continue;
4301064f874SEric W. Biederman 
4311064f874SEric W. Biederman 		/* Is there exactly one mount on the child that covers
4321064f874SEric W. Biederman 		 * it completely whose reference should be ignored?
4331064f874SEric W. Biederman 		 */
4341064f874SEric W. Biederman 		topper = find_topper(child);
4351064f874SEric W. Biederman 		if (topper)
4361064f874SEric W. Biederman 			count += 1;
4371064f874SEric W. Biederman 		else if (!list_empty(&child->mnt_mounts))
4381064f874SEric W. Biederman 			continue;
4391064f874SEric W. Biederman 
4401064f874SEric W. Biederman 		if (do_refcount_check(child, count))
4411064f874SEric W. Biederman 			return 1;
442a05964f3SRam Pai 	}
4431064f874SEric W. Biederman 	return 0;
444a05964f3SRam Pai }
445a05964f3SRam Pai 
446a05964f3SRam Pai /*
4475d88457eSEric W. Biederman  * Clear MNT_LOCKED when it can be shown to be safe.
4485d88457eSEric W. Biederman  *
4495d88457eSEric W. Biederman  * mount_lock lock must be held for write
4505d88457eSEric W. Biederman  */
propagate_mount_unlock(struct mount * mnt)4515d88457eSEric W. Biederman void propagate_mount_unlock(struct mount *mnt)
4525d88457eSEric W. Biederman {
4535d88457eSEric W. Biederman 	struct mount *parent = mnt->mnt_parent;
4545d88457eSEric W. Biederman 	struct mount *m, *child;
4555d88457eSEric W. Biederman 
4565d88457eSEric W. Biederman 	BUG_ON(parent == mnt);
4575d88457eSEric W. Biederman 
4585d88457eSEric W. Biederman 	for (m = propagation_next(parent, parent); m;
4595d88457eSEric W. Biederman 			m = propagation_next(m, parent)) {
4601064f874SEric W. Biederman 		child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
4615d88457eSEric W. Biederman 		if (child)
4625d88457eSEric W. Biederman 			child->mnt.mnt_flags &= ~MNT_LOCKED;
4635d88457eSEric W. Biederman 	}
4645d88457eSEric W. Biederman }
4655d88457eSEric W. Biederman 
umount_one(struct mount * mnt,struct list_head * to_umount)46699b19d16SEric W. Biederman static void umount_one(struct mount *mnt, struct list_head *to_umount)
4670c56fe31SEric W. Biederman {
46899b19d16SEric W. Biederman 	CLEAR_MNT_MARK(mnt);
46999b19d16SEric W. Biederman 	mnt->mnt.mnt_flags |= MNT_UMOUNT;
47099b19d16SEric W. Biederman 	list_del_init(&mnt->mnt_child);
47199b19d16SEric W. Biederman 	list_del_init(&mnt->mnt_umounting);
4722eea9ce4SMiklos Szeredi 	move_from_ns(mnt, to_umount);
4730c56fe31SEric W. Biederman }
4740c56fe31SEric W. Biederman 
4750c56fe31SEric W. Biederman /*
476a05964f3SRam Pai  * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
477a05964f3SRam Pai  * parent propagates to.
478a05964f3SRam Pai  */
__propagate_umount(struct mount * mnt,struct list_head * to_umount,struct list_head * to_restore)47999b19d16SEric W. Biederman static bool __propagate_umount(struct mount *mnt,
48099b19d16SEric W. Biederman 			       struct list_head *to_umount,
48199b19d16SEric W. Biederman 			       struct list_head *to_restore)
482a05964f3SRam Pai {
48399b19d16SEric W. Biederman 	bool progress = false;
48499b19d16SEric W. Biederman 	struct mount *child;
485a05964f3SRam Pai 
486a05964f3SRam Pai 	/*
48799b19d16SEric W. Biederman 	 * The state of the parent won't change if this mount is
48899b19d16SEric W. Biederman 	 * already unmounted or marked as without children.
489a05964f3SRam Pai 	 */
49099b19d16SEric W. Biederman 	if (mnt->mnt.mnt_flags & (MNT_UMOUNT | MNT_MARKED))
49199b19d16SEric W. Biederman 		goto out;
49299b19d16SEric W. Biederman 
49399b19d16SEric W. Biederman 	/* Verify topper is the only grandchild that has not been
49499b19d16SEric W. Biederman 	 * speculatively unmounted.
49599b19d16SEric W. Biederman 	 */
49699b19d16SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
49799b19d16SEric W. Biederman 		if (child->mnt_mountpoint == mnt->mnt.mnt_root)
4980c56fe31SEric W. Biederman 			continue;
49999b19d16SEric W. Biederman 		if (!list_empty(&child->mnt_umounting) && IS_MNT_MARKED(child))
50099b19d16SEric W. Biederman 			continue;
50199b19d16SEric W. Biederman 		/* Found a mounted child */
50299b19d16SEric W. Biederman 		goto children;
503a05964f3SRam Pai 	}
504a05964f3SRam Pai 
50599b19d16SEric W. Biederman 	/* Mark mounts that can be unmounted if not locked */
50699b19d16SEric W. Biederman 	SET_MNT_MARK(mnt);
50799b19d16SEric W. Biederman 	progress = true;
50899b19d16SEric W. Biederman 
50999b19d16SEric W. Biederman 	/* If a mount is without children and not locked umount it. */
51099b19d16SEric W. Biederman 	if (!IS_MNT_LOCKED(mnt)) {
51199b19d16SEric W. Biederman 		umount_one(mnt, to_umount);
51299b19d16SEric W. Biederman 	} else {
51399b19d16SEric W. Biederman children:
51499b19d16SEric W. Biederman 		list_move_tail(&mnt->mnt_umounting, to_restore);
51599b19d16SEric W. Biederman 	}
51699b19d16SEric W. Biederman out:
51799b19d16SEric W. Biederman 	return progress;
51899b19d16SEric W. Biederman }
51999b19d16SEric W. Biederman 
umount_list(struct list_head * to_umount,struct list_head * to_restore)52099b19d16SEric W. Biederman static void umount_list(struct list_head *to_umount,
52199b19d16SEric W. Biederman 			struct list_head *to_restore)
522570487d3SEric W. Biederman {
52399b19d16SEric W. Biederman 	struct mount *mnt, *child, *tmp;
52499b19d16SEric W. Biederman 	list_for_each_entry(mnt, to_umount, mnt_list) {
52599b19d16SEric W. Biederman 		list_for_each_entry_safe(child, tmp, &mnt->mnt_mounts, mnt_child) {
52699b19d16SEric W. Biederman 			/* topper? */
52799b19d16SEric W. Biederman 			if (child->mnt_mountpoint == mnt->mnt.mnt_root)
52899b19d16SEric W. Biederman 				list_move_tail(&child->mnt_umounting, to_restore);
52999b19d16SEric W. Biederman 			else
53099b19d16SEric W. Biederman 				umount_one(child, to_umount);
53199b19d16SEric W. Biederman 		}
53299b19d16SEric W. Biederman 	}
53399b19d16SEric W. Biederman }
53499b19d16SEric W. Biederman 
restore_mounts(struct list_head * to_restore)53599b19d16SEric W. Biederman static void restore_mounts(struct list_head *to_restore)
53699b19d16SEric W. Biederman {
53799b19d16SEric W. Biederman 	/* Restore mounts to a clean working state */
53899b19d16SEric W. Biederman 	while (!list_empty(to_restore)) {
539570487d3SEric W. Biederman 		struct mount *mnt, *parent;
540570487d3SEric W. Biederman 		struct mountpoint *mp;
541570487d3SEric W. Biederman 
54299b19d16SEric W. Biederman 		mnt = list_first_entry(to_restore, struct mount, mnt_umounting);
54399b19d16SEric W. Biederman 		CLEAR_MNT_MARK(mnt);
54499b19d16SEric W. Biederman 		list_del_init(&mnt->mnt_umounting);
545570487d3SEric W. Biederman 
54699b19d16SEric W. Biederman 		/* Should this mount be reparented? */
547570487d3SEric W. Biederman 		mp = mnt->mnt_mp;
548570487d3SEric W. Biederman 		parent = mnt->mnt_parent;
549570487d3SEric W. Biederman 		while (parent->mnt.mnt_flags & MNT_UMOUNT) {
550570487d3SEric W. Biederman 			mp = parent->mnt_mp;
551570487d3SEric W. Biederman 			parent = parent->mnt_parent;
552570487d3SEric W. Biederman 		}
553bf630c40SMiklos Szeredi 		if (parent != mnt->mnt_parent) {
554570487d3SEric W. Biederman 			mnt_change_mountpoint(parent, mp, mnt);
555bf630c40SMiklos Szeredi 			mnt_notify_add(mnt);
556bf630c40SMiklos Szeredi 		}
557570487d3SEric W. Biederman 	}
558570487d3SEric W. Biederman }
559570487d3SEric W. Biederman 
cleanup_umount_visitations(struct list_head * visited)560296990deSEric W. Biederman static void cleanup_umount_visitations(struct list_head *visited)
561296990deSEric W. Biederman {
562296990deSEric W. Biederman 	while (!list_empty(visited)) {
563296990deSEric W. Biederman 		struct mount *mnt =
564296990deSEric W. Biederman 			list_first_entry(visited, struct mount, mnt_umounting);
565296990deSEric W. Biederman 		list_del_init(&mnt->mnt_umounting);
566296990deSEric W. Biederman 	}
567296990deSEric W. Biederman }
568296990deSEric W. Biederman 
569a05964f3SRam Pai /*
570a05964f3SRam Pai  * collect all mounts that receive propagation from the mount in @list,
571a05964f3SRam Pai  * and return these additional mounts in the same list.
572a05964f3SRam Pai  * @list: the list of mounts to be unmounted.
57399b7db7bSNick Piggin  *
57499b7db7bSNick Piggin  * vfsmount lock must be held for write
575a05964f3SRam Pai  */
propagate_umount(struct list_head * list)576c003b26fSEric W. Biederman int propagate_umount(struct list_head *list)
577a05964f3SRam Pai {
57861ef47b1SAl Viro 	struct mount *mnt;
57999b19d16SEric W. Biederman 	LIST_HEAD(to_restore);
58099b19d16SEric W. Biederman 	LIST_HEAD(to_umount);
581296990deSEric W. Biederman 	LIST_HEAD(visited);
582a05964f3SRam Pai 
583296990deSEric W. Biederman 	/* Find candidates for unmounting */
584296990deSEric W. Biederman 	list_for_each_entry_reverse(mnt, list, mnt_list) {
58599b19d16SEric W. Biederman 		struct mount *parent = mnt->mnt_parent;
58699b19d16SEric W. Biederman 		struct mount *m;
5870c56fe31SEric W. Biederman 
588296990deSEric W. Biederman 		/*
589296990deSEric W. Biederman 		 * If this mount has already been visited it is known that it's
590296990deSEric W. Biederman 		 * entire peer group and all of their slaves in the propagation
591296990deSEric W. Biederman 		 * tree for the mountpoint has already been visited and there is
592296990deSEric W. Biederman 		 * no need to visit them again.
593296990deSEric W. Biederman 		 */
594296990deSEric W. Biederman 		if (!list_empty(&mnt->mnt_umounting))
595296990deSEric W. Biederman 			continue;
596296990deSEric W. Biederman 
597296990deSEric W. Biederman 		list_add_tail(&mnt->mnt_umounting, &visited);
59899b19d16SEric W. Biederman 		for (m = propagation_next(parent, parent); m;
59999b19d16SEric W. Biederman 		     m = propagation_next(m, parent)) {
60099b19d16SEric W. Biederman 			struct mount *child = __lookup_mnt(&m->mnt,
60199b19d16SEric W. Biederman 							   mnt->mnt_mountpoint);
60299b19d16SEric W. Biederman 			if (!child)
60399b19d16SEric W. Biederman 				continue;
604570487d3SEric W. Biederman 
605296990deSEric W. Biederman 			if (!list_empty(&child->mnt_umounting)) {
606296990deSEric W. Biederman 				/*
607296990deSEric W. Biederman 				 * If the child has already been visited it is
608296990deSEric W. Biederman 				 * know that it's entire peer group and all of
609296990deSEric W. Biederman 				 * their slaves in the propgation tree for the
610296990deSEric W. Biederman 				 * mountpoint has already been visited and there
611296990deSEric W. Biederman 				 * is no need to visit this subtree again.
612296990deSEric W. Biederman 				 */
613296990deSEric W. Biederman 				m = skip_propagation_subtree(m, parent);
614296990deSEric W. Biederman 				continue;
615296990deSEric W. Biederman 			} else if (child->mnt.mnt_flags & MNT_UMOUNT) {
616296990deSEric W. Biederman 				/*
617175c6a21SZhu Jun 				 * We have come across a partially unmounted
618175c6a21SZhu Jun 				 * mount in a list that has not been visited
619175c6a21SZhu Jun 				 * yet. Remember it has been visited and
620175c6a21SZhu Jun 				 * continue about our merry way.
621296990deSEric W. Biederman 				 */
622296990deSEric W. Biederman 				list_add_tail(&child->mnt_umounting, &visited);
623296990deSEric W. Biederman 				continue;
624296990deSEric W. Biederman 			}
625296990deSEric W. Biederman 
62699b19d16SEric W. Biederman 			/* Check the child and parents while progress is made */
62799b19d16SEric W. Biederman 			while (__propagate_umount(child,
62899b19d16SEric W. Biederman 						  &to_umount, &to_restore)) {
62999b19d16SEric W. Biederman 				/* Is the parent a umount candidate? */
63099b19d16SEric W. Biederman 				child = child->mnt_parent;
63199b19d16SEric W. Biederman 				if (list_empty(&child->mnt_umounting))
63299b19d16SEric W. Biederman 					break;
63399b19d16SEric W. Biederman 			}
63499b19d16SEric W. Biederman 		}
63599b19d16SEric W. Biederman 	}
63699b19d16SEric W. Biederman 
63799b19d16SEric W. Biederman 	umount_list(&to_umount, &to_restore);
63899b19d16SEric W. Biederman 	restore_mounts(&to_restore);
639296990deSEric W. Biederman 	cleanup_umount_visitations(&visited);
64099b19d16SEric W. Biederman 	list_splice_tail(&to_umount, list);
641570487d3SEric W. Biederman 
642a05964f3SRam Pai 	return 0;
643a05964f3SRam Pai }
644