xref: /linux/fs/pnode.c (revision 9676f0c6389b62bd6b24d77d4b3abdbcfa32d0f2)
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  */
907b20889SRam Pai #include <linux/namespace.h>
1007b20889SRam Pai #include <linux/mount.h>
1107b20889SRam Pai #include <linux/fs.h>
1207b20889SRam Pai #include "pnode.h"
1307b20889SRam Pai 
1403e06e68SRam Pai /* return the next shared peer mount of @p */
1503e06e68SRam Pai static inline struct vfsmount *next_peer(struct vfsmount *p)
1603e06e68SRam Pai {
1703e06e68SRam Pai 	return list_entry(p->mnt_share.next, struct vfsmount, mnt_share);
1803e06e68SRam Pai }
1903e06e68SRam Pai 
205afe0022SRam Pai static inline struct vfsmount *first_slave(struct vfsmount *p)
215afe0022SRam Pai {
225afe0022SRam Pai 	return list_entry(p->mnt_slave_list.next, struct vfsmount, mnt_slave);
235afe0022SRam Pai }
245afe0022SRam Pai 
255afe0022SRam Pai static inline struct vfsmount *next_slave(struct vfsmount *p)
265afe0022SRam Pai {
275afe0022SRam Pai 	return list_entry(p->mnt_slave.next, struct vfsmount, mnt_slave);
285afe0022SRam Pai }
295afe0022SRam Pai 
30a58b0eb8SRam Pai static int do_make_slave(struct vfsmount *mnt)
31a58b0eb8SRam Pai {
32a58b0eb8SRam Pai 	struct vfsmount *peer_mnt = mnt, *master = mnt->mnt_master;
33a58b0eb8SRam Pai 	struct vfsmount *slave_mnt;
34a58b0eb8SRam Pai 
35a58b0eb8SRam Pai 	/*
36a58b0eb8SRam Pai 	 * slave 'mnt' to a peer mount that has the
37a58b0eb8SRam Pai 	 * same root dentry. If none is available than
38a58b0eb8SRam Pai 	 * slave it to anything that is available.
39a58b0eb8SRam Pai 	 */
40a58b0eb8SRam Pai 	while ((peer_mnt = next_peer(peer_mnt)) != mnt &&
41a58b0eb8SRam Pai 	       peer_mnt->mnt_root != mnt->mnt_root) ;
42a58b0eb8SRam Pai 
43a58b0eb8SRam Pai 	if (peer_mnt == mnt) {
44a58b0eb8SRam Pai 		peer_mnt = next_peer(mnt);
45a58b0eb8SRam Pai 		if (peer_mnt == mnt)
46a58b0eb8SRam Pai 			peer_mnt = NULL;
47a58b0eb8SRam Pai 	}
48a58b0eb8SRam Pai 	list_del_init(&mnt->mnt_share);
49a58b0eb8SRam Pai 
50a58b0eb8SRam Pai 	if (peer_mnt)
51a58b0eb8SRam Pai 		master = peer_mnt;
52a58b0eb8SRam Pai 
53a58b0eb8SRam Pai 	if (master) {
54a58b0eb8SRam Pai 		list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
55a58b0eb8SRam Pai 			slave_mnt->mnt_master = master;
56a58b0eb8SRam Pai 		list_del(&mnt->mnt_slave);
57a58b0eb8SRam Pai 		list_add(&mnt->mnt_slave, &master->mnt_slave_list);
58a58b0eb8SRam Pai 		list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
59a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
60a58b0eb8SRam Pai 	} else {
61a58b0eb8SRam Pai 		struct list_head *p = &mnt->mnt_slave_list;
62a58b0eb8SRam Pai 		while (!list_empty(p)) {
63a58b0eb8SRam Pai                         slave_mnt = list_entry(p->next,
64a58b0eb8SRam Pai 					struct vfsmount, mnt_slave);
65a58b0eb8SRam Pai 			list_del_init(&slave_mnt->mnt_slave);
66a58b0eb8SRam Pai 			slave_mnt->mnt_master = NULL;
67a58b0eb8SRam Pai 		}
68a58b0eb8SRam Pai 	}
69a58b0eb8SRam Pai 	mnt->mnt_master = master;
70a58b0eb8SRam Pai 	CLEAR_MNT_SHARED(mnt);
71a58b0eb8SRam Pai 	INIT_LIST_HEAD(&mnt->mnt_slave_list);
72a58b0eb8SRam Pai 	return 0;
73a58b0eb8SRam Pai }
74a58b0eb8SRam Pai 
7507b20889SRam Pai void change_mnt_propagation(struct vfsmount *mnt, int type)
7607b20889SRam Pai {
7703e06e68SRam Pai 	if (type == MS_SHARED) {
78b90fa9aeSRam Pai 		set_mnt_shared(mnt);
79a58b0eb8SRam Pai 		return;
80a58b0eb8SRam Pai 	}
81a58b0eb8SRam Pai 	do_make_slave(mnt);
82a58b0eb8SRam Pai 	if (type != MS_SLAVE) {
83a58b0eb8SRam Pai 		list_del_init(&mnt->mnt_slave);
84a58b0eb8SRam Pai 		mnt->mnt_master = NULL;
85*9676f0c6SRam Pai 		if (type == MS_UNBINDABLE)
86*9676f0c6SRam Pai 			mnt->mnt_flags |= MNT_UNBINDABLE;
8707b20889SRam Pai 	}
8803e06e68SRam Pai }
89b90fa9aeSRam Pai 
90b90fa9aeSRam Pai /*
91b90fa9aeSRam Pai  * get the next mount in the propagation tree.
92b90fa9aeSRam Pai  * @m: the mount seen last
93b90fa9aeSRam Pai  * @origin: the original mount from where the tree walk initiated
94b90fa9aeSRam Pai  */
95b90fa9aeSRam Pai static struct vfsmount *propagation_next(struct vfsmount *m,
96b90fa9aeSRam Pai 					 struct vfsmount *origin)
97b90fa9aeSRam Pai {
985afe0022SRam Pai 	/* are there any slaves of this mount? */
995afe0022SRam Pai 	if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
1005afe0022SRam Pai 		return first_slave(m);
1015afe0022SRam Pai 
1025afe0022SRam Pai 	while (1) {
1035afe0022SRam Pai 		struct vfsmount *next;
1045afe0022SRam Pai 		struct vfsmount *master = m->mnt_master;
1055afe0022SRam Pai 
1065afe0022SRam Pai 		if ( master == origin->mnt_master ) {
1075afe0022SRam Pai 			next = next_peer(m);
1085afe0022SRam Pai 			return ((next == origin) ? NULL : next);
1095afe0022SRam Pai 		} else if (m->mnt_slave.next != &master->mnt_slave_list)
1105afe0022SRam Pai 			return next_slave(m);
1115afe0022SRam Pai 
1125afe0022SRam Pai 		/* back at master */
1135afe0022SRam Pai 		m = master;
1145afe0022SRam Pai 	}
1155afe0022SRam Pai }
1165afe0022SRam Pai 
1175afe0022SRam Pai /*
1185afe0022SRam Pai  * return the source mount to be used for cloning
1195afe0022SRam Pai  *
1205afe0022SRam Pai  * @dest 	the current destination mount
1215afe0022SRam Pai  * @last_dest  	the last seen destination mount
1225afe0022SRam Pai  * @last_src  	the last seen source mount
1235afe0022SRam Pai  * @type	return CL_SLAVE if the new mount has to be
1245afe0022SRam Pai  * 		cloned as a slave.
1255afe0022SRam Pai  */
1265afe0022SRam Pai static struct vfsmount *get_source(struct vfsmount *dest,
1275afe0022SRam Pai 					struct vfsmount *last_dest,
1285afe0022SRam Pai 					struct vfsmount *last_src,
1295afe0022SRam Pai 					int *type)
1305afe0022SRam Pai {
1315afe0022SRam Pai 	struct vfsmount *p_last_src = NULL;
1325afe0022SRam Pai 	struct vfsmount *p_last_dest = NULL;
1335afe0022SRam Pai 	*type = CL_PROPAGATION;;
1345afe0022SRam Pai 
1355afe0022SRam Pai 	if (IS_MNT_SHARED(dest))
1365afe0022SRam Pai 		*type |= CL_MAKE_SHARED;
1375afe0022SRam Pai 
1385afe0022SRam Pai 	while (last_dest != dest->mnt_master) {
1395afe0022SRam Pai 		p_last_dest = last_dest;
1405afe0022SRam Pai 		p_last_src = last_src;
1415afe0022SRam Pai 		last_dest = last_dest->mnt_master;
1425afe0022SRam Pai 		last_src = last_src->mnt_master;
1435afe0022SRam Pai 	}
1445afe0022SRam Pai 
1455afe0022SRam Pai 	if (p_last_dest) {
1465afe0022SRam Pai 		do {
1475afe0022SRam Pai 			p_last_dest = next_peer(p_last_dest);
1485afe0022SRam Pai 		} while (IS_MNT_NEW(p_last_dest));
1495afe0022SRam Pai 	}
1505afe0022SRam Pai 
1515afe0022SRam Pai 	if (dest != p_last_dest) {
1525afe0022SRam Pai 		*type |= CL_SLAVE;
1535afe0022SRam Pai 		return last_src;
1545afe0022SRam Pai 	} else
1555afe0022SRam Pai 		return p_last_src;
156b90fa9aeSRam Pai }
157b90fa9aeSRam Pai 
158b90fa9aeSRam Pai /*
159b90fa9aeSRam Pai  * mount 'source_mnt' under the destination 'dest_mnt' at
160b90fa9aeSRam Pai  * dentry 'dest_dentry'. And propagate that mount to
161b90fa9aeSRam Pai  * all the peer and slave mounts of 'dest_mnt'.
162b90fa9aeSRam Pai  * Link all the new mounts into a propagation tree headed at
163b90fa9aeSRam Pai  * source_mnt. Also link all the new mounts using ->mnt_list
164b90fa9aeSRam Pai  * headed at source_mnt's ->mnt_list
165b90fa9aeSRam Pai  *
166b90fa9aeSRam Pai  * @dest_mnt: destination mount.
167b90fa9aeSRam Pai  * @dest_dentry: destination dentry.
168b90fa9aeSRam Pai  * @source_mnt: source mount.
169b90fa9aeSRam Pai  * @tree_list : list of heads of trees to be attached.
170b90fa9aeSRam Pai  */
171b90fa9aeSRam Pai int propagate_mnt(struct vfsmount *dest_mnt, struct dentry *dest_dentry,
172b90fa9aeSRam Pai 		    struct vfsmount *source_mnt, struct list_head *tree_list)
173b90fa9aeSRam Pai {
174b90fa9aeSRam Pai 	struct vfsmount *m, *child;
175b90fa9aeSRam Pai 	int ret = 0;
176b90fa9aeSRam Pai 	struct vfsmount *prev_dest_mnt = dest_mnt;
177b90fa9aeSRam Pai 	struct vfsmount *prev_src_mnt  = source_mnt;
178b90fa9aeSRam Pai 	LIST_HEAD(tmp_list);
179b90fa9aeSRam Pai 	LIST_HEAD(umount_list);
180b90fa9aeSRam Pai 
181b90fa9aeSRam Pai 	for (m = propagation_next(dest_mnt, dest_mnt); m;
182b90fa9aeSRam Pai 			m = propagation_next(m, dest_mnt)) {
1835afe0022SRam Pai 		int type;
1845afe0022SRam Pai 		struct vfsmount *source;
185b90fa9aeSRam Pai 
186b90fa9aeSRam Pai 		if (IS_MNT_NEW(m))
187b90fa9aeSRam Pai 			continue;
188b90fa9aeSRam Pai 
1895afe0022SRam Pai 		source =  get_source(m, prev_dest_mnt, prev_src_mnt, &type);
190b90fa9aeSRam Pai 
1915afe0022SRam Pai 		if (!(child = copy_tree(source, source->mnt_root, type))) {
192b90fa9aeSRam Pai 			ret = -ENOMEM;
193b90fa9aeSRam Pai 			list_splice(tree_list, tmp_list.prev);
194b90fa9aeSRam Pai 			goto out;
195b90fa9aeSRam Pai 		}
196b90fa9aeSRam Pai 
197b90fa9aeSRam Pai 		if (is_subdir(dest_dentry, m->mnt_root)) {
198b90fa9aeSRam Pai 			mnt_set_mountpoint(m, dest_dentry, child);
199b90fa9aeSRam Pai 			list_add_tail(&child->mnt_hash, tree_list);
200b90fa9aeSRam Pai 		} else {
201b90fa9aeSRam Pai 			/*
202b90fa9aeSRam Pai 			 * This can happen if the parent mount was bind mounted
203b90fa9aeSRam Pai 			 * on some subdirectory of a shared/slave mount.
204b90fa9aeSRam Pai 			 */
205b90fa9aeSRam Pai 			list_add_tail(&child->mnt_hash, &tmp_list);
206b90fa9aeSRam Pai 		}
207b90fa9aeSRam Pai 		prev_dest_mnt = m;
208b90fa9aeSRam Pai 		prev_src_mnt  = child;
209b90fa9aeSRam Pai 	}
210b90fa9aeSRam Pai out:
211b90fa9aeSRam Pai 	spin_lock(&vfsmount_lock);
212b90fa9aeSRam Pai 	while (!list_empty(&tmp_list)) {
213b90fa9aeSRam Pai 		child = list_entry(tmp_list.next, struct vfsmount, mnt_hash);
214b90fa9aeSRam Pai 		list_del_init(&child->mnt_hash);
215a05964f3SRam Pai 		umount_tree(child, 0, &umount_list);
216b90fa9aeSRam Pai 	}
217b90fa9aeSRam Pai 	spin_unlock(&vfsmount_lock);
218b90fa9aeSRam Pai 	release_mounts(&umount_list);
219b90fa9aeSRam Pai 	return ret;
220b90fa9aeSRam Pai }
221a05964f3SRam Pai 
222a05964f3SRam Pai /*
223a05964f3SRam Pai  * return true if the refcount is greater than count
224a05964f3SRam Pai  */
225a05964f3SRam Pai static inline int do_refcount_check(struct vfsmount *mnt, int count)
226a05964f3SRam Pai {
227a05964f3SRam Pai 	int mycount = atomic_read(&mnt->mnt_count);
228a05964f3SRam Pai 	return (mycount > count);
229a05964f3SRam Pai }
230a05964f3SRam Pai 
231a05964f3SRam Pai /*
232a05964f3SRam Pai  * check if the mount 'mnt' can be unmounted successfully.
233a05964f3SRam Pai  * @mnt: the mount to be checked for unmount
234a05964f3SRam Pai  * NOTE: unmounting 'mnt' would naturally propagate to all
235a05964f3SRam Pai  * other mounts its parent propagates to.
236a05964f3SRam Pai  * Check if any of these mounts that **do not have submounts**
237a05964f3SRam Pai  * have more references than 'refcnt'. If so return busy.
238a05964f3SRam Pai  */
239a05964f3SRam Pai int propagate_mount_busy(struct vfsmount *mnt, int refcnt)
240a05964f3SRam Pai {
241a05964f3SRam Pai 	struct vfsmount *m, *child;
242a05964f3SRam Pai 	struct vfsmount *parent = mnt->mnt_parent;
243a05964f3SRam Pai 	int ret = 0;
244a05964f3SRam Pai 
245a05964f3SRam Pai 	if (mnt == parent)
246a05964f3SRam Pai 		return do_refcount_check(mnt, refcnt);
247a05964f3SRam Pai 
248a05964f3SRam Pai 	/*
249a05964f3SRam Pai 	 * quickly check if the current mount can be unmounted.
250a05964f3SRam Pai 	 * If not, we don't have to go checking for all other
251a05964f3SRam Pai 	 * mounts
252a05964f3SRam Pai 	 */
253a05964f3SRam Pai 	if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
254a05964f3SRam Pai 		return 1;
255a05964f3SRam Pai 
256a05964f3SRam Pai 	for (m = propagation_next(parent, parent); m;
257a05964f3SRam Pai 	     		m = propagation_next(m, parent)) {
258a05964f3SRam Pai 		child = __lookup_mnt(m, mnt->mnt_mountpoint, 0);
259a05964f3SRam Pai 		if (child && list_empty(&child->mnt_mounts) &&
260a05964f3SRam Pai 		    (ret = do_refcount_check(child, 1)))
261a05964f3SRam Pai 			break;
262a05964f3SRam Pai 	}
263a05964f3SRam Pai 	return ret;
264a05964f3SRam Pai }
265a05964f3SRam Pai 
266a05964f3SRam Pai /*
267a05964f3SRam Pai  * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
268a05964f3SRam Pai  * parent propagates to.
269a05964f3SRam Pai  */
270a05964f3SRam Pai static void __propagate_umount(struct vfsmount *mnt)
271a05964f3SRam Pai {
272a05964f3SRam Pai 	struct vfsmount *parent = mnt->mnt_parent;
273a05964f3SRam Pai 	struct vfsmount *m;
274a05964f3SRam Pai 
275a05964f3SRam Pai 	BUG_ON(parent == mnt);
276a05964f3SRam Pai 
277a05964f3SRam Pai 	for (m = propagation_next(parent, parent); m;
278a05964f3SRam Pai 			m = propagation_next(m, parent)) {
279a05964f3SRam Pai 
280a05964f3SRam Pai 		struct vfsmount *child = __lookup_mnt(m,
281a05964f3SRam Pai 					mnt->mnt_mountpoint, 0);
282a05964f3SRam Pai 		/*
283a05964f3SRam Pai 		 * umount the child only if the child has no
284a05964f3SRam Pai 		 * other children
285a05964f3SRam Pai 		 */
286a05964f3SRam Pai 		if (child && list_empty(&child->mnt_mounts)) {
287a05964f3SRam Pai 			list_del(&child->mnt_hash);
288a05964f3SRam Pai 			list_add_tail(&child->mnt_hash, &mnt->mnt_hash);
289a05964f3SRam Pai 		}
290a05964f3SRam Pai 	}
291a05964f3SRam Pai }
292a05964f3SRam Pai 
293a05964f3SRam Pai /*
294a05964f3SRam Pai  * collect all mounts that receive propagation from the mount in @list,
295a05964f3SRam Pai  * and return these additional mounts in the same list.
296a05964f3SRam Pai  * @list: the list of mounts to be unmounted.
297a05964f3SRam Pai  */
298a05964f3SRam Pai int propagate_umount(struct list_head *list)
299a05964f3SRam Pai {
300a05964f3SRam Pai 	struct vfsmount *mnt;
301a05964f3SRam Pai 
302a05964f3SRam Pai 	list_for_each_entry(mnt, list, mnt_hash)
303a05964f3SRam Pai 		__propagate_umount(mnt);
304a05964f3SRam Pai 	return 0;
305a05964f3SRam Pai }
306