xref: /openbmc/linux/fs/namespace.c (revision 169b480e)
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>
185b825c3aSIngo Molnar #include <linux/cred.h>
1973cd49ecSMiklos Szeredi #include <linux/idr.h>
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>
260818bf27SAl Viro #include <linux/bootmem.h>
279ea459e1SAl Viro #include <linux/task_work.h>
289164bb4aSIngo Molnar #include <linux/sched/task.h>
299164bb4aSIngo Molnar 
3007b20889SRam Pai #include "pnode.h"
31948730b0SAdrian Bunk #include "internal.h"
321da177e4SLinus Torvalds 
33d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */
34d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000;
35d2921684SEric W. Biederman 
360818bf27SAl Viro static unsigned int m_hash_mask __read_mostly;
370818bf27SAl Viro static unsigned int m_hash_shift __read_mostly;
380818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly;
390818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly;
400818bf27SAl Viro 
410818bf27SAl Viro static __initdata unsigned long mhash_entries;
420818bf27SAl Viro static int __init set_mhash_entries(char *str)
430818bf27SAl Viro {
440818bf27SAl Viro 	if (!str)
450818bf27SAl Viro 		return 0;
460818bf27SAl Viro 	mhash_entries = simple_strtoul(str, &str, 0);
470818bf27SAl Viro 	return 1;
480818bf27SAl Viro }
490818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries);
500818bf27SAl Viro 
510818bf27SAl Viro static __initdata unsigned long mphash_entries;
520818bf27SAl Viro static int __init set_mphash_entries(char *str)
530818bf27SAl Viro {
540818bf27SAl Viro 	if (!str)
550818bf27SAl Viro 		return 0;
560818bf27SAl Viro 	mphash_entries = simple_strtoul(str, &str, 0);
570818bf27SAl Viro 	return 1;
580818bf27SAl Viro }
590818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries);
6013f14b4dSEric Dumazet 
61c7999c36SAl Viro static u64 event;
6273cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
63719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
645addc5ddSAl Viro 
6538129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly;
660818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly;
67e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
6859aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem);
691da177e4SLinus Torvalds 
70f87fd4c2SMiklos Szeredi /* /sys/fs */
7100d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
7200d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
73f87fd4c2SMiklos Szeredi 
7499b7db7bSNick Piggin /*
7599b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
7699b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
7799b7db7bSNick Piggin  * up the tree.
7899b7db7bSNick Piggin  *
7999b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
8099b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
8199b7db7bSNick Piggin  */
8248a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
8399b7db7bSNick Piggin 
8438129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
871da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
880818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
890818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
900818bf27SAl Viro }
910818bf27SAl Viro 
920818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
930818bf27SAl Viro {
940818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
950818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
960818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
99b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
10073cd49ecSMiklos Szeredi {
101169b480eSMatthew Wilcox 	int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
10273cd49ecSMiklos Szeredi 
103169b480eSMatthew Wilcox 	if (res < 0)
10473cd49ecSMiklos Szeredi 		return res;
105169b480eSMatthew Wilcox 	mnt->mnt_id = res;
106169b480eSMatthew Wilcox 	return 0;
10773cd49ecSMiklos Szeredi }
10873cd49ecSMiklos Szeredi 
109b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
11073cd49ecSMiklos Szeredi {
111169b480eSMatthew Wilcox 	ida_free(&mnt_id_ida, mnt->mnt_id);
11273cd49ecSMiklos Szeredi }
11373cd49ecSMiklos Szeredi 
114719f5d7fSMiklos Szeredi /*
115719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
116719f5d7fSMiklos Szeredi  */
1174b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
118719f5d7fSMiklos Szeredi {
119169b480eSMatthew Wilcox 	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
120f21f6220SAl Viro 
121169b480eSMatthew Wilcox 	if (res < 0)
122f21f6220SAl Viro 		return res;
123169b480eSMatthew Wilcox 	mnt->mnt_group_id = res;
124169b480eSMatthew Wilcox 	return 0;
125719f5d7fSMiklos Szeredi }
126719f5d7fSMiklos Szeredi 
127719f5d7fSMiklos Szeredi /*
128719f5d7fSMiklos Szeredi  * Release a peer group ID
129719f5d7fSMiklos Szeredi  */
1304b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
131719f5d7fSMiklos Szeredi {
132169b480eSMatthew Wilcox 	ida_free(&mnt_group_ida, mnt->mnt_group_id);
13315169fe7SAl Viro 	mnt->mnt_group_id = 0;
134719f5d7fSMiklos Szeredi }
135719f5d7fSMiklos Szeredi 
136b3e19d92SNick Piggin /*
137b3e19d92SNick Piggin  * vfsmount lock must be held for read
138b3e19d92SNick Piggin  */
13983adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
140b3e19d92SNick Piggin {
141b3e19d92SNick Piggin #ifdef CONFIG_SMP
14268e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
143b3e19d92SNick Piggin #else
144b3e19d92SNick Piggin 	preempt_disable();
14568e8a9feSAl Viro 	mnt->mnt_count += n;
146b3e19d92SNick Piggin 	preempt_enable();
147b3e19d92SNick Piggin #endif
148b3e19d92SNick Piggin }
149b3e19d92SNick Piggin 
150b3e19d92SNick Piggin /*
151b3e19d92SNick Piggin  * vfsmount lock must be held for write
152b3e19d92SNick Piggin  */
15383adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt)
154b3e19d92SNick Piggin {
155b3e19d92SNick Piggin #ifdef CONFIG_SMP
156f03c6599SAl Viro 	unsigned int count = 0;
157b3e19d92SNick Piggin 	int cpu;
158b3e19d92SNick Piggin 
159b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
16068e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
161b3e19d92SNick Piggin 	}
162b3e19d92SNick Piggin 
163b3e19d92SNick Piggin 	return count;
164b3e19d92SNick Piggin #else
16568e8a9feSAl Viro 	return mnt->mnt_count;
166b3e19d92SNick Piggin #endif
167b3e19d92SNick Piggin }
168b3e19d92SNick Piggin 
16987b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p)
17087b95ce0SAl Viro {
17187b95ce0SAl Viro 	struct mount *m = container_of(p, struct mount, mnt_umount);
17287b95ce0SAl Viro 	dput(m->mnt_ex_mountpoint);
17387b95ce0SAl Viro 	pin_remove(p);
17487b95ce0SAl Viro 	mntput(&m->mnt);
17587b95ce0SAl Viro }
17687b95ce0SAl Viro 
177b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
1781da177e4SLinus Torvalds {
179c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
180c63181e6SAl Viro 	if (mnt) {
18173cd49ecSMiklos Szeredi 		int err;
18273cd49ecSMiklos Szeredi 
183c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
18488b38782SLi Zefan 		if (err)
18588b38782SLi Zefan 			goto out_free_cache;
18688b38782SLi Zefan 
18788b38782SLi Zefan 		if (name) {
188fcc139aeSAndrzej Hajda 			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
189c63181e6SAl Viro 			if (!mnt->mnt_devname)
19088b38782SLi Zefan 				goto out_free_id;
19173cd49ecSMiklos Szeredi 		}
19273cd49ecSMiklos Szeredi 
193b3e19d92SNick Piggin #ifdef CONFIG_SMP
194c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
195c63181e6SAl Viro 		if (!mnt->mnt_pcp)
196b3e19d92SNick Piggin 			goto out_free_devname;
197b3e19d92SNick Piggin 
198c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
199b3e19d92SNick Piggin #else
200c63181e6SAl Viro 		mnt->mnt_count = 1;
201c63181e6SAl Viro 		mnt->mnt_writers = 0;
202b3e19d92SNick Piggin #endif
203b3e19d92SNick Piggin 
20438129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
205c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
206c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
207c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
208c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
209c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
210c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
211c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2120a5eb7c8SEric W. Biederman 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
21399b19d16SEric W. Biederman 		INIT_LIST_HEAD(&mnt->mnt_umounting);
21487b95ce0SAl Viro 		init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
2151da177e4SLinus Torvalds 	}
216c63181e6SAl Viro 	return mnt;
21788b38782SLi Zefan 
218d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
219d3ef3d73Snpiggin@suse.de out_free_devname:
220fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
221d3ef3d73Snpiggin@suse.de #endif
22288b38782SLi Zefan out_free_id:
223c63181e6SAl Viro 	mnt_free_id(mnt);
22488b38782SLi Zefan out_free_cache:
225c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
22688b38782SLi Zefan 	return NULL;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2298366025eSDave Hansen /*
2308366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2318366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2328366025eSDave Hansen  * We must keep track of when those operations start
2338366025eSDave Hansen  * (for permission checks) and when they end, so that
2348366025eSDave Hansen  * we can determine when writes are able to occur to
2358366025eSDave Hansen  * a filesystem.
2368366025eSDave Hansen  */
2373d733633SDave Hansen /*
2383d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2393d733633SDave Hansen  * @mnt: the mount to check for its write status
2403d733633SDave Hansen  *
2413d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2423d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2433d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2443d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2453d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2463d733633SDave Hansen  * r/w.
2473d733633SDave Hansen  */
2483d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2493d733633SDave Hansen {
2502e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2512e4b7fcdSDave Hansen 		return 1;
252bc98a42cSDavid Howells 	if (sb_rdonly(mnt->mnt_sb))
2532e4b7fcdSDave Hansen 		return 1;
2542e4b7fcdSDave Hansen 	return 0;
2553d733633SDave Hansen }
2563d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2573d733633SDave Hansen 
25883adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2593d733633SDave Hansen {
260d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
26168e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
262d3ef3d73Snpiggin@suse.de #else
26368e8a9feSAl Viro 	mnt->mnt_writers++;
264d3ef3d73Snpiggin@suse.de #endif
2653d733633SDave Hansen }
2663d733633SDave Hansen 
26783adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2683d733633SDave Hansen {
269d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
27068e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
271d3ef3d73Snpiggin@suse.de #else
27268e8a9feSAl Viro 	mnt->mnt_writers--;
273d3ef3d73Snpiggin@suse.de #endif
274d3ef3d73Snpiggin@suse.de }
275d3ef3d73Snpiggin@suse.de 
27683adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
277d3ef3d73Snpiggin@suse.de {
278d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
279d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2803d733633SDave Hansen 	int cpu;
2813d733633SDave Hansen 
2823d733633SDave Hansen 	for_each_possible_cpu(cpu) {
28368e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
2843d733633SDave Hansen 	}
2853d733633SDave Hansen 
286d3ef3d73Snpiggin@suse.de 	return count;
287d3ef3d73Snpiggin@suse.de #else
288d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
289d3ef3d73Snpiggin@suse.de #endif
2903d733633SDave Hansen }
2913d733633SDave Hansen 
2924ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
2934ed5e82fSMiklos Szeredi {
2944ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
2954ed5e82fSMiklos Szeredi 		return 1;
2964ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
2974ed5e82fSMiklos Szeredi 	smp_rmb();
2984ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
2994ed5e82fSMiklos Szeredi }
3004ed5e82fSMiklos Szeredi 
3013d733633SDave Hansen /*
302eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
303eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
304eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
305eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
3063d733633SDave Hansen  */
3078366025eSDave Hansen /**
308eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
30983adc753SAl Viro  * @m: the mount on which to take a write
3108366025eSDave Hansen  *
311eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
312eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
313eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
314eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
315eb04c282SJan Kara  * called. This is effectively a refcount.
3168366025eSDave Hansen  */
317eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3188366025eSDave Hansen {
31983adc753SAl Viro 	struct mount *mnt = real_mount(m);
3203d733633SDave Hansen 	int ret = 0;
3213d733633SDave Hansen 
322d3ef3d73Snpiggin@suse.de 	preempt_disable();
323c6653a83SNick Piggin 	mnt_inc_writers(mnt);
324d3ef3d73Snpiggin@suse.de 	/*
325c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
326d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
327d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
328d3ef3d73Snpiggin@suse.de 	 */
329d3ef3d73Snpiggin@suse.de 	smp_mb();
3306aa7de05SMark Rutland 	while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
331d3ef3d73Snpiggin@suse.de 		cpu_relax();
332d3ef3d73Snpiggin@suse.de 	/*
333d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
334d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
335d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
336d3ef3d73Snpiggin@suse.de 	 */
337d3ef3d73Snpiggin@suse.de 	smp_rmb();
3384ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
339c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3403d733633SDave Hansen 		ret = -EROFS;
3413d733633SDave Hansen 	}
342d3ef3d73Snpiggin@suse.de 	preempt_enable();
343eb04c282SJan Kara 
344eb04c282SJan Kara 	return ret;
345eb04c282SJan Kara }
346eb04c282SJan Kara 
347eb04c282SJan Kara /**
348eb04c282SJan Kara  * mnt_want_write - get write access to a mount
349eb04c282SJan Kara  * @m: the mount on which to take a write
350eb04c282SJan Kara  *
351eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
352eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
353eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
354eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
355eb04c282SJan Kara  */
356eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
357eb04c282SJan Kara {
358eb04c282SJan Kara 	int ret;
359eb04c282SJan Kara 
360eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
361eb04c282SJan Kara 	ret = __mnt_want_write(m);
362eb04c282SJan Kara 	if (ret)
363eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3643d733633SDave Hansen 	return ret;
3658366025eSDave Hansen }
3668366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3678366025eSDave Hansen 
3688366025eSDave Hansen /**
36996029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
37096029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
37196029c4eSnpiggin@suse.de  *
37296029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
37396029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
37496029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
37596029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
37696029c4eSnpiggin@suse.de  *
37796029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
37896029c4eSnpiggin@suse.de  * drop the reference.
37996029c4eSnpiggin@suse.de  */
38096029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
38196029c4eSnpiggin@suse.de {
38296029c4eSnpiggin@suse.de 	/* superblock may be r/o */
38396029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
38496029c4eSnpiggin@suse.de 		return -EROFS;
38596029c4eSnpiggin@suse.de 	preempt_disable();
38683adc753SAl Viro 	mnt_inc_writers(real_mount(mnt));
38796029c4eSnpiggin@suse.de 	preempt_enable();
38896029c4eSnpiggin@suse.de 	return 0;
38996029c4eSnpiggin@suse.de }
39096029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
39196029c4eSnpiggin@suse.de 
39296029c4eSnpiggin@suse.de /**
393eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
394eb04c282SJan Kara  * @file: the file who's mount on which to take a write
395eb04c282SJan Kara  *
396eb04c282SJan Kara  * This is like __mnt_want_write, but it takes a file and can
397eb04c282SJan Kara  * do some optimisations if the file is open for write already
398eb04c282SJan Kara  */
399eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
400eb04c282SJan Kara {
40183f936c7SAl Viro 	if (!(file->f_mode & FMODE_WRITER))
402eb04c282SJan Kara 		return __mnt_want_write(file->f_path.mnt);
403eb04c282SJan Kara 	else
404eb04c282SJan Kara 		return mnt_clone_write(file->f_path.mnt);
405eb04c282SJan Kara }
406eb04c282SJan Kara 
407eb04c282SJan Kara /**
4087c6893e3SMiklos Szeredi  * mnt_want_write_file_path - get write access to a file's mount
40996029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
41096029c4eSnpiggin@suse.de  *
41196029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
41296029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
4137c6893e3SMiklos Szeredi  *
4147c6893e3SMiklos Szeredi  * Called by the vfs for cases when we have an open file at hand, but will do an
4157c6893e3SMiklos Szeredi  * inode operation on it (important distinction for files opened on overlayfs,
4167c6893e3SMiklos Szeredi  * since the file operations will come from the real underlying file, while
4177c6893e3SMiklos Szeredi  * inode operations come from the overlay).
41896029c4eSnpiggin@suse.de  */
4197c6893e3SMiklos Szeredi int mnt_want_write_file_path(struct file *file)
42096029c4eSnpiggin@suse.de {
421eb04c282SJan Kara 	int ret;
422eb04c282SJan Kara 
423eb04c282SJan Kara 	sb_start_write(file->f_path.mnt->mnt_sb);
424eb04c282SJan Kara 	ret = __mnt_want_write_file(file);
425eb04c282SJan Kara 	if (ret)
426eb04c282SJan Kara 		sb_end_write(file->f_path.mnt->mnt_sb);
427eb04c282SJan Kara 	return ret;
42896029c4eSnpiggin@suse.de }
4297c6893e3SMiklos Szeredi 
4307c6893e3SMiklos Szeredi static inline int may_write_real(struct file *file)
4317c6893e3SMiklos Szeredi {
4327c6893e3SMiklos Szeredi 	struct dentry *dentry = file->f_path.dentry;
4337c6893e3SMiklos Szeredi 	struct dentry *upperdentry;
4347c6893e3SMiklos Szeredi 
4357c6893e3SMiklos Szeredi 	/* Writable file? */
4367c6893e3SMiklos Szeredi 	if (file->f_mode & FMODE_WRITER)
4377c6893e3SMiklos Szeredi 		return 0;
4387c6893e3SMiklos Szeredi 
4397c6893e3SMiklos Szeredi 	/* Not overlayfs? */
4407c6893e3SMiklos Szeredi 	if (likely(!(dentry->d_flags & DCACHE_OP_REAL)))
4417c6893e3SMiklos Szeredi 		return 0;
4427c6893e3SMiklos Szeredi 
4437c6893e3SMiklos Szeredi 	/* File refers to upper, writable layer? */
4447c6893e3SMiklos Szeredi 	upperdentry = d_real(dentry, NULL, 0, D_REAL_UPPER);
445954c736fSAmir Goldstein 	if (upperdentry &&
446954c736fSAmir Goldstein 	    (file_inode(file) == d_inode(upperdentry) ||
447954c736fSAmir Goldstein 	     file_inode(file) == d_inode(dentry)))
4487c6893e3SMiklos Szeredi 		return 0;
4497c6893e3SMiklos Szeredi 
4507c6893e3SMiklos Szeredi 	/* Lower layer: can't write to real file, sorry... */
4517c6893e3SMiklos Szeredi 	return -EPERM;
4527c6893e3SMiklos Szeredi }
4537c6893e3SMiklos Szeredi 
4547c6893e3SMiklos Szeredi /**
4557c6893e3SMiklos Szeredi  * mnt_want_write_file - get write access to a file's mount
4567c6893e3SMiklos Szeredi  * @file: the file who's mount on which to take a write
4577c6893e3SMiklos Szeredi  *
4587c6893e3SMiklos Szeredi  * This is like mnt_want_write, but it takes a file and can
4597c6893e3SMiklos Szeredi  * do some optimisations if the file is open for write already
4607c6893e3SMiklos Szeredi  *
4617c6893e3SMiklos Szeredi  * Mostly called by filesystems from their ioctl operation before performing
4627c6893e3SMiklos Szeredi  * modification.  On overlayfs this needs to check if the file is on a read-only
4637c6893e3SMiklos Szeredi  * lower layer and deny access in that case.
4647c6893e3SMiklos Szeredi  */
4657c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file)
4667c6893e3SMiklos Szeredi {
4677c6893e3SMiklos Szeredi 	int ret;
4687c6893e3SMiklos Szeredi 
4697c6893e3SMiklos Szeredi 	ret = may_write_real(file);
4707c6893e3SMiklos Szeredi 	if (!ret) {
4717c6893e3SMiklos Szeredi 		sb_start_write(file_inode(file)->i_sb);
4727c6893e3SMiklos Szeredi 		ret = __mnt_want_write_file(file);
4737c6893e3SMiklos Szeredi 		if (ret)
4747c6893e3SMiklos Szeredi 			sb_end_write(file_inode(file)->i_sb);
4757c6893e3SMiklos Szeredi 	}
4767c6893e3SMiklos Szeredi 	return ret;
4777c6893e3SMiklos Szeredi }
47896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
47996029c4eSnpiggin@suse.de 
48096029c4eSnpiggin@suse.de /**
481eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4828366025eSDave Hansen  * @mnt: the mount on which to give up write access
4838366025eSDave Hansen  *
4848366025eSDave Hansen  * Tells the low-level filesystem that we are done
4858366025eSDave Hansen  * performing writes to it.  Must be matched with
486eb04c282SJan Kara  * __mnt_want_write() call above.
4878366025eSDave Hansen  */
488eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4898366025eSDave Hansen {
490d3ef3d73Snpiggin@suse.de 	preempt_disable();
49183adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
492d3ef3d73Snpiggin@suse.de 	preempt_enable();
4938366025eSDave Hansen }
494eb04c282SJan Kara 
495eb04c282SJan Kara /**
496eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
497eb04c282SJan Kara  * @mnt: the mount on which to give up write access
498eb04c282SJan Kara  *
499eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
500eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
501eb04c282SJan Kara  * mnt_want_write() call above.
502eb04c282SJan Kara  */
503eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
504eb04c282SJan Kara {
505eb04c282SJan Kara 	__mnt_drop_write(mnt);
506eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
507eb04c282SJan Kara }
5088366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
5098366025eSDave Hansen 
510eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
511eb04c282SJan Kara {
512eb04c282SJan Kara 	__mnt_drop_write(file->f_path.mnt);
513eb04c282SJan Kara }
514eb04c282SJan Kara 
5157c6893e3SMiklos Szeredi void mnt_drop_write_file_path(struct file *file)
5162a79f17eSAl Viro {
5172a79f17eSAl Viro 	mnt_drop_write(file->f_path.mnt);
5182a79f17eSAl Viro }
5197c6893e3SMiklos Szeredi 
5207c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file)
5217c6893e3SMiklos Szeredi {
5227c6893e3SMiklos Szeredi 	__mnt_drop_write(file->f_path.mnt);
5237c6893e3SMiklos Szeredi 	sb_end_write(file_inode(file)->i_sb);
5247c6893e3SMiklos Szeredi }
5252a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
5262a79f17eSAl Viro 
52783adc753SAl Viro static int mnt_make_readonly(struct mount *mnt)
5288366025eSDave Hansen {
5293d733633SDave Hansen 	int ret = 0;
5303d733633SDave Hansen 
531719ea2fbSAl Viro 	lock_mount_hash();
53283adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
533d3ef3d73Snpiggin@suse.de 	/*
534d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
535d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
536d3ef3d73Snpiggin@suse.de 	 */
537d3ef3d73Snpiggin@suse.de 	smp_mb();
538d3ef3d73Snpiggin@suse.de 
539d3ef3d73Snpiggin@suse.de 	/*
540d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
541d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
542d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
543d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
544d3ef3d73Snpiggin@suse.de 	 *
545d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
546d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
547d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
548d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
549d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
550d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
551d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
552d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
553d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
554d3ef3d73Snpiggin@suse.de 	 */
555c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
556d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
557d3ef3d73Snpiggin@suse.de 	else
55883adc753SAl Viro 		mnt->mnt.mnt_flags |= MNT_READONLY;
559d3ef3d73Snpiggin@suse.de 	/*
560d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
561d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
562d3ef3d73Snpiggin@suse.de 	 */
563d3ef3d73Snpiggin@suse.de 	smp_wmb();
56483adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
565719ea2fbSAl Viro 	unlock_mount_hash();
5663d733633SDave Hansen 	return ret;
5673d733633SDave Hansen }
5688366025eSDave Hansen 
56983adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt)
5702e4b7fcdSDave Hansen {
571719ea2fbSAl Viro 	lock_mount_hash();
57283adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
573719ea2fbSAl Viro 	unlock_mount_hash();
5742e4b7fcdSDave Hansen }
5752e4b7fcdSDave Hansen 
5764ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5774ed5e82fSMiklos Szeredi {
5784ed5e82fSMiklos Szeredi 	struct mount *mnt;
5794ed5e82fSMiklos Szeredi 	int err = 0;
5804ed5e82fSMiklos Szeredi 
5818e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5828e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5838e8b8796SMiklos Szeredi 		return -EBUSY;
5848e8b8796SMiklos Szeredi 
585719ea2fbSAl Viro 	lock_mount_hash();
5864ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5874ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
5884ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
5894ed5e82fSMiklos Szeredi 			smp_mb();
5904ed5e82fSMiklos Szeredi 			if (mnt_get_writers(mnt) > 0) {
5914ed5e82fSMiklos Szeredi 				err = -EBUSY;
5924ed5e82fSMiklos Szeredi 				break;
5934ed5e82fSMiklos Szeredi 			}
5944ed5e82fSMiklos Szeredi 		}
5954ed5e82fSMiklos Szeredi 	}
5968e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5978e8b8796SMiklos Szeredi 		err = -EBUSY;
5988e8b8796SMiklos Szeredi 
5994ed5e82fSMiklos Szeredi 	if (!err) {
6004ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
6014ed5e82fSMiklos Szeredi 		smp_wmb();
6024ed5e82fSMiklos Szeredi 	}
6034ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
6044ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
6054ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
6064ed5e82fSMiklos Szeredi 	}
607719ea2fbSAl Viro 	unlock_mount_hash();
6084ed5e82fSMiklos Szeredi 
6094ed5e82fSMiklos Szeredi 	return err;
6104ed5e82fSMiklos Szeredi }
6114ed5e82fSMiklos Szeredi 
612b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
6131da177e4SLinus Torvalds {
614fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
615d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
61668e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
617d3ef3d73Snpiggin@suse.de #endif
618b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
6218ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
6228ffcb32eSDavid Howells {
6238ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
6248ffcb32eSDavid Howells }
6258ffcb32eSDavid Howells 
62648a066e7SAl Viro /* call under rcu_read_lock */
627294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
62848a066e7SAl Viro {
62948a066e7SAl Viro 	struct mount *mnt;
63048a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
631294d71ffSAl Viro 		return 1;
63248a066e7SAl Viro 	if (bastard == NULL)
633294d71ffSAl Viro 		return 0;
63448a066e7SAl Viro 	mnt = real_mount(bastard);
63548a066e7SAl Viro 	mnt_add_count(mnt, 1);
636119e1ef8SAl Viro 	smp_mb();			// see mntput_no_expire()
63748a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
638294d71ffSAl Viro 		return 0;
63948a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
64048a066e7SAl Viro 		mnt_add_count(mnt, -1);
641294d71ffSAl Viro 		return 1;
64248a066e7SAl Viro 	}
643119e1ef8SAl Viro 	lock_mount_hash();
644119e1ef8SAl Viro 	if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
645119e1ef8SAl Viro 		mnt_add_count(mnt, -1);
646119e1ef8SAl Viro 		unlock_mount_hash();
647119e1ef8SAl Viro 		return 1;
648119e1ef8SAl Viro 	}
649119e1ef8SAl Viro 	unlock_mount_hash();
650119e1ef8SAl Viro 	/* caller will mntput() */
651294d71ffSAl Viro 	return -1;
652294d71ffSAl Viro }
653294d71ffSAl Viro 
654294d71ffSAl Viro /* call under rcu_read_lock */
655294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
656294d71ffSAl Viro {
657294d71ffSAl Viro 	int res = __legitimize_mnt(bastard, seq);
658294d71ffSAl Viro 	if (likely(!res))
659294d71ffSAl Viro 		return true;
660294d71ffSAl Viro 	if (unlikely(res < 0)) {
66148a066e7SAl Viro 		rcu_read_unlock();
66248a066e7SAl Viro 		mntput(bastard);
66348a066e7SAl Viro 		rcu_read_lock();
664294d71ffSAl Viro 	}
66548a066e7SAl Viro 	return false;
66648a066e7SAl Viro }
66748a066e7SAl Viro 
6681da177e4SLinus Torvalds /*
669474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
67048a066e7SAl Viro  * call under rcu_read_lock()
6711da177e4SLinus Torvalds  */
672474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6731da177e4SLinus Torvalds {
67438129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
675474279dcSAl Viro 	struct mount *p;
6761da177e4SLinus Torvalds 
67738129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
678474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
679474279dcSAl Viro 			return p;
680474279dcSAl Viro 	return NULL;
6811da177e4SLinus Torvalds }
682474279dcSAl Viro 
683474279dcSAl Viro /*
684f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
685f015f126SDavid Howells  *
686f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
687f015f126SDavid Howells  * following mounts:
688f015f126SDavid Howells  *
689f015f126SDavid Howells  * mount /dev/sda1 /mnt
690f015f126SDavid Howells  * mount /dev/sda2 /mnt
691f015f126SDavid Howells  * mount /dev/sda3 /mnt
692f015f126SDavid Howells  *
693f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
694f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
695f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
696f015f126SDavid Howells  *
697f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
698a05964f3SRam Pai  */
699ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path)
700a05964f3SRam Pai {
701c7105365SAl Viro 	struct mount *child_mnt;
70248a066e7SAl Viro 	struct vfsmount *m;
70348a066e7SAl Viro 	unsigned seq;
70499b7db7bSNick Piggin 
70548a066e7SAl Viro 	rcu_read_lock();
70648a066e7SAl Viro 	do {
70748a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
708474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
70948a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
71048a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
71148a066e7SAl Viro 	rcu_read_unlock();
71248a066e7SAl Viro 	return m;
713a05964f3SRam Pai }
714a05964f3SRam Pai 
7157af1364fSEric W. Biederman /*
7167af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
7177af1364fSEric W. Biederman  *                         current mount namespace.
7187af1364fSEric W. Biederman  *
7197af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
7207af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
7217af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
7227af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
7237af1364fSEric W. Biederman  * is a mountpoint.
7247af1364fSEric W. Biederman  *
7257af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
7267af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
7277af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
7287af1364fSEric W. Biederman  * parent mount.
7297af1364fSEric W. Biederman  */
7307af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
7317af1364fSEric W. Biederman {
7327af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
7337af1364fSEric W. Biederman 	struct mount *mnt;
7347af1364fSEric W. Biederman 	bool is_covered = false;
7357af1364fSEric W. Biederman 
7367af1364fSEric W. Biederman 	if (!d_mountpoint(dentry))
7377af1364fSEric W. Biederman 		goto out;
7387af1364fSEric W. Biederman 
7397af1364fSEric W. Biederman 	down_read(&namespace_sem);
7407af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
7417af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
7427af1364fSEric W. Biederman 		if (is_covered)
7437af1364fSEric W. Biederman 			break;
7447af1364fSEric W. Biederman 	}
7457af1364fSEric W. Biederman 	up_read(&namespace_sem);
7467af1364fSEric W. Biederman out:
7477af1364fSEric W. Biederman 	return is_covered;
7487af1364fSEric W. Biederman }
7497af1364fSEric W. Biederman 
750e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
75184d17192SAl Viro {
7520818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
75384d17192SAl Viro 	struct mountpoint *mp;
75484d17192SAl Viro 
7550818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
75684d17192SAl Viro 		if (mp->m_dentry == dentry) {
75784d17192SAl Viro 			/* might be worth a WARN_ON() */
75884d17192SAl Viro 			if (d_unlinked(dentry))
75984d17192SAl Viro 				return ERR_PTR(-ENOENT);
76084d17192SAl Viro 			mp->m_count++;
76184d17192SAl Viro 			return mp;
76284d17192SAl Viro 		}
76384d17192SAl Viro 	}
764e2dfa935SEric W. Biederman 	return NULL;
765e2dfa935SEric W. Biederman }
766e2dfa935SEric W. Biederman 
7673895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry)
768e2dfa935SEric W. Biederman {
7693895dbf8SEric W. Biederman 	struct mountpoint *mp, *new = NULL;
770e2dfa935SEric W. Biederman 	int ret;
77184d17192SAl Viro 
7723895dbf8SEric W. Biederman 	if (d_mountpoint(dentry)) {
7733895dbf8SEric W. Biederman mountpoint:
7743895dbf8SEric W. Biederman 		read_seqlock_excl(&mount_lock);
7753895dbf8SEric W. Biederman 		mp = lookup_mountpoint(dentry);
7763895dbf8SEric W. Biederman 		read_sequnlock_excl(&mount_lock);
7773895dbf8SEric W. Biederman 		if (mp)
7783895dbf8SEric W. Biederman 			goto done;
77984d17192SAl Viro 	}
780eed81007SMiklos Szeredi 
7813895dbf8SEric W. Biederman 	if (!new)
7823895dbf8SEric W. Biederman 		new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
7833895dbf8SEric W. Biederman 	if (!new)
7843895dbf8SEric W. Biederman 		return ERR_PTR(-ENOMEM);
7853895dbf8SEric W. Biederman 
7863895dbf8SEric W. Biederman 
7873895dbf8SEric W. Biederman 	/* Exactly one processes may set d_mounted */
7883895dbf8SEric W. Biederman 	ret = d_set_mounted(dentry);
7893895dbf8SEric W. Biederman 
7903895dbf8SEric W. Biederman 	/* Someone else set d_mounted? */
7913895dbf8SEric W. Biederman 	if (ret == -EBUSY)
7923895dbf8SEric W. Biederman 		goto mountpoint;
7933895dbf8SEric W. Biederman 
7943895dbf8SEric W. Biederman 	/* The dentry is not available as a mountpoint? */
7953895dbf8SEric W. Biederman 	mp = ERR_PTR(ret);
7963895dbf8SEric W. Biederman 	if (ret)
7973895dbf8SEric W. Biederman 		goto done;
7983895dbf8SEric W. Biederman 
7993895dbf8SEric W. Biederman 	/* Add the new mountpoint to the hash table */
8003895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
8013895dbf8SEric W. Biederman 	new->m_dentry = dentry;
8023895dbf8SEric W. Biederman 	new->m_count = 1;
8033895dbf8SEric W. Biederman 	hlist_add_head(&new->m_hash, mp_hash(dentry));
8043895dbf8SEric W. Biederman 	INIT_HLIST_HEAD(&new->m_list);
8053895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
8063895dbf8SEric W. Biederman 
8073895dbf8SEric W. Biederman 	mp = new;
8083895dbf8SEric W. Biederman 	new = NULL;
8093895dbf8SEric W. Biederman done:
8103895dbf8SEric W. Biederman 	kfree(new);
81184d17192SAl Viro 	return mp;
81284d17192SAl Viro }
81384d17192SAl Viro 
81484d17192SAl Viro static void put_mountpoint(struct mountpoint *mp)
81584d17192SAl Viro {
81684d17192SAl Viro 	if (!--mp->m_count) {
81784d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
8180a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
81984d17192SAl Viro 		spin_lock(&dentry->d_lock);
82084d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
82184d17192SAl Viro 		spin_unlock(&dentry->d_lock);
8220818bf27SAl Viro 		hlist_del(&mp->m_hash);
82384d17192SAl Viro 		kfree(mp);
82484d17192SAl Viro 	}
82584d17192SAl Viro }
82684d17192SAl Viro 
827143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
8281da177e4SLinus Torvalds {
8296b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
83299b7db7bSNick Piggin /*
83399b7db7bSNick Piggin  * vfsmount lock must be held for write
83499b7db7bSNick Piggin  */
8356b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
8365addc5ddSAl Viro {
8375addc5ddSAl Viro 	if (ns) {
8385addc5ddSAl Viro 		ns->event = ++event;
8395addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
8405addc5ddSAl Viro 	}
8415addc5ddSAl Viro }
8425addc5ddSAl Viro 
84399b7db7bSNick Piggin /*
84499b7db7bSNick Piggin  * vfsmount lock must be held for write
84599b7db7bSNick Piggin  */
8466b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
8475addc5ddSAl Viro {
8485addc5ddSAl Viro 	if (ns && ns->event != event) {
8495addc5ddSAl Viro 		ns->event = event;
8505addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
8515addc5ddSAl Viro 	}
8525addc5ddSAl Viro }
8535addc5ddSAl Viro 
85499b7db7bSNick Piggin /*
85599b7db7bSNick Piggin  * vfsmount lock must be held for write
85699b7db7bSNick Piggin  */
8577bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt)
8581da177e4SLinus Torvalds {
8590714a533SAl Viro 	mnt->mnt_parent = mnt;
860a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8616b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
86238129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
8630a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
86484d17192SAl Viro 	put_mountpoint(mnt->mnt_mp);
86584d17192SAl Viro 	mnt->mnt_mp = NULL;
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
86899b7db7bSNick Piggin /*
86999b7db7bSNick Piggin  * vfsmount lock must be held for write
87099b7db7bSNick Piggin  */
8717bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path)
8727bdb11deSEric W. Biederman {
8737bdb11deSEric W. Biederman 	old_path->dentry = mnt->mnt_mountpoint;
8747bdb11deSEric W. Biederman 	old_path->mnt = &mnt->mnt_parent->mnt;
8757bdb11deSEric W. Biederman 	unhash_mnt(mnt);
8767bdb11deSEric W. Biederman }
8777bdb11deSEric W. Biederman 
8787bdb11deSEric W. Biederman /*
8797bdb11deSEric W. Biederman  * vfsmount lock must be held for write
8807bdb11deSEric W. Biederman  */
8816a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt)
8826a46c573SEric W. Biederman {
8836a46c573SEric W. Biederman 	/* old mountpoint will be dropped when we can do that */
8846a46c573SEric W. Biederman 	mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
8856a46c573SEric W. Biederman 	unhash_mnt(mnt);
8866a46c573SEric W. Biederman }
8876a46c573SEric W. Biederman 
8886a46c573SEric W. Biederman /*
8896a46c573SEric W. Biederman  * vfsmount lock must be held for write
8906a46c573SEric W. Biederman  */
89184d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
89284d17192SAl Viro 			struct mountpoint *mp,
89344d964d6SAl Viro 			struct mount *child_mnt)
894b90fa9aeSRam Pai {
89584d17192SAl Viro 	mp->m_count++;
8963a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
89784d17192SAl Viro 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
8983a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
89984d17192SAl Viro 	child_mnt->mnt_mp = mp;
9000a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
901b90fa9aeSRam Pai }
902b90fa9aeSRam Pai 
9031064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent)
9041064f874SEric W. Biederman {
9051064f874SEric W. Biederman 	hlist_add_head_rcu(&mnt->mnt_hash,
9061064f874SEric W. Biederman 			   m_hash(&parent->mnt, mnt->mnt_mountpoint));
9071064f874SEric W. Biederman 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
9081064f874SEric W. Biederman }
9091064f874SEric W. Biederman 
91099b7db7bSNick Piggin /*
91199b7db7bSNick Piggin  * vfsmount lock must be held for write
91299b7db7bSNick Piggin  */
91384d17192SAl Viro static void attach_mnt(struct mount *mnt,
91484d17192SAl Viro 			struct mount *parent,
91584d17192SAl Viro 			struct mountpoint *mp)
9161da177e4SLinus Torvalds {
91784d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
9181064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
919b90fa9aeSRam Pai }
920b90fa9aeSRam Pai 
9211064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
92212a5b529SAl Viro {
9231064f874SEric W. Biederman 	struct mountpoint *old_mp = mnt->mnt_mp;
9241064f874SEric W. Biederman 	struct dentry *old_mountpoint = mnt->mnt_mountpoint;
9251064f874SEric W. Biederman 	struct mount *old_parent = mnt->mnt_parent;
9261064f874SEric W. Biederman 
9271064f874SEric W. Biederman 	list_del_init(&mnt->mnt_child);
9281064f874SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
9291064f874SEric W. Biederman 	hlist_del_init_rcu(&mnt->mnt_hash);
9301064f874SEric W. Biederman 
9311064f874SEric W. Biederman 	attach_mnt(mnt, parent, mp);
9321064f874SEric W. Biederman 
9331064f874SEric W. Biederman 	put_mountpoint(old_mp);
9341064f874SEric W. Biederman 
9351064f874SEric W. Biederman 	/*
9361064f874SEric W. Biederman 	 * Safely avoid even the suggestion this code might sleep or
9371064f874SEric W. Biederman 	 * lock the mount hash by taking advantage of the knowledge that
9381064f874SEric W. Biederman 	 * mnt_change_mountpoint will not release the final reference
9391064f874SEric W. Biederman 	 * to a mountpoint.
9401064f874SEric W. Biederman 	 *
9411064f874SEric W. Biederman 	 * During mounting, the mount passed in as the parent mount will
9421064f874SEric W. Biederman 	 * continue to use the old mountpoint and during unmounting, the
9431064f874SEric W. Biederman 	 * old mountpoint will continue to exist until namespace_unlock,
9441064f874SEric W. Biederman 	 * which happens well after mnt_change_mountpoint.
9451064f874SEric W. Biederman 	 */
9461064f874SEric W. Biederman 	spin_lock(&old_mountpoint->d_lock);
9471064f874SEric W. Biederman 	old_mountpoint->d_lockref.count--;
9481064f874SEric W. Biederman 	spin_unlock(&old_mountpoint->d_lock);
9491064f874SEric W. Biederman 
9501064f874SEric W. Biederman 	mnt_add_count(old_parent, -1);
95112a5b529SAl Viro }
95212a5b529SAl Viro 
953b90fa9aeSRam Pai /*
95499b7db7bSNick Piggin  * vfsmount lock must be held for write
955b90fa9aeSRam Pai  */
9561064f874SEric W. Biederman static void commit_tree(struct mount *mnt)
957b90fa9aeSRam Pai {
9580714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
95983adc753SAl Viro 	struct mount *m;
960b90fa9aeSRam Pai 	LIST_HEAD(head);
961143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
962b90fa9aeSRam Pai 
9630714a533SAl Viro 	BUG_ON(parent == mnt);
964b90fa9aeSRam Pai 
9651a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
966f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
967143c8c91SAl Viro 		m->mnt_ns = n;
968f03c6599SAl Viro 
969b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
970b90fa9aeSRam Pai 
971d2921684SEric W. Biederman 	n->mounts += n->pending_mounts;
972d2921684SEric W. Biederman 	n->pending_mounts = 0;
973d2921684SEric W. Biederman 
9741064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
9756b3286edSKirill Korotaev 	touch_mnt_namespace(n);
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
978909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
9791da177e4SLinus Torvalds {
9806b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
9816b41d536SAl Viro 	if (next == &p->mnt_mounts) {
9821da177e4SLinus Torvalds 		while (1) {
983909b0a88SAl Viro 			if (p == root)
9841da177e4SLinus Torvalds 				return NULL;
9856b41d536SAl Viro 			next = p->mnt_child.next;
9866b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
9871da177e4SLinus Torvalds 				break;
9880714a533SAl Viro 			p = p->mnt_parent;
9891da177e4SLinus Torvalds 		}
9901da177e4SLinus Torvalds 	}
9916b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
994315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
9959676f0c6SRam Pai {
9966b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
9976b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
9986b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
9996b41d536SAl Viro 		prev = p->mnt_mounts.prev;
10009676f0c6SRam Pai 	}
10019676f0c6SRam Pai 	return p;
10029676f0c6SRam Pai }
10039676f0c6SRam Pai 
10049d412a43SAl Viro struct vfsmount *
10059d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
10069d412a43SAl Viro {
1007b105e270SAl Viro 	struct mount *mnt;
10089d412a43SAl Viro 	struct dentry *root;
10099d412a43SAl Viro 
10109d412a43SAl Viro 	if (!type)
10119d412a43SAl Viro 		return ERR_PTR(-ENODEV);
10129d412a43SAl Viro 
10139d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
10149d412a43SAl Viro 	if (!mnt)
10159d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
10169d412a43SAl Viro 
1017e462ec50SDavid Howells 	if (flags & SB_KERNMOUNT)
1018b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
10199d412a43SAl Viro 
10209d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
10219d412a43SAl Viro 	if (IS_ERR(root)) {
10228ffcb32eSDavid Howells 		mnt_free_id(mnt);
10239d412a43SAl Viro 		free_vfsmnt(mnt);
10249d412a43SAl Viro 		return ERR_CAST(root);
10259d412a43SAl Viro 	}
10269d412a43SAl Viro 
1027b105e270SAl Viro 	mnt->mnt.mnt_root = root;
1028b105e270SAl Viro 	mnt->mnt.mnt_sb = root->d_sb;
1029a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
10300714a533SAl Viro 	mnt->mnt_parent = mnt;
1031719ea2fbSAl Viro 	lock_mount_hash();
103239f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
1033719ea2fbSAl Viro 	unlock_mount_hash();
1034b105e270SAl Viro 	return &mnt->mnt;
10359d412a43SAl Viro }
10369d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
10379d412a43SAl Viro 
103893faccbbSEric W. Biederman struct vfsmount *
103993faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
104093faccbbSEric W. Biederman 	     const char *name, void *data)
104193faccbbSEric W. Biederman {
104293faccbbSEric W. Biederman 	/* Until it is worked out how to pass the user namespace
104393faccbbSEric W. Biederman 	 * through from the parent mount to the submount don't support
104493faccbbSEric W. Biederman 	 * unprivileged mounts with submounts.
104593faccbbSEric W. Biederman 	 */
104693faccbbSEric W. Biederman 	if (mountpoint->d_sb->s_user_ns != &init_user_ns)
104793faccbbSEric W. Biederman 		return ERR_PTR(-EPERM);
104893faccbbSEric W. Biederman 
1049e462ec50SDavid Howells 	return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
105093faccbbSEric W. Biederman }
105193faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount);
105293faccbbSEric W. Biederman 
105387129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
105436341f64SRam Pai 					int flag)
10551da177e4SLinus Torvalds {
105687129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
1057be34d1a3SDavid Howells 	struct mount *mnt;
1058be34d1a3SDavid Howells 	int err;
10591da177e4SLinus Torvalds 
1060be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
1061be34d1a3SDavid Howells 	if (!mnt)
1062be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
1063be34d1a3SDavid Howells 
10647a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
106515169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
1066719f5d7fSMiklos Szeredi 	else
106715169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
1068719f5d7fSMiklos Szeredi 
106915169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1070be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
1071719f5d7fSMiklos Szeredi 		if (err)
1072719f5d7fSMiklos Szeredi 			goto out_free;
1073719f5d7fSMiklos Szeredi 	}
1074719f5d7fSMiklos Szeredi 
107516a34adbSAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags;
107616a34adbSAl Viro 	mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
1077132c94e3SEric W. Biederman 	/* Don't allow unprivileged users to change mount flags */
10789566d674SEric W. Biederman 	if (flag & CL_UNPRIVILEGED) {
10799566d674SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
10809566d674SEric W. Biederman 
10819566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_READONLY)
1082132c94e3SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
1083132c94e3SEric W. Biederman 
10849566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NODEV)
10859566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
10869566d674SEric W. Biederman 
10879566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NOSUID)
10889566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
10899566d674SEric W. Biederman 
10909566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NOEXEC)
10919566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
10929566d674SEric W. Biederman 	}
10939566d674SEric W. Biederman 
10945ff9d8a6SEric W. Biederman 	/* Don't allow unprivileged users to reveal what is under a mount */
1095381cacb1SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) &&
1096381cacb1SEric W. Biederman 	    (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
10975ff9d8a6SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCKED;
10985ff9d8a6SEric W. Biederman 
10991da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1100b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1101b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1102a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
11030714a533SAl Viro 	mnt->mnt_parent = mnt;
1104719ea2fbSAl Viro 	lock_mount_hash();
110539f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1106719ea2fbSAl Viro 	unlock_mount_hash();
1107b90fa9aeSRam Pai 
11087a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
11097a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
11106776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
111132301920SAl Viro 		mnt->mnt_master = old;
1112fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
11138aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1114fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
11156776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1116d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
11176776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1118d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
11195235d448SAl Viro 	} else {
11205235d448SAl Viro 		CLEAR_MNT_SHARED(mnt);
11215afe0022SRam Pai 	}
1122b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
11230f0afb1dSAl Viro 		set_mnt_shared(mnt);
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
11261da177e4SLinus Torvalds 	 * as the original if that was on one */
112736341f64SRam Pai 	if (flag & CL_EXPIRE) {
11286776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
11296776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
11301da177e4SLinus Torvalds 	}
1131be34d1a3SDavid Howells 
1132cb338d06SAl Viro 	return mnt;
1133719f5d7fSMiklos Szeredi 
1134719f5d7fSMiklos Szeredi  out_free:
11358ffcb32eSDavid Howells 	mnt_free_id(mnt);
1136719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1137be34d1a3SDavid Howells 	return ERR_PTR(err);
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
11409ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
11419ea459e1SAl Viro {
11429ea459e1SAl Viro 	/*
11439ea459e1SAl Viro 	 * This probably indicates that somebody messed
11449ea459e1SAl Viro 	 * up a mnt_want/drop_write() pair.  If this
11459ea459e1SAl Viro 	 * happens, the filesystem was probably unable
11469ea459e1SAl Viro 	 * to make r/w->r/o transitions.
11479ea459e1SAl Viro 	 */
11489ea459e1SAl Viro 	/*
11499ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
11509ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
11519ea459e1SAl Viro 	 */
11529ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
11539ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
11549ea459e1SAl Viro 		mnt_pin_kill(mnt);
11559ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
11569ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
11579ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
11589ea459e1SAl Viro 	mnt_free_id(mnt);
11599ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
11609ea459e1SAl Viro }
11619ea459e1SAl Viro 
11629ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
11639ea459e1SAl Viro {
11649ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
11659ea459e1SAl Viro }
11669ea459e1SAl Viro 
11679ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
11689ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
11699ea459e1SAl Viro {
11709ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
117129785735SByungchul Park 	struct mount *m, *t;
11729ea459e1SAl Viro 
117329785735SByungchul Park 	llist_for_each_entry_safe(m, t, node, mnt_llist)
117429785735SByungchul Park 		cleanup_mnt(m);
11759ea459e1SAl Viro }
11769ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
11779ea459e1SAl Viro 
1178900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
11797b7b1aceSAl Viro {
118048a066e7SAl Viro 	rcu_read_lock();
11819ea0a46cSAl Viro 	if (likely(READ_ONCE(mnt->mnt_ns))) {
11829ea0a46cSAl Viro 		/*
11839ea0a46cSAl Viro 		 * Since we don't do lock_mount_hash() here,
11849ea0a46cSAl Viro 		 * ->mnt_ns can change under us.  However, if it's
11859ea0a46cSAl Viro 		 * non-NULL, then there's a reference that won't
11869ea0a46cSAl Viro 		 * be dropped until after an RCU delay done after
11879ea0a46cSAl Viro 		 * turning ->mnt_ns NULL.  So if we observe it
11889ea0a46cSAl Viro 		 * non-NULL under rcu_read_lock(), the reference
11899ea0a46cSAl Viro 		 * we are dropping is not the final one.
11909ea0a46cSAl Viro 		 */
1191aa9c0e07SAl Viro 		mnt_add_count(mnt, -1);
119248a066e7SAl Viro 		rcu_read_unlock();
119399b7db7bSNick Piggin 		return;
1194b3e19d92SNick Piggin 	}
1195719ea2fbSAl Viro 	lock_mount_hash();
1196119e1ef8SAl Viro 	/*
1197119e1ef8SAl Viro 	 * make sure that if __legitimize_mnt() has not seen us grab
1198119e1ef8SAl Viro 	 * mount_lock, we'll see their refcount increment here.
1199119e1ef8SAl Viro 	 */
1200119e1ef8SAl Viro 	smp_mb();
12019ea0a46cSAl Viro 	mnt_add_count(mnt, -1);
1202b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
120348a066e7SAl Viro 		rcu_read_unlock();
1204719ea2fbSAl Viro 		unlock_mount_hash();
120599b7db7bSNick Piggin 		return;
120699b7db7bSNick Piggin 	}
120748a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
120848a066e7SAl Viro 		rcu_read_unlock();
120948a066e7SAl Viro 		unlock_mount_hash();
121048a066e7SAl Viro 		return;
121148a066e7SAl Viro 	}
121248a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
121348a066e7SAl Viro 	rcu_read_unlock();
1214962830dfSAndi Kleen 
121539f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1216ce07d891SEric W. Biederman 
1217ce07d891SEric W. Biederman 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1218ce07d891SEric W. Biederman 		struct mount *p, *tmp;
1219ce07d891SEric W. Biederman 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
1220ce07d891SEric W. Biederman 			umount_mnt(p);
1221ce07d891SEric W. Biederman 		}
1222ce07d891SEric W. Biederman 	}
1223719ea2fbSAl Viro 	unlock_mount_hash();
1224649a795aSAl Viro 
12259ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
12269ea459e1SAl Viro 		struct task_struct *task = current;
12279ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
12289ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
12299ea459e1SAl Viro 			if (!task_work_add(task, &mnt->mnt_rcu, true))
12309ea459e1SAl Viro 				return;
12319ea459e1SAl Viro 		}
12329ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
12339ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
12349ea459e1SAl Viro 		return;
12359ea459e1SAl Viro 	}
12369ea459e1SAl Viro 	cleanup_mnt(mnt);
1237b3e19d92SNick Piggin }
1238b3e19d92SNick Piggin 
1239b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1240b3e19d92SNick Piggin {
1241b3e19d92SNick Piggin 	if (mnt) {
1242863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1243b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1244863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1245863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1246863d684fSAl Viro 		mntput_no_expire(m);
1247b3e19d92SNick Piggin 	}
1248b3e19d92SNick Piggin }
1249b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1250b3e19d92SNick Piggin 
1251b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1252b3e19d92SNick Piggin {
1253b3e19d92SNick Piggin 	if (mnt)
125483adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1255b3e19d92SNick Piggin 	return mnt;
1256b3e19d92SNick Piggin }
1257b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1258b3e19d92SNick Piggin 
1259c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current
1260c6609c0aSIan Kent  *                          namespace.
1261c6609c0aSIan Kent  *
1262c6609c0aSIan Kent  *  d_mountpoint() can only be used reliably to establish if a dentry is
1263c6609c0aSIan Kent  *  not mounted in any namespace and that common case is handled inline.
1264c6609c0aSIan Kent  *  d_mountpoint() isn't aware of the possibility there may be multiple
1265c6609c0aSIan Kent  *  mounts using a given dentry in a different namespace. This function
1266c6609c0aSIan Kent  *  checks if the passed in path is a mountpoint rather than the dentry
1267c6609c0aSIan Kent  *  alone.
1268c6609c0aSIan Kent  */
1269c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path)
1270c6609c0aSIan Kent {
1271c6609c0aSIan Kent 	unsigned seq;
1272c6609c0aSIan Kent 	bool res;
1273c6609c0aSIan Kent 
1274c6609c0aSIan Kent 	if (!d_mountpoint(path->dentry))
1275c6609c0aSIan Kent 		return false;
1276c6609c0aSIan Kent 
1277c6609c0aSIan Kent 	rcu_read_lock();
1278c6609c0aSIan Kent 	do {
1279c6609c0aSIan Kent 		seq = read_seqbegin(&mount_lock);
1280c6609c0aSIan Kent 		res = __path_is_mountpoint(path);
1281c6609c0aSIan Kent 	} while (read_seqretry(&mount_lock, seq));
1282c6609c0aSIan Kent 	rcu_read_unlock();
1283c6609c0aSIan Kent 
1284c6609c0aSIan Kent 	return res;
1285c6609c0aSIan Kent }
1286c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint);
1287c6609c0aSIan Kent 
1288ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path)
12897b7b1aceSAl Viro {
12903064c356SAl Viro 	struct mount *p;
12913064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
12923064c356SAl Viro 	if (IS_ERR(p))
12933064c356SAl Viro 		return ERR_CAST(p);
12943064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
12953064c356SAl Viro 	return &p->mnt;
12967b7b1aceSAl Viro }
12971da177e4SLinus Torvalds 
1298a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
12990226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
13001da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
13011da177e4SLinus Torvalds {
1302ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
13031da177e4SLinus Torvalds 
1304390c6843SRam Pai 	down_read(&namespace_sem);
1305c7999c36SAl Viro 	if (p->cached_event == p->ns->event) {
1306c7999c36SAl Viro 		void *v = p->cached_mount;
1307c7999c36SAl Viro 		if (*pos == p->cached_index)
1308c7999c36SAl Viro 			return v;
1309c7999c36SAl Viro 		if (*pos == p->cached_index + 1) {
1310c7999c36SAl Viro 			v = seq_list_next(v, &p->ns->list, &p->cached_index);
1311c7999c36SAl Viro 			return p->cached_mount = v;
1312c7999c36SAl Viro 		}
1313c7999c36SAl Viro 	}
1314c7999c36SAl Viro 
1315c7999c36SAl Viro 	p->cached_event = p->ns->event;
1316c7999c36SAl Viro 	p->cached_mount = seq_list_start(&p->ns->list, *pos);
1317c7999c36SAl Viro 	p->cached_index = *pos;
1318c7999c36SAl Viro 	return p->cached_mount;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
13221da177e4SLinus Torvalds {
1323ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
1324b0765fb8SPavel Emelianov 
1325c7999c36SAl Viro 	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1326c7999c36SAl Viro 	p->cached_index = *pos;
1327c7999c36SAl Viro 	return p->cached_mount;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
13311da177e4SLinus Torvalds {
1332390c6843SRam Pai 	up_read(&namespace_sem);
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
13350226f492SAl Viro static int m_show(struct seq_file *m, void *v)
13369f5596afSAl Viro {
1337ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
13381a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
13390226f492SAl Viro 	return p->show(m, &r->mnt);
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
1342a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
13431da177e4SLinus Torvalds 	.start	= m_start,
13441da177e4SLinus Torvalds 	.next	= m_next,
13451da177e4SLinus Torvalds 	.stop	= m_stop,
13460226f492SAl Viro 	.show	= m_show,
1347b4629fe2SChuck Lever };
1348a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1349b4629fe2SChuck Lever 
13501da177e4SLinus Torvalds /**
13511da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
13521da177e4SLinus Torvalds  * @mnt: root of mount tree
13531da177e4SLinus Torvalds  *
13541da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
13551da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
13561da177e4SLinus Torvalds  * busy.
13571da177e4SLinus Torvalds  */
1358909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
13591da177e4SLinus Torvalds {
1360909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
136136341f64SRam Pai 	int actual_refs = 0;
136236341f64SRam Pai 	int minimum_refs = 0;
1363315fc83eSAl Viro 	struct mount *p;
1364909b0a88SAl Viro 	BUG_ON(!m);
13651da177e4SLinus Torvalds 
1366b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1367719ea2fbSAl Viro 	lock_mount_hash();
1368909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
136983adc753SAl Viro 		actual_refs += mnt_get_count(p);
13701da177e4SLinus Torvalds 		minimum_refs += 2;
13711da177e4SLinus Torvalds 	}
1372719ea2fbSAl Viro 	unlock_mount_hash();
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
13751da177e4SLinus Torvalds 		return 0;
1376e3474a8eSIan Kent 
1377e3474a8eSIan Kent 	return 1;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds /**
13831da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
13841da177e4SLinus Torvalds  * @mnt: root of mount
13851da177e4SLinus Torvalds  *
13861da177e4SLinus Torvalds  * This is called to check if a mount point has any
13871da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
13881da177e4SLinus Torvalds  * mount has sub mounts this will return busy
13891da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
13901da177e4SLinus Torvalds  *
13911da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
13921da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
13931da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
13941da177e4SLinus Torvalds  */
13951da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
13961da177e4SLinus Torvalds {
1397e3474a8eSIan Kent 	int ret = 1;
13988ad08d8aSAl Viro 	down_read(&namespace_sem);
1399719ea2fbSAl Viro 	lock_mount_hash();
14001ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1401e3474a8eSIan Kent 		ret = 0;
1402719ea2fbSAl Viro 	unlock_mount_hash();
14038ad08d8aSAl Viro 	up_read(&namespace_sem);
1404a05964f3SRam Pai 	return ret;
14051da177e4SLinus Torvalds }
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
14081da177e4SLinus Torvalds 
140938129a13SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
1410e3197d83SAl Viro 
141197216be0SAl Viro static void namespace_unlock(void)
14121da177e4SLinus Torvalds {
1413a3b3c562SEric W. Biederman 	struct hlist_head head;
141497216be0SAl Viro 
1415a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
1416a3b3c562SEric W. Biederman 
141797216be0SAl Viro 	up_write(&namespace_sem);
1418a3b3c562SEric W. Biederman 
1419a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
142097216be0SAl Viro 		return;
142197216be0SAl Viro 
142248a066e7SAl Viro 	synchronize_rcu();
142348a066e7SAl Viro 
142487b95ce0SAl Viro 	group_pin_kill(&head);
142570fbcdf4SRam Pai }
142670fbcdf4SRam Pai 
142797216be0SAl Viro static inline void namespace_lock(void)
1428e3197d83SAl Viro {
142997216be0SAl Viro 	down_write(&namespace_sem);
1430e3197d83SAl Viro }
1431e3197d83SAl Viro 
1432e819f152SEric W. Biederman enum umount_tree_flags {
1433e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1434e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1435e0c9c0afSEric W. Biederman 	UMOUNT_CONNECTED = 4,
1436e819f152SEric W. Biederman };
1437f2d0a123SEric W. Biederman 
1438f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1439f2d0a123SEric W. Biederman {
1440f2d0a123SEric W. Biederman 	/* Leaving mounts connected is only valid for lazy umounts */
1441f2d0a123SEric W. Biederman 	if (how & UMOUNT_SYNC)
1442f2d0a123SEric W. Biederman 		return true;
1443f2d0a123SEric W. Biederman 
1444f2d0a123SEric W. Biederman 	/* A mount without a parent has nothing to be connected to */
1445f2d0a123SEric W. Biederman 	if (!mnt_has_parent(mnt))
1446f2d0a123SEric W. Biederman 		return true;
1447f2d0a123SEric W. Biederman 
1448f2d0a123SEric W. Biederman 	/* Because the reference counting rules change when mounts are
1449f2d0a123SEric W. Biederman 	 * unmounted and connected, umounted mounts may not be
1450f2d0a123SEric W. Biederman 	 * connected to mounted mounts.
1451f2d0a123SEric W. Biederman 	 */
1452f2d0a123SEric W. Biederman 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1453f2d0a123SEric W. Biederman 		return true;
1454f2d0a123SEric W. Biederman 
1455f2d0a123SEric W. Biederman 	/* Has it been requested that the mount remain connected? */
1456f2d0a123SEric W. Biederman 	if (how & UMOUNT_CONNECTED)
1457f2d0a123SEric W. Biederman 		return false;
1458f2d0a123SEric W. Biederman 
1459f2d0a123SEric W. Biederman 	/* Is the mount locked such that it needs to remain connected? */
1460f2d0a123SEric W. Biederman 	if (IS_MNT_LOCKED(mnt))
1461f2d0a123SEric W. Biederman 		return false;
1462f2d0a123SEric W. Biederman 
1463f2d0a123SEric W. Biederman 	/* By default disconnect the mount */
1464f2d0a123SEric W. Biederman 	return true;
1465f2d0a123SEric W. Biederman }
1466f2d0a123SEric W. Biederman 
146799b7db7bSNick Piggin /*
146848a066e7SAl Viro  * mount_lock must be held
146999b7db7bSNick Piggin  * namespace_sem must be held for write
147099b7db7bSNick Piggin  */
1471e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
147270fbcdf4SRam Pai {
1473c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1474315fc83eSAl Viro 	struct mount *p;
147570fbcdf4SRam Pai 
14765d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14775d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
14785d88457eSEric W. Biederman 
1479c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1480590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1481590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1482c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1483590ce4bcSEric W. Biederman 	}
1484c003b26fSEric W. Biederman 
1485411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1486c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1487c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
148838129a13SAl Viro 	}
148970fbcdf4SRam Pai 
1490c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1491e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14927b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1493a05964f3SRam Pai 
1494c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1495d2921684SEric W. Biederman 		struct mnt_namespace *ns;
1496ce07d891SEric W. Biederman 		bool disconnect;
1497c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
14986776db3dSAl Viro 		list_del_init(&p->mnt_expire);
14991a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1500d2921684SEric W. Biederman 		ns = p->mnt_ns;
1501d2921684SEric W. Biederman 		if (ns) {
1502d2921684SEric W. Biederman 			ns->mounts--;
1503d2921684SEric W. Biederman 			__touch_mnt_namespace(ns);
1504d2921684SEric W. Biederman 		}
150563d37a84SAl Viro 		p->mnt_ns = NULL;
1506e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
150748a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
150887b95ce0SAl Viro 
1509f2d0a123SEric W. Biederman 		disconnect = disconnect_mount(p, how);
1510ce07d891SEric W. Biederman 
1511ce07d891SEric W. Biederman 		pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1512ce07d891SEric W. Biederman 				 disconnect ? &unmounted : NULL);
1513676da58dSAl Viro 		if (mnt_has_parent(p)) {
151481b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
1515ce07d891SEric W. Biederman 			if (!disconnect) {
1516ce07d891SEric W. Biederman 				/* Don't forget about p */
1517ce07d891SEric W. Biederman 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1518ce07d891SEric W. Biederman 			} else {
15196a46c573SEric W. Biederman 				umount_mnt(p);
15207c4b93d8SAl Viro 			}
1521ce07d891SEric W. Biederman 		}
15220f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
152338129a13SAl Viro 	}
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds 
1526b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1527c35038beSAl Viro 
15281ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
15291da177e4SLinus Torvalds {
15301ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
15311da177e4SLinus Torvalds 	int retval;
15321da177e4SLinus Torvalds 
15331ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
15341da177e4SLinus Torvalds 	if (retval)
15351da177e4SLinus Torvalds 		return retval;
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	/*
15381da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
15391da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
15401da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
15411da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
15421da177e4SLinus Torvalds 	 */
15431da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
15441ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
15451da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
15461da177e4SLinus Torvalds 			return -EINVAL;
15471da177e4SLinus Torvalds 
1548b3e19d92SNick Piggin 		/*
1549b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1550b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1551b3e19d92SNick Piggin 		 */
1552719ea2fbSAl Viro 		lock_mount_hash();
155383adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1554719ea2fbSAl Viro 			unlock_mount_hash();
15551da177e4SLinus Torvalds 			return -EBUSY;
1556b3e19d92SNick Piggin 		}
1557719ea2fbSAl Viro 		unlock_mount_hash();
15581da177e4SLinus Torvalds 
1559863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
15601da177e4SLinus Torvalds 			return -EAGAIN;
15611da177e4SLinus Torvalds 	}
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	/*
15641da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
15651da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
15661da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
15671da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
15681da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
15691da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
15701da177e4SLinus Torvalds 	 * about for the moment.
15711da177e4SLinus Torvalds 	 */
15721da177e4SLinus Torvalds 
157342faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
157442faad99SAl Viro 		sb->s_op->umount_begin(sb);
157542faad99SAl Viro 	}
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	/*
15781da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
15791da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
15801da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
15811da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
15821da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
15831da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
15841da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
15851da177e4SLinus Torvalds 	 */
15861ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
15871da177e4SLinus Torvalds 		/*
15881da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
15891da177e4SLinus Torvalds 		 * we just try to remount it readonly.
15901da177e4SLinus Torvalds 		 */
1591bc6155d1SEric W. Biederman 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1592a1480dccSAndy Lutomirski 			return -EPERM;
15931da177e4SLinus Torvalds 		down_write(&sb->s_umount);
1594bc98a42cSDavid Howells 		if (!sb_rdonly(sb))
1595e462ec50SDavid Howells 			retval = do_remount_sb(sb, SB_RDONLY, NULL, 0);
15961da177e4SLinus Torvalds 		up_write(&sb->s_umount);
15971da177e4SLinus Torvalds 		return retval;
15981da177e4SLinus Torvalds 	}
15991da177e4SLinus Torvalds 
160097216be0SAl Viro 	namespace_lock();
1601719ea2fbSAl Viro 	lock_mount_hash();
16025addc5ddSAl Viro 	event++;
16031da177e4SLinus Torvalds 
160448a066e7SAl Viro 	if (flags & MNT_DETACH) {
160548a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1606e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
160748a066e7SAl Viro 		retval = 0;
160848a066e7SAl Viro 	} else {
1609b54b9be7SAl Viro 		shrink_submounts(mnt);
16101da177e4SLinus Torvalds 		retval = -EBUSY;
161148a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
16121a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1613e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
16141da177e4SLinus Torvalds 			retval = 0;
16151da177e4SLinus Torvalds 		}
161648a066e7SAl Viro 	}
1617719ea2fbSAl Viro 	unlock_mount_hash();
1618e3197d83SAl Viro 	namespace_unlock();
16191da177e4SLinus Torvalds 	return retval;
16201da177e4SLinus Torvalds }
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds /*
162380b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
162480b5dce8SEric W. Biederman  *
162580b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
162680b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
162780b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
162880b5dce8SEric W. Biederman  * leaking them.
162980b5dce8SEric W. Biederman  *
163080b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
163180b5dce8SEric W. Biederman  */
163280b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
163380b5dce8SEric W. Biederman {
163480b5dce8SEric W. Biederman 	struct mountpoint *mp;
163580b5dce8SEric W. Biederman 	struct mount *mnt;
163680b5dce8SEric W. Biederman 
163780b5dce8SEric W. Biederman 	namespace_lock();
16383895dbf8SEric W. Biederman 	lock_mount_hash();
163980b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
1640f53e5797SEric W. Biederman 	if (IS_ERR_OR_NULL(mp))
164180b5dce8SEric W. Biederman 		goto out_unlock;
164280b5dce8SEric W. Biederman 
1643e06b933eSAndrey Ulanov 	event++;
164480b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
164580b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1646ce07d891SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1647fe78fcc8SEric W. Biederman 			hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1648fe78fcc8SEric W. Biederman 			umount_mnt(mnt);
1649ce07d891SEric W. Biederman 		}
1650e0c9c0afSEric W. Biederman 		else umount_tree(mnt, UMOUNT_CONNECTED);
165180b5dce8SEric W. Biederman 	}
165280b5dce8SEric W. Biederman 	put_mountpoint(mp);
165380b5dce8SEric W. Biederman out_unlock:
16543895dbf8SEric W. Biederman 	unlock_mount_hash();
165580b5dce8SEric W. Biederman 	namespace_unlock();
165680b5dce8SEric W. Biederman }
165780b5dce8SEric W. Biederman 
165880b5dce8SEric W. Biederman /*
16599b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
16609b40bc90SAl Viro  */
16619b40bc90SAl Viro static inline bool may_mount(void)
16629b40bc90SAl Viro {
16639b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
16649b40bc90SAl Viro }
16659b40bc90SAl Viro 
16669e8925b6SJeff Layton static inline bool may_mandlock(void)
16679e8925b6SJeff Layton {
16689e8925b6SJeff Layton #ifndef	CONFIG_MANDATORY_FILE_LOCKING
16699e8925b6SJeff Layton 	return false;
16709e8925b6SJeff Layton #endif
167195ace754SEric W. Biederman 	return capable(CAP_SYS_ADMIN);
16729e8925b6SJeff Layton }
16739e8925b6SJeff Layton 
16749b40bc90SAl Viro /*
16751da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
16761da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
16771da177e4SLinus Torvalds  *
16781da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
16791da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
16801da177e4SLinus Torvalds  */
16811da177e4SLinus Torvalds 
16823a18ef5cSDominik Brodowski int ksys_umount(char __user *name, int flags)
16831da177e4SLinus Torvalds {
16842d8f3038SAl Viro 	struct path path;
1685900148dcSAl Viro 	struct mount *mnt;
16861da177e4SLinus Torvalds 	int retval;
1687db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
16881da177e4SLinus Torvalds 
1689db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1690db1f05bbSMiklos Szeredi 		return -EINVAL;
1691db1f05bbSMiklos Szeredi 
16929b40bc90SAl Viro 	if (!may_mount())
16939b40bc90SAl Viro 		return -EPERM;
16949b40bc90SAl Viro 
1695db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1696db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1697db1f05bbSMiklos Szeredi 
1698197df04cSAl Viro 	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
16991da177e4SLinus Torvalds 	if (retval)
17001da177e4SLinus Torvalds 		goto out;
1701900148dcSAl Viro 	mnt = real_mount(path.mnt);
17021da177e4SLinus Torvalds 	retval = -EINVAL;
17032d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
17041da177e4SLinus Torvalds 		goto dput_and_out;
1705143c8c91SAl Viro 	if (!check_mnt(mnt))
17061da177e4SLinus Torvalds 		goto dput_and_out;
17075ff9d8a6SEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
17085ff9d8a6SEric W. Biederman 		goto dput_and_out;
1709b2f5d4dcSEric W. Biederman 	retval = -EPERM;
1710b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1711b2f5d4dcSEric W. Biederman 		goto dput_and_out;
17121da177e4SLinus Torvalds 
1713900148dcSAl Viro 	retval = do_umount(mnt, flags);
17141da177e4SLinus Torvalds dput_and_out:
1715429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
17162d8f3038SAl Viro 	dput(path.dentry);
1717900148dcSAl Viro 	mntput_no_expire(mnt);
17181da177e4SLinus Torvalds out:
17191da177e4SLinus Torvalds 	return retval;
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17223a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
17233a18ef5cSDominik Brodowski {
17243a18ef5cSDominik Brodowski 	return ksys_umount(name, flags);
17253a18ef5cSDominik Brodowski }
17263a18ef5cSDominik Brodowski 
17271da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds /*
17301da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
17311da177e4SLinus Torvalds  */
1732bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
17331da177e4SLinus Torvalds {
17343a18ef5cSDominik Brodowski 	return ksys_umount(name, 0);
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds #endif
17381da177e4SLinus Torvalds 
17394ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
17408823c079SEric W. Biederman {
17414ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1742e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1743e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
17444ce5d2b1SEric W. Biederman }
17454ce5d2b1SEric W. Biederman 
174658be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
174758be2825SAl Viro {
174858be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
174958be2825SAl Viro }
175058be2825SAl Viro 
17514ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
17524ce5d2b1SEric W. Biederman {
17534ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
17544ce5d2b1SEric W. Biederman 	 * mount namespace loop?
17554ce5d2b1SEric W. Biederman 	 */
17564ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
17574ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
17584ce5d2b1SEric W. Biederman 		return false;
17594ce5d2b1SEric W. Biederman 
1760f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
17618823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
17628823c079SEric W. Biederman }
17638823c079SEric W. Biederman 
176487129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
176536341f64SRam Pai 					int flag)
17661da177e4SLinus Torvalds {
176784d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
17681da177e4SLinus Torvalds 
17694ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
17704ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
17714ce5d2b1SEric W. Biederman 
17724ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1773be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
17749676f0c6SRam Pai 
177536341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1776be34d1a3SDavid Howells 	if (IS_ERR(q))
1777be34d1a3SDavid Howells 		return q;
1778be34d1a3SDavid Howells 
1779a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	p = mnt;
17826b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1783315fc83eSAl Viro 		struct mount *s;
17847ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
17851da177e4SLinus Torvalds 			continue;
17861da177e4SLinus Torvalds 
1787909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
17884ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
17894ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
17904ce5d2b1SEric W. Biederman 				s = skip_mnt_tree(s);
17914ce5d2b1SEric W. Biederman 				continue;
17924ce5d2b1SEric W. Biederman 			}
17934ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
17944ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
17959676f0c6SRam Pai 				s = skip_mnt_tree(s);
17969676f0c6SRam Pai 				continue;
17979676f0c6SRam Pai 			}
17980714a533SAl Viro 			while (p != s->mnt_parent) {
17990714a533SAl Viro 				p = p->mnt_parent;
18000714a533SAl Viro 				q = q->mnt_parent;
18011da177e4SLinus Torvalds 			}
180287129cc0SAl Viro 			p = s;
180384d17192SAl Viro 			parent = q;
180487129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1805be34d1a3SDavid Howells 			if (IS_ERR(q))
1806be34d1a3SDavid Howells 				goto out;
1807719ea2fbSAl Viro 			lock_mount_hash();
18081a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
18091064f874SEric W. Biederman 			attach_mnt(q, parent, p->mnt_mp);
1810719ea2fbSAl Viro 			unlock_mount_hash();
18111da177e4SLinus Torvalds 		}
18121da177e4SLinus Torvalds 	}
18131da177e4SLinus Torvalds 	return res;
1814be34d1a3SDavid Howells out:
18151da177e4SLinus Torvalds 	if (res) {
1816719ea2fbSAl Viro 		lock_mount_hash();
1817e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
1818719ea2fbSAl Viro 		unlock_mount_hash();
18191da177e4SLinus Torvalds 	}
1820be34d1a3SDavid Howells 	return q;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
1823be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1824be34d1a3SDavid Howells 
1825ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path)
18268aec0809SAl Viro {
1827cb338d06SAl Viro 	struct mount *tree;
182897216be0SAl Viro 	namespace_lock();
1829cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
1830cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
1831cd4a4017SEric W. Biederman 	else
183287129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
183387129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
1834328e6d90SAl Viro 	namespace_unlock();
1835be34d1a3SDavid Howells 	if (IS_ERR(tree))
183652e220d3SDan Carpenter 		return ERR_CAST(tree);
1837be34d1a3SDavid Howells 	return &tree->mnt;
18388aec0809SAl Viro }
18398aec0809SAl Viro 
18408aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
18418aec0809SAl Viro {
184297216be0SAl Viro 	namespace_lock();
1843719ea2fbSAl Viro 	lock_mount_hash();
1844e819f152SEric W. Biederman 	umount_tree(real_mount(mnt), UMOUNT_SYNC);
1845719ea2fbSAl Viro 	unlock_mount_hash();
18463ab6abeeSAl Viro 	namespace_unlock();
18478aec0809SAl Viro }
18488aec0809SAl Viro 
1849c771d683SMiklos Szeredi /**
1850c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
1851c771d683SMiklos Szeredi  *
1852c771d683SMiklos Szeredi  * This creates a new vfsmount, which will be the clone of @path.  The new will
1853c771d683SMiklos Szeredi  * not be attached anywhere in the namespace and will be private (i.e. changes
1854c771d683SMiklos Szeredi  * to the originating mount won't be propagated into this).
1855c771d683SMiklos Szeredi  *
1856c771d683SMiklos Szeredi  * Release with mntput().
1857c771d683SMiklos Szeredi  */
1858ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path)
1859c771d683SMiklos Szeredi {
1860c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
1861c771d683SMiklos Szeredi 	struct mount *new_mnt;
1862c771d683SMiklos Szeredi 
1863c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
1864c771d683SMiklos Szeredi 		return ERR_PTR(-EINVAL);
1865c771d683SMiklos Szeredi 
1866c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1867c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
1868c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
1869c771d683SMiklos Szeredi 
1870c771d683SMiklos Szeredi 	return &new_mnt->mnt;
1871c771d683SMiklos Szeredi }
1872c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
1873c771d683SMiklos Szeredi 
18741f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
18751f707137SAl Viro 		   struct vfsmount *root)
18761f707137SAl Viro {
18771a4eeaf2SAl Viro 	struct mount *mnt;
18781f707137SAl Viro 	int res = f(root, arg);
18791f707137SAl Viro 	if (res)
18801f707137SAl Viro 		return res;
18811a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
18821a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
18831f707137SAl Viro 		if (res)
18841f707137SAl Viro 			return res;
18851f707137SAl Viro 	}
18861f707137SAl Viro 	return 0;
18871f707137SAl Viro }
18881f707137SAl Viro 
18894b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1890719f5d7fSMiklos Szeredi {
1891315fc83eSAl Viro 	struct mount *p;
1892719f5d7fSMiklos Szeredi 
1893909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1894fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
18954b8b21f4SAl Viro 			mnt_release_group_id(p);
1896719f5d7fSMiklos Szeredi 	}
1897719f5d7fSMiklos Szeredi }
1898719f5d7fSMiklos Szeredi 
18994b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1900719f5d7fSMiklos Szeredi {
1901315fc83eSAl Viro 	struct mount *p;
1902719f5d7fSMiklos Szeredi 
1903909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1904fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
19054b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1906719f5d7fSMiklos Szeredi 			if (err) {
19074b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1908719f5d7fSMiklos Szeredi 				return err;
1909719f5d7fSMiklos Szeredi 			}
1910719f5d7fSMiklos Szeredi 		}
1911719f5d7fSMiklos Szeredi 	}
1912719f5d7fSMiklos Szeredi 
1913719f5d7fSMiklos Szeredi 	return 0;
1914719f5d7fSMiklos Szeredi }
1915719f5d7fSMiklos Szeredi 
1916d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1917d2921684SEric W. Biederman {
1918d2921684SEric W. Biederman 	unsigned int max = READ_ONCE(sysctl_mount_max);
1919d2921684SEric W. Biederman 	unsigned int mounts = 0, old, pending, sum;
1920d2921684SEric W. Biederman 	struct mount *p;
1921d2921684SEric W. Biederman 
1922d2921684SEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt))
1923d2921684SEric W. Biederman 		mounts++;
1924d2921684SEric W. Biederman 
1925d2921684SEric W. Biederman 	old = ns->mounts;
1926d2921684SEric W. Biederman 	pending = ns->pending_mounts;
1927d2921684SEric W. Biederman 	sum = old + pending;
1928d2921684SEric W. Biederman 	if ((old > sum) ||
1929d2921684SEric W. Biederman 	    (pending > sum) ||
1930d2921684SEric W. Biederman 	    (max < sum) ||
1931d2921684SEric W. Biederman 	    (mounts > (max - sum)))
1932d2921684SEric W. Biederman 		return -ENOSPC;
1933d2921684SEric W. Biederman 
1934d2921684SEric W. Biederman 	ns->pending_mounts = pending + mounts;
1935d2921684SEric W. Biederman 	return 0;
1936d2921684SEric W. Biederman }
1937d2921684SEric W. Biederman 
1938b90fa9aeSRam Pai /*
1939b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1940b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
194121444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
194221444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
194321444403SRam Pai  *  		   (done when source_mnt is moved)
1944b90fa9aeSRam Pai  *
1945b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1946b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
19479676f0c6SRam Pai  * ---------------------------------------------------------------------------
1948b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
19499676f0c6SRam Pai  * |**************************************************************************
19509676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
19519676f0c6SRam Pai  * | dest     |               |                |                |            |
19529676f0c6SRam Pai  * |   |      |               |                |                |            |
19539676f0c6SRam Pai  * |   v      |               |                |                |            |
19549676f0c6SRam Pai  * |**************************************************************************
19559676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
19565afe0022SRam Pai  * |          |               |                |                |            |
19579676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
19589676f0c6SRam Pai  * ***************************************************************************
1959b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1960b90fa9aeSRam Pai  * destination mount.
1961b90fa9aeSRam Pai  *
1962b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1963b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1964b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1965b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1966b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1967b90fa9aeSRam Pai  *       mount.
19685afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
19695afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
19705afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
19715afe0022SRam Pai  *       is marked as 'shared and slave'.
19725afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
19735afe0022SRam Pai  * 	 source mount.
19745afe0022SRam Pai  *
19759676f0c6SRam Pai  * ---------------------------------------------------------------------------
197621444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
19779676f0c6SRam Pai  * |**************************************************************************
19789676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
19799676f0c6SRam Pai  * | dest     |               |                |                |            |
19809676f0c6SRam Pai  * |   |      |               |                |                |            |
19819676f0c6SRam Pai  * |   v      |               |                |                |            |
19829676f0c6SRam Pai  * |**************************************************************************
19839676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
19845afe0022SRam Pai  * |          |               |                |                |            |
19859676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
19869676f0c6SRam Pai  * ***************************************************************************
19875afe0022SRam Pai  *
19885afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
19895afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
199021444403SRam Pai  * (+*)  the mount is moved to the destination.
19915afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
19925afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
19935afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
19945afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1995b90fa9aeSRam Pai  *
1996b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1997b90fa9aeSRam Pai  * applied to each mount in the tree.
1998b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1999b90fa9aeSRam Pai  * in allocations.
2000b90fa9aeSRam Pai  */
20010fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
200284d17192SAl Viro 			struct mount *dest_mnt,
200384d17192SAl Viro 			struct mountpoint *dest_mp,
200484d17192SAl Viro 			struct path *parent_path)
2005b90fa9aeSRam Pai {
200638129a13SAl Viro 	HLIST_HEAD(tree_list);
2007d2921684SEric W. Biederman 	struct mnt_namespace *ns = dest_mnt->mnt_ns;
20081064f874SEric W. Biederman 	struct mountpoint *smp;
2009315fc83eSAl Viro 	struct mount *child, *p;
201038129a13SAl Viro 	struct hlist_node *n;
2011719f5d7fSMiklos Szeredi 	int err;
2012b90fa9aeSRam Pai 
20131064f874SEric W. Biederman 	/* Preallocate a mountpoint in case the new mounts need
20141064f874SEric W. Biederman 	 * to be tucked under other mounts.
20151064f874SEric W. Biederman 	 */
20161064f874SEric W. Biederman 	smp = get_mountpoint(source_mnt->mnt.mnt_root);
20171064f874SEric W. Biederman 	if (IS_ERR(smp))
20181064f874SEric W. Biederman 		return PTR_ERR(smp);
20191064f874SEric W. Biederman 
2020d2921684SEric W. Biederman 	/* Is there space to add these mounts to the mount namespace? */
2021d2921684SEric W. Biederman 	if (!parent_path) {
2022d2921684SEric W. Biederman 		err = count_mounts(ns, source_mnt);
2023d2921684SEric W. Biederman 		if (err)
2024d2921684SEric W. Biederman 			goto out;
2025d2921684SEric W. Biederman 	}
2026d2921684SEric W. Biederman 
2027fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
20280fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
2029719f5d7fSMiklos Szeredi 		if (err)
2030719f5d7fSMiklos Szeredi 			goto out;
203184d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
2032f2ebb3a9SAl Viro 		lock_mount_hash();
2033719f5d7fSMiklos Szeredi 		if (err)
2034719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
2035909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
20360f0afb1dSAl Viro 			set_mnt_shared(p);
20370b1b901bSAl Viro 	} else {
20380b1b901bSAl Viro 		lock_mount_hash();
2039b90fa9aeSRam Pai 	}
20401a390689SAl Viro 	if (parent_path) {
20410fb54e50SAl Viro 		detach_mnt(source_mnt, parent_path);
204284d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
2043143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
204421444403SRam Pai 	} else {
204584d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
20461064f874SEric W. Biederman 		commit_tree(source_mnt);
204721444403SRam Pai 	}
2048b90fa9aeSRam Pai 
204938129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
20501d6a32acSAl Viro 		struct mount *q;
205138129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
20521064f874SEric W. Biederman 		q = __lookup_mnt(&child->mnt_parent->mnt,
20531d6a32acSAl Viro 				 child->mnt_mountpoint);
20541064f874SEric W. Biederman 		if (q)
20551064f874SEric W. Biederman 			mnt_change_mountpoint(child, smp, q);
20561064f874SEric W. Biederman 		commit_tree(child);
2057b90fa9aeSRam Pai 	}
20581064f874SEric W. Biederman 	put_mountpoint(smp);
2059719ea2fbSAl Viro 	unlock_mount_hash();
206099b7db7bSNick Piggin 
2061b90fa9aeSRam Pai 	return 0;
2062719f5d7fSMiklos Szeredi 
2063719f5d7fSMiklos Szeredi  out_cleanup_ids:
2064f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
2065f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2066d2921684SEric W. Biederman 		child->mnt_parent->mnt_ns->pending_mounts = 0;
2067e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
2068f2ebb3a9SAl Viro 	}
2069f2ebb3a9SAl Viro 	unlock_mount_hash();
20700fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
2071719f5d7fSMiklos Szeredi  out:
2072d2921684SEric W. Biederman 	ns->pending_mounts = 0;
20731064f874SEric W. Biederman 
20741064f874SEric W. Biederman 	read_seqlock_excl(&mount_lock);
20751064f874SEric W. Biederman 	put_mountpoint(smp);
20761064f874SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
20771064f874SEric W. Biederman 
2078719f5d7fSMiklos Szeredi 	return err;
2079b90fa9aeSRam Pai }
2080b90fa9aeSRam Pai 
208184d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
2082b12cea91SAl Viro {
2083b12cea91SAl Viro 	struct vfsmount *mnt;
208484d17192SAl Viro 	struct dentry *dentry = path->dentry;
2085b12cea91SAl Viro retry:
20865955102cSAl Viro 	inode_lock(dentry->d_inode);
208784d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
20885955102cSAl Viro 		inode_unlock(dentry->d_inode);
208984d17192SAl Viro 		return ERR_PTR(-ENOENT);
2090b12cea91SAl Viro 	}
209197216be0SAl Viro 	namespace_lock();
2092b12cea91SAl Viro 	mnt = lookup_mnt(path);
209384d17192SAl Viro 	if (likely(!mnt)) {
20943895dbf8SEric W. Biederman 		struct mountpoint *mp = get_mountpoint(dentry);
209584d17192SAl Viro 		if (IS_ERR(mp)) {
209697216be0SAl Viro 			namespace_unlock();
20975955102cSAl Viro 			inode_unlock(dentry->d_inode);
209884d17192SAl Viro 			return mp;
209984d17192SAl Viro 		}
210084d17192SAl Viro 		return mp;
210184d17192SAl Viro 	}
210297216be0SAl Viro 	namespace_unlock();
21035955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
2104b12cea91SAl Viro 	path_put(path);
2105b12cea91SAl Viro 	path->mnt = mnt;
210684d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
2107b12cea91SAl Viro 	goto retry;
2108b12cea91SAl Viro }
2109b12cea91SAl Viro 
211084d17192SAl Viro static void unlock_mount(struct mountpoint *where)
2111b12cea91SAl Viro {
211284d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
21133895dbf8SEric W. Biederman 
21143895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
211584d17192SAl Viro 	put_mountpoint(where);
21163895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
21173895dbf8SEric W. Biederman 
2118328e6d90SAl Viro 	namespace_unlock();
21195955102cSAl Viro 	inode_unlock(dentry->d_inode);
2120b12cea91SAl Viro }
2121b12cea91SAl Viro 
212284d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
21231da177e4SLinus Torvalds {
2124e462ec50SDavid Howells 	if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
21251da177e4SLinus Torvalds 		return -EINVAL;
21261da177e4SLinus Torvalds 
2127e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
2128e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
21291da177e4SLinus Torvalds 		return -ENOTDIR;
21301da177e4SLinus Torvalds 
213184d17192SAl Viro 	return attach_recursive_mnt(mnt, p, mp, NULL);
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds /*
21357a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
21367a2e8a8fSValerie Aurora  */
21377a2e8a8fSValerie Aurora 
2138e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags)
21397a2e8a8fSValerie Aurora {
2140e462ec50SDavid Howells 	int type = ms_flags & ~(MS_REC | MS_SILENT);
21417a2e8a8fSValerie Aurora 
21427a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
21437a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
21447a2e8a8fSValerie Aurora 		return 0;
21457a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
21467a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
21477a2e8a8fSValerie Aurora 		return 0;
21487a2e8a8fSValerie Aurora 	return type;
21497a2e8a8fSValerie Aurora }
21507a2e8a8fSValerie Aurora 
21517a2e8a8fSValerie Aurora /*
215207b20889SRam Pai  * recursively change the type of the mountpoint.
215307b20889SRam Pai  */
2154e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags)
215507b20889SRam Pai {
2156315fc83eSAl Viro 	struct mount *m;
21574b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
2158e462ec50SDavid Howells 	int recurse = ms_flags & MS_REC;
21597a2e8a8fSValerie Aurora 	int type;
2160719f5d7fSMiklos Szeredi 	int err = 0;
216107b20889SRam Pai 
21622d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
216307b20889SRam Pai 		return -EINVAL;
216407b20889SRam Pai 
2165e462ec50SDavid Howells 	type = flags_to_propagation_type(ms_flags);
21667a2e8a8fSValerie Aurora 	if (!type)
21677a2e8a8fSValerie Aurora 		return -EINVAL;
21687a2e8a8fSValerie Aurora 
216997216be0SAl Viro 	namespace_lock();
2170719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
2171719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
2172719f5d7fSMiklos Szeredi 		if (err)
2173719f5d7fSMiklos Szeredi 			goto out_unlock;
2174719f5d7fSMiklos Szeredi 	}
2175719f5d7fSMiklos Szeredi 
2176719ea2fbSAl Viro 	lock_mount_hash();
2177909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
21780f0afb1dSAl Viro 		change_mnt_propagation(m, type);
2179719ea2fbSAl Viro 	unlock_mount_hash();
2180719f5d7fSMiklos Szeredi 
2181719f5d7fSMiklos Szeredi  out_unlock:
218297216be0SAl Viro 	namespace_unlock();
2183719f5d7fSMiklos Szeredi 	return err;
218407b20889SRam Pai }
218507b20889SRam Pai 
21865ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
21875ff9d8a6SEric W. Biederman {
21885ff9d8a6SEric W. Biederman 	struct mount *child;
21895ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
21905ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
21915ff9d8a6SEric W. Biederman 			continue;
21925ff9d8a6SEric W. Biederman 
21935ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
21945ff9d8a6SEric W. Biederman 			return true;
21955ff9d8a6SEric W. Biederman 	}
21965ff9d8a6SEric W. Biederman 	return false;
21975ff9d8a6SEric W. Biederman }
21985ff9d8a6SEric W. Biederman 
219907b20889SRam Pai /*
22001da177e4SLinus Torvalds  * do loopback mount.
22011da177e4SLinus Torvalds  */
2202808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
22032dafe1c4SEric Sandeen 				int recurse)
22041da177e4SLinus Torvalds {
22052d92ab3cSAl Viro 	struct path old_path;
220684d17192SAl Viro 	struct mount *mnt = NULL, *old, *parent;
220784d17192SAl Viro 	struct mountpoint *mp;
220857eccb83SAl Viro 	int err;
22091da177e4SLinus Torvalds 	if (!old_name || !*old_name)
22101da177e4SLinus Torvalds 		return -EINVAL;
2211815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
22121da177e4SLinus Torvalds 	if (err)
22131da177e4SLinus Torvalds 		return err;
22141da177e4SLinus Torvalds 
22158823c079SEric W. Biederman 	err = -EINVAL;
22164ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
22178823c079SEric W. Biederman 		goto out;
22188823c079SEric W. Biederman 
221984d17192SAl Viro 	mp = lock_mount(path);
222084d17192SAl Viro 	err = PTR_ERR(mp);
222184d17192SAl Viro 	if (IS_ERR(mp))
22229676f0c6SRam Pai 		goto out;
22239676f0c6SRam Pai 
222487129cc0SAl Viro 	old = real_mount(old_path.mnt);
222584d17192SAl Viro 	parent = real_mount(path->mnt);
222687129cc0SAl Viro 
2227b12cea91SAl Viro 	err = -EINVAL;
2228fc7be130SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2229b12cea91SAl Viro 		goto out2;
2230b12cea91SAl Viro 
2231e149ed2bSAl Viro 	if (!check_mnt(parent))
2232e149ed2bSAl Viro 		goto out2;
2233e149ed2bSAl Viro 
2234e149ed2bSAl Viro 	if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
2235b12cea91SAl Viro 		goto out2;
2236ccd48bc7SAl Viro 
22375ff9d8a6SEric W. Biederman 	if (!recurse && has_locked_children(old, old_path.dentry))
22385ff9d8a6SEric W. Biederman 		goto out2;
22395ff9d8a6SEric W. Biederman 
22401da177e4SLinus Torvalds 	if (recurse)
22414ce5d2b1SEric W. Biederman 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
22421da177e4SLinus Torvalds 	else
224387129cc0SAl Viro 		mnt = clone_mnt(old, old_path.dentry, 0);
22441da177e4SLinus Torvalds 
2245be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2246be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2247e9c5d8a5SAndrey Vagin 		goto out2;
2248be34d1a3SDavid Howells 	}
2249ccd48bc7SAl Viro 
22505ff9d8a6SEric W. Biederman 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
22515ff9d8a6SEric W. Biederman 
225284d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
22531da177e4SLinus Torvalds 	if (err) {
2254719ea2fbSAl Viro 		lock_mount_hash();
2255e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2256719ea2fbSAl Viro 		unlock_mount_hash();
22575b83d2c5SRam Pai 	}
2258b12cea91SAl Viro out2:
225984d17192SAl Viro 	unlock_mount(mp);
2260ccd48bc7SAl Viro out:
22612d92ab3cSAl Viro 	path_put(&old_path);
22621da177e4SLinus Torvalds 	return err;
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
22652e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
22662e4b7fcdSDave Hansen {
22672e4b7fcdSDave Hansen 	int error = 0;
22682e4b7fcdSDave Hansen 	int readonly_request = 0;
22692e4b7fcdSDave Hansen 
22702e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
22712e4b7fcdSDave Hansen 		readonly_request = 1;
22722e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
22732e4b7fcdSDave Hansen 		return 0;
22742e4b7fcdSDave Hansen 
22752e4b7fcdSDave Hansen 	if (readonly_request)
227683adc753SAl Viro 		error = mnt_make_readonly(real_mount(mnt));
22772e4b7fcdSDave Hansen 	else
227883adc753SAl Viro 		__mnt_unmake_readonly(real_mount(mnt));
22792e4b7fcdSDave Hansen 	return error;
22802e4b7fcdSDave Hansen }
22812e4b7fcdSDave Hansen 
22821da177e4SLinus Torvalds /*
22831da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
22841da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
22851da177e4SLinus Torvalds  * on it - tough luck.
22861da177e4SLinus Torvalds  */
2287e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags,
2288e462ec50SDavid Howells 		      int mnt_flags, void *data)
22891da177e4SLinus Torvalds {
22901da177e4SLinus Torvalds 	int err;
22912d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2292143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
22931da177e4SLinus Torvalds 
2294143c8c91SAl Viro 	if (!check_mnt(mnt))
22951da177e4SLinus Torvalds 		return -EINVAL;
22961da177e4SLinus Torvalds 
22972d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
22981da177e4SLinus Torvalds 		return -EINVAL;
22991da177e4SLinus Torvalds 
230007b64558SEric W. Biederman 	/* Don't allow changing of locked mnt flags.
230107b64558SEric W. Biederman 	 *
230207b64558SEric W. Biederman 	 * No locks need to be held here while testing the various
230307b64558SEric W. Biederman 	 * MNT_LOCK flags because those flags can never be cleared
230407b64558SEric W. Biederman 	 * once they are set.
230507b64558SEric W. Biederman 	 */
230607b64558SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
230707b64558SEric W. Biederman 	    !(mnt_flags & MNT_READONLY)) {
230807b64558SEric W. Biederman 		return -EPERM;
230907b64558SEric W. Biederman 	}
23109566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
23119566d674SEric W. Biederman 	    !(mnt_flags & MNT_NODEV)) {
23129566d674SEric W. Biederman 		return -EPERM;
23139566d674SEric W. Biederman 	}
23149566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
23159566d674SEric W. Biederman 	    !(mnt_flags & MNT_NOSUID)) {
23169566d674SEric W. Biederman 		return -EPERM;
23179566d674SEric W. Biederman 	}
23189566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
23199566d674SEric W. Biederman 	    !(mnt_flags & MNT_NOEXEC)) {
23209566d674SEric W. Biederman 		return -EPERM;
23219566d674SEric W. Biederman 	}
23229566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
23239566d674SEric W. Biederman 	    ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
23249566d674SEric W. Biederman 		return -EPERM;
23259566d674SEric W. Biederman 	}
23269566d674SEric W. Biederman 
2327ff36fe2cSEric Paris 	err = security_sb_remount(sb, data);
2328ff36fe2cSEric Paris 	if (err)
2329ff36fe2cSEric Paris 		return err;
2330ff36fe2cSEric Paris 
23311da177e4SLinus Torvalds 	down_write(&sb->s_umount);
2332e462ec50SDavid Howells 	if (ms_flags & MS_BIND)
2333e462ec50SDavid Howells 		err = change_mount_flags(path->mnt, ms_flags);
2334bc6155d1SEric W. Biederman 	else if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
233557eccb83SAl Viro 		err = -EPERM;
23364aa98cf7SAl Viro 	else
2337e462ec50SDavid Howells 		err = do_remount_sb(sb, sb_flags, data, 0);
23387b43a79fSAl Viro 	if (!err) {
2339719ea2fbSAl Viro 		lock_mount_hash();
2340a6138db8SEric W. Biederman 		mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2341143c8c91SAl Viro 		mnt->mnt.mnt_flags = mnt_flags;
2342143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2343719ea2fbSAl Viro 		unlock_mount_hash();
23440e55a7ccSDan Williams 	}
23456339dab8SAl Viro 	up_write(&sb->s_umount);
23461da177e4SLinus Torvalds 	return err;
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds 
2349cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
23509676f0c6SRam Pai {
2351315fc83eSAl Viro 	struct mount *p;
2352909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2353fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
23549676f0c6SRam Pai 			return 1;
23559676f0c6SRam Pai 	}
23569676f0c6SRam Pai 	return 0;
23579676f0c6SRam Pai }
23589676f0c6SRam Pai 
2359808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name)
23601da177e4SLinus Torvalds {
23612d92ab3cSAl Viro 	struct path old_path, parent_path;
2362676da58dSAl Viro 	struct mount *p;
23630fb54e50SAl Viro 	struct mount *old;
236484d17192SAl Viro 	struct mountpoint *mp;
236557eccb83SAl Viro 	int err;
23661da177e4SLinus Torvalds 	if (!old_name || !*old_name)
23671da177e4SLinus Torvalds 		return -EINVAL;
23682d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
23691da177e4SLinus Torvalds 	if (err)
23701da177e4SLinus Torvalds 		return err;
23711da177e4SLinus Torvalds 
237284d17192SAl Viro 	mp = lock_mount(path);
237384d17192SAl Viro 	err = PTR_ERR(mp);
237484d17192SAl Viro 	if (IS_ERR(mp))
2375cc53ce53SDavid Howells 		goto out;
2376cc53ce53SDavid Howells 
2377143c8c91SAl Viro 	old = real_mount(old_path.mnt);
2378fc7be130SAl Viro 	p = real_mount(path->mnt);
2379143c8c91SAl Viro 
23801da177e4SLinus Torvalds 	err = -EINVAL;
2381fc7be130SAl Viro 	if (!check_mnt(p) || !check_mnt(old))
23821da177e4SLinus Torvalds 		goto out1;
23831da177e4SLinus Torvalds 
23845ff9d8a6SEric W. Biederman 	if (old->mnt.mnt_flags & MNT_LOCKED)
23855ff9d8a6SEric W. Biederman 		goto out1;
23865ff9d8a6SEric W. Biederman 
23871da177e4SLinus Torvalds 	err = -EINVAL;
23882d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
238921444403SRam Pai 		goto out1;
23901da177e4SLinus Torvalds 
2391676da58dSAl Viro 	if (!mnt_has_parent(old))
239221444403SRam Pai 		goto out1;
23931da177e4SLinus Torvalds 
2394e36cb0b8SDavid Howells 	if (d_is_dir(path->dentry) !=
2395e36cb0b8SDavid Howells 	      d_is_dir(old_path.dentry))
239621444403SRam Pai 		goto out1;
239721444403SRam Pai 	/*
239821444403SRam Pai 	 * Don't move a mount residing in a shared parent.
239921444403SRam Pai 	 */
2400fc7be130SAl Viro 	if (IS_MNT_SHARED(old->mnt_parent))
240121444403SRam Pai 		goto out1;
24029676f0c6SRam Pai 	/*
24039676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
24049676f0c6SRam Pai 	 * mount which is shared.
24059676f0c6SRam Pai 	 */
2406fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
24079676f0c6SRam Pai 		goto out1;
24081da177e4SLinus Torvalds 	err = -ELOOP;
2409fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2410676da58dSAl Viro 		if (p == old)
241121444403SRam Pai 			goto out1;
24121da177e4SLinus Torvalds 
241384d17192SAl Viro 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
24144ac91378SJan Blunck 	if (err)
241521444403SRam Pai 		goto out1;
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
24181da177e4SLinus Torvalds 	 * automatically */
24196776db3dSAl Viro 	list_del_init(&old->mnt_expire);
24201da177e4SLinus Torvalds out1:
242184d17192SAl Viro 	unlock_mount(mp);
24221da177e4SLinus Torvalds out:
24231da177e4SLinus Torvalds 	if (!err)
24241a390689SAl Viro 		path_put(&parent_path);
24252d92ab3cSAl Viro 	path_put(&old_path);
24261da177e4SLinus Torvalds 	return err;
24271da177e4SLinus Torvalds }
24281da177e4SLinus Torvalds 
24299d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
24309d412a43SAl Viro {
24319d412a43SAl Viro 	int err;
24329d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
24339d412a43SAl Viro 	if (subtype) {
24349d412a43SAl Viro 		subtype++;
24359d412a43SAl Viro 		err = -EINVAL;
24369d412a43SAl Viro 		if (!subtype[0])
24379d412a43SAl Viro 			goto err;
24389d412a43SAl Viro 	} else
24399d412a43SAl Viro 		subtype = "";
24409d412a43SAl Viro 
24419d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
24429d412a43SAl Viro 	err = -ENOMEM;
24439d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
24449d412a43SAl Viro 		goto err;
24459d412a43SAl Viro 	return mnt;
24469d412a43SAl Viro 
24479d412a43SAl Viro  err:
24489d412a43SAl Viro 	mntput(mnt);
24499d412a43SAl Viro 	return ERR_PTR(err);
24509d412a43SAl Viro }
24519d412a43SAl Viro 
24529d412a43SAl Viro /*
24539d412a43SAl Viro  * add a mount into a namespace's mount tree
24549d412a43SAl Viro  */
245595bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
24569d412a43SAl Viro {
245784d17192SAl Viro 	struct mountpoint *mp;
245884d17192SAl Viro 	struct mount *parent;
24599d412a43SAl Viro 	int err;
24609d412a43SAl Viro 
2461f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
24629d412a43SAl Viro 
246384d17192SAl Viro 	mp = lock_mount(path);
246484d17192SAl Viro 	if (IS_ERR(mp))
246584d17192SAl Viro 		return PTR_ERR(mp);
24669d412a43SAl Viro 
246784d17192SAl Viro 	parent = real_mount(path->mnt);
24689d412a43SAl Viro 	err = -EINVAL;
246984d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2470156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2471156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
24729d412a43SAl Viro 			goto unlock;
2473156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
247484d17192SAl Viro 		if (!parent->mnt_ns)
2475156cacb1SAl Viro 			goto unlock;
2476156cacb1SAl Viro 	}
24779d412a43SAl Viro 
24789d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
24799d412a43SAl Viro 	err = -EBUSY;
248095bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
24819d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
24829d412a43SAl Viro 		goto unlock;
24839d412a43SAl Viro 
24849d412a43SAl Viro 	err = -EINVAL;
2485e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
24869d412a43SAl Viro 		goto unlock;
24879d412a43SAl Viro 
248895bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
248984d17192SAl Viro 	err = graft_tree(newmnt, parent, mp);
24909d412a43SAl Viro 
24919d412a43SAl Viro unlock:
249284d17192SAl Viro 	unlock_mount(mp);
24939d412a43SAl Viro 	return err;
24949d412a43SAl Viro }
2495b1e75df4SAl Viro 
24968654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
24971b852bceSEric W. Biederman 
24981da177e4SLinus Torvalds /*
24991da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
25001da177e4SLinus Torvalds  * namespace's tree
25011da177e4SLinus Torvalds  */
2502e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
2503808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
25041da177e4SLinus Torvalds {
25050c55cfc4SEric W. Biederman 	struct file_system_type *type;
25061da177e4SLinus Torvalds 	struct vfsmount *mnt;
250715f9a3f3SAl Viro 	int err;
25081da177e4SLinus Torvalds 
25090c55cfc4SEric W. Biederman 	if (!fstype)
25101da177e4SLinus Torvalds 		return -EINVAL;
25111da177e4SLinus Torvalds 
25120c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
25130c55cfc4SEric W. Biederman 	if (!type)
25140c55cfc4SEric W. Biederman 		return -ENODEV;
25150c55cfc4SEric W. Biederman 
2516e462ec50SDavid Howells 	mnt = vfs_kern_mount(type, sb_flags, name, data);
25170c55cfc4SEric W. Biederman 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
25180c55cfc4SEric W. Biederman 	    !mnt->mnt_sb->s_subtype)
25190c55cfc4SEric W. Biederman 		mnt = fs_set_subtype(mnt, fstype);
25200c55cfc4SEric W. Biederman 
25210c55cfc4SEric W. Biederman 	put_filesystem(type);
25221da177e4SLinus Torvalds 	if (IS_ERR(mnt))
25231da177e4SLinus Torvalds 		return PTR_ERR(mnt);
25241da177e4SLinus Torvalds 
25258654df4eSEric W. Biederman 	if (mount_too_revealing(mnt, &mnt_flags)) {
25268654df4eSEric W. Biederman 		mntput(mnt);
25278654df4eSEric W. Biederman 		return -EPERM;
25288654df4eSEric W. Biederman 	}
25298654df4eSEric W. Biederman 
253095bc5f25SAl Viro 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
253115f9a3f3SAl Viro 	if (err)
253215f9a3f3SAl Viro 		mntput(mnt);
253315f9a3f3SAl Viro 	return err;
25341da177e4SLinus Torvalds }
25351da177e4SLinus Torvalds 
253619a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
253719a167afSAl Viro {
25386776db3dSAl Viro 	struct mount *mnt = real_mount(m);
253919a167afSAl Viro 	int err;
254019a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
254119a167afSAl Viro 	 * expired before we get a chance to add it
254219a167afSAl Viro 	 */
25436776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
254419a167afSAl Viro 
254519a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
254619a167afSAl Viro 	    m->mnt_root == path->dentry) {
2547b1e75df4SAl Viro 		err = -ELOOP;
2548b1e75df4SAl Viro 		goto fail;
254919a167afSAl Viro 	}
255019a167afSAl Viro 
255195bc5f25SAl Viro 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2552b1e75df4SAl Viro 	if (!err)
2553b1e75df4SAl Viro 		return 0;
2554b1e75df4SAl Viro fail:
2555b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
25566776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
255797216be0SAl Viro 		namespace_lock();
25586776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
255997216be0SAl Viro 		namespace_unlock();
256019a167afSAl Viro 	}
2561b1e75df4SAl Viro 	mntput(m);
2562b1e75df4SAl Viro 	mntput(m);
256319a167afSAl Viro 	return err;
256419a167afSAl Viro }
256519a167afSAl Viro 
2566ea5b778aSDavid Howells /**
2567ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2568ea5b778aSDavid Howells  * @mnt: The mount to list.
2569ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2570ea5b778aSDavid Howells  */
2571ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2572ea5b778aSDavid Howells {
257397216be0SAl Viro 	namespace_lock();
2574ea5b778aSDavid Howells 
25756776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2576ea5b778aSDavid Howells 
257797216be0SAl Viro 	namespace_unlock();
2578ea5b778aSDavid Howells }
2579ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2580ea5b778aSDavid Howells 
2581ea5b778aSDavid Howells /*
25821da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
25831da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
25841da177e4SLinus Torvalds  * here
25851da177e4SLinus Torvalds  */
25861da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
25871da177e4SLinus Torvalds {
2588761d5c38SAl Viro 	struct mount *mnt, *next;
25891da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	if (list_empty(mounts))
25921da177e4SLinus Torvalds 		return;
25931da177e4SLinus Torvalds 
259497216be0SAl Viro 	namespace_lock();
2595719ea2fbSAl Viro 	lock_mount_hash();
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
25981da177e4SLinus Torvalds 	 * following criteria:
25991da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
26001da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
26011da177e4SLinus Torvalds 	 *   cleared by mntput())
26021da177e4SLinus Torvalds 	 */
26036776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2604863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
26051ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
26061da177e4SLinus Torvalds 			continue;
26076776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
26081da177e4SLinus Torvalds 	}
2609bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
26106776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2611143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2612e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2613bcc5c7d2SAl Viro 	}
2614719ea2fbSAl Viro 	unlock_mount_hash();
26153ab6abeeSAl Viro 	namespace_unlock();
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds /*
26215528f911STrond Myklebust  * Ripoff of 'select_parent()'
26225528f911STrond Myklebust  *
26235528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
26245528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
26255528f911STrond Myklebust  */
2626692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
26275528f911STrond Myklebust {
2628692afc31SAl Viro 	struct mount *this_parent = parent;
26295528f911STrond Myklebust 	struct list_head *next;
26305528f911STrond Myklebust 	int found = 0;
26315528f911STrond Myklebust 
26325528f911STrond Myklebust repeat:
26336b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
26345528f911STrond Myklebust resume:
26356b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
26365528f911STrond Myklebust 		struct list_head *tmp = next;
26376b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
26385528f911STrond Myklebust 
26395528f911STrond Myklebust 		next = tmp->next;
2640692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
26415528f911STrond Myklebust 			continue;
26425528f911STrond Myklebust 		/*
26435528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
26445528f911STrond Myklebust 		 */
26456b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
26465528f911STrond Myklebust 			this_parent = mnt;
26475528f911STrond Myklebust 			goto repeat;
26485528f911STrond Myklebust 		}
26495528f911STrond Myklebust 
26501ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
26516776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
26525528f911STrond Myklebust 			found++;
26535528f911STrond Myklebust 		}
26545528f911STrond Myklebust 	}
26555528f911STrond Myklebust 	/*
26565528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
26575528f911STrond Myklebust 	 */
26585528f911STrond Myklebust 	if (this_parent != parent) {
26596b41d536SAl Viro 		next = this_parent->mnt_child.next;
26600714a533SAl Viro 		this_parent = this_parent->mnt_parent;
26615528f911STrond Myklebust 		goto resume;
26625528f911STrond Myklebust 	}
26635528f911STrond Myklebust 	return found;
26645528f911STrond Myklebust }
26655528f911STrond Myklebust 
26665528f911STrond Myklebust /*
26675528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
26685528f911STrond Myklebust  * submounts of a specific parent mountpoint
266999b7db7bSNick Piggin  *
267048a066e7SAl Viro  * mount_lock must be held for write
26715528f911STrond Myklebust  */
2672b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
26735528f911STrond Myklebust {
26745528f911STrond Myklebust 	LIST_HEAD(graveyard);
2675761d5c38SAl Viro 	struct mount *m;
26765528f911STrond Myklebust 
26775528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2678c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2679bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2680761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
26816776db3dSAl Viro 						mnt_expire);
2682143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
2683e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2684bcc5c7d2SAl Viro 		}
2685bcc5c7d2SAl Viro 	}
26865528f911STrond Myklebust }
26875528f911STrond Myklebust 
26885528f911STrond Myklebust /*
26891da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
26901da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
26911da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
26921da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
26931da177e4SLinus Torvalds  */
2694b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2695b58fed8bSRam Pai 				 unsigned long n)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	char *t = to;
26981da177e4SLinus Torvalds 	const char __user *f = from;
26991da177e4SLinus Torvalds 	char c;
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
27021da177e4SLinus Torvalds 		return n;
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	while (n) {
27051da177e4SLinus Torvalds 		if (__get_user(c, f)) {
27061da177e4SLinus Torvalds 			memset(t, 0, n);
27071da177e4SLinus Torvalds 			break;
27081da177e4SLinus Torvalds 		}
27091da177e4SLinus Torvalds 		*t++ = c;
27101da177e4SLinus Torvalds 		f++;
27111da177e4SLinus Torvalds 		n--;
27121da177e4SLinus Torvalds 	}
27131da177e4SLinus Torvalds 	return n;
27141da177e4SLinus Torvalds }
27151da177e4SLinus Torvalds 
2716b40ef869SAl Viro void *copy_mount_options(const void __user * data)
27171da177e4SLinus Torvalds {
27181da177e4SLinus Torvalds 	int i;
27191da177e4SLinus Torvalds 	unsigned long size;
2720b40ef869SAl Viro 	char *copy;
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds 	if (!data)
2723b40ef869SAl Viro 		return NULL;
27241da177e4SLinus Torvalds 
2725b40ef869SAl Viro 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2726b40ef869SAl Viro 	if (!copy)
2727b40ef869SAl Viro 		return ERR_PTR(-ENOMEM);
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
27301da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
27311da177e4SLinus Torvalds 	 * the remainder of the page.
27321da177e4SLinus Torvalds 	 */
27331da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
27341da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
27351da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
27361da177e4SLinus Torvalds 		size = PAGE_SIZE;
27371da177e4SLinus Torvalds 
2738b40ef869SAl Viro 	i = size - exact_copy_from_user(copy, data, size);
27391da177e4SLinus Torvalds 	if (!i) {
2740b40ef869SAl Viro 		kfree(copy);
2741b40ef869SAl Viro 		return ERR_PTR(-EFAULT);
27421da177e4SLinus Torvalds 	}
27431da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
2744b40ef869SAl Viro 		memset(copy + i, 0, PAGE_SIZE - i);
2745b40ef869SAl Viro 	return copy;
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
2748b8850d1fSTim Gardner char *copy_mount_string(const void __user *data)
2749eca6f534SVegard Nossum {
2750b8850d1fSTim Gardner 	return data ? strndup_user(data, PAGE_SIZE) : NULL;
2751eca6f534SVegard Nossum }
2752eca6f534SVegard Nossum 
27531da177e4SLinus Torvalds /*
27541da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
27551da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
27561da177e4SLinus Torvalds  *
27571da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
27581da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
27591da177e4SLinus Torvalds  * information (or be NULL).
27601da177e4SLinus Torvalds  *
27611da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
27621da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
27631da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
27641da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
27651da177e4SLinus Torvalds  * and must be discarded.
27661da177e4SLinus Torvalds  */
27675e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name,
2768808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
27691da177e4SLinus Torvalds {
27702d92ab3cSAl Viro 	struct path path;
2771e462ec50SDavid Howells 	unsigned int mnt_flags = 0, sb_flags;
27721da177e4SLinus Torvalds 	int retval = 0;
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	/* Discard magic */
27751da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
27761da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 	/* Basic sanity checks */
27791da177e4SLinus Torvalds 	if (data_page)
27801da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
27811da177e4SLinus Torvalds 
2782e462ec50SDavid Howells 	if (flags & MS_NOUSER)
2783e462ec50SDavid Howells 		return -EINVAL;
2784e462ec50SDavid Howells 
2785a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
27865e6123f3SSeunghun Lee 	retval = user_path(dir_name, &path);
2787a27ab9f2STetsuo Handa 	if (retval)
2788a27ab9f2STetsuo Handa 		return retval;
2789a27ab9f2STetsuo Handa 
2790a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2791a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
27920d5cadb8SAl Viro 	if (!retval && !may_mount())
27930d5cadb8SAl Viro 		retval = -EPERM;
2794e462ec50SDavid Howells 	if (!retval && (flags & SB_MANDLOCK) && !may_mandlock())
27959e8925b6SJeff Layton 		retval = -EPERM;
2796a27ab9f2STetsuo Handa 	if (retval)
2797a27ab9f2STetsuo Handa 		goto dput_out;
2798a27ab9f2STetsuo Handa 
2799613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2800613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
28010a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
28020a1c01c9SMatthew Garrett 
28031da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
28041da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
28051da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
28061da177e4SLinus Torvalds 	if (flags & MS_NODEV)
28071da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
28081da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
28091da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2810fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2811fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2812fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2813fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2814d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2815d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
2816a9e5b732SDavid Howells 	if (flags & MS_RDONLY)
28172e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2818fc33a7bbSChristoph Hellwig 
2819ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
2820ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
2821ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2822ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
2823ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
2824ffbc6f0eSEric W. Biederman 		mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2825ffbc6f0eSEric W. Biederman 	}
2826ffbc6f0eSEric W. Biederman 
2827e462ec50SDavid Howells 	sb_flags = flags & (SB_RDONLY |
2828e462ec50SDavid Howells 			    SB_SYNCHRONOUS |
2829e462ec50SDavid Howells 			    SB_MANDLOCK |
2830e462ec50SDavid Howells 			    SB_DIRSYNC |
2831e462ec50SDavid Howells 			    SB_SILENT |
2832917086ffSMimi Zohar 			    SB_POSIXACL |
2833d7ee9469SMarkus Trippelsdorf 			    SB_LAZYTIME |
2834917086ffSMimi Zohar 			    SB_I_VERSION);
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
2837e462ec50SDavid Howells 		retval = do_remount(&path, flags, sb_flags, mnt_flags,
28381da177e4SLinus Torvalds 				    data_page);
28391da177e4SLinus Torvalds 	else if (flags & MS_BIND)
28402d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
28419676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
28422d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
28431da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
28442d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
28451da177e4SLinus Torvalds 	else
2846e462ec50SDavid Howells 		retval = do_new_mount(&path, type_page, sb_flags, mnt_flags,
28471da177e4SLinus Torvalds 				      dev_name, data_page);
28481da177e4SLinus Torvalds dput_out:
28492d92ab3cSAl Viro 	path_put(&path);
28501da177e4SLinus Torvalds 	return retval;
28511da177e4SLinus Torvalds }
28521da177e4SLinus Torvalds 
2853537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
2854537f7ccbSEric W. Biederman {
2855537f7ccbSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
2856537f7ccbSEric W. Biederman }
2857537f7ccbSEric W. Biederman 
2858537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts)
2859537f7ccbSEric W. Biederman {
2860537f7ccbSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
2861537f7ccbSEric W. Biederman }
2862537f7ccbSEric W. Biederman 
2863771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
2864771b1371SEric W. Biederman {
28656344c433SAl Viro 	ns_free_inum(&ns->ns);
2866537f7ccbSEric W. Biederman 	dec_mnt_namespaces(ns->ucounts);
2867771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
2868771b1371SEric W. Biederman 	kfree(ns);
2869771b1371SEric W. Biederman }
2870771b1371SEric W. Biederman 
28718823c079SEric W. Biederman /*
28728823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
28738823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
28748823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
28758823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
28768823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
28778823c079SEric W. Biederman  */
28788823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
28798823c079SEric W. Biederman 
2880771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2881cf8d2c11STrond Myklebust {
2882cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2883537f7ccbSEric W. Biederman 	struct ucounts *ucounts;
288498f842e6SEric W. Biederman 	int ret;
2885cf8d2c11STrond Myklebust 
2886537f7ccbSEric W. Biederman 	ucounts = inc_mnt_namespaces(user_ns);
2887537f7ccbSEric W. Biederman 	if (!ucounts)
2888df75e774SEric W. Biederman 		return ERR_PTR(-ENOSPC);
2889537f7ccbSEric W. Biederman 
2890cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2891537f7ccbSEric W. Biederman 	if (!new_ns) {
2892537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
2893cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
2894537f7ccbSEric W. Biederman 	}
28956344c433SAl Viro 	ret = ns_alloc_inum(&new_ns->ns);
289698f842e6SEric W. Biederman 	if (ret) {
289798f842e6SEric W. Biederman 		kfree(new_ns);
2898537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
289998f842e6SEric W. Biederman 		return ERR_PTR(ret);
290098f842e6SEric W. Biederman 	}
290133c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
29028823c079SEric W. Biederman 	new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2903cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2904cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2905cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2906cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2907cf8d2c11STrond Myklebust 	new_ns->event = 0;
2908771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
2909537f7ccbSEric W. Biederman 	new_ns->ucounts = ucounts;
2910d2921684SEric W. Biederman 	new_ns->mounts = 0;
2911d2921684SEric W. Biederman 	new_ns->pending_mounts = 0;
2912cf8d2c11STrond Myklebust 	return new_ns;
2913cf8d2c11STrond Myklebust }
2914cf8d2c11STrond Myklebust 
29150766f788SEmese Revfy __latent_entropy
29169559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
29179559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
29181da177e4SLinus Torvalds {
29196b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
29207f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2921315fc83eSAl Viro 	struct mount *p, *q;
29229559f689SAl Viro 	struct mount *old;
2923cb338d06SAl Viro 	struct mount *new;
29247a472ef4SEric W. Biederman 	int copy_flags;
29251da177e4SLinus Torvalds 
29269559f689SAl Viro 	BUG_ON(!ns);
29279559f689SAl Viro 
29289559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
29299559f689SAl Viro 		get_mnt_ns(ns);
29309559f689SAl Viro 		return ns;
29319559f689SAl Viro 	}
29329559f689SAl Viro 
29339559f689SAl Viro 	old = ns->root;
29349559f689SAl Viro 
2935771b1371SEric W. Biederman 	new_ns = alloc_mnt_ns(user_ns);
2936cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2937cf8d2c11STrond Myklebust 		return new_ns;
29381da177e4SLinus Torvalds 
293997216be0SAl Viro 	namespace_lock();
29401da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
29414ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
29429559f689SAl Viro 	if (user_ns != ns->user_ns)
2943132c94e3SEric W. Biederman 		copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
29447a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2945be34d1a3SDavid Howells 	if (IS_ERR(new)) {
2946328e6d90SAl Viro 		namespace_unlock();
2947771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
2948be34d1a3SDavid Howells 		return ERR_CAST(new);
29491da177e4SLinus Torvalds 	}
2950be08d6d2SAl Viro 	new_ns->root = new;
29511a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds 	/*
29541da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
29551da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
29561da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
29571da177e4SLinus Torvalds 	 */
2958909b0a88SAl Viro 	p = old;
2959cb338d06SAl Viro 	q = new;
29601da177e4SLinus Torvalds 	while (p) {
2961143c8c91SAl Viro 		q->mnt_ns = new_ns;
2962d2921684SEric W. Biederman 		new_ns->mounts++;
29639559f689SAl Viro 		if (new_fs) {
29649559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
29659559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
2966315fc83eSAl Viro 				rootmnt = &p->mnt;
29671da177e4SLinus Torvalds 			}
29689559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
29699559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
2970315fc83eSAl Viro 				pwdmnt = &p->mnt;
29711da177e4SLinus Torvalds 			}
29721da177e4SLinus Torvalds 		}
2973909b0a88SAl Viro 		p = next_mnt(p, old);
2974909b0a88SAl Viro 		q = next_mnt(q, new);
29754ce5d2b1SEric W. Biederman 		if (!q)
29764ce5d2b1SEric W. Biederman 			break;
29774ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
29784ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
29791da177e4SLinus Torvalds 	}
2980328e6d90SAl Viro 	namespace_unlock();
29811da177e4SLinus Torvalds 
29821da177e4SLinus Torvalds 	if (rootmnt)
2983f03c6599SAl Viro 		mntput(rootmnt);
29841da177e4SLinus Torvalds 	if (pwdmnt)
2985f03c6599SAl Viro 		mntput(pwdmnt);
29861da177e4SLinus Torvalds 
2987741a2951SJANAK DESAI 	return new_ns;
2988741a2951SJANAK DESAI }
2989741a2951SJANAK DESAI 
2990cf8d2c11STrond Myklebust /**
2991cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2992cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2993cf8d2c11STrond Myklebust  */
29941a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2995cf8d2c11STrond Myklebust {
2996771b1371SEric W. Biederman 	struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2997cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
29981a4eeaf2SAl Viro 		struct mount *mnt = real_mount(m);
29991a4eeaf2SAl Viro 		mnt->mnt_ns = new_ns;
3000be08d6d2SAl Viro 		new_ns->root = mnt;
3001d2921684SEric W. Biederman 		new_ns->mounts++;
3002b1983cd8SAl Viro 		list_add(&mnt->mnt_list, &new_ns->list);
3003c1334495SAl Viro 	} else {
30041a4eeaf2SAl Viro 		mntput(m);
3005cf8d2c11STrond Myklebust 	}
3006cf8d2c11STrond Myklebust 	return new_ns;
3007cf8d2c11STrond Myklebust }
3008cf8d2c11STrond Myklebust 
3009ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
3010ea441d11SAl Viro {
3011ea441d11SAl Viro 	struct mnt_namespace *ns;
3012d31da0f0SAl Viro 	struct super_block *s;
3013ea441d11SAl Viro 	struct path path;
3014ea441d11SAl Viro 	int err;
3015ea441d11SAl Viro 
3016ea441d11SAl Viro 	ns = create_mnt_ns(mnt);
3017ea441d11SAl Viro 	if (IS_ERR(ns))
3018ea441d11SAl Viro 		return ERR_CAST(ns);
3019ea441d11SAl Viro 
3020ea441d11SAl Viro 	err = vfs_path_lookup(mnt->mnt_root, mnt,
3021ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3022ea441d11SAl Viro 
3023ea441d11SAl Viro 	put_mnt_ns(ns);
3024ea441d11SAl Viro 
3025ea441d11SAl Viro 	if (err)
3026ea441d11SAl Viro 		return ERR_PTR(err);
3027ea441d11SAl Viro 
3028ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
3029d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
3030d31da0f0SAl Viro 	atomic_inc(&s->s_active);
3031ea441d11SAl Viro 	mntput(path.mnt);
3032ea441d11SAl Viro 	/* lock the sucker */
3033d31da0f0SAl Viro 	down_write(&s->s_umount);
3034ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
3035ea441d11SAl Viro 	return path.dentry;
3036ea441d11SAl Viro }
3037ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
3038ea441d11SAl Viro 
3039312db1aaSDominik Brodowski int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
3040312db1aaSDominik Brodowski 	       unsigned long flags, void __user *data)
30411da177e4SLinus Torvalds {
3042eca6f534SVegard Nossum 	int ret;
3043eca6f534SVegard Nossum 	char *kernel_type;
3044eca6f534SVegard Nossum 	char *kernel_dev;
3045b40ef869SAl Viro 	void *options;
30461da177e4SLinus Torvalds 
3047b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
3048b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
3049b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
3050eca6f534SVegard Nossum 		goto out_type;
30511da177e4SLinus Torvalds 
3052b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
3053b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
3054b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
3055eca6f534SVegard Nossum 		goto out_dev;
30561da177e4SLinus Torvalds 
3057b40ef869SAl Viro 	options = copy_mount_options(data);
3058b40ef869SAl Viro 	ret = PTR_ERR(options);
3059b40ef869SAl Viro 	if (IS_ERR(options))
3060eca6f534SVegard Nossum 		goto out_data;
30611da177e4SLinus Torvalds 
3062b40ef869SAl Viro 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
3063eca6f534SVegard Nossum 
3064b40ef869SAl Viro 	kfree(options);
3065eca6f534SVegard Nossum out_data:
3066eca6f534SVegard Nossum 	kfree(kernel_dev);
3067eca6f534SVegard Nossum out_dev:
3068eca6f534SVegard Nossum 	kfree(kernel_type);
3069eca6f534SVegard Nossum out_type:
3070eca6f534SVegard Nossum 	return ret;
30711da177e4SLinus Torvalds }
30721da177e4SLinus Torvalds 
3073312db1aaSDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3074312db1aaSDominik Brodowski 		char __user *, type, unsigned long, flags, void __user *, data)
3075312db1aaSDominik Brodowski {
3076312db1aaSDominik Brodowski 	return ksys_mount(dev_name, dir_name, type, flags, data);
3077312db1aaSDominik Brodowski }
3078312db1aaSDominik Brodowski 
30791da177e4SLinus Torvalds /*
3080afac7cbaSAl Viro  * Return true if path is reachable from root
3081afac7cbaSAl Viro  *
308248a066e7SAl Viro  * namespace_sem or mount_lock is held
3083afac7cbaSAl Viro  */
3084643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
3085afac7cbaSAl Viro 			 const struct path *root)
3086afac7cbaSAl Viro {
3087643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
3088a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
30890714a533SAl Viro 		mnt = mnt->mnt_parent;
3090afac7cbaSAl Viro 	}
3091643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
3092afac7cbaSAl Viro }
3093afac7cbaSAl Viro 
3094640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2)
3095afac7cbaSAl Viro {
309625ab4c9bSYaowei Bai 	bool res;
309748a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
3098643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
309948a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
3100afac7cbaSAl Viro 	return res;
3101afac7cbaSAl Viro }
3102afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
3103afac7cbaSAl Viro 
3104afac7cbaSAl Viro /*
31051da177e4SLinus Torvalds  * pivot_root Semantics:
31061da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
31071da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
31081da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
31091da177e4SLinus Torvalds  *
31101da177e4SLinus Torvalds  * Restrictions:
31111da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
31121da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
31131da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
31141da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
31151da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
31161da177e4SLinus Torvalds  *
31174a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
31184a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
31194a0d11faSNeil Brown  * in this situation.
31204a0d11faSNeil Brown  *
31211da177e4SLinus Torvalds  * Notes:
31221da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
31231da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
31241da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
31251da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
31261da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
31271da177e4SLinus Torvalds  *    first.
31281da177e4SLinus Torvalds  */
31293480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
31303480b257SHeiko Carstens 		const char __user *, put_old)
31311da177e4SLinus Torvalds {
31322d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
313384d17192SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt;
313484d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
31351da177e4SLinus Torvalds 	int error;
31361da177e4SLinus Torvalds 
31379b40bc90SAl Viro 	if (!may_mount())
31381da177e4SLinus Torvalds 		return -EPERM;
31391da177e4SLinus Torvalds 
31402d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
31411da177e4SLinus Torvalds 	if (error)
31421da177e4SLinus Torvalds 		goto out0;
31431da177e4SLinus Torvalds 
31442d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
31451da177e4SLinus Torvalds 	if (error)
31461da177e4SLinus Torvalds 		goto out1;
31471da177e4SLinus Torvalds 
31482d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
3149b12cea91SAl Viro 	if (error)
3150b12cea91SAl Viro 		goto out2;
31511da177e4SLinus Torvalds 
3152f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
315384d17192SAl Viro 	old_mp = lock_mount(&old);
315484d17192SAl Viro 	error = PTR_ERR(old_mp);
315584d17192SAl Viro 	if (IS_ERR(old_mp))
3156b12cea91SAl Viro 		goto out3;
3157b12cea91SAl Viro 
31581da177e4SLinus Torvalds 	error = -EINVAL;
3159419148daSAl Viro 	new_mnt = real_mount(new.mnt);
3160419148daSAl Viro 	root_mnt = real_mount(root.mnt);
316184d17192SAl Viro 	old_mnt = real_mount(old.mnt);
316284d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
3163fc7be130SAl Viro 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
3164fc7be130SAl Viro 		IS_MNT_SHARED(root_mnt->mnt_parent))
3165b12cea91SAl Viro 		goto out4;
3166143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
3167b12cea91SAl Viro 		goto out4;
31685ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
31695ff9d8a6SEric W. Biederman 		goto out4;
31701da177e4SLinus Torvalds 	error = -ENOENT;
3171f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
3172b12cea91SAl Viro 		goto out4;
31731da177e4SLinus Torvalds 	error = -EBUSY;
317484d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
3175b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
31761da177e4SLinus Torvalds 	error = -EINVAL;
31778c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
3178b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3179676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
3180b12cea91SAl Viro 		goto out4; /* not attached */
318184d17192SAl Viro 	root_mp = root_mnt->mnt_mp;
31822d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
3183b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3184676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
3185b12cea91SAl Viro 		goto out4; /* not attached */
31864ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
318784d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
3188b12cea91SAl Viro 		goto out4;
31890d082601SEric W. Biederman 	/* make certain new is below the root */
31900d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
31910d082601SEric W. Biederman 		goto out4;
319284d17192SAl Viro 	root_mp->m_count++; /* pin it so it won't go away */
3193719ea2fbSAl Viro 	lock_mount_hash();
3194419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
3195419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
31965ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
31975ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
31985ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
31995ff9d8a6SEric W. Biederman 	}
32004ac91378SJan Blunck 	/* mount old root on put_old */
320184d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
32024ac91378SJan Blunck 	/* mount new_root on / */
320384d17192SAl Viro 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
32046b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
32054fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
32064fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
32073895dbf8SEric W. Biederman 	put_mountpoint(root_mp);
3208719ea2fbSAl Viro 	unlock_mount_hash();
32092d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
32101da177e4SLinus Torvalds 	error = 0;
3211b12cea91SAl Viro out4:
321284d17192SAl Viro 	unlock_mount(old_mp);
3213b12cea91SAl Viro 	if (!error) {
32141a390689SAl Viro 		path_put(&root_parent);
32151a390689SAl Viro 		path_put(&parent_path);
3216b12cea91SAl Viro 	}
3217b12cea91SAl Viro out3:
32188c3ee42eSAl Viro 	path_put(&root);
3219b12cea91SAl Viro out2:
32202d8f3038SAl Viro 	path_put(&old);
32211da177e4SLinus Torvalds out1:
32222d8f3038SAl Viro 	path_put(&new);
32231da177e4SLinus Torvalds out0:
32241da177e4SLinus Torvalds 	return error;
32251da177e4SLinus Torvalds }
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds static void __init init_mount_tree(void)
32281da177e4SLinus Torvalds {
32291da177e4SLinus Torvalds 	struct vfsmount *mnt;
32306b3286edSKirill Korotaev 	struct mnt_namespace *ns;
3231ac748a09SJan Blunck 	struct path root;
32320c55cfc4SEric W. Biederman 	struct file_system_type *type;
32331da177e4SLinus Torvalds 
32340c55cfc4SEric W. Biederman 	type = get_fs_type("rootfs");
32350c55cfc4SEric W. Biederman 	if (!type)
32360c55cfc4SEric W. Biederman 		panic("Can't find rootfs type");
32370c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
32380c55cfc4SEric W. Biederman 	put_filesystem(type);
32391da177e4SLinus Torvalds 	if (IS_ERR(mnt))
32401da177e4SLinus Torvalds 		panic("Can't create rootfs");
3241b3e19d92SNick Piggin 
32423b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
32433b22edc5STrond Myklebust 	if (IS_ERR(ns))
32441da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
32451da177e4SLinus Torvalds 
32466b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
32476b3286edSKirill Korotaev 	get_mnt_ns(ns);
32481da177e4SLinus Torvalds 
3249be08d6d2SAl Viro 	root.mnt = mnt;
3250be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
3251da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
3252ac748a09SJan Blunck 
3253ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
3254ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
32551da177e4SLinus Torvalds }
32561da177e4SLinus Torvalds 
325774bf17cfSDenis Cheng void __init mnt_init(void)
32581da177e4SLinus Torvalds {
325915a67dd8SRandy Dunlap 	int err;
32601da177e4SLinus Torvalds 
32617d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
326220c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
32631da177e4SLinus Torvalds 
32640818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
326538129a13SAl Viro 				sizeof(struct hlist_head),
32660818bf27SAl Viro 				mhash_entries, 19,
32673d375d78SPavel Tatashin 				HASH_ZERO,
32680818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
32690818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
32700818bf27SAl Viro 				sizeof(struct hlist_head),
32710818bf27SAl Viro 				mphash_entries, 19,
32723d375d78SPavel Tatashin 				HASH_ZERO,
32730818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
32741da177e4SLinus Torvalds 
327584d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
32761da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
32771da177e4SLinus Torvalds 
32784b93dc9bSTejun Heo 	kernfs_init();
32794b93dc9bSTejun Heo 
328015a67dd8SRandy Dunlap 	err = sysfs_init();
328115a67dd8SRandy Dunlap 	if (err)
328215a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
32838e24eea7SHarvey Harrison 			__func__, err);
328400d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
328500d26666SGreg Kroah-Hartman 	if (!fs_kobj)
32868e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
32871da177e4SLinus Torvalds 	init_rootfs();
32881da177e4SLinus Torvalds 	init_mount_tree();
32891da177e4SLinus Torvalds }
32901da177e4SLinus Torvalds 
3291616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
32921da177e4SLinus Torvalds {
3293d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
3294616511d0STrond Myklebust 		return;
32957b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
3296771b1371SEric W. Biederman 	free_mnt_ns(ns);
32971da177e4SLinus Torvalds }
32989d412a43SAl Viro 
32999d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
33009d412a43SAl Viro {
3301423e0ab0STim Chen 	struct vfsmount *mnt;
3302e462ec50SDavid Howells 	mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, data);
3303423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
3304423e0ab0STim Chen 		/*
3305423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
3306423e0ab0STim Chen 		 * we unmount before file sys is unregistered
3307423e0ab0STim Chen 		*/
3308f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
3309423e0ab0STim Chen 	}
3310423e0ab0STim Chen 	return mnt;
33119d412a43SAl Viro }
33129d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data);
3313423e0ab0STim Chen 
3314423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
3315423e0ab0STim Chen {
3316423e0ab0STim Chen 	/* release long term mount so mount point can be released */
3317423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
3318f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
331948a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
3320423e0ab0STim Chen 		mntput(mnt);
3321423e0ab0STim Chen 	}
3322423e0ab0STim Chen }
3323423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
332402125a82SAl Viro 
332502125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
332602125a82SAl Viro {
3327143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
332802125a82SAl Viro }
33298823c079SEric W. Biederman 
33303151527eSEric W. Biederman bool current_chrooted(void)
33313151527eSEric W. Biederman {
33323151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
33333151527eSEric W. Biederman 	struct path ns_root;
33343151527eSEric W. Biederman 	struct path fs_root;
33353151527eSEric W. Biederman 	bool chrooted;
33363151527eSEric W. Biederman 
33373151527eSEric W. Biederman 	/* Find the namespace root */
33383151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
33393151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
33403151527eSEric W. Biederman 	path_get(&ns_root);
33413151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
33423151527eSEric W. Biederman 		;
33433151527eSEric W. Biederman 
33443151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
33453151527eSEric W. Biederman 
33463151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
33473151527eSEric W. Biederman 
33483151527eSEric W. Biederman 	path_put(&fs_root);
33493151527eSEric W. Biederman 	path_put(&ns_root);
33503151527eSEric W. Biederman 
33513151527eSEric W. Biederman 	return chrooted;
33523151527eSEric W. Biederman }
33533151527eSEric W. Biederman 
33548654df4eSEric W. Biederman static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
33558654df4eSEric W. Biederman 				int *new_mnt_flags)
335687a8ebd6SEric W. Biederman {
33578c6cf9ccSEric W. Biederman 	int new_flags = *new_mnt_flags;
335887a8ebd6SEric W. Biederman 	struct mount *mnt;
3359e51db735SEric W. Biederman 	bool visible = false;
336087a8ebd6SEric W. Biederman 
336144bb4385SAl Viro 	down_read(&namespace_sem);
336287a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
3363e51db735SEric W. Biederman 		struct mount *child;
336477b1a97dSEric W. Biederman 		int mnt_flags;
336577b1a97dSEric W. Biederman 
33668654df4eSEric W. Biederman 		if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
3367e51db735SEric W. Biederman 			continue;
3368e51db735SEric W. Biederman 
33697e96c1b0SEric W. Biederman 		/* This mount is not fully visible if it's root directory
33707e96c1b0SEric W. Biederman 		 * is not the root directory of the filesystem.
33717e96c1b0SEric W. Biederman 		 */
33727e96c1b0SEric W. Biederman 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
33737e96c1b0SEric W. Biederman 			continue;
33747e96c1b0SEric W. Biederman 
3375a1935c17SEric W. Biederman 		/* A local view of the mount flags */
337677b1a97dSEric W. Biederman 		mnt_flags = mnt->mnt.mnt_flags;
337777b1a97dSEric W. Biederman 
3378695e9df0SEric W. Biederman 		/* Don't miss readonly hidden in the superblock flags */
3379bc98a42cSDavid Howells 		if (sb_rdonly(mnt->mnt.mnt_sb))
3380695e9df0SEric W. Biederman 			mnt_flags |= MNT_LOCK_READONLY;
3381695e9df0SEric W. Biederman 
33828c6cf9ccSEric W. Biederman 		/* Verify the mount flags are equal to or more permissive
33838c6cf9ccSEric W. Biederman 		 * than the proposed new mount.
33848c6cf9ccSEric W. Biederman 		 */
338577b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_READONLY) &&
33868c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_READONLY))
33878c6cf9ccSEric W. Biederman 			continue;
338877b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_ATIME) &&
338977b1a97dSEric W. Biederman 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
33908c6cf9ccSEric W. Biederman 			continue;
33918c6cf9ccSEric W. Biederman 
3392ceeb0e5dSEric W. Biederman 		/* This mount is not fully visible if there are any
3393ceeb0e5dSEric W. Biederman 		 * locked child mounts that cover anything except for
3394ceeb0e5dSEric W. Biederman 		 * empty directories.
3395e51db735SEric W. Biederman 		 */
3396e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3397e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
3398ceeb0e5dSEric W. Biederman 			/* Only worry about locked mounts */
3399d71ed6c9SEric W. Biederman 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
3400ceeb0e5dSEric W. Biederman 				continue;
34017236c85eSEric W. Biederman 			/* Is the directory permanetly empty? */
34027236c85eSEric W. Biederman 			if (!is_empty_dir_inode(inode))
3403e51db735SEric W. Biederman 				goto next;
340487a8ebd6SEric W. Biederman 		}
34058c6cf9ccSEric W. Biederman 		/* Preserve the locked attributes */
340677b1a97dSEric W. Biederman 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
34078c6cf9ccSEric W. Biederman 					       MNT_LOCK_ATIME);
3408e51db735SEric W. Biederman 		visible = true;
3409e51db735SEric W. Biederman 		goto found;
3410e51db735SEric W. Biederman 	next:	;
341187a8ebd6SEric W. Biederman 	}
3412e51db735SEric W. Biederman found:
341344bb4385SAl Viro 	up_read(&namespace_sem);
3414e51db735SEric W. Biederman 	return visible;
341587a8ebd6SEric W. Biederman }
341687a8ebd6SEric W. Biederman 
34178654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
34188654df4eSEric W. Biederman {
3419a1935c17SEric W. Biederman 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
34208654df4eSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
34218654df4eSEric W. Biederman 	unsigned long s_iflags;
34228654df4eSEric W. Biederman 
34238654df4eSEric W. Biederman 	if (ns->user_ns == &init_user_ns)
34248654df4eSEric W. Biederman 		return false;
34258654df4eSEric W. Biederman 
34268654df4eSEric W. Biederman 	/* Can this filesystem be too revealing? */
34278654df4eSEric W. Biederman 	s_iflags = mnt->mnt_sb->s_iflags;
34288654df4eSEric W. Biederman 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
34298654df4eSEric W. Biederman 		return false;
34308654df4eSEric W. Biederman 
3431a1935c17SEric W. Biederman 	if ((s_iflags & required_iflags) != required_iflags) {
3432a1935c17SEric W. Biederman 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3433a1935c17SEric W. Biederman 			  required_iflags);
3434a1935c17SEric W. Biederman 		return true;
3435a1935c17SEric W. Biederman 	}
3436a1935c17SEric W. Biederman 
34378654df4eSEric W. Biederman 	return !mnt_already_visible(ns, mnt, new_mnt_flags);
34388654df4eSEric W. Biederman }
34398654df4eSEric W. Biederman 
3440380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt)
3441380cf5baSAndy Lutomirski {
3442380cf5baSAndy Lutomirski 	/*
3443380cf5baSAndy Lutomirski 	 * Foreign mounts (accessed via fchdir or through /proc
3444380cf5baSAndy Lutomirski 	 * symlinks) are always treated as if they are nosuid.  This
3445380cf5baSAndy Lutomirski 	 * prevents namespaces from trusting potentially unsafe
3446380cf5baSAndy Lutomirski 	 * suid/sgid bits, file caps, or security labels that originate
3447380cf5baSAndy Lutomirski 	 * in other namespaces.
3448380cf5baSAndy Lutomirski 	 */
3449380cf5baSAndy Lutomirski 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3450380cf5baSAndy Lutomirski 	       current_in_userns(mnt->mnt_sb->s_user_ns);
3451380cf5baSAndy Lutomirski }
3452380cf5baSAndy Lutomirski 
345364964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
34548823c079SEric W. Biederman {
345558be2825SAl Viro 	struct ns_common *ns = NULL;
34568823c079SEric W. Biederman 	struct nsproxy *nsproxy;
34578823c079SEric W. Biederman 
3458728dba3aSEric W. Biederman 	task_lock(task);
3459728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
34608823c079SEric W. Biederman 	if (nsproxy) {
346158be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
346258be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
34638823c079SEric W. Biederman 	}
3464728dba3aSEric W. Biederman 	task_unlock(task);
34658823c079SEric W. Biederman 
34668823c079SEric W. Biederman 	return ns;
34678823c079SEric W. Biederman }
34688823c079SEric W. Biederman 
346964964528SAl Viro static void mntns_put(struct ns_common *ns)
34708823c079SEric W. Biederman {
347158be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
34728823c079SEric W. Biederman }
34738823c079SEric W. Biederman 
347464964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
34758823c079SEric W. Biederman {
34768823c079SEric W. Biederman 	struct fs_struct *fs = current->fs;
34774f757f3cSAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
34788823c079SEric W. Biederman 	struct path root;
34794f757f3cSAl Viro 	int err;
34808823c079SEric W. Biederman 
34810c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3482c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3483c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
3484ae11e0f1SZhao Hongjiang 		return -EPERM;
34858823c079SEric W. Biederman 
34868823c079SEric W. Biederman 	if (fs->users != 1)
34878823c079SEric W. Biederman 		return -EINVAL;
34888823c079SEric W. Biederman 
34898823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
34904f757f3cSAl Viro 	old_mnt_ns = nsproxy->mnt_ns;
34918823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
34928823c079SEric W. Biederman 
34938823c079SEric W. Biederman 	/* Find the root */
34944f757f3cSAl Viro 	err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
34954f757f3cSAl Viro 				"/", LOOKUP_DOWN, &root);
34964f757f3cSAl Viro 	if (err) {
34974f757f3cSAl Viro 		/* revert to old namespace */
34984f757f3cSAl Viro 		nsproxy->mnt_ns = old_mnt_ns;
34994f757f3cSAl Viro 		put_mnt_ns(mnt_ns);
35004f757f3cSAl Viro 		return err;
35014f757f3cSAl Viro 	}
35028823c079SEric W. Biederman 
35034068367cSAndrei Vagin 	put_mnt_ns(old_mnt_ns);
35044068367cSAndrei Vagin 
35058823c079SEric W. Biederman 	/* Update the pwd and root */
35068823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
35078823c079SEric W. Biederman 	set_fs_root(fs, &root);
35088823c079SEric W. Biederman 
35098823c079SEric W. Biederman 	path_put(&root);
35108823c079SEric W. Biederman 	return 0;
35118823c079SEric W. Biederman }
35128823c079SEric W. Biederman 
3513bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns)
3514bcac25a5SAndrey Vagin {
3515bcac25a5SAndrey Vagin 	return to_mnt_ns(ns)->user_ns;
3516bcac25a5SAndrey Vagin }
3517bcac25a5SAndrey Vagin 
35188823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
35198823c079SEric W. Biederman 	.name		= "mnt",
35208823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
35218823c079SEric W. Biederman 	.get		= mntns_get,
35228823c079SEric W. Biederman 	.put		= mntns_put,
35238823c079SEric W. Biederman 	.install	= mntns_install,
3524bcac25a5SAndrey Vagin 	.owner		= mntns_owner,
35258823c079SEric W. Biederman };
3526