xref: /openbmc/linux/fs/namespace.c (revision 4ce5d2b1)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namespace.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright Al Viro 2000, 2001
51da177e4SLinus Torvalds  *	Released under GPL v2.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Based on code from fs/super.c, copyright Linus Torvalds and others.
81da177e4SLinus Torvalds  * Heavily rewritten.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/syscalls.h>
12d10577a8SAl Viro #include <linux/export.h>
1316f7e0feSRandy Dunlap #include <linux/capability.h>
146b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
15771b1371SEric W. Biederman #include <linux/user_namespace.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
171da177e4SLinus Torvalds #include <linux/security.h>
1873cd49ecSMiklos Szeredi #include <linux/idr.h>
19d10577a8SAl Viro #include <linux/acct.h>		/* acct_auto_close_mnt */
20d10577a8SAl Viro #include <linux/ramfs.h>	/* init_rootfs */
21d10577a8SAl Viro #include <linux/fs_struct.h>	/* get_fs_root et.al. */
22d10577a8SAl Viro #include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
23d10577a8SAl Viro #include <linux/uaccess.h>
240bb80f24SDavid Howells #include <linux/proc_ns.h>
2520b4fb48SLinus Torvalds #include <linux/magic.h>
2607b20889SRam Pai #include "pnode.h"
27948730b0SAdrian Bunk #include "internal.h"
281da177e4SLinus Torvalds 
2913f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
3013f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT)
3113f14b4dSEric Dumazet 
325addc5ddSAl Viro static int event;
3373cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
34719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
3599b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock);
36f21f6220SAl Viro static int mnt_id_start = 0;
37f21f6220SAl Viro static int mnt_group_start = 1;
385addc5ddSAl Viro 
39fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly;
4084d17192SAl Viro static struct list_head *mountpoint_hashtable __read_mostly;
41e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
42390c6843SRam Pai static struct rw_semaphore namespace_sem;
431da177e4SLinus Torvalds 
44f87fd4c2SMiklos Szeredi /* /sys/fs */
4500d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
4600d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
47f87fd4c2SMiklos Szeredi 
4899b7db7bSNick Piggin /*
4999b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
5099b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
5199b7db7bSNick Piggin  * up the tree.
5299b7db7bSNick Piggin  *
5399b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
5499b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
5599b7db7bSNick Piggin  */
5699b7db7bSNick Piggin DEFINE_BRLOCK(vfsmount_lock);
5799b7db7bSNick Piggin 
581da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
611da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
6213f14b4dSEric Dumazet 	tmp = tmp + (tmp >> HASH_SHIFT);
6313f14b4dSEric Dumazet 	return tmp & (HASH_SIZE - 1);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
663d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
673d733633SDave Hansen 
6899b7db7bSNick Piggin /*
6999b7db7bSNick Piggin  * allocation is serialized by namespace_sem, but we need the spinlock to
7099b7db7bSNick Piggin  * serialize with freeing.
7199b7db7bSNick Piggin  */
72b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
7373cd49ecSMiklos Szeredi {
7473cd49ecSMiklos Szeredi 	int res;
7573cd49ecSMiklos Szeredi 
7673cd49ecSMiklos Szeredi retry:
7773cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
7899b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
7915169fe7SAl Viro 	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
80f21f6220SAl Viro 	if (!res)
8115169fe7SAl Viro 		mnt_id_start = mnt->mnt_id + 1;
8299b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
8373cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
8473cd49ecSMiklos Szeredi 		goto retry;
8573cd49ecSMiklos Szeredi 
8673cd49ecSMiklos Szeredi 	return res;
8773cd49ecSMiklos Szeredi }
8873cd49ecSMiklos Szeredi 
89b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
9073cd49ecSMiklos Szeredi {
9115169fe7SAl Viro 	int id = mnt->mnt_id;
9299b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
93f21f6220SAl Viro 	ida_remove(&mnt_id_ida, id);
94f21f6220SAl Viro 	if (mnt_id_start > id)
95f21f6220SAl Viro 		mnt_id_start = id;
9699b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
9773cd49ecSMiklos Szeredi }
9873cd49ecSMiklos Szeredi 
99719f5d7fSMiklos Szeredi /*
100719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
101719f5d7fSMiklos Szeredi  *
102719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
103719f5d7fSMiklos Szeredi  */
1044b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
105719f5d7fSMiklos Szeredi {
106f21f6220SAl Viro 	int res;
107f21f6220SAl Viro 
108719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
109719f5d7fSMiklos Szeredi 		return -ENOMEM;
110719f5d7fSMiklos Szeredi 
111f21f6220SAl Viro 	res = ida_get_new_above(&mnt_group_ida,
112f21f6220SAl Viro 				mnt_group_start,
11315169fe7SAl Viro 				&mnt->mnt_group_id);
114f21f6220SAl Viro 	if (!res)
11515169fe7SAl Viro 		mnt_group_start = mnt->mnt_group_id + 1;
116f21f6220SAl Viro 
117f21f6220SAl Viro 	return res;
118719f5d7fSMiklos Szeredi }
119719f5d7fSMiklos Szeredi 
120719f5d7fSMiklos Szeredi /*
121719f5d7fSMiklos Szeredi  * Release a peer group ID
122719f5d7fSMiklos Szeredi  */
1234b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
124719f5d7fSMiklos Szeredi {
12515169fe7SAl Viro 	int id = mnt->mnt_group_id;
126f21f6220SAl Viro 	ida_remove(&mnt_group_ida, id);
127f21f6220SAl Viro 	if (mnt_group_start > id)
128f21f6220SAl Viro 		mnt_group_start = id;
12915169fe7SAl Viro 	mnt->mnt_group_id = 0;
130719f5d7fSMiklos Szeredi }
131719f5d7fSMiklos Szeredi 
132b3e19d92SNick Piggin /*
133b3e19d92SNick Piggin  * vfsmount lock must be held for read
134b3e19d92SNick Piggin  */
13583adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
136b3e19d92SNick Piggin {
137b3e19d92SNick Piggin #ifdef CONFIG_SMP
13868e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
139b3e19d92SNick Piggin #else
140b3e19d92SNick Piggin 	preempt_disable();
14168e8a9feSAl Viro 	mnt->mnt_count += n;
142b3e19d92SNick Piggin 	preempt_enable();
143b3e19d92SNick Piggin #endif
144b3e19d92SNick Piggin }
145b3e19d92SNick Piggin 
146b3e19d92SNick Piggin /*
147b3e19d92SNick Piggin  * vfsmount lock must be held for write
148b3e19d92SNick Piggin  */
14983adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt)
150b3e19d92SNick Piggin {
151b3e19d92SNick Piggin #ifdef CONFIG_SMP
152f03c6599SAl Viro 	unsigned int count = 0;
153b3e19d92SNick Piggin 	int cpu;
154b3e19d92SNick Piggin 
155b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
15668e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
157b3e19d92SNick Piggin 	}
158b3e19d92SNick Piggin 
159b3e19d92SNick Piggin 	return count;
160b3e19d92SNick Piggin #else
16168e8a9feSAl Viro 	return mnt->mnt_count;
162b3e19d92SNick Piggin #endif
163b3e19d92SNick Piggin }
164b3e19d92SNick Piggin 
165b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
1661da177e4SLinus Torvalds {
167c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
168c63181e6SAl Viro 	if (mnt) {
16973cd49ecSMiklos Szeredi 		int err;
17073cd49ecSMiklos Szeredi 
171c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
17288b38782SLi Zefan 		if (err)
17388b38782SLi Zefan 			goto out_free_cache;
17488b38782SLi Zefan 
17588b38782SLi Zefan 		if (name) {
176c63181e6SAl Viro 			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
177c63181e6SAl Viro 			if (!mnt->mnt_devname)
17888b38782SLi Zefan 				goto out_free_id;
17973cd49ecSMiklos Szeredi 		}
18073cd49ecSMiklos Szeredi 
181b3e19d92SNick Piggin #ifdef CONFIG_SMP
182c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
183c63181e6SAl Viro 		if (!mnt->mnt_pcp)
184b3e19d92SNick Piggin 			goto out_free_devname;
185b3e19d92SNick Piggin 
186c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
187b3e19d92SNick Piggin #else
188c63181e6SAl Viro 		mnt->mnt_count = 1;
189c63181e6SAl Viro 		mnt->mnt_writers = 0;
190b3e19d92SNick Piggin #endif
191b3e19d92SNick Piggin 
192c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_hash);
193c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
194c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
195c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
196c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
197c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
198c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
199c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2002504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY
2012504c5d6SAndreas Gruenbacher 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
2022504c5d6SAndreas Gruenbacher #endif
2031da177e4SLinus Torvalds 	}
204c63181e6SAl Viro 	return mnt;
20588b38782SLi Zefan 
206d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
207d3ef3d73Snpiggin@suse.de out_free_devname:
208c63181e6SAl Viro 	kfree(mnt->mnt_devname);
209d3ef3d73Snpiggin@suse.de #endif
21088b38782SLi Zefan out_free_id:
211c63181e6SAl Viro 	mnt_free_id(mnt);
21288b38782SLi Zefan out_free_cache:
213c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
21488b38782SLi Zefan 	return NULL;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2178366025eSDave Hansen /*
2188366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2198366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2208366025eSDave Hansen  * We must keep track of when those operations start
2218366025eSDave Hansen  * (for permission checks) and when they end, so that
2228366025eSDave Hansen  * we can determine when writes are able to occur to
2238366025eSDave Hansen  * a filesystem.
2248366025eSDave Hansen  */
2253d733633SDave Hansen /*
2263d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2273d733633SDave Hansen  * @mnt: the mount to check for its write status
2283d733633SDave Hansen  *
2293d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2303d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2313d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2323d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2333d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2343d733633SDave Hansen  * r/w.
2353d733633SDave Hansen  */
2363d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2373d733633SDave Hansen {
2382e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2392e4b7fcdSDave Hansen 		return 1;
2402e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
2412e4b7fcdSDave Hansen 		return 1;
2422e4b7fcdSDave Hansen 	return 0;
2433d733633SDave Hansen }
2443d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2453d733633SDave Hansen 
24683adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2473d733633SDave Hansen {
248d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
24968e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
250d3ef3d73Snpiggin@suse.de #else
25168e8a9feSAl Viro 	mnt->mnt_writers++;
252d3ef3d73Snpiggin@suse.de #endif
2533d733633SDave Hansen }
2543d733633SDave Hansen 
25583adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2563d733633SDave Hansen {
257d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
25868e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
259d3ef3d73Snpiggin@suse.de #else
26068e8a9feSAl Viro 	mnt->mnt_writers--;
261d3ef3d73Snpiggin@suse.de #endif
262d3ef3d73Snpiggin@suse.de }
263d3ef3d73Snpiggin@suse.de 
26483adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
265d3ef3d73Snpiggin@suse.de {
266d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
267d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2683d733633SDave Hansen 	int cpu;
2693d733633SDave Hansen 
2703d733633SDave Hansen 	for_each_possible_cpu(cpu) {
27168e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
2723d733633SDave Hansen 	}
2733d733633SDave Hansen 
274d3ef3d73Snpiggin@suse.de 	return count;
275d3ef3d73Snpiggin@suse.de #else
276d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
277d3ef3d73Snpiggin@suse.de #endif
2783d733633SDave Hansen }
2793d733633SDave Hansen 
2804ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
2814ed5e82fSMiklos Szeredi {
2824ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
2834ed5e82fSMiklos Szeredi 		return 1;
2844ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
2854ed5e82fSMiklos Szeredi 	smp_rmb();
2864ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
2874ed5e82fSMiklos Szeredi }
2884ed5e82fSMiklos Szeredi 
2893d733633SDave Hansen /*
290eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
291eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
292eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
293eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
2943d733633SDave Hansen  */
2958366025eSDave Hansen /**
296eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
29783adc753SAl Viro  * @m: the mount on which to take a write
2988366025eSDave Hansen  *
299eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
300eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
301eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
302eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
303eb04c282SJan Kara  * called. This is effectively a refcount.
3048366025eSDave Hansen  */
305eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3068366025eSDave Hansen {
30783adc753SAl Viro 	struct mount *mnt = real_mount(m);
3083d733633SDave Hansen 	int ret = 0;
3093d733633SDave Hansen 
310d3ef3d73Snpiggin@suse.de 	preempt_disable();
311c6653a83SNick Piggin 	mnt_inc_writers(mnt);
312d3ef3d73Snpiggin@suse.de 	/*
313c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
314d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
315d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
316d3ef3d73Snpiggin@suse.de 	 */
317d3ef3d73Snpiggin@suse.de 	smp_mb();
3181e75529eSMiao Xie 	while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
319d3ef3d73Snpiggin@suse.de 		cpu_relax();
320d3ef3d73Snpiggin@suse.de 	/*
321d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
322d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
323d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
324d3ef3d73Snpiggin@suse.de 	 */
325d3ef3d73Snpiggin@suse.de 	smp_rmb();
3264ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
327c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3283d733633SDave Hansen 		ret = -EROFS;
3293d733633SDave Hansen 	}
330d3ef3d73Snpiggin@suse.de 	preempt_enable();
331eb04c282SJan Kara 
332eb04c282SJan Kara 	return ret;
333eb04c282SJan Kara }
334eb04c282SJan Kara 
335eb04c282SJan Kara /**
336eb04c282SJan Kara  * mnt_want_write - get write access to a mount
337eb04c282SJan Kara  * @m: the mount on which to take a write
338eb04c282SJan Kara  *
339eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
340eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
341eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
342eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
343eb04c282SJan Kara  */
344eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
345eb04c282SJan Kara {
346eb04c282SJan Kara 	int ret;
347eb04c282SJan Kara 
348eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
349eb04c282SJan Kara 	ret = __mnt_want_write(m);
350eb04c282SJan Kara 	if (ret)
351eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3523d733633SDave Hansen 	return ret;
3538366025eSDave Hansen }
3548366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3558366025eSDave Hansen 
3568366025eSDave Hansen /**
35796029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
35896029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
35996029c4eSnpiggin@suse.de  *
36096029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
36196029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
36296029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
36396029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
36496029c4eSnpiggin@suse.de  *
36596029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
36696029c4eSnpiggin@suse.de  * drop the reference.
36796029c4eSnpiggin@suse.de  */
36896029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
36996029c4eSnpiggin@suse.de {
37096029c4eSnpiggin@suse.de 	/* superblock may be r/o */
37196029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
37296029c4eSnpiggin@suse.de 		return -EROFS;
37396029c4eSnpiggin@suse.de 	preempt_disable();
37483adc753SAl Viro 	mnt_inc_writers(real_mount(mnt));
37596029c4eSnpiggin@suse.de 	preempt_enable();
37696029c4eSnpiggin@suse.de 	return 0;
37796029c4eSnpiggin@suse.de }
37896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
37996029c4eSnpiggin@suse.de 
38096029c4eSnpiggin@suse.de /**
381eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
382eb04c282SJan Kara  * @file: the file who's mount on which to take a write
383eb04c282SJan Kara  *
384eb04c282SJan Kara  * This is like __mnt_want_write, but it takes a file and can
385eb04c282SJan Kara  * do some optimisations if the file is open for write already
386eb04c282SJan Kara  */
387eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
388eb04c282SJan Kara {
389496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
390eb04c282SJan Kara 
391eb04c282SJan Kara 	if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
392eb04c282SJan Kara 		return __mnt_want_write(file->f_path.mnt);
393eb04c282SJan Kara 	else
394eb04c282SJan Kara 		return mnt_clone_write(file->f_path.mnt);
395eb04c282SJan Kara }
396eb04c282SJan Kara 
397eb04c282SJan Kara /**
39896029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
39996029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
40096029c4eSnpiggin@suse.de  *
40196029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
40296029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
40396029c4eSnpiggin@suse.de  */
40496029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
40596029c4eSnpiggin@suse.de {
406eb04c282SJan Kara 	int ret;
407eb04c282SJan Kara 
408eb04c282SJan Kara 	sb_start_write(file->f_path.mnt->mnt_sb);
409eb04c282SJan Kara 	ret = __mnt_want_write_file(file);
410eb04c282SJan Kara 	if (ret)
411eb04c282SJan Kara 		sb_end_write(file->f_path.mnt->mnt_sb);
412eb04c282SJan Kara 	return ret;
41396029c4eSnpiggin@suse.de }
41496029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
41596029c4eSnpiggin@suse.de 
41696029c4eSnpiggin@suse.de /**
417eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4188366025eSDave Hansen  * @mnt: the mount on which to give up write access
4198366025eSDave Hansen  *
4208366025eSDave Hansen  * Tells the low-level filesystem that we are done
4218366025eSDave Hansen  * performing writes to it.  Must be matched with
422eb04c282SJan Kara  * __mnt_want_write() call above.
4238366025eSDave Hansen  */
424eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4258366025eSDave Hansen {
426d3ef3d73Snpiggin@suse.de 	preempt_disable();
42783adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
428d3ef3d73Snpiggin@suse.de 	preempt_enable();
4298366025eSDave Hansen }
430eb04c282SJan Kara 
431eb04c282SJan Kara /**
432eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
433eb04c282SJan Kara  * @mnt: the mount on which to give up write access
434eb04c282SJan Kara  *
435eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
436eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
437eb04c282SJan Kara  * mnt_want_write() call above.
438eb04c282SJan Kara  */
439eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
440eb04c282SJan Kara {
441eb04c282SJan Kara 	__mnt_drop_write(mnt);
442eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
443eb04c282SJan Kara }
4448366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4458366025eSDave Hansen 
446eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
447eb04c282SJan Kara {
448eb04c282SJan Kara 	__mnt_drop_write(file->f_path.mnt);
449eb04c282SJan Kara }
450eb04c282SJan Kara 
4512a79f17eSAl Viro void mnt_drop_write_file(struct file *file)
4522a79f17eSAl Viro {
4532a79f17eSAl Viro 	mnt_drop_write(file->f_path.mnt);
4542a79f17eSAl Viro }
4552a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4562a79f17eSAl Viro 
45783adc753SAl Viro static int mnt_make_readonly(struct mount *mnt)
4588366025eSDave Hansen {
4593d733633SDave Hansen 	int ret = 0;
4603d733633SDave Hansen 
461962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
46283adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
463d3ef3d73Snpiggin@suse.de 	/*
464d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
465d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
466d3ef3d73Snpiggin@suse.de 	 */
467d3ef3d73Snpiggin@suse.de 	smp_mb();
468d3ef3d73Snpiggin@suse.de 
469d3ef3d73Snpiggin@suse.de 	/*
470d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
471d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
472d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
473d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
474d3ef3d73Snpiggin@suse.de 	 *
475d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
476d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
477d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
478d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
479d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
480d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
481d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
482d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
483d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
484d3ef3d73Snpiggin@suse.de 	 */
485c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
486d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
487d3ef3d73Snpiggin@suse.de 	else
48883adc753SAl Viro 		mnt->mnt.mnt_flags |= MNT_READONLY;
489d3ef3d73Snpiggin@suse.de 	/*
490d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
491d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
492d3ef3d73Snpiggin@suse.de 	 */
493d3ef3d73Snpiggin@suse.de 	smp_wmb();
49483adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
495962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
4963d733633SDave Hansen 	return ret;
4973d733633SDave Hansen }
4988366025eSDave Hansen 
49983adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt)
5002e4b7fcdSDave Hansen {
501962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
50283adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
503962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
5042e4b7fcdSDave Hansen }
5052e4b7fcdSDave Hansen 
5064ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5074ed5e82fSMiklos Szeredi {
5084ed5e82fSMiklos Szeredi 	struct mount *mnt;
5094ed5e82fSMiklos Szeredi 	int err = 0;
5104ed5e82fSMiklos Szeredi 
5118e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5128e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5138e8b8796SMiklos Szeredi 		return -EBUSY;
5148e8b8796SMiklos Szeredi 
515962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
5164ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5174ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
5184ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
5194ed5e82fSMiklos Szeredi 			smp_mb();
5204ed5e82fSMiklos Szeredi 			if (mnt_get_writers(mnt) > 0) {
5214ed5e82fSMiklos Szeredi 				err = -EBUSY;
5224ed5e82fSMiklos Szeredi 				break;
5234ed5e82fSMiklos Szeredi 			}
5244ed5e82fSMiklos Szeredi 		}
5254ed5e82fSMiklos Szeredi 	}
5268e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5278e8b8796SMiklos Szeredi 		err = -EBUSY;
5288e8b8796SMiklos Szeredi 
5294ed5e82fSMiklos Szeredi 	if (!err) {
5304ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
5314ed5e82fSMiklos Szeredi 		smp_wmb();
5324ed5e82fSMiklos Szeredi 	}
5334ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5344ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
5354ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
5364ed5e82fSMiklos Szeredi 	}
537962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
5384ed5e82fSMiklos Szeredi 
5394ed5e82fSMiklos Szeredi 	return err;
5404ed5e82fSMiklos Szeredi }
5414ed5e82fSMiklos Szeredi 
542b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
5431da177e4SLinus Torvalds {
54452ba1621SAl Viro 	kfree(mnt->mnt_devname);
54573cd49ecSMiklos Szeredi 	mnt_free_id(mnt);
546d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
54768e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
548d3ef3d73Snpiggin@suse.de #endif
549b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds /*
553a05964f3SRam Pai  * find the first or last mount at @dentry on vfsmount @mnt depending on
554a05964f3SRam Pai  * @dir. If @dir is set return the first mount else return the last mount.
55599b7db7bSNick Piggin  * vfsmount_lock must be held for read or write.
5561da177e4SLinus Torvalds  */
557c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
558a05964f3SRam Pai 			      int dir)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
5611da177e4SLinus Torvalds 	struct list_head *tmp = head;
562c7105365SAl Viro 	struct mount *p, *found = NULL;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	for (;;) {
565a05964f3SRam Pai 		tmp = dir ? tmp->next : tmp->prev;
5661da177e4SLinus Torvalds 		p = NULL;
5671da177e4SLinus Torvalds 		if (tmp == head)
5681da177e4SLinus Torvalds 			break;
5691b8e5564SAl Viro 		p = list_entry(tmp, struct mount, mnt_hash);
570a73324daSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) {
571a05964f3SRam Pai 			found = p;
5721da177e4SLinus Torvalds 			break;
5731da177e4SLinus Torvalds 		}
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 	return found;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
578a05964f3SRam Pai /*
579f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
580f015f126SDavid Howells  *
581f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
582f015f126SDavid Howells  * following mounts:
583f015f126SDavid Howells  *
584f015f126SDavid Howells  * mount /dev/sda1 /mnt
585f015f126SDavid Howells  * mount /dev/sda2 /mnt
586f015f126SDavid Howells  * mount /dev/sda3 /mnt
587f015f126SDavid Howells  *
588f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
589f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
590f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
591f015f126SDavid Howells  *
592f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
593a05964f3SRam Pai  */
5941c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
595a05964f3SRam Pai {
596c7105365SAl Viro 	struct mount *child_mnt;
59799b7db7bSNick Piggin 
598962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
599c7105365SAl Viro 	child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
600c7105365SAl Viro 	if (child_mnt) {
601c7105365SAl Viro 		mnt_add_count(child_mnt, 1);
602962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
603c7105365SAl Viro 		return &child_mnt->mnt;
604c7105365SAl Viro 	} else {
605962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
606c7105365SAl Viro 		return NULL;
607c7105365SAl Viro 	}
608a05964f3SRam Pai }
609a05964f3SRam Pai 
61084d17192SAl Viro static struct mountpoint *new_mountpoint(struct dentry *dentry)
61184d17192SAl Viro {
61284d17192SAl Viro 	struct list_head *chain = mountpoint_hashtable + hash(NULL, dentry);
61384d17192SAl Viro 	struct mountpoint *mp;
61484d17192SAl Viro 
61584d17192SAl Viro 	list_for_each_entry(mp, chain, m_hash) {
61684d17192SAl Viro 		if (mp->m_dentry == dentry) {
61784d17192SAl Viro 			/* might be worth a WARN_ON() */
61884d17192SAl Viro 			if (d_unlinked(dentry))
61984d17192SAl Viro 				return ERR_PTR(-ENOENT);
62084d17192SAl Viro 			mp->m_count++;
62184d17192SAl Viro 			return mp;
62284d17192SAl Viro 		}
62384d17192SAl Viro 	}
62484d17192SAl Viro 
62584d17192SAl Viro 	mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
62684d17192SAl Viro 	if (!mp)
62784d17192SAl Viro 		return ERR_PTR(-ENOMEM);
62884d17192SAl Viro 
62984d17192SAl Viro 	spin_lock(&dentry->d_lock);
63084d17192SAl Viro 	if (d_unlinked(dentry)) {
63184d17192SAl Viro 		spin_unlock(&dentry->d_lock);
63284d17192SAl Viro 		kfree(mp);
63384d17192SAl Viro 		return ERR_PTR(-ENOENT);
63484d17192SAl Viro 	}
63584d17192SAl Viro 	dentry->d_flags |= DCACHE_MOUNTED;
63684d17192SAl Viro 	spin_unlock(&dentry->d_lock);
63784d17192SAl Viro 	mp->m_dentry = dentry;
63884d17192SAl Viro 	mp->m_count = 1;
63984d17192SAl Viro 	list_add(&mp->m_hash, chain);
64084d17192SAl Viro 	return mp;
64184d17192SAl Viro }
64284d17192SAl Viro 
64384d17192SAl Viro static void put_mountpoint(struct mountpoint *mp)
64484d17192SAl Viro {
64584d17192SAl Viro 	if (!--mp->m_count) {
64684d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
64784d17192SAl Viro 		spin_lock(&dentry->d_lock);
64884d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
64984d17192SAl Viro 		spin_unlock(&dentry->d_lock);
65084d17192SAl Viro 		list_del(&mp->m_hash);
65184d17192SAl Viro 		kfree(mp);
65284d17192SAl Viro 	}
65384d17192SAl Viro }
65484d17192SAl Viro 
655143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
6561da177e4SLinus Torvalds {
6576b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
66099b7db7bSNick Piggin /*
66199b7db7bSNick Piggin  * vfsmount lock must be held for write
66299b7db7bSNick Piggin  */
6636b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
6645addc5ddSAl Viro {
6655addc5ddSAl Viro 	if (ns) {
6665addc5ddSAl Viro 		ns->event = ++event;
6675addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
6685addc5ddSAl Viro 	}
6695addc5ddSAl Viro }
6705addc5ddSAl Viro 
67199b7db7bSNick Piggin /*
67299b7db7bSNick Piggin  * vfsmount lock must be held for write
67399b7db7bSNick Piggin  */
6746b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
6755addc5ddSAl Viro {
6765addc5ddSAl Viro 	if (ns && ns->event != event) {
6775addc5ddSAl Viro 		ns->event = event;
6785addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
6795addc5ddSAl Viro 	}
6805addc5ddSAl Viro }
6815addc5ddSAl Viro 
68299b7db7bSNick Piggin /*
68399b7db7bSNick Piggin  * vfsmount lock must be held for write
68499b7db7bSNick Piggin  */
685419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path)
6861da177e4SLinus Torvalds {
687a73324daSAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
6880714a533SAl Viro 	old_path->mnt = &mnt->mnt_parent->mnt;
6890714a533SAl Viro 	mnt->mnt_parent = mnt;
690a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
6916b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
6921b8e5564SAl Viro 	list_del_init(&mnt->mnt_hash);
69384d17192SAl Viro 	put_mountpoint(mnt->mnt_mp);
69484d17192SAl Viro 	mnt->mnt_mp = NULL;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
69799b7db7bSNick Piggin /*
69899b7db7bSNick Piggin  * vfsmount lock must be held for write
69999b7db7bSNick Piggin  */
70084d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
70184d17192SAl Viro 			struct mountpoint *mp,
70244d964d6SAl Viro 			struct mount *child_mnt)
703b90fa9aeSRam Pai {
70484d17192SAl Viro 	mp->m_count++;
7053a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
70684d17192SAl Viro 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
7073a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
70884d17192SAl Viro 	child_mnt->mnt_mp = mp;
709b90fa9aeSRam Pai }
710b90fa9aeSRam Pai 
71199b7db7bSNick Piggin /*
71299b7db7bSNick Piggin  * vfsmount lock must be held for write
71399b7db7bSNick Piggin  */
71484d17192SAl Viro static void attach_mnt(struct mount *mnt,
71584d17192SAl Viro 			struct mount *parent,
71684d17192SAl Viro 			struct mountpoint *mp)
7171da177e4SLinus Torvalds {
71884d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
7191b8e5564SAl Viro 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
72084d17192SAl Viro 			hash(&parent->mnt, mp->m_dentry));
72184d17192SAl Viro 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
722b90fa9aeSRam Pai }
723b90fa9aeSRam Pai 
724b90fa9aeSRam Pai /*
72599b7db7bSNick Piggin  * vfsmount lock must be held for write
726b90fa9aeSRam Pai  */
7274b2619a5SAl Viro static void commit_tree(struct mount *mnt)
728b90fa9aeSRam Pai {
7290714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
73083adc753SAl Viro 	struct mount *m;
731b90fa9aeSRam Pai 	LIST_HEAD(head);
732143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
733b90fa9aeSRam Pai 
7340714a533SAl Viro 	BUG_ON(parent == mnt);
735b90fa9aeSRam Pai 
7361a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
737f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
738143c8c91SAl Viro 		m->mnt_ns = n;
739f03c6599SAl Viro 
740b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
741b90fa9aeSRam Pai 
7421b8e5564SAl Viro 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
743a73324daSAl Viro 				hash(&parent->mnt, mnt->mnt_mountpoint));
7446b41d536SAl Viro 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
7456b3286edSKirill Korotaev 	touch_mnt_namespace(n);
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
748909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
7491da177e4SLinus Torvalds {
7506b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
7516b41d536SAl Viro 	if (next == &p->mnt_mounts) {
7521da177e4SLinus Torvalds 		while (1) {
753909b0a88SAl Viro 			if (p == root)
7541da177e4SLinus Torvalds 				return NULL;
7556b41d536SAl Viro 			next = p->mnt_child.next;
7566b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
7571da177e4SLinus Torvalds 				break;
7580714a533SAl Viro 			p = p->mnt_parent;
7591da177e4SLinus Torvalds 		}
7601da177e4SLinus Torvalds 	}
7616b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
764315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
7659676f0c6SRam Pai {
7666b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
7676b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
7686b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
7696b41d536SAl Viro 		prev = p->mnt_mounts.prev;
7709676f0c6SRam Pai 	}
7719676f0c6SRam Pai 	return p;
7729676f0c6SRam Pai }
7739676f0c6SRam Pai 
7749d412a43SAl Viro struct vfsmount *
7759d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
7769d412a43SAl Viro {
777b105e270SAl Viro 	struct mount *mnt;
7789d412a43SAl Viro 	struct dentry *root;
7799d412a43SAl Viro 
7809d412a43SAl Viro 	if (!type)
7819d412a43SAl Viro 		return ERR_PTR(-ENODEV);
7829d412a43SAl Viro 
7839d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
7849d412a43SAl Viro 	if (!mnt)
7859d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
7869d412a43SAl Viro 
7879d412a43SAl Viro 	if (flags & MS_KERNMOUNT)
788b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
7899d412a43SAl Viro 
7909d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
7919d412a43SAl Viro 	if (IS_ERR(root)) {
7929d412a43SAl Viro 		free_vfsmnt(mnt);
7939d412a43SAl Viro 		return ERR_CAST(root);
7949d412a43SAl Viro 	}
7959d412a43SAl Viro 
796b105e270SAl Viro 	mnt->mnt.mnt_root = root;
797b105e270SAl Viro 	mnt->mnt.mnt_sb = root->d_sb;
798a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
7990714a533SAl Viro 	mnt->mnt_parent = mnt;
800962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
80139f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
802962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
803b105e270SAl Viro 	return &mnt->mnt;
8049d412a43SAl Viro }
8059d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
8069d412a43SAl Viro 
80787129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
80836341f64SRam Pai 					int flag)
8091da177e4SLinus Torvalds {
81087129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
811be34d1a3SDavid Howells 	struct mount *mnt;
812be34d1a3SDavid Howells 	int err;
8131da177e4SLinus Torvalds 
814be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
815be34d1a3SDavid Howells 	if (!mnt)
816be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
817be34d1a3SDavid Howells 
8187a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
81915169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
820719f5d7fSMiklos Szeredi 	else
82115169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
822719f5d7fSMiklos Szeredi 
82315169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
824be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
825719f5d7fSMiklos Szeredi 		if (err)
826719f5d7fSMiklos Szeredi 			goto out_free;
827719f5d7fSMiklos Szeredi 	}
828719f5d7fSMiklos Szeredi 
82987129cc0SAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
830132c94e3SEric W. Biederman 	/* Don't allow unprivileged users to change mount flags */
831132c94e3SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY))
832132c94e3SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
833132c94e3SEric W. Biederman 
8345ff9d8a6SEric W. Biederman 	/* Don't allow unprivileged users to reveal what is under a mount */
8355ff9d8a6SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire))
8365ff9d8a6SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCKED;
8375ff9d8a6SEric W. Biederman 
8381da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
839b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
840b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
841a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8420714a533SAl Viro 	mnt->mnt_parent = mnt;
843962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
84439f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
845962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
846b90fa9aeSRam Pai 
8477a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
8487a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
8496776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
85032301920SAl Viro 		mnt->mnt_master = old;
851fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
8528aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
853fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
8546776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
855d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
8566776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
857d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
8585afe0022SRam Pai 	}
859b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
8600f0afb1dSAl Viro 		set_mnt_shared(mnt);
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
8631da177e4SLinus Torvalds 	 * as the original if that was on one */
86436341f64SRam Pai 	if (flag & CL_EXPIRE) {
8656776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
8666776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
8671da177e4SLinus Torvalds 	}
868be34d1a3SDavid Howells 
869cb338d06SAl Viro 	return mnt;
870719f5d7fSMiklos Szeredi 
871719f5d7fSMiklos Szeredi  out_free:
872719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
873be34d1a3SDavid Howells 	return ERR_PTR(err);
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
87683adc753SAl Viro static inline void mntfree(struct mount *mnt)
8771da177e4SLinus Torvalds {
87883adc753SAl Viro 	struct vfsmount *m = &mnt->mnt;
87983adc753SAl Viro 	struct super_block *sb = m->mnt_sb;
880b3e19d92SNick Piggin 
8813d733633SDave Hansen 	/*
8823d733633SDave Hansen 	 * This probably indicates that somebody messed
8833d733633SDave Hansen 	 * up a mnt_want/drop_write() pair.  If this
8843d733633SDave Hansen 	 * happens, the filesystem was probably unable
8853d733633SDave Hansen 	 * to make r/w->r/o transitions.
8863d733633SDave Hansen 	 */
887d3ef3d73Snpiggin@suse.de 	/*
888b3e19d92SNick Piggin 	 * The locking used to deal with mnt_count decrement provides barriers,
889b3e19d92SNick Piggin 	 * so mnt_get_writers() below is safe.
890d3ef3d73Snpiggin@suse.de 	 */
891c6653a83SNick Piggin 	WARN_ON(mnt_get_writers(mnt));
89283adc753SAl Viro 	fsnotify_vfsmount_delete(m);
89383adc753SAl Viro 	dput(m->mnt_root);
89483adc753SAl Viro 	free_vfsmnt(mnt);
8951da177e4SLinus Torvalds 	deactivate_super(sb);
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
898900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
8997b7b1aceSAl Viro {
900b3e19d92SNick Piggin put_again:
901f03c6599SAl Viro #ifdef CONFIG_SMP
902962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
903f7a99c5bSAl Viro 	if (likely(mnt->mnt_ns)) {
904f7a99c5bSAl Viro 		/* shouldn't be the last one */
905aa9c0e07SAl Viro 		mnt_add_count(mnt, -1);
906962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
90799b7db7bSNick Piggin 		return;
908b3e19d92SNick Piggin 	}
909962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
910b3e19d92SNick Piggin 
911962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
912aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
913b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
914962830dfSAndi Kleen 		br_write_unlock(&vfsmount_lock);
91599b7db7bSNick Piggin 		return;
91699b7db7bSNick Piggin 	}
917b3e19d92SNick Piggin #else
918aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
919b3e19d92SNick Piggin 	if (likely(mnt_get_count(mnt)))
920b3e19d92SNick Piggin 		return;
921962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
922f03c6599SAl Viro #endif
923863d684fSAl Viro 	if (unlikely(mnt->mnt_pinned)) {
924863d684fSAl Viro 		mnt_add_count(mnt, mnt->mnt_pinned + 1);
925863d684fSAl Viro 		mnt->mnt_pinned = 0;
926962830dfSAndi Kleen 		br_write_unlock(&vfsmount_lock);
927900148dcSAl Viro 		acct_auto_close_mnt(&mnt->mnt);
928b3e19d92SNick Piggin 		goto put_again;
929b3e19d92SNick Piggin 	}
930962830dfSAndi Kleen 
93139f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
932962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
933b3e19d92SNick Piggin 	mntfree(mnt);
934b3e19d92SNick Piggin }
935b3e19d92SNick Piggin 
936b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
937b3e19d92SNick Piggin {
938b3e19d92SNick Piggin 	if (mnt) {
939863d684fSAl Viro 		struct mount *m = real_mount(mnt);
940b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
941863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
942863d684fSAl Viro 			m->mnt_expiry_mark = 0;
943863d684fSAl Viro 		mntput_no_expire(m);
944b3e19d92SNick Piggin 	}
945b3e19d92SNick Piggin }
946b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
947b3e19d92SNick Piggin 
948b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
949b3e19d92SNick Piggin {
950b3e19d92SNick Piggin 	if (mnt)
95183adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
952b3e19d92SNick Piggin 	return mnt;
953b3e19d92SNick Piggin }
954b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
955b3e19d92SNick Piggin 
9567b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
9577b7b1aceSAl Viro {
958962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
959863d684fSAl Viro 	real_mount(mnt)->mnt_pinned++;
960962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
9617b7b1aceSAl Viro }
9627b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
9637b7b1aceSAl Viro 
964863d684fSAl Viro void mnt_unpin(struct vfsmount *m)
9657b7b1aceSAl Viro {
966863d684fSAl Viro 	struct mount *mnt = real_mount(m);
967962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
9687b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
969863d684fSAl Viro 		mnt_add_count(mnt, 1);
9707b7b1aceSAl Viro 		mnt->mnt_pinned--;
9717b7b1aceSAl Viro 	}
972962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
9737b7b1aceSAl Viro }
9747b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
9751da177e4SLinus Torvalds 
976b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
977b3b304a2SMiklos Szeredi {
978b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
979b3b304a2SMiklos Szeredi }
980b3b304a2SMiklos Szeredi 
981b3b304a2SMiklos Szeredi /*
982b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
983b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
984b3b304a2SMiklos Szeredi  *
985b3b304a2SMiklos Szeredi  * See also save_mount_options().
986b3b304a2SMiklos Szeredi  */
98734c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root)
988b3b304a2SMiklos Szeredi {
9892a32cebdSAl Viro 	const char *options;
9902a32cebdSAl Viro 
9912a32cebdSAl Viro 	rcu_read_lock();
99234c80b1dSAl Viro 	options = rcu_dereference(root->d_sb->s_options);
993b3b304a2SMiklos Szeredi 
994b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
995b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
996b3b304a2SMiklos Szeredi 		mangle(m, options);
997b3b304a2SMiklos Szeredi 	}
9982a32cebdSAl Viro 	rcu_read_unlock();
999b3b304a2SMiklos Szeredi 
1000b3b304a2SMiklos Szeredi 	return 0;
1001b3b304a2SMiklos Szeredi }
1002b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
1003b3b304a2SMiklos Szeredi 
1004b3b304a2SMiklos Szeredi /*
1005b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
1006b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
1007b3b304a2SMiklos Szeredi  *
1008b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
1009b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
1010b3b304a2SMiklos Szeredi  * remount fails.
1011b3b304a2SMiklos Szeredi  *
1012b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
1013b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
1014b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
1015b3b304a2SMiklos Szeredi  * any more.
1016b3b304a2SMiklos Szeredi  */
1017b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
1018b3b304a2SMiklos Szeredi {
10192a32cebdSAl Viro 	BUG_ON(sb->s_options);
10202a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1021b3b304a2SMiklos Szeredi }
1022b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
1023b3b304a2SMiklos Szeredi 
10242a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
10252a32cebdSAl Viro {
10262a32cebdSAl Viro 	char *old = sb->s_options;
10272a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
10282a32cebdSAl Viro 	if (old) {
10292a32cebdSAl Viro 		synchronize_rcu();
10302a32cebdSAl Viro 		kfree(old);
10312a32cebdSAl Viro 	}
10322a32cebdSAl Viro }
10332a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
10342a32cebdSAl Viro 
1035a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
10360226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
10371da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
10381da177e4SLinus Torvalds {
10396ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
10401da177e4SLinus Torvalds 
1041390c6843SRam Pai 	down_read(&namespace_sem);
1042a1a2c409SMiklos Szeredi 	return seq_list_start(&p->ns->list, *pos);
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
10461da177e4SLinus Torvalds {
10476ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
1048b0765fb8SPavel Emelianov 
1049a1a2c409SMiklos Szeredi 	return seq_list_next(v, &p->ns->list, pos);
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
10531da177e4SLinus Torvalds {
1054390c6843SRam Pai 	up_read(&namespace_sem);
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10570226f492SAl Viro static int m_show(struct seq_file *m, void *v)
10589f5596afSAl Viro {
10596ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
10601a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
10610226f492SAl Viro 	return p->show(m, &r->mnt);
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
1064a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
10651da177e4SLinus Torvalds 	.start	= m_start,
10661da177e4SLinus Torvalds 	.next	= m_next,
10671da177e4SLinus Torvalds 	.stop	= m_stop,
10680226f492SAl Viro 	.show	= m_show,
1069b4629fe2SChuck Lever };
1070a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1071b4629fe2SChuck Lever 
10721da177e4SLinus Torvalds /**
10731da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
10741da177e4SLinus Torvalds  * @mnt: root of mount tree
10751da177e4SLinus Torvalds  *
10761da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
10771da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
10781da177e4SLinus Torvalds  * busy.
10791da177e4SLinus Torvalds  */
1080909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
10811da177e4SLinus Torvalds {
1082909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
108336341f64SRam Pai 	int actual_refs = 0;
108436341f64SRam Pai 	int minimum_refs = 0;
1085315fc83eSAl Viro 	struct mount *p;
1086909b0a88SAl Viro 	BUG_ON(!m);
10871da177e4SLinus Torvalds 
1088b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1089962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
1090909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
109183adc753SAl Viro 		actual_refs += mnt_get_count(p);
10921da177e4SLinus Torvalds 		minimum_refs += 2;
10931da177e4SLinus Torvalds 	}
1094962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
10971da177e4SLinus Torvalds 		return 0;
1098e3474a8eSIan Kent 
1099e3474a8eSIan Kent 	return 1;
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds /**
11051da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
11061da177e4SLinus Torvalds  * @mnt: root of mount
11071da177e4SLinus Torvalds  *
11081da177e4SLinus Torvalds  * This is called to check if a mount point has any
11091da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
11101da177e4SLinus Torvalds  * mount has sub mounts this will return busy
11111da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
11121da177e4SLinus Torvalds  *
11131da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
11141da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
11151da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
11161da177e4SLinus Torvalds  */
11171da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
11181da177e4SLinus Torvalds {
1119e3474a8eSIan Kent 	int ret = 1;
11208ad08d8aSAl Viro 	down_read(&namespace_sem);
1121962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
11221ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1123e3474a8eSIan Kent 		ret = 0;
1124962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
11258ad08d8aSAl Viro 	up_read(&namespace_sem);
1126a05964f3SRam Pai 	return ret;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
11301da177e4SLinus Torvalds 
1131e3197d83SAl Viro static LIST_HEAD(unmounted);	/* protected by namespace_sem */
1132e3197d83SAl Viro 
113397216be0SAl Viro static void namespace_unlock(void)
11341da177e4SLinus Torvalds {
1135d5e50f74SAl Viro 	struct mount *mnt;
113697216be0SAl Viro 	LIST_HEAD(head);
113797216be0SAl Viro 
113897216be0SAl Viro 	if (likely(list_empty(&unmounted))) {
113997216be0SAl Viro 		up_write(&namespace_sem);
114097216be0SAl Viro 		return;
114197216be0SAl Viro 	}
114297216be0SAl Viro 
114397216be0SAl Viro 	list_splice_init(&unmounted, &head);
114497216be0SAl Viro 	up_write(&namespace_sem);
114597216be0SAl Viro 
114697216be0SAl Viro 	while (!list_empty(&head)) {
114797216be0SAl Viro 		mnt = list_first_entry(&head, struct mount, mnt_hash);
11481b8e5564SAl Viro 		list_del_init(&mnt->mnt_hash);
1149676da58dSAl Viro 		if (mnt_has_parent(mnt)) {
115070fbcdf4SRam Pai 			struct dentry *dentry;
1151863d684fSAl Viro 			struct mount *m;
115299b7db7bSNick Piggin 
1153962830dfSAndi Kleen 			br_write_lock(&vfsmount_lock);
1154a73324daSAl Viro 			dentry = mnt->mnt_mountpoint;
1155863d684fSAl Viro 			m = mnt->mnt_parent;
1156a73324daSAl Viro 			mnt->mnt_mountpoint = mnt->mnt.mnt_root;
11570714a533SAl Viro 			mnt->mnt_parent = mnt;
11587c4b93d8SAl Viro 			m->mnt_ghosts--;
1159962830dfSAndi Kleen 			br_write_unlock(&vfsmount_lock);
116070fbcdf4SRam Pai 			dput(dentry);
1161863d684fSAl Viro 			mntput(&m->mnt);
11621da177e4SLinus Torvalds 		}
1163d5e50f74SAl Viro 		mntput(&mnt->mnt);
116470fbcdf4SRam Pai 	}
116570fbcdf4SRam Pai }
116670fbcdf4SRam Pai 
116797216be0SAl Viro static inline void namespace_lock(void)
1168e3197d83SAl Viro {
116997216be0SAl Viro 	down_write(&namespace_sem);
1170e3197d83SAl Viro }
1171e3197d83SAl Viro 
117299b7db7bSNick Piggin /*
117399b7db7bSNick Piggin  * vfsmount lock must be held for write
117499b7db7bSNick Piggin  * namespace_sem must be held for write
117599b7db7bSNick Piggin  */
1176328e6d90SAl Viro void umount_tree(struct mount *mnt, int propagate)
117770fbcdf4SRam Pai {
11787b8a53fdSAl Viro 	LIST_HEAD(tmp_list);
1179315fc83eSAl Viro 	struct mount *p;
118070fbcdf4SRam Pai 
1181909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt))
11821b8e5564SAl Viro 		list_move(&p->mnt_hash, &tmp_list);
118370fbcdf4SRam Pai 
1184a05964f3SRam Pai 	if (propagate)
11857b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1186a05964f3SRam Pai 
11871b8e5564SAl Viro 	list_for_each_entry(p, &tmp_list, mnt_hash) {
11886776db3dSAl Viro 		list_del_init(&p->mnt_expire);
11891a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1190143c8c91SAl Viro 		__touch_mnt_namespace(p->mnt_ns);
119163d37a84SAl Viro 		p->mnt_ns = NULL;
11926b41d536SAl Viro 		list_del_init(&p->mnt_child);
1193676da58dSAl Viro 		if (mnt_has_parent(p)) {
1194863d684fSAl Viro 			p->mnt_parent->mnt_ghosts++;
119584d17192SAl Viro 			put_mountpoint(p->mnt_mp);
119684d17192SAl Viro 			p->mnt_mp = NULL;
11977c4b93d8SAl Viro 		}
11980f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
11991da177e4SLinus Torvalds 	}
1200328e6d90SAl Viro 	list_splice(&tmp_list, &unmounted);
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
1203b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1204c35038beSAl Viro 
12051ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
12061da177e4SLinus Torvalds {
12071ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
12081da177e4SLinus Torvalds 	int retval;
12091da177e4SLinus Torvalds 
12101ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
12111da177e4SLinus Torvalds 	if (retval)
12121da177e4SLinus Torvalds 		return retval;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	/*
12151da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
12161da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
12171da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
12181da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
12191da177e4SLinus Torvalds 	 */
12201da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
12211ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
12221da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
12231da177e4SLinus Torvalds 			return -EINVAL;
12241da177e4SLinus Torvalds 
1225b3e19d92SNick Piggin 		/*
1226b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1227b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1228b3e19d92SNick Piggin 		 */
1229962830dfSAndi Kleen 		br_write_lock(&vfsmount_lock);
123083adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1231962830dfSAndi Kleen 			br_write_unlock(&vfsmount_lock);
12321da177e4SLinus Torvalds 			return -EBUSY;
1233b3e19d92SNick Piggin 		}
1234962830dfSAndi Kleen 		br_write_unlock(&vfsmount_lock);
12351da177e4SLinus Torvalds 
1236863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
12371da177e4SLinus Torvalds 			return -EAGAIN;
12381da177e4SLinus Torvalds 	}
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	/*
12411da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
12421da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
12431da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
12441da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
12451da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
12461da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
12471da177e4SLinus Torvalds 	 * about for the moment.
12481da177e4SLinus Torvalds 	 */
12491da177e4SLinus Torvalds 
125042faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
125142faad99SAl Viro 		sb->s_op->umount_begin(sb);
125242faad99SAl Viro 	}
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	/*
12551da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
12561da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
12571da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
12581da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
12591da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
12601da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
12611da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
12621da177e4SLinus Torvalds 	 */
12631ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
12641da177e4SLinus Torvalds 		/*
12651da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
12661da177e4SLinus Torvalds 		 * we just try to remount it readonly.
12671da177e4SLinus Torvalds 		 */
12681da177e4SLinus Torvalds 		down_write(&sb->s_umount);
12694aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
12701da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
12711da177e4SLinus Torvalds 		up_write(&sb->s_umount);
12721da177e4SLinus Torvalds 		return retval;
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 
127597216be0SAl Viro 	namespace_lock();
1276962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
12775addc5ddSAl Viro 	event++;
12781da177e4SLinus Torvalds 
1279c35038beSAl Viro 	if (!(flags & MNT_DETACH))
1280b54b9be7SAl Viro 		shrink_submounts(mnt);
1281c35038beSAl Viro 
12821da177e4SLinus Torvalds 	retval = -EBUSY;
1283a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
12841a4eeaf2SAl Viro 		if (!list_empty(&mnt->mnt_list))
1285328e6d90SAl Viro 			umount_tree(mnt, 1);
12861da177e4SLinus Torvalds 		retval = 0;
12871da177e4SLinus Torvalds 	}
1288962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
1289e3197d83SAl Viro 	namespace_unlock();
12901da177e4SLinus Torvalds 	return retval;
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds /*
12949b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
12959b40bc90SAl Viro  */
12969b40bc90SAl Viro static inline bool may_mount(void)
12979b40bc90SAl Viro {
12989b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
12999b40bc90SAl Viro }
13009b40bc90SAl Viro 
13019b40bc90SAl Viro /*
13021da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
13031da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
13041da177e4SLinus Torvalds  *
13051da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
13061da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
13071da177e4SLinus Torvalds  */
13081da177e4SLinus Torvalds 
1309bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
13101da177e4SLinus Torvalds {
13112d8f3038SAl Viro 	struct path path;
1312900148dcSAl Viro 	struct mount *mnt;
13131da177e4SLinus Torvalds 	int retval;
1314db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
13151da177e4SLinus Torvalds 
1316db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1317db1f05bbSMiklos Szeredi 		return -EINVAL;
1318db1f05bbSMiklos Szeredi 
13199b40bc90SAl Viro 	if (!may_mount())
13209b40bc90SAl Viro 		return -EPERM;
13219b40bc90SAl Viro 
1322db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1323db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1324db1f05bbSMiklos Szeredi 
1325db1f05bbSMiklos Szeredi 	retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
13261da177e4SLinus Torvalds 	if (retval)
13271da177e4SLinus Torvalds 		goto out;
1328900148dcSAl Viro 	mnt = real_mount(path.mnt);
13291da177e4SLinus Torvalds 	retval = -EINVAL;
13302d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
13311da177e4SLinus Torvalds 		goto dput_and_out;
1332143c8c91SAl Viro 	if (!check_mnt(mnt))
13331da177e4SLinus Torvalds 		goto dput_and_out;
13345ff9d8a6SEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
13355ff9d8a6SEric W. Biederman 		goto dput_and_out;
13361da177e4SLinus Torvalds 
1337900148dcSAl Viro 	retval = do_umount(mnt, flags);
13381da177e4SLinus Torvalds dput_and_out:
1339429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
13402d8f3038SAl Viro 	dput(path.dentry);
1341900148dcSAl Viro 	mntput_no_expire(mnt);
13421da177e4SLinus Torvalds out:
13431da177e4SLinus Torvalds 	return retval;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds /*
13491da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
13501da177e4SLinus Torvalds  */
1351bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	return sys_umount(name, 0);
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds #endif
13571da177e4SLinus Torvalds 
13584ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
13598823c079SEric W. Biederman {
13604ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
13614ce5d2b1SEric W. Biederman 	struct inode *inode = dentry->d_inode;
13620bb80f24SDavid Howells 	struct proc_ns *ei;
13638823c079SEric W. Biederman 
13648823c079SEric W. Biederman 	if (!proc_ns_inode(inode))
13658823c079SEric W. Biederman 		return false;
13668823c079SEric W. Biederman 
13670bb80f24SDavid Howells 	ei = get_proc_ns(inode);
13688823c079SEric W. Biederman 	if (ei->ns_ops != &mntns_operations)
13698823c079SEric W. Biederman 		return false;
13708823c079SEric W. Biederman 
13714ce5d2b1SEric W. Biederman 	return true;
13724ce5d2b1SEric W. Biederman }
13734ce5d2b1SEric W. Biederman 
13744ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
13754ce5d2b1SEric W. Biederman {
13764ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
13774ce5d2b1SEric W. Biederman 	 * mount namespace loop?
13784ce5d2b1SEric W. Biederman 	 */
13794ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
13804ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
13814ce5d2b1SEric W. Biederman 		return false;
13824ce5d2b1SEric W. Biederman 
13834ce5d2b1SEric W. Biederman 	mnt_ns = get_proc_ns(dentry->d_inode)->ns;
13848823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
13858823c079SEric W. Biederman }
13868823c079SEric W. Biederman 
138787129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
138836341f64SRam Pai 					int flag)
13891da177e4SLinus Torvalds {
139084d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
13911da177e4SLinus Torvalds 
13924ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
13934ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
13944ce5d2b1SEric W. Biederman 
13954ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1396be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
13979676f0c6SRam Pai 
139836341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1399be34d1a3SDavid Howells 	if (IS_ERR(q))
1400be34d1a3SDavid Howells 		return q;
1401be34d1a3SDavid Howells 
14025ff9d8a6SEric W. Biederman 	q->mnt.mnt_flags &= ~MNT_LOCKED;
1403a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	p = mnt;
14066b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1407315fc83eSAl Viro 		struct mount *s;
14087ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
14091da177e4SLinus Torvalds 			continue;
14101da177e4SLinus Torvalds 
1411909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
14124ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
14134ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
14144ce5d2b1SEric W. Biederman 				s = skip_mnt_tree(s);
14154ce5d2b1SEric W. Biederman 				continue;
14164ce5d2b1SEric W. Biederman 			}
14174ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
14184ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
14199676f0c6SRam Pai 				s = skip_mnt_tree(s);
14209676f0c6SRam Pai 				continue;
14219676f0c6SRam Pai 			}
14220714a533SAl Viro 			while (p != s->mnt_parent) {
14230714a533SAl Viro 				p = p->mnt_parent;
14240714a533SAl Viro 				q = q->mnt_parent;
14251da177e4SLinus Torvalds 			}
142687129cc0SAl Viro 			p = s;
142784d17192SAl Viro 			parent = q;
142887129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1429be34d1a3SDavid Howells 			if (IS_ERR(q))
1430be34d1a3SDavid Howells 				goto out;
1431962830dfSAndi Kleen 			br_write_lock(&vfsmount_lock);
14321a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
143384d17192SAl Viro 			attach_mnt(q, parent, p->mnt_mp);
1434962830dfSAndi Kleen 			br_write_unlock(&vfsmount_lock);
14351da177e4SLinus Torvalds 		}
14361da177e4SLinus Torvalds 	}
14371da177e4SLinus Torvalds 	return res;
1438be34d1a3SDavid Howells out:
14391da177e4SLinus Torvalds 	if (res) {
1440962830dfSAndi Kleen 		br_write_lock(&vfsmount_lock);
1441328e6d90SAl Viro 		umount_tree(res, 0);
1442962830dfSAndi Kleen 		br_write_unlock(&vfsmount_lock);
14431da177e4SLinus Torvalds 	}
1444be34d1a3SDavid Howells 	return q;
14451da177e4SLinus Torvalds }
14461da177e4SLinus Torvalds 
1447be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1448be34d1a3SDavid Howells 
1449589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
14508aec0809SAl Viro {
1451cb338d06SAl Viro 	struct mount *tree;
145297216be0SAl Viro 	namespace_lock();
145387129cc0SAl Viro 	tree = copy_tree(real_mount(path->mnt), path->dentry,
145487129cc0SAl Viro 			 CL_COPY_ALL | CL_PRIVATE);
1455328e6d90SAl Viro 	namespace_unlock();
1456be34d1a3SDavid Howells 	if (IS_ERR(tree))
1457be34d1a3SDavid Howells 		return NULL;
1458be34d1a3SDavid Howells 	return &tree->mnt;
14598aec0809SAl Viro }
14608aec0809SAl Viro 
14618aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
14628aec0809SAl Viro {
146397216be0SAl Viro 	namespace_lock();
1464962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
1465328e6d90SAl Viro 	umount_tree(real_mount(mnt), 0);
1466962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
14673ab6abeeSAl Viro 	namespace_unlock();
14688aec0809SAl Viro }
14698aec0809SAl Viro 
14701f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
14711f707137SAl Viro 		   struct vfsmount *root)
14721f707137SAl Viro {
14731a4eeaf2SAl Viro 	struct mount *mnt;
14741f707137SAl Viro 	int res = f(root, arg);
14751f707137SAl Viro 	if (res)
14761f707137SAl Viro 		return res;
14771a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
14781a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
14791f707137SAl Viro 		if (res)
14801f707137SAl Viro 			return res;
14811f707137SAl Viro 	}
14821f707137SAl Viro 	return 0;
14831f707137SAl Viro }
14841f707137SAl Viro 
14854b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1486719f5d7fSMiklos Szeredi {
1487315fc83eSAl Viro 	struct mount *p;
1488719f5d7fSMiklos Szeredi 
1489909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1490fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
14914b8b21f4SAl Viro 			mnt_release_group_id(p);
1492719f5d7fSMiklos Szeredi 	}
1493719f5d7fSMiklos Szeredi }
1494719f5d7fSMiklos Szeredi 
14954b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1496719f5d7fSMiklos Szeredi {
1497315fc83eSAl Viro 	struct mount *p;
1498719f5d7fSMiklos Szeredi 
1499909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1500fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
15014b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1502719f5d7fSMiklos Szeredi 			if (err) {
15034b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1504719f5d7fSMiklos Szeredi 				return err;
1505719f5d7fSMiklos Szeredi 			}
1506719f5d7fSMiklos Szeredi 		}
1507719f5d7fSMiklos Szeredi 	}
1508719f5d7fSMiklos Szeredi 
1509719f5d7fSMiklos Szeredi 	return 0;
1510719f5d7fSMiklos Szeredi }
1511719f5d7fSMiklos Szeredi 
1512b90fa9aeSRam Pai /*
1513b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1514b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
151521444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
151621444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
151721444403SRam Pai  *  		   (done when source_mnt is moved)
1518b90fa9aeSRam Pai  *
1519b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1520b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
15219676f0c6SRam Pai  * ---------------------------------------------------------------------------
1522b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
15239676f0c6SRam Pai  * |**************************************************************************
15249676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
15259676f0c6SRam Pai  * | dest     |               |                |                |            |
15269676f0c6SRam Pai  * |   |      |               |                |                |            |
15279676f0c6SRam Pai  * |   v      |               |                |                |            |
15289676f0c6SRam Pai  * |**************************************************************************
15299676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
15305afe0022SRam Pai  * |          |               |                |                |            |
15319676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
15329676f0c6SRam Pai  * ***************************************************************************
1533b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1534b90fa9aeSRam Pai  * destination mount.
1535b90fa9aeSRam Pai  *
1536b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1537b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1538b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1539b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1540b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1541b90fa9aeSRam Pai  *       mount.
15425afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
15435afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
15445afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
15455afe0022SRam Pai  *       is marked as 'shared and slave'.
15465afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
15475afe0022SRam Pai  * 	 source mount.
15485afe0022SRam Pai  *
15499676f0c6SRam Pai  * ---------------------------------------------------------------------------
155021444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
15519676f0c6SRam Pai  * |**************************************************************************
15529676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
15539676f0c6SRam Pai  * | dest     |               |                |                |            |
15549676f0c6SRam Pai  * |   |      |               |                |                |            |
15559676f0c6SRam Pai  * |   v      |               |                |                |            |
15569676f0c6SRam Pai  * |**************************************************************************
15579676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
15585afe0022SRam Pai  * |          |               |                |                |            |
15599676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
15609676f0c6SRam Pai  * ***************************************************************************
15615afe0022SRam Pai  *
15625afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
15635afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
156421444403SRam Pai  * (+*)  the mount is moved to the destination.
15655afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
15665afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
15675afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
15685afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1569b90fa9aeSRam Pai  *
1570b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1571b90fa9aeSRam Pai  * applied to each mount in the tree.
1572b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1573b90fa9aeSRam Pai  * in allocations.
1574b90fa9aeSRam Pai  */
15750fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
157684d17192SAl Viro 			struct mount *dest_mnt,
157784d17192SAl Viro 			struct mountpoint *dest_mp,
157884d17192SAl Viro 			struct path *parent_path)
1579b90fa9aeSRam Pai {
1580b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
1581315fc83eSAl Viro 	struct mount *child, *p;
1582719f5d7fSMiklos Szeredi 	int err;
1583b90fa9aeSRam Pai 
1584fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
15850fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
1586719f5d7fSMiklos Szeredi 		if (err)
1587719f5d7fSMiklos Szeredi 			goto out;
1588719f5d7fSMiklos Szeredi 	}
158984d17192SAl Viro 	err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1590719f5d7fSMiklos Szeredi 	if (err)
1591719f5d7fSMiklos Szeredi 		goto out_cleanup_ids;
1592b90fa9aeSRam Pai 
1593962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
1594df1a1ad2SAl Viro 
1595fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
1596909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
15970f0afb1dSAl Viro 			set_mnt_shared(p);
1598b90fa9aeSRam Pai 	}
15991a390689SAl Viro 	if (parent_path) {
16000fb54e50SAl Viro 		detach_mnt(source_mnt, parent_path);
160184d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
1602143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
160321444403SRam Pai 	} else {
160484d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
16050fb54e50SAl Viro 		commit_tree(source_mnt);
160621444403SRam Pai 	}
1607b90fa9aeSRam Pai 
16081b8e5564SAl Viro 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
16091b8e5564SAl Viro 		list_del_init(&child->mnt_hash);
16104b2619a5SAl Viro 		commit_tree(child);
1611b90fa9aeSRam Pai 	}
1612962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
161399b7db7bSNick Piggin 
1614b90fa9aeSRam Pai 	return 0;
1615719f5d7fSMiklos Szeredi 
1616719f5d7fSMiklos Szeredi  out_cleanup_ids:
1617fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt))
16180fb54e50SAl Viro 		cleanup_group_ids(source_mnt, NULL);
1619719f5d7fSMiklos Szeredi  out:
1620719f5d7fSMiklos Szeredi 	return err;
1621b90fa9aeSRam Pai }
1622b90fa9aeSRam Pai 
162384d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
1624b12cea91SAl Viro {
1625b12cea91SAl Viro 	struct vfsmount *mnt;
162684d17192SAl Viro 	struct dentry *dentry = path->dentry;
1627b12cea91SAl Viro retry:
162884d17192SAl Viro 	mutex_lock(&dentry->d_inode->i_mutex);
162984d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
163084d17192SAl Viro 		mutex_unlock(&dentry->d_inode->i_mutex);
163184d17192SAl Viro 		return ERR_PTR(-ENOENT);
1632b12cea91SAl Viro 	}
163397216be0SAl Viro 	namespace_lock();
1634b12cea91SAl Viro 	mnt = lookup_mnt(path);
163584d17192SAl Viro 	if (likely(!mnt)) {
163684d17192SAl Viro 		struct mountpoint *mp = new_mountpoint(dentry);
163784d17192SAl Viro 		if (IS_ERR(mp)) {
163897216be0SAl Viro 			namespace_unlock();
163984d17192SAl Viro 			mutex_unlock(&dentry->d_inode->i_mutex);
164084d17192SAl Viro 			return mp;
164184d17192SAl Viro 		}
164284d17192SAl Viro 		return mp;
164384d17192SAl Viro 	}
164497216be0SAl Viro 	namespace_unlock();
1645b12cea91SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
1646b12cea91SAl Viro 	path_put(path);
1647b12cea91SAl Viro 	path->mnt = mnt;
164884d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
1649b12cea91SAl Viro 	goto retry;
1650b12cea91SAl Viro }
1651b12cea91SAl Viro 
165284d17192SAl Viro static void unlock_mount(struct mountpoint *where)
1653b12cea91SAl Viro {
165484d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
165584d17192SAl Viro 	put_mountpoint(where);
1656328e6d90SAl Viro 	namespace_unlock();
165784d17192SAl Viro 	mutex_unlock(&dentry->d_inode->i_mutex);
1658b12cea91SAl Viro }
1659b12cea91SAl Viro 
166084d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
16611da177e4SLinus Torvalds {
166295bc5f25SAl Viro 	if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
16631da177e4SLinus Torvalds 		return -EINVAL;
16641da177e4SLinus Torvalds 
166584d17192SAl Viro 	if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
166695bc5f25SAl Viro 	      S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
16671da177e4SLinus Torvalds 		return -ENOTDIR;
16681da177e4SLinus Torvalds 
166984d17192SAl Viro 	return attach_recursive_mnt(mnt, p, mp, NULL);
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds /*
16737a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
16747a2e8a8fSValerie Aurora  */
16757a2e8a8fSValerie Aurora 
16767a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags)
16777a2e8a8fSValerie Aurora {
16787c6e984dSRoman Borisov 	int type = flags & ~(MS_REC | MS_SILENT);
16797a2e8a8fSValerie Aurora 
16807a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
16817a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
16827a2e8a8fSValerie Aurora 		return 0;
16837a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
16847a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
16857a2e8a8fSValerie Aurora 		return 0;
16867a2e8a8fSValerie Aurora 	return type;
16877a2e8a8fSValerie Aurora }
16887a2e8a8fSValerie Aurora 
16897a2e8a8fSValerie Aurora /*
169007b20889SRam Pai  * recursively change the type of the mountpoint.
169107b20889SRam Pai  */
16920a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
169307b20889SRam Pai {
1694315fc83eSAl Viro 	struct mount *m;
16954b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
169607b20889SRam Pai 	int recurse = flag & MS_REC;
16977a2e8a8fSValerie Aurora 	int type;
1698719f5d7fSMiklos Szeredi 	int err = 0;
169907b20889SRam Pai 
17002d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
170107b20889SRam Pai 		return -EINVAL;
170207b20889SRam Pai 
17037a2e8a8fSValerie Aurora 	type = flags_to_propagation_type(flag);
17047a2e8a8fSValerie Aurora 	if (!type)
17057a2e8a8fSValerie Aurora 		return -EINVAL;
17067a2e8a8fSValerie Aurora 
170797216be0SAl Viro 	namespace_lock();
1708719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1709719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1710719f5d7fSMiklos Szeredi 		if (err)
1711719f5d7fSMiklos Szeredi 			goto out_unlock;
1712719f5d7fSMiklos Szeredi 	}
1713719f5d7fSMiklos Szeredi 
1714962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
1715909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
17160f0afb1dSAl Viro 		change_mnt_propagation(m, type);
1717962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
1718719f5d7fSMiklos Szeredi 
1719719f5d7fSMiklos Szeredi  out_unlock:
172097216be0SAl Viro 	namespace_unlock();
1721719f5d7fSMiklos Szeredi 	return err;
172207b20889SRam Pai }
172307b20889SRam Pai 
17245ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
17255ff9d8a6SEric W. Biederman {
17265ff9d8a6SEric W. Biederman 	struct mount *child;
17275ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
17285ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
17295ff9d8a6SEric W. Biederman 			continue;
17305ff9d8a6SEric W. Biederman 
17315ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
17325ff9d8a6SEric W. Biederman 			return true;
17335ff9d8a6SEric W. Biederman 	}
17345ff9d8a6SEric W. Biederman 	return false;
17355ff9d8a6SEric W. Biederman }
17365ff9d8a6SEric W. Biederman 
173707b20889SRam Pai /*
17381da177e4SLinus Torvalds  * do loopback mount.
17391da177e4SLinus Torvalds  */
1740808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
17412dafe1c4SEric Sandeen 				int recurse)
17421da177e4SLinus Torvalds {
17432d92ab3cSAl Viro 	struct path old_path;
174484d17192SAl Viro 	struct mount *mnt = NULL, *old, *parent;
174584d17192SAl Viro 	struct mountpoint *mp;
174657eccb83SAl Viro 	int err;
17471da177e4SLinus Torvalds 	if (!old_name || !*old_name)
17481da177e4SLinus Torvalds 		return -EINVAL;
1749815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
17501da177e4SLinus Torvalds 	if (err)
17511da177e4SLinus Torvalds 		return err;
17521da177e4SLinus Torvalds 
17538823c079SEric W. Biederman 	err = -EINVAL;
17544ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
17558823c079SEric W. Biederman 		goto out;
17568823c079SEric W. Biederman 
175784d17192SAl Viro 	mp = lock_mount(path);
175884d17192SAl Viro 	err = PTR_ERR(mp);
175984d17192SAl Viro 	if (IS_ERR(mp))
17609676f0c6SRam Pai 		goto out;
17619676f0c6SRam Pai 
176287129cc0SAl Viro 	old = real_mount(old_path.mnt);
176384d17192SAl Viro 	parent = real_mount(path->mnt);
176487129cc0SAl Viro 
1765b12cea91SAl Viro 	err = -EINVAL;
1766fc7be130SAl Viro 	if (IS_MNT_UNBINDABLE(old))
1767b12cea91SAl Viro 		goto out2;
1768b12cea91SAl Viro 
176984d17192SAl Viro 	if (!check_mnt(parent) || !check_mnt(old))
1770b12cea91SAl Viro 		goto out2;
1771ccd48bc7SAl Viro 
17725ff9d8a6SEric W. Biederman 	if (!recurse && has_locked_children(old, old_path.dentry))
17735ff9d8a6SEric W. Biederman 		goto out2;
17745ff9d8a6SEric W. Biederman 
17751da177e4SLinus Torvalds 	if (recurse)
17764ce5d2b1SEric W. Biederman 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
17771da177e4SLinus Torvalds 	else
177887129cc0SAl Viro 		mnt = clone_mnt(old, old_path.dentry, 0);
17791da177e4SLinus Torvalds 
1780be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
1781be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
1782e9c5d8a5SAndrey Vagin 		goto out2;
1783be34d1a3SDavid Howells 	}
1784ccd48bc7SAl Viro 
17855ff9d8a6SEric W. Biederman 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
17865ff9d8a6SEric W. Biederman 
178784d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
17881da177e4SLinus Torvalds 	if (err) {
1789962830dfSAndi Kleen 		br_write_lock(&vfsmount_lock);
1790328e6d90SAl Viro 		umount_tree(mnt, 0);
1791962830dfSAndi Kleen 		br_write_unlock(&vfsmount_lock);
17925b83d2c5SRam Pai 	}
1793b12cea91SAl Viro out2:
179484d17192SAl Viro 	unlock_mount(mp);
1795ccd48bc7SAl Viro out:
17962d92ab3cSAl Viro 	path_put(&old_path);
17971da177e4SLinus Torvalds 	return err;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
18002e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
18012e4b7fcdSDave Hansen {
18022e4b7fcdSDave Hansen 	int error = 0;
18032e4b7fcdSDave Hansen 	int readonly_request = 0;
18042e4b7fcdSDave Hansen 
18052e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
18062e4b7fcdSDave Hansen 		readonly_request = 1;
18072e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
18082e4b7fcdSDave Hansen 		return 0;
18092e4b7fcdSDave Hansen 
181090563b19SEric W. Biederman 	if (mnt->mnt_flags & MNT_LOCK_READONLY)
181190563b19SEric W. Biederman 		return -EPERM;
181290563b19SEric W. Biederman 
18132e4b7fcdSDave Hansen 	if (readonly_request)
181483adc753SAl Viro 		error = mnt_make_readonly(real_mount(mnt));
18152e4b7fcdSDave Hansen 	else
181683adc753SAl Viro 		__mnt_unmake_readonly(real_mount(mnt));
18172e4b7fcdSDave Hansen 	return error;
18182e4b7fcdSDave Hansen }
18192e4b7fcdSDave Hansen 
18201da177e4SLinus Torvalds /*
18211da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
18221da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
18231da177e4SLinus Torvalds  * on it - tough luck.
18241da177e4SLinus Torvalds  */
18250a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
18261da177e4SLinus Torvalds 		      void *data)
18271da177e4SLinus Torvalds {
18281da177e4SLinus Torvalds 	int err;
18292d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
1830143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
18311da177e4SLinus Torvalds 
1832143c8c91SAl Viro 	if (!check_mnt(mnt))
18331da177e4SLinus Torvalds 		return -EINVAL;
18341da177e4SLinus Torvalds 
18352d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
18361da177e4SLinus Torvalds 		return -EINVAL;
18371da177e4SLinus Torvalds 
1838ff36fe2cSEric Paris 	err = security_sb_remount(sb, data);
1839ff36fe2cSEric Paris 	if (err)
1840ff36fe2cSEric Paris 		return err;
1841ff36fe2cSEric Paris 
18421da177e4SLinus Torvalds 	down_write(&sb->s_umount);
18432e4b7fcdSDave Hansen 	if (flags & MS_BIND)
18442d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
184557eccb83SAl Viro 	else if (!capable(CAP_SYS_ADMIN))
184657eccb83SAl Viro 		err = -EPERM;
18474aa98cf7SAl Viro 	else
18481da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
18497b43a79fSAl Viro 	if (!err) {
1850962830dfSAndi Kleen 		br_write_lock(&vfsmount_lock);
1851143c8c91SAl Viro 		mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
1852143c8c91SAl Viro 		mnt->mnt.mnt_flags = mnt_flags;
1853962830dfSAndi Kleen 		br_write_unlock(&vfsmount_lock);
18547b43a79fSAl Viro 	}
18551da177e4SLinus Torvalds 	up_write(&sb->s_umount);
18560e55a7ccSDan Williams 	if (!err) {
1857962830dfSAndi Kleen 		br_write_lock(&vfsmount_lock);
1858143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1859962830dfSAndi Kleen 		br_write_unlock(&vfsmount_lock);
18600e55a7ccSDan Williams 	}
18611da177e4SLinus Torvalds 	return err;
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds 
1864cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
18659676f0c6SRam Pai {
1866315fc83eSAl Viro 	struct mount *p;
1867909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1868fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
18699676f0c6SRam Pai 			return 1;
18709676f0c6SRam Pai 	}
18719676f0c6SRam Pai 	return 0;
18729676f0c6SRam Pai }
18739676f0c6SRam Pai 
1874808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name)
18751da177e4SLinus Torvalds {
18762d92ab3cSAl Viro 	struct path old_path, parent_path;
1877676da58dSAl Viro 	struct mount *p;
18780fb54e50SAl Viro 	struct mount *old;
187984d17192SAl Viro 	struct mountpoint *mp;
188057eccb83SAl Viro 	int err;
18811da177e4SLinus Torvalds 	if (!old_name || !*old_name)
18821da177e4SLinus Torvalds 		return -EINVAL;
18832d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
18841da177e4SLinus Torvalds 	if (err)
18851da177e4SLinus Torvalds 		return err;
18861da177e4SLinus Torvalds 
188784d17192SAl Viro 	mp = lock_mount(path);
188884d17192SAl Viro 	err = PTR_ERR(mp);
188984d17192SAl Viro 	if (IS_ERR(mp))
1890cc53ce53SDavid Howells 		goto out;
1891cc53ce53SDavid Howells 
1892143c8c91SAl Viro 	old = real_mount(old_path.mnt);
1893fc7be130SAl Viro 	p = real_mount(path->mnt);
1894143c8c91SAl Viro 
18951da177e4SLinus Torvalds 	err = -EINVAL;
1896fc7be130SAl Viro 	if (!check_mnt(p) || !check_mnt(old))
18971da177e4SLinus Torvalds 		goto out1;
18981da177e4SLinus Torvalds 
18995ff9d8a6SEric W. Biederman 	if (old->mnt.mnt_flags & MNT_LOCKED)
19005ff9d8a6SEric W. Biederman 		goto out1;
19015ff9d8a6SEric W. Biederman 
19021da177e4SLinus Torvalds 	err = -EINVAL;
19032d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
190421444403SRam Pai 		goto out1;
19051da177e4SLinus Torvalds 
1906676da58dSAl Viro 	if (!mnt_has_parent(old))
190721444403SRam Pai 		goto out1;
19081da177e4SLinus Torvalds 
19092d92ab3cSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
19102d92ab3cSAl Viro 	      S_ISDIR(old_path.dentry->d_inode->i_mode))
191121444403SRam Pai 		goto out1;
191221444403SRam Pai 	/*
191321444403SRam Pai 	 * Don't move a mount residing in a shared parent.
191421444403SRam Pai 	 */
1915fc7be130SAl Viro 	if (IS_MNT_SHARED(old->mnt_parent))
191621444403SRam Pai 		goto out1;
19179676f0c6SRam Pai 	/*
19189676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
19199676f0c6SRam Pai 	 * mount which is shared.
19209676f0c6SRam Pai 	 */
1921fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
19229676f0c6SRam Pai 		goto out1;
19231da177e4SLinus Torvalds 	err = -ELOOP;
1924fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
1925676da58dSAl Viro 		if (p == old)
192621444403SRam Pai 			goto out1;
19271da177e4SLinus Torvalds 
192884d17192SAl Viro 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
19294ac91378SJan Blunck 	if (err)
193021444403SRam Pai 		goto out1;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
19331da177e4SLinus Torvalds 	 * automatically */
19346776db3dSAl Viro 	list_del_init(&old->mnt_expire);
19351da177e4SLinus Torvalds out1:
193684d17192SAl Viro 	unlock_mount(mp);
19371da177e4SLinus Torvalds out:
19381da177e4SLinus Torvalds 	if (!err)
19391a390689SAl Viro 		path_put(&parent_path);
19402d92ab3cSAl Viro 	path_put(&old_path);
19411da177e4SLinus Torvalds 	return err;
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds 
19449d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
19459d412a43SAl Viro {
19469d412a43SAl Viro 	int err;
19479d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
19489d412a43SAl Viro 	if (subtype) {
19499d412a43SAl Viro 		subtype++;
19509d412a43SAl Viro 		err = -EINVAL;
19519d412a43SAl Viro 		if (!subtype[0])
19529d412a43SAl Viro 			goto err;
19539d412a43SAl Viro 	} else
19549d412a43SAl Viro 		subtype = "";
19559d412a43SAl Viro 
19569d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
19579d412a43SAl Viro 	err = -ENOMEM;
19589d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
19599d412a43SAl Viro 		goto err;
19609d412a43SAl Viro 	return mnt;
19619d412a43SAl Viro 
19629d412a43SAl Viro  err:
19639d412a43SAl Viro 	mntput(mnt);
19649d412a43SAl Viro 	return ERR_PTR(err);
19659d412a43SAl Viro }
19669d412a43SAl Viro 
19679d412a43SAl Viro /*
19689d412a43SAl Viro  * add a mount into a namespace's mount tree
19699d412a43SAl Viro  */
197095bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
19719d412a43SAl Viro {
197284d17192SAl Viro 	struct mountpoint *mp;
197384d17192SAl Viro 	struct mount *parent;
19749d412a43SAl Viro 	int err;
19759d412a43SAl Viro 
19769d412a43SAl Viro 	mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
19779d412a43SAl Viro 
197884d17192SAl Viro 	mp = lock_mount(path);
197984d17192SAl Viro 	if (IS_ERR(mp))
198084d17192SAl Viro 		return PTR_ERR(mp);
19819d412a43SAl Viro 
198284d17192SAl Viro 	parent = real_mount(path->mnt);
19839d412a43SAl Viro 	err = -EINVAL;
198484d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
1985156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
1986156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
19879d412a43SAl Viro 			goto unlock;
1988156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
198984d17192SAl Viro 		if (!parent->mnt_ns)
1990156cacb1SAl Viro 			goto unlock;
1991156cacb1SAl Viro 	}
19929d412a43SAl Viro 
19939d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
19949d412a43SAl Viro 	err = -EBUSY;
199595bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
19969d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
19979d412a43SAl Viro 		goto unlock;
19989d412a43SAl Viro 
19999d412a43SAl Viro 	err = -EINVAL;
200095bc5f25SAl Viro 	if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
20019d412a43SAl Viro 		goto unlock;
20029d412a43SAl Viro 
200395bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
200484d17192SAl Viro 	err = graft_tree(newmnt, parent, mp);
20059d412a43SAl Viro 
20069d412a43SAl Viro unlock:
200784d17192SAl Viro 	unlock_mount(mp);
20089d412a43SAl Viro 	return err;
20099d412a43SAl Viro }
2010b1e75df4SAl Viro 
20111da177e4SLinus Torvalds /*
20121da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
20131da177e4SLinus Torvalds  * namespace's tree
20141da177e4SLinus Torvalds  */
20150c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags,
2016808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
20171da177e4SLinus Torvalds {
20180c55cfc4SEric W. Biederman 	struct file_system_type *type;
20199b40bc90SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
20201da177e4SLinus Torvalds 	struct vfsmount *mnt;
202115f9a3f3SAl Viro 	int err;
20221da177e4SLinus Torvalds 
20230c55cfc4SEric W. Biederman 	if (!fstype)
20241da177e4SLinus Torvalds 		return -EINVAL;
20251da177e4SLinus Torvalds 
20260c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
20270c55cfc4SEric W. Biederman 	if (!type)
20280c55cfc4SEric W. Biederman 		return -ENODEV;
20290c55cfc4SEric W. Biederman 
20300c55cfc4SEric W. Biederman 	if (user_ns != &init_user_ns) {
20310c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_MOUNT)) {
20320c55cfc4SEric W. Biederman 			put_filesystem(type);
20330c55cfc4SEric W. Biederman 			return -EPERM;
20340c55cfc4SEric W. Biederman 		}
20350c55cfc4SEric W. Biederman 		/* Only in special cases allow devices from mounts
20360c55cfc4SEric W. Biederman 		 * created outside the initial user namespace.
20370c55cfc4SEric W. Biederman 		 */
20380c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
20390c55cfc4SEric W. Biederman 			flags |= MS_NODEV;
20400c55cfc4SEric W. Biederman 			mnt_flags |= MNT_NODEV;
20410c55cfc4SEric W. Biederman 		}
20420c55cfc4SEric W. Biederman 	}
20430c55cfc4SEric W. Biederman 
20440c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, flags, name, data);
20450c55cfc4SEric W. Biederman 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
20460c55cfc4SEric W. Biederman 	    !mnt->mnt_sb->s_subtype)
20470c55cfc4SEric W. Biederman 		mnt = fs_set_subtype(mnt, fstype);
20480c55cfc4SEric W. Biederman 
20490c55cfc4SEric W. Biederman 	put_filesystem(type);
20501da177e4SLinus Torvalds 	if (IS_ERR(mnt))
20511da177e4SLinus Torvalds 		return PTR_ERR(mnt);
20521da177e4SLinus Torvalds 
205395bc5f25SAl Viro 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
205415f9a3f3SAl Viro 	if (err)
205515f9a3f3SAl Viro 		mntput(mnt);
205615f9a3f3SAl Viro 	return err;
20571da177e4SLinus Torvalds }
20581da177e4SLinus Torvalds 
205919a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
206019a167afSAl Viro {
20616776db3dSAl Viro 	struct mount *mnt = real_mount(m);
206219a167afSAl Viro 	int err;
206319a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
206419a167afSAl Viro 	 * expired before we get a chance to add it
206519a167afSAl Viro 	 */
20666776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
206719a167afSAl Viro 
206819a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
206919a167afSAl Viro 	    m->mnt_root == path->dentry) {
2070b1e75df4SAl Viro 		err = -ELOOP;
2071b1e75df4SAl Viro 		goto fail;
207219a167afSAl Viro 	}
207319a167afSAl Viro 
207495bc5f25SAl Viro 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2075b1e75df4SAl Viro 	if (!err)
2076b1e75df4SAl Viro 		return 0;
2077b1e75df4SAl Viro fail:
2078b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
20796776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
208097216be0SAl Viro 		namespace_lock();
2081962830dfSAndi Kleen 		br_write_lock(&vfsmount_lock);
20826776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
2083962830dfSAndi Kleen 		br_write_unlock(&vfsmount_lock);
208497216be0SAl Viro 		namespace_unlock();
208519a167afSAl Viro 	}
2086b1e75df4SAl Viro 	mntput(m);
2087b1e75df4SAl Viro 	mntput(m);
208819a167afSAl Viro 	return err;
208919a167afSAl Viro }
209019a167afSAl Viro 
2091ea5b778aSDavid Howells /**
2092ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2093ea5b778aSDavid Howells  * @mnt: The mount to list.
2094ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2095ea5b778aSDavid Howells  */
2096ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2097ea5b778aSDavid Howells {
209897216be0SAl Viro 	namespace_lock();
2099962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
2100ea5b778aSDavid Howells 
21016776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2102ea5b778aSDavid Howells 
2103962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
210497216be0SAl Viro 	namespace_unlock();
2105ea5b778aSDavid Howells }
2106ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2107ea5b778aSDavid Howells 
2108ea5b778aSDavid Howells /*
21091da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
21101da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
21111da177e4SLinus Torvalds  * here
21121da177e4SLinus Torvalds  */
21131da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
21141da177e4SLinus Torvalds {
2115761d5c38SAl Viro 	struct mount *mnt, *next;
21161da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	if (list_empty(mounts))
21191da177e4SLinus Torvalds 		return;
21201da177e4SLinus Torvalds 
212197216be0SAl Viro 	namespace_lock();
2122962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
21251da177e4SLinus Torvalds 	 * following criteria:
21261da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
21271da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
21281da177e4SLinus Torvalds 	 *   cleared by mntput())
21291da177e4SLinus Torvalds 	 */
21306776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2131863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
21321ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
21331da177e4SLinus Torvalds 			continue;
21346776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
21351da177e4SLinus Torvalds 	}
2136bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
21376776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2138143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2139328e6d90SAl Viro 		umount_tree(mnt, 1);
2140bcc5c7d2SAl Viro 	}
2141962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
21423ab6abeeSAl Viro 	namespace_unlock();
21431da177e4SLinus Torvalds }
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
21461da177e4SLinus Torvalds 
21471da177e4SLinus Torvalds /*
21485528f911STrond Myklebust  * Ripoff of 'select_parent()'
21495528f911STrond Myklebust  *
21505528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
21515528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
21525528f911STrond Myklebust  */
2153692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
21545528f911STrond Myklebust {
2155692afc31SAl Viro 	struct mount *this_parent = parent;
21565528f911STrond Myklebust 	struct list_head *next;
21575528f911STrond Myklebust 	int found = 0;
21585528f911STrond Myklebust 
21595528f911STrond Myklebust repeat:
21606b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
21615528f911STrond Myklebust resume:
21626b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
21635528f911STrond Myklebust 		struct list_head *tmp = next;
21646b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
21655528f911STrond Myklebust 
21665528f911STrond Myklebust 		next = tmp->next;
2167692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
21685528f911STrond Myklebust 			continue;
21695528f911STrond Myklebust 		/*
21705528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
21715528f911STrond Myklebust 		 */
21726b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
21735528f911STrond Myklebust 			this_parent = mnt;
21745528f911STrond Myklebust 			goto repeat;
21755528f911STrond Myklebust 		}
21765528f911STrond Myklebust 
21771ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
21786776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
21795528f911STrond Myklebust 			found++;
21805528f911STrond Myklebust 		}
21815528f911STrond Myklebust 	}
21825528f911STrond Myklebust 	/*
21835528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
21845528f911STrond Myklebust 	 */
21855528f911STrond Myklebust 	if (this_parent != parent) {
21866b41d536SAl Viro 		next = this_parent->mnt_child.next;
21870714a533SAl Viro 		this_parent = this_parent->mnt_parent;
21885528f911STrond Myklebust 		goto resume;
21895528f911STrond Myklebust 	}
21905528f911STrond Myklebust 	return found;
21915528f911STrond Myklebust }
21925528f911STrond Myklebust 
21935528f911STrond Myklebust /*
21945528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
21955528f911STrond Myklebust  * submounts of a specific parent mountpoint
219699b7db7bSNick Piggin  *
219799b7db7bSNick Piggin  * vfsmount_lock must be held for write
21985528f911STrond Myklebust  */
2199b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
22005528f911STrond Myklebust {
22015528f911STrond Myklebust 	LIST_HEAD(graveyard);
2202761d5c38SAl Viro 	struct mount *m;
22035528f911STrond Myklebust 
22045528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2205c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2206bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2207761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
22086776db3dSAl Viro 						mnt_expire);
2209143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
2210328e6d90SAl Viro 			umount_tree(m, 1);
2211bcc5c7d2SAl Viro 		}
2212bcc5c7d2SAl Viro 	}
22135528f911STrond Myklebust }
22145528f911STrond Myklebust 
22155528f911STrond Myklebust /*
22161da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
22171da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
22181da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
22191da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
22201da177e4SLinus Torvalds  */
2221b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2222b58fed8bSRam Pai 				 unsigned long n)
22231da177e4SLinus Torvalds {
22241da177e4SLinus Torvalds 	char *t = to;
22251da177e4SLinus Torvalds 	const char __user *f = from;
22261da177e4SLinus Torvalds 	char c;
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
22291da177e4SLinus Torvalds 		return n;
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	while (n) {
22321da177e4SLinus Torvalds 		if (__get_user(c, f)) {
22331da177e4SLinus Torvalds 			memset(t, 0, n);
22341da177e4SLinus Torvalds 			break;
22351da177e4SLinus Torvalds 		}
22361da177e4SLinus Torvalds 		*t++ = c;
22371da177e4SLinus Torvalds 		f++;
22381da177e4SLinus Torvalds 		n--;
22391da177e4SLinus Torvalds 	}
22401da177e4SLinus Torvalds 	return n;
22411da177e4SLinus Torvalds }
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
22441da177e4SLinus Torvalds {
22451da177e4SLinus Torvalds 	int i;
22461da177e4SLinus Torvalds 	unsigned long page;
22471da177e4SLinus Torvalds 	unsigned long size;
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds 	*where = 0;
22501da177e4SLinus Torvalds 	if (!data)
22511da177e4SLinus Torvalds 		return 0;
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
22541da177e4SLinus Torvalds 		return -ENOMEM;
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
22571da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
22581da177e4SLinus Torvalds 	 * the remainder of the page.
22591da177e4SLinus Torvalds 	 */
22601da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
22611da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
22621da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
22631da177e4SLinus Torvalds 		size = PAGE_SIZE;
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
22661da177e4SLinus Torvalds 	if (!i) {
22671da177e4SLinus Torvalds 		free_page(page);
22681da177e4SLinus Torvalds 		return -EFAULT;
22691da177e4SLinus Torvalds 	}
22701da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
22711da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
22721da177e4SLinus Torvalds 	*where = page;
22731da177e4SLinus Torvalds 	return 0;
22741da177e4SLinus Torvalds }
22751da177e4SLinus Torvalds 
2276eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where)
2277eca6f534SVegard Nossum {
2278eca6f534SVegard Nossum 	char *tmp;
2279eca6f534SVegard Nossum 
2280eca6f534SVegard Nossum 	if (!data) {
2281eca6f534SVegard Nossum 		*where = NULL;
2282eca6f534SVegard Nossum 		return 0;
2283eca6f534SVegard Nossum 	}
2284eca6f534SVegard Nossum 
2285eca6f534SVegard Nossum 	tmp = strndup_user(data, PAGE_SIZE);
2286eca6f534SVegard Nossum 	if (IS_ERR(tmp))
2287eca6f534SVegard Nossum 		return PTR_ERR(tmp);
2288eca6f534SVegard Nossum 
2289eca6f534SVegard Nossum 	*where = tmp;
2290eca6f534SVegard Nossum 	return 0;
2291eca6f534SVegard Nossum }
2292eca6f534SVegard Nossum 
22931da177e4SLinus Torvalds /*
22941da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
22951da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
22961da177e4SLinus Torvalds  *
22971da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
22981da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
22991da177e4SLinus Torvalds  * information (or be NULL).
23001da177e4SLinus Torvalds  *
23011da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
23021da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
23031da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
23041da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
23051da177e4SLinus Torvalds  * and must be discarded.
23061da177e4SLinus Torvalds  */
2307808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name,
2308808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
23091da177e4SLinus Torvalds {
23102d92ab3cSAl Viro 	struct path path;
23111da177e4SLinus Torvalds 	int retval = 0;
23121da177e4SLinus Torvalds 	int mnt_flags = 0;
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 	/* Discard magic */
23151da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
23161da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds 	/* Basic sanity checks */
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
23211da177e4SLinus Torvalds 		return -EINVAL;
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds 	if (data_page)
23241da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
23251da177e4SLinus Torvalds 
2326a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
2327a27ab9f2STetsuo Handa 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2328a27ab9f2STetsuo Handa 	if (retval)
2329a27ab9f2STetsuo Handa 		return retval;
2330a27ab9f2STetsuo Handa 
2331a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2332a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
23330d5cadb8SAl Viro 	if (!retval && !may_mount())
23340d5cadb8SAl Viro 		retval = -EPERM;
2335a27ab9f2STetsuo Handa 	if (retval)
2336a27ab9f2STetsuo Handa 		goto dput_out;
2337a27ab9f2STetsuo Handa 
2338613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2339613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
23400a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
23410a1c01c9SMatthew Garrett 
23421da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
23431da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
23441da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
23451da177e4SLinus Torvalds 	if (flags & MS_NODEV)
23461da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
23471da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
23481da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2349fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2350fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2351fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2352fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2353d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2354d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
23552e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
23562e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2357fc33a7bbSChristoph Hellwig 
23587a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2359d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2360d0adde57SMatthew Garrett 		   MS_STRICTATIME);
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
23632d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
23641da177e4SLinus Torvalds 				    data_page);
23651da177e4SLinus Torvalds 	else if (flags & MS_BIND)
23662d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
23679676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
23682d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
23691da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
23702d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
23711da177e4SLinus Torvalds 	else
23722d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
23731da177e4SLinus Torvalds 				      dev_name, data_page);
23741da177e4SLinus Torvalds dput_out:
23752d92ab3cSAl Viro 	path_put(&path);
23761da177e4SLinus Torvalds 	return retval;
23771da177e4SLinus Torvalds }
23781da177e4SLinus Torvalds 
2379771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
2380771b1371SEric W. Biederman {
238198f842e6SEric W. Biederman 	proc_free_inum(ns->proc_inum);
2382771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
2383771b1371SEric W. Biederman 	kfree(ns);
2384771b1371SEric W. Biederman }
2385771b1371SEric W. Biederman 
23868823c079SEric W. Biederman /*
23878823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
23888823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
23898823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
23908823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
23918823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
23928823c079SEric W. Biederman  */
23938823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
23948823c079SEric W. Biederman 
2395771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2396cf8d2c11STrond Myklebust {
2397cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
239898f842e6SEric W. Biederman 	int ret;
2399cf8d2c11STrond Myklebust 
2400cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2401cf8d2c11STrond Myklebust 	if (!new_ns)
2402cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
240398f842e6SEric W. Biederman 	ret = proc_alloc_inum(&new_ns->proc_inum);
240498f842e6SEric W. Biederman 	if (ret) {
240598f842e6SEric W. Biederman 		kfree(new_ns);
240698f842e6SEric W. Biederman 		return ERR_PTR(ret);
240798f842e6SEric W. Biederman 	}
24088823c079SEric W. Biederman 	new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2409cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2410cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2411cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2412cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2413cf8d2c11STrond Myklebust 	new_ns->event = 0;
2414771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
2415cf8d2c11STrond Myklebust 	return new_ns;
2416cf8d2c11STrond Myklebust }
2417cf8d2c11STrond Myklebust 
2418741a2951SJANAK DESAI /*
2419741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
2420741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
2421741a2951SJANAK DESAI  */
2422e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
2423771b1371SEric W. Biederman 		struct user_namespace *user_ns, struct fs_struct *fs)
24241da177e4SLinus Torvalds {
24256b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
24267f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2427315fc83eSAl Viro 	struct mount *p, *q;
2428be08d6d2SAl Viro 	struct mount *old = mnt_ns->root;
2429cb338d06SAl Viro 	struct mount *new;
24307a472ef4SEric W. Biederman 	int copy_flags;
24311da177e4SLinus Torvalds 
2432771b1371SEric W. Biederman 	new_ns = alloc_mnt_ns(user_ns);
2433cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2434cf8d2c11STrond Myklebust 		return new_ns;
24351da177e4SLinus Torvalds 
243697216be0SAl Viro 	namespace_lock();
24371da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
24384ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
24397a472ef4SEric W. Biederman 	if (user_ns != mnt_ns->user_ns)
2440132c94e3SEric W. Biederman 		copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
24417a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2442be34d1a3SDavid Howells 	if (IS_ERR(new)) {
2443328e6d90SAl Viro 		namespace_unlock();
2444771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
2445be34d1a3SDavid Howells 		return ERR_CAST(new);
24461da177e4SLinus Torvalds 	}
2447be08d6d2SAl Viro 	new_ns->root = new;
2448962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
24491a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
2450962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds 	/*
24531da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
24541da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
24551da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
24561da177e4SLinus Torvalds 	 */
2457909b0a88SAl Viro 	p = old;
2458cb338d06SAl Viro 	q = new;
24591da177e4SLinus Torvalds 	while (p) {
2460143c8c91SAl Viro 		q->mnt_ns = new_ns;
24611da177e4SLinus Torvalds 		if (fs) {
2462315fc83eSAl Viro 			if (&p->mnt == fs->root.mnt) {
2463315fc83eSAl Viro 				fs->root.mnt = mntget(&q->mnt);
2464315fc83eSAl Viro 				rootmnt = &p->mnt;
24651da177e4SLinus Torvalds 			}
2466315fc83eSAl Viro 			if (&p->mnt == fs->pwd.mnt) {
2467315fc83eSAl Viro 				fs->pwd.mnt = mntget(&q->mnt);
2468315fc83eSAl Viro 				pwdmnt = &p->mnt;
24691da177e4SLinus Torvalds 			}
24701da177e4SLinus Torvalds 		}
2471909b0a88SAl Viro 		p = next_mnt(p, old);
2472909b0a88SAl Viro 		q = next_mnt(q, new);
24734ce5d2b1SEric W. Biederman 		if (!q)
24744ce5d2b1SEric W. Biederman 			break;
24754ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
24764ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
24771da177e4SLinus Torvalds 	}
2478328e6d90SAl Viro 	namespace_unlock();
24791da177e4SLinus Torvalds 
24801da177e4SLinus Torvalds 	if (rootmnt)
2481f03c6599SAl Viro 		mntput(rootmnt);
24821da177e4SLinus Torvalds 	if (pwdmnt)
2483f03c6599SAl Viro 		mntput(pwdmnt);
24841da177e4SLinus Torvalds 
2485741a2951SJANAK DESAI 	return new_ns;
2486741a2951SJANAK DESAI }
2487741a2951SJANAK DESAI 
2488213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2489771b1371SEric W. Biederman 		struct user_namespace *user_ns, struct fs_struct *new_fs)
2490741a2951SJANAK DESAI {
24916b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
2492741a2951SJANAK DESAI 
2493e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
24946b3286edSKirill Korotaev 	get_mnt_ns(ns);
2495741a2951SJANAK DESAI 
2496741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
2497e3222c4eSBadari Pulavarty 		return ns;
2498741a2951SJANAK DESAI 
2499771b1371SEric W. Biederman 	new_ns = dup_mnt_ns(ns, user_ns, new_fs);
2500741a2951SJANAK DESAI 
25016b3286edSKirill Korotaev 	put_mnt_ns(ns);
2502e3222c4eSBadari Pulavarty 	return new_ns;
25031da177e4SLinus Torvalds }
25041da177e4SLinus Torvalds 
2505cf8d2c11STrond Myklebust /**
2506cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2507cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2508cf8d2c11STrond Myklebust  */
25091a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2510cf8d2c11STrond Myklebust {
2511771b1371SEric W. Biederman 	struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2512cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
25131a4eeaf2SAl Viro 		struct mount *mnt = real_mount(m);
25141a4eeaf2SAl Viro 		mnt->mnt_ns = new_ns;
2515be08d6d2SAl Viro 		new_ns->root = mnt;
2516b1983cd8SAl Viro 		list_add(&mnt->mnt_list, &new_ns->list);
2517c1334495SAl Viro 	} else {
25181a4eeaf2SAl Viro 		mntput(m);
2519cf8d2c11STrond Myklebust 	}
2520cf8d2c11STrond Myklebust 	return new_ns;
2521cf8d2c11STrond Myklebust }
2522cf8d2c11STrond Myklebust 
2523ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2524ea441d11SAl Viro {
2525ea441d11SAl Viro 	struct mnt_namespace *ns;
2526d31da0f0SAl Viro 	struct super_block *s;
2527ea441d11SAl Viro 	struct path path;
2528ea441d11SAl Viro 	int err;
2529ea441d11SAl Viro 
2530ea441d11SAl Viro 	ns = create_mnt_ns(mnt);
2531ea441d11SAl Viro 	if (IS_ERR(ns))
2532ea441d11SAl Viro 		return ERR_CAST(ns);
2533ea441d11SAl Viro 
2534ea441d11SAl Viro 	err = vfs_path_lookup(mnt->mnt_root, mnt,
2535ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2536ea441d11SAl Viro 
2537ea441d11SAl Viro 	put_mnt_ns(ns);
2538ea441d11SAl Viro 
2539ea441d11SAl Viro 	if (err)
2540ea441d11SAl Viro 		return ERR_PTR(err);
2541ea441d11SAl Viro 
2542ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
2543d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
2544d31da0f0SAl Viro 	atomic_inc(&s->s_active);
2545ea441d11SAl Viro 	mntput(path.mnt);
2546ea441d11SAl Viro 	/* lock the sucker */
2547d31da0f0SAl Viro 	down_write(&s->s_umount);
2548ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
2549ea441d11SAl Viro 	return path.dentry;
2550ea441d11SAl Viro }
2551ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
2552ea441d11SAl Viro 
2553bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2554bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
25551da177e4SLinus Torvalds {
2556eca6f534SVegard Nossum 	int ret;
2557eca6f534SVegard Nossum 	char *kernel_type;
255891a27b2aSJeff Layton 	struct filename *kernel_dir;
2559eca6f534SVegard Nossum 	char *kernel_dev;
25601da177e4SLinus Torvalds 	unsigned long data_page;
25611da177e4SLinus Torvalds 
2562eca6f534SVegard Nossum 	ret = copy_mount_string(type, &kernel_type);
2563eca6f534SVegard Nossum 	if (ret < 0)
2564eca6f534SVegard Nossum 		goto out_type;
25651da177e4SLinus Torvalds 
2566eca6f534SVegard Nossum 	kernel_dir = getname(dir_name);
2567eca6f534SVegard Nossum 	if (IS_ERR(kernel_dir)) {
2568eca6f534SVegard Nossum 		ret = PTR_ERR(kernel_dir);
2569eca6f534SVegard Nossum 		goto out_dir;
2570eca6f534SVegard Nossum 	}
25711da177e4SLinus Torvalds 
2572eca6f534SVegard Nossum 	ret = copy_mount_string(dev_name, &kernel_dev);
2573eca6f534SVegard Nossum 	if (ret < 0)
2574eca6f534SVegard Nossum 		goto out_dev;
25751da177e4SLinus Torvalds 
2576eca6f534SVegard Nossum 	ret = copy_mount_options(data, &data_page);
2577eca6f534SVegard Nossum 	if (ret < 0)
2578eca6f534SVegard Nossum 		goto out_data;
25791da177e4SLinus Torvalds 
258091a27b2aSJeff Layton 	ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags,
2581eca6f534SVegard Nossum 		(void *) data_page);
2582eca6f534SVegard Nossum 
25831da177e4SLinus Torvalds 	free_page(data_page);
2584eca6f534SVegard Nossum out_data:
2585eca6f534SVegard Nossum 	kfree(kernel_dev);
2586eca6f534SVegard Nossum out_dev:
2587eca6f534SVegard Nossum 	putname(kernel_dir);
2588eca6f534SVegard Nossum out_dir:
2589eca6f534SVegard Nossum 	kfree(kernel_type);
2590eca6f534SVegard Nossum out_type:
2591eca6f534SVegard Nossum 	return ret;
25921da177e4SLinus Torvalds }
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds /*
2595afac7cbaSAl Viro  * Return true if path is reachable from root
2596afac7cbaSAl Viro  *
2597afac7cbaSAl Viro  * namespace_sem or vfsmount_lock is held
2598afac7cbaSAl Viro  */
2599643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2600afac7cbaSAl Viro 			 const struct path *root)
2601afac7cbaSAl Viro {
2602643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2603a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
26040714a533SAl Viro 		mnt = mnt->mnt_parent;
2605afac7cbaSAl Viro 	}
2606643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2607afac7cbaSAl Viro }
2608afac7cbaSAl Viro 
2609afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2)
2610afac7cbaSAl Viro {
2611afac7cbaSAl Viro 	int res;
2612962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
2613643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
2614962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
2615afac7cbaSAl Viro 	return res;
2616afac7cbaSAl Viro }
2617afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
2618afac7cbaSAl Viro 
2619afac7cbaSAl Viro /*
26201da177e4SLinus Torvalds  * pivot_root Semantics:
26211da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
26221da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
26231da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
26241da177e4SLinus Torvalds  *
26251da177e4SLinus Torvalds  * Restrictions:
26261da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
26271da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
26281da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
26291da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
26301da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
26311da177e4SLinus Torvalds  *
26324a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
26334a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
26344a0d11faSNeil Brown  * in this situation.
26354a0d11faSNeil Brown  *
26361da177e4SLinus Torvalds  * Notes:
26371da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
26381da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
26391da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
26401da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
26411da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
26421da177e4SLinus Torvalds  *    first.
26431da177e4SLinus Torvalds  */
26443480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
26453480b257SHeiko Carstens 		const char __user *, put_old)
26461da177e4SLinus Torvalds {
26472d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
264884d17192SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt;
264984d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
26501da177e4SLinus Torvalds 	int error;
26511da177e4SLinus Torvalds 
26529b40bc90SAl Viro 	if (!may_mount())
26531da177e4SLinus Torvalds 		return -EPERM;
26541da177e4SLinus Torvalds 
26552d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
26561da177e4SLinus Torvalds 	if (error)
26571da177e4SLinus Torvalds 		goto out0;
26581da177e4SLinus Torvalds 
26592d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
26601da177e4SLinus Torvalds 	if (error)
26611da177e4SLinus Torvalds 		goto out1;
26621da177e4SLinus Torvalds 
26632d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
2664b12cea91SAl Viro 	if (error)
2665b12cea91SAl Viro 		goto out2;
26661da177e4SLinus Torvalds 
2667f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
266884d17192SAl Viro 	old_mp = lock_mount(&old);
266984d17192SAl Viro 	error = PTR_ERR(old_mp);
267084d17192SAl Viro 	if (IS_ERR(old_mp))
2671b12cea91SAl Viro 		goto out3;
2672b12cea91SAl Viro 
26731da177e4SLinus Torvalds 	error = -EINVAL;
2674419148daSAl Viro 	new_mnt = real_mount(new.mnt);
2675419148daSAl Viro 	root_mnt = real_mount(root.mnt);
267684d17192SAl Viro 	old_mnt = real_mount(old.mnt);
267784d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
2678fc7be130SAl Viro 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
2679fc7be130SAl Viro 		IS_MNT_SHARED(root_mnt->mnt_parent))
2680b12cea91SAl Viro 		goto out4;
2681143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2682b12cea91SAl Viro 		goto out4;
26835ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
26845ff9d8a6SEric W. Biederman 		goto out4;
26851da177e4SLinus Torvalds 	error = -ENOENT;
2686f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
2687b12cea91SAl Viro 		goto out4;
26881da177e4SLinus Torvalds 	error = -EBUSY;
268984d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
2690b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
26911da177e4SLinus Torvalds 	error = -EINVAL;
26928c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
2693b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2694676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
2695b12cea91SAl Viro 		goto out4; /* not attached */
269684d17192SAl Viro 	root_mp = root_mnt->mnt_mp;
26972d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
2698b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2699676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
2700b12cea91SAl Viro 		goto out4; /* not attached */
27014ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
270284d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
2703b12cea91SAl Viro 		goto out4;
270484d17192SAl Viro 	root_mp->m_count++; /* pin it so it won't go away */
2705962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
2706419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
2707419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
27085ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
27095ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
27105ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
27115ff9d8a6SEric W. Biederman 	}
27124ac91378SJan Blunck 	/* mount old root on put_old */
271384d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
27144ac91378SJan Blunck 	/* mount new_root on / */
271584d17192SAl Viro 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
27166b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
2717962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
27182d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
271984d17192SAl Viro 	put_mountpoint(root_mp);
27201da177e4SLinus Torvalds 	error = 0;
2721b12cea91SAl Viro out4:
272284d17192SAl Viro 	unlock_mount(old_mp);
2723b12cea91SAl Viro 	if (!error) {
27241a390689SAl Viro 		path_put(&root_parent);
27251a390689SAl Viro 		path_put(&parent_path);
2726b12cea91SAl Viro 	}
2727b12cea91SAl Viro out3:
27288c3ee42eSAl Viro 	path_put(&root);
2729b12cea91SAl Viro out2:
27302d8f3038SAl Viro 	path_put(&old);
27311da177e4SLinus Torvalds out1:
27322d8f3038SAl Viro 	path_put(&new);
27331da177e4SLinus Torvalds out0:
27341da177e4SLinus Torvalds 	return error;
27351da177e4SLinus Torvalds }
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds static void __init init_mount_tree(void)
27381da177e4SLinus Torvalds {
27391da177e4SLinus Torvalds 	struct vfsmount *mnt;
27406b3286edSKirill Korotaev 	struct mnt_namespace *ns;
2741ac748a09SJan Blunck 	struct path root;
27420c55cfc4SEric W. Biederman 	struct file_system_type *type;
27431da177e4SLinus Torvalds 
27440c55cfc4SEric W. Biederman 	type = get_fs_type("rootfs");
27450c55cfc4SEric W. Biederman 	if (!type)
27460c55cfc4SEric W. Biederman 		panic("Can't find rootfs type");
27470c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
27480c55cfc4SEric W. Biederman 	put_filesystem(type);
27491da177e4SLinus Torvalds 	if (IS_ERR(mnt))
27501da177e4SLinus Torvalds 		panic("Can't create rootfs");
2751b3e19d92SNick Piggin 
27523b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
27533b22edc5STrond Myklebust 	if (IS_ERR(ns))
27541da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
27551da177e4SLinus Torvalds 
27566b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
27576b3286edSKirill Korotaev 	get_mnt_ns(ns);
27581da177e4SLinus Torvalds 
2759be08d6d2SAl Viro 	root.mnt = mnt;
2760be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
2761ac748a09SJan Blunck 
2762ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
2763ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
27641da177e4SLinus Torvalds }
27651da177e4SLinus Torvalds 
276674bf17cfSDenis Cheng void __init mnt_init(void)
27671da177e4SLinus Torvalds {
276813f14b4dSEric Dumazet 	unsigned u;
276915a67dd8SRandy Dunlap 	int err;
27701da177e4SLinus Torvalds 
2771390c6843SRam Pai 	init_rwsem(&namespace_sem);
2772390c6843SRam Pai 
27737d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
277420c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
27751da177e4SLinus Torvalds 
2776b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
277784d17192SAl Viro 	mountpoint_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
27781da177e4SLinus Torvalds 
277984d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
27801da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
27811da177e4SLinus Torvalds 
278280cdc6daSMandeep Singh Baines 	printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
27831da177e4SLinus Torvalds 
278413f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
278513f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
278684d17192SAl Viro 	for (u = 0; u < HASH_SIZE; u++)
278784d17192SAl Viro 		INIT_LIST_HEAD(&mountpoint_hashtable[u]);
27881da177e4SLinus Torvalds 
2789962830dfSAndi Kleen 	br_lock_init(&vfsmount_lock);
279099b7db7bSNick Piggin 
279115a67dd8SRandy Dunlap 	err = sysfs_init();
279215a67dd8SRandy Dunlap 	if (err)
279315a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
27948e24eea7SHarvey Harrison 			__func__, err);
279500d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
279600d26666SGreg Kroah-Hartman 	if (!fs_kobj)
27978e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
27981da177e4SLinus Torvalds 	init_rootfs();
27991da177e4SLinus Torvalds 	init_mount_tree();
28001da177e4SLinus Torvalds }
28011da177e4SLinus Torvalds 
2802616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
28031da177e4SLinus Torvalds {
2804d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
2805616511d0STrond Myklebust 		return;
280697216be0SAl Viro 	namespace_lock();
2807962830dfSAndi Kleen 	br_write_lock(&vfsmount_lock);
2808328e6d90SAl Viro 	umount_tree(ns->root, 0);
2809962830dfSAndi Kleen 	br_write_unlock(&vfsmount_lock);
28103ab6abeeSAl Viro 	namespace_unlock();
2811771b1371SEric W. Biederman 	free_mnt_ns(ns);
28121da177e4SLinus Torvalds }
28139d412a43SAl Viro 
28149d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
28159d412a43SAl Viro {
2816423e0ab0STim Chen 	struct vfsmount *mnt;
2817423e0ab0STim Chen 	mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
2818423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
2819423e0ab0STim Chen 		/*
2820423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
2821423e0ab0STim Chen 		 * we unmount before file sys is unregistered
2822423e0ab0STim Chen 		*/
2823f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
2824423e0ab0STim Chen 	}
2825423e0ab0STim Chen 	return mnt;
28269d412a43SAl Viro }
28279d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data);
2828423e0ab0STim Chen 
2829423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
2830423e0ab0STim Chen {
2831423e0ab0STim Chen 	/* release long term mount so mount point can be released */
2832423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
2833f7a99c5bSAl Viro 		br_write_lock(&vfsmount_lock);
2834f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
2835f7a99c5bSAl Viro 		br_write_unlock(&vfsmount_lock);
2836423e0ab0STim Chen 		mntput(mnt);
2837423e0ab0STim Chen 	}
2838423e0ab0STim Chen }
2839423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
284002125a82SAl Viro 
284102125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
284202125a82SAl Viro {
2843143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
284402125a82SAl Viro }
28458823c079SEric W. Biederman 
28463151527eSEric W. Biederman bool current_chrooted(void)
28473151527eSEric W. Biederman {
28483151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
28493151527eSEric W. Biederman 	struct path ns_root;
28503151527eSEric W. Biederman 	struct path fs_root;
28513151527eSEric W. Biederman 	bool chrooted;
28523151527eSEric W. Biederman 
28533151527eSEric W. Biederman 	/* Find the namespace root */
28543151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
28553151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
28563151527eSEric W. Biederman 	path_get(&ns_root);
28573151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
28583151527eSEric W. Biederman 		;
28593151527eSEric W. Biederman 
28603151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
28613151527eSEric W. Biederman 
28623151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
28633151527eSEric W. Biederman 
28643151527eSEric W. Biederman 	path_put(&fs_root);
28653151527eSEric W. Biederman 	path_put(&ns_root);
28663151527eSEric W. Biederman 
28673151527eSEric W. Biederman 	return chrooted;
28683151527eSEric W. Biederman }
28693151527eSEric W. Biederman 
287087a8ebd6SEric W. Biederman void update_mnt_policy(struct user_namespace *userns)
287187a8ebd6SEric W. Biederman {
287287a8ebd6SEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
287387a8ebd6SEric W. Biederman 	struct mount *mnt;
287487a8ebd6SEric W. Biederman 
287587a8ebd6SEric W. Biederman 	down_read(&namespace_sem);
287687a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
287787a8ebd6SEric W. Biederman 		switch (mnt->mnt.mnt_sb->s_magic) {
287887a8ebd6SEric W. Biederman 		case SYSFS_MAGIC:
287987a8ebd6SEric W. Biederman 			userns->may_mount_sysfs = true;
288087a8ebd6SEric W. Biederman 			break;
288187a8ebd6SEric W. Biederman 		case PROC_SUPER_MAGIC:
288287a8ebd6SEric W. Biederman 			userns->may_mount_proc = true;
288387a8ebd6SEric W. Biederman 			break;
288487a8ebd6SEric W. Biederman 		}
288587a8ebd6SEric W. Biederman 		if (userns->may_mount_sysfs && userns->may_mount_proc)
288687a8ebd6SEric W. Biederman 			break;
288787a8ebd6SEric W. Biederman 	}
288887a8ebd6SEric W. Biederman 	up_read(&namespace_sem);
288987a8ebd6SEric W. Biederman }
289087a8ebd6SEric W. Biederman 
28918823c079SEric W. Biederman static void *mntns_get(struct task_struct *task)
28928823c079SEric W. Biederman {
28938823c079SEric W. Biederman 	struct mnt_namespace *ns = NULL;
28948823c079SEric W. Biederman 	struct nsproxy *nsproxy;
28958823c079SEric W. Biederman 
28968823c079SEric W. Biederman 	rcu_read_lock();
28978823c079SEric W. Biederman 	nsproxy = task_nsproxy(task);
28988823c079SEric W. Biederman 	if (nsproxy) {
28998823c079SEric W. Biederman 		ns = nsproxy->mnt_ns;
29008823c079SEric W. Biederman 		get_mnt_ns(ns);
29018823c079SEric W. Biederman 	}
29028823c079SEric W. Biederman 	rcu_read_unlock();
29038823c079SEric W. Biederman 
29048823c079SEric W. Biederman 	return ns;
29058823c079SEric W. Biederman }
29068823c079SEric W. Biederman 
29078823c079SEric W. Biederman static void mntns_put(void *ns)
29088823c079SEric W. Biederman {
29098823c079SEric W. Biederman 	put_mnt_ns(ns);
29108823c079SEric W. Biederman }
29118823c079SEric W. Biederman 
29128823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns)
29138823c079SEric W. Biederman {
29148823c079SEric W. Biederman 	struct fs_struct *fs = current->fs;
29158823c079SEric W. Biederman 	struct mnt_namespace *mnt_ns = ns;
29168823c079SEric W. Biederman 	struct path root;
29178823c079SEric W. Biederman 
29180c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
29195e4a0847SEric W. Biederman 	    !nsown_capable(CAP_SYS_CHROOT) ||
29205e4a0847SEric W. Biederman 	    !nsown_capable(CAP_SYS_ADMIN))
2921ae11e0f1SZhao Hongjiang 		return -EPERM;
29228823c079SEric W. Biederman 
29238823c079SEric W. Biederman 	if (fs->users != 1)
29248823c079SEric W. Biederman 		return -EINVAL;
29258823c079SEric W. Biederman 
29268823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
29278823c079SEric W. Biederman 	put_mnt_ns(nsproxy->mnt_ns);
29288823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
29298823c079SEric W. Biederman 
29308823c079SEric W. Biederman 	/* Find the root */
29318823c079SEric W. Biederman 	root.mnt    = &mnt_ns->root->mnt;
29328823c079SEric W. Biederman 	root.dentry = mnt_ns->root->mnt.mnt_root;
29338823c079SEric W. Biederman 	path_get(&root);
29348823c079SEric W. Biederman 	while(d_mountpoint(root.dentry) && follow_down_one(&root))
29358823c079SEric W. Biederman 		;
29368823c079SEric W. Biederman 
29378823c079SEric W. Biederman 	/* Update the pwd and root */
29388823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
29398823c079SEric W. Biederman 	set_fs_root(fs, &root);
29408823c079SEric W. Biederman 
29418823c079SEric W. Biederman 	path_put(&root);
29428823c079SEric W. Biederman 	return 0;
29438823c079SEric W. Biederman }
29448823c079SEric W. Biederman 
294598f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns)
294698f842e6SEric W. Biederman {
294798f842e6SEric W. Biederman 	struct mnt_namespace *mnt_ns = ns;
294898f842e6SEric W. Biederman 	return mnt_ns->proc_inum;
294998f842e6SEric W. Biederman }
295098f842e6SEric W. Biederman 
29518823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
29528823c079SEric W. Biederman 	.name		= "mnt",
29538823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
29548823c079SEric W. Biederman 	.get		= mntns_get,
29558823c079SEric W. Biederman 	.put		= mntns_put,
29568823c079SEric W. Biederman 	.install	= mntns_install,
295798f842e6SEric W. Biederman 	.inum		= mntns_inum,
29588823c079SEric W. Biederman };
2959