xref: /openbmc/linux/fs/namespace.c (revision 74e83122)
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>
2657c8a661SMike Rapoport #include <linux/memblock.h>
279ea459e1SAl Viro #include <linux/task_work.h>
289164bb4aSIngo Molnar #include <linux/sched/task.h>
29e262e32dSDavid Howells #include <uapi/linux/mount.h>
309164bb4aSIngo Molnar 
3107b20889SRam Pai #include "pnode.h"
32948730b0SAdrian Bunk #include "internal.h"
331da177e4SLinus Torvalds 
34d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */
35d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000;
36d2921684SEric W. Biederman 
370818bf27SAl Viro static unsigned int m_hash_mask __read_mostly;
380818bf27SAl Viro static unsigned int m_hash_shift __read_mostly;
390818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly;
400818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly;
410818bf27SAl Viro 
420818bf27SAl Viro static __initdata unsigned long mhash_entries;
430818bf27SAl Viro static int __init set_mhash_entries(char *str)
440818bf27SAl Viro {
450818bf27SAl Viro 	if (!str)
460818bf27SAl Viro 		return 0;
470818bf27SAl Viro 	mhash_entries = simple_strtoul(str, &str, 0);
480818bf27SAl Viro 	return 1;
490818bf27SAl Viro }
500818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries);
510818bf27SAl Viro 
520818bf27SAl Viro static __initdata unsigned long mphash_entries;
530818bf27SAl Viro static int __init set_mphash_entries(char *str)
540818bf27SAl Viro {
550818bf27SAl Viro 	if (!str)
560818bf27SAl Viro 		return 0;
570818bf27SAl Viro 	mphash_entries = simple_strtoul(str, &str, 0);
580818bf27SAl Viro 	return 1;
590818bf27SAl Viro }
600818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries);
6113f14b4dSEric Dumazet 
62c7999c36SAl Viro static u64 event;
6373cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
64719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
655addc5ddSAl Viro 
6638129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly;
670818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly;
68e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
6959aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem);
701da177e4SLinus Torvalds 
71f87fd4c2SMiklos Szeredi /* /sys/fs */
7200d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
7300d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
74f87fd4c2SMiklos Szeredi 
7599b7db7bSNick Piggin /*
7699b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
7799b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
7899b7db7bSNick Piggin  * up the tree.
7999b7db7bSNick Piggin  *
8099b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
8199b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
8299b7db7bSNick Piggin  */
8348a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
8499b7db7bSNick Piggin 
8538129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
881da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
890818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
900818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
910818bf27SAl Viro }
920818bf27SAl Viro 
930818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
940818bf27SAl Viro {
950818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
960818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
970818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
100b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
10173cd49ecSMiklos Szeredi {
102169b480eSMatthew Wilcox 	int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
10373cd49ecSMiklos Szeredi 
104169b480eSMatthew Wilcox 	if (res < 0)
10573cd49ecSMiklos Szeredi 		return res;
106169b480eSMatthew Wilcox 	mnt->mnt_id = res;
107169b480eSMatthew Wilcox 	return 0;
10873cd49ecSMiklos Szeredi }
10973cd49ecSMiklos Szeredi 
110b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
11173cd49ecSMiklos Szeredi {
112169b480eSMatthew Wilcox 	ida_free(&mnt_id_ida, mnt->mnt_id);
11373cd49ecSMiklos Szeredi }
11473cd49ecSMiklos Szeredi 
115719f5d7fSMiklos Szeredi /*
116719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
117719f5d7fSMiklos Szeredi  */
1184b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
119719f5d7fSMiklos Szeredi {
120169b480eSMatthew Wilcox 	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
121f21f6220SAl Viro 
122169b480eSMatthew Wilcox 	if (res < 0)
123f21f6220SAl Viro 		return res;
124169b480eSMatthew Wilcox 	mnt->mnt_group_id = res;
125169b480eSMatthew Wilcox 	return 0;
126719f5d7fSMiklos Szeredi }
127719f5d7fSMiklos Szeredi 
128719f5d7fSMiklos Szeredi /*
129719f5d7fSMiklos Szeredi  * Release a peer group ID
130719f5d7fSMiklos Szeredi  */
1314b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
132719f5d7fSMiklos Szeredi {
133169b480eSMatthew Wilcox 	ida_free(&mnt_group_ida, mnt->mnt_group_id);
13415169fe7SAl Viro 	mnt->mnt_group_id = 0;
135719f5d7fSMiklos Szeredi }
136719f5d7fSMiklos Szeredi 
137b3e19d92SNick Piggin /*
138b3e19d92SNick Piggin  * vfsmount lock must be held for read
139b3e19d92SNick Piggin  */
14083adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
141b3e19d92SNick Piggin {
142b3e19d92SNick Piggin #ifdef CONFIG_SMP
14368e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
144b3e19d92SNick Piggin #else
145b3e19d92SNick Piggin 	preempt_disable();
14668e8a9feSAl Viro 	mnt->mnt_count += n;
147b3e19d92SNick Piggin 	preempt_enable();
148b3e19d92SNick Piggin #endif
149b3e19d92SNick Piggin }
150b3e19d92SNick Piggin 
151b3e19d92SNick Piggin /*
152b3e19d92SNick Piggin  * vfsmount lock must be held for write
153b3e19d92SNick Piggin  */
15483adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt)
155b3e19d92SNick Piggin {
156b3e19d92SNick Piggin #ifdef CONFIG_SMP
157f03c6599SAl Viro 	unsigned int count = 0;
158b3e19d92SNick Piggin 	int cpu;
159b3e19d92SNick Piggin 
160b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
16168e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
162b3e19d92SNick Piggin 	}
163b3e19d92SNick Piggin 
164b3e19d92SNick Piggin 	return count;
165b3e19d92SNick Piggin #else
16668e8a9feSAl Viro 	return mnt->mnt_count;
167b3e19d92SNick Piggin #endif
168b3e19d92SNick Piggin }
169b3e19d92SNick Piggin 
17087b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p)
17187b95ce0SAl Viro {
17287b95ce0SAl Viro 	struct mount *m = container_of(p, struct mount, mnt_umount);
17387b95ce0SAl Viro 	dput(m->mnt_ex_mountpoint);
17487b95ce0SAl Viro 	pin_remove(p);
17587b95ce0SAl Viro 	mntput(&m->mnt);
17687b95ce0SAl Viro }
17787b95ce0SAl Viro 
178b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
1791da177e4SLinus Torvalds {
180c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
181c63181e6SAl Viro 	if (mnt) {
18273cd49ecSMiklos Szeredi 		int err;
18373cd49ecSMiklos Szeredi 
184c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
18588b38782SLi Zefan 		if (err)
18688b38782SLi Zefan 			goto out_free_cache;
18788b38782SLi Zefan 
18888b38782SLi Zefan 		if (name) {
189fcc139aeSAndrzej Hajda 			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
190c63181e6SAl Viro 			if (!mnt->mnt_devname)
19188b38782SLi Zefan 				goto out_free_id;
19273cd49ecSMiklos Szeredi 		}
19373cd49ecSMiklos Szeredi 
194b3e19d92SNick Piggin #ifdef CONFIG_SMP
195c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
196c63181e6SAl Viro 		if (!mnt->mnt_pcp)
197b3e19d92SNick Piggin 			goto out_free_devname;
198b3e19d92SNick Piggin 
199c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
200b3e19d92SNick Piggin #else
201c63181e6SAl Viro 		mnt->mnt_count = 1;
202c63181e6SAl Viro 		mnt->mnt_writers = 0;
203b3e19d92SNick Piggin #endif
204b3e19d92SNick Piggin 
20538129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
206c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
207c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
208c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
209c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
210c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
211c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
212c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2130a5eb7c8SEric W. Biederman 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
21499b19d16SEric W. Biederman 		INIT_LIST_HEAD(&mnt->mnt_umounting);
21587b95ce0SAl Viro 		init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
2161da177e4SLinus Torvalds 	}
217c63181e6SAl Viro 	return mnt;
21888b38782SLi Zefan 
219d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
220d3ef3d73Snpiggin@suse.de out_free_devname:
221fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
222d3ef3d73Snpiggin@suse.de #endif
22388b38782SLi Zefan out_free_id:
224c63181e6SAl Viro 	mnt_free_id(mnt);
22588b38782SLi Zefan out_free_cache:
226c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
22788b38782SLi Zefan 	return NULL;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2308366025eSDave Hansen /*
2318366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2328366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2338366025eSDave Hansen  * We must keep track of when those operations start
2348366025eSDave Hansen  * (for permission checks) and when they end, so that
2358366025eSDave Hansen  * we can determine when writes are able to occur to
2368366025eSDave Hansen  * a filesystem.
2378366025eSDave Hansen  */
2383d733633SDave Hansen /*
2393d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2403d733633SDave Hansen  * @mnt: the mount to check for its write status
2413d733633SDave Hansen  *
2423d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2433d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2443d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2453d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2463d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2473d733633SDave Hansen  * r/w.
2483d733633SDave Hansen  */
24943f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt)
2503d733633SDave Hansen {
25143f5e655SDavid Howells 	return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
2523d733633SDave Hansen }
2533d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2543d733633SDave Hansen 
25583adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2563d733633SDave Hansen {
257d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
25868e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
259d3ef3d73Snpiggin@suse.de #else
26068e8a9feSAl Viro 	mnt->mnt_writers++;
261d3ef3d73Snpiggin@suse.de #endif
2623d733633SDave Hansen }
2633d733633SDave Hansen 
26483adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2653d733633SDave Hansen {
266d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
26768e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
268d3ef3d73Snpiggin@suse.de #else
26968e8a9feSAl Viro 	mnt->mnt_writers--;
270d3ef3d73Snpiggin@suse.de #endif
271d3ef3d73Snpiggin@suse.de }
272d3ef3d73Snpiggin@suse.de 
27383adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
274d3ef3d73Snpiggin@suse.de {
275d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
276d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2773d733633SDave Hansen 	int cpu;
2783d733633SDave Hansen 
2793d733633SDave Hansen 	for_each_possible_cpu(cpu) {
28068e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
2813d733633SDave Hansen 	}
2823d733633SDave Hansen 
283d3ef3d73Snpiggin@suse.de 	return count;
284d3ef3d73Snpiggin@suse.de #else
285d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
286d3ef3d73Snpiggin@suse.de #endif
2873d733633SDave Hansen }
2883d733633SDave Hansen 
2894ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
2904ed5e82fSMiklos Szeredi {
2914ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
2924ed5e82fSMiklos Szeredi 		return 1;
2934ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
2944ed5e82fSMiklos Szeredi 	smp_rmb();
2954ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
2964ed5e82fSMiklos Szeredi }
2974ed5e82fSMiklos Szeredi 
2983d733633SDave Hansen /*
299eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
300eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
301eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
302eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
3033d733633SDave Hansen  */
3048366025eSDave Hansen /**
305eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
30683adc753SAl Viro  * @m: the mount on which to take a write
3078366025eSDave Hansen  *
308eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
309eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
310eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
311eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
312eb04c282SJan Kara  * called. This is effectively a refcount.
3138366025eSDave Hansen  */
314eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3158366025eSDave Hansen {
31683adc753SAl Viro 	struct mount *mnt = real_mount(m);
3173d733633SDave Hansen 	int ret = 0;
3183d733633SDave Hansen 
319d3ef3d73Snpiggin@suse.de 	preempt_disable();
320c6653a83SNick Piggin 	mnt_inc_writers(mnt);
321d3ef3d73Snpiggin@suse.de 	/*
322c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
323d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
324d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
325d3ef3d73Snpiggin@suse.de 	 */
326d3ef3d73Snpiggin@suse.de 	smp_mb();
3276aa7de05SMark Rutland 	while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
328d3ef3d73Snpiggin@suse.de 		cpu_relax();
329d3ef3d73Snpiggin@suse.de 	/*
330d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
331d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
332d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
333d3ef3d73Snpiggin@suse.de 	 */
334d3ef3d73Snpiggin@suse.de 	smp_rmb();
3354ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
336c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3373d733633SDave Hansen 		ret = -EROFS;
3383d733633SDave Hansen 	}
339d3ef3d73Snpiggin@suse.de 	preempt_enable();
340eb04c282SJan Kara 
341eb04c282SJan Kara 	return ret;
342eb04c282SJan Kara }
343eb04c282SJan Kara 
344eb04c282SJan Kara /**
345eb04c282SJan Kara  * mnt_want_write - get write access to a mount
346eb04c282SJan Kara  * @m: the mount on which to take a write
347eb04c282SJan Kara  *
348eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
349eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
350eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
351eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
352eb04c282SJan Kara  */
353eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
354eb04c282SJan Kara {
355eb04c282SJan Kara 	int ret;
356eb04c282SJan Kara 
357eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
358eb04c282SJan Kara 	ret = __mnt_want_write(m);
359eb04c282SJan Kara 	if (ret)
360eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3613d733633SDave Hansen 	return ret;
3628366025eSDave Hansen }
3638366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3648366025eSDave Hansen 
3658366025eSDave Hansen /**
36696029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
36796029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
36896029c4eSnpiggin@suse.de  *
36996029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
37096029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
37196029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
37296029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
37396029c4eSnpiggin@suse.de  *
37496029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
37596029c4eSnpiggin@suse.de  * drop the reference.
37696029c4eSnpiggin@suse.de  */
37796029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
37896029c4eSnpiggin@suse.de {
37996029c4eSnpiggin@suse.de 	/* superblock may be r/o */
38096029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
38196029c4eSnpiggin@suse.de 		return -EROFS;
38296029c4eSnpiggin@suse.de 	preempt_disable();
38383adc753SAl Viro 	mnt_inc_writers(real_mount(mnt));
38496029c4eSnpiggin@suse.de 	preempt_enable();
38596029c4eSnpiggin@suse.de 	return 0;
38696029c4eSnpiggin@suse.de }
38796029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
38896029c4eSnpiggin@suse.de 
38996029c4eSnpiggin@suse.de /**
390eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
391eb04c282SJan Kara  * @file: the file who's mount on which to take a write
392eb04c282SJan Kara  *
393eb04c282SJan Kara  * This is like __mnt_want_write, but it takes a file and can
394eb04c282SJan Kara  * do some optimisations if the file is open for write already
395eb04c282SJan Kara  */
396eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
397eb04c282SJan Kara {
39883f936c7SAl Viro 	if (!(file->f_mode & FMODE_WRITER))
399eb04c282SJan Kara 		return __mnt_want_write(file->f_path.mnt);
400eb04c282SJan Kara 	else
401eb04c282SJan Kara 		return mnt_clone_write(file->f_path.mnt);
402eb04c282SJan Kara }
403eb04c282SJan Kara 
404eb04c282SJan Kara /**
4057c6893e3SMiklos Szeredi  * mnt_want_write_file - get write access to a file's mount
4067c6893e3SMiklos Szeredi  * @file: the file who's mount on which to take a write
4077c6893e3SMiklos Szeredi  *
4087c6893e3SMiklos Szeredi  * This is like mnt_want_write, but it takes a file and can
4097c6893e3SMiklos Szeredi  * do some optimisations if the file is open for write already
4107c6893e3SMiklos Szeredi  */
4117c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file)
4127c6893e3SMiklos Szeredi {
4137c6893e3SMiklos Szeredi 	int ret;
4147c6893e3SMiklos Szeredi 
4157c6893e3SMiklos Szeredi 	sb_start_write(file_inode(file)->i_sb);
4167c6893e3SMiklos Szeredi 	ret = __mnt_want_write_file(file);
4177c6893e3SMiklos Szeredi 	if (ret)
4187c6893e3SMiklos Szeredi 		sb_end_write(file_inode(file)->i_sb);
4197c6893e3SMiklos Szeredi 	return ret;
4207c6893e3SMiklos Szeredi }
42196029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
42296029c4eSnpiggin@suse.de 
42396029c4eSnpiggin@suse.de /**
424eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4258366025eSDave Hansen  * @mnt: the mount on which to give up write access
4268366025eSDave Hansen  *
4278366025eSDave Hansen  * Tells the low-level filesystem that we are done
4288366025eSDave Hansen  * performing writes to it.  Must be matched with
429eb04c282SJan Kara  * __mnt_want_write() call above.
4308366025eSDave Hansen  */
431eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4328366025eSDave Hansen {
433d3ef3d73Snpiggin@suse.de 	preempt_disable();
43483adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
435d3ef3d73Snpiggin@suse.de 	preempt_enable();
4368366025eSDave Hansen }
437eb04c282SJan Kara 
438eb04c282SJan Kara /**
439eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
440eb04c282SJan Kara  * @mnt: the mount on which to give up write access
441eb04c282SJan Kara  *
442eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
443eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
444eb04c282SJan Kara  * mnt_want_write() call above.
445eb04c282SJan Kara  */
446eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
447eb04c282SJan Kara {
448eb04c282SJan Kara 	__mnt_drop_write(mnt);
449eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
450eb04c282SJan Kara }
4518366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4528366025eSDave Hansen 
453eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
454eb04c282SJan Kara {
455eb04c282SJan Kara 	__mnt_drop_write(file->f_path.mnt);
456eb04c282SJan Kara }
457eb04c282SJan Kara 
4587c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file)
4597c6893e3SMiklos Szeredi {
460a6795a58SMiklos Szeredi 	__mnt_drop_write_file(file);
4617c6893e3SMiklos Szeredi 	sb_end_write(file_inode(file)->i_sb);
4627c6893e3SMiklos Szeredi }
4632a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4642a79f17eSAl Viro 
46583adc753SAl Viro static int mnt_make_readonly(struct mount *mnt)
4668366025eSDave Hansen {
4673d733633SDave Hansen 	int ret = 0;
4683d733633SDave Hansen 
469719ea2fbSAl Viro 	lock_mount_hash();
47083adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
471d3ef3d73Snpiggin@suse.de 	/*
472d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
473d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
474d3ef3d73Snpiggin@suse.de 	 */
475d3ef3d73Snpiggin@suse.de 	smp_mb();
476d3ef3d73Snpiggin@suse.de 
477d3ef3d73Snpiggin@suse.de 	/*
478d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
479d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
480d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
481d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
482d3ef3d73Snpiggin@suse.de 	 *
483d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
484d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
485d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
486d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
487d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
488d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
489d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
490d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
491d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
492d3ef3d73Snpiggin@suse.de 	 */
493c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
494d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
495d3ef3d73Snpiggin@suse.de 	else
49683adc753SAl Viro 		mnt->mnt.mnt_flags |= MNT_READONLY;
497d3ef3d73Snpiggin@suse.de 	/*
498d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
499d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
500d3ef3d73Snpiggin@suse.de 	 */
501d3ef3d73Snpiggin@suse.de 	smp_wmb();
50283adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
503719ea2fbSAl Viro 	unlock_mount_hash();
5043d733633SDave Hansen 	return ret;
5053d733633SDave Hansen }
5068366025eSDave Hansen 
50743f5e655SDavid Howells static int __mnt_unmake_readonly(struct mount *mnt)
5082e4b7fcdSDave Hansen {
509719ea2fbSAl Viro 	lock_mount_hash();
51083adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
511719ea2fbSAl Viro 	unlock_mount_hash();
51243f5e655SDavid Howells 	return 0;
5132e4b7fcdSDave Hansen }
5142e4b7fcdSDave Hansen 
5154ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5164ed5e82fSMiklos Szeredi {
5174ed5e82fSMiklos Szeredi 	struct mount *mnt;
5184ed5e82fSMiklos Szeredi 	int err = 0;
5194ed5e82fSMiklos Szeredi 
5208e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5218e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5228e8b8796SMiklos Szeredi 		return -EBUSY;
5238e8b8796SMiklos Szeredi 
524719ea2fbSAl Viro 	lock_mount_hash();
5254ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5264ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
5274ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
5284ed5e82fSMiklos Szeredi 			smp_mb();
5294ed5e82fSMiklos Szeredi 			if (mnt_get_writers(mnt) > 0) {
5304ed5e82fSMiklos Szeredi 				err = -EBUSY;
5314ed5e82fSMiklos Szeredi 				break;
5324ed5e82fSMiklos Szeredi 			}
5334ed5e82fSMiklos Szeredi 		}
5344ed5e82fSMiklos Szeredi 	}
5358e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5368e8b8796SMiklos Szeredi 		err = -EBUSY;
5378e8b8796SMiklos Szeredi 
5384ed5e82fSMiklos Szeredi 	if (!err) {
5394ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
5404ed5e82fSMiklos Szeredi 		smp_wmb();
5414ed5e82fSMiklos Szeredi 	}
5424ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5434ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
5444ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
5454ed5e82fSMiklos Szeredi 	}
546719ea2fbSAl Viro 	unlock_mount_hash();
5474ed5e82fSMiklos Szeredi 
5484ed5e82fSMiklos Szeredi 	return err;
5494ed5e82fSMiklos Szeredi }
5504ed5e82fSMiklos Szeredi 
551b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
5521da177e4SLinus Torvalds {
553fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
554d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
55568e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
556d3ef3d73Snpiggin@suse.de #endif
557b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5608ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
5618ffcb32eSDavid Howells {
5628ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
5638ffcb32eSDavid Howells }
5648ffcb32eSDavid Howells 
56548a066e7SAl Viro /* call under rcu_read_lock */
566294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
56748a066e7SAl Viro {
56848a066e7SAl Viro 	struct mount *mnt;
56948a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
570294d71ffSAl Viro 		return 1;
57148a066e7SAl Viro 	if (bastard == NULL)
572294d71ffSAl Viro 		return 0;
57348a066e7SAl Viro 	mnt = real_mount(bastard);
57448a066e7SAl Viro 	mnt_add_count(mnt, 1);
575119e1ef8SAl Viro 	smp_mb();			// see mntput_no_expire()
57648a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
577294d71ffSAl Viro 		return 0;
57848a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
57948a066e7SAl Viro 		mnt_add_count(mnt, -1);
580294d71ffSAl Viro 		return 1;
58148a066e7SAl Viro 	}
582119e1ef8SAl Viro 	lock_mount_hash();
583119e1ef8SAl Viro 	if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
584119e1ef8SAl Viro 		mnt_add_count(mnt, -1);
585119e1ef8SAl Viro 		unlock_mount_hash();
586119e1ef8SAl Viro 		return 1;
587119e1ef8SAl Viro 	}
588119e1ef8SAl Viro 	unlock_mount_hash();
589119e1ef8SAl Viro 	/* caller will mntput() */
590294d71ffSAl Viro 	return -1;
591294d71ffSAl Viro }
592294d71ffSAl Viro 
593294d71ffSAl Viro /* call under rcu_read_lock */
594294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
595294d71ffSAl Viro {
596294d71ffSAl Viro 	int res = __legitimize_mnt(bastard, seq);
597294d71ffSAl Viro 	if (likely(!res))
598294d71ffSAl Viro 		return true;
599294d71ffSAl Viro 	if (unlikely(res < 0)) {
60048a066e7SAl Viro 		rcu_read_unlock();
60148a066e7SAl Viro 		mntput(bastard);
60248a066e7SAl Viro 		rcu_read_lock();
603294d71ffSAl Viro 	}
60448a066e7SAl Viro 	return false;
60548a066e7SAl Viro }
60648a066e7SAl Viro 
6071da177e4SLinus Torvalds /*
608474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
60948a066e7SAl Viro  * call under rcu_read_lock()
6101da177e4SLinus Torvalds  */
611474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6121da177e4SLinus Torvalds {
61338129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
614474279dcSAl Viro 	struct mount *p;
6151da177e4SLinus Torvalds 
61638129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
617474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
618474279dcSAl Viro 			return p;
619474279dcSAl Viro 	return NULL;
6201da177e4SLinus Torvalds }
621474279dcSAl Viro 
622474279dcSAl Viro /*
623f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
624f015f126SDavid Howells  *
625f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
626f015f126SDavid Howells  * following mounts:
627f015f126SDavid Howells  *
628f015f126SDavid Howells  * mount /dev/sda1 /mnt
629f015f126SDavid Howells  * mount /dev/sda2 /mnt
630f015f126SDavid Howells  * mount /dev/sda3 /mnt
631f015f126SDavid Howells  *
632f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
633f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
634f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
635f015f126SDavid Howells  *
636f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
637a05964f3SRam Pai  */
638ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path)
639a05964f3SRam Pai {
640c7105365SAl Viro 	struct mount *child_mnt;
64148a066e7SAl Viro 	struct vfsmount *m;
64248a066e7SAl Viro 	unsigned seq;
64399b7db7bSNick Piggin 
64448a066e7SAl Viro 	rcu_read_lock();
64548a066e7SAl Viro 	do {
64648a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
647474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
64848a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
64948a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
65048a066e7SAl Viro 	rcu_read_unlock();
65148a066e7SAl Viro 	return m;
652a05964f3SRam Pai }
653a05964f3SRam Pai 
6547af1364fSEric W. Biederman /*
6557af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
6567af1364fSEric W. Biederman  *                         current mount namespace.
6577af1364fSEric W. Biederman  *
6587af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
6597af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
6607af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
6617af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
6627af1364fSEric W. Biederman  * is a mountpoint.
6637af1364fSEric W. Biederman  *
6647af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
6657af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
6667af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
6677af1364fSEric W. Biederman  * parent mount.
6687af1364fSEric W. Biederman  */
6697af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
6707af1364fSEric W. Biederman {
6717af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
6727af1364fSEric W. Biederman 	struct mount *mnt;
6737af1364fSEric W. Biederman 	bool is_covered = false;
6747af1364fSEric W. Biederman 
6757af1364fSEric W. Biederman 	if (!d_mountpoint(dentry))
6767af1364fSEric W. Biederman 		goto out;
6777af1364fSEric W. Biederman 
6787af1364fSEric W. Biederman 	down_read(&namespace_sem);
6797af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
6807af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
6817af1364fSEric W. Biederman 		if (is_covered)
6827af1364fSEric W. Biederman 			break;
6837af1364fSEric W. Biederman 	}
6847af1364fSEric W. Biederman 	up_read(&namespace_sem);
6857af1364fSEric W. Biederman out:
6867af1364fSEric W. Biederman 	return is_covered;
6877af1364fSEric W. Biederman }
6887af1364fSEric W. Biederman 
689e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
69084d17192SAl Viro {
6910818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
69284d17192SAl Viro 	struct mountpoint *mp;
69384d17192SAl Viro 
6940818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
69584d17192SAl Viro 		if (mp->m_dentry == dentry) {
69684d17192SAl Viro 			mp->m_count++;
69784d17192SAl Viro 			return mp;
69884d17192SAl Viro 		}
69984d17192SAl Viro 	}
700e2dfa935SEric W. Biederman 	return NULL;
701e2dfa935SEric W. Biederman }
702e2dfa935SEric W. Biederman 
7033895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry)
704e2dfa935SEric W. Biederman {
7053895dbf8SEric W. Biederman 	struct mountpoint *mp, *new = NULL;
706e2dfa935SEric W. Biederman 	int ret;
70784d17192SAl Viro 
7083895dbf8SEric W. Biederman 	if (d_mountpoint(dentry)) {
7091e9c75fbSBenjamin Coddington 		/* might be worth a WARN_ON() */
7101e9c75fbSBenjamin Coddington 		if (d_unlinked(dentry))
7111e9c75fbSBenjamin Coddington 			return ERR_PTR(-ENOENT);
7123895dbf8SEric W. Biederman mountpoint:
7133895dbf8SEric W. Biederman 		read_seqlock_excl(&mount_lock);
7143895dbf8SEric W. Biederman 		mp = lookup_mountpoint(dentry);
7153895dbf8SEric W. Biederman 		read_sequnlock_excl(&mount_lock);
7163895dbf8SEric W. Biederman 		if (mp)
7173895dbf8SEric W. Biederman 			goto done;
71884d17192SAl Viro 	}
719eed81007SMiklos Szeredi 
7203895dbf8SEric W. Biederman 	if (!new)
7213895dbf8SEric W. Biederman 		new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
7223895dbf8SEric W. Biederman 	if (!new)
7233895dbf8SEric W. Biederman 		return ERR_PTR(-ENOMEM);
7243895dbf8SEric W. Biederman 
7253895dbf8SEric W. Biederman 
7263895dbf8SEric W. Biederman 	/* Exactly one processes may set d_mounted */
7273895dbf8SEric W. Biederman 	ret = d_set_mounted(dentry);
7283895dbf8SEric W. Biederman 
7293895dbf8SEric W. Biederman 	/* Someone else set d_mounted? */
7303895dbf8SEric W. Biederman 	if (ret == -EBUSY)
7313895dbf8SEric W. Biederman 		goto mountpoint;
7323895dbf8SEric W. Biederman 
7333895dbf8SEric W. Biederman 	/* The dentry is not available as a mountpoint? */
7343895dbf8SEric W. Biederman 	mp = ERR_PTR(ret);
7353895dbf8SEric W. Biederman 	if (ret)
7363895dbf8SEric W. Biederman 		goto done;
7373895dbf8SEric W. Biederman 
7383895dbf8SEric W. Biederman 	/* Add the new mountpoint to the hash table */
7393895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
7403895dbf8SEric W. Biederman 	new->m_dentry = dentry;
7413895dbf8SEric W. Biederman 	new->m_count = 1;
7423895dbf8SEric W. Biederman 	hlist_add_head(&new->m_hash, mp_hash(dentry));
7433895dbf8SEric W. Biederman 	INIT_HLIST_HEAD(&new->m_list);
7443895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
7453895dbf8SEric W. Biederman 
7463895dbf8SEric W. Biederman 	mp = new;
7473895dbf8SEric W. Biederman 	new = NULL;
7483895dbf8SEric W. Biederman done:
7493895dbf8SEric W. Biederman 	kfree(new);
75084d17192SAl Viro 	return mp;
75184d17192SAl Viro }
75284d17192SAl Viro 
75384d17192SAl Viro static void put_mountpoint(struct mountpoint *mp)
75484d17192SAl Viro {
75584d17192SAl Viro 	if (!--mp->m_count) {
75684d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
7570a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
75884d17192SAl Viro 		spin_lock(&dentry->d_lock);
75984d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
76084d17192SAl Viro 		spin_unlock(&dentry->d_lock);
7610818bf27SAl Viro 		hlist_del(&mp->m_hash);
76284d17192SAl Viro 		kfree(mp);
76384d17192SAl Viro 	}
76484d17192SAl Viro }
76584d17192SAl Viro 
766143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
7671da177e4SLinus Torvalds {
7686b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
77199b7db7bSNick Piggin /*
77299b7db7bSNick Piggin  * vfsmount lock must be held for write
77399b7db7bSNick Piggin  */
7746b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
7755addc5ddSAl Viro {
7765addc5ddSAl Viro 	if (ns) {
7775addc5ddSAl Viro 		ns->event = ++event;
7785addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7795addc5ddSAl Viro 	}
7805addc5ddSAl Viro }
7815addc5ddSAl Viro 
78299b7db7bSNick Piggin /*
78399b7db7bSNick Piggin  * vfsmount lock must be held for write
78499b7db7bSNick Piggin  */
7856b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
7865addc5ddSAl Viro {
7875addc5ddSAl Viro 	if (ns && ns->event != event) {
7885addc5ddSAl Viro 		ns->event = event;
7895addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7905addc5ddSAl Viro 	}
7915addc5ddSAl Viro }
7925addc5ddSAl Viro 
79399b7db7bSNick Piggin /*
79499b7db7bSNick Piggin  * vfsmount lock must be held for write
79599b7db7bSNick Piggin  */
7967bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt)
7971da177e4SLinus Torvalds {
7980714a533SAl Viro 	mnt->mnt_parent = mnt;
799a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8006b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
80138129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
8020a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
80384d17192SAl Viro 	put_mountpoint(mnt->mnt_mp);
80484d17192SAl Viro 	mnt->mnt_mp = NULL;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
80799b7db7bSNick Piggin /*
80899b7db7bSNick Piggin  * vfsmount lock must be held for write
80999b7db7bSNick Piggin  */
8107bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path)
8117bdb11deSEric W. Biederman {
8127bdb11deSEric W. Biederman 	old_path->dentry = mnt->mnt_mountpoint;
8137bdb11deSEric W. Biederman 	old_path->mnt = &mnt->mnt_parent->mnt;
8147bdb11deSEric W. Biederman 	unhash_mnt(mnt);
8157bdb11deSEric W. Biederman }
8167bdb11deSEric W. Biederman 
8177bdb11deSEric W. Biederman /*
8187bdb11deSEric W. Biederman  * vfsmount lock must be held for write
8197bdb11deSEric W. Biederman  */
8206a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt)
8216a46c573SEric W. Biederman {
8226a46c573SEric W. Biederman 	/* old mountpoint will be dropped when we can do that */
8236a46c573SEric W. Biederman 	mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
8246a46c573SEric W. Biederman 	unhash_mnt(mnt);
8256a46c573SEric W. Biederman }
8266a46c573SEric W. Biederman 
8276a46c573SEric W. Biederman /*
8286a46c573SEric W. Biederman  * vfsmount lock must be held for write
8296a46c573SEric W. Biederman  */
83084d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
83184d17192SAl Viro 			struct mountpoint *mp,
83244d964d6SAl Viro 			struct mount *child_mnt)
833b90fa9aeSRam Pai {
83484d17192SAl Viro 	mp->m_count++;
8353a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
83684d17192SAl Viro 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
8373a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
83884d17192SAl Viro 	child_mnt->mnt_mp = mp;
8390a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
840b90fa9aeSRam Pai }
841b90fa9aeSRam Pai 
8421064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent)
8431064f874SEric W. Biederman {
8441064f874SEric W. Biederman 	hlist_add_head_rcu(&mnt->mnt_hash,
8451064f874SEric W. Biederman 			   m_hash(&parent->mnt, mnt->mnt_mountpoint));
8461064f874SEric W. Biederman 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
8471064f874SEric W. Biederman }
8481064f874SEric W. Biederman 
84999b7db7bSNick Piggin /*
85099b7db7bSNick Piggin  * vfsmount lock must be held for write
85199b7db7bSNick Piggin  */
85284d17192SAl Viro static void attach_mnt(struct mount *mnt,
85384d17192SAl Viro 			struct mount *parent,
85484d17192SAl Viro 			struct mountpoint *mp)
8551da177e4SLinus Torvalds {
85684d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
8571064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
858b90fa9aeSRam Pai }
859b90fa9aeSRam Pai 
8601064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
86112a5b529SAl Viro {
8621064f874SEric W. Biederman 	struct mountpoint *old_mp = mnt->mnt_mp;
8631064f874SEric W. Biederman 	struct dentry *old_mountpoint = mnt->mnt_mountpoint;
8641064f874SEric W. Biederman 	struct mount *old_parent = mnt->mnt_parent;
8651064f874SEric W. Biederman 
8661064f874SEric W. Biederman 	list_del_init(&mnt->mnt_child);
8671064f874SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
8681064f874SEric W. Biederman 	hlist_del_init_rcu(&mnt->mnt_hash);
8691064f874SEric W. Biederman 
8701064f874SEric W. Biederman 	attach_mnt(mnt, parent, mp);
8711064f874SEric W. Biederman 
8721064f874SEric W. Biederman 	put_mountpoint(old_mp);
8731064f874SEric W. Biederman 
8741064f874SEric W. Biederman 	/*
8751064f874SEric W. Biederman 	 * Safely avoid even the suggestion this code might sleep or
8761064f874SEric W. Biederman 	 * lock the mount hash by taking advantage of the knowledge that
8771064f874SEric W. Biederman 	 * mnt_change_mountpoint will not release the final reference
8781064f874SEric W. Biederman 	 * to a mountpoint.
8791064f874SEric W. Biederman 	 *
8801064f874SEric W. Biederman 	 * During mounting, the mount passed in as the parent mount will
8811064f874SEric W. Biederman 	 * continue to use the old mountpoint and during unmounting, the
8821064f874SEric W. Biederman 	 * old mountpoint will continue to exist until namespace_unlock,
8831064f874SEric W. Biederman 	 * which happens well after mnt_change_mountpoint.
8841064f874SEric W. Biederman 	 */
8851064f874SEric W. Biederman 	spin_lock(&old_mountpoint->d_lock);
8861064f874SEric W. Biederman 	old_mountpoint->d_lockref.count--;
8871064f874SEric W. Biederman 	spin_unlock(&old_mountpoint->d_lock);
8881064f874SEric W. Biederman 
8891064f874SEric W. Biederman 	mnt_add_count(old_parent, -1);
89012a5b529SAl Viro }
89112a5b529SAl Viro 
892b90fa9aeSRam Pai /*
89399b7db7bSNick Piggin  * vfsmount lock must be held for write
894b90fa9aeSRam Pai  */
8951064f874SEric W. Biederman static void commit_tree(struct mount *mnt)
896b90fa9aeSRam Pai {
8970714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
89883adc753SAl Viro 	struct mount *m;
899b90fa9aeSRam Pai 	LIST_HEAD(head);
900143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
901b90fa9aeSRam Pai 
9020714a533SAl Viro 	BUG_ON(parent == mnt);
903b90fa9aeSRam Pai 
9041a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
905f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
906143c8c91SAl Viro 		m->mnt_ns = n;
907f03c6599SAl Viro 
908b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
909b90fa9aeSRam Pai 
910d2921684SEric W. Biederman 	n->mounts += n->pending_mounts;
911d2921684SEric W. Biederman 	n->pending_mounts = 0;
912d2921684SEric W. Biederman 
9131064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
9146b3286edSKirill Korotaev 	touch_mnt_namespace(n);
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
917909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
9181da177e4SLinus Torvalds {
9196b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
9206b41d536SAl Viro 	if (next == &p->mnt_mounts) {
9211da177e4SLinus Torvalds 		while (1) {
922909b0a88SAl Viro 			if (p == root)
9231da177e4SLinus Torvalds 				return NULL;
9246b41d536SAl Viro 			next = p->mnt_child.next;
9256b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
9261da177e4SLinus Torvalds 				break;
9270714a533SAl Viro 			p = p->mnt_parent;
9281da177e4SLinus Torvalds 		}
9291da177e4SLinus Torvalds 	}
9306b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
933315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
9349676f0c6SRam Pai {
9356b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
9366b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
9376b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
9386b41d536SAl Viro 		prev = p->mnt_mounts.prev;
9399676f0c6SRam Pai 	}
9409676f0c6SRam Pai 	return p;
9419676f0c6SRam Pai }
9429676f0c6SRam Pai 
9439d412a43SAl Viro struct vfsmount *
9449d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
9459d412a43SAl Viro {
946b105e270SAl Viro 	struct mount *mnt;
9479d412a43SAl Viro 	struct dentry *root;
9489d412a43SAl Viro 
9499d412a43SAl Viro 	if (!type)
9509d412a43SAl Viro 		return ERR_PTR(-ENODEV);
9519d412a43SAl Viro 
9529d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
9539d412a43SAl Viro 	if (!mnt)
9549d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
9559d412a43SAl Viro 
956e462ec50SDavid Howells 	if (flags & SB_KERNMOUNT)
957b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
9589d412a43SAl Viro 
9599d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
9609d412a43SAl Viro 	if (IS_ERR(root)) {
9618ffcb32eSDavid Howells 		mnt_free_id(mnt);
9629d412a43SAl Viro 		free_vfsmnt(mnt);
9639d412a43SAl Viro 		return ERR_CAST(root);
9649d412a43SAl Viro 	}
9659d412a43SAl Viro 
966b105e270SAl Viro 	mnt->mnt.mnt_root = root;
967b105e270SAl Viro 	mnt->mnt.mnt_sb = root->d_sb;
968a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
9690714a533SAl Viro 	mnt->mnt_parent = mnt;
970719ea2fbSAl Viro 	lock_mount_hash();
97139f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
972719ea2fbSAl Viro 	unlock_mount_hash();
973b105e270SAl Viro 	return &mnt->mnt;
9749d412a43SAl Viro }
9759d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
9769d412a43SAl Viro 
97793faccbbSEric W. Biederman struct vfsmount *
97893faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
97993faccbbSEric W. Biederman 	     const char *name, void *data)
98093faccbbSEric W. Biederman {
98193faccbbSEric W. Biederman 	/* Until it is worked out how to pass the user namespace
98293faccbbSEric W. Biederman 	 * through from the parent mount to the submount don't support
98393faccbbSEric W. Biederman 	 * unprivileged mounts with submounts.
98493faccbbSEric W. Biederman 	 */
98593faccbbSEric W. Biederman 	if (mountpoint->d_sb->s_user_ns != &init_user_ns)
98693faccbbSEric W. Biederman 		return ERR_PTR(-EPERM);
98793faccbbSEric W. Biederman 
988e462ec50SDavid Howells 	return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
98993faccbbSEric W. Biederman }
99093faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount);
99193faccbbSEric W. Biederman 
99287129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
99336341f64SRam Pai 					int flag)
9941da177e4SLinus Torvalds {
99587129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
996be34d1a3SDavid Howells 	struct mount *mnt;
997be34d1a3SDavid Howells 	int err;
9981da177e4SLinus Torvalds 
999be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
1000be34d1a3SDavid Howells 	if (!mnt)
1001be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
1002be34d1a3SDavid Howells 
10037a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
100415169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
1005719f5d7fSMiklos Szeredi 	else
100615169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
1007719f5d7fSMiklos Szeredi 
100815169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1009be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
1010719f5d7fSMiklos Szeredi 		if (err)
1011719f5d7fSMiklos Szeredi 			goto out_free;
1012719f5d7fSMiklos Szeredi 	}
1013719f5d7fSMiklos Szeredi 
101416a34adbSAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags;
101516a34adbSAl Viro 	mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
10165ff9d8a6SEric W. Biederman 
10171da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1018b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1019b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1020a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
10210714a533SAl Viro 	mnt->mnt_parent = mnt;
1022719ea2fbSAl Viro 	lock_mount_hash();
102339f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1024719ea2fbSAl Viro 	unlock_mount_hash();
1025b90fa9aeSRam Pai 
10267a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
10277a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
10286776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
102932301920SAl Viro 		mnt->mnt_master = old;
1030fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
10318aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1032fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
10336776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1034d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
10356776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1036d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
10375235d448SAl Viro 	} else {
10385235d448SAl Viro 		CLEAR_MNT_SHARED(mnt);
10395afe0022SRam Pai 	}
1040b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
10410f0afb1dSAl Viro 		set_mnt_shared(mnt);
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
10441da177e4SLinus Torvalds 	 * as the original if that was on one */
104536341f64SRam Pai 	if (flag & CL_EXPIRE) {
10466776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
10476776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
10481da177e4SLinus Torvalds 	}
1049be34d1a3SDavid Howells 
1050cb338d06SAl Viro 	return mnt;
1051719f5d7fSMiklos Szeredi 
1052719f5d7fSMiklos Szeredi  out_free:
10538ffcb32eSDavid Howells 	mnt_free_id(mnt);
1054719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1055be34d1a3SDavid Howells 	return ERR_PTR(err);
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10589ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
10599ea459e1SAl Viro {
10609ea459e1SAl Viro 	/*
10619ea459e1SAl Viro 	 * This probably indicates that somebody messed
10629ea459e1SAl Viro 	 * up a mnt_want/drop_write() pair.  If this
10639ea459e1SAl Viro 	 * happens, the filesystem was probably unable
10649ea459e1SAl Viro 	 * to make r/w->r/o transitions.
10659ea459e1SAl Viro 	 */
10669ea459e1SAl Viro 	/*
10679ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
10689ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
10699ea459e1SAl Viro 	 */
10709ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
10719ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
10729ea459e1SAl Viro 		mnt_pin_kill(mnt);
10739ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
10749ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
10759ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
10769ea459e1SAl Viro 	mnt_free_id(mnt);
10779ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
10789ea459e1SAl Viro }
10799ea459e1SAl Viro 
10809ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
10819ea459e1SAl Viro {
10829ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
10839ea459e1SAl Viro }
10849ea459e1SAl Viro 
10859ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
10869ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
10879ea459e1SAl Viro {
10889ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
108929785735SByungchul Park 	struct mount *m, *t;
10909ea459e1SAl Viro 
109129785735SByungchul Park 	llist_for_each_entry_safe(m, t, node, mnt_llist)
109229785735SByungchul Park 		cleanup_mnt(m);
10939ea459e1SAl Viro }
10949ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
10959ea459e1SAl Viro 
1096900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
10977b7b1aceSAl Viro {
109848a066e7SAl Viro 	rcu_read_lock();
10999ea0a46cSAl Viro 	if (likely(READ_ONCE(mnt->mnt_ns))) {
11009ea0a46cSAl Viro 		/*
11019ea0a46cSAl Viro 		 * Since we don't do lock_mount_hash() here,
11029ea0a46cSAl Viro 		 * ->mnt_ns can change under us.  However, if it's
11039ea0a46cSAl Viro 		 * non-NULL, then there's a reference that won't
11049ea0a46cSAl Viro 		 * be dropped until after an RCU delay done after
11059ea0a46cSAl Viro 		 * turning ->mnt_ns NULL.  So if we observe it
11069ea0a46cSAl Viro 		 * non-NULL under rcu_read_lock(), the reference
11079ea0a46cSAl Viro 		 * we are dropping is not the final one.
11089ea0a46cSAl Viro 		 */
1109aa9c0e07SAl Viro 		mnt_add_count(mnt, -1);
111048a066e7SAl Viro 		rcu_read_unlock();
111199b7db7bSNick Piggin 		return;
1112b3e19d92SNick Piggin 	}
1113719ea2fbSAl Viro 	lock_mount_hash();
1114119e1ef8SAl Viro 	/*
1115119e1ef8SAl Viro 	 * make sure that if __legitimize_mnt() has not seen us grab
1116119e1ef8SAl Viro 	 * mount_lock, we'll see their refcount increment here.
1117119e1ef8SAl Viro 	 */
1118119e1ef8SAl Viro 	smp_mb();
11199ea0a46cSAl Viro 	mnt_add_count(mnt, -1);
1120b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
112148a066e7SAl Viro 		rcu_read_unlock();
1122719ea2fbSAl Viro 		unlock_mount_hash();
112399b7db7bSNick Piggin 		return;
112499b7db7bSNick Piggin 	}
112548a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
112648a066e7SAl Viro 		rcu_read_unlock();
112748a066e7SAl Viro 		unlock_mount_hash();
112848a066e7SAl Viro 		return;
112948a066e7SAl Viro 	}
113048a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
113148a066e7SAl Viro 	rcu_read_unlock();
1132962830dfSAndi Kleen 
113339f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1134ce07d891SEric W. Biederman 
1135ce07d891SEric W. Biederman 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1136ce07d891SEric W. Biederman 		struct mount *p, *tmp;
1137ce07d891SEric W. Biederman 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
1138ce07d891SEric W. Biederman 			umount_mnt(p);
1139ce07d891SEric W. Biederman 		}
1140ce07d891SEric W. Biederman 	}
1141719ea2fbSAl Viro 	unlock_mount_hash();
1142649a795aSAl Viro 
11439ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
11449ea459e1SAl Viro 		struct task_struct *task = current;
11459ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
11469ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
11479ea459e1SAl Viro 			if (!task_work_add(task, &mnt->mnt_rcu, true))
11489ea459e1SAl Viro 				return;
11499ea459e1SAl Viro 		}
11509ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
11519ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
11529ea459e1SAl Viro 		return;
11539ea459e1SAl Viro 	}
11549ea459e1SAl Viro 	cleanup_mnt(mnt);
1155b3e19d92SNick Piggin }
1156b3e19d92SNick Piggin 
1157b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1158b3e19d92SNick Piggin {
1159b3e19d92SNick Piggin 	if (mnt) {
1160863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1161b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1162863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1163863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1164863d684fSAl Viro 		mntput_no_expire(m);
1165b3e19d92SNick Piggin 	}
1166b3e19d92SNick Piggin }
1167b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1168b3e19d92SNick Piggin 
1169b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1170b3e19d92SNick Piggin {
1171b3e19d92SNick Piggin 	if (mnt)
117283adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1173b3e19d92SNick Piggin 	return mnt;
1174b3e19d92SNick Piggin }
1175b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1176b3e19d92SNick Piggin 
1177c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current
1178c6609c0aSIan Kent  *                          namespace.
1179c6609c0aSIan Kent  *
1180c6609c0aSIan Kent  *  d_mountpoint() can only be used reliably to establish if a dentry is
1181c6609c0aSIan Kent  *  not mounted in any namespace and that common case is handled inline.
1182c6609c0aSIan Kent  *  d_mountpoint() isn't aware of the possibility there may be multiple
1183c6609c0aSIan Kent  *  mounts using a given dentry in a different namespace. This function
1184c6609c0aSIan Kent  *  checks if the passed in path is a mountpoint rather than the dentry
1185c6609c0aSIan Kent  *  alone.
1186c6609c0aSIan Kent  */
1187c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path)
1188c6609c0aSIan Kent {
1189c6609c0aSIan Kent 	unsigned seq;
1190c6609c0aSIan Kent 	bool res;
1191c6609c0aSIan Kent 
1192c6609c0aSIan Kent 	if (!d_mountpoint(path->dentry))
1193c6609c0aSIan Kent 		return false;
1194c6609c0aSIan Kent 
1195c6609c0aSIan Kent 	rcu_read_lock();
1196c6609c0aSIan Kent 	do {
1197c6609c0aSIan Kent 		seq = read_seqbegin(&mount_lock);
1198c6609c0aSIan Kent 		res = __path_is_mountpoint(path);
1199c6609c0aSIan Kent 	} while (read_seqretry(&mount_lock, seq));
1200c6609c0aSIan Kent 	rcu_read_unlock();
1201c6609c0aSIan Kent 
1202c6609c0aSIan Kent 	return res;
1203c6609c0aSIan Kent }
1204c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint);
1205c6609c0aSIan Kent 
1206ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path)
12077b7b1aceSAl Viro {
12083064c356SAl Viro 	struct mount *p;
12093064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
12103064c356SAl Viro 	if (IS_ERR(p))
12113064c356SAl Viro 		return ERR_CAST(p);
12123064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
12133064c356SAl Viro 	return &p->mnt;
12147b7b1aceSAl Viro }
12151da177e4SLinus Torvalds 
1216a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
12170226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
12181da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
12191da177e4SLinus Torvalds {
1220ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
12211da177e4SLinus Torvalds 
1222390c6843SRam Pai 	down_read(&namespace_sem);
1223c7999c36SAl Viro 	if (p->cached_event == p->ns->event) {
1224c7999c36SAl Viro 		void *v = p->cached_mount;
1225c7999c36SAl Viro 		if (*pos == p->cached_index)
1226c7999c36SAl Viro 			return v;
1227c7999c36SAl Viro 		if (*pos == p->cached_index + 1) {
1228c7999c36SAl Viro 			v = seq_list_next(v, &p->ns->list, &p->cached_index);
1229c7999c36SAl Viro 			return p->cached_mount = v;
1230c7999c36SAl Viro 		}
1231c7999c36SAl Viro 	}
1232c7999c36SAl Viro 
1233c7999c36SAl Viro 	p->cached_event = p->ns->event;
1234c7999c36SAl Viro 	p->cached_mount = seq_list_start(&p->ns->list, *pos);
1235c7999c36SAl Viro 	p->cached_index = *pos;
1236c7999c36SAl Viro 	return p->cached_mount;
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
12401da177e4SLinus Torvalds {
1241ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
1242b0765fb8SPavel Emelianov 
1243c7999c36SAl Viro 	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1244c7999c36SAl Viro 	p->cached_index = *pos;
1245c7999c36SAl Viro 	return p->cached_mount;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
12491da177e4SLinus Torvalds {
1250390c6843SRam Pai 	up_read(&namespace_sem);
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds 
12530226f492SAl Viro static int m_show(struct seq_file *m, void *v)
12549f5596afSAl Viro {
1255ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
12561a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
12570226f492SAl Viro 	return p->show(m, &r->mnt);
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
1260a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
12611da177e4SLinus Torvalds 	.start	= m_start,
12621da177e4SLinus Torvalds 	.next	= m_next,
12631da177e4SLinus Torvalds 	.stop	= m_stop,
12640226f492SAl Viro 	.show	= m_show,
1265b4629fe2SChuck Lever };
1266a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1267b4629fe2SChuck Lever 
12681da177e4SLinus Torvalds /**
12691da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
12701da177e4SLinus Torvalds  * @mnt: root of mount tree
12711da177e4SLinus Torvalds  *
12721da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
12731da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
12741da177e4SLinus Torvalds  * busy.
12751da177e4SLinus Torvalds  */
1276909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
12771da177e4SLinus Torvalds {
1278909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
127936341f64SRam Pai 	int actual_refs = 0;
128036341f64SRam Pai 	int minimum_refs = 0;
1281315fc83eSAl Viro 	struct mount *p;
1282909b0a88SAl Viro 	BUG_ON(!m);
12831da177e4SLinus Torvalds 
1284b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1285719ea2fbSAl Viro 	lock_mount_hash();
1286909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
128783adc753SAl Viro 		actual_refs += mnt_get_count(p);
12881da177e4SLinus Torvalds 		minimum_refs += 2;
12891da177e4SLinus Torvalds 	}
1290719ea2fbSAl Viro 	unlock_mount_hash();
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
12931da177e4SLinus Torvalds 		return 0;
1294e3474a8eSIan Kent 
1295e3474a8eSIan Kent 	return 1;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds /**
13011da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
13021da177e4SLinus Torvalds  * @mnt: root of mount
13031da177e4SLinus Torvalds  *
13041da177e4SLinus Torvalds  * This is called to check if a mount point has any
13051da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
13061da177e4SLinus Torvalds  * mount has sub mounts this will return busy
13071da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
13081da177e4SLinus Torvalds  *
13091da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
13101da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
13111da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
13121da177e4SLinus Torvalds  */
13131da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
13141da177e4SLinus Torvalds {
1315e3474a8eSIan Kent 	int ret = 1;
13168ad08d8aSAl Viro 	down_read(&namespace_sem);
1317719ea2fbSAl Viro 	lock_mount_hash();
13181ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1319e3474a8eSIan Kent 		ret = 0;
1320719ea2fbSAl Viro 	unlock_mount_hash();
13218ad08d8aSAl Viro 	up_read(&namespace_sem);
1322a05964f3SRam Pai 	return ret;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
13261da177e4SLinus Torvalds 
132738129a13SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
1328e3197d83SAl Viro 
132997216be0SAl Viro static void namespace_unlock(void)
13301da177e4SLinus Torvalds {
1331a3b3c562SEric W. Biederman 	struct hlist_head head;
133297216be0SAl Viro 
1333a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
1334a3b3c562SEric W. Biederman 
133597216be0SAl Viro 	up_write(&namespace_sem);
1336a3b3c562SEric W. Biederman 
1337a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
133897216be0SAl Viro 		return;
133997216be0SAl Viro 
134022cb7405SNeilBrown 	synchronize_rcu_expedited();
134148a066e7SAl Viro 
134287b95ce0SAl Viro 	group_pin_kill(&head);
134370fbcdf4SRam Pai }
134470fbcdf4SRam Pai 
134597216be0SAl Viro static inline void namespace_lock(void)
1346e3197d83SAl Viro {
134797216be0SAl Viro 	down_write(&namespace_sem);
1348e3197d83SAl Viro }
1349e3197d83SAl Viro 
1350e819f152SEric W. Biederman enum umount_tree_flags {
1351e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1352e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1353e0c9c0afSEric W. Biederman 	UMOUNT_CONNECTED = 4,
1354e819f152SEric W. Biederman };
1355f2d0a123SEric W. Biederman 
1356f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1357f2d0a123SEric W. Biederman {
1358f2d0a123SEric W. Biederman 	/* Leaving mounts connected is only valid for lazy umounts */
1359f2d0a123SEric W. Biederman 	if (how & UMOUNT_SYNC)
1360f2d0a123SEric W. Biederman 		return true;
1361f2d0a123SEric W. Biederman 
1362f2d0a123SEric W. Biederman 	/* A mount without a parent has nothing to be connected to */
1363f2d0a123SEric W. Biederman 	if (!mnt_has_parent(mnt))
1364f2d0a123SEric W. Biederman 		return true;
1365f2d0a123SEric W. Biederman 
1366f2d0a123SEric W. Biederman 	/* Because the reference counting rules change when mounts are
1367f2d0a123SEric W. Biederman 	 * unmounted and connected, umounted mounts may not be
1368f2d0a123SEric W. Biederman 	 * connected to mounted mounts.
1369f2d0a123SEric W. Biederman 	 */
1370f2d0a123SEric W. Biederman 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1371f2d0a123SEric W. Biederman 		return true;
1372f2d0a123SEric W. Biederman 
1373f2d0a123SEric W. Biederman 	/* Has it been requested that the mount remain connected? */
1374f2d0a123SEric W. Biederman 	if (how & UMOUNT_CONNECTED)
1375f2d0a123SEric W. Biederman 		return false;
1376f2d0a123SEric W. Biederman 
1377f2d0a123SEric W. Biederman 	/* Is the mount locked such that it needs to remain connected? */
1378f2d0a123SEric W. Biederman 	if (IS_MNT_LOCKED(mnt))
1379f2d0a123SEric W. Biederman 		return false;
1380f2d0a123SEric W. Biederman 
1381f2d0a123SEric W. Biederman 	/* By default disconnect the mount */
1382f2d0a123SEric W. Biederman 	return true;
1383f2d0a123SEric W. Biederman }
1384f2d0a123SEric W. Biederman 
138599b7db7bSNick Piggin /*
138648a066e7SAl Viro  * mount_lock must be held
138799b7db7bSNick Piggin  * namespace_sem must be held for write
138899b7db7bSNick Piggin  */
1389e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
139070fbcdf4SRam Pai {
1391c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1392315fc83eSAl Viro 	struct mount *p;
139370fbcdf4SRam Pai 
13945d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
13955d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
13965d88457eSEric W. Biederman 
1397c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1398590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1399590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1400c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1401590ce4bcSEric W. Biederman 	}
1402c003b26fSEric W. Biederman 
1403411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1404c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1405c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
140638129a13SAl Viro 	}
140770fbcdf4SRam Pai 
1408c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1409e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14107b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1411a05964f3SRam Pai 
1412c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1413d2921684SEric W. Biederman 		struct mnt_namespace *ns;
1414ce07d891SEric W. Biederman 		bool disconnect;
1415c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
14166776db3dSAl Viro 		list_del_init(&p->mnt_expire);
14171a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1418d2921684SEric W. Biederman 		ns = p->mnt_ns;
1419d2921684SEric W. Biederman 		if (ns) {
1420d2921684SEric W. Biederman 			ns->mounts--;
1421d2921684SEric W. Biederman 			__touch_mnt_namespace(ns);
1422d2921684SEric W. Biederman 		}
142363d37a84SAl Viro 		p->mnt_ns = NULL;
1424e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
142548a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
142687b95ce0SAl Viro 
1427f2d0a123SEric W. Biederman 		disconnect = disconnect_mount(p, how);
1428ce07d891SEric W. Biederman 
1429ce07d891SEric W. Biederman 		pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1430ce07d891SEric W. Biederman 				 disconnect ? &unmounted : NULL);
1431676da58dSAl Viro 		if (mnt_has_parent(p)) {
143281b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
1433ce07d891SEric W. Biederman 			if (!disconnect) {
1434ce07d891SEric W. Biederman 				/* Don't forget about p */
1435ce07d891SEric W. Biederman 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1436ce07d891SEric W. Biederman 			} else {
14376a46c573SEric W. Biederman 				umount_mnt(p);
14387c4b93d8SAl Viro 			}
1439ce07d891SEric W. Biederman 		}
14400f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
144138129a13SAl Viro 	}
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
1444b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1445c35038beSAl Viro 
14461ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
14471da177e4SLinus Torvalds {
14481ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
14491da177e4SLinus Torvalds 	int retval;
14501da177e4SLinus Torvalds 
14511ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
14521da177e4SLinus Torvalds 	if (retval)
14531da177e4SLinus Torvalds 		return retval;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	/*
14561da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
14571da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
14581da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
14591da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
14601da177e4SLinus Torvalds 	 */
14611da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
14621ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
14631da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
14641da177e4SLinus Torvalds 			return -EINVAL;
14651da177e4SLinus Torvalds 
1466b3e19d92SNick Piggin 		/*
1467b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1468b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1469b3e19d92SNick Piggin 		 */
1470719ea2fbSAl Viro 		lock_mount_hash();
147183adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1472719ea2fbSAl Viro 			unlock_mount_hash();
14731da177e4SLinus Torvalds 			return -EBUSY;
1474b3e19d92SNick Piggin 		}
1475719ea2fbSAl Viro 		unlock_mount_hash();
14761da177e4SLinus Torvalds 
1477863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
14781da177e4SLinus Torvalds 			return -EAGAIN;
14791da177e4SLinus Torvalds 	}
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	/*
14821da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
14831da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
14841da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
14851da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
14861da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
14871da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
14881da177e4SLinus Torvalds 	 * about for the moment.
14891da177e4SLinus Torvalds 	 */
14901da177e4SLinus Torvalds 
149142faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
149242faad99SAl Viro 		sb->s_op->umount_begin(sb);
149342faad99SAl Viro 	}
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	/*
14961da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
14971da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
14981da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
14991da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
15001da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
15011da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
15021da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
15031da177e4SLinus Torvalds 	 */
15041ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
15051da177e4SLinus Torvalds 		/*
15061da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
15071da177e4SLinus Torvalds 		 * we just try to remount it readonly.
15081da177e4SLinus Torvalds 		 */
1509bc6155d1SEric W. Biederman 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1510a1480dccSAndy Lutomirski 			return -EPERM;
15111da177e4SLinus Torvalds 		down_write(&sb->s_umount);
1512bc98a42cSDavid Howells 		if (!sb_rdonly(sb))
1513e462ec50SDavid Howells 			retval = do_remount_sb(sb, SB_RDONLY, NULL, 0);
15141da177e4SLinus Torvalds 		up_write(&sb->s_umount);
15151da177e4SLinus Torvalds 		return retval;
15161da177e4SLinus Torvalds 	}
15171da177e4SLinus Torvalds 
151897216be0SAl Viro 	namespace_lock();
1519719ea2fbSAl Viro 	lock_mount_hash();
15201da177e4SLinus Torvalds 
152125d202edSEric W. Biederman 	/* Recheck MNT_LOCKED with the locks held */
152225d202edSEric W. Biederman 	retval = -EINVAL;
152325d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
152425d202edSEric W. Biederman 		goto out;
152525d202edSEric W. Biederman 
152625d202edSEric W. Biederman 	event++;
152748a066e7SAl Viro 	if (flags & MNT_DETACH) {
152848a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1529e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
153048a066e7SAl Viro 		retval = 0;
153148a066e7SAl Viro 	} else {
1532b54b9be7SAl Viro 		shrink_submounts(mnt);
15331da177e4SLinus Torvalds 		retval = -EBUSY;
153448a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
15351a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1536e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
15371da177e4SLinus Torvalds 			retval = 0;
15381da177e4SLinus Torvalds 		}
153948a066e7SAl Viro 	}
154025d202edSEric W. Biederman out:
1541719ea2fbSAl Viro 	unlock_mount_hash();
1542e3197d83SAl Viro 	namespace_unlock();
15431da177e4SLinus Torvalds 	return retval;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds /*
154780b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
154880b5dce8SEric W. Biederman  *
154980b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
155080b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
155180b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
155280b5dce8SEric W. Biederman  * leaking them.
155380b5dce8SEric W. Biederman  *
155480b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
155580b5dce8SEric W. Biederman  */
155680b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
155780b5dce8SEric W. Biederman {
155880b5dce8SEric W. Biederman 	struct mountpoint *mp;
155980b5dce8SEric W. Biederman 	struct mount *mnt;
156080b5dce8SEric W. Biederman 
156180b5dce8SEric W. Biederman 	namespace_lock();
15623895dbf8SEric W. Biederman 	lock_mount_hash();
156380b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
1564f53e5797SEric W. Biederman 	if (IS_ERR_OR_NULL(mp))
156580b5dce8SEric W. Biederman 		goto out_unlock;
156680b5dce8SEric W. Biederman 
1567e06b933eSAndrey Ulanov 	event++;
156880b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
156980b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1570ce07d891SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1571fe78fcc8SEric W. Biederman 			hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1572fe78fcc8SEric W. Biederman 			umount_mnt(mnt);
1573ce07d891SEric W. Biederman 		}
1574e0c9c0afSEric W. Biederman 		else umount_tree(mnt, UMOUNT_CONNECTED);
157580b5dce8SEric W. Biederman 	}
157680b5dce8SEric W. Biederman 	put_mountpoint(mp);
157780b5dce8SEric W. Biederman out_unlock:
15783895dbf8SEric W. Biederman 	unlock_mount_hash();
157980b5dce8SEric W. Biederman 	namespace_unlock();
158080b5dce8SEric W. Biederman }
158180b5dce8SEric W. Biederman 
158280b5dce8SEric W. Biederman /*
15839b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
15849b40bc90SAl Viro  */
15859b40bc90SAl Viro static inline bool may_mount(void)
15869b40bc90SAl Viro {
15879b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
15889b40bc90SAl Viro }
15899b40bc90SAl Viro 
15909e8925b6SJeff Layton static inline bool may_mandlock(void)
15919e8925b6SJeff Layton {
15929e8925b6SJeff Layton #ifndef	CONFIG_MANDATORY_FILE_LOCKING
15939e8925b6SJeff Layton 	return false;
15949e8925b6SJeff Layton #endif
159595ace754SEric W. Biederman 	return capable(CAP_SYS_ADMIN);
15969e8925b6SJeff Layton }
15979e8925b6SJeff Layton 
15989b40bc90SAl Viro /*
15991da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
16001da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
16011da177e4SLinus Torvalds  *
16021da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
16031da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
16041da177e4SLinus Torvalds  */
16051da177e4SLinus Torvalds 
16063a18ef5cSDominik Brodowski int ksys_umount(char __user *name, int flags)
16071da177e4SLinus Torvalds {
16082d8f3038SAl Viro 	struct path path;
1609900148dcSAl Viro 	struct mount *mnt;
16101da177e4SLinus Torvalds 	int retval;
1611db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
16121da177e4SLinus Torvalds 
1613db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1614db1f05bbSMiklos Szeredi 		return -EINVAL;
1615db1f05bbSMiklos Szeredi 
16169b40bc90SAl Viro 	if (!may_mount())
16179b40bc90SAl Viro 		return -EPERM;
16189b40bc90SAl Viro 
1619db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1620db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1621db1f05bbSMiklos Szeredi 
1622197df04cSAl Viro 	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
16231da177e4SLinus Torvalds 	if (retval)
16241da177e4SLinus Torvalds 		goto out;
1625900148dcSAl Viro 	mnt = real_mount(path.mnt);
16261da177e4SLinus Torvalds 	retval = -EINVAL;
16272d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
16281da177e4SLinus Torvalds 		goto dput_and_out;
1629143c8c91SAl Viro 	if (!check_mnt(mnt))
16301da177e4SLinus Torvalds 		goto dput_and_out;
163125d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
16325ff9d8a6SEric W. Biederman 		goto dput_and_out;
1633b2f5d4dcSEric W. Biederman 	retval = -EPERM;
1634b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1635b2f5d4dcSEric W. Biederman 		goto dput_and_out;
16361da177e4SLinus Torvalds 
1637900148dcSAl Viro 	retval = do_umount(mnt, flags);
16381da177e4SLinus Torvalds dput_and_out:
1639429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
16402d8f3038SAl Viro 	dput(path.dentry);
1641900148dcSAl Viro 	mntput_no_expire(mnt);
16421da177e4SLinus Torvalds out:
16431da177e4SLinus Torvalds 	return retval;
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds 
16463a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
16473a18ef5cSDominik Brodowski {
16483a18ef5cSDominik Brodowski 	return ksys_umount(name, flags);
16493a18ef5cSDominik Brodowski }
16503a18ef5cSDominik Brodowski 
16511da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds /*
16541da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
16551da177e4SLinus Torvalds  */
1656bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
16571da177e4SLinus Torvalds {
16583a18ef5cSDominik Brodowski 	return ksys_umount(name, 0);
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds #endif
16621da177e4SLinus Torvalds 
16634ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
16648823c079SEric W. Biederman {
16654ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1666e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1667e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
16684ce5d2b1SEric W. Biederman }
16694ce5d2b1SEric W. Biederman 
167058be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
167158be2825SAl Viro {
167258be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
167358be2825SAl Viro }
167458be2825SAl Viro 
16754ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
16764ce5d2b1SEric W. Biederman {
16774ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
16784ce5d2b1SEric W. Biederman 	 * mount namespace loop?
16794ce5d2b1SEric W. Biederman 	 */
16804ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
16814ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
16824ce5d2b1SEric W. Biederman 		return false;
16834ce5d2b1SEric W. Biederman 
1684f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
16858823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
16868823c079SEric W. Biederman }
16878823c079SEric W. Biederman 
168887129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
168936341f64SRam Pai 					int flag)
16901da177e4SLinus Torvalds {
169184d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
16921da177e4SLinus Torvalds 
16934ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
16944ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
16954ce5d2b1SEric W. Biederman 
16964ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1697be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
16989676f0c6SRam Pai 
169936341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1700be34d1a3SDavid Howells 	if (IS_ERR(q))
1701be34d1a3SDavid Howells 		return q;
1702be34d1a3SDavid Howells 
1703a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	p = mnt;
17066b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1707315fc83eSAl Viro 		struct mount *s;
17087ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
17091da177e4SLinus Torvalds 			continue;
17101da177e4SLinus Torvalds 
1711909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
17124ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
17134ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
1714df7342b2SEric W. Biederman 				if (s->mnt.mnt_flags & MNT_LOCKED) {
1715df7342b2SEric W. Biederman 					/* Both unbindable and locked. */
1716df7342b2SEric W. Biederman 					q = ERR_PTR(-EPERM);
1717df7342b2SEric W. Biederman 					goto out;
1718df7342b2SEric W. Biederman 				} else {
17194ce5d2b1SEric W. Biederman 					s = skip_mnt_tree(s);
17204ce5d2b1SEric W. Biederman 					continue;
17214ce5d2b1SEric W. Biederman 				}
1722df7342b2SEric W. Biederman 			}
17234ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
17244ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
17259676f0c6SRam Pai 				s = skip_mnt_tree(s);
17269676f0c6SRam Pai 				continue;
17279676f0c6SRam Pai 			}
17280714a533SAl Viro 			while (p != s->mnt_parent) {
17290714a533SAl Viro 				p = p->mnt_parent;
17300714a533SAl Viro 				q = q->mnt_parent;
17311da177e4SLinus Torvalds 			}
173287129cc0SAl Viro 			p = s;
173384d17192SAl Viro 			parent = q;
173487129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1735be34d1a3SDavid Howells 			if (IS_ERR(q))
1736be34d1a3SDavid Howells 				goto out;
1737719ea2fbSAl Viro 			lock_mount_hash();
17381a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
17391064f874SEric W. Biederman 			attach_mnt(q, parent, p->mnt_mp);
1740719ea2fbSAl Viro 			unlock_mount_hash();
17411da177e4SLinus Torvalds 		}
17421da177e4SLinus Torvalds 	}
17431da177e4SLinus Torvalds 	return res;
1744be34d1a3SDavid Howells out:
17451da177e4SLinus Torvalds 	if (res) {
1746719ea2fbSAl Viro 		lock_mount_hash();
1747e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
1748719ea2fbSAl Viro 		unlock_mount_hash();
17491da177e4SLinus Torvalds 	}
1750be34d1a3SDavid Howells 	return q;
17511da177e4SLinus Torvalds }
17521da177e4SLinus Torvalds 
1753be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1754be34d1a3SDavid Howells 
1755ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path)
17568aec0809SAl Viro {
1757cb338d06SAl Viro 	struct mount *tree;
175897216be0SAl Viro 	namespace_lock();
1759cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
1760cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
1761cd4a4017SEric W. Biederman 	else
176287129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
176387129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
1764328e6d90SAl Viro 	namespace_unlock();
1765be34d1a3SDavid Howells 	if (IS_ERR(tree))
176652e220d3SDan Carpenter 		return ERR_CAST(tree);
1767be34d1a3SDavid Howells 	return &tree->mnt;
17688aec0809SAl Viro }
17698aec0809SAl Viro 
17708aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
17718aec0809SAl Viro {
177297216be0SAl Viro 	namespace_lock();
1773719ea2fbSAl Viro 	lock_mount_hash();
17749c8e0a1bSEric W. Biederman 	umount_tree(real_mount(mnt), 0);
1775719ea2fbSAl Viro 	unlock_mount_hash();
17763ab6abeeSAl Viro 	namespace_unlock();
17778aec0809SAl Viro }
17788aec0809SAl Viro 
1779c771d683SMiklos Szeredi /**
1780c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
1781c771d683SMiklos Szeredi  *
1782c771d683SMiklos Szeredi  * This creates a new vfsmount, which will be the clone of @path.  The new will
1783c771d683SMiklos Szeredi  * not be attached anywhere in the namespace and will be private (i.e. changes
1784c771d683SMiklos Szeredi  * to the originating mount won't be propagated into this).
1785c771d683SMiklos Szeredi  *
1786c771d683SMiklos Szeredi  * Release with mntput().
1787c771d683SMiklos Szeredi  */
1788ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path)
1789c771d683SMiklos Szeredi {
1790c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
1791c771d683SMiklos Szeredi 	struct mount *new_mnt;
1792c771d683SMiklos Szeredi 
1793c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
1794c771d683SMiklos Szeredi 		return ERR_PTR(-EINVAL);
1795c771d683SMiklos Szeredi 
1796c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1797c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
1798c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
1799c771d683SMiklos Szeredi 
1800c771d683SMiklos Szeredi 	return &new_mnt->mnt;
1801c771d683SMiklos Szeredi }
1802c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
1803c771d683SMiklos Szeredi 
18041f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
18051f707137SAl Viro 		   struct vfsmount *root)
18061f707137SAl Viro {
18071a4eeaf2SAl Viro 	struct mount *mnt;
18081f707137SAl Viro 	int res = f(root, arg);
18091f707137SAl Viro 	if (res)
18101f707137SAl Viro 		return res;
18111a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
18121a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
18131f707137SAl Viro 		if (res)
18141f707137SAl Viro 			return res;
18151f707137SAl Viro 	}
18161f707137SAl Viro 	return 0;
18171f707137SAl Viro }
18181f707137SAl Viro 
18193bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt)
18203bd045ccSAl Viro {
18213bd045ccSAl Viro 	struct mount *p;
18223bd045ccSAl Viro 
18233bd045ccSAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
18243bd045ccSAl Viro 		int flags = p->mnt.mnt_flags;
18253bd045ccSAl Viro 		/* Don't allow unprivileged users to change mount flags */
18263bd045ccSAl Viro 		flags |= MNT_LOCK_ATIME;
18273bd045ccSAl Viro 
18283bd045ccSAl Viro 		if (flags & MNT_READONLY)
18293bd045ccSAl Viro 			flags |= MNT_LOCK_READONLY;
18303bd045ccSAl Viro 
18313bd045ccSAl Viro 		if (flags & MNT_NODEV)
18323bd045ccSAl Viro 			flags |= MNT_LOCK_NODEV;
18333bd045ccSAl Viro 
18343bd045ccSAl Viro 		if (flags & MNT_NOSUID)
18353bd045ccSAl Viro 			flags |= MNT_LOCK_NOSUID;
18363bd045ccSAl Viro 
18373bd045ccSAl Viro 		if (flags & MNT_NOEXEC)
18383bd045ccSAl Viro 			flags |= MNT_LOCK_NOEXEC;
18393bd045ccSAl Viro 		/* Don't allow unprivileged users to reveal what is under a mount */
18403bd045ccSAl Viro 		if (list_empty(&p->mnt_expire))
18413bd045ccSAl Viro 			flags |= MNT_LOCKED;
18423bd045ccSAl Viro 		p->mnt.mnt_flags = flags;
18433bd045ccSAl Viro 	}
18443bd045ccSAl Viro }
18453bd045ccSAl Viro 
18464b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1847719f5d7fSMiklos Szeredi {
1848315fc83eSAl Viro 	struct mount *p;
1849719f5d7fSMiklos Szeredi 
1850909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1851fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
18524b8b21f4SAl Viro 			mnt_release_group_id(p);
1853719f5d7fSMiklos Szeredi 	}
1854719f5d7fSMiklos Szeredi }
1855719f5d7fSMiklos Szeredi 
18564b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1857719f5d7fSMiklos Szeredi {
1858315fc83eSAl Viro 	struct mount *p;
1859719f5d7fSMiklos Szeredi 
1860909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1861fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
18624b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1863719f5d7fSMiklos Szeredi 			if (err) {
18644b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1865719f5d7fSMiklos Szeredi 				return err;
1866719f5d7fSMiklos Szeredi 			}
1867719f5d7fSMiklos Szeredi 		}
1868719f5d7fSMiklos Szeredi 	}
1869719f5d7fSMiklos Szeredi 
1870719f5d7fSMiklos Szeredi 	return 0;
1871719f5d7fSMiklos Szeredi }
1872719f5d7fSMiklos Szeredi 
1873d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1874d2921684SEric W. Biederman {
1875d2921684SEric W. Biederman 	unsigned int max = READ_ONCE(sysctl_mount_max);
1876d2921684SEric W. Biederman 	unsigned int mounts = 0, old, pending, sum;
1877d2921684SEric W. Biederman 	struct mount *p;
1878d2921684SEric W. Biederman 
1879d2921684SEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt))
1880d2921684SEric W. Biederman 		mounts++;
1881d2921684SEric W. Biederman 
1882d2921684SEric W. Biederman 	old = ns->mounts;
1883d2921684SEric W. Biederman 	pending = ns->pending_mounts;
1884d2921684SEric W. Biederman 	sum = old + pending;
1885d2921684SEric W. Biederman 	if ((old > sum) ||
1886d2921684SEric W. Biederman 	    (pending > sum) ||
1887d2921684SEric W. Biederman 	    (max < sum) ||
1888d2921684SEric W. Biederman 	    (mounts > (max - sum)))
1889d2921684SEric W. Biederman 		return -ENOSPC;
1890d2921684SEric W. Biederman 
1891d2921684SEric W. Biederman 	ns->pending_mounts = pending + mounts;
1892d2921684SEric W. Biederman 	return 0;
1893d2921684SEric W. Biederman }
1894d2921684SEric W. Biederman 
1895b90fa9aeSRam Pai /*
1896b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1897b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
189821444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
189921444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
190021444403SRam Pai  *  		   (done when source_mnt is moved)
1901b90fa9aeSRam Pai  *
1902b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1903b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
19049676f0c6SRam Pai  * ---------------------------------------------------------------------------
1905b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
19069676f0c6SRam Pai  * |**************************************************************************
19079676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
19089676f0c6SRam Pai  * | dest     |               |                |                |            |
19099676f0c6SRam Pai  * |   |      |               |                |                |            |
19109676f0c6SRam Pai  * |   v      |               |                |                |            |
19119676f0c6SRam Pai  * |**************************************************************************
19129676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
19135afe0022SRam Pai  * |          |               |                |                |            |
19149676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
19159676f0c6SRam Pai  * ***************************************************************************
1916b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1917b90fa9aeSRam Pai  * destination mount.
1918b90fa9aeSRam Pai  *
1919b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1920b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1921b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1922b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1923b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1924b90fa9aeSRam Pai  *       mount.
19255afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
19265afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
19275afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
19285afe0022SRam Pai  *       is marked as 'shared and slave'.
19295afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
19305afe0022SRam Pai  * 	 source mount.
19315afe0022SRam Pai  *
19329676f0c6SRam Pai  * ---------------------------------------------------------------------------
193321444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
19349676f0c6SRam Pai  * |**************************************************************************
19359676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
19369676f0c6SRam Pai  * | dest     |               |                |                |            |
19379676f0c6SRam Pai  * |   |      |               |                |                |            |
19389676f0c6SRam Pai  * |   v      |               |                |                |            |
19399676f0c6SRam Pai  * |**************************************************************************
19409676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
19415afe0022SRam Pai  * |          |               |                |                |            |
19429676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
19439676f0c6SRam Pai  * ***************************************************************************
19445afe0022SRam Pai  *
19455afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
19465afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
194721444403SRam Pai  * (+*)  the mount is moved to the destination.
19485afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
19495afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
19505afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
19515afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1952b90fa9aeSRam Pai  *
1953b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1954b90fa9aeSRam Pai  * applied to each mount in the tree.
1955b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1956b90fa9aeSRam Pai  * in allocations.
1957b90fa9aeSRam Pai  */
19580fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
195984d17192SAl Viro 			struct mount *dest_mnt,
196084d17192SAl Viro 			struct mountpoint *dest_mp,
196184d17192SAl Viro 			struct path *parent_path)
1962b90fa9aeSRam Pai {
19633bd045ccSAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
196438129a13SAl Viro 	HLIST_HEAD(tree_list);
1965d2921684SEric W. Biederman 	struct mnt_namespace *ns = dest_mnt->mnt_ns;
19661064f874SEric W. Biederman 	struct mountpoint *smp;
1967315fc83eSAl Viro 	struct mount *child, *p;
196838129a13SAl Viro 	struct hlist_node *n;
1969719f5d7fSMiklos Szeredi 	int err;
1970b90fa9aeSRam Pai 
19711064f874SEric W. Biederman 	/* Preallocate a mountpoint in case the new mounts need
19721064f874SEric W. Biederman 	 * to be tucked under other mounts.
19731064f874SEric W. Biederman 	 */
19741064f874SEric W. Biederman 	smp = get_mountpoint(source_mnt->mnt.mnt_root);
19751064f874SEric W. Biederman 	if (IS_ERR(smp))
19761064f874SEric W. Biederman 		return PTR_ERR(smp);
19771064f874SEric W. Biederman 
1978d2921684SEric W. Biederman 	/* Is there space to add these mounts to the mount namespace? */
1979d2921684SEric W. Biederman 	if (!parent_path) {
1980d2921684SEric W. Biederman 		err = count_mounts(ns, source_mnt);
1981d2921684SEric W. Biederman 		if (err)
1982d2921684SEric W. Biederman 			goto out;
1983d2921684SEric W. Biederman 	}
1984d2921684SEric W. Biederman 
1985fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
19860fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
1987719f5d7fSMiklos Szeredi 		if (err)
1988719f5d7fSMiklos Szeredi 			goto out;
198984d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1990f2ebb3a9SAl Viro 		lock_mount_hash();
1991719f5d7fSMiklos Szeredi 		if (err)
1992719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
1993909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
19940f0afb1dSAl Viro 			set_mnt_shared(p);
19950b1b901bSAl Viro 	} else {
19960b1b901bSAl Viro 		lock_mount_hash();
1997b90fa9aeSRam Pai 	}
19981a390689SAl Viro 	if (parent_path) {
19990fb54e50SAl Viro 		detach_mnt(source_mnt, parent_path);
200084d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
2001143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
200221444403SRam Pai 	} else {
200384d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
20041064f874SEric W. Biederman 		commit_tree(source_mnt);
200521444403SRam Pai 	}
2006b90fa9aeSRam Pai 
200738129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
20081d6a32acSAl Viro 		struct mount *q;
200938129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
20101064f874SEric W. Biederman 		q = __lookup_mnt(&child->mnt_parent->mnt,
20111d6a32acSAl Viro 				 child->mnt_mountpoint);
20121064f874SEric W. Biederman 		if (q)
20131064f874SEric W. Biederman 			mnt_change_mountpoint(child, smp, q);
20143bd045ccSAl Viro 		/* Notice when we are propagating across user namespaces */
20153bd045ccSAl Viro 		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
20163bd045ccSAl Viro 			lock_mnt_tree(child);
20171064f874SEric W. Biederman 		commit_tree(child);
2018b90fa9aeSRam Pai 	}
20191064f874SEric W. Biederman 	put_mountpoint(smp);
2020719ea2fbSAl Viro 	unlock_mount_hash();
202199b7db7bSNick Piggin 
2022b90fa9aeSRam Pai 	return 0;
2023719f5d7fSMiklos Szeredi 
2024719f5d7fSMiklos Szeredi  out_cleanup_ids:
2025f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
2026f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2027d2921684SEric W. Biederman 		child->mnt_parent->mnt_ns->pending_mounts = 0;
2028e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
2029f2ebb3a9SAl Viro 	}
2030f2ebb3a9SAl Viro 	unlock_mount_hash();
20310fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
2032719f5d7fSMiklos Szeredi  out:
2033d2921684SEric W. Biederman 	ns->pending_mounts = 0;
20341064f874SEric W. Biederman 
20351064f874SEric W. Biederman 	read_seqlock_excl(&mount_lock);
20361064f874SEric W. Biederman 	put_mountpoint(smp);
20371064f874SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
20381064f874SEric W. Biederman 
2039719f5d7fSMiklos Szeredi 	return err;
2040b90fa9aeSRam Pai }
2041b90fa9aeSRam Pai 
204284d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
2043b12cea91SAl Viro {
2044b12cea91SAl Viro 	struct vfsmount *mnt;
204584d17192SAl Viro 	struct dentry *dentry = path->dentry;
2046b12cea91SAl Viro retry:
20475955102cSAl Viro 	inode_lock(dentry->d_inode);
204884d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
20495955102cSAl Viro 		inode_unlock(dentry->d_inode);
205084d17192SAl Viro 		return ERR_PTR(-ENOENT);
2051b12cea91SAl Viro 	}
205297216be0SAl Viro 	namespace_lock();
2053b12cea91SAl Viro 	mnt = lookup_mnt(path);
205484d17192SAl Viro 	if (likely(!mnt)) {
20553895dbf8SEric W. Biederman 		struct mountpoint *mp = get_mountpoint(dentry);
205684d17192SAl Viro 		if (IS_ERR(mp)) {
205797216be0SAl Viro 			namespace_unlock();
20585955102cSAl Viro 			inode_unlock(dentry->d_inode);
205984d17192SAl Viro 			return mp;
206084d17192SAl Viro 		}
206184d17192SAl Viro 		return mp;
206284d17192SAl Viro 	}
206397216be0SAl Viro 	namespace_unlock();
20645955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
2065b12cea91SAl Viro 	path_put(path);
2066b12cea91SAl Viro 	path->mnt = mnt;
206784d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
2068b12cea91SAl Viro 	goto retry;
2069b12cea91SAl Viro }
2070b12cea91SAl Viro 
207184d17192SAl Viro static void unlock_mount(struct mountpoint *where)
2072b12cea91SAl Viro {
207384d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
20743895dbf8SEric W. Biederman 
20753895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
207684d17192SAl Viro 	put_mountpoint(where);
20773895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
20783895dbf8SEric W. Biederman 
2079328e6d90SAl Viro 	namespace_unlock();
20805955102cSAl Viro 	inode_unlock(dentry->d_inode);
2081b12cea91SAl Viro }
2082b12cea91SAl Viro 
208384d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
20841da177e4SLinus Torvalds {
2085e462ec50SDavid Howells 	if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
20861da177e4SLinus Torvalds 		return -EINVAL;
20871da177e4SLinus Torvalds 
2088e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
2089e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
20901da177e4SLinus Torvalds 		return -ENOTDIR;
20911da177e4SLinus Torvalds 
209284d17192SAl Viro 	return attach_recursive_mnt(mnt, p, mp, NULL);
20931da177e4SLinus Torvalds }
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds /*
20967a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
20977a2e8a8fSValerie Aurora  */
20987a2e8a8fSValerie Aurora 
2099e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags)
21007a2e8a8fSValerie Aurora {
2101e462ec50SDavid Howells 	int type = ms_flags & ~(MS_REC | MS_SILENT);
21027a2e8a8fSValerie Aurora 
21037a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
21047a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
21057a2e8a8fSValerie Aurora 		return 0;
21067a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
21077a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
21087a2e8a8fSValerie Aurora 		return 0;
21097a2e8a8fSValerie Aurora 	return type;
21107a2e8a8fSValerie Aurora }
21117a2e8a8fSValerie Aurora 
21127a2e8a8fSValerie Aurora /*
211307b20889SRam Pai  * recursively change the type of the mountpoint.
211407b20889SRam Pai  */
2115e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags)
211607b20889SRam Pai {
2117315fc83eSAl Viro 	struct mount *m;
21184b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
2119e462ec50SDavid Howells 	int recurse = ms_flags & MS_REC;
21207a2e8a8fSValerie Aurora 	int type;
2121719f5d7fSMiklos Szeredi 	int err = 0;
212207b20889SRam Pai 
21232d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
212407b20889SRam Pai 		return -EINVAL;
212507b20889SRam Pai 
2126e462ec50SDavid Howells 	type = flags_to_propagation_type(ms_flags);
21277a2e8a8fSValerie Aurora 	if (!type)
21287a2e8a8fSValerie Aurora 		return -EINVAL;
21297a2e8a8fSValerie Aurora 
213097216be0SAl Viro 	namespace_lock();
2131719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
2132719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
2133719f5d7fSMiklos Szeredi 		if (err)
2134719f5d7fSMiklos Szeredi 			goto out_unlock;
2135719f5d7fSMiklos Szeredi 	}
2136719f5d7fSMiklos Szeredi 
2137719ea2fbSAl Viro 	lock_mount_hash();
2138909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
21390f0afb1dSAl Viro 		change_mnt_propagation(m, type);
2140719ea2fbSAl Viro 	unlock_mount_hash();
2141719f5d7fSMiklos Szeredi 
2142719f5d7fSMiklos Szeredi  out_unlock:
214397216be0SAl Viro 	namespace_unlock();
2144719f5d7fSMiklos Szeredi 	return err;
214507b20889SRam Pai }
214607b20889SRam Pai 
21475ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
21485ff9d8a6SEric W. Biederman {
21495ff9d8a6SEric W. Biederman 	struct mount *child;
21505ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
21515ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
21525ff9d8a6SEric W. Biederman 			continue;
21535ff9d8a6SEric W. Biederman 
21545ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
21555ff9d8a6SEric W. Biederman 			return true;
21565ff9d8a6SEric W. Biederman 	}
21575ff9d8a6SEric W. Biederman 	return false;
21585ff9d8a6SEric W. Biederman }
21595ff9d8a6SEric W. Biederman 
216007b20889SRam Pai /*
21611da177e4SLinus Torvalds  * do loopback mount.
21621da177e4SLinus Torvalds  */
2163808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
21642dafe1c4SEric Sandeen 				int recurse)
21651da177e4SLinus Torvalds {
21662d92ab3cSAl Viro 	struct path old_path;
216784d17192SAl Viro 	struct mount *mnt = NULL, *old, *parent;
216884d17192SAl Viro 	struct mountpoint *mp;
216957eccb83SAl Viro 	int err;
21701da177e4SLinus Torvalds 	if (!old_name || !*old_name)
21711da177e4SLinus Torvalds 		return -EINVAL;
2172815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
21731da177e4SLinus Torvalds 	if (err)
21741da177e4SLinus Torvalds 		return err;
21751da177e4SLinus Torvalds 
21768823c079SEric W. Biederman 	err = -EINVAL;
21774ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
21788823c079SEric W. Biederman 		goto out;
21798823c079SEric W. Biederman 
218084d17192SAl Viro 	mp = lock_mount(path);
218184d17192SAl Viro 	err = PTR_ERR(mp);
218284d17192SAl Viro 	if (IS_ERR(mp))
21839676f0c6SRam Pai 		goto out;
21849676f0c6SRam Pai 
218587129cc0SAl Viro 	old = real_mount(old_path.mnt);
218684d17192SAl Viro 	parent = real_mount(path->mnt);
218787129cc0SAl Viro 
2188b12cea91SAl Viro 	err = -EINVAL;
2189fc7be130SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2190b12cea91SAl Viro 		goto out2;
2191b12cea91SAl Viro 
2192e149ed2bSAl Viro 	if (!check_mnt(parent))
2193e149ed2bSAl Viro 		goto out2;
2194e149ed2bSAl Viro 
2195e149ed2bSAl Viro 	if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
2196b12cea91SAl Viro 		goto out2;
2197ccd48bc7SAl Viro 
21985ff9d8a6SEric W. Biederman 	if (!recurse && has_locked_children(old, old_path.dentry))
21995ff9d8a6SEric W. Biederman 		goto out2;
22005ff9d8a6SEric W. Biederman 
22011da177e4SLinus Torvalds 	if (recurse)
22024ce5d2b1SEric W. Biederman 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
22031da177e4SLinus Torvalds 	else
220487129cc0SAl Viro 		mnt = clone_mnt(old, old_path.dentry, 0);
22051da177e4SLinus Torvalds 
2206be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2207be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2208e9c5d8a5SAndrey Vagin 		goto out2;
2209be34d1a3SDavid Howells 	}
2210ccd48bc7SAl Viro 
22115ff9d8a6SEric W. Biederman 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
22125ff9d8a6SEric W. Biederman 
221384d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
22141da177e4SLinus Torvalds 	if (err) {
2215719ea2fbSAl Viro 		lock_mount_hash();
2216e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2217719ea2fbSAl Viro 		unlock_mount_hash();
22185b83d2c5SRam Pai 	}
2219b12cea91SAl Viro out2:
222084d17192SAl Viro 	unlock_mount(mp);
2221ccd48bc7SAl Viro out:
22222d92ab3cSAl Viro 	path_put(&old_path);
22231da177e4SLinus Torvalds 	return err;
22241da177e4SLinus Torvalds }
22251da177e4SLinus Torvalds 
222643f5e655SDavid Howells /*
222743f5e655SDavid Howells  * Don't allow locked mount flags to be cleared.
222843f5e655SDavid Howells  *
222943f5e655SDavid Howells  * No locks need to be held here while testing the various MNT_LOCK
223043f5e655SDavid Howells  * flags because those flags can never be cleared once they are set.
223143f5e655SDavid Howells  */
223243f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
22332e4b7fcdSDave Hansen {
223443f5e655SDavid Howells 	unsigned int fl = mnt->mnt.mnt_flags;
22352e4b7fcdSDave Hansen 
223643f5e655SDavid Howells 	if ((fl & MNT_LOCK_READONLY) &&
223743f5e655SDavid Howells 	    !(mnt_flags & MNT_READONLY))
223843f5e655SDavid Howells 		return false;
223943f5e655SDavid Howells 
224043f5e655SDavid Howells 	if ((fl & MNT_LOCK_NODEV) &&
224143f5e655SDavid Howells 	    !(mnt_flags & MNT_NODEV))
224243f5e655SDavid Howells 		return false;
224343f5e655SDavid Howells 
224443f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOSUID) &&
224543f5e655SDavid Howells 	    !(mnt_flags & MNT_NOSUID))
224643f5e655SDavid Howells 		return false;
224743f5e655SDavid Howells 
224843f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOEXEC) &&
224943f5e655SDavid Howells 	    !(mnt_flags & MNT_NOEXEC))
225043f5e655SDavid Howells 		return false;
225143f5e655SDavid Howells 
225243f5e655SDavid Howells 	if ((fl & MNT_LOCK_ATIME) &&
225343f5e655SDavid Howells 	    ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
225443f5e655SDavid Howells 		return false;
225543f5e655SDavid Howells 
225643f5e655SDavid Howells 	return true;
225743f5e655SDavid Howells }
225843f5e655SDavid Howells 
225943f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
226043f5e655SDavid Howells {
226143f5e655SDavid Howells 	bool readonly_request = (mnt_flags & MNT_READONLY);
226243f5e655SDavid Howells 
226343f5e655SDavid Howells 	if (readonly_request == __mnt_is_readonly(&mnt->mnt))
22642e4b7fcdSDave Hansen 		return 0;
22652e4b7fcdSDave Hansen 
22662e4b7fcdSDave Hansen 	if (readonly_request)
226743f5e655SDavid Howells 		return mnt_make_readonly(mnt);
226843f5e655SDavid Howells 
226943f5e655SDavid Howells 	return __mnt_unmake_readonly(mnt);
227043f5e655SDavid Howells }
227143f5e655SDavid Howells 
227243f5e655SDavid Howells /*
227343f5e655SDavid Howells  * Update the user-settable attributes on a mount.  The caller must hold
227443f5e655SDavid Howells  * sb->s_umount for writing.
227543f5e655SDavid Howells  */
227643f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
227743f5e655SDavid Howells {
227843f5e655SDavid Howells 	lock_mount_hash();
227943f5e655SDavid Howells 	mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
228043f5e655SDavid Howells 	mnt->mnt.mnt_flags = mnt_flags;
228143f5e655SDavid Howells 	touch_mnt_namespace(mnt->mnt_ns);
228243f5e655SDavid Howells 	unlock_mount_hash();
228343f5e655SDavid Howells }
228443f5e655SDavid Howells 
228543f5e655SDavid Howells /*
228643f5e655SDavid Howells  * Handle reconfiguration of the mountpoint only without alteration of the
228743f5e655SDavid Howells  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
228843f5e655SDavid Howells  * to mount(2).
228943f5e655SDavid Howells  */
229043f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
229143f5e655SDavid Howells {
229243f5e655SDavid Howells 	struct super_block *sb = path->mnt->mnt_sb;
229343f5e655SDavid Howells 	struct mount *mnt = real_mount(path->mnt);
229443f5e655SDavid Howells 	int ret;
229543f5e655SDavid Howells 
229643f5e655SDavid Howells 	if (!check_mnt(mnt))
229743f5e655SDavid Howells 		return -EINVAL;
229843f5e655SDavid Howells 
229943f5e655SDavid Howells 	if (path->dentry != mnt->mnt.mnt_root)
230043f5e655SDavid Howells 		return -EINVAL;
230143f5e655SDavid Howells 
230243f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
230343f5e655SDavid Howells 		return -EPERM;
230443f5e655SDavid Howells 
230543f5e655SDavid Howells 	down_write(&sb->s_umount);
230643f5e655SDavid Howells 	ret = change_mount_ro_state(mnt, mnt_flags);
230743f5e655SDavid Howells 	if (ret == 0)
230843f5e655SDavid Howells 		set_mount_attributes(mnt, mnt_flags);
230943f5e655SDavid Howells 	up_write(&sb->s_umount);
231043f5e655SDavid Howells 	return ret;
23112e4b7fcdSDave Hansen }
23122e4b7fcdSDave Hansen 
23131da177e4SLinus Torvalds /*
23141da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
23151da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
23161da177e4SLinus Torvalds  * on it - tough luck.
23171da177e4SLinus Torvalds  */
2318e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags,
2319e462ec50SDavid Howells 		      int mnt_flags, void *data)
23201da177e4SLinus Torvalds {
23211da177e4SLinus Torvalds 	int err;
23222d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2323143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
2324204cc0ccSAl Viro 	void *sec_opts = NULL;
23251da177e4SLinus Torvalds 
2326143c8c91SAl Viro 	if (!check_mnt(mnt))
23271da177e4SLinus Torvalds 		return -EINVAL;
23281da177e4SLinus Torvalds 
23292d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
23301da177e4SLinus Torvalds 		return -EINVAL;
23311da177e4SLinus Torvalds 
233243f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
233307b64558SEric W. Biederman 		return -EPERM;
23349566d674SEric W. Biederman 
2335c039bc3cSAl Viro 	if (data && !(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)) {
2336204cc0ccSAl Viro 		err = security_sb_eat_lsm_opts(data, &sec_opts);
2337c039bc3cSAl Viro 		if (err)
2338c039bc3cSAl Viro 			return err;
2339c039bc3cSAl Viro 	}
2340204cc0ccSAl Viro 	err = security_sb_remount(sb, sec_opts);
2341204cc0ccSAl Viro 	security_free_mnt_opts(&sec_opts);
2342ff36fe2cSEric Paris 	if (err)
2343ff36fe2cSEric Paris 		return err;
2344ff36fe2cSEric Paris 
23451da177e4SLinus Torvalds 	down_write(&sb->s_umount);
234657eccb83SAl Viro 	err = -EPERM;
234743f5e655SDavid Howells 	if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
2348e462ec50SDavid Howells 		err = do_remount_sb(sb, sb_flags, data, 0);
234943f5e655SDavid Howells 		if (!err)
235043f5e655SDavid Howells 			set_mount_attributes(mnt, mnt_flags);
23510e55a7ccSDan Williams 	}
23526339dab8SAl Viro 	up_write(&sb->s_umount);
23531da177e4SLinus Torvalds 	return err;
23541da177e4SLinus Torvalds }
23551da177e4SLinus Torvalds 
2356cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
23579676f0c6SRam Pai {
2358315fc83eSAl Viro 	struct mount *p;
2359909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2360fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
23619676f0c6SRam Pai 			return 1;
23629676f0c6SRam Pai 	}
23639676f0c6SRam Pai 	return 0;
23649676f0c6SRam Pai }
23659676f0c6SRam Pai 
2366808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name)
23671da177e4SLinus Torvalds {
23682d92ab3cSAl Viro 	struct path old_path, parent_path;
2369676da58dSAl Viro 	struct mount *p;
23700fb54e50SAl Viro 	struct mount *old;
237184d17192SAl Viro 	struct mountpoint *mp;
237257eccb83SAl Viro 	int err;
23731da177e4SLinus Torvalds 	if (!old_name || !*old_name)
23741da177e4SLinus Torvalds 		return -EINVAL;
23752d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
23761da177e4SLinus Torvalds 	if (err)
23771da177e4SLinus Torvalds 		return err;
23781da177e4SLinus Torvalds 
237984d17192SAl Viro 	mp = lock_mount(path);
238084d17192SAl Viro 	err = PTR_ERR(mp);
238184d17192SAl Viro 	if (IS_ERR(mp))
2382cc53ce53SDavid Howells 		goto out;
2383cc53ce53SDavid Howells 
2384143c8c91SAl Viro 	old = real_mount(old_path.mnt);
2385fc7be130SAl Viro 	p = real_mount(path->mnt);
2386143c8c91SAl Viro 
23871da177e4SLinus Torvalds 	err = -EINVAL;
2388fc7be130SAl Viro 	if (!check_mnt(p) || !check_mnt(old))
23891da177e4SLinus Torvalds 		goto out1;
23901da177e4SLinus Torvalds 
23915ff9d8a6SEric W. Biederman 	if (old->mnt.mnt_flags & MNT_LOCKED)
23925ff9d8a6SEric W. Biederman 		goto out1;
23935ff9d8a6SEric W. Biederman 
23941da177e4SLinus Torvalds 	err = -EINVAL;
23952d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
239621444403SRam Pai 		goto out1;
23971da177e4SLinus Torvalds 
2398676da58dSAl Viro 	if (!mnt_has_parent(old))
239921444403SRam Pai 		goto out1;
24001da177e4SLinus Torvalds 
2401e36cb0b8SDavid Howells 	if (d_is_dir(path->dentry) !=
2402e36cb0b8SDavid Howells 	      d_is_dir(old_path.dentry))
240321444403SRam Pai 		goto out1;
240421444403SRam Pai 	/*
240521444403SRam Pai 	 * Don't move a mount residing in a shared parent.
240621444403SRam Pai 	 */
2407fc7be130SAl Viro 	if (IS_MNT_SHARED(old->mnt_parent))
240821444403SRam Pai 		goto out1;
24099676f0c6SRam Pai 	/*
24109676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
24119676f0c6SRam Pai 	 * mount which is shared.
24129676f0c6SRam Pai 	 */
2413fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
24149676f0c6SRam Pai 		goto out1;
24151da177e4SLinus Torvalds 	err = -ELOOP;
2416fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2417676da58dSAl Viro 		if (p == old)
241821444403SRam Pai 			goto out1;
24191da177e4SLinus Torvalds 
242084d17192SAl Viro 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
24214ac91378SJan Blunck 	if (err)
242221444403SRam Pai 		goto out1;
24231da177e4SLinus Torvalds 
24241da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
24251da177e4SLinus Torvalds 	 * automatically */
24266776db3dSAl Viro 	list_del_init(&old->mnt_expire);
24271da177e4SLinus Torvalds out1:
242884d17192SAl Viro 	unlock_mount(mp);
24291da177e4SLinus Torvalds out:
24301da177e4SLinus Torvalds 	if (!err)
24311a390689SAl Viro 		path_put(&parent_path);
24322d92ab3cSAl Viro 	path_put(&old_path);
24331da177e4SLinus Torvalds 	return err;
24341da177e4SLinus Torvalds }
24351da177e4SLinus Torvalds 
24369d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
24379d412a43SAl Viro {
24389d412a43SAl Viro 	int err;
24399d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
24409d412a43SAl Viro 	if (subtype) {
24419d412a43SAl Viro 		subtype++;
24429d412a43SAl Viro 		err = -EINVAL;
24439d412a43SAl Viro 		if (!subtype[0])
24449d412a43SAl Viro 			goto err;
24459d412a43SAl Viro 	} else
24469d412a43SAl Viro 		subtype = "";
24479d412a43SAl Viro 
24489d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
24499d412a43SAl Viro 	err = -ENOMEM;
24509d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
24519d412a43SAl Viro 		goto err;
24529d412a43SAl Viro 	return mnt;
24539d412a43SAl Viro 
24549d412a43SAl Viro  err:
24559d412a43SAl Viro 	mntput(mnt);
24569d412a43SAl Viro 	return ERR_PTR(err);
24579d412a43SAl Viro }
24589d412a43SAl Viro 
24599d412a43SAl Viro /*
24609d412a43SAl Viro  * add a mount into a namespace's mount tree
24619d412a43SAl Viro  */
246295bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
24639d412a43SAl Viro {
246484d17192SAl Viro 	struct mountpoint *mp;
246584d17192SAl Viro 	struct mount *parent;
24669d412a43SAl Viro 	int err;
24679d412a43SAl Viro 
2468f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
24699d412a43SAl Viro 
247084d17192SAl Viro 	mp = lock_mount(path);
247184d17192SAl Viro 	if (IS_ERR(mp))
247284d17192SAl Viro 		return PTR_ERR(mp);
24739d412a43SAl Viro 
247484d17192SAl Viro 	parent = real_mount(path->mnt);
24759d412a43SAl Viro 	err = -EINVAL;
247684d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2477156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2478156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
24799d412a43SAl Viro 			goto unlock;
2480156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
248184d17192SAl Viro 		if (!parent->mnt_ns)
2482156cacb1SAl Viro 			goto unlock;
2483156cacb1SAl Viro 	}
24849d412a43SAl Viro 
24859d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
24869d412a43SAl Viro 	err = -EBUSY;
248795bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
24889d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
24899d412a43SAl Viro 		goto unlock;
24909d412a43SAl Viro 
24919d412a43SAl Viro 	err = -EINVAL;
2492e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
24939d412a43SAl Viro 		goto unlock;
24949d412a43SAl Viro 
249595bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
249684d17192SAl Viro 	err = graft_tree(newmnt, parent, mp);
24979d412a43SAl Viro 
24989d412a43SAl Viro unlock:
249984d17192SAl Viro 	unlock_mount(mp);
25009d412a43SAl Viro 	return err;
25019d412a43SAl Viro }
2502b1e75df4SAl Viro 
25038654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
25041b852bceSEric W. Biederman 
25051da177e4SLinus Torvalds /*
25061da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
25071da177e4SLinus Torvalds  * namespace's tree
25081da177e4SLinus Torvalds  */
2509e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
2510808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
25111da177e4SLinus Torvalds {
25120c55cfc4SEric W. Biederman 	struct file_system_type *type;
25131da177e4SLinus Torvalds 	struct vfsmount *mnt;
251415f9a3f3SAl Viro 	int err;
25151da177e4SLinus Torvalds 
25160c55cfc4SEric W. Biederman 	if (!fstype)
25171da177e4SLinus Torvalds 		return -EINVAL;
25181da177e4SLinus Torvalds 
25190c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
25200c55cfc4SEric W. Biederman 	if (!type)
25210c55cfc4SEric W. Biederman 		return -ENODEV;
25220c55cfc4SEric W. Biederman 
2523e462ec50SDavid Howells 	mnt = vfs_kern_mount(type, sb_flags, name, data);
25240c55cfc4SEric W. Biederman 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
25250c55cfc4SEric W. Biederman 	    !mnt->mnt_sb->s_subtype)
25260c55cfc4SEric W. Biederman 		mnt = fs_set_subtype(mnt, fstype);
25270c55cfc4SEric W. Biederman 
25280c55cfc4SEric W. Biederman 	put_filesystem(type);
25291da177e4SLinus Torvalds 	if (IS_ERR(mnt))
25301da177e4SLinus Torvalds 		return PTR_ERR(mnt);
25311da177e4SLinus Torvalds 
25328654df4eSEric W. Biederman 	if (mount_too_revealing(mnt, &mnt_flags)) {
25338654df4eSEric W. Biederman 		mntput(mnt);
25348654df4eSEric W. Biederman 		return -EPERM;
25358654df4eSEric W. Biederman 	}
25368654df4eSEric W. Biederman 
253795bc5f25SAl Viro 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
253815f9a3f3SAl Viro 	if (err)
253915f9a3f3SAl Viro 		mntput(mnt);
254015f9a3f3SAl Viro 	return err;
25411da177e4SLinus Torvalds }
25421da177e4SLinus Torvalds 
254319a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
254419a167afSAl Viro {
25456776db3dSAl Viro 	struct mount *mnt = real_mount(m);
254619a167afSAl Viro 	int err;
254719a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
254819a167afSAl Viro 	 * expired before we get a chance to add it
254919a167afSAl Viro 	 */
25506776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
255119a167afSAl Viro 
255219a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
255319a167afSAl Viro 	    m->mnt_root == path->dentry) {
2554b1e75df4SAl Viro 		err = -ELOOP;
2555b1e75df4SAl Viro 		goto fail;
255619a167afSAl Viro 	}
255719a167afSAl Viro 
255895bc5f25SAl Viro 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2559b1e75df4SAl Viro 	if (!err)
2560b1e75df4SAl Viro 		return 0;
2561b1e75df4SAl Viro fail:
2562b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
25636776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
256497216be0SAl Viro 		namespace_lock();
25656776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
256697216be0SAl Viro 		namespace_unlock();
256719a167afSAl Viro 	}
2568b1e75df4SAl Viro 	mntput(m);
2569b1e75df4SAl Viro 	mntput(m);
257019a167afSAl Viro 	return err;
257119a167afSAl Viro }
257219a167afSAl Viro 
2573ea5b778aSDavid Howells /**
2574ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2575ea5b778aSDavid Howells  * @mnt: The mount to list.
2576ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2577ea5b778aSDavid Howells  */
2578ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2579ea5b778aSDavid Howells {
258097216be0SAl Viro 	namespace_lock();
2581ea5b778aSDavid Howells 
25826776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2583ea5b778aSDavid Howells 
258497216be0SAl Viro 	namespace_unlock();
2585ea5b778aSDavid Howells }
2586ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2587ea5b778aSDavid Howells 
2588ea5b778aSDavid Howells /*
25891da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
25901da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
25911da177e4SLinus Torvalds  * here
25921da177e4SLinus Torvalds  */
25931da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
25941da177e4SLinus Torvalds {
2595761d5c38SAl Viro 	struct mount *mnt, *next;
25961da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	if (list_empty(mounts))
25991da177e4SLinus Torvalds 		return;
26001da177e4SLinus Torvalds 
260197216be0SAl Viro 	namespace_lock();
2602719ea2fbSAl Viro 	lock_mount_hash();
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
26051da177e4SLinus Torvalds 	 * following criteria:
26061da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
26071da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
26081da177e4SLinus Torvalds 	 *   cleared by mntput())
26091da177e4SLinus Torvalds 	 */
26106776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2611863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
26121ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
26131da177e4SLinus Torvalds 			continue;
26146776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
26151da177e4SLinus Torvalds 	}
2616bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
26176776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2618143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2619e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2620bcc5c7d2SAl Viro 	}
2621719ea2fbSAl Viro 	unlock_mount_hash();
26223ab6abeeSAl Viro 	namespace_unlock();
26231da177e4SLinus Torvalds }
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds /*
26285528f911STrond Myklebust  * Ripoff of 'select_parent()'
26295528f911STrond Myklebust  *
26305528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
26315528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
26325528f911STrond Myklebust  */
2633692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
26345528f911STrond Myklebust {
2635692afc31SAl Viro 	struct mount *this_parent = parent;
26365528f911STrond Myklebust 	struct list_head *next;
26375528f911STrond Myklebust 	int found = 0;
26385528f911STrond Myklebust 
26395528f911STrond Myklebust repeat:
26406b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
26415528f911STrond Myklebust resume:
26426b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
26435528f911STrond Myklebust 		struct list_head *tmp = next;
26446b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
26455528f911STrond Myklebust 
26465528f911STrond Myklebust 		next = tmp->next;
2647692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
26485528f911STrond Myklebust 			continue;
26495528f911STrond Myklebust 		/*
26505528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
26515528f911STrond Myklebust 		 */
26526b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
26535528f911STrond Myklebust 			this_parent = mnt;
26545528f911STrond Myklebust 			goto repeat;
26555528f911STrond Myklebust 		}
26565528f911STrond Myklebust 
26571ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
26586776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
26595528f911STrond Myklebust 			found++;
26605528f911STrond Myklebust 		}
26615528f911STrond Myklebust 	}
26625528f911STrond Myklebust 	/*
26635528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
26645528f911STrond Myklebust 	 */
26655528f911STrond Myklebust 	if (this_parent != parent) {
26666b41d536SAl Viro 		next = this_parent->mnt_child.next;
26670714a533SAl Viro 		this_parent = this_parent->mnt_parent;
26685528f911STrond Myklebust 		goto resume;
26695528f911STrond Myklebust 	}
26705528f911STrond Myklebust 	return found;
26715528f911STrond Myklebust }
26725528f911STrond Myklebust 
26735528f911STrond Myklebust /*
26745528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
26755528f911STrond Myklebust  * submounts of a specific parent mountpoint
267699b7db7bSNick Piggin  *
267748a066e7SAl Viro  * mount_lock must be held for write
26785528f911STrond Myklebust  */
2679b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
26805528f911STrond Myklebust {
26815528f911STrond Myklebust 	LIST_HEAD(graveyard);
2682761d5c38SAl Viro 	struct mount *m;
26835528f911STrond Myklebust 
26845528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2685c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2686bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2687761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
26886776db3dSAl Viro 						mnt_expire);
2689143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
2690e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2691bcc5c7d2SAl Viro 		}
2692bcc5c7d2SAl Viro 	}
26935528f911STrond Myklebust }
26945528f911STrond Myklebust 
26955528f911STrond Myklebust /*
26961da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
26971da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
26981da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
26991da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
27001da177e4SLinus Torvalds  */
2701b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2702b58fed8bSRam Pai 				 unsigned long n)
27031da177e4SLinus Torvalds {
27041da177e4SLinus Torvalds 	char *t = to;
27051da177e4SLinus Torvalds 	const char __user *f = from;
27061da177e4SLinus Torvalds 	char c;
27071da177e4SLinus Torvalds 
270896d4f267SLinus Torvalds 	if (!access_ok(from, n))
27091da177e4SLinus Torvalds 		return n;
27101da177e4SLinus Torvalds 
27119da3f2b7SJann Horn 	current->kernel_uaccess_faults_ok++;
27121da177e4SLinus Torvalds 	while (n) {
27131da177e4SLinus Torvalds 		if (__get_user(c, f)) {
27141da177e4SLinus Torvalds 			memset(t, 0, n);
27151da177e4SLinus Torvalds 			break;
27161da177e4SLinus Torvalds 		}
27171da177e4SLinus Torvalds 		*t++ = c;
27181da177e4SLinus Torvalds 		f++;
27191da177e4SLinus Torvalds 		n--;
27201da177e4SLinus Torvalds 	}
27219da3f2b7SJann Horn 	current->kernel_uaccess_faults_ok--;
27221da177e4SLinus Torvalds 	return n;
27231da177e4SLinus Torvalds }
27241da177e4SLinus Torvalds 
2725b40ef869SAl Viro void *copy_mount_options(const void __user * data)
27261da177e4SLinus Torvalds {
27271da177e4SLinus Torvalds 	int i;
27281da177e4SLinus Torvalds 	unsigned long size;
2729b40ef869SAl Viro 	char *copy;
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	if (!data)
2732b40ef869SAl Viro 		return NULL;
27331da177e4SLinus Torvalds 
2734b40ef869SAl Viro 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2735b40ef869SAl Viro 	if (!copy)
2736b40ef869SAl Viro 		return ERR_PTR(-ENOMEM);
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
27391da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
27401da177e4SLinus Torvalds 	 * the remainder of the page.
27411da177e4SLinus Torvalds 	 */
27421da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
27431da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
27441da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
27451da177e4SLinus Torvalds 		size = PAGE_SIZE;
27461da177e4SLinus Torvalds 
2747b40ef869SAl Viro 	i = size - exact_copy_from_user(copy, data, size);
27481da177e4SLinus Torvalds 	if (!i) {
2749b40ef869SAl Viro 		kfree(copy);
2750b40ef869SAl Viro 		return ERR_PTR(-EFAULT);
27511da177e4SLinus Torvalds 	}
27521da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
2753b40ef869SAl Viro 		memset(copy + i, 0, PAGE_SIZE - i);
2754b40ef869SAl Viro 	return copy;
27551da177e4SLinus Torvalds }
27561da177e4SLinus Torvalds 
2757b8850d1fSTim Gardner char *copy_mount_string(const void __user *data)
2758eca6f534SVegard Nossum {
2759b8850d1fSTim Gardner 	return data ? strndup_user(data, PAGE_SIZE) : NULL;
2760eca6f534SVegard Nossum }
2761eca6f534SVegard Nossum 
27621da177e4SLinus Torvalds /*
27631da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
27641da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
27651da177e4SLinus Torvalds  *
27661da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
27671da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
27681da177e4SLinus Torvalds  * information (or be NULL).
27691da177e4SLinus Torvalds  *
27701da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
27711da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
27721da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
27731da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
27741da177e4SLinus Torvalds  * and must be discarded.
27751da177e4SLinus Torvalds  */
27765e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name,
2777808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
27781da177e4SLinus Torvalds {
27792d92ab3cSAl Viro 	struct path path;
2780e462ec50SDavid Howells 	unsigned int mnt_flags = 0, sb_flags;
27811da177e4SLinus Torvalds 	int retval = 0;
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	/* Discard magic */
27841da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
27851da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	/* Basic sanity checks */
27881da177e4SLinus Torvalds 	if (data_page)
27891da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
27901da177e4SLinus Torvalds 
2791e462ec50SDavid Howells 	if (flags & MS_NOUSER)
2792e462ec50SDavid Howells 		return -EINVAL;
2793e462ec50SDavid Howells 
2794a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
27955e6123f3SSeunghun Lee 	retval = user_path(dir_name, &path);
2796a27ab9f2STetsuo Handa 	if (retval)
2797a27ab9f2STetsuo Handa 		return retval;
2798a27ab9f2STetsuo Handa 
2799a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2800a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
28010d5cadb8SAl Viro 	if (!retval && !may_mount())
28020d5cadb8SAl Viro 		retval = -EPERM;
2803e462ec50SDavid Howells 	if (!retval && (flags & SB_MANDLOCK) && !may_mandlock())
28049e8925b6SJeff Layton 		retval = -EPERM;
2805a27ab9f2STetsuo Handa 	if (retval)
2806a27ab9f2STetsuo Handa 		goto dput_out;
2807a27ab9f2STetsuo Handa 
2808613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2809613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
28100a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
28110a1c01c9SMatthew Garrett 
28121da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
28131da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
28141da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
28151da177e4SLinus Torvalds 	if (flags & MS_NODEV)
28161da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
28171da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
28181da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2819fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2820fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2821fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2822fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2823d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2824d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
2825a9e5b732SDavid Howells 	if (flags & MS_RDONLY)
28262e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2827fc33a7bbSChristoph Hellwig 
2828ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
2829ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
2830ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2831ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
2832ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
2833ffbc6f0eSEric W. Biederman 		mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2834ffbc6f0eSEric W. Biederman 	}
2835ffbc6f0eSEric W. Biederman 
2836e462ec50SDavid Howells 	sb_flags = flags & (SB_RDONLY |
2837e462ec50SDavid Howells 			    SB_SYNCHRONOUS |
2838e462ec50SDavid Howells 			    SB_MANDLOCK |
2839e462ec50SDavid Howells 			    SB_DIRSYNC |
2840e462ec50SDavid Howells 			    SB_SILENT |
2841917086ffSMimi Zohar 			    SB_POSIXACL |
2842d7ee9469SMarkus Trippelsdorf 			    SB_LAZYTIME |
2843917086ffSMimi Zohar 			    SB_I_VERSION);
28441da177e4SLinus Torvalds 
284543f5e655SDavid Howells 	if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
284643f5e655SDavid Howells 		retval = do_reconfigure_mnt(&path, mnt_flags);
284743f5e655SDavid Howells 	else if (flags & MS_REMOUNT)
2848e462ec50SDavid Howells 		retval = do_remount(&path, flags, sb_flags, mnt_flags,
28491da177e4SLinus Torvalds 				    data_page);
28501da177e4SLinus Torvalds 	else if (flags & MS_BIND)
28512d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
28529676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
28532d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
28541da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
28552d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
28561da177e4SLinus Torvalds 	else
2857e462ec50SDavid Howells 		retval = do_new_mount(&path, type_page, sb_flags, mnt_flags,
28581da177e4SLinus Torvalds 				      dev_name, data_page);
28591da177e4SLinus Torvalds dput_out:
28602d92ab3cSAl Viro 	path_put(&path);
28611da177e4SLinus Torvalds 	return retval;
28621da177e4SLinus Torvalds }
28631da177e4SLinus Torvalds 
2864537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
2865537f7ccbSEric W. Biederman {
2866537f7ccbSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
2867537f7ccbSEric W. Biederman }
2868537f7ccbSEric W. Biederman 
2869537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts)
2870537f7ccbSEric W. Biederman {
2871537f7ccbSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
2872537f7ccbSEric W. Biederman }
2873537f7ccbSEric W. Biederman 
2874771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
2875771b1371SEric W. Biederman {
287674e83122SAl Viro 	if (!is_anon_ns(ns))
28776344c433SAl Viro 		ns_free_inum(&ns->ns);
2878537f7ccbSEric W. Biederman 	dec_mnt_namespaces(ns->ucounts);
2879771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
2880771b1371SEric W. Biederman 	kfree(ns);
2881771b1371SEric W. Biederman }
2882771b1371SEric W. Biederman 
28838823c079SEric W. Biederman /*
28848823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
28858823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
28868823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
28878823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
28888823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
28898823c079SEric W. Biederman  */
28908823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
28918823c079SEric W. Biederman 
289274e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
2893cf8d2c11STrond Myklebust {
2894cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2895537f7ccbSEric W. Biederman 	struct ucounts *ucounts;
289698f842e6SEric W. Biederman 	int ret;
2897cf8d2c11STrond Myklebust 
2898537f7ccbSEric W. Biederman 	ucounts = inc_mnt_namespaces(user_ns);
2899537f7ccbSEric W. Biederman 	if (!ucounts)
2900df75e774SEric W. Biederman 		return ERR_PTR(-ENOSPC);
2901537f7ccbSEric W. Biederman 
290274e83122SAl Viro 	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2903537f7ccbSEric W. Biederman 	if (!new_ns) {
2904537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
2905cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
2906537f7ccbSEric W. Biederman 	}
290774e83122SAl Viro 	if (!anon) {
29086344c433SAl Viro 		ret = ns_alloc_inum(&new_ns->ns);
290998f842e6SEric W. Biederman 		if (ret) {
291098f842e6SEric W. Biederman 			kfree(new_ns);
2911537f7ccbSEric W. Biederman 			dec_mnt_namespaces(ucounts);
291298f842e6SEric W. Biederman 			return ERR_PTR(ret);
291398f842e6SEric W. Biederman 		}
291474e83122SAl Viro 	}
291533c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
291674e83122SAl Viro 	if (!anon)
29178823c079SEric W. Biederman 		new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2918cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2919cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2920cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2921771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
2922537f7ccbSEric W. Biederman 	new_ns->ucounts = ucounts;
2923cf8d2c11STrond Myklebust 	return new_ns;
2924cf8d2c11STrond Myklebust }
2925cf8d2c11STrond Myklebust 
29260766f788SEmese Revfy __latent_entropy
29279559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
29289559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
29291da177e4SLinus Torvalds {
29306b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
29317f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2932315fc83eSAl Viro 	struct mount *p, *q;
29339559f689SAl Viro 	struct mount *old;
2934cb338d06SAl Viro 	struct mount *new;
29357a472ef4SEric W. Biederman 	int copy_flags;
29361da177e4SLinus Torvalds 
29379559f689SAl Viro 	BUG_ON(!ns);
29389559f689SAl Viro 
29399559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
29409559f689SAl Viro 		get_mnt_ns(ns);
29419559f689SAl Viro 		return ns;
29429559f689SAl Viro 	}
29439559f689SAl Viro 
29449559f689SAl Viro 	old = ns->root;
29459559f689SAl Viro 
294674e83122SAl Viro 	new_ns = alloc_mnt_ns(user_ns, false);
2947cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2948cf8d2c11STrond Myklebust 		return new_ns;
29491da177e4SLinus Torvalds 
295097216be0SAl Viro 	namespace_lock();
29511da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
29524ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
29539559f689SAl Viro 	if (user_ns != ns->user_ns)
29543bd045ccSAl Viro 		copy_flags |= CL_SHARED_TO_SLAVE;
29557a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2956be34d1a3SDavid Howells 	if (IS_ERR(new)) {
2957328e6d90SAl Viro 		namespace_unlock();
2958771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
2959be34d1a3SDavid Howells 		return ERR_CAST(new);
29601da177e4SLinus Torvalds 	}
29613bd045ccSAl Viro 	if (user_ns != ns->user_ns) {
29623bd045ccSAl Viro 		lock_mount_hash();
29633bd045ccSAl Viro 		lock_mnt_tree(new);
29643bd045ccSAl Viro 		unlock_mount_hash();
29653bd045ccSAl Viro 	}
2966be08d6d2SAl Viro 	new_ns->root = new;
29671a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds 	/*
29701da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
29711da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
29721da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
29731da177e4SLinus Torvalds 	 */
2974909b0a88SAl Viro 	p = old;
2975cb338d06SAl Viro 	q = new;
29761da177e4SLinus Torvalds 	while (p) {
2977143c8c91SAl Viro 		q->mnt_ns = new_ns;
2978d2921684SEric W. Biederman 		new_ns->mounts++;
29799559f689SAl Viro 		if (new_fs) {
29809559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
29819559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
2982315fc83eSAl Viro 				rootmnt = &p->mnt;
29831da177e4SLinus Torvalds 			}
29849559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
29859559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
2986315fc83eSAl Viro 				pwdmnt = &p->mnt;
29871da177e4SLinus Torvalds 			}
29881da177e4SLinus Torvalds 		}
2989909b0a88SAl Viro 		p = next_mnt(p, old);
2990909b0a88SAl Viro 		q = next_mnt(q, new);
29914ce5d2b1SEric W. Biederman 		if (!q)
29924ce5d2b1SEric W. Biederman 			break;
29934ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
29944ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
29951da177e4SLinus Torvalds 	}
2996328e6d90SAl Viro 	namespace_unlock();
29971da177e4SLinus Torvalds 
29981da177e4SLinus Torvalds 	if (rootmnt)
2999f03c6599SAl Viro 		mntput(rootmnt);
30001da177e4SLinus Torvalds 	if (pwdmnt)
3001f03c6599SAl Viro 		mntput(pwdmnt);
30021da177e4SLinus Torvalds 
3003741a2951SJANAK DESAI 	return new_ns;
3004741a2951SJANAK DESAI }
3005741a2951SJANAK DESAI 
300674e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name)
3007cf8d2c11STrond Myklebust {
30081a4eeaf2SAl Viro 	struct mount *mnt = real_mount(m);
3009ea441d11SAl Viro 	struct mnt_namespace *ns;
3010d31da0f0SAl Viro 	struct super_block *s;
3011ea441d11SAl Viro 	struct path path;
3012ea441d11SAl Viro 	int err;
3013ea441d11SAl Viro 
301474e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, true);
301574e83122SAl Viro 	if (IS_ERR(ns)) {
301674e83122SAl Viro 		mntput(m);
3017ea441d11SAl Viro 		return ERR_CAST(ns);
301874e83122SAl Viro 	}
301974e83122SAl Viro 	mnt->mnt_ns = ns;
302074e83122SAl Viro 	ns->root = mnt;
302174e83122SAl Viro 	ns->mounts++;
302274e83122SAl Viro 	list_add(&mnt->mnt_list, &ns->list);
3023ea441d11SAl Viro 
302474e83122SAl Viro 	err = vfs_path_lookup(m->mnt_root, m,
3025ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3026ea441d11SAl Viro 
3027ea441d11SAl Viro 	put_mnt_ns(ns);
3028ea441d11SAl Viro 
3029ea441d11SAl Viro 	if (err)
3030ea441d11SAl Viro 		return ERR_PTR(err);
3031ea441d11SAl Viro 
3032ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
3033d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
3034d31da0f0SAl Viro 	atomic_inc(&s->s_active);
3035ea441d11SAl Viro 	mntput(path.mnt);
3036ea441d11SAl Viro 	/* lock the sucker */
3037d31da0f0SAl Viro 	down_write(&s->s_umount);
3038ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
3039ea441d11SAl Viro 	return path.dentry;
3040ea441d11SAl Viro }
3041ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
3042ea441d11SAl Viro 
3043312db1aaSDominik Brodowski int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
3044312db1aaSDominik Brodowski 	       unsigned long flags, void __user *data)
30451da177e4SLinus Torvalds {
3046eca6f534SVegard Nossum 	int ret;
3047eca6f534SVegard Nossum 	char *kernel_type;
3048eca6f534SVegard Nossum 	char *kernel_dev;
3049b40ef869SAl Viro 	void *options;
30501da177e4SLinus Torvalds 
3051b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
3052b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
3053b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
3054eca6f534SVegard Nossum 		goto out_type;
30551da177e4SLinus Torvalds 
3056b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
3057b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
3058b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
3059eca6f534SVegard Nossum 		goto out_dev;
30601da177e4SLinus Torvalds 
3061b40ef869SAl Viro 	options = copy_mount_options(data);
3062b40ef869SAl Viro 	ret = PTR_ERR(options);
3063b40ef869SAl Viro 	if (IS_ERR(options))
3064eca6f534SVegard Nossum 		goto out_data;
30651da177e4SLinus Torvalds 
3066b40ef869SAl Viro 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
3067eca6f534SVegard Nossum 
3068b40ef869SAl Viro 	kfree(options);
3069eca6f534SVegard Nossum out_data:
3070eca6f534SVegard Nossum 	kfree(kernel_dev);
3071eca6f534SVegard Nossum out_dev:
3072eca6f534SVegard Nossum 	kfree(kernel_type);
3073eca6f534SVegard Nossum out_type:
3074eca6f534SVegard Nossum 	return ret;
30751da177e4SLinus Torvalds }
30761da177e4SLinus Torvalds 
3077312db1aaSDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3078312db1aaSDominik Brodowski 		char __user *, type, unsigned long, flags, void __user *, data)
3079312db1aaSDominik Brodowski {
3080312db1aaSDominik Brodowski 	return ksys_mount(dev_name, dir_name, type, flags, data);
3081312db1aaSDominik Brodowski }
3082312db1aaSDominik Brodowski 
30831da177e4SLinus Torvalds /*
3084afac7cbaSAl Viro  * Return true if path is reachable from root
3085afac7cbaSAl Viro  *
308648a066e7SAl Viro  * namespace_sem or mount_lock is held
3087afac7cbaSAl Viro  */
3088643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
3089afac7cbaSAl Viro 			 const struct path *root)
3090afac7cbaSAl Viro {
3091643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
3092a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
30930714a533SAl Viro 		mnt = mnt->mnt_parent;
3094afac7cbaSAl Viro 	}
3095643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
3096afac7cbaSAl Viro }
3097afac7cbaSAl Viro 
3098640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2)
3099afac7cbaSAl Viro {
310025ab4c9bSYaowei Bai 	bool res;
310148a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
3102643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
310348a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
3104afac7cbaSAl Viro 	return res;
3105afac7cbaSAl Viro }
3106afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
3107afac7cbaSAl Viro 
3108afac7cbaSAl Viro /*
31091da177e4SLinus Torvalds  * pivot_root Semantics:
31101da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
31111da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
31121da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
31131da177e4SLinus Torvalds  *
31141da177e4SLinus Torvalds  * Restrictions:
31151da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
31161da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
31171da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
31181da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
31191da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
31201da177e4SLinus Torvalds  *
31214a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
31224a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
31234a0d11faSNeil Brown  * in this situation.
31244a0d11faSNeil Brown  *
31251da177e4SLinus Torvalds  * Notes:
31261da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
31271da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
31281da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
31291da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
31301da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
31311da177e4SLinus Torvalds  *    first.
31321da177e4SLinus Torvalds  */
31333480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
31343480b257SHeiko Carstens 		const char __user *, put_old)
31351da177e4SLinus Torvalds {
31362d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
313784d17192SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt;
313884d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
31391da177e4SLinus Torvalds 	int error;
31401da177e4SLinus Torvalds 
31419b40bc90SAl Viro 	if (!may_mount())
31421da177e4SLinus Torvalds 		return -EPERM;
31431da177e4SLinus Torvalds 
31442d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
31451da177e4SLinus Torvalds 	if (error)
31461da177e4SLinus Torvalds 		goto out0;
31471da177e4SLinus Torvalds 
31482d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
31491da177e4SLinus Torvalds 	if (error)
31501da177e4SLinus Torvalds 		goto out1;
31511da177e4SLinus Torvalds 
31522d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
3153b12cea91SAl Viro 	if (error)
3154b12cea91SAl Viro 		goto out2;
31551da177e4SLinus Torvalds 
3156f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
315784d17192SAl Viro 	old_mp = lock_mount(&old);
315884d17192SAl Viro 	error = PTR_ERR(old_mp);
315984d17192SAl Viro 	if (IS_ERR(old_mp))
3160b12cea91SAl Viro 		goto out3;
3161b12cea91SAl Viro 
31621da177e4SLinus Torvalds 	error = -EINVAL;
3163419148daSAl Viro 	new_mnt = real_mount(new.mnt);
3164419148daSAl Viro 	root_mnt = real_mount(root.mnt);
316584d17192SAl Viro 	old_mnt = real_mount(old.mnt);
316684d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
3167fc7be130SAl Viro 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
3168fc7be130SAl Viro 		IS_MNT_SHARED(root_mnt->mnt_parent))
3169b12cea91SAl Viro 		goto out4;
3170143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
3171b12cea91SAl Viro 		goto out4;
31725ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
31735ff9d8a6SEric W. Biederman 		goto out4;
31741da177e4SLinus Torvalds 	error = -ENOENT;
3175f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
3176b12cea91SAl Viro 		goto out4;
31771da177e4SLinus Torvalds 	error = -EBUSY;
317884d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
3179b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
31801da177e4SLinus Torvalds 	error = -EINVAL;
31818c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
3182b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3183676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
3184b12cea91SAl Viro 		goto out4; /* not attached */
318584d17192SAl Viro 	root_mp = root_mnt->mnt_mp;
31862d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
3187b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3188676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
3189b12cea91SAl Viro 		goto out4; /* not attached */
31904ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
319184d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
3192b12cea91SAl Viro 		goto out4;
31930d082601SEric W. Biederman 	/* make certain new is below the root */
31940d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
31950d082601SEric W. Biederman 		goto out4;
319684d17192SAl Viro 	root_mp->m_count++; /* pin it so it won't go away */
3197719ea2fbSAl Viro 	lock_mount_hash();
3198419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
3199419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
32005ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
32015ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
32025ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
32035ff9d8a6SEric W. Biederman 	}
32044ac91378SJan Blunck 	/* mount old root on put_old */
320584d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
32064ac91378SJan Blunck 	/* mount new_root on / */
320784d17192SAl Viro 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
32086b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
32094fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
32104fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
32113895dbf8SEric W. Biederman 	put_mountpoint(root_mp);
3212719ea2fbSAl Viro 	unlock_mount_hash();
32132d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
32141da177e4SLinus Torvalds 	error = 0;
3215b12cea91SAl Viro out4:
321684d17192SAl Viro 	unlock_mount(old_mp);
3217b12cea91SAl Viro 	if (!error) {
32181a390689SAl Viro 		path_put(&root_parent);
32191a390689SAl Viro 		path_put(&parent_path);
3220b12cea91SAl Viro 	}
3221b12cea91SAl Viro out3:
32228c3ee42eSAl Viro 	path_put(&root);
3223b12cea91SAl Viro out2:
32242d8f3038SAl Viro 	path_put(&old);
32251da177e4SLinus Torvalds out1:
32262d8f3038SAl Viro 	path_put(&new);
32271da177e4SLinus Torvalds out0:
32281da177e4SLinus Torvalds 	return error;
32291da177e4SLinus Torvalds }
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds static void __init init_mount_tree(void)
32321da177e4SLinus Torvalds {
32331da177e4SLinus Torvalds 	struct vfsmount *mnt;
323474e83122SAl Viro 	struct mount *m;
32356b3286edSKirill Korotaev 	struct mnt_namespace *ns;
3236ac748a09SJan Blunck 	struct path root;
32370c55cfc4SEric W. Biederman 	struct file_system_type *type;
32381da177e4SLinus Torvalds 
32390c55cfc4SEric W. Biederman 	type = get_fs_type("rootfs");
32400c55cfc4SEric W. Biederman 	if (!type)
32410c55cfc4SEric W. Biederman 		panic("Can't find rootfs type");
32420c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
32430c55cfc4SEric W. Biederman 	put_filesystem(type);
32441da177e4SLinus Torvalds 	if (IS_ERR(mnt))
32451da177e4SLinus Torvalds 		panic("Can't create rootfs");
3246b3e19d92SNick Piggin 
324774e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, false);
32483b22edc5STrond Myklebust 	if (IS_ERR(ns))
32491da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
325074e83122SAl Viro 	m = real_mount(mnt);
325174e83122SAl Viro 	m->mnt_ns = ns;
325274e83122SAl Viro 	ns->root = m;
325374e83122SAl Viro 	ns->mounts = 1;
325474e83122SAl Viro 	list_add(&m->mnt_list, &ns->list);
32556b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
32566b3286edSKirill Korotaev 	get_mnt_ns(ns);
32571da177e4SLinus Torvalds 
3258be08d6d2SAl Viro 	root.mnt = mnt;
3259be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
3260da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
3261ac748a09SJan Blunck 
3262ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
3263ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
32641da177e4SLinus Torvalds }
32651da177e4SLinus Torvalds 
326674bf17cfSDenis Cheng void __init mnt_init(void)
32671da177e4SLinus Torvalds {
326815a67dd8SRandy Dunlap 	int err;
32691da177e4SLinus Torvalds 
32707d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
327120c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
32721da177e4SLinus Torvalds 
32730818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
327438129a13SAl Viro 				sizeof(struct hlist_head),
32750818bf27SAl Viro 				mhash_entries, 19,
32763d375d78SPavel Tatashin 				HASH_ZERO,
32770818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
32780818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
32790818bf27SAl Viro 				sizeof(struct hlist_head),
32800818bf27SAl Viro 				mphash_entries, 19,
32813d375d78SPavel Tatashin 				HASH_ZERO,
32820818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
32831da177e4SLinus Torvalds 
328484d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
32851da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
32861da177e4SLinus Torvalds 
32874b93dc9bSTejun Heo 	kernfs_init();
32884b93dc9bSTejun Heo 
328915a67dd8SRandy Dunlap 	err = sysfs_init();
329015a67dd8SRandy Dunlap 	if (err)
329115a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
32928e24eea7SHarvey Harrison 			__func__, err);
329300d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
329400d26666SGreg Kroah-Hartman 	if (!fs_kobj)
32958e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
32961da177e4SLinus Torvalds 	init_rootfs();
32971da177e4SLinus Torvalds 	init_mount_tree();
32981da177e4SLinus Torvalds }
32991da177e4SLinus Torvalds 
3300616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
33011da177e4SLinus Torvalds {
3302d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
3303616511d0STrond Myklebust 		return;
33047b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
3305771b1371SEric W. Biederman 	free_mnt_ns(ns);
33061da177e4SLinus Torvalds }
33079d412a43SAl Viro 
33089d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
33099d412a43SAl Viro {
3310423e0ab0STim Chen 	struct vfsmount *mnt;
3311e462ec50SDavid Howells 	mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, data);
3312423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
3313423e0ab0STim Chen 		/*
3314423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
3315423e0ab0STim Chen 		 * we unmount before file sys is unregistered
3316423e0ab0STim Chen 		*/
3317f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
3318423e0ab0STim Chen 	}
3319423e0ab0STim Chen 	return mnt;
33209d412a43SAl Viro }
33219d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data);
3322423e0ab0STim Chen 
3323423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
3324423e0ab0STim Chen {
3325423e0ab0STim Chen 	/* release long term mount so mount point can be released */
3326423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
3327f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
332848a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
3329423e0ab0STim Chen 		mntput(mnt);
3330423e0ab0STim Chen 	}
3331423e0ab0STim Chen }
3332423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
333302125a82SAl Viro 
333402125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
333502125a82SAl Viro {
3336143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
333702125a82SAl Viro }
33388823c079SEric W. Biederman 
33393151527eSEric W. Biederman bool current_chrooted(void)
33403151527eSEric W. Biederman {
33413151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
33423151527eSEric W. Biederman 	struct path ns_root;
33433151527eSEric W. Biederman 	struct path fs_root;
33443151527eSEric W. Biederman 	bool chrooted;
33453151527eSEric W. Biederman 
33463151527eSEric W. Biederman 	/* Find the namespace root */
33473151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
33483151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
33493151527eSEric W. Biederman 	path_get(&ns_root);
33503151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
33513151527eSEric W. Biederman 		;
33523151527eSEric W. Biederman 
33533151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
33543151527eSEric W. Biederman 
33553151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
33563151527eSEric W. Biederman 
33573151527eSEric W. Biederman 	path_put(&fs_root);
33583151527eSEric W. Biederman 	path_put(&ns_root);
33593151527eSEric W. Biederman 
33603151527eSEric W. Biederman 	return chrooted;
33613151527eSEric W. Biederman }
33623151527eSEric W. Biederman 
33638654df4eSEric W. Biederman static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
33648654df4eSEric W. Biederman 				int *new_mnt_flags)
336587a8ebd6SEric W. Biederman {
33668c6cf9ccSEric W. Biederman 	int new_flags = *new_mnt_flags;
336787a8ebd6SEric W. Biederman 	struct mount *mnt;
3368e51db735SEric W. Biederman 	bool visible = false;
336987a8ebd6SEric W. Biederman 
337044bb4385SAl Viro 	down_read(&namespace_sem);
337187a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
3372e51db735SEric W. Biederman 		struct mount *child;
337377b1a97dSEric W. Biederman 		int mnt_flags;
337477b1a97dSEric W. Biederman 
33758654df4eSEric W. Biederman 		if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
3376e51db735SEric W. Biederman 			continue;
3377e51db735SEric W. Biederman 
33787e96c1b0SEric W. Biederman 		/* This mount is not fully visible if it's root directory
33797e96c1b0SEric W. Biederman 		 * is not the root directory of the filesystem.
33807e96c1b0SEric W. Biederman 		 */
33817e96c1b0SEric W. Biederman 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
33827e96c1b0SEric W. Biederman 			continue;
33837e96c1b0SEric W. Biederman 
3384a1935c17SEric W. Biederman 		/* A local view of the mount flags */
338577b1a97dSEric W. Biederman 		mnt_flags = mnt->mnt.mnt_flags;
338677b1a97dSEric W. Biederman 
3387695e9df0SEric W. Biederman 		/* Don't miss readonly hidden in the superblock flags */
3388bc98a42cSDavid Howells 		if (sb_rdonly(mnt->mnt.mnt_sb))
3389695e9df0SEric W. Biederman 			mnt_flags |= MNT_LOCK_READONLY;
3390695e9df0SEric W. Biederman 
33918c6cf9ccSEric W. Biederman 		/* Verify the mount flags are equal to or more permissive
33928c6cf9ccSEric W. Biederman 		 * than the proposed new mount.
33938c6cf9ccSEric W. Biederman 		 */
339477b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_READONLY) &&
33958c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_READONLY))
33968c6cf9ccSEric W. Biederman 			continue;
339777b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_ATIME) &&
339877b1a97dSEric W. Biederman 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
33998c6cf9ccSEric W. Biederman 			continue;
34008c6cf9ccSEric W. Biederman 
3401ceeb0e5dSEric W. Biederman 		/* This mount is not fully visible if there are any
3402ceeb0e5dSEric W. Biederman 		 * locked child mounts that cover anything except for
3403ceeb0e5dSEric W. Biederman 		 * empty directories.
3404e51db735SEric W. Biederman 		 */
3405e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3406e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
3407ceeb0e5dSEric W. Biederman 			/* Only worry about locked mounts */
3408d71ed6c9SEric W. Biederman 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
3409ceeb0e5dSEric W. Biederman 				continue;
34107236c85eSEric W. Biederman 			/* Is the directory permanetly empty? */
34117236c85eSEric W. Biederman 			if (!is_empty_dir_inode(inode))
3412e51db735SEric W. Biederman 				goto next;
341387a8ebd6SEric W. Biederman 		}
34148c6cf9ccSEric W. Biederman 		/* Preserve the locked attributes */
341577b1a97dSEric W. Biederman 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
34168c6cf9ccSEric W. Biederman 					       MNT_LOCK_ATIME);
3417e51db735SEric W. Biederman 		visible = true;
3418e51db735SEric W. Biederman 		goto found;
3419e51db735SEric W. Biederman 	next:	;
342087a8ebd6SEric W. Biederman 	}
3421e51db735SEric W. Biederman found:
342244bb4385SAl Viro 	up_read(&namespace_sem);
3423e51db735SEric W. Biederman 	return visible;
342487a8ebd6SEric W. Biederman }
342587a8ebd6SEric W. Biederman 
34268654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
34278654df4eSEric W. Biederman {
3428a1935c17SEric W. Biederman 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
34298654df4eSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
34308654df4eSEric W. Biederman 	unsigned long s_iflags;
34318654df4eSEric W. Biederman 
34328654df4eSEric W. Biederman 	if (ns->user_ns == &init_user_ns)
34338654df4eSEric W. Biederman 		return false;
34348654df4eSEric W. Biederman 
34358654df4eSEric W. Biederman 	/* Can this filesystem be too revealing? */
34368654df4eSEric W. Biederman 	s_iflags = mnt->mnt_sb->s_iflags;
34378654df4eSEric W. Biederman 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
34388654df4eSEric W. Biederman 		return false;
34398654df4eSEric W. Biederman 
3440a1935c17SEric W. Biederman 	if ((s_iflags & required_iflags) != required_iflags) {
3441a1935c17SEric W. Biederman 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3442a1935c17SEric W. Biederman 			  required_iflags);
3443a1935c17SEric W. Biederman 		return true;
3444a1935c17SEric W. Biederman 	}
3445a1935c17SEric W. Biederman 
34468654df4eSEric W. Biederman 	return !mnt_already_visible(ns, mnt, new_mnt_flags);
34478654df4eSEric W. Biederman }
34488654df4eSEric W. Biederman 
3449380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt)
3450380cf5baSAndy Lutomirski {
3451380cf5baSAndy Lutomirski 	/*
3452380cf5baSAndy Lutomirski 	 * Foreign mounts (accessed via fchdir or through /proc
3453380cf5baSAndy Lutomirski 	 * symlinks) are always treated as if they are nosuid.  This
3454380cf5baSAndy Lutomirski 	 * prevents namespaces from trusting potentially unsafe
3455380cf5baSAndy Lutomirski 	 * suid/sgid bits, file caps, or security labels that originate
3456380cf5baSAndy Lutomirski 	 * in other namespaces.
3457380cf5baSAndy Lutomirski 	 */
3458380cf5baSAndy Lutomirski 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3459380cf5baSAndy Lutomirski 	       current_in_userns(mnt->mnt_sb->s_user_ns);
3460380cf5baSAndy Lutomirski }
3461380cf5baSAndy Lutomirski 
346264964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
34638823c079SEric W. Biederman {
346458be2825SAl Viro 	struct ns_common *ns = NULL;
34658823c079SEric W. Biederman 	struct nsproxy *nsproxy;
34668823c079SEric W. Biederman 
3467728dba3aSEric W. Biederman 	task_lock(task);
3468728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
34698823c079SEric W. Biederman 	if (nsproxy) {
347058be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
347158be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
34728823c079SEric W. Biederman 	}
3473728dba3aSEric W. Biederman 	task_unlock(task);
34748823c079SEric W. Biederman 
34758823c079SEric W. Biederman 	return ns;
34768823c079SEric W. Biederman }
34778823c079SEric W. Biederman 
347864964528SAl Viro static void mntns_put(struct ns_common *ns)
34798823c079SEric W. Biederman {
348058be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
34818823c079SEric W. Biederman }
34828823c079SEric W. Biederman 
348364964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
34848823c079SEric W. Biederman {
34858823c079SEric W. Biederman 	struct fs_struct *fs = current->fs;
34864f757f3cSAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
34878823c079SEric W. Biederman 	struct path root;
34884f757f3cSAl Viro 	int err;
34898823c079SEric W. Biederman 
34900c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3491c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3492c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
3493ae11e0f1SZhao Hongjiang 		return -EPERM;
34948823c079SEric W. Biederman 
349574e83122SAl Viro 	if (is_anon_ns(mnt_ns))
349674e83122SAl Viro 		return -EINVAL;
349774e83122SAl Viro 
34988823c079SEric W. Biederman 	if (fs->users != 1)
34998823c079SEric W. Biederman 		return -EINVAL;
35008823c079SEric W. Biederman 
35018823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
35024f757f3cSAl Viro 	old_mnt_ns = nsproxy->mnt_ns;
35038823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
35048823c079SEric W. Biederman 
35058823c079SEric W. Biederman 	/* Find the root */
35064f757f3cSAl Viro 	err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
35074f757f3cSAl Viro 				"/", LOOKUP_DOWN, &root);
35084f757f3cSAl Viro 	if (err) {
35094f757f3cSAl Viro 		/* revert to old namespace */
35104f757f3cSAl Viro 		nsproxy->mnt_ns = old_mnt_ns;
35114f757f3cSAl Viro 		put_mnt_ns(mnt_ns);
35124f757f3cSAl Viro 		return err;
35134f757f3cSAl Viro 	}
35148823c079SEric W. Biederman 
35154068367cSAndrei Vagin 	put_mnt_ns(old_mnt_ns);
35164068367cSAndrei Vagin 
35178823c079SEric W. Biederman 	/* Update the pwd and root */
35188823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
35198823c079SEric W. Biederman 	set_fs_root(fs, &root);
35208823c079SEric W. Biederman 
35218823c079SEric W. Biederman 	path_put(&root);
35228823c079SEric W. Biederman 	return 0;
35238823c079SEric W. Biederman }
35248823c079SEric W. Biederman 
3525bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns)
3526bcac25a5SAndrey Vagin {
3527bcac25a5SAndrey Vagin 	return to_mnt_ns(ns)->user_ns;
3528bcac25a5SAndrey Vagin }
3529bcac25a5SAndrey Vagin 
35308823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
35318823c079SEric W. Biederman 	.name		= "mnt",
35328823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
35338823c079SEric W. Biederman 	.get		= mntns_get,
35348823c079SEric W. Biederman 	.put		= mntns_put,
35358823c079SEric W. Biederman 	.install	= mntns_install,
3536bcac25a5SAndrey Vagin 	.owner		= mntns_owner,
35378823c079SEric W. Biederman };
3538