xref: /openbmc/linux/fs/namespace.c (revision 4b93dc9b)
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 */
2057f150a5SRob Landley #include <linux/init.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;
4259aa0da8SAl Viro static DECLARE_RWSEM(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  */
5648a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_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 
6699b7db7bSNick Piggin /*
6799b7db7bSNick Piggin  * allocation is serialized by namespace_sem, but we need the spinlock to
6899b7db7bSNick Piggin  * serialize with freeing.
6999b7db7bSNick Piggin  */
70b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
7173cd49ecSMiklos Szeredi {
7273cd49ecSMiklos Szeredi 	int res;
7373cd49ecSMiklos Szeredi 
7473cd49ecSMiklos Szeredi retry:
7573cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
7699b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
7715169fe7SAl Viro 	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
78f21f6220SAl Viro 	if (!res)
7915169fe7SAl Viro 		mnt_id_start = mnt->mnt_id + 1;
8099b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
8173cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
8273cd49ecSMiklos Szeredi 		goto retry;
8373cd49ecSMiklos Szeredi 
8473cd49ecSMiklos Szeredi 	return res;
8573cd49ecSMiklos Szeredi }
8673cd49ecSMiklos Szeredi 
87b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
8873cd49ecSMiklos Szeredi {
8915169fe7SAl Viro 	int id = mnt->mnt_id;
9099b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
91f21f6220SAl Viro 	ida_remove(&mnt_id_ida, id);
92f21f6220SAl Viro 	if (mnt_id_start > id)
93f21f6220SAl Viro 		mnt_id_start = id;
9499b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
9573cd49ecSMiklos Szeredi }
9673cd49ecSMiklos Szeredi 
97719f5d7fSMiklos Szeredi /*
98719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
99719f5d7fSMiklos Szeredi  *
100719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
101719f5d7fSMiklos Szeredi  */
1024b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
103719f5d7fSMiklos Szeredi {
104f21f6220SAl Viro 	int res;
105f21f6220SAl Viro 
106719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
107719f5d7fSMiklos Szeredi 		return -ENOMEM;
108719f5d7fSMiklos Szeredi 
109f21f6220SAl Viro 	res = ida_get_new_above(&mnt_group_ida,
110f21f6220SAl Viro 				mnt_group_start,
11115169fe7SAl Viro 				&mnt->mnt_group_id);
112f21f6220SAl Viro 	if (!res)
11315169fe7SAl Viro 		mnt_group_start = mnt->mnt_group_id + 1;
114f21f6220SAl Viro 
115f21f6220SAl Viro 	return res;
116719f5d7fSMiklos Szeredi }
117719f5d7fSMiklos Szeredi 
118719f5d7fSMiklos Szeredi /*
119719f5d7fSMiklos Szeredi  * Release a peer group ID
120719f5d7fSMiklos Szeredi  */
1214b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
122719f5d7fSMiklos Szeredi {
12315169fe7SAl Viro 	int id = mnt->mnt_group_id;
124f21f6220SAl Viro 	ida_remove(&mnt_group_ida, id);
125f21f6220SAl Viro 	if (mnt_group_start > id)
126f21f6220SAl Viro 		mnt_group_start = id;
12715169fe7SAl Viro 	mnt->mnt_group_id = 0;
128719f5d7fSMiklos Szeredi }
129719f5d7fSMiklos Szeredi 
130b3e19d92SNick Piggin /*
131b3e19d92SNick Piggin  * vfsmount lock must be held for read
132b3e19d92SNick Piggin  */
13383adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
134b3e19d92SNick Piggin {
135b3e19d92SNick Piggin #ifdef CONFIG_SMP
13668e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
137b3e19d92SNick Piggin #else
138b3e19d92SNick Piggin 	preempt_disable();
13968e8a9feSAl Viro 	mnt->mnt_count += n;
140b3e19d92SNick Piggin 	preempt_enable();
141b3e19d92SNick Piggin #endif
142b3e19d92SNick Piggin }
143b3e19d92SNick Piggin 
144b3e19d92SNick Piggin /*
145b3e19d92SNick Piggin  * vfsmount lock must be held for write
146b3e19d92SNick Piggin  */
14783adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt)
148b3e19d92SNick Piggin {
149b3e19d92SNick Piggin #ifdef CONFIG_SMP
150f03c6599SAl Viro 	unsigned int count = 0;
151b3e19d92SNick Piggin 	int cpu;
152b3e19d92SNick Piggin 
153b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
15468e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
155b3e19d92SNick Piggin 	}
156b3e19d92SNick Piggin 
157b3e19d92SNick Piggin 	return count;
158b3e19d92SNick Piggin #else
15968e8a9feSAl Viro 	return mnt->mnt_count;
160b3e19d92SNick Piggin #endif
161b3e19d92SNick Piggin }
162b3e19d92SNick Piggin 
163b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
1641da177e4SLinus Torvalds {
165c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
166c63181e6SAl Viro 	if (mnt) {
16773cd49ecSMiklos Szeredi 		int err;
16873cd49ecSMiklos Szeredi 
169c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
17088b38782SLi Zefan 		if (err)
17188b38782SLi Zefan 			goto out_free_cache;
17288b38782SLi Zefan 
17388b38782SLi Zefan 		if (name) {
174c63181e6SAl Viro 			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
175c63181e6SAl Viro 			if (!mnt->mnt_devname)
17688b38782SLi Zefan 				goto out_free_id;
17773cd49ecSMiklos Szeredi 		}
17873cd49ecSMiklos Szeredi 
179b3e19d92SNick Piggin #ifdef CONFIG_SMP
180c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
181c63181e6SAl Viro 		if (!mnt->mnt_pcp)
182b3e19d92SNick Piggin 			goto out_free_devname;
183b3e19d92SNick Piggin 
184c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
185b3e19d92SNick Piggin #else
186c63181e6SAl Viro 		mnt->mnt_count = 1;
187c63181e6SAl Viro 		mnt->mnt_writers = 0;
188b3e19d92SNick Piggin #endif
189b3e19d92SNick Piggin 
190c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_hash);
191c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
192c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
193c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
194c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
195c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
196c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
197c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
1982504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY
1992504c5d6SAndreas Gruenbacher 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
2002504c5d6SAndreas Gruenbacher #endif
2011da177e4SLinus Torvalds 	}
202c63181e6SAl Viro 	return mnt;
20388b38782SLi Zefan 
204d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
205d3ef3d73Snpiggin@suse.de out_free_devname:
206c63181e6SAl Viro 	kfree(mnt->mnt_devname);
207d3ef3d73Snpiggin@suse.de #endif
20888b38782SLi Zefan out_free_id:
209c63181e6SAl Viro 	mnt_free_id(mnt);
21088b38782SLi Zefan out_free_cache:
211c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
21288b38782SLi Zefan 	return NULL;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2158366025eSDave Hansen /*
2168366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2178366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2188366025eSDave Hansen  * We must keep track of when those operations start
2198366025eSDave Hansen  * (for permission checks) and when they end, so that
2208366025eSDave Hansen  * we can determine when writes are able to occur to
2218366025eSDave Hansen  * a filesystem.
2228366025eSDave Hansen  */
2233d733633SDave Hansen /*
2243d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2253d733633SDave Hansen  * @mnt: the mount to check for its write status
2263d733633SDave Hansen  *
2273d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2283d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2293d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2303d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2313d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2323d733633SDave Hansen  * r/w.
2333d733633SDave Hansen  */
2343d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2353d733633SDave Hansen {
2362e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2372e4b7fcdSDave Hansen 		return 1;
2382e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
2392e4b7fcdSDave Hansen 		return 1;
2402e4b7fcdSDave Hansen 	return 0;
2413d733633SDave Hansen }
2423d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2433d733633SDave Hansen 
24483adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2453d733633SDave Hansen {
246d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
24768e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
248d3ef3d73Snpiggin@suse.de #else
24968e8a9feSAl Viro 	mnt->mnt_writers++;
250d3ef3d73Snpiggin@suse.de #endif
2513d733633SDave Hansen }
2523d733633SDave Hansen 
25383adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2543d733633SDave Hansen {
255d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
25668e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
257d3ef3d73Snpiggin@suse.de #else
25868e8a9feSAl Viro 	mnt->mnt_writers--;
259d3ef3d73Snpiggin@suse.de #endif
260d3ef3d73Snpiggin@suse.de }
261d3ef3d73Snpiggin@suse.de 
26283adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
263d3ef3d73Snpiggin@suse.de {
264d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
265d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2663d733633SDave Hansen 	int cpu;
2673d733633SDave Hansen 
2683d733633SDave Hansen 	for_each_possible_cpu(cpu) {
26968e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
2703d733633SDave Hansen 	}
2713d733633SDave Hansen 
272d3ef3d73Snpiggin@suse.de 	return count;
273d3ef3d73Snpiggin@suse.de #else
274d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
275d3ef3d73Snpiggin@suse.de #endif
2763d733633SDave Hansen }
2773d733633SDave Hansen 
2784ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
2794ed5e82fSMiklos Szeredi {
2804ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
2814ed5e82fSMiklos Szeredi 		return 1;
2824ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
2834ed5e82fSMiklos Szeredi 	smp_rmb();
2844ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
2854ed5e82fSMiklos Szeredi }
2864ed5e82fSMiklos Szeredi 
2873d733633SDave Hansen /*
288eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
289eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
290eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
291eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
2923d733633SDave Hansen  */
2938366025eSDave Hansen /**
294eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
29583adc753SAl Viro  * @m: the mount on which to take a write
2968366025eSDave Hansen  *
297eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
298eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
299eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
300eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
301eb04c282SJan Kara  * called. This is effectively a refcount.
3028366025eSDave Hansen  */
303eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3048366025eSDave Hansen {
30583adc753SAl Viro 	struct mount *mnt = real_mount(m);
3063d733633SDave Hansen 	int ret = 0;
3073d733633SDave Hansen 
308d3ef3d73Snpiggin@suse.de 	preempt_disable();
309c6653a83SNick Piggin 	mnt_inc_writers(mnt);
310d3ef3d73Snpiggin@suse.de 	/*
311c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
312d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
313d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
314d3ef3d73Snpiggin@suse.de 	 */
315d3ef3d73Snpiggin@suse.de 	smp_mb();
3161e75529eSMiao Xie 	while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
317d3ef3d73Snpiggin@suse.de 		cpu_relax();
318d3ef3d73Snpiggin@suse.de 	/*
319d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
320d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
321d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
322d3ef3d73Snpiggin@suse.de 	 */
323d3ef3d73Snpiggin@suse.de 	smp_rmb();
3244ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
325c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3263d733633SDave Hansen 		ret = -EROFS;
3273d733633SDave Hansen 	}
328d3ef3d73Snpiggin@suse.de 	preempt_enable();
329eb04c282SJan Kara 
330eb04c282SJan Kara 	return ret;
331eb04c282SJan Kara }
332eb04c282SJan Kara 
333eb04c282SJan Kara /**
334eb04c282SJan Kara  * mnt_want_write - get write access to a mount
335eb04c282SJan Kara  * @m: the mount on which to take a write
336eb04c282SJan Kara  *
337eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
338eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
339eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
340eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
341eb04c282SJan Kara  */
342eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
343eb04c282SJan Kara {
344eb04c282SJan Kara 	int ret;
345eb04c282SJan Kara 
346eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
347eb04c282SJan Kara 	ret = __mnt_want_write(m);
348eb04c282SJan Kara 	if (ret)
349eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3503d733633SDave Hansen 	return ret;
3518366025eSDave Hansen }
3528366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3538366025eSDave Hansen 
3548366025eSDave Hansen /**
35596029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
35696029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
35796029c4eSnpiggin@suse.de  *
35896029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
35996029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
36096029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
36196029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
36296029c4eSnpiggin@suse.de  *
36396029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
36496029c4eSnpiggin@suse.de  * drop the reference.
36596029c4eSnpiggin@suse.de  */
36696029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
36796029c4eSnpiggin@suse.de {
36896029c4eSnpiggin@suse.de 	/* superblock may be r/o */
36996029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
37096029c4eSnpiggin@suse.de 		return -EROFS;
37196029c4eSnpiggin@suse.de 	preempt_disable();
37283adc753SAl Viro 	mnt_inc_writers(real_mount(mnt));
37396029c4eSnpiggin@suse.de 	preempt_enable();
37496029c4eSnpiggin@suse.de 	return 0;
37596029c4eSnpiggin@suse.de }
37696029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
37796029c4eSnpiggin@suse.de 
37896029c4eSnpiggin@suse.de /**
379eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
380eb04c282SJan Kara  * @file: the file who's mount on which to take a write
381eb04c282SJan Kara  *
382eb04c282SJan Kara  * This is like __mnt_want_write, but it takes a file and can
383eb04c282SJan Kara  * do some optimisations if the file is open for write already
384eb04c282SJan Kara  */
385eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
386eb04c282SJan Kara {
387496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
388eb04c282SJan Kara 
389eb04c282SJan Kara 	if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
390eb04c282SJan Kara 		return __mnt_want_write(file->f_path.mnt);
391eb04c282SJan Kara 	else
392eb04c282SJan Kara 		return mnt_clone_write(file->f_path.mnt);
393eb04c282SJan Kara }
394eb04c282SJan Kara 
395eb04c282SJan Kara /**
39696029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
39796029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
39896029c4eSnpiggin@suse.de  *
39996029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
40096029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
40196029c4eSnpiggin@suse.de  */
40296029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
40396029c4eSnpiggin@suse.de {
404eb04c282SJan Kara 	int ret;
405eb04c282SJan Kara 
406eb04c282SJan Kara 	sb_start_write(file->f_path.mnt->mnt_sb);
407eb04c282SJan Kara 	ret = __mnt_want_write_file(file);
408eb04c282SJan Kara 	if (ret)
409eb04c282SJan Kara 		sb_end_write(file->f_path.mnt->mnt_sb);
410eb04c282SJan Kara 	return ret;
41196029c4eSnpiggin@suse.de }
41296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
41396029c4eSnpiggin@suse.de 
41496029c4eSnpiggin@suse.de /**
415eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4168366025eSDave Hansen  * @mnt: the mount on which to give up write access
4178366025eSDave Hansen  *
4188366025eSDave Hansen  * Tells the low-level filesystem that we are done
4198366025eSDave Hansen  * performing writes to it.  Must be matched with
420eb04c282SJan Kara  * __mnt_want_write() call above.
4218366025eSDave Hansen  */
422eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4238366025eSDave Hansen {
424d3ef3d73Snpiggin@suse.de 	preempt_disable();
42583adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
426d3ef3d73Snpiggin@suse.de 	preempt_enable();
4278366025eSDave Hansen }
428eb04c282SJan Kara 
429eb04c282SJan Kara /**
430eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
431eb04c282SJan Kara  * @mnt: the mount on which to give up write access
432eb04c282SJan Kara  *
433eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
434eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
435eb04c282SJan Kara  * mnt_want_write() call above.
436eb04c282SJan Kara  */
437eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
438eb04c282SJan Kara {
439eb04c282SJan Kara 	__mnt_drop_write(mnt);
440eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
441eb04c282SJan Kara }
4428366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4438366025eSDave Hansen 
444eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
445eb04c282SJan Kara {
446eb04c282SJan Kara 	__mnt_drop_write(file->f_path.mnt);
447eb04c282SJan Kara }
448eb04c282SJan Kara 
4492a79f17eSAl Viro void mnt_drop_write_file(struct file *file)
4502a79f17eSAl Viro {
4512a79f17eSAl Viro 	mnt_drop_write(file->f_path.mnt);
4522a79f17eSAl Viro }
4532a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4542a79f17eSAl Viro 
45583adc753SAl Viro static int mnt_make_readonly(struct mount *mnt)
4568366025eSDave Hansen {
4573d733633SDave Hansen 	int ret = 0;
4583d733633SDave Hansen 
459719ea2fbSAl Viro 	lock_mount_hash();
46083adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
461d3ef3d73Snpiggin@suse.de 	/*
462d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
463d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
464d3ef3d73Snpiggin@suse.de 	 */
465d3ef3d73Snpiggin@suse.de 	smp_mb();
466d3ef3d73Snpiggin@suse.de 
467d3ef3d73Snpiggin@suse.de 	/*
468d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
469d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
470d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
471d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
472d3ef3d73Snpiggin@suse.de 	 *
473d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
474d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
475d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
476d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
477d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
478d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
479d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
480d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
481d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
482d3ef3d73Snpiggin@suse.de 	 */
483c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
484d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
485d3ef3d73Snpiggin@suse.de 	else
48683adc753SAl Viro 		mnt->mnt.mnt_flags |= MNT_READONLY;
487d3ef3d73Snpiggin@suse.de 	/*
488d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
489d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
490d3ef3d73Snpiggin@suse.de 	 */
491d3ef3d73Snpiggin@suse.de 	smp_wmb();
49283adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
493719ea2fbSAl Viro 	unlock_mount_hash();
4943d733633SDave Hansen 	return ret;
4953d733633SDave Hansen }
4968366025eSDave Hansen 
49783adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt)
4982e4b7fcdSDave Hansen {
499719ea2fbSAl Viro 	lock_mount_hash();
50083adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
501719ea2fbSAl Viro 	unlock_mount_hash();
5022e4b7fcdSDave Hansen }
5032e4b7fcdSDave Hansen 
5044ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5054ed5e82fSMiklos Szeredi {
5064ed5e82fSMiklos Szeredi 	struct mount *mnt;
5074ed5e82fSMiklos Szeredi 	int err = 0;
5084ed5e82fSMiklos Szeredi 
5098e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5108e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5118e8b8796SMiklos Szeredi 		return -EBUSY;
5128e8b8796SMiklos Szeredi 
513719ea2fbSAl Viro 	lock_mount_hash();
5144ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5154ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
5164ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
5174ed5e82fSMiklos Szeredi 			smp_mb();
5184ed5e82fSMiklos Szeredi 			if (mnt_get_writers(mnt) > 0) {
5194ed5e82fSMiklos Szeredi 				err = -EBUSY;
5204ed5e82fSMiklos Szeredi 				break;
5214ed5e82fSMiklos Szeredi 			}
5224ed5e82fSMiklos Szeredi 		}
5234ed5e82fSMiklos Szeredi 	}
5248e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5258e8b8796SMiklos Szeredi 		err = -EBUSY;
5268e8b8796SMiklos Szeredi 
5274ed5e82fSMiklos Szeredi 	if (!err) {
5284ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
5294ed5e82fSMiklos Szeredi 		smp_wmb();
5304ed5e82fSMiklos Szeredi 	}
5314ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5324ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
5334ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
5344ed5e82fSMiklos Szeredi 	}
535719ea2fbSAl Viro 	unlock_mount_hash();
5364ed5e82fSMiklos Szeredi 
5374ed5e82fSMiklos Szeredi 	return err;
5384ed5e82fSMiklos Szeredi }
5394ed5e82fSMiklos Szeredi 
540b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
5411da177e4SLinus Torvalds {
54252ba1621SAl Viro 	kfree(mnt->mnt_devname);
54373cd49ecSMiklos Szeredi 	mnt_free_id(mnt);
544d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
54568e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
546d3ef3d73Snpiggin@suse.de #endif
547b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
55048a066e7SAl Viro /* call under rcu_read_lock */
55148a066e7SAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
55248a066e7SAl Viro {
55348a066e7SAl Viro 	struct mount *mnt;
55448a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
55548a066e7SAl Viro 		return false;
55648a066e7SAl Viro 	if (bastard == NULL)
55748a066e7SAl Viro 		return true;
55848a066e7SAl Viro 	mnt = real_mount(bastard);
55948a066e7SAl Viro 	mnt_add_count(mnt, 1);
56048a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
56148a066e7SAl Viro 		return true;
56248a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
56348a066e7SAl Viro 		mnt_add_count(mnt, -1);
56448a066e7SAl Viro 		return false;
56548a066e7SAl Viro 	}
56648a066e7SAl Viro 	rcu_read_unlock();
56748a066e7SAl Viro 	mntput(bastard);
56848a066e7SAl Viro 	rcu_read_lock();
56948a066e7SAl Viro 	return false;
57048a066e7SAl Viro }
57148a066e7SAl Viro 
5721da177e4SLinus Torvalds /*
573474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
57448a066e7SAl Viro  * call under rcu_read_lock()
5751da177e4SLinus Torvalds  */
576474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
5771da177e4SLinus Torvalds {
5781da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
579474279dcSAl Viro 	struct mount *p;
5801da177e4SLinus Torvalds 
58148a066e7SAl Viro 	list_for_each_entry_rcu(p, head, mnt_hash)
582474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
583474279dcSAl Viro 			return p;
584474279dcSAl Viro 	return NULL;
5851da177e4SLinus Torvalds }
586474279dcSAl Viro 
587474279dcSAl Viro /*
588474279dcSAl Viro  * find the last mount at @dentry on vfsmount @mnt.
58948a066e7SAl Viro  * mount_lock must be held.
590474279dcSAl Viro  */
591474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
592474279dcSAl Viro {
593474279dcSAl Viro 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
594474279dcSAl Viro 	struct mount *p;
595474279dcSAl Viro 
596474279dcSAl Viro 	list_for_each_entry_reverse(p, head, mnt_hash)
597474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
598474279dcSAl Viro 			return p;
599474279dcSAl Viro 	return NULL;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
602a05964f3SRam Pai /*
603f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
604f015f126SDavid Howells  *
605f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
606f015f126SDavid Howells  * following mounts:
607f015f126SDavid Howells  *
608f015f126SDavid Howells  * mount /dev/sda1 /mnt
609f015f126SDavid Howells  * mount /dev/sda2 /mnt
610f015f126SDavid Howells  * mount /dev/sda3 /mnt
611f015f126SDavid Howells  *
612f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
613f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
614f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
615f015f126SDavid Howells  *
616f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
617a05964f3SRam Pai  */
6181c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
619a05964f3SRam Pai {
620c7105365SAl Viro 	struct mount *child_mnt;
62148a066e7SAl Viro 	struct vfsmount *m;
62248a066e7SAl Viro 	unsigned seq;
62399b7db7bSNick Piggin 
62448a066e7SAl Viro 	rcu_read_lock();
62548a066e7SAl Viro 	do {
62648a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
627474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
62848a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
62948a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
63048a066e7SAl Viro 	rcu_read_unlock();
63148a066e7SAl Viro 	return m;
632a05964f3SRam Pai }
633a05964f3SRam Pai 
63484d17192SAl Viro static struct mountpoint *new_mountpoint(struct dentry *dentry)
63584d17192SAl Viro {
63684d17192SAl Viro 	struct list_head *chain = mountpoint_hashtable + hash(NULL, dentry);
63784d17192SAl Viro 	struct mountpoint *mp;
638eed81007SMiklos Szeredi 	int ret;
63984d17192SAl Viro 
64084d17192SAl Viro 	list_for_each_entry(mp, chain, m_hash) {
64184d17192SAl Viro 		if (mp->m_dentry == dentry) {
64284d17192SAl Viro 			/* might be worth a WARN_ON() */
64384d17192SAl Viro 			if (d_unlinked(dentry))
64484d17192SAl Viro 				return ERR_PTR(-ENOENT);
64584d17192SAl Viro 			mp->m_count++;
64684d17192SAl Viro 			return mp;
64784d17192SAl Viro 		}
64884d17192SAl Viro 	}
64984d17192SAl Viro 
65084d17192SAl Viro 	mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
65184d17192SAl Viro 	if (!mp)
65284d17192SAl Viro 		return ERR_PTR(-ENOMEM);
65384d17192SAl Viro 
654eed81007SMiklos Szeredi 	ret = d_set_mounted(dentry);
655eed81007SMiklos Szeredi 	if (ret) {
65684d17192SAl Viro 		kfree(mp);
657eed81007SMiklos Szeredi 		return ERR_PTR(ret);
65884d17192SAl Viro 	}
659eed81007SMiklos Szeredi 
66084d17192SAl Viro 	mp->m_dentry = dentry;
66184d17192SAl Viro 	mp->m_count = 1;
66284d17192SAl Viro 	list_add(&mp->m_hash, chain);
66384d17192SAl Viro 	return mp;
66484d17192SAl Viro }
66584d17192SAl Viro 
66684d17192SAl Viro static void put_mountpoint(struct mountpoint *mp)
66784d17192SAl Viro {
66884d17192SAl Viro 	if (!--mp->m_count) {
66984d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
67084d17192SAl Viro 		spin_lock(&dentry->d_lock);
67184d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
67284d17192SAl Viro 		spin_unlock(&dentry->d_lock);
67384d17192SAl Viro 		list_del(&mp->m_hash);
67484d17192SAl Viro 		kfree(mp);
67584d17192SAl Viro 	}
67684d17192SAl Viro }
67784d17192SAl Viro 
678143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
6791da177e4SLinus Torvalds {
6806b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
68399b7db7bSNick Piggin /*
68499b7db7bSNick Piggin  * vfsmount lock must be held for write
68599b7db7bSNick Piggin  */
6866b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
6875addc5ddSAl Viro {
6885addc5ddSAl Viro 	if (ns) {
6895addc5ddSAl Viro 		ns->event = ++event;
6905addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
6915addc5ddSAl Viro 	}
6925addc5ddSAl Viro }
6935addc5ddSAl Viro 
69499b7db7bSNick Piggin /*
69599b7db7bSNick Piggin  * vfsmount lock must be held for write
69699b7db7bSNick Piggin  */
6976b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
6985addc5ddSAl Viro {
6995addc5ddSAl Viro 	if (ns && ns->event != event) {
7005addc5ddSAl Viro 		ns->event = event;
7015addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7025addc5ddSAl Viro 	}
7035addc5ddSAl Viro }
7045addc5ddSAl Viro 
70599b7db7bSNick Piggin /*
70699b7db7bSNick Piggin  * vfsmount lock must be held for write
70799b7db7bSNick Piggin  */
708419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path)
7091da177e4SLinus Torvalds {
710a73324daSAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
7110714a533SAl Viro 	old_path->mnt = &mnt->mnt_parent->mnt;
7120714a533SAl Viro 	mnt->mnt_parent = mnt;
713a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
7146b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
7151b8e5564SAl Viro 	list_del_init(&mnt->mnt_hash);
71684d17192SAl Viro 	put_mountpoint(mnt->mnt_mp);
71784d17192SAl Viro 	mnt->mnt_mp = NULL;
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
72099b7db7bSNick Piggin /*
72199b7db7bSNick Piggin  * vfsmount lock must be held for write
72299b7db7bSNick Piggin  */
72384d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
72484d17192SAl Viro 			struct mountpoint *mp,
72544d964d6SAl Viro 			struct mount *child_mnt)
726b90fa9aeSRam Pai {
72784d17192SAl Viro 	mp->m_count++;
7283a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
72984d17192SAl Viro 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
7303a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
73184d17192SAl Viro 	child_mnt->mnt_mp = mp;
732b90fa9aeSRam Pai }
733b90fa9aeSRam Pai 
73499b7db7bSNick Piggin /*
73599b7db7bSNick Piggin  * vfsmount lock must be held for write
73699b7db7bSNick Piggin  */
73784d17192SAl Viro static void attach_mnt(struct mount *mnt,
73884d17192SAl Viro 			struct mount *parent,
73984d17192SAl Viro 			struct mountpoint *mp)
7401da177e4SLinus Torvalds {
74184d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
7421b8e5564SAl Viro 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
74384d17192SAl Viro 			hash(&parent->mnt, mp->m_dentry));
74484d17192SAl Viro 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
745b90fa9aeSRam Pai }
746b90fa9aeSRam Pai 
747b90fa9aeSRam Pai /*
74899b7db7bSNick Piggin  * vfsmount lock must be held for write
749b90fa9aeSRam Pai  */
7504b2619a5SAl Viro static void commit_tree(struct mount *mnt)
751b90fa9aeSRam Pai {
7520714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
75383adc753SAl Viro 	struct mount *m;
754b90fa9aeSRam Pai 	LIST_HEAD(head);
755143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
756b90fa9aeSRam Pai 
7570714a533SAl Viro 	BUG_ON(parent == mnt);
758b90fa9aeSRam Pai 
7591a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
760f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
761143c8c91SAl Viro 		m->mnt_ns = n;
762f03c6599SAl Viro 
763b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
764b90fa9aeSRam Pai 
7651b8e5564SAl Viro 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
766a73324daSAl Viro 				hash(&parent->mnt, mnt->mnt_mountpoint));
7676b41d536SAl Viro 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
7686b3286edSKirill Korotaev 	touch_mnt_namespace(n);
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
771909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
7721da177e4SLinus Torvalds {
7736b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
7746b41d536SAl Viro 	if (next == &p->mnt_mounts) {
7751da177e4SLinus Torvalds 		while (1) {
776909b0a88SAl Viro 			if (p == root)
7771da177e4SLinus Torvalds 				return NULL;
7786b41d536SAl Viro 			next = p->mnt_child.next;
7796b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
7801da177e4SLinus Torvalds 				break;
7810714a533SAl Viro 			p = p->mnt_parent;
7821da177e4SLinus Torvalds 		}
7831da177e4SLinus Torvalds 	}
7846b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
787315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
7889676f0c6SRam Pai {
7896b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
7906b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
7916b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
7926b41d536SAl Viro 		prev = p->mnt_mounts.prev;
7939676f0c6SRam Pai 	}
7949676f0c6SRam Pai 	return p;
7959676f0c6SRam Pai }
7969676f0c6SRam Pai 
7979d412a43SAl Viro struct vfsmount *
7989d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
7999d412a43SAl Viro {
800b105e270SAl Viro 	struct mount *mnt;
8019d412a43SAl Viro 	struct dentry *root;
8029d412a43SAl Viro 
8039d412a43SAl Viro 	if (!type)
8049d412a43SAl Viro 		return ERR_PTR(-ENODEV);
8059d412a43SAl Viro 
8069d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
8079d412a43SAl Viro 	if (!mnt)
8089d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
8099d412a43SAl Viro 
8109d412a43SAl Viro 	if (flags & MS_KERNMOUNT)
811b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
8129d412a43SAl Viro 
8139d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
8149d412a43SAl Viro 	if (IS_ERR(root)) {
8159d412a43SAl Viro 		free_vfsmnt(mnt);
8169d412a43SAl Viro 		return ERR_CAST(root);
8179d412a43SAl Viro 	}
8189d412a43SAl Viro 
819b105e270SAl Viro 	mnt->mnt.mnt_root = root;
820b105e270SAl Viro 	mnt->mnt.mnt_sb = root->d_sb;
821a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8220714a533SAl Viro 	mnt->mnt_parent = mnt;
823719ea2fbSAl Viro 	lock_mount_hash();
82439f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
825719ea2fbSAl Viro 	unlock_mount_hash();
826b105e270SAl Viro 	return &mnt->mnt;
8279d412a43SAl Viro }
8289d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
8299d412a43SAl Viro 
83087129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
83136341f64SRam Pai 					int flag)
8321da177e4SLinus Torvalds {
83387129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
834be34d1a3SDavid Howells 	struct mount *mnt;
835be34d1a3SDavid Howells 	int err;
8361da177e4SLinus Torvalds 
837be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
838be34d1a3SDavid Howells 	if (!mnt)
839be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
840be34d1a3SDavid Howells 
8417a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
84215169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
843719f5d7fSMiklos Szeredi 	else
84415169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
845719f5d7fSMiklos Szeredi 
84615169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
847be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
848719f5d7fSMiklos Szeredi 		if (err)
849719f5d7fSMiklos Szeredi 			goto out_free;
850719f5d7fSMiklos Szeredi 	}
851719f5d7fSMiklos Szeredi 
85287129cc0SAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
853132c94e3SEric W. Biederman 	/* Don't allow unprivileged users to change mount flags */
854132c94e3SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY))
855132c94e3SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
856132c94e3SEric W. Biederman 
8575ff9d8a6SEric W. Biederman 	/* Don't allow unprivileged users to reveal what is under a mount */
8585ff9d8a6SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire))
8595ff9d8a6SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCKED;
8605ff9d8a6SEric W. Biederman 
8611da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
862b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
863b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
864a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8650714a533SAl Viro 	mnt->mnt_parent = mnt;
866719ea2fbSAl Viro 	lock_mount_hash();
86739f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
868719ea2fbSAl Viro 	unlock_mount_hash();
869b90fa9aeSRam Pai 
8707a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
8717a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
8726776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
87332301920SAl Viro 		mnt->mnt_master = old;
874fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
8758aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
876fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
8776776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
878d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
8796776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
880d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
8815afe0022SRam Pai 	}
882b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
8830f0afb1dSAl Viro 		set_mnt_shared(mnt);
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
8861da177e4SLinus Torvalds 	 * as the original if that was on one */
88736341f64SRam Pai 	if (flag & CL_EXPIRE) {
8886776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
8896776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
8901da177e4SLinus Torvalds 	}
891be34d1a3SDavid Howells 
892cb338d06SAl Viro 	return mnt;
893719f5d7fSMiklos Szeredi 
894719f5d7fSMiklos Szeredi  out_free:
895719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
896be34d1a3SDavid Howells 	return ERR_PTR(err);
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
89948a066e7SAl Viro static void delayed_free(struct rcu_head *head)
90048a066e7SAl Viro {
90148a066e7SAl Viro 	struct mount *mnt = container_of(head, struct mount, mnt_rcu);
90248a066e7SAl Viro 	kfree(mnt->mnt_devname);
90348a066e7SAl Viro #ifdef CONFIG_SMP
90448a066e7SAl Viro 	free_percpu(mnt->mnt_pcp);
90548a066e7SAl Viro #endif
90648a066e7SAl Viro 	kmem_cache_free(mnt_cache, mnt);
90748a066e7SAl Viro }
90848a066e7SAl Viro 
909900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
9107b7b1aceSAl Viro {
911b3e19d92SNick Piggin put_again:
91248a066e7SAl Viro 	rcu_read_lock();
913aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
91448a066e7SAl Viro 	if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
91548a066e7SAl Viro 		rcu_read_unlock();
91699b7db7bSNick Piggin 		return;
917b3e19d92SNick Piggin 	}
918719ea2fbSAl Viro 	lock_mount_hash();
919b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
92048a066e7SAl Viro 		rcu_read_unlock();
921719ea2fbSAl Viro 		unlock_mount_hash();
92299b7db7bSNick Piggin 		return;
92399b7db7bSNick Piggin 	}
924863d684fSAl Viro 	if (unlikely(mnt->mnt_pinned)) {
925863d684fSAl Viro 		mnt_add_count(mnt, mnt->mnt_pinned + 1);
926863d684fSAl Viro 		mnt->mnt_pinned = 0;
92748a066e7SAl Viro 		rcu_read_unlock();
928719ea2fbSAl Viro 		unlock_mount_hash();
929900148dcSAl Viro 		acct_auto_close_mnt(&mnt->mnt);
930b3e19d92SNick Piggin 		goto put_again;
931b3e19d92SNick Piggin 	}
93248a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
93348a066e7SAl Viro 		rcu_read_unlock();
93448a066e7SAl Viro 		unlock_mount_hash();
93548a066e7SAl Viro 		return;
93648a066e7SAl Viro 	}
93748a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
93848a066e7SAl Viro 	rcu_read_unlock();
939962830dfSAndi Kleen 
94039f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
941719ea2fbSAl Viro 	unlock_mount_hash();
942649a795aSAl Viro 
943649a795aSAl Viro 	/*
944649a795aSAl Viro 	 * This probably indicates that somebody messed
945649a795aSAl Viro 	 * up a mnt_want/drop_write() pair.  If this
946649a795aSAl Viro 	 * happens, the filesystem was probably unable
947649a795aSAl Viro 	 * to make r/w->r/o transitions.
948649a795aSAl Viro 	 */
949649a795aSAl Viro 	/*
950649a795aSAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
951649a795aSAl Viro 	 * so mnt_get_writers() below is safe.
952649a795aSAl Viro 	 */
953649a795aSAl Viro 	WARN_ON(mnt_get_writers(mnt));
954649a795aSAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
955649a795aSAl Viro 	dput(mnt->mnt.mnt_root);
956649a795aSAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
95748a066e7SAl Viro 	mnt_free_id(mnt);
95848a066e7SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free);
959b3e19d92SNick Piggin }
960b3e19d92SNick Piggin 
961b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
962b3e19d92SNick Piggin {
963b3e19d92SNick Piggin 	if (mnt) {
964863d684fSAl Viro 		struct mount *m = real_mount(mnt);
965b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
966863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
967863d684fSAl Viro 			m->mnt_expiry_mark = 0;
968863d684fSAl Viro 		mntput_no_expire(m);
969b3e19d92SNick Piggin 	}
970b3e19d92SNick Piggin }
971b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
972b3e19d92SNick Piggin 
973b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
974b3e19d92SNick Piggin {
975b3e19d92SNick Piggin 	if (mnt)
97683adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
977b3e19d92SNick Piggin 	return mnt;
978b3e19d92SNick Piggin }
979b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
980b3e19d92SNick Piggin 
9817b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
9827b7b1aceSAl Viro {
983719ea2fbSAl Viro 	lock_mount_hash();
984863d684fSAl Viro 	real_mount(mnt)->mnt_pinned++;
985719ea2fbSAl Viro 	unlock_mount_hash();
9867b7b1aceSAl Viro }
9877b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
9887b7b1aceSAl Viro 
989863d684fSAl Viro void mnt_unpin(struct vfsmount *m)
9907b7b1aceSAl Viro {
991863d684fSAl Viro 	struct mount *mnt = real_mount(m);
992719ea2fbSAl Viro 	lock_mount_hash();
9937b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
994863d684fSAl Viro 		mnt_add_count(mnt, 1);
9957b7b1aceSAl Viro 		mnt->mnt_pinned--;
9967b7b1aceSAl Viro 	}
997719ea2fbSAl Viro 	unlock_mount_hash();
9987b7b1aceSAl Viro }
9997b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
10001da177e4SLinus Torvalds 
1001b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
1002b3b304a2SMiklos Szeredi {
1003b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
1004b3b304a2SMiklos Szeredi }
1005b3b304a2SMiklos Szeredi 
1006b3b304a2SMiklos Szeredi /*
1007b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
1008b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
1009b3b304a2SMiklos Szeredi  *
1010b3b304a2SMiklos Szeredi  * See also save_mount_options().
1011b3b304a2SMiklos Szeredi  */
101234c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root)
1013b3b304a2SMiklos Szeredi {
10142a32cebdSAl Viro 	const char *options;
10152a32cebdSAl Viro 
10162a32cebdSAl Viro 	rcu_read_lock();
101734c80b1dSAl Viro 	options = rcu_dereference(root->d_sb->s_options);
1018b3b304a2SMiklos Szeredi 
1019b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
1020b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
1021b3b304a2SMiklos Szeredi 		mangle(m, options);
1022b3b304a2SMiklos Szeredi 	}
10232a32cebdSAl Viro 	rcu_read_unlock();
1024b3b304a2SMiklos Szeredi 
1025b3b304a2SMiklos Szeredi 	return 0;
1026b3b304a2SMiklos Szeredi }
1027b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
1028b3b304a2SMiklos Szeredi 
1029b3b304a2SMiklos Szeredi /*
1030b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
1031b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
1032b3b304a2SMiklos Szeredi  *
1033b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
1034b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
1035b3b304a2SMiklos Szeredi  * remount fails.
1036b3b304a2SMiklos Szeredi  *
1037b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
1038b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
1039b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
1040b3b304a2SMiklos Szeredi  * any more.
1041b3b304a2SMiklos Szeredi  */
1042b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
1043b3b304a2SMiklos Szeredi {
10442a32cebdSAl Viro 	BUG_ON(sb->s_options);
10452a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1046b3b304a2SMiklos Szeredi }
1047b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
1048b3b304a2SMiklos Szeredi 
10492a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
10502a32cebdSAl Viro {
10512a32cebdSAl Viro 	char *old = sb->s_options;
10522a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
10532a32cebdSAl Viro 	if (old) {
10542a32cebdSAl Viro 		synchronize_rcu();
10552a32cebdSAl Viro 		kfree(old);
10562a32cebdSAl Viro 	}
10572a32cebdSAl Viro }
10582a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
10592a32cebdSAl Viro 
1060a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
10610226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
10621da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
10631da177e4SLinus Torvalds {
10646ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
10651da177e4SLinus Torvalds 
1066390c6843SRam Pai 	down_read(&namespace_sem);
1067a1a2c409SMiklos Szeredi 	return seq_list_start(&p->ns->list, *pos);
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
10711da177e4SLinus Torvalds {
10726ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
1073b0765fb8SPavel Emelianov 
1074a1a2c409SMiklos Szeredi 	return seq_list_next(v, &p->ns->list, pos);
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
10781da177e4SLinus Torvalds {
1079390c6843SRam Pai 	up_read(&namespace_sem);
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10820226f492SAl Viro static int m_show(struct seq_file *m, void *v)
10839f5596afSAl Viro {
10846ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
10851a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
10860226f492SAl Viro 	return p->show(m, &r->mnt);
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
1089a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
10901da177e4SLinus Torvalds 	.start	= m_start,
10911da177e4SLinus Torvalds 	.next	= m_next,
10921da177e4SLinus Torvalds 	.stop	= m_stop,
10930226f492SAl Viro 	.show	= m_show,
1094b4629fe2SChuck Lever };
1095a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1096b4629fe2SChuck Lever 
10971da177e4SLinus Torvalds /**
10981da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
10991da177e4SLinus Torvalds  * @mnt: root of mount tree
11001da177e4SLinus Torvalds  *
11011da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
11021da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
11031da177e4SLinus Torvalds  * busy.
11041da177e4SLinus Torvalds  */
1105909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
11061da177e4SLinus Torvalds {
1107909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
110836341f64SRam Pai 	int actual_refs = 0;
110936341f64SRam Pai 	int minimum_refs = 0;
1110315fc83eSAl Viro 	struct mount *p;
1111909b0a88SAl Viro 	BUG_ON(!m);
11121da177e4SLinus Torvalds 
1113b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1114719ea2fbSAl Viro 	lock_mount_hash();
1115909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
111683adc753SAl Viro 		actual_refs += mnt_get_count(p);
11171da177e4SLinus Torvalds 		minimum_refs += 2;
11181da177e4SLinus Torvalds 	}
1119719ea2fbSAl Viro 	unlock_mount_hash();
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
11221da177e4SLinus Torvalds 		return 0;
1123e3474a8eSIan Kent 
1124e3474a8eSIan Kent 	return 1;
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds /**
11301da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
11311da177e4SLinus Torvalds  * @mnt: root of mount
11321da177e4SLinus Torvalds  *
11331da177e4SLinus Torvalds  * This is called to check if a mount point has any
11341da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
11351da177e4SLinus Torvalds  * mount has sub mounts this will return busy
11361da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
11371da177e4SLinus Torvalds  *
11381da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
11391da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
11401da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
11411da177e4SLinus Torvalds  */
11421da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
11431da177e4SLinus Torvalds {
1144e3474a8eSIan Kent 	int ret = 1;
11458ad08d8aSAl Viro 	down_read(&namespace_sem);
1146719ea2fbSAl Viro 	lock_mount_hash();
11471ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1148e3474a8eSIan Kent 		ret = 0;
1149719ea2fbSAl Viro 	unlock_mount_hash();
11508ad08d8aSAl Viro 	up_read(&namespace_sem);
1151a05964f3SRam Pai 	return ret;
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
11551da177e4SLinus Torvalds 
1156e3197d83SAl Viro static LIST_HEAD(unmounted);	/* protected by namespace_sem */
1157e3197d83SAl Viro 
115897216be0SAl Viro static void namespace_unlock(void)
11591da177e4SLinus Torvalds {
1160d5e50f74SAl Viro 	struct mount *mnt;
116197216be0SAl Viro 	LIST_HEAD(head);
116297216be0SAl Viro 
116397216be0SAl Viro 	if (likely(list_empty(&unmounted))) {
116497216be0SAl Viro 		up_write(&namespace_sem);
116597216be0SAl Viro 		return;
116697216be0SAl Viro 	}
116797216be0SAl Viro 
116897216be0SAl Viro 	list_splice_init(&unmounted, &head);
116997216be0SAl Viro 	up_write(&namespace_sem);
117097216be0SAl Viro 
117148a066e7SAl Viro 	synchronize_rcu();
117248a066e7SAl Viro 
117397216be0SAl Viro 	while (!list_empty(&head)) {
117497216be0SAl Viro 		mnt = list_first_entry(&head, struct mount, mnt_hash);
11751b8e5564SAl Viro 		list_del_init(&mnt->mnt_hash);
1176aba809cfSAl Viro 		if (mnt->mnt_ex_mountpoint.mnt)
1177aba809cfSAl Viro 			path_put(&mnt->mnt_ex_mountpoint);
1178d5e50f74SAl Viro 		mntput(&mnt->mnt);
117970fbcdf4SRam Pai 	}
118070fbcdf4SRam Pai }
118170fbcdf4SRam Pai 
118297216be0SAl Viro static inline void namespace_lock(void)
1183e3197d83SAl Viro {
118497216be0SAl Viro 	down_write(&namespace_sem);
1185e3197d83SAl Viro }
1186e3197d83SAl Viro 
118799b7db7bSNick Piggin /*
118848a066e7SAl Viro  * mount_lock must be held
118999b7db7bSNick Piggin  * namespace_sem must be held for write
119048a066e7SAl Viro  * how = 0 => just this tree, don't propagate
119148a066e7SAl Viro  * how = 1 => propagate; we know that nobody else has reference to any victims
119248a066e7SAl Viro  * how = 2 => lazy umount
119399b7db7bSNick Piggin  */
119448a066e7SAl Viro void umount_tree(struct mount *mnt, int how)
119570fbcdf4SRam Pai {
11967b8a53fdSAl Viro 	LIST_HEAD(tmp_list);
1197315fc83eSAl Viro 	struct mount *p;
119870fbcdf4SRam Pai 
1199909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt))
12001b8e5564SAl Viro 		list_move(&p->mnt_hash, &tmp_list);
120170fbcdf4SRam Pai 
120248a066e7SAl Viro 	if (how)
12037b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1204a05964f3SRam Pai 
12051b8e5564SAl Viro 	list_for_each_entry(p, &tmp_list, mnt_hash) {
12066776db3dSAl Viro 		list_del_init(&p->mnt_expire);
12071a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1208143c8c91SAl Viro 		__touch_mnt_namespace(p->mnt_ns);
120963d37a84SAl Viro 		p->mnt_ns = NULL;
121048a066e7SAl Viro 		if (how < 2)
121148a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
12126b41d536SAl Viro 		list_del_init(&p->mnt_child);
1213676da58dSAl Viro 		if (mnt_has_parent(p)) {
121484d17192SAl Viro 			put_mountpoint(p->mnt_mp);
1215aba809cfSAl Viro 			/* move the reference to mountpoint into ->mnt_ex_mountpoint */
1216aba809cfSAl Viro 			p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint;
1217aba809cfSAl Viro 			p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt;
1218aba809cfSAl Viro 			p->mnt_mountpoint = p->mnt.mnt_root;
1219aba809cfSAl Viro 			p->mnt_parent = p;
122084d17192SAl Viro 			p->mnt_mp = NULL;
12217c4b93d8SAl Viro 		}
12220f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
12231da177e4SLinus Torvalds 	}
1224328e6d90SAl Viro 	list_splice(&tmp_list, &unmounted);
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
1227b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1228c35038beSAl Viro 
12291ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
12301da177e4SLinus Torvalds {
12311ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
12321da177e4SLinus Torvalds 	int retval;
12331da177e4SLinus Torvalds 
12341ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
12351da177e4SLinus Torvalds 	if (retval)
12361da177e4SLinus Torvalds 		return retval;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	/*
12391da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
12401da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
12411da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
12421da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
12431da177e4SLinus Torvalds 	 */
12441da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
12451ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
12461da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
12471da177e4SLinus Torvalds 			return -EINVAL;
12481da177e4SLinus Torvalds 
1249b3e19d92SNick Piggin 		/*
1250b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1251b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1252b3e19d92SNick Piggin 		 */
1253719ea2fbSAl Viro 		lock_mount_hash();
125483adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1255719ea2fbSAl Viro 			unlock_mount_hash();
12561da177e4SLinus Torvalds 			return -EBUSY;
1257b3e19d92SNick Piggin 		}
1258719ea2fbSAl Viro 		unlock_mount_hash();
12591da177e4SLinus Torvalds 
1260863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
12611da177e4SLinus Torvalds 			return -EAGAIN;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	/*
12651da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
12661da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
12671da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
12681da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
12691da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
12701da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
12711da177e4SLinus Torvalds 	 * about for the moment.
12721da177e4SLinus Torvalds 	 */
12731da177e4SLinus Torvalds 
127442faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
127542faad99SAl Viro 		sb->s_op->umount_begin(sb);
127642faad99SAl Viro 	}
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	/*
12791da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
12801da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
12811da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
12821da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
12831da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
12841da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
12851da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
12861da177e4SLinus Torvalds 	 */
12871ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
12881da177e4SLinus Torvalds 		/*
12891da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
12901da177e4SLinus Torvalds 		 * we just try to remount it readonly.
12911da177e4SLinus Torvalds 		 */
12921da177e4SLinus Torvalds 		down_write(&sb->s_umount);
12934aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
12941da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
12951da177e4SLinus Torvalds 		up_write(&sb->s_umount);
12961da177e4SLinus Torvalds 		return retval;
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds 
129997216be0SAl Viro 	namespace_lock();
1300719ea2fbSAl Viro 	lock_mount_hash();
13015addc5ddSAl Viro 	event++;
13021da177e4SLinus Torvalds 
130348a066e7SAl Viro 	if (flags & MNT_DETACH) {
130448a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
130548a066e7SAl Viro 			umount_tree(mnt, 2);
130648a066e7SAl Viro 		retval = 0;
130748a066e7SAl Viro 	} else {
1308b54b9be7SAl Viro 		shrink_submounts(mnt);
13091da177e4SLinus Torvalds 		retval = -EBUSY;
131048a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
13111a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1312328e6d90SAl Viro 				umount_tree(mnt, 1);
13131da177e4SLinus Torvalds 			retval = 0;
13141da177e4SLinus Torvalds 		}
131548a066e7SAl Viro 	}
1316719ea2fbSAl Viro 	unlock_mount_hash();
1317e3197d83SAl Viro 	namespace_unlock();
13181da177e4SLinus Torvalds 	return retval;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds /*
13229b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
13239b40bc90SAl Viro  */
13249b40bc90SAl Viro static inline bool may_mount(void)
13259b40bc90SAl Viro {
13269b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
13279b40bc90SAl Viro }
13289b40bc90SAl Viro 
13299b40bc90SAl Viro /*
13301da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
13311da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
13321da177e4SLinus Torvalds  *
13331da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
13341da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
13351da177e4SLinus Torvalds  */
13361da177e4SLinus Torvalds 
1337bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
13381da177e4SLinus Torvalds {
13392d8f3038SAl Viro 	struct path path;
1340900148dcSAl Viro 	struct mount *mnt;
13411da177e4SLinus Torvalds 	int retval;
1342db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
13431da177e4SLinus Torvalds 
1344db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1345db1f05bbSMiklos Szeredi 		return -EINVAL;
1346db1f05bbSMiklos Szeredi 
13479b40bc90SAl Viro 	if (!may_mount())
13489b40bc90SAl Viro 		return -EPERM;
13499b40bc90SAl Viro 
1350db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1351db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1352db1f05bbSMiklos Szeredi 
1353197df04cSAl Viro 	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
13541da177e4SLinus Torvalds 	if (retval)
13551da177e4SLinus Torvalds 		goto out;
1356900148dcSAl Viro 	mnt = real_mount(path.mnt);
13571da177e4SLinus Torvalds 	retval = -EINVAL;
13582d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
13591da177e4SLinus Torvalds 		goto dput_and_out;
1360143c8c91SAl Viro 	if (!check_mnt(mnt))
13611da177e4SLinus Torvalds 		goto dput_and_out;
13625ff9d8a6SEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
13635ff9d8a6SEric W. Biederman 		goto dput_and_out;
13641da177e4SLinus Torvalds 
1365900148dcSAl Viro 	retval = do_umount(mnt, flags);
13661da177e4SLinus Torvalds dput_and_out:
1367429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
13682d8f3038SAl Viro 	dput(path.dentry);
1369900148dcSAl Viro 	mntput_no_expire(mnt);
13701da177e4SLinus Torvalds out:
13711da177e4SLinus Torvalds 	return retval;
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds /*
13771da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
13781da177e4SLinus Torvalds  */
1379bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	return sys_umount(name, 0);
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds #endif
13851da177e4SLinus Torvalds 
13864ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
13878823c079SEric W. Biederman {
13884ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
13894ce5d2b1SEric W. Biederman 	struct inode *inode = dentry->d_inode;
13900bb80f24SDavid Howells 	struct proc_ns *ei;
13918823c079SEric W. Biederman 
13928823c079SEric W. Biederman 	if (!proc_ns_inode(inode))
13938823c079SEric W. Biederman 		return false;
13948823c079SEric W. Biederman 
13950bb80f24SDavid Howells 	ei = get_proc_ns(inode);
13968823c079SEric W. Biederman 	if (ei->ns_ops != &mntns_operations)
13978823c079SEric W. Biederman 		return false;
13988823c079SEric W. Biederman 
13994ce5d2b1SEric W. Biederman 	return true;
14004ce5d2b1SEric W. Biederman }
14014ce5d2b1SEric W. Biederman 
14024ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
14034ce5d2b1SEric W. Biederman {
14044ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
14054ce5d2b1SEric W. Biederman 	 * mount namespace loop?
14064ce5d2b1SEric W. Biederman 	 */
14074ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
14084ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
14094ce5d2b1SEric W. Biederman 		return false;
14104ce5d2b1SEric W. Biederman 
14114ce5d2b1SEric W. Biederman 	mnt_ns = get_proc_ns(dentry->d_inode)->ns;
14128823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
14138823c079SEric W. Biederman }
14148823c079SEric W. Biederman 
141587129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
141636341f64SRam Pai 					int flag)
14171da177e4SLinus Torvalds {
141884d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
14191da177e4SLinus Torvalds 
14204ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
14214ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
14224ce5d2b1SEric W. Biederman 
14234ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1424be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
14259676f0c6SRam Pai 
142636341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1427be34d1a3SDavid Howells 	if (IS_ERR(q))
1428be34d1a3SDavid Howells 		return q;
1429be34d1a3SDavid Howells 
14305ff9d8a6SEric W. Biederman 	q->mnt.mnt_flags &= ~MNT_LOCKED;
1431a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	p = mnt;
14346b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1435315fc83eSAl Viro 		struct mount *s;
14367ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
14371da177e4SLinus Torvalds 			continue;
14381da177e4SLinus Torvalds 
1439909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
14404ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
14414ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
14424ce5d2b1SEric W. Biederman 				s = skip_mnt_tree(s);
14434ce5d2b1SEric W. Biederman 				continue;
14444ce5d2b1SEric W. Biederman 			}
14454ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
14464ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
14479676f0c6SRam Pai 				s = skip_mnt_tree(s);
14489676f0c6SRam Pai 				continue;
14499676f0c6SRam Pai 			}
14500714a533SAl Viro 			while (p != s->mnt_parent) {
14510714a533SAl Viro 				p = p->mnt_parent;
14520714a533SAl Viro 				q = q->mnt_parent;
14531da177e4SLinus Torvalds 			}
145487129cc0SAl Viro 			p = s;
145584d17192SAl Viro 			parent = q;
145687129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1457be34d1a3SDavid Howells 			if (IS_ERR(q))
1458be34d1a3SDavid Howells 				goto out;
1459719ea2fbSAl Viro 			lock_mount_hash();
14601a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
146184d17192SAl Viro 			attach_mnt(q, parent, p->mnt_mp);
1462719ea2fbSAl Viro 			unlock_mount_hash();
14631da177e4SLinus Torvalds 		}
14641da177e4SLinus Torvalds 	}
14651da177e4SLinus Torvalds 	return res;
1466be34d1a3SDavid Howells out:
14671da177e4SLinus Torvalds 	if (res) {
1468719ea2fbSAl Viro 		lock_mount_hash();
1469328e6d90SAl Viro 		umount_tree(res, 0);
1470719ea2fbSAl Viro 		unlock_mount_hash();
14711da177e4SLinus Torvalds 	}
1472be34d1a3SDavid Howells 	return q;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
1475be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1476be34d1a3SDavid Howells 
1477589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
14788aec0809SAl Viro {
1479cb338d06SAl Viro 	struct mount *tree;
148097216be0SAl Viro 	namespace_lock();
148187129cc0SAl Viro 	tree = copy_tree(real_mount(path->mnt), path->dentry,
148287129cc0SAl Viro 			 CL_COPY_ALL | CL_PRIVATE);
1483328e6d90SAl Viro 	namespace_unlock();
1484be34d1a3SDavid Howells 	if (IS_ERR(tree))
148552e220d3SDan Carpenter 		return ERR_CAST(tree);
1486be34d1a3SDavid Howells 	return &tree->mnt;
14878aec0809SAl Viro }
14888aec0809SAl Viro 
14898aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
14908aec0809SAl Viro {
149197216be0SAl Viro 	namespace_lock();
1492719ea2fbSAl Viro 	lock_mount_hash();
1493328e6d90SAl Viro 	umount_tree(real_mount(mnt), 0);
1494719ea2fbSAl Viro 	unlock_mount_hash();
14953ab6abeeSAl Viro 	namespace_unlock();
14968aec0809SAl Viro }
14978aec0809SAl Viro 
14981f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
14991f707137SAl Viro 		   struct vfsmount *root)
15001f707137SAl Viro {
15011a4eeaf2SAl Viro 	struct mount *mnt;
15021f707137SAl Viro 	int res = f(root, arg);
15031f707137SAl Viro 	if (res)
15041f707137SAl Viro 		return res;
15051a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
15061a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
15071f707137SAl Viro 		if (res)
15081f707137SAl Viro 			return res;
15091f707137SAl Viro 	}
15101f707137SAl Viro 	return 0;
15111f707137SAl Viro }
15121f707137SAl Viro 
15134b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1514719f5d7fSMiklos Szeredi {
1515315fc83eSAl Viro 	struct mount *p;
1516719f5d7fSMiklos Szeredi 
1517909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1518fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
15194b8b21f4SAl Viro 			mnt_release_group_id(p);
1520719f5d7fSMiklos Szeredi 	}
1521719f5d7fSMiklos Szeredi }
1522719f5d7fSMiklos Szeredi 
15234b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1524719f5d7fSMiklos Szeredi {
1525315fc83eSAl Viro 	struct mount *p;
1526719f5d7fSMiklos Szeredi 
1527909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1528fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
15294b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1530719f5d7fSMiklos Szeredi 			if (err) {
15314b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1532719f5d7fSMiklos Szeredi 				return err;
1533719f5d7fSMiklos Szeredi 			}
1534719f5d7fSMiklos Szeredi 		}
1535719f5d7fSMiklos Szeredi 	}
1536719f5d7fSMiklos Szeredi 
1537719f5d7fSMiklos Szeredi 	return 0;
1538719f5d7fSMiklos Szeredi }
1539719f5d7fSMiklos Szeredi 
1540b90fa9aeSRam Pai /*
1541b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1542b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
154321444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
154421444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
154521444403SRam Pai  *  		   (done when source_mnt is moved)
1546b90fa9aeSRam Pai  *
1547b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1548b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
15499676f0c6SRam Pai  * ---------------------------------------------------------------------------
1550b90fa9aeSRam Pai  * |         BIND 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 (*) |  invalid   |
15609676f0c6SRam Pai  * ***************************************************************************
1561b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1562b90fa9aeSRam Pai  * destination mount.
1563b90fa9aeSRam Pai  *
1564b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1565b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1566b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1567b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1568b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1569b90fa9aeSRam Pai  *       mount.
15705afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
15715afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
15725afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
15735afe0022SRam Pai  *       is marked as 'shared and slave'.
15745afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
15755afe0022SRam Pai  * 	 source mount.
15765afe0022SRam Pai  *
15779676f0c6SRam Pai  * ---------------------------------------------------------------------------
157821444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
15799676f0c6SRam Pai  * |**************************************************************************
15809676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
15819676f0c6SRam Pai  * | dest     |               |                |                |            |
15829676f0c6SRam Pai  * |   |      |               |                |                |            |
15839676f0c6SRam Pai  * |   v      |               |                |                |            |
15849676f0c6SRam Pai  * |**************************************************************************
15859676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
15865afe0022SRam Pai  * |          |               |                |                |            |
15879676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
15889676f0c6SRam Pai  * ***************************************************************************
15895afe0022SRam Pai  *
15905afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
15915afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
159221444403SRam Pai  * (+*)  the mount is moved to the destination.
15935afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
15945afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
15955afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
15965afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1597b90fa9aeSRam Pai  *
1598b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1599b90fa9aeSRam Pai  * applied to each mount in the tree.
1600b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1601b90fa9aeSRam Pai  * in allocations.
1602b90fa9aeSRam Pai  */
16030fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
160484d17192SAl Viro 			struct mount *dest_mnt,
160584d17192SAl Viro 			struct mountpoint *dest_mp,
160684d17192SAl Viro 			struct path *parent_path)
1607b90fa9aeSRam Pai {
1608b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
1609315fc83eSAl Viro 	struct mount *child, *p;
1610719f5d7fSMiklos Szeredi 	int err;
1611b90fa9aeSRam Pai 
1612fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
16130fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
1614719f5d7fSMiklos Szeredi 		if (err)
1615719f5d7fSMiklos Szeredi 			goto out;
1616719f5d7fSMiklos Szeredi 	}
161784d17192SAl Viro 	err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1618719f5d7fSMiklos Szeredi 	if (err)
1619719f5d7fSMiklos Szeredi 		goto out_cleanup_ids;
1620b90fa9aeSRam Pai 
1621719ea2fbSAl Viro 	lock_mount_hash();
1622df1a1ad2SAl Viro 
1623fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
1624909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
16250f0afb1dSAl Viro 			set_mnt_shared(p);
1626b90fa9aeSRam Pai 	}
16271a390689SAl Viro 	if (parent_path) {
16280fb54e50SAl Viro 		detach_mnt(source_mnt, parent_path);
162984d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
1630143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
163121444403SRam Pai 	} else {
163284d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
16330fb54e50SAl Viro 		commit_tree(source_mnt);
163421444403SRam Pai 	}
1635b90fa9aeSRam Pai 
16361b8e5564SAl Viro 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
16371b8e5564SAl Viro 		list_del_init(&child->mnt_hash);
16384b2619a5SAl Viro 		commit_tree(child);
1639b90fa9aeSRam Pai 	}
1640719ea2fbSAl Viro 	unlock_mount_hash();
164199b7db7bSNick Piggin 
1642b90fa9aeSRam Pai 	return 0;
1643719f5d7fSMiklos Szeredi 
1644719f5d7fSMiklos Szeredi  out_cleanup_ids:
1645fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt))
16460fb54e50SAl Viro 		cleanup_group_ids(source_mnt, NULL);
1647719f5d7fSMiklos Szeredi  out:
1648719f5d7fSMiklos Szeredi 	return err;
1649b90fa9aeSRam Pai }
1650b90fa9aeSRam Pai 
165184d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
1652b12cea91SAl Viro {
1653b12cea91SAl Viro 	struct vfsmount *mnt;
165484d17192SAl Viro 	struct dentry *dentry = path->dentry;
1655b12cea91SAl Viro retry:
165684d17192SAl Viro 	mutex_lock(&dentry->d_inode->i_mutex);
165784d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
165884d17192SAl Viro 		mutex_unlock(&dentry->d_inode->i_mutex);
165984d17192SAl Viro 		return ERR_PTR(-ENOENT);
1660b12cea91SAl Viro 	}
166197216be0SAl Viro 	namespace_lock();
1662b12cea91SAl Viro 	mnt = lookup_mnt(path);
166384d17192SAl Viro 	if (likely(!mnt)) {
166484d17192SAl Viro 		struct mountpoint *mp = new_mountpoint(dentry);
166584d17192SAl Viro 		if (IS_ERR(mp)) {
166697216be0SAl Viro 			namespace_unlock();
166784d17192SAl Viro 			mutex_unlock(&dentry->d_inode->i_mutex);
166884d17192SAl Viro 			return mp;
166984d17192SAl Viro 		}
167084d17192SAl Viro 		return mp;
167184d17192SAl Viro 	}
167297216be0SAl Viro 	namespace_unlock();
1673b12cea91SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
1674b12cea91SAl Viro 	path_put(path);
1675b12cea91SAl Viro 	path->mnt = mnt;
167684d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
1677b12cea91SAl Viro 	goto retry;
1678b12cea91SAl Viro }
1679b12cea91SAl Viro 
168084d17192SAl Viro static void unlock_mount(struct mountpoint *where)
1681b12cea91SAl Viro {
168284d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
168384d17192SAl Viro 	put_mountpoint(where);
1684328e6d90SAl Viro 	namespace_unlock();
168584d17192SAl Viro 	mutex_unlock(&dentry->d_inode->i_mutex);
1686b12cea91SAl Viro }
1687b12cea91SAl Viro 
168884d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
16891da177e4SLinus Torvalds {
169095bc5f25SAl Viro 	if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
16911da177e4SLinus Torvalds 		return -EINVAL;
16921da177e4SLinus Torvalds 
169384d17192SAl Viro 	if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
169495bc5f25SAl Viro 	      S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
16951da177e4SLinus Torvalds 		return -ENOTDIR;
16961da177e4SLinus Torvalds 
169784d17192SAl Viro 	return attach_recursive_mnt(mnt, p, mp, NULL);
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds /*
17017a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
17027a2e8a8fSValerie Aurora  */
17037a2e8a8fSValerie Aurora 
17047a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags)
17057a2e8a8fSValerie Aurora {
17067c6e984dSRoman Borisov 	int type = flags & ~(MS_REC | MS_SILENT);
17077a2e8a8fSValerie Aurora 
17087a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
17097a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
17107a2e8a8fSValerie Aurora 		return 0;
17117a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
17127a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
17137a2e8a8fSValerie Aurora 		return 0;
17147a2e8a8fSValerie Aurora 	return type;
17157a2e8a8fSValerie Aurora }
17167a2e8a8fSValerie Aurora 
17177a2e8a8fSValerie Aurora /*
171807b20889SRam Pai  * recursively change the type of the mountpoint.
171907b20889SRam Pai  */
17200a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
172107b20889SRam Pai {
1722315fc83eSAl Viro 	struct mount *m;
17234b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
172407b20889SRam Pai 	int recurse = flag & MS_REC;
17257a2e8a8fSValerie Aurora 	int type;
1726719f5d7fSMiklos Szeredi 	int err = 0;
172707b20889SRam Pai 
17282d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
172907b20889SRam Pai 		return -EINVAL;
173007b20889SRam Pai 
17317a2e8a8fSValerie Aurora 	type = flags_to_propagation_type(flag);
17327a2e8a8fSValerie Aurora 	if (!type)
17337a2e8a8fSValerie Aurora 		return -EINVAL;
17347a2e8a8fSValerie Aurora 
173597216be0SAl Viro 	namespace_lock();
1736719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1737719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1738719f5d7fSMiklos Szeredi 		if (err)
1739719f5d7fSMiklos Szeredi 			goto out_unlock;
1740719f5d7fSMiklos Szeredi 	}
1741719f5d7fSMiklos Szeredi 
1742719ea2fbSAl Viro 	lock_mount_hash();
1743909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
17440f0afb1dSAl Viro 		change_mnt_propagation(m, type);
1745719ea2fbSAl Viro 	unlock_mount_hash();
1746719f5d7fSMiklos Szeredi 
1747719f5d7fSMiklos Szeredi  out_unlock:
174897216be0SAl Viro 	namespace_unlock();
1749719f5d7fSMiklos Szeredi 	return err;
175007b20889SRam Pai }
175107b20889SRam Pai 
17525ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
17535ff9d8a6SEric W. Biederman {
17545ff9d8a6SEric W. Biederman 	struct mount *child;
17555ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
17565ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
17575ff9d8a6SEric W. Biederman 			continue;
17585ff9d8a6SEric W. Biederman 
17595ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
17605ff9d8a6SEric W. Biederman 			return true;
17615ff9d8a6SEric W. Biederman 	}
17625ff9d8a6SEric W. Biederman 	return false;
17635ff9d8a6SEric W. Biederman }
17645ff9d8a6SEric W. Biederman 
176507b20889SRam Pai /*
17661da177e4SLinus Torvalds  * do loopback mount.
17671da177e4SLinus Torvalds  */
1768808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
17692dafe1c4SEric Sandeen 				int recurse)
17701da177e4SLinus Torvalds {
17712d92ab3cSAl Viro 	struct path old_path;
177284d17192SAl Viro 	struct mount *mnt = NULL, *old, *parent;
177384d17192SAl Viro 	struct mountpoint *mp;
177457eccb83SAl Viro 	int err;
17751da177e4SLinus Torvalds 	if (!old_name || !*old_name)
17761da177e4SLinus Torvalds 		return -EINVAL;
1777815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
17781da177e4SLinus Torvalds 	if (err)
17791da177e4SLinus Torvalds 		return err;
17801da177e4SLinus Torvalds 
17818823c079SEric W. Biederman 	err = -EINVAL;
17824ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
17838823c079SEric W. Biederman 		goto out;
17848823c079SEric W. Biederman 
178584d17192SAl Viro 	mp = lock_mount(path);
178684d17192SAl Viro 	err = PTR_ERR(mp);
178784d17192SAl Viro 	if (IS_ERR(mp))
17889676f0c6SRam Pai 		goto out;
17899676f0c6SRam Pai 
179087129cc0SAl Viro 	old = real_mount(old_path.mnt);
179184d17192SAl Viro 	parent = real_mount(path->mnt);
179287129cc0SAl Viro 
1793b12cea91SAl Viro 	err = -EINVAL;
1794fc7be130SAl Viro 	if (IS_MNT_UNBINDABLE(old))
1795b12cea91SAl Viro 		goto out2;
1796b12cea91SAl Viro 
179784d17192SAl Viro 	if (!check_mnt(parent) || !check_mnt(old))
1798b12cea91SAl Viro 		goto out2;
1799ccd48bc7SAl Viro 
18005ff9d8a6SEric W. Biederman 	if (!recurse && has_locked_children(old, old_path.dentry))
18015ff9d8a6SEric W. Biederman 		goto out2;
18025ff9d8a6SEric W. Biederman 
18031da177e4SLinus Torvalds 	if (recurse)
18044ce5d2b1SEric W. Biederman 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
18051da177e4SLinus Torvalds 	else
180687129cc0SAl Viro 		mnt = clone_mnt(old, old_path.dentry, 0);
18071da177e4SLinus Torvalds 
1808be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
1809be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
1810e9c5d8a5SAndrey Vagin 		goto out2;
1811be34d1a3SDavid Howells 	}
1812ccd48bc7SAl Viro 
18135ff9d8a6SEric W. Biederman 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
18145ff9d8a6SEric W. Biederman 
181584d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
18161da177e4SLinus Torvalds 	if (err) {
1817719ea2fbSAl Viro 		lock_mount_hash();
1818328e6d90SAl Viro 		umount_tree(mnt, 0);
1819719ea2fbSAl Viro 		unlock_mount_hash();
18205b83d2c5SRam Pai 	}
1821b12cea91SAl Viro out2:
182284d17192SAl Viro 	unlock_mount(mp);
1823ccd48bc7SAl Viro out:
18242d92ab3cSAl Viro 	path_put(&old_path);
18251da177e4SLinus Torvalds 	return err;
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
18282e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
18292e4b7fcdSDave Hansen {
18302e4b7fcdSDave Hansen 	int error = 0;
18312e4b7fcdSDave Hansen 	int readonly_request = 0;
18322e4b7fcdSDave Hansen 
18332e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
18342e4b7fcdSDave Hansen 		readonly_request = 1;
18352e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
18362e4b7fcdSDave Hansen 		return 0;
18372e4b7fcdSDave Hansen 
183890563b19SEric W. Biederman 	if (mnt->mnt_flags & MNT_LOCK_READONLY)
183990563b19SEric W. Biederman 		return -EPERM;
184090563b19SEric W. Biederman 
18412e4b7fcdSDave Hansen 	if (readonly_request)
184283adc753SAl Viro 		error = mnt_make_readonly(real_mount(mnt));
18432e4b7fcdSDave Hansen 	else
184483adc753SAl Viro 		__mnt_unmake_readonly(real_mount(mnt));
18452e4b7fcdSDave Hansen 	return error;
18462e4b7fcdSDave Hansen }
18472e4b7fcdSDave Hansen 
18481da177e4SLinus Torvalds /*
18491da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
18501da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
18511da177e4SLinus Torvalds  * on it - tough luck.
18521da177e4SLinus Torvalds  */
18530a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
18541da177e4SLinus Torvalds 		      void *data)
18551da177e4SLinus Torvalds {
18561da177e4SLinus Torvalds 	int err;
18572d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
1858143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
18591da177e4SLinus Torvalds 
1860143c8c91SAl Viro 	if (!check_mnt(mnt))
18611da177e4SLinus Torvalds 		return -EINVAL;
18621da177e4SLinus Torvalds 
18632d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
18641da177e4SLinus Torvalds 		return -EINVAL;
18651da177e4SLinus Torvalds 
1866ff36fe2cSEric Paris 	err = security_sb_remount(sb, data);
1867ff36fe2cSEric Paris 	if (err)
1868ff36fe2cSEric Paris 		return err;
1869ff36fe2cSEric Paris 
18701da177e4SLinus Torvalds 	down_write(&sb->s_umount);
18712e4b7fcdSDave Hansen 	if (flags & MS_BIND)
18722d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
187357eccb83SAl Viro 	else if (!capable(CAP_SYS_ADMIN))
187457eccb83SAl Viro 		err = -EPERM;
18754aa98cf7SAl Viro 	else
18761da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
18777b43a79fSAl Viro 	if (!err) {
1878719ea2fbSAl Viro 		lock_mount_hash();
1879143c8c91SAl Viro 		mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
1880143c8c91SAl Viro 		mnt->mnt.mnt_flags = mnt_flags;
1881143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1882719ea2fbSAl Viro 		unlock_mount_hash();
18830e55a7ccSDan Williams 	}
18846339dab8SAl Viro 	up_write(&sb->s_umount);
18851da177e4SLinus Torvalds 	return err;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
1888cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
18899676f0c6SRam Pai {
1890315fc83eSAl Viro 	struct mount *p;
1891909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1892fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
18939676f0c6SRam Pai 			return 1;
18949676f0c6SRam Pai 	}
18959676f0c6SRam Pai 	return 0;
18969676f0c6SRam Pai }
18979676f0c6SRam Pai 
1898808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name)
18991da177e4SLinus Torvalds {
19002d92ab3cSAl Viro 	struct path old_path, parent_path;
1901676da58dSAl Viro 	struct mount *p;
19020fb54e50SAl Viro 	struct mount *old;
190384d17192SAl Viro 	struct mountpoint *mp;
190457eccb83SAl Viro 	int err;
19051da177e4SLinus Torvalds 	if (!old_name || !*old_name)
19061da177e4SLinus Torvalds 		return -EINVAL;
19072d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
19081da177e4SLinus Torvalds 	if (err)
19091da177e4SLinus Torvalds 		return err;
19101da177e4SLinus Torvalds 
191184d17192SAl Viro 	mp = lock_mount(path);
191284d17192SAl Viro 	err = PTR_ERR(mp);
191384d17192SAl Viro 	if (IS_ERR(mp))
1914cc53ce53SDavid Howells 		goto out;
1915cc53ce53SDavid Howells 
1916143c8c91SAl Viro 	old = real_mount(old_path.mnt);
1917fc7be130SAl Viro 	p = real_mount(path->mnt);
1918143c8c91SAl Viro 
19191da177e4SLinus Torvalds 	err = -EINVAL;
1920fc7be130SAl Viro 	if (!check_mnt(p) || !check_mnt(old))
19211da177e4SLinus Torvalds 		goto out1;
19221da177e4SLinus Torvalds 
19235ff9d8a6SEric W. Biederman 	if (old->mnt.mnt_flags & MNT_LOCKED)
19245ff9d8a6SEric W. Biederman 		goto out1;
19255ff9d8a6SEric W. Biederman 
19261da177e4SLinus Torvalds 	err = -EINVAL;
19272d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
192821444403SRam Pai 		goto out1;
19291da177e4SLinus Torvalds 
1930676da58dSAl Viro 	if (!mnt_has_parent(old))
193121444403SRam Pai 		goto out1;
19321da177e4SLinus Torvalds 
19332d92ab3cSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
19342d92ab3cSAl Viro 	      S_ISDIR(old_path.dentry->d_inode->i_mode))
193521444403SRam Pai 		goto out1;
193621444403SRam Pai 	/*
193721444403SRam Pai 	 * Don't move a mount residing in a shared parent.
193821444403SRam Pai 	 */
1939fc7be130SAl Viro 	if (IS_MNT_SHARED(old->mnt_parent))
194021444403SRam Pai 		goto out1;
19419676f0c6SRam Pai 	/*
19429676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
19439676f0c6SRam Pai 	 * mount which is shared.
19449676f0c6SRam Pai 	 */
1945fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
19469676f0c6SRam Pai 		goto out1;
19471da177e4SLinus Torvalds 	err = -ELOOP;
1948fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
1949676da58dSAl Viro 		if (p == old)
195021444403SRam Pai 			goto out1;
19511da177e4SLinus Torvalds 
195284d17192SAl Viro 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
19534ac91378SJan Blunck 	if (err)
195421444403SRam Pai 		goto out1;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
19571da177e4SLinus Torvalds 	 * automatically */
19586776db3dSAl Viro 	list_del_init(&old->mnt_expire);
19591da177e4SLinus Torvalds out1:
196084d17192SAl Viro 	unlock_mount(mp);
19611da177e4SLinus Torvalds out:
19621da177e4SLinus Torvalds 	if (!err)
19631a390689SAl Viro 		path_put(&parent_path);
19642d92ab3cSAl Viro 	path_put(&old_path);
19651da177e4SLinus Torvalds 	return err;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19689d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
19699d412a43SAl Viro {
19709d412a43SAl Viro 	int err;
19719d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
19729d412a43SAl Viro 	if (subtype) {
19739d412a43SAl Viro 		subtype++;
19749d412a43SAl Viro 		err = -EINVAL;
19759d412a43SAl Viro 		if (!subtype[0])
19769d412a43SAl Viro 			goto err;
19779d412a43SAl Viro 	} else
19789d412a43SAl Viro 		subtype = "";
19799d412a43SAl Viro 
19809d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
19819d412a43SAl Viro 	err = -ENOMEM;
19829d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
19839d412a43SAl Viro 		goto err;
19849d412a43SAl Viro 	return mnt;
19859d412a43SAl Viro 
19869d412a43SAl Viro  err:
19879d412a43SAl Viro 	mntput(mnt);
19889d412a43SAl Viro 	return ERR_PTR(err);
19899d412a43SAl Viro }
19909d412a43SAl Viro 
19919d412a43SAl Viro /*
19929d412a43SAl Viro  * add a mount into a namespace's mount tree
19939d412a43SAl Viro  */
199495bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
19959d412a43SAl Viro {
199684d17192SAl Viro 	struct mountpoint *mp;
199784d17192SAl Viro 	struct mount *parent;
19989d412a43SAl Viro 	int err;
19999d412a43SAl Viro 
200048a066e7SAl Viro 	mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL | MNT_DOOMED | MNT_SYNC_UMOUNT);
20019d412a43SAl Viro 
200284d17192SAl Viro 	mp = lock_mount(path);
200384d17192SAl Viro 	if (IS_ERR(mp))
200484d17192SAl Viro 		return PTR_ERR(mp);
20059d412a43SAl Viro 
200684d17192SAl Viro 	parent = real_mount(path->mnt);
20079d412a43SAl Viro 	err = -EINVAL;
200884d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2009156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2010156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
20119d412a43SAl Viro 			goto unlock;
2012156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
201384d17192SAl Viro 		if (!parent->mnt_ns)
2014156cacb1SAl Viro 			goto unlock;
2015156cacb1SAl Viro 	}
20169d412a43SAl Viro 
20179d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
20189d412a43SAl Viro 	err = -EBUSY;
201995bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
20209d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
20219d412a43SAl Viro 		goto unlock;
20229d412a43SAl Viro 
20239d412a43SAl Viro 	err = -EINVAL;
202495bc5f25SAl Viro 	if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
20259d412a43SAl Viro 		goto unlock;
20269d412a43SAl Viro 
202795bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
202884d17192SAl Viro 	err = graft_tree(newmnt, parent, mp);
20299d412a43SAl Viro 
20309d412a43SAl Viro unlock:
203184d17192SAl Viro 	unlock_mount(mp);
20329d412a43SAl Viro 	return err;
20339d412a43SAl Viro }
2034b1e75df4SAl Viro 
20351da177e4SLinus Torvalds /*
20361da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
20371da177e4SLinus Torvalds  * namespace's tree
20381da177e4SLinus Torvalds  */
20390c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags,
2040808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
20411da177e4SLinus Torvalds {
20420c55cfc4SEric W. Biederman 	struct file_system_type *type;
20439b40bc90SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
20441da177e4SLinus Torvalds 	struct vfsmount *mnt;
204515f9a3f3SAl Viro 	int err;
20461da177e4SLinus Torvalds 
20470c55cfc4SEric W. Biederman 	if (!fstype)
20481da177e4SLinus Torvalds 		return -EINVAL;
20491da177e4SLinus Torvalds 
20500c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
20510c55cfc4SEric W. Biederman 	if (!type)
20520c55cfc4SEric W. Biederman 		return -ENODEV;
20530c55cfc4SEric W. Biederman 
20540c55cfc4SEric W. Biederman 	if (user_ns != &init_user_ns) {
20550c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_MOUNT)) {
20560c55cfc4SEric W. Biederman 			put_filesystem(type);
20570c55cfc4SEric W. Biederman 			return -EPERM;
20580c55cfc4SEric W. Biederman 		}
20590c55cfc4SEric W. Biederman 		/* Only in special cases allow devices from mounts
20600c55cfc4SEric W. Biederman 		 * created outside the initial user namespace.
20610c55cfc4SEric W. Biederman 		 */
20620c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
20630c55cfc4SEric W. Biederman 			flags |= MS_NODEV;
20640c55cfc4SEric W. Biederman 			mnt_flags |= MNT_NODEV;
20650c55cfc4SEric W. Biederman 		}
20660c55cfc4SEric W. Biederman 	}
20670c55cfc4SEric W. Biederman 
20680c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, flags, name, data);
20690c55cfc4SEric W. Biederman 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
20700c55cfc4SEric W. Biederman 	    !mnt->mnt_sb->s_subtype)
20710c55cfc4SEric W. Biederman 		mnt = fs_set_subtype(mnt, fstype);
20720c55cfc4SEric W. Biederman 
20730c55cfc4SEric W. Biederman 	put_filesystem(type);
20741da177e4SLinus Torvalds 	if (IS_ERR(mnt))
20751da177e4SLinus Torvalds 		return PTR_ERR(mnt);
20761da177e4SLinus Torvalds 
207795bc5f25SAl Viro 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
207815f9a3f3SAl Viro 	if (err)
207915f9a3f3SAl Viro 		mntput(mnt);
208015f9a3f3SAl Viro 	return err;
20811da177e4SLinus Torvalds }
20821da177e4SLinus Torvalds 
208319a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
208419a167afSAl Viro {
20856776db3dSAl Viro 	struct mount *mnt = real_mount(m);
208619a167afSAl Viro 	int err;
208719a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
208819a167afSAl Viro 	 * expired before we get a chance to add it
208919a167afSAl Viro 	 */
20906776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
209119a167afSAl Viro 
209219a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
209319a167afSAl Viro 	    m->mnt_root == path->dentry) {
2094b1e75df4SAl Viro 		err = -ELOOP;
2095b1e75df4SAl Viro 		goto fail;
209619a167afSAl Viro 	}
209719a167afSAl Viro 
209895bc5f25SAl Viro 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2099b1e75df4SAl Viro 	if (!err)
2100b1e75df4SAl Viro 		return 0;
2101b1e75df4SAl Viro fail:
2102b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
21036776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
210497216be0SAl Viro 		namespace_lock();
21056776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
210697216be0SAl Viro 		namespace_unlock();
210719a167afSAl Viro 	}
2108b1e75df4SAl Viro 	mntput(m);
2109b1e75df4SAl Viro 	mntput(m);
211019a167afSAl Viro 	return err;
211119a167afSAl Viro }
211219a167afSAl Viro 
2113ea5b778aSDavid Howells /**
2114ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2115ea5b778aSDavid Howells  * @mnt: The mount to list.
2116ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2117ea5b778aSDavid Howells  */
2118ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2119ea5b778aSDavid Howells {
212097216be0SAl Viro 	namespace_lock();
2121ea5b778aSDavid Howells 
21226776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2123ea5b778aSDavid Howells 
212497216be0SAl Viro 	namespace_unlock();
2125ea5b778aSDavid Howells }
2126ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2127ea5b778aSDavid Howells 
2128ea5b778aSDavid Howells /*
21291da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
21301da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
21311da177e4SLinus Torvalds  * here
21321da177e4SLinus Torvalds  */
21331da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
21341da177e4SLinus Torvalds {
2135761d5c38SAl Viro 	struct mount *mnt, *next;
21361da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	if (list_empty(mounts))
21391da177e4SLinus Torvalds 		return;
21401da177e4SLinus Torvalds 
214197216be0SAl Viro 	namespace_lock();
2142719ea2fbSAl Viro 	lock_mount_hash();
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
21451da177e4SLinus Torvalds 	 * following criteria:
21461da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
21471da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
21481da177e4SLinus Torvalds 	 *   cleared by mntput())
21491da177e4SLinus Torvalds 	 */
21506776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2151863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
21521ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
21531da177e4SLinus Torvalds 			continue;
21546776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
21551da177e4SLinus Torvalds 	}
2156bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
21576776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2158143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2159328e6d90SAl Viro 		umount_tree(mnt, 1);
2160bcc5c7d2SAl Viro 	}
2161719ea2fbSAl Viro 	unlock_mount_hash();
21623ab6abeeSAl Viro 	namespace_unlock();
21631da177e4SLinus Torvalds }
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds /*
21685528f911STrond Myklebust  * Ripoff of 'select_parent()'
21695528f911STrond Myklebust  *
21705528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
21715528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
21725528f911STrond Myklebust  */
2173692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
21745528f911STrond Myklebust {
2175692afc31SAl Viro 	struct mount *this_parent = parent;
21765528f911STrond Myklebust 	struct list_head *next;
21775528f911STrond Myklebust 	int found = 0;
21785528f911STrond Myklebust 
21795528f911STrond Myklebust repeat:
21806b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
21815528f911STrond Myklebust resume:
21826b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
21835528f911STrond Myklebust 		struct list_head *tmp = next;
21846b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
21855528f911STrond Myklebust 
21865528f911STrond Myklebust 		next = tmp->next;
2187692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
21885528f911STrond Myklebust 			continue;
21895528f911STrond Myklebust 		/*
21905528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
21915528f911STrond Myklebust 		 */
21926b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
21935528f911STrond Myklebust 			this_parent = mnt;
21945528f911STrond Myklebust 			goto repeat;
21955528f911STrond Myklebust 		}
21965528f911STrond Myklebust 
21971ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
21986776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
21995528f911STrond Myklebust 			found++;
22005528f911STrond Myklebust 		}
22015528f911STrond Myklebust 	}
22025528f911STrond Myklebust 	/*
22035528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
22045528f911STrond Myklebust 	 */
22055528f911STrond Myklebust 	if (this_parent != parent) {
22066b41d536SAl Viro 		next = this_parent->mnt_child.next;
22070714a533SAl Viro 		this_parent = this_parent->mnt_parent;
22085528f911STrond Myklebust 		goto resume;
22095528f911STrond Myklebust 	}
22105528f911STrond Myklebust 	return found;
22115528f911STrond Myklebust }
22125528f911STrond Myklebust 
22135528f911STrond Myklebust /*
22145528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
22155528f911STrond Myklebust  * submounts of a specific parent mountpoint
221699b7db7bSNick Piggin  *
221748a066e7SAl Viro  * mount_lock must be held for write
22185528f911STrond Myklebust  */
2219b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
22205528f911STrond Myklebust {
22215528f911STrond Myklebust 	LIST_HEAD(graveyard);
2222761d5c38SAl Viro 	struct mount *m;
22235528f911STrond Myklebust 
22245528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2225c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2226bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2227761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
22286776db3dSAl Viro 						mnt_expire);
2229143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
2230328e6d90SAl Viro 			umount_tree(m, 1);
2231bcc5c7d2SAl Viro 		}
2232bcc5c7d2SAl Viro 	}
22335528f911STrond Myklebust }
22345528f911STrond Myklebust 
22355528f911STrond Myklebust /*
22361da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
22371da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
22381da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
22391da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
22401da177e4SLinus Torvalds  */
2241b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2242b58fed8bSRam Pai 				 unsigned long n)
22431da177e4SLinus Torvalds {
22441da177e4SLinus Torvalds 	char *t = to;
22451da177e4SLinus Torvalds 	const char __user *f = from;
22461da177e4SLinus Torvalds 	char c;
22471da177e4SLinus Torvalds 
22481da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
22491da177e4SLinus Torvalds 		return n;
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds 	while (n) {
22521da177e4SLinus Torvalds 		if (__get_user(c, f)) {
22531da177e4SLinus Torvalds 			memset(t, 0, n);
22541da177e4SLinus Torvalds 			break;
22551da177e4SLinus Torvalds 		}
22561da177e4SLinus Torvalds 		*t++ = c;
22571da177e4SLinus Torvalds 		f++;
22581da177e4SLinus Torvalds 		n--;
22591da177e4SLinus Torvalds 	}
22601da177e4SLinus Torvalds 	return n;
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
22641da177e4SLinus Torvalds {
22651da177e4SLinus Torvalds 	int i;
22661da177e4SLinus Torvalds 	unsigned long page;
22671da177e4SLinus Torvalds 	unsigned long size;
22681da177e4SLinus Torvalds 
22691da177e4SLinus Torvalds 	*where = 0;
22701da177e4SLinus Torvalds 	if (!data)
22711da177e4SLinus Torvalds 		return 0;
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
22741da177e4SLinus Torvalds 		return -ENOMEM;
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
22771da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
22781da177e4SLinus Torvalds 	 * the remainder of the page.
22791da177e4SLinus Torvalds 	 */
22801da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
22811da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
22821da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
22831da177e4SLinus Torvalds 		size = PAGE_SIZE;
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
22861da177e4SLinus Torvalds 	if (!i) {
22871da177e4SLinus Torvalds 		free_page(page);
22881da177e4SLinus Torvalds 		return -EFAULT;
22891da177e4SLinus Torvalds 	}
22901da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
22911da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
22921da177e4SLinus Torvalds 	*where = page;
22931da177e4SLinus Torvalds 	return 0;
22941da177e4SLinus Torvalds }
22951da177e4SLinus Torvalds 
2296eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where)
2297eca6f534SVegard Nossum {
2298eca6f534SVegard Nossum 	char *tmp;
2299eca6f534SVegard Nossum 
2300eca6f534SVegard Nossum 	if (!data) {
2301eca6f534SVegard Nossum 		*where = NULL;
2302eca6f534SVegard Nossum 		return 0;
2303eca6f534SVegard Nossum 	}
2304eca6f534SVegard Nossum 
2305eca6f534SVegard Nossum 	tmp = strndup_user(data, PAGE_SIZE);
2306eca6f534SVegard Nossum 	if (IS_ERR(tmp))
2307eca6f534SVegard Nossum 		return PTR_ERR(tmp);
2308eca6f534SVegard Nossum 
2309eca6f534SVegard Nossum 	*where = tmp;
2310eca6f534SVegard Nossum 	return 0;
2311eca6f534SVegard Nossum }
2312eca6f534SVegard Nossum 
23131da177e4SLinus Torvalds /*
23141da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
23151da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
23161da177e4SLinus Torvalds  *
23171da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
23181da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
23191da177e4SLinus Torvalds  * information (or be NULL).
23201da177e4SLinus Torvalds  *
23211da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
23221da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
23231da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
23241da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
23251da177e4SLinus Torvalds  * and must be discarded.
23261da177e4SLinus Torvalds  */
2327808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name,
2328808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
23291da177e4SLinus Torvalds {
23302d92ab3cSAl Viro 	struct path path;
23311da177e4SLinus Torvalds 	int retval = 0;
23321da177e4SLinus Torvalds 	int mnt_flags = 0;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	/* Discard magic */
23351da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
23361da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 	/* Basic sanity checks */
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
23411da177e4SLinus Torvalds 		return -EINVAL;
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds 	if (data_page)
23441da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
23451da177e4SLinus Torvalds 
2346a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
2347a27ab9f2STetsuo Handa 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2348a27ab9f2STetsuo Handa 	if (retval)
2349a27ab9f2STetsuo Handa 		return retval;
2350a27ab9f2STetsuo Handa 
2351a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2352a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
23530d5cadb8SAl Viro 	if (!retval && !may_mount())
23540d5cadb8SAl Viro 		retval = -EPERM;
2355a27ab9f2STetsuo Handa 	if (retval)
2356a27ab9f2STetsuo Handa 		goto dput_out;
2357a27ab9f2STetsuo Handa 
2358613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2359613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
23600a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
23610a1c01c9SMatthew Garrett 
23621da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
23631da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
23641da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
23651da177e4SLinus Torvalds 	if (flags & MS_NODEV)
23661da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
23671da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
23681da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2369fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2370fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2371fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2372fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2373d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2374d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
23752e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
23762e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2377fc33a7bbSChristoph Hellwig 
23787a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2379d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2380d0adde57SMatthew Garrett 		   MS_STRICTATIME);
23811da177e4SLinus Torvalds 
23821da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
23832d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
23841da177e4SLinus Torvalds 				    data_page);
23851da177e4SLinus Torvalds 	else if (flags & MS_BIND)
23862d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
23879676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
23882d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
23891da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
23902d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
23911da177e4SLinus Torvalds 	else
23922d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
23931da177e4SLinus Torvalds 				      dev_name, data_page);
23941da177e4SLinus Torvalds dput_out:
23952d92ab3cSAl Viro 	path_put(&path);
23961da177e4SLinus Torvalds 	return retval;
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds 
2399771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
2400771b1371SEric W. Biederman {
240198f842e6SEric W. Biederman 	proc_free_inum(ns->proc_inum);
2402771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
2403771b1371SEric W. Biederman 	kfree(ns);
2404771b1371SEric W. Biederman }
2405771b1371SEric W. Biederman 
24068823c079SEric W. Biederman /*
24078823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
24088823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
24098823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
24108823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
24118823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
24128823c079SEric W. Biederman  */
24138823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
24148823c079SEric W. Biederman 
2415771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2416cf8d2c11STrond Myklebust {
2417cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
241898f842e6SEric W. Biederman 	int ret;
2419cf8d2c11STrond Myklebust 
2420cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2421cf8d2c11STrond Myklebust 	if (!new_ns)
2422cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
242398f842e6SEric W. Biederman 	ret = proc_alloc_inum(&new_ns->proc_inum);
242498f842e6SEric W. Biederman 	if (ret) {
242598f842e6SEric W. Biederman 		kfree(new_ns);
242698f842e6SEric W. Biederman 		return ERR_PTR(ret);
242798f842e6SEric W. Biederman 	}
24288823c079SEric W. Biederman 	new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2429cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2430cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2431cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2432cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2433cf8d2c11STrond Myklebust 	new_ns->event = 0;
2434771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
2435cf8d2c11STrond Myklebust 	return new_ns;
2436cf8d2c11STrond Myklebust }
2437cf8d2c11STrond Myklebust 
24389559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
24399559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
24401da177e4SLinus Torvalds {
24416b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
24427f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2443315fc83eSAl Viro 	struct mount *p, *q;
24449559f689SAl Viro 	struct mount *old;
2445cb338d06SAl Viro 	struct mount *new;
24467a472ef4SEric W. Biederman 	int copy_flags;
24471da177e4SLinus Torvalds 
24489559f689SAl Viro 	BUG_ON(!ns);
24499559f689SAl Viro 
24509559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
24519559f689SAl Viro 		get_mnt_ns(ns);
24529559f689SAl Viro 		return ns;
24539559f689SAl Viro 	}
24549559f689SAl Viro 
24559559f689SAl Viro 	old = ns->root;
24569559f689SAl Viro 
2457771b1371SEric W. Biederman 	new_ns = alloc_mnt_ns(user_ns);
2458cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2459cf8d2c11STrond Myklebust 		return new_ns;
24601da177e4SLinus Torvalds 
246197216be0SAl Viro 	namespace_lock();
24621da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
24634ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
24649559f689SAl Viro 	if (user_ns != ns->user_ns)
2465132c94e3SEric W. Biederman 		copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
24667a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2467be34d1a3SDavid Howells 	if (IS_ERR(new)) {
2468328e6d90SAl Viro 		namespace_unlock();
2469771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
2470be34d1a3SDavid Howells 		return ERR_CAST(new);
24711da177e4SLinus Torvalds 	}
2472be08d6d2SAl Viro 	new_ns->root = new;
24731a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	/*
24761da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
24771da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
24781da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
24791da177e4SLinus Torvalds 	 */
2480909b0a88SAl Viro 	p = old;
2481cb338d06SAl Viro 	q = new;
24821da177e4SLinus Torvalds 	while (p) {
2483143c8c91SAl Viro 		q->mnt_ns = new_ns;
24849559f689SAl Viro 		if (new_fs) {
24859559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
24869559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
2487315fc83eSAl Viro 				rootmnt = &p->mnt;
24881da177e4SLinus Torvalds 			}
24899559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
24909559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
2491315fc83eSAl Viro 				pwdmnt = &p->mnt;
24921da177e4SLinus Torvalds 			}
24931da177e4SLinus Torvalds 		}
2494909b0a88SAl Viro 		p = next_mnt(p, old);
2495909b0a88SAl Viro 		q = next_mnt(q, new);
24964ce5d2b1SEric W. Biederman 		if (!q)
24974ce5d2b1SEric W. Biederman 			break;
24984ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
24994ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
25001da177e4SLinus Torvalds 	}
2501328e6d90SAl Viro 	namespace_unlock();
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds 	if (rootmnt)
2504f03c6599SAl Viro 		mntput(rootmnt);
25051da177e4SLinus Torvalds 	if (pwdmnt)
2506f03c6599SAl Viro 		mntput(pwdmnt);
25071da177e4SLinus Torvalds 
2508741a2951SJANAK DESAI 	return new_ns;
2509741a2951SJANAK DESAI }
2510741a2951SJANAK DESAI 
2511cf8d2c11STrond Myklebust /**
2512cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2513cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2514cf8d2c11STrond Myklebust  */
25151a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2516cf8d2c11STrond Myklebust {
2517771b1371SEric W. Biederman 	struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2518cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
25191a4eeaf2SAl Viro 		struct mount *mnt = real_mount(m);
25201a4eeaf2SAl Viro 		mnt->mnt_ns = new_ns;
2521be08d6d2SAl Viro 		new_ns->root = mnt;
2522b1983cd8SAl Viro 		list_add(&mnt->mnt_list, &new_ns->list);
2523c1334495SAl Viro 	} else {
25241a4eeaf2SAl Viro 		mntput(m);
2525cf8d2c11STrond Myklebust 	}
2526cf8d2c11STrond Myklebust 	return new_ns;
2527cf8d2c11STrond Myklebust }
2528cf8d2c11STrond Myklebust 
2529ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2530ea441d11SAl Viro {
2531ea441d11SAl Viro 	struct mnt_namespace *ns;
2532d31da0f0SAl Viro 	struct super_block *s;
2533ea441d11SAl Viro 	struct path path;
2534ea441d11SAl Viro 	int err;
2535ea441d11SAl Viro 
2536ea441d11SAl Viro 	ns = create_mnt_ns(mnt);
2537ea441d11SAl Viro 	if (IS_ERR(ns))
2538ea441d11SAl Viro 		return ERR_CAST(ns);
2539ea441d11SAl Viro 
2540ea441d11SAl Viro 	err = vfs_path_lookup(mnt->mnt_root, mnt,
2541ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2542ea441d11SAl Viro 
2543ea441d11SAl Viro 	put_mnt_ns(ns);
2544ea441d11SAl Viro 
2545ea441d11SAl Viro 	if (err)
2546ea441d11SAl Viro 		return ERR_PTR(err);
2547ea441d11SAl Viro 
2548ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
2549d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
2550d31da0f0SAl Viro 	atomic_inc(&s->s_active);
2551ea441d11SAl Viro 	mntput(path.mnt);
2552ea441d11SAl Viro 	/* lock the sucker */
2553d31da0f0SAl Viro 	down_write(&s->s_umount);
2554ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
2555ea441d11SAl Viro 	return path.dentry;
2556ea441d11SAl Viro }
2557ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
2558ea441d11SAl Viro 
2559bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2560bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
25611da177e4SLinus Torvalds {
2562eca6f534SVegard Nossum 	int ret;
2563eca6f534SVegard Nossum 	char *kernel_type;
256491a27b2aSJeff Layton 	struct filename *kernel_dir;
2565eca6f534SVegard Nossum 	char *kernel_dev;
25661da177e4SLinus Torvalds 	unsigned long data_page;
25671da177e4SLinus Torvalds 
2568eca6f534SVegard Nossum 	ret = copy_mount_string(type, &kernel_type);
2569eca6f534SVegard Nossum 	if (ret < 0)
2570eca6f534SVegard Nossum 		goto out_type;
25711da177e4SLinus Torvalds 
2572eca6f534SVegard Nossum 	kernel_dir = getname(dir_name);
2573eca6f534SVegard Nossum 	if (IS_ERR(kernel_dir)) {
2574eca6f534SVegard Nossum 		ret = PTR_ERR(kernel_dir);
2575eca6f534SVegard Nossum 		goto out_dir;
2576eca6f534SVegard Nossum 	}
25771da177e4SLinus Torvalds 
2578eca6f534SVegard Nossum 	ret = copy_mount_string(dev_name, &kernel_dev);
2579eca6f534SVegard Nossum 	if (ret < 0)
2580eca6f534SVegard Nossum 		goto out_dev;
25811da177e4SLinus Torvalds 
2582eca6f534SVegard Nossum 	ret = copy_mount_options(data, &data_page);
2583eca6f534SVegard Nossum 	if (ret < 0)
2584eca6f534SVegard Nossum 		goto out_data;
25851da177e4SLinus Torvalds 
258691a27b2aSJeff Layton 	ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags,
2587eca6f534SVegard Nossum 		(void *) data_page);
2588eca6f534SVegard Nossum 
25891da177e4SLinus Torvalds 	free_page(data_page);
2590eca6f534SVegard Nossum out_data:
2591eca6f534SVegard Nossum 	kfree(kernel_dev);
2592eca6f534SVegard Nossum out_dev:
2593eca6f534SVegard Nossum 	putname(kernel_dir);
2594eca6f534SVegard Nossum out_dir:
2595eca6f534SVegard Nossum 	kfree(kernel_type);
2596eca6f534SVegard Nossum out_type:
2597eca6f534SVegard Nossum 	return ret;
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds /*
2601afac7cbaSAl Viro  * Return true if path is reachable from root
2602afac7cbaSAl Viro  *
260348a066e7SAl Viro  * namespace_sem or mount_lock is held
2604afac7cbaSAl Viro  */
2605643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2606afac7cbaSAl Viro 			 const struct path *root)
2607afac7cbaSAl Viro {
2608643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2609a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
26100714a533SAl Viro 		mnt = mnt->mnt_parent;
2611afac7cbaSAl Viro 	}
2612643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2613afac7cbaSAl Viro }
2614afac7cbaSAl Viro 
2615afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2)
2616afac7cbaSAl Viro {
2617afac7cbaSAl Viro 	int res;
261848a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
2619643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
262048a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
2621afac7cbaSAl Viro 	return res;
2622afac7cbaSAl Viro }
2623afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
2624afac7cbaSAl Viro 
2625afac7cbaSAl Viro /*
26261da177e4SLinus Torvalds  * pivot_root Semantics:
26271da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
26281da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
26291da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
26301da177e4SLinus Torvalds  *
26311da177e4SLinus Torvalds  * Restrictions:
26321da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
26331da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
26341da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
26351da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
26361da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
26371da177e4SLinus Torvalds  *
26384a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
26394a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
26404a0d11faSNeil Brown  * in this situation.
26414a0d11faSNeil Brown  *
26421da177e4SLinus Torvalds  * Notes:
26431da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
26441da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
26451da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
26461da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
26471da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
26481da177e4SLinus Torvalds  *    first.
26491da177e4SLinus Torvalds  */
26503480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
26513480b257SHeiko Carstens 		const char __user *, put_old)
26521da177e4SLinus Torvalds {
26532d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
265484d17192SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt;
265584d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
26561da177e4SLinus Torvalds 	int error;
26571da177e4SLinus Torvalds 
26589b40bc90SAl Viro 	if (!may_mount())
26591da177e4SLinus Torvalds 		return -EPERM;
26601da177e4SLinus Torvalds 
26612d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
26621da177e4SLinus Torvalds 	if (error)
26631da177e4SLinus Torvalds 		goto out0;
26641da177e4SLinus Torvalds 
26652d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
26661da177e4SLinus Torvalds 	if (error)
26671da177e4SLinus Torvalds 		goto out1;
26681da177e4SLinus Torvalds 
26692d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
2670b12cea91SAl Viro 	if (error)
2671b12cea91SAl Viro 		goto out2;
26721da177e4SLinus Torvalds 
2673f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
267484d17192SAl Viro 	old_mp = lock_mount(&old);
267584d17192SAl Viro 	error = PTR_ERR(old_mp);
267684d17192SAl Viro 	if (IS_ERR(old_mp))
2677b12cea91SAl Viro 		goto out3;
2678b12cea91SAl Viro 
26791da177e4SLinus Torvalds 	error = -EINVAL;
2680419148daSAl Viro 	new_mnt = real_mount(new.mnt);
2681419148daSAl Viro 	root_mnt = real_mount(root.mnt);
268284d17192SAl Viro 	old_mnt = real_mount(old.mnt);
268384d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
2684fc7be130SAl Viro 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
2685fc7be130SAl Viro 		IS_MNT_SHARED(root_mnt->mnt_parent))
2686b12cea91SAl Viro 		goto out4;
2687143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2688b12cea91SAl Viro 		goto out4;
26895ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
26905ff9d8a6SEric W. Biederman 		goto out4;
26911da177e4SLinus Torvalds 	error = -ENOENT;
2692f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
2693b12cea91SAl Viro 		goto out4;
26941da177e4SLinus Torvalds 	error = -EBUSY;
269584d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
2696b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
26971da177e4SLinus Torvalds 	error = -EINVAL;
26988c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
2699b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2700676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
2701b12cea91SAl Viro 		goto out4; /* not attached */
270284d17192SAl Viro 	root_mp = root_mnt->mnt_mp;
27032d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
2704b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2705676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
2706b12cea91SAl Viro 		goto out4; /* not attached */
27074ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
270884d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
2709b12cea91SAl Viro 		goto out4;
271084d17192SAl Viro 	root_mp->m_count++; /* pin it so it won't go away */
2711719ea2fbSAl Viro 	lock_mount_hash();
2712419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
2713419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
27145ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
27155ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
27165ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
27175ff9d8a6SEric W. Biederman 	}
27184ac91378SJan Blunck 	/* mount old root on put_old */
271984d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
27204ac91378SJan Blunck 	/* mount new_root on / */
272184d17192SAl Viro 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
27226b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
2723719ea2fbSAl Viro 	unlock_mount_hash();
27242d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
272584d17192SAl Viro 	put_mountpoint(root_mp);
27261da177e4SLinus Torvalds 	error = 0;
2727b12cea91SAl Viro out4:
272884d17192SAl Viro 	unlock_mount(old_mp);
2729b12cea91SAl Viro 	if (!error) {
27301a390689SAl Viro 		path_put(&root_parent);
27311a390689SAl Viro 		path_put(&parent_path);
2732b12cea91SAl Viro 	}
2733b12cea91SAl Viro out3:
27348c3ee42eSAl Viro 	path_put(&root);
2735b12cea91SAl Viro out2:
27362d8f3038SAl Viro 	path_put(&old);
27371da177e4SLinus Torvalds out1:
27382d8f3038SAl Viro 	path_put(&new);
27391da177e4SLinus Torvalds out0:
27401da177e4SLinus Torvalds 	return error;
27411da177e4SLinus Torvalds }
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds static void __init init_mount_tree(void)
27441da177e4SLinus Torvalds {
27451da177e4SLinus Torvalds 	struct vfsmount *mnt;
27466b3286edSKirill Korotaev 	struct mnt_namespace *ns;
2747ac748a09SJan Blunck 	struct path root;
27480c55cfc4SEric W. Biederman 	struct file_system_type *type;
27491da177e4SLinus Torvalds 
27500c55cfc4SEric W. Biederman 	type = get_fs_type("rootfs");
27510c55cfc4SEric W. Biederman 	if (!type)
27520c55cfc4SEric W. Biederman 		panic("Can't find rootfs type");
27530c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
27540c55cfc4SEric W. Biederman 	put_filesystem(type);
27551da177e4SLinus Torvalds 	if (IS_ERR(mnt))
27561da177e4SLinus Torvalds 		panic("Can't create rootfs");
2757b3e19d92SNick Piggin 
27583b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
27593b22edc5STrond Myklebust 	if (IS_ERR(ns))
27601da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
27611da177e4SLinus Torvalds 
27626b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
27636b3286edSKirill Korotaev 	get_mnt_ns(ns);
27641da177e4SLinus Torvalds 
2765be08d6d2SAl Viro 	root.mnt = mnt;
2766be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
2767ac748a09SJan Blunck 
2768ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
2769ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
27701da177e4SLinus Torvalds }
27711da177e4SLinus Torvalds 
277274bf17cfSDenis Cheng void __init mnt_init(void)
27731da177e4SLinus Torvalds {
277413f14b4dSEric Dumazet 	unsigned u;
277515a67dd8SRandy Dunlap 	int err;
27761da177e4SLinus Torvalds 
27777d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
277820c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
27791da177e4SLinus Torvalds 
2780b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
278184d17192SAl Viro 	mountpoint_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
27821da177e4SLinus Torvalds 
278384d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
27841da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
27851da177e4SLinus Torvalds 
278680cdc6daSMandeep Singh Baines 	printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
27871da177e4SLinus Torvalds 
278813f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
278913f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
279084d17192SAl Viro 	for (u = 0; u < HASH_SIZE; u++)
279184d17192SAl Viro 		INIT_LIST_HEAD(&mountpoint_hashtable[u]);
27921da177e4SLinus Torvalds 
27934b93dc9bSTejun Heo 	kernfs_init();
27944b93dc9bSTejun Heo 
279515a67dd8SRandy Dunlap 	err = sysfs_init();
279615a67dd8SRandy Dunlap 	if (err)
279715a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
27988e24eea7SHarvey Harrison 			__func__, err);
279900d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
280000d26666SGreg Kroah-Hartman 	if (!fs_kobj)
28018e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
28021da177e4SLinus Torvalds 	init_rootfs();
28031da177e4SLinus Torvalds 	init_mount_tree();
28041da177e4SLinus Torvalds }
28051da177e4SLinus Torvalds 
2806616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
28071da177e4SLinus Torvalds {
2808d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
2809616511d0STrond Myklebust 		return;
28107b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
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 		real_mount(mnt)->mnt_ns = NULL;
283448a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
2835423e0ab0STim Chen 		mntput(mnt);
2836423e0ab0STim Chen 	}
2837423e0ab0STim Chen }
2838423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
283902125a82SAl Viro 
284002125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
284102125a82SAl Viro {
2842143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
284302125a82SAl Viro }
28448823c079SEric W. Biederman 
28453151527eSEric W. Biederman bool current_chrooted(void)
28463151527eSEric W. Biederman {
28473151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
28483151527eSEric W. Biederman 	struct path ns_root;
28493151527eSEric W. Biederman 	struct path fs_root;
28503151527eSEric W. Biederman 	bool chrooted;
28513151527eSEric W. Biederman 
28523151527eSEric W. Biederman 	/* Find the namespace root */
28533151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
28543151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
28553151527eSEric W. Biederman 	path_get(&ns_root);
28563151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
28573151527eSEric W. Biederman 		;
28583151527eSEric W. Biederman 
28593151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
28603151527eSEric W. Biederman 
28613151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
28623151527eSEric W. Biederman 
28633151527eSEric W. Biederman 	path_put(&fs_root);
28643151527eSEric W. Biederman 	path_put(&ns_root);
28653151527eSEric W. Biederman 
28663151527eSEric W. Biederman 	return chrooted;
28673151527eSEric W. Biederman }
28683151527eSEric W. Biederman 
2869e51db735SEric W. Biederman bool fs_fully_visible(struct file_system_type *type)
287087a8ebd6SEric W. Biederman {
287187a8ebd6SEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
287287a8ebd6SEric W. Biederman 	struct mount *mnt;
2873e51db735SEric W. Biederman 	bool visible = false;
287487a8ebd6SEric W. Biederman 
2875e51db735SEric W. Biederman 	if (unlikely(!ns))
2876e51db735SEric W. Biederman 		return false;
2877e51db735SEric W. Biederman 
287844bb4385SAl Viro 	down_read(&namespace_sem);
287987a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
2880e51db735SEric W. Biederman 		struct mount *child;
2881e51db735SEric W. Biederman 		if (mnt->mnt.mnt_sb->s_type != type)
2882e51db735SEric W. Biederman 			continue;
2883e51db735SEric W. Biederman 
2884e51db735SEric W. Biederman 		/* This mount is not fully visible if there are any child mounts
2885e51db735SEric W. Biederman 		 * that cover anything except for empty directories.
2886e51db735SEric W. Biederman 		 */
2887e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2888e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
2889e51db735SEric W. Biederman 			if (!S_ISDIR(inode->i_mode))
2890e51db735SEric W. Biederman 				goto next;
2891e51db735SEric W. Biederman 			if (inode->i_nlink != 2)
2892e51db735SEric W. Biederman 				goto next;
289387a8ebd6SEric W. Biederman 		}
2894e51db735SEric W. Biederman 		visible = true;
2895e51db735SEric W. Biederman 		goto found;
2896e51db735SEric W. Biederman 	next:	;
289787a8ebd6SEric W. Biederman 	}
2898e51db735SEric W. Biederman found:
289944bb4385SAl Viro 	up_read(&namespace_sem);
2900e51db735SEric W. Biederman 	return visible;
290187a8ebd6SEric W. Biederman }
290287a8ebd6SEric W. Biederman 
29038823c079SEric W. Biederman static void *mntns_get(struct task_struct *task)
29048823c079SEric W. Biederman {
29058823c079SEric W. Biederman 	struct mnt_namespace *ns = NULL;
29068823c079SEric W. Biederman 	struct nsproxy *nsproxy;
29078823c079SEric W. Biederman 
29088823c079SEric W. Biederman 	rcu_read_lock();
29098823c079SEric W. Biederman 	nsproxy = task_nsproxy(task);
29108823c079SEric W. Biederman 	if (nsproxy) {
29118823c079SEric W. Biederman 		ns = nsproxy->mnt_ns;
29128823c079SEric W. Biederman 		get_mnt_ns(ns);
29138823c079SEric W. Biederman 	}
29148823c079SEric W. Biederman 	rcu_read_unlock();
29158823c079SEric W. Biederman 
29168823c079SEric W. Biederman 	return ns;
29178823c079SEric W. Biederman }
29188823c079SEric W. Biederman 
29198823c079SEric W. Biederman static void mntns_put(void *ns)
29208823c079SEric W. Biederman {
29218823c079SEric W. Biederman 	put_mnt_ns(ns);
29228823c079SEric W. Biederman }
29238823c079SEric W. Biederman 
29248823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns)
29258823c079SEric W. Biederman {
29268823c079SEric W. Biederman 	struct fs_struct *fs = current->fs;
29278823c079SEric W. Biederman 	struct mnt_namespace *mnt_ns = ns;
29288823c079SEric W. Biederman 	struct path root;
29298823c079SEric W. Biederman 
29300c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
2931c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
2932c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
2933ae11e0f1SZhao Hongjiang 		return -EPERM;
29348823c079SEric W. Biederman 
29358823c079SEric W. Biederman 	if (fs->users != 1)
29368823c079SEric W. Biederman 		return -EINVAL;
29378823c079SEric W. Biederman 
29388823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
29398823c079SEric W. Biederman 	put_mnt_ns(nsproxy->mnt_ns);
29408823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
29418823c079SEric W. Biederman 
29428823c079SEric W. Biederman 	/* Find the root */
29438823c079SEric W. Biederman 	root.mnt    = &mnt_ns->root->mnt;
29448823c079SEric W. Biederman 	root.dentry = mnt_ns->root->mnt.mnt_root;
29458823c079SEric W. Biederman 	path_get(&root);
29468823c079SEric W. Biederman 	while(d_mountpoint(root.dentry) && follow_down_one(&root))
29478823c079SEric W. Biederman 		;
29488823c079SEric W. Biederman 
29498823c079SEric W. Biederman 	/* Update the pwd and root */
29508823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
29518823c079SEric W. Biederman 	set_fs_root(fs, &root);
29528823c079SEric W. Biederman 
29538823c079SEric W. Biederman 	path_put(&root);
29548823c079SEric W. Biederman 	return 0;
29558823c079SEric W. Biederman }
29568823c079SEric W. Biederman 
295798f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns)
295898f842e6SEric W. Biederman {
295998f842e6SEric W. Biederman 	struct mnt_namespace *mnt_ns = ns;
296098f842e6SEric W. Biederman 	return mnt_ns->proc_inum;
296198f842e6SEric W. Biederman }
296298f842e6SEric W. Biederman 
29638823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
29648823c079SEric W. Biederman 	.name		= "mnt",
29658823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
29668823c079SEric W. Biederman 	.get		= mntns_get,
29678823c079SEric W. Biederman 	.put		= mntns_put,
29688823c079SEric W. Biederman 	.install	= mntns_install,
296998f842e6SEric W. Biederman 	.inum		= mntns_inum,
29708823c079SEric W. Biederman };
2971