xref: /openbmc/linux/fs/namespace.c (revision f2a8d52e)
159bd9dedSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/namespace.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * (C) Copyright Al Viro 2000, 2001
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 */
23a07b2000SAl Viro #include <linux/file.h>
24d10577a8SAl Viro #include <linux/uaccess.h>
250bb80f24SDavid Howells #include <linux/proc_ns.h>
2620b4fb48SLinus Torvalds #include <linux/magic.h>
2757c8a661SMike Rapoport #include <linux/memblock.h>
289ea459e1SAl Viro #include <linux/task_work.h>
299164bb4aSIngo Molnar #include <linux/sched/task.h>
30e262e32dSDavid Howells #include <uapi/linux/mount.h>
319bc61ab1SDavid Howells #include <linux/fs_context.h>
32037f11b4SAl Viro #include <linux/shmem_fs.h>
339164bb4aSIngo Molnar 
3407b20889SRam Pai #include "pnode.h"
35948730b0SAdrian Bunk #include "internal.h"
361da177e4SLinus Torvalds 
37d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */
38d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000;
39d2921684SEric W. Biederman 
400818bf27SAl Viro static unsigned int m_hash_mask __read_mostly;
410818bf27SAl Viro static unsigned int m_hash_shift __read_mostly;
420818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly;
430818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly;
440818bf27SAl Viro 
450818bf27SAl Viro static __initdata unsigned long mhash_entries;
460818bf27SAl Viro static int __init set_mhash_entries(char *str)
470818bf27SAl Viro {
480818bf27SAl Viro 	if (!str)
490818bf27SAl Viro 		return 0;
500818bf27SAl Viro 	mhash_entries = simple_strtoul(str, &str, 0);
510818bf27SAl Viro 	return 1;
520818bf27SAl Viro }
530818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries);
540818bf27SAl Viro 
550818bf27SAl Viro static __initdata unsigned long mphash_entries;
560818bf27SAl Viro static int __init set_mphash_entries(char *str)
570818bf27SAl Viro {
580818bf27SAl Viro 	if (!str)
590818bf27SAl Viro 		return 0;
600818bf27SAl Viro 	mphash_entries = simple_strtoul(str, &str, 0);
610818bf27SAl Viro 	return 1;
620818bf27SAl Viro }
630818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries);
6413f14b4dSEric Dumazet 
65c7999c36SAl Viro static u64 event;
6673cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
67719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
685addc5ddSAl Viro 
6938129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly;
700818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly;
71e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
7259aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem);
734edbe133SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
744edbe133SAl Viro static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
751da177e4SLinus Torvalds 
76f87fd4c2SMiklos Szeredi /* /sys/fs */
7700d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
7800d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
79f87fd4c2SMiklos Szeredi 
8099b7db7bSNick Piggin /*
8199b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
8299b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
8399b7db7bSNick Piggin  * up the tree.
8499b7db7bSNick Piggin  *
8599b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
8699b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
8799b7db7bSNick Piggin  */
8848a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
8999b7db7bSNick Piggin 
9038129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
931da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
940818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
950818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
960818bf27SAl Viro }
970818bf27SAl Viro 
980818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
990818bf27SAl Viro {
1000818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
1010818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
1020818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
105b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
10673cd49ecSMiklos Szeredi {
107169b480eSMatthew Wilcox 	int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
10873cd49ecSMiklos Szeredi 
109169b480eSMatthew Wilcox 	if (res < 0)
11073cd49ecSMiklos Szeredi 		return res;
111169b480eSMatthew Wilcox 	mnt->mnt_id = res;
112169b480eSMatthew Wilcox 	return 0;
11373cd49ecSMiklos Szeredi }
11473cd49ecSMiklos Szeredi 
115b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
11673cd49ecSMiklos Szeredi {
117169b480eSMatthew Wilcox 	ida_free(&mnt_id_ida, mnt->mnt_id);
11873cd49ecSMiklos Szeredi }
11973cd49ecSMiklos Szeredi 
120719f5d7fSMiklos Szeredi /*
121719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
122719f5d7fSMiklos Szeredi  */
1234b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
124719f5d7fSMiklos Szeredi {
125169b480eSMatthew Wilcox 	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
126f21f6220SAl Viro 
127169b480eSMatthew Wilcox 	if (res < 0)
128f21f6220SAl Viro 		return res;
129169b480eSMatthew Wilcox 	mnt->mnt_group_id = res;
130169b480eSMatthew Wilcox 	return 0;
131719f5d7fSMiklos Szeredi }
132719f5d7fSMiklos Szeredi 
133719f5d7fSMiklos Szeredi /*
134719f5d7fSMiklos Szeredi  * Release a peer group ID
135719f5d7fSMiklos Szeredi  */
1364b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
137719f5d7fSMiklos Szeredi {
138169b480eSMatthew Wilcox 	ida_free(&mnt_group_ida, mnt->mnt_group_id);
13915169fe7SAl Viro 	mnt->mnt_group_id = 0;
140719f5d7fSMiklos Szeredi }
141719f5d7fSMiklos Szeredi 
142b3e19d92SNick Piggin /*
143b3e19d92SNick Piggin  * vfsmount lock must be held for read
144b3e19d92SNick Piggin  */
14583adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
146b3e19d92SNick Piggin {
147b3e19d92SNick Piggin #ifdef CONFIG_SMP
14868e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
149b3e19d92SNick Piggin #else
150b3e19d92SNick Piggin 	preempt_disable();
15168e8a9feSAl Viro 	mnt->mnt_count += n;
152b3e19d92SNick Piggin 	preempt_enable();
153b3e19d92SNick Piggin #endif
154b3e19d92SNick Piggin }
155b3e19d92SNick Piggin 
156b3e19d92SNick Piggin /*
157b3e19d92SNick Piggin  * vfsmount lock must be held for write
158b3e19d92SNick Piggin  */
15983adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt)
160b3e19d92SNick Piggin {
161b3e19d92SNick Piggin #ifdef CONFIG_SMP
162f03c6599SAl Viro 	unsigned int count = 0;
163b3e19d92SNick Piggin 	int cpu;
164b3e19d92SNick Piggin 
165b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
16668e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
167b3e19d92SNick Piggin 	}
168b3e19d92SNick Piggin 
169b3e19d92SNick Piggin 	return count;
170b3e19d92SNick Piggin #else
17168e8a9feSAl Viro 	return mnt->mnt_count;
172b3e19d92SNick Piggin #endif
173b3e19d92SNick Piggin }
174b3e19d92SNick Piggin 
175b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
1761da177e4SLinus Torvalds {
177c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
178c63181e6SAl Viro 	if (mnt) {
17973cd49ecSMiklos Szeredi 		int err;
18073cd49ecSMiklos Szeredi 
181c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
18288b38782SLi Zefan 		if (err)
18388b38782SLi Zefan 			goto out_free_cache;
18488b38782SLi Zefan 
18588b38782SLi Zefan 		if (name) {
186fcc139aeSAndrzej Hajda 			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
187c63181e6SAl Viro 			if (!mnt->mnt_devname)
18888b38782SLi Zefan 				goto out_free_id;
18973cd49ecSMiklos Szeredi 		}
19073cd49ecSMiklos Szeredi 
191b3e19d92SNick Piggin #ifdef CONFIG_SMP
192c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
193c63181e6SAl Viro 		if (!mnt->mnt_pcp)
194b3e19d92SNick Piggin 			goto out_free_devname;
195b3e19d92SNick Piggin 
196c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
197b3e19d92SNick Piggin #else
198c63181e6SAl Viro 		mnt->mnt_count = 1;
199c63181e6SAl Viro 		mnt->mnt_writers = 0;
200b3e19d92SNick Piggin #endif
201b3e19d92SNick Piggin 
20238129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
203c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
204c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
205c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
206c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
207c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
208c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
209c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2100a5eb7c8SEric W. Biederman 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
21199b19d16SEric W. Biederman 		INIT_LIST_HEAD(&mnt->mnt_umounting);
21256cbb429SAl Viro 		INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
2131da177e4SLinus Torvalds 	}
214c63181e6SAl Viro 	return mnt;
21588b38782SLi Zefan 
216d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
217d3ef3d73Snpiggin@suse.de out_free_devname:
218fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
219d3ef3d73Snpiggin@suse.de #endif
22088b38782SLi Zefan out_free_id:
221c63181e6SAl Viro 	mnt_free_id(mnt);
22288b38782SLi Zefan out_free_cache:
223c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
22488b38782SLi Zefan 	return NULL;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2278366025eSDave Hansen /*
2288366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2298366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2308366025eSDave Hansen  * We must keep track of when those operations start
2318366025eSDave Hansen  * (for permission checks) and when they end, so that
2328366025eSDave Hansen  * we can determine when writes are able to occur to
2338366025eSDave Hansen  * a filesystem.
2348366025eSDave Hansen  */
2353d733633SDave Hansen /*
2363d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2373d733633SDave Hansen  * @mnt: the mount to check for its write status
2383d733633SDave Hansen  *
2393d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2403d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2413d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2423d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2433d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2443d733633SDave Hansen  * r/w.
2453d733633SDave Hansen  */
24643f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt)
2473d733633SDave Hansen {
24843f5e655SDavid Howells 	return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
2493d733633SDave Hansen }
2503d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2513d733633SDave Hansen 
25283adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2533d733633SDave Hansen {
254d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
25568e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
256d3ef3d73Snpiggin@suse.de #else
25768e8a9feSAl Viro 	mnt->mnt_writers++;
258d3ef3d73Snpiggin@suse.de #endif
2593d733633SDave Hansen }
2603d733633SDave Hansen 
26183adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2623d733633SDave Hansen {
263d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
26468e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
265d3ef3d73Snpiggin@suse.de #else
26668e8a9feSAl Viro 	mnt->mnt_writers--;
267d3ef3d73Snpiggin@suse.de #endif
268d3ef3d73Snpiggin@suse.de }
269d3ef3d73Snpiggin@suse.de 
27083adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
271d3ef3d73Snpiggin@suse.de {
272d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
273d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2743d733633SDave Hansen 	int cpu;
2753d733633SDave Hansen 
2763d733633SDave Hansen 	for_each_possible_cpu(cpu) {
27768e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
2783d733633SDave Hansen 	}
2793d733633SDave Hansen 
280d3ef3d73Snpiggin@suse.de 	return count;
281d3ef3d73Snpiggin@suse.de #else
282d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
283d3ef3d73Snpiggin@suse.de #endif
2843d733633SDave Hansen }
2853d733633SDave Hansen 
2864ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
2874ed5e82fSMiklos Szeredi {
2884ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
2894ed5e82fSMiklos Szeredi 		return 1;
2904ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
2914ed5e82fSMiklos Szeredi 	smp_rmb();
2924ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
2934ed5e82fSMiklos Szeredi }
2944ed5e82fSMiklos Szeredi 
2953d733633SDave Hansen /*
296eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
297eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
298eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
299eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
3003d733633SDave Hansen  */
3018366025eSDave Hansen /**
302eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
30383adc753SAl Viro  * @m: the mount on which to take a write
3048366025eSDave Hansen  *
305eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
306eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
307eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
308eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
309eb04c282SJan Kara  * called. This is effectively a refcount.
3108366025eSDave Hansen  */
311eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3128366025eSDave Hansen {
31383adc753SAl Viro 	struct mount *mnt = real_mount(m);
3143d733633SDave Hansen 	int ret = 0;
3153d733633SDave Hansen 
316d3ef3d73Snpiggin@suse.de 	preempt_disable();
317c6653a83SNick Piggin 	mnt_inc_writers(mnt);
318d3ef3d73Snpiggin@suse.de 	/*
319c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
320d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
321d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
322d3ef3d73Snpiggin@suse.de 	 */
323d3ef3d73Snpiggin@suse.de 	smp_mb();
3246aa7de05SMark Rutland 	while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
325d3ef3d73Snpiggin@suse.de 		cpu_relax();
326d3ef3d73Snpiggin@suse.de 	/*
327d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
328d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
329d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
330d3ef3d73Snpiggin@suse.de 	 */
331d3ef3d73Snpiggin@suse.de 	smp_rmb();
3324ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
333c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3343d733633SDave Hansen 		ret = -EROFS;
3353d733633SDave Hansen 	}
336d3ef3d73Snpiggin@suse.de 	preempt_enable();
337eb04c282SJan Kara 
338eb04c282SJan Kara 	return ret;
339eb04c282SJan Kara }
340eb04c282SJan Kara 
341eb04c282SJan Kara /**
342eb04c282SJan Kara  * mnt_want_write - get write access to a mount
343eb04c282SJan Kara  * @m: the mount on which to take a write
344eb04c282SJan Kara  *
345eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
346eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
347eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
348eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
349eb04c282SJan Kara  */
350eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
351eb04c282SJan Kara {
352eb04c282SJan Kara 	int ret;
353eb04c282SJan Kara 
354eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
355eb04c282SJan Kara 	ret = __mnt_want_write(m);
356eb04c282SJan Kara 	if (ret)
357eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3583d733633SDave Hansen 	return ret;
3598366025eSDave Hansen }
3608366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3618366025eSDave Hansen 
3628366025eSDave Hansen /**
36396029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
36496029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
36596029c4eSnpiggin@suse.de  *
36696029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
36796029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
36896029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
36996029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
37096029c4eSnpiggin@suse.de  *
37196029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
37296029c4eSnpiggin@suse.de  * drop the reference.
37396029c4eSnpiggin@suse.de  */
37496029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
37596029c4eSnpiggin@suse.de {
37696029c4eSnpiggin@suse.de 	/* superblock may be r/o */
37796029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
37896029c4eSnpiggin@suse.de 		return -EROFS;
37996029c4eSnpiggin@suse.de 	preempt_disable();
38083adc753SAl Viro 	mnt_inc_writers(real_mount(mnt));
38196029c4eSnpiggin@suse.de 	preempt_enable();
38296029c4eSnpiggin@suse.de 	return 0;
38396029c4eSnpiggin@suse.de }
38496029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
38596029c4eSnpiggin@suse.de 
38696029c4eSnpiggin@suse.de /**
387eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
388eb04c282SJan Kara  * @file: the file who's mount on which to take a write
389eb04c282SJan Kara  *
390eb04c282SJan Kara  * This is like __mnt_want_write, but it takes a file and can
391eb04c282SJan Kara  * do some optimisations if the file is open for write already
392eb04c282SJan Kara  */
393eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
394eb04c282SJan Kara {
39583f936c7SAl Viro 	if (!(file->f_mode & FMODE_WRITER))
396eb04c282SJan Kara 		return __mnt_want_write(file->f_path.mnt);
397eb04c282SJan Kara 	else
398eb04c282SJan Kara 		return mnt_clone_write(file->f_path.mnt);
399eb04c282SJan Kara }
400eb04c282SJan Kara 
401eb04c282SJan Kara /**
4027c6893e3SMiklos Szeredi  * mnt_want_write_file - get write access to a file's mount
4037c6893e3SMiklos Szeredi  * @file: the file who's mount on which to take a write
4047c6893e3SMiklos Szeredi  *
4057c6893e3SMiklos Szeredi  * This is like mnt_want_write, but it takes a file and can
4067c6893e3SMiklos Szeredi  * do some optimisations if the file is open for write already
4077c6893e3SMiklos Szeredi  */
4087c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file)
4097c6893e3SMiklos Szeredi {
4107c6893e3SMiklos Szeredi 	int ret;
4117c6893e3SMiklos Szeredi 
4127c6893e3SMiklos Szeredi 	sb_start_write(file_inode(file)->i_sb);
4137c6893e3SMiklos Szeredi 	ret = __mnt_want_write_file(file);
4147c6893e3SMiklos Szeredi 	if (ret)
4157c6893e3SMiklos Szeredi 		sb_end_write(file_inode(file)->i_sb);
4167c6893e3SMiklos Szeredi 	return ret;
4177c6893e3SMiklos Szeredi }
41896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
41996029c4eSnpiggin@suse.de 
42096029c4eSnpiggin@suse.de /**
421eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4228366025eSDave Hansen  * @mnt: the mount on which to give up write access
4238366025eSDave Hansen  *
4248366025eSDave Hansen  * Tells the low-level filesystem that we are done
4258366025eSDave Hansen  * performing writes to it.  Must be matched with
426eb04c282SJan Kara  * __mnt_want_write() call above.
4278366025eSDave Hansen  */
428eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4298366025eSDave Hansen {
430d3ef3d73Snpiggin@suse.de 	preempt_disable();
43183adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
432d3ef3d73Snpiggin@suse.de 	preempt_enable();
4338366025eSDave Hansen }
434eb04c282SJan Kara 
435eb04c282SJan Kara /**
436eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
437eb04c282SJan Kara  * @mnt: the mount on which to give up write access
438eb04c282SJan Kara  *
439eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
440eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
441eb04c282SJan Kara  * mnt_want_write() call above.
442eb04c282SJan Kara  */
443eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
444eb04c282SJan Kara {
445eb04c282SJan Kara 	__mnt_drop_write(mnt);
446eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
447eb04c282SJan Kara }
4488366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4498366025eSDave Hansen 
450eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
451eb04c282SJan Kara {
452eb04c282SJan Kara 	__mnt_drop_write(file->f_path.mnt);
453eb04c282SJan Kara }
454eb04c282SJan Kara 
4557c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file)
4567c6893e3SMiklos Szeredi {
457a6795a58SMiklos Szeredi 	__mnt_drop_write_file(file);
4587c6893e3SMiklos Szeredi 	sb_end_write(file_inode(file)->i_sb);
4597c6893e3SMiklos Szeredi }
4602a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4612a79f17eSAl Viro 
46283adc753SAl Viro static int mnt_make_readonly(struct mount *mnt)
4638366025eSDave Hansen {
4643d733633SDave Hansen 	int ret = 0;
4653d733633SDave Hansen 
466719ea2fbSAl Viro 	lock_mount_hash();
46783adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
468d3ef3d73Snpiggin@suse.de 	/*
469d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
470d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
471d3ef3d73Snpiggin@suse.de 	 */
472d3ef3d73Snpiggin@suse.de 	smp_mb();
473d3ef3d73Snpiggin@suse.de 
474d3ef3d73Snpiggin@suse.de 	/*
475d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
476d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
477d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
478d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
479d3ef3d73Snpiggin@suse.de 	 *
480d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
481d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
482d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
483d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
484d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
485d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
486d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
487d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
488d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
489d3ef3d73Snpiggin@suse.de 	 */
490c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
491d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
492d3ef3d73Snpiggin@suse.de 	else
49383adc753SAl Viro 		mnt->mnt.mnt_flags |= MNT_READONLY;
494d3ef3d73Snpiggin@suse.de 	/*
495d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
496d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
497d3ef3d73Snpiggin@suse.de 	 */
498d3ef3d73Snpiggin@suse.de 	smp_wmb();
49983adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
500719ea2fbSAl Viro 	unlock_mount_hash();
5013d733633SDave Hansen 	return ret;
5023d733633SDave Hansen }
5038366025eSDave Hansen 
50443f5e655SDavid Howells static int __mnt_unmake_readonly(struct mount *mnt)
5052e4b7fcdSDave Hansen {
506719ea2fbSAl Viro 	lock_mount_hash();
50783adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
508719ea2fbSAl Viro 	unlock_mount_hash();
50943f5e655SDavid Howells 	return 0;
5102e4b7fcdSDave Hansen }
5112e4b7fcdSDave Hansen 
5124ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5134ed5e82fSMiklos Szeredi {
5144ed5e82fSMiklos Szeredi 	struct mount *mnt;
5154ed5e82fSMiklos Szeredi 	int err = 0;
5164ed5e82fSMiklos Szeredi 
5178e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5188e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5198e8b8796SMiklos Szeredi 		return -EBUSY;
5208e8b8796SMiklos Szeredi 
521719ea2fbSAl Viro 	lock_mount_hash();
5224ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5234ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
5244ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
5254ed5e82fSMiklos Szeredi 			smp_mb();
5264ed5e82fSMiklos Szeredi 			if (mnt_get_writers(mnt) > 0) {
5274ed5e82fSMiklos Szeredi 				err = -EBUSY;
5284ed5e82fSMiklos Szeredi 				break;
5294ed5e82fSMiklos Szeredi 			}
5304ed5e82fSMiklos Szeredi 		}
5314ed5e82fSMiklos Szeredi 	}
5328e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5338e8b8796SMiklos Szeredi 		err = -EBUSY;
5348e8b8796SMiklos Szeredi 
5354ed5e82fSMiklos Szeredi 	if (!err) {
5364ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
5374ed5e82fSMiklos Szeredi 		smp_wmb();
5384ed5e82fSMiklos Szeredi 	}
5394ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5404ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
5414ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
5424ed5e82fSMiklos Szeredi 	}
543719ea2fbSAl Viro 	unlock_mount_hash();
5444ed5e82fSMiklos Szeredi 
5454ed5e82fSMiklos Szeredi 	return err;
5464ed5e82fSMiklos Szeredi }
5474ed5e82fSMiklos Szeredi 
548b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
5491da177e4SLinus Torvalds {
550fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
551d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
55268e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
553d3ef3d73Snpiggin@suse.de #endif
554b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5578ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
5588ffcb32eSDavid Howells {
5598ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
5608ffcb32eSDavid Howells }
5618ffcb32eSDavid Howells 
56248a066e7SAl Viro /* call under rcu_read_lock */
563294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
56448a066e7SAl Viro {
56548a066e7SAl Viro 	struct mount *mnt;
56648a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
567294d71ffSAl Viro 		return 1;
56848a066e7SAl Viro 	if (bastard == NULL)
569294d71ffSAl Viro 		return 0;
57048a066e7SAl Viro 	mnt = real_mount(bastard);
57148a066e7SAl Viro 	mnt_add_count(mnt, 1);
572119e1ef8SAl Viro 	smp_mb();			// see mntput_no_expire()
57348a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
574294d71ffSAl Viro 		return 0;
57548a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
57648a066e7SAl Viro 		mnt_add_count(mnt, -1);
577294d71ffSAl Viro 		return 1;
57848a066e7SAl Viro 	}
579119e1ef8SAl Viro 	lock_mount_hash();
580119e1ef8SAl Viro 	if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
581119e1ef8SAl Viro 		mnt_add_count(mnt, -1);
582119e1ef8SAl Viro 		unlock_mount_hash();
583119e1ef8SAl Viro 		return 1;
584119e1ef8SAl Viro 	}
585119e1ef8SAl Viro 	unlock_mount_hash();
586119e1ef8SAl Viro 	/* caller will mntput() */
587294d71ffSAl Viro 	return -1;
588294d71ffSAl Viro }
589294d71ffSAl Viro 
590294d71ffSAl Viro /* call under rcu_read_lock */
591294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
592294d71ffSAl Viro {
593294d71ffSAl Viro 	int res = __legitimize_mnt(bastard, seq);
594294d71ffSAl Viro 	if (likely(!res))
595294d71ffSAl Viro 		return true;
596294d71ffSAl Viro 	if (unlikely(res < 0)) {
59748a066e7SAl Viro 		rcu_read_unlock();
59848a066e7SAl Viro 		mntput(bastard);
59948a066e7SAl Viro 		rcu_read_lock();
600294d71ffSAl Viro 	}
60148a066e7SAl Viro 	return false;
60248a066e7SAl Viro }
60348a066e7SAl Viro 
6041da177e4SLinus Torvalds /*
605474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
60648a066e7SAl Viro  * call under rcu_read_lock()
6071da177e4SLinus Torvalds  */
608474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6091da177e4SLinus Torvalds {
61038129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
611474279dcSAl Viro 	struct mount *p;
6121da177e4SLinus Torvalds 
61338129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
614474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
615474279dcSAl Viro 			return p;
616474279dcSAl Viro 	return NULL;
6171da177e4SLinus Torvalds }
618474279dcSAl Viro 
619474279dcSAl Viro /*
620f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
621f015f126SDavid Howells  *
622f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
623f015f126SDavid Howells  * following mounts:
624f015f126SDavid Howells  *
625f015f126SDavid Howells  * mount /dev/sda1 /mnt
626f015f126SDavid Howells  * mount /dev/sda2 /mnt
627f015f126SDavid Howells  * mount /dev/sda3 /mnt
628f015f126SDavid Howells  *
629f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
630f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
631f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
632f015f126SDavid Howells  *
633f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
634a05964f3SRam Pai  */
635ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path)
636a05964f3SRam Pai {
637c7105365SAl Viro 	struct mount *child_mnt;
63848a066e7SAl Viro 	struct vfsmount *m;
63948a066e7SAl Viro 	unsigned seq;
64099b7db7bSNick Piggin 
64148a066e7SAl Viro 	rcu_read_lock();
64248a066e7SAl Viro 	do {
64348a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
644474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
64548a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
64648a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
64748a066e7SAl Viro 	rcu_read_unlock();
64848a066e7SAl Viro 	return m;
649a05964f3SRam Pai }
650a05964f3SRam Pai 
6517af1364fSEric W. Biederman /*
6527af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
6537af1364fSEric W. Biederman  *                         current mount namespace.
6547af1364fSEric W. Biederman  *
6557af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
6567af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
6577af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
6587af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
6597af1364fSEric W. Biederman  * is a mountpoint.
6607af1364fSEric W. Biederman  *
6617af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
6627af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
6637af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
6647af1364fSEric W. Biederman  * parent mount.
6657af1364fSEric W. Biederman  */
6667af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
6677af1364fSEric W. Biederman {
6687af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
6697af1364fSEric W. Biederman 	struct mount *mnt;
6707af1364fSEric W. Biederman 	bool is_covered = false;
6717af1364fSEric W. Biederman 
6727af1364fSEric W. Biederman 	if (!d_mountpoint(dentry))
6737af1364fSEric W. Biederman 		goto out;
6747af1364fSEric W. Biederman 
6757af1364fSEric W. Biederman 	down_read(&namespace_sem);
6767af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
6777af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
6787af1364fSEric W. Biederman 		if (is_covered)
6797af1364fSEric W. Biederman 			break;
6807af1364fSEric W. Biederman 	}
6817af1364fSEric W. Biederman 	up_read(&namespace_sem);
6827af1364fSEric W. Biederman out:
6837af1364fSEric W. Biederman 	return is_covered;
6847af1364fSEric W. Biederman }
6857af1364fSEric W. Biederman 
686e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
68784d17192SAl Viro {
6880818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
68984d17192SAl Viro 	struct mountpoint *mp;
69084d17192SAl Viro 
6910818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
69284d17192SAl Viro 		if (mp->m_dentry == dentry) {
69384d17192SAl Viro 			mp->m_count++;
69484d17192SAl Viro 			return mp;
69584d17192SAl Viro 		}
69684d17192SAl Viro 	}
697e2dfa935SEric W. Biederman 	return NULL;
698e2dfa935SEric W. Biederman }
699e2dfa935SEric W. Biederman 
7003895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry)
701e2dfa935SEric W. Biederman {
7023895dbf8SEric W. Biederman 	struct mountpoint *mp, *new = NULL;
703e2dfa935SEric W. Biederman 	int ret;
70484d17192SAl Viro 
7053895dbf8SEric W. Biederman 	if (d_mountpoint(dentry)) {
7061e9c75fbSBenjamin Coddington 		/* might be worth a WARN_ON() */
7071e9c75fbSBenjamin Coddington 		if (d_unlinked(dentry))
7081e9c75fbSBenjamin Coddington 			return ERR_PTR(-ENOENT);
7093895dbf8SEric W. Biederman mountpoint:
7103895dbf8SEric W. Biederman 		read_seqlock_excl(&mount_lock);
7113895dbf8SEric W. Biederman 		mp = lookup_mountpoint(dentry);
7123895dbf8SEric W. Biederman 		read_sequnlock_excl(&mount_lock);
7133895dbf8SEric W. Biederman 		if (mp)
7143895dbf8SEric W. Biederman 			goto done;
71584d17192SAl Viro 	}
716eed81007SMiklos Szeredi 
7173895dbf8SEric W. Biederman 	if (!new)
7183895dbf8SEric W. Biederman 		new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
7193895dbf8SEric W. Biederman 	if (!new)
7203895dbf8SEric W. Biederman 		return ERR_PTR(-ENOMEM);
7213895dbf8SEric W. Biederman 
7223895dbf8SEric W. Biederman 
7233895dbf8SEric W. Biederman 	/* Exactly one processes may set d_mounted */
7243895dbf8SEric W. Biederman 	ret = d_set_mounted(dentry);
7253895dbf8SEric W. Biederman 
7263895dbf8SEric W. Biederman 	/* Someone else set d_mounted? */
7273895dbf8SEric W. Biederman 	if (ret == -EBUSY)
7283895dbf8SEric W. Biederman 		goto mountpoint;
7293895dbf8SEric W. Biederman 
7303895dbf8SEric W. Biederman 	/* The dentry is not available as a mountpoint? */
7313895dbf8SEric W. Biederman 	mp = ERR_PTR(ret);
7323895dbf8SEric W. Biederman 	if (ret)
7333895dbf8SEric W. Biederman 		goto done;
7343895dbf8SEric W. Biederman 
7353895dbf8SEric W. Biederman 	/* Add the new mountpoint to the hash table */
7363895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
7374edbe133SAl Viro 	new->m_dentry = dget(dentry);
7383895dbf8SEric W. Biederman 	new->m_count = 1;
7393895dbf8SEric W. Biederman 	hlist_add_head(&new->m_hash, mp_hash(dentry));
7403895dbf8SEric W. Biederman 	INIT_HLIST_HEAD(&new->m_list);
7413895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
7423895dbf8SEric W. Biederman 
7433895dbf8SEric W. Biederman 	mp = new;
7443895dbf8SEric W. Biederman 	new = NULL;
7453895dbf8SEric W. Biederman done:
7463895dbf8SEric W. Biederman 	kfree(new);
74784d17192SAl Viro 	return mp;
74884d17192SAl Viro }
74984d17192SAl Viro 
7504edbe133SAl Viro /*
7514edbe133SAl Viro  * vfsmount lock must be held.  Additionally, the caller is responsible
7524edbe133SAl Viro  * for serializing calls for given disposal list.
7534edbe133SAl Viro  */
7544edbe133SAl Viro static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
75584d17192SAl Viro {
75684d17192SAl Viro 	if (!--mp->m_count) {
75784d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
7580a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
75984d17192SAl Viro 		spin_lock(&dentry->d_lock);
76084d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
76184d17192SAl Viro 		spin_unlock(&dentry->d_lock);
7624edbe133SAl Viro 		dput_to_list(dentry, list);
7630818bf27SAl Viro 		hlist_del(&mp->m_hash);
76484d17192SAl Viro 		kfree(mp);
76584d17192SAl Viro 	}
76684d17192SAl Viro }
76784d17192SAl Viro 
7684edbe133SAl Viro /* called with namespace_lock and vfsmount lock */
7694edbe133SAl Viro static void put_mountpoint(struct mountpoint *mp)
7704edbe133SAl Viro {
7714edbe133SAl Viro 	__put_mountpoint(mp, &ex_mountpoints);
7724edbe133SAl Viro }
7734edbe133SAl Viro 
774143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
7751da177e4SLinus Torvalds {
7766b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
77999b7db7bSNick Piggin /*
78099b7db7bSNick Piggin  * vfsmount lock must be held for write
78199b7db7bSNick Piggin  */
7826b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
7835addc5ddSAl Viro {
7845addc5ddSAl Viro 	if (ns) {
7855addc5ddSAl Viro 		ns->event = ++event;
7865addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7875addc5ddSAl Viro 	}
7885addc5ddSAl Viro }
7895addc5ddSAl Viro 
79099b7db7bSNick Piggin /*
79199b7db7bSNick Piggin  * vfsmount lock must be held for write
79299b7db7bSNick Piggin  */
7936b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
7945addc5ddSAl Viro {
7955addc5ddSAl Viro 	if (ns && ns->event != event) {
7965addc5ddSAl Viro 		ns->event = event;
7975addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7985addc5ddSAl Viro 	}
7995addc5ddSAl Viro }
8005addc5ddSAl Viro 
80199b7db7bSNick Piggin /*
80299b7db7bSNick Piggin  * vfsmount lock must be held for write
80399b7db7bSNick Piggin  */
804e4e59906SAl Viro static struct mountpoint *unhash_mnt(struct mount *mnt)
8051da177e4SLinus Torvalds {
806e4e59906SAl Viro 	struct mountpoint *mp;
8070714a533SAl Viro 	mnt->mnt_parent = mnt;
808a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8096b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
81038129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
8110a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
812e4e59906SAl Viro 	mp = mnt->mnt_mp;
81384d17192SAl Viro 	mnt->mnt_mp = NULL;
814e4e59906SAl Viro 	return mp;
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 {
822e4e59906SAl Viro 	put_mountpoint(unhash_mnt(mnt));
8236a46c573SEric W. Biederman }
8246a46c573SEric W. Biederman 
8256a46c573SEric W. Biederman /*
8266a46c573SEric W. Biederman  * vfsmount lock must be held for write
8276a46c573SEric W. Biederman  */
82884d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
82984d17192SAl Viro 			struct mountpoint *mp,
83044d964d6SAl Viro 			struct mount *child_mnt)
831b90fa9aeSRam Pai {
83284d17192SAl Viro 	mp->m_count++;
8333a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
8344edbe133SAl Viro 	child_mnt->mnt_mountpoint = mp->m_dentry;
8353a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
83684d17192SAl Viro 	child_mnt->mnt_mp = mp;
8370a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
838b90fa9aeSRam Pai }
839b90fa9aeSRam Pai 
8401064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent)
8411064f874SEric W. Biederman {
8421064f874SEric W. Biederman 	hlist_add_head_rcu(&mnt->mnt_hash,
8431064f874SEric W. Biederman 			   m_hash(&parent->mnt, mnt->mnt_mountpoint));
8441064f874SEric W. Biederman 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
8451064f874SEric W. Biederman }
8461064f874SEric W. Biederman 
84799b7db7bSNick Piggin /*
84899b7db7bSNick Piggin  * vfsmount lock must be held for write
84999b7db7bSNick Piggin  */
85084d17192SAl Viro static void attach_mnt(struct mount *mnt,
85184d17192SAl Viro 			struct mount *parent,
85284d17192SAl Viro 			struct mountpoint *mp)
8531da177e4SLinus Torvalds {
85484d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
8551064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
856b90fa9aeSRam Pai }
857b90fa9aeSRam Pai 
8581064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
85912a5b529SAl Viro {
8601064f874SEric W. Biederman 	struct mountpoint *old_mp = mnt->mnt_mp;
8611064f874SEric W. Biederman 	struct mount *old_parent = mnt->mnt_parent;
8621064f874SEric W. Biederman 
8631064f874SEric W. Biederman 	list_del_init(&mnt->mnt_child);
8641064f874SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
8651064f874SEric W. Biederman 	hlist_del_init_rcu(&mnt->mnt_hash);
8661064f874SEric W. Biederman 
8671064f874SEric W. Biederman 	attach_mnt(mnt, parent, mp);
8681064f874SEric W. Biederman 
8691064f874SEric W. Biederman 	put_mountpoint(old_mp);
8701064f874SEric W. Biederman 	mnt_add_count(old_parent, -1);
87112a5b529SAl Viro }
87212a5b529SAl Viro 
873b90fa9aeSRam Pai /*
87499b7db7bSNick Piggin  * vfsmount lock must be held for write
875b90fa9aeSRam Pai  */
8761064f874SEric W. Biederman static void commit_tree(struct mount *mnt)
877b90fa9aeSRam Pai {
8780714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
87983adc753SAl Viro 	struct mount *m;
880b90fa9aeSRam Pai 	LIST_HEAD(head);
881143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
882b90fa9aeSRam Pai 
8830714a533SAl Viro 	BUG_ON(parent == mnt);
884b90fa9aeSRam Pai 
8851a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
886f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
887143c8c91SAl Viro 		m->mnt_ns = n;
888f03c6599SAl Viro 
889b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
890b90fa9aeSRam Pai 
891d2921684SEric W. Biederman 	n->mounts += n->pending_mounts;
892d2921684SEric W. Biederman 	n->pending_mounts = 0;
893d2921684SEric W. Biederman 
8941064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
8956b3286edSKirill Korotaev 	touch_mnt_namespace(n);
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
898909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
8991da177e4SLinus Torvalds {
9006b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
9016b41d536SAl Viro 	if (next == &p->mnt_mounts) {
9021da177e4SLinus Torvalds 		while (1) {
903909b0a88SAl Viro 			if (p == root)
9041da177e4SLinus Torvalds 				return NULL;
9056b41d536SAl Viro 			next = p->mnt_child.next;
9066b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
9071da177e4SLinus Torvalds 				break;
9080714a533SAl Viro 			p = p->mnt_parent;
9091da177e4SLinus Torvalds 		}
9101da177e4SLinus Torvalds 	}
9116b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
914315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
9159676f0c6SRam Pai {
9166b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
9176b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
9186b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
9196b41d536SAl Viro 		prev = p->mnt_mounts.prev;
9209676f0c6SRam Pai 	}
9219676f0c6SRam Pai 	return p;
9229676f0c6SRam Pai }
9239676f0c6SRam Pai 
9248f291889SAl Viro /**
9258f291889SAl Viro  * vfs_create_mount - Create a mount for a configured superblock
9268f291889SAl Viro  * @fc: The configuration context with the superblock attached
9278f291889SAl Viro  *
9288f291889SAl Viro  * Create a mount to an already configured superblock.  If necessary, the
9298f291889SAl Viro  * caller should invoke vfs_get_tree() before calling this.
9308f291889SAl Viro  *
9318f291889SAl Viro  * Note that this does not attach the mount to anything.
9328f291889SAl Viro  */
9338f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc)
9349d412a43SAl Viro {
935b105e270SAl Viro 	struct mount *mnt;
9369d412a43SAl Viro 
9378f291889SAl Viro 	if (!fc->root)
9388f291889SAl Viro 		return ERR_PTR(-EINVAL);
9399d412a43SAl Viro 
9408f291889SAl Viro 	mnt = alloc_vfsmnt(fc->source ?: "none");
9419d412a43SAl Viro 	if (!mnt)
9429d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
9439d412a43SAl Viro 
9448f291889SAl Viro 	if (fc->sb_flags & SB_KERNMOUNT)
945b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
9469d412a43SAl Viro 
9478f291889SAl Viro 	atomic_inc(&fc->root->d_sb->s_active);
9488f291889SAl Viro 	mnt->mnt.mnt_sb		= fc->root->d_sb;
9498f291889SAl Viro 	mnt->mnt.mnt_root	= dget(fc->root);
950a73324daSAl Viro 	mnt->mnt_mountpoint	= mnt->mnt.mnt_root;
9510714a533SAl Viro 	mnt->mnt_parent		= mnt;
9528f291889SAl Viro 
953719ea2fbSAl Viro 	lock_mount_hash();
9548f291889SAl Viro 	list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
955719ea2fbSAl Viro 	unlock_mount_hash();
956b105e270SAl Viro 	return &mnt->mnt;
9579d412a43SAl Viro }
9588f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount);
9598f291889SAl Viro 
9608f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc)
9618f291889SAl Viro {
9628f291889SAl Viro 	int err = vfs_get_tree(fc);
9638f291889SAl Viro 	if (!err) {
9648f291889SAl Viro 		up_write(&fc->root->d_sb->s_umount);
9658f291889SAl Viro 		return vfs_create_mount(fc);
9668f291889SAl Viro 	}
9678f291889SAl Viro 	return ERR_PTR(err);
9688f291889SAl Viro }
9698f291889SAl Viro EXPORT_SYMBOL(fc_mount);
9708f291889SAl Viro 
9719bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type,
9729bc61ab1SDavid Howells 				int flags, const char *name,
9739bc61ab1SDavid Howells 				void *data)
9741da177e4SLinus Torvalds {
9759bc61ab1SDavid Howells 	struct fs_context *fc;
9768f291889SAl Viro 	struct vfsmount *mnt;
9779bc61ab1SDavid Howells 	int ret = 0;
9789d412a43SAl Viro 
9799d412a43SAl Viro 	if (!type)
9803e1aeb00SDavid Howells 		return ERR_PTR(-EINVAL);
9819d412a43SAl Viro 
9829bc61ab1SDavid Howells 	fc = fs_context_for_mount(type, flags);
9839bc61ab1SDavid Howells 	if (IS_ERR(fc))
9849bc61ab1SDavid Howells 		return ERR_CAST(fc);
9859bc61ab1SDavid Howells 
9863e1aeb00SDavid Howells 	if (name)
9873e1aeb00SDavid Howells 		ret = vfs_parse_fs_string(fc, "source",
9883e1aeb00SDavid Howells 					  name, strlen(name));
9899bc61ab1SDavid Howells 	if (!ret)
9909bc61ab1SDavid Howells 		ret = parse_monolithic_mount_data(fc, data);
9919bc61ab1SDavid Howells 	if (!ret)
9928f291889SAl Viro 		mnt = fc_mount(fc);
9938f291889SAl Viro 	else
9948f291889SAl Viro 		mnt = ERR_PTR(ret);
9959d412a43SAl Viro 
9969bc61ab1SDavid Howells 	put_fs_context(fc);
9978f291889SAl Viro 	return mnt;
9989d412a43SAl Viro }
9999d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
10009d412a43SAl Viro 
100193faccbbSEric W. Biederman struct vfsmount *
100293faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
100393faccbbSEric W. Biederman 	     const char *name, void *data)
100493faccbbSEric W. Biederman {
100593faccbbSEric W. Biederman 	/* Until it is worked out how to pass the user namespace
100693faccbbSEric W. Biederman 	 * through from the parent mount to the submount don't support
100793faccbbSEric W. Biederman 	 * unprivileged mounts with submounts.
100893faccbbSEric W. Biederman 	 */
100993faccbbSEric W. Biederman 	if (mountpoint->d_sb->s_user_ns != &init_user_ns)
101093faccbbSEric W. Biederman 		return ERR_PTR(-EPERM);
101193faccbbSEric W. Biederman 
1012e462ec50SDavid Howells 	return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
101393faccbbSEric W. Biederman }
101493faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount);
101593faccbbSEric W. Biederman 
101687129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
101736341f64SRam Pai 					int flag)
10181da177e4SLinus Torvalds {
101987129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
1020be34d1a3SDavid Howells 	struct mount *mnt;
1021be34d1a3SDavid Howells 	int err;
10221da177e4SLinus Torvalds 
1023be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
1024be34d1a3SDavid Howells 	if (!mnt)
1025be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
1026be34d1a3SDavid Howells 
10277a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
102815169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
1029719f5d7fSMiklos Szeredi 	else
103015169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
1031719f5d7fSMiklos Szeredi 
103215169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1033be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
1034719f5d7fSMiklos Szeredi 		if (err)
1035719f5d7fSMiklos Szeredi 			goto out_free;
1036719f5d7fSMiklos Szeredi 	}
1037719f5d7fSMiklos Szeredi 
103816a34adbSAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags;
103916a34adbSAl Viro 	mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
10405ff9d8a6SEric W. Biederman 
10411da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1042b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1043b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1044a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
10450714a533SAl Viro 	mnt->mnt_parent = mnt;
1046719ea2fbSAl Viro 	lock_mount_hash();
104739f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1048719ea2fbSAl Viro 	unlock_mount_hash();
1049b90fa9aeSRam Pai 
10507a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
10517a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
10526776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
105332301920SAl Viro 		mnt->mnt_master = old;
1054fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
10558aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1056fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
10576776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1058d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
10596776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1060d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
10615235d448SAl Viro 	} else {
10625235d448SAl Viro 		CLEAR_MNT_SHARED(mnt);
10635afe0022SRam Pai 	}
1064b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
10650f0afb1dSAl Viro 		set_mnt_shared(mnt);
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
10681da177e4SLinus Torvalds 	 * as the original if that was on one */
106936341f64SRam Pai 	if (flag & CL_EXPIRE) {
10706776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
10716776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
10721da177e4SLinus Torvalds 	}
1073be34d1a3SDavid Howells 
1074cb338d06SAl Viro 	return mnt;
1075719f5d7fSMiklos Szeredi 
1076719f5d7fSMiklos Szeredi  out_free:
10778ffcb32eSDavid Howells 	mnt_free_id(mnt);
1078719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1079be34d1a3SDavid Howells 	return ERR_PTR(err);
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10829ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
10839ea459e1SAl Viro {
108456cbb429SAl Viro 	struct hlist_node *p;
108556cbb429SAl Viro 	struct mount *m;
10869ea459e1SAl Viro 	/*
108756cbb429SAl Viro 	 * The warning here probably indicates that somebody messed
108856cbb429SAl Viro 	 * up a mnt_want/drop_write() pair.  If this happens, the
108956cbb429SAl Viro 	 * filesystem was probably unable to make r/w->r/o transitions.
10909ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
10919ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
10929ea459e1SAl Viro 	 */
10939ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
10949ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
10959ea459e1SAl Viro 		mnt_pin_kill(mnt);
109656cbb429SAl Viro 	hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
109756cbb429SAl Viro 		hlist_del(&m->mnt_umount);
109856cbb429SAl Viro 		mntput(&m->mnt);
109956cbb429SAl Viro 	}
11009ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
11019ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
11029ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
11039ea459e1SAl Viro 	mnt_free_id(mnt);
11049ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
11059ea459e1SAl Viro }
11069ea459e1SAl Viro 
11079ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
11089ea459e1SAl Viro {
11099ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
11109ea459e1SAl Viro }
11119ea459e1SAl Viro 
11129ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
11139ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
11149ea459e1SAl Viro {
11159ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
111629785735SByungchul Park 	struct mount *m, *t;
11179ea459e1SAl Viro 
111829785735SByungchul Park 	llist_for_each_entry_safe(m, t, node, mnt_llist)
111929785735SByungchul Park 		cleanup_mnt(m);
11209ea459e1SAl Viro }
11219ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
11229ea459e1SAl Viro 
1123900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
11247b7b1aceSAl Viro {
11254edbe133SAl Viro 	LIST_HEAD(list);
11264edbe133SAl Viro 
112748a066e7SAl Viro 	rcu_read_lock();
11289ea0a46cSAl Viro 	if (likely(READ_ONCE(mnt->mnt_ns))) {
11299ea0a46cSAl Viro 		/*
11309ea0a46cSAl Viro 		 * Since we don't do lock_mount_hash() here,
11319ea0a46cSAl Viro 		 * ->mnt_ns can change under us.  However, if it's
11329ea0a46cSAl Viro 		 * non-NULL, then there's a reference that won't
11339ea0a46cSAl Viro 		 * be dropped until after an RCU delay done after
11349ea0a46cSAl Viro 		 * turning ->mnt_ns NULL.  So if we observe it
11359ea0a46cSAl Viro 		 * non-NULL under rcu_read_lock(), the reference
11369ea0a46cSAl Viro 		 * we are dropping is not the final one.
11379ea0a46cSAl Viro 		 */
1138aa9c0e07SAl Viro 		mnt_add_count(mnt, -1);
113948a066e7SAl Viro 		rcu_read_unlock();
114099b7db7bSNick Piggin 		return;
1141b3e19d92SNick Piggin 	}
1142719ea2fbSAl Viro 	lock_mount_hash();
1143119e1ef8SAl Viro 	/*
1144119e1ef8SAl Viro 	 * make sure that if __legitimize_mnt() has not seen us grab
1145119e1ef8SAl Viro 	 * mount_lock, we'll see their refcount increment here.
1146119e1ef8SAl Viro 	 */
1147119e1ef8SAl Viro 	smp_mb();
11489ea0a46cSAl Viro 	mnt_add_count(mnt, -1);
1149b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
115048a066e7SAl Viro 		rcu_read_unlock();
1151719ea2fbSAl Viro 		unlock_mount_hash();
115299b7db7bSNick Piggin 		return;
115399b7db7bSNick Piggin 	}
115448a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
115548a066e7SAl Viro 		rcu_read_unlock();
115648a066e7SAl Viro 		unlock_mount_hash();
115748a066e7SAl Viro 		return;
115848a066e7SAl Viro 	}
115948a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
116048a066e7SAl Viro 	rcu_read_unlock();
1161962830dfSAndi Kleen 
116239f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1163ce07d891SEric W. Biederman 
1164ce07d891SEric W. Biederman 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1165ce07d891SEric W. Biederman 		struct mount *p, *tmp;
1166ce07d891SEric W. Biederman 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
11674edbe133SAl Viro 			__put_mountpoint(unhash_mnt(p), &list);
116856cbb429SAl Viro 			hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
1169ce07d891SEric W. Biederman 		}
1170ce07d891SEric W. Biederman 	}
1171719ea2fbSAl Viro 	unlock_mount_hash();
11724edbe133SAl Viro 	shrink_dentry_list(&list);
1173649a795aSAl Viro 
11749ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
11759ea459e1SAl Viro 		struct task_struct *task = current;
11769ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
11779ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
11789ea459e1SAl Viro 			if (!task_work_add(task, &mnt->mnt_rcu, true))
11799ea459e1SAl Viro 				return;
11809ea459e1SAl Viro 		}
11819ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
11829ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
11839ea459e1SAl Viro 		return;
11849ea459e1SAl Viro 	}
11859ea459e1SAl Viro 	cleanup_mnt(mnt);
1186b3e19d92SNick Piggin }
1187b3e19d92SNick Piggin 
1188b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1189b3e19d92SNick Piggin {
1190b3e19d92SNick Piggin 	if (mnt) {
1191863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1192b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1193863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1194863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1195863d684fSAl Viro 		mntput_no_expire(m);
1196b3e19d92SNick Piggin 	}
1197b3e19d92SNick Piggin }
1198b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1199b3e19d92SNick Piggin 
1200b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1201b3e19d92SNick Piggin {
1202b3e19d92SNick Piggin 	if (mnt)
120383adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1204b3e19d92SNick Piggin 	return mnt;
1205b3e19d92SNick Piggin }
1206b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1207b3e19d92SNick Piggin 
1208c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current
1209c6609c0aSIan Kent  *                          namespace.
1210c6609c0aSIan Kent  *
1211c6609c0aSIan Kent  *  d_mountpoint() can only be used reliably to establish if a dentry is
1212c6609c0aSIan Kent  *  not mounted in any namespace and that common case is handled inline.
1213c6609c0aSIan Kent  *  d_mountpoint() isn't aware of the possibility there may be multiple
1214c6609c0aSIan Kent  *  mounts using a given dentry in a different namespace. This function
1215c6609c0aSIan Kent  *  checks if the passed in path is a mountpoint rather than the dentry
1216c6609c0aSIan Kent  *  alone.
1217c6609c0aSIan Kent  */
1218c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path)
1219c6609c0aSIan Kent {
1220c6609c0aSIan Kent 	unsigned seq;
1221c6609c0aSIan Kent 	bool res;
1222c6609c0aSIan Kent 
1223c6609c0aSIan Kent 	if (!d_mountpoint(path->dentry))
1224c6609c0aSIan Kent 		return false;
1225c6609c0aSIan Kent 
1226c6609c0aSIan Kent 	rcu_read_lock();
1227c6609c0aSIan Kent 	do {
1228c6609c0aSIan Kent 		seq = read_seqbegin(&mount_lock);
1229c6609c0aSIan Kent 		res = __path_is_mountpoint(path);
1230c6609c0aSIan Kent 	} while (read_seqretry(&mount_lock, seq));
1231c6609c0aSIan Kent 	rcu_read_unlock();
1232c6609c0aSIan Kent 
1233c6609c0aSIan Kent 	return res;
1234c6609c0aSIan Kent }
1235c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint);
1236c6609c0aSIan Kent 
1237ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path)
12387b7b1aceSAl Viro {
12393064c356SAl Viro 	struct mount *p;
12403064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
12413064c356SAl Viro 	if (IS_ERR(p))
12423064c356SAl Viro 		return ERR_CAST(p);
12433064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
12443064c356SAl Viro 	return &p->mnt;
12457b7b1aceSAl Viro }
12461da177e4SLinus Torvalds 
1247a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
12480226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
12491da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
12501da177e4SLinus Torvalds {
1251ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
12521da177e4SLinus Torvalds 
1253390c6843SRam Pai 	down_read(&namespace_sem);
1254c7999c36SAl Viro 	if (p->cached_event == p->ns->event) {
1255c7999c36SAl Viro 		void *v = p->cached_mount;
1256c7999c36SAl Viro 		if (*pos == p->cached_index)
1257c7999c36SAl Viro 			return v;
1258c7999c36SAl Viro 		if (*pos == p->cached_index + 1) {
1259c7999c36SAl Viro 			v = seq_list_next(v, &p->ns->list, &p->cached_index);
1260c7999c36SAl Viro 			return p->cached_mount = v;
1261c7999c36SAl Viro 		}
1262c7999c36SAl Viro 	}
1263c7999c36SAl Viro 
1264c7999c36SAl Viro 	p->cached_event = p->ns->event;
1265c7999c36SAl Viro 	p->cached_mount = seq_list_start(&p->ns->list, *pos);
1266c7999c36SAl Viro 	p->cached_index = *pos;
1267c7999c36SAl Viro 	return p->cached_mount;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
12711da177e4SLinus Torvalds {
1272ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
1273b0765fb8SPavel Emelianov 
1274c7999c36SAl Viro 	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1275c7999c36SAl Viro 	p->cached_index = *pos;
1276c7999c36SAl Viro 	return p->cached_mount;
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
12801da177e4SLinus Torvalds {
1281390c6843SRam Pai 	up_read(&namespace_sem);
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
12840226f492SAl Viro static int m_show(struct seq_file *m, void *v)
12859f5596afSAl Viro {
1286ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
12871a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
12880226f492SAl Viro 	return p->show(m, &r->mnt);
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
1291a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
12921da177e4SLinus Torvalds 	.start	= m_start,
12931da177e4SLinus Torvalds 	.next	= m_next,
12941da177e4SLinus Torvalds 	.stop	= m_stop,
12950226f492SAl Viro 	.show	= m_show,
1296b4629fe2SChuck Lever };
1297a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1298b4629fe2SChuck Lever 
12991da177e4SLinus Torvalds /**
13001da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
13011da177e4SLinus Torvalds  * @mnt: root of mount tree
13021da177e4SLinus Torvalds  *
13031da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
13041da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
13051da177e4SLinus Torvalds  * busy.
13061da177e4SLinus Torvalds  */
1307909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
13081da177e4SLinus Torvalds {
1309909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
131036341f64SRam Pai 	int actual_refs = 0;
131136341f64SRam Pai 	int minimum_refs = 0;
1312315fc83eSAl Viro 	struct mount *p;
1313909b0a88SAl Viro 	BUG_ON(!m);
13141da177e4SLinus Torvalds 
1315b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1316719ea2fbSAl Viro 	lock_mount_hash();
1317909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
131883adc753SAl Viro 		actual_refs += mnt_get_count(p);
13191da177e4SLinus Torvalds 		minimum_refs += 2;
13201da177e4SLinus Torvalds 	}
1321719ea2fbSAl Viro 	unlock_mount_hash();
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
13241da177e4SLinus Torvalds 		return 0;
1325e3474a8eSIan Kent 
1326e3474a8eSIan Kent 	return 1;
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds /**
13321da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
13331da177e4SLinus Torvalds  * @mnt: root of mount
13341da177e4SLinus Torvalds  *
13351da177e4SLinus Torvalds  * This is called to check if a mount point has any
13361da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
13371da177e4SLinus Torvalds  * mount has sub mounts this will return busy
13381da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
13391da177e4SLinus Torvalds  *
13401da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
13411da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
13421da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
13431da177e4SLinus Torvalds  */
13441da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
13451da177e4SLinus Torvalds {
1346e3474a8eSIan Kent 	int ret = 1;
13478ad08d8aSAl Viro 	down_read(&namespace_sem);
1348719ea2fbSAl Viro 	lock_mount_hash();
13491ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1350e3474a8eSIan Kent 		ret = 0;
1351719ea2fbSAl Viro 	unlock_mount_hash();
13528ad08d8aSAl Viro 	up_read(&namespace_sem);
1353a05964f3SRam Pai 	return ret;
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
13571da177e4SLinus Torvalds 
135897216be0SAl Viro static void namespace_unlock(void)
13591da177e4SLinus Torvalds {
1360a3b3c562SEric W. Biederman 	struct hlist_head head;
136156cbb429SAl Viro 	struct hlist_node *p;
136256cbb429SAl Viro 	struct mount *m;
13634edbe133SAl Viro 	LIST_HEAD(list);
136497216be0SAl Viro 
1365a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
13664edbe133SAl Viro 	list_splice_init(&ex_mountpoints, &list);
1367a3b3c562SEric W. Biederman 
136897216be0SAl Viro 	up_write(&namespace_sem);
1369a3b3c562SEric W. Biederman 
13704edbe133SAl Viro 	shrink_dentry_list(&list);
13714edbe133SAl Viro 
1372a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
137397216be0SAl Viro 		return;
137497216be0SAl Viro 
137522cb7405SNeilBrown 	synchronize_rcu_expedited();
137648a066e7SAl Viro 
137756cbb429SAl Viro 	hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
137856cbb429SAl Viro 		hlist_del(&m->mnt_umount);
137956cbb429SAl Viro 		mntput(&m->mnt);
138056cbb429SAl Viro 	}
138170fbcdf4SRam Pai }
138270fbcdf4SRam Pai 
138397216be0SAl Viro static inline void namespace_lock(void)
1384e3197d83SAl Viro {
138597216be0SAl Viro 	down_write(&namespace_sem);
1386e3197d83SAl Viro }
1387e3197d83SAl Viro 
1388e819f152SEric W. Biederman enum umount_tree_flags {
1389e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1390e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1391e0c9c0afSEric W. Biederman 	UMOUNT_CONNECTED = 4,
1392e819f152SEric W. Biederman };
1393f2d0a123SEric W. Biederman 
1394f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1395f2d0a123SEric W. Biederman {
1396f2d0a123SEric W. Biederman 	/* Leaving mounts connected is only valid for lazy umounts */
1397f2d0a123SEric W. Biederman 	if (how & UMOUNT_SYNC)
1398f2d0a123SEric W. Biederman 		return true;
1399f2d0a123SEric W. Biederman 
1400f2d0a123SEric W. Biederman 	/* A mount without a parent has nothing to be connected to */
1401f2d0a123SEric W. Biederman 	if (!mnt_has_parent(mnt))
1402f2d0a123SEric W. Biederman 		return true;
1403f2d0a123SEric W. Biederman 
1404f2d0a123SEric W. Biederman 	/* Because the reference counting rules change when mounts are
1405f2d0a123SEric W. Biederman 	 * unmounted and connected, umounted mounts may not be
1406f2d0a123SEric W. Biederman 	 * connected to mounted mounts.
1407f2d0a123SEric W. Biederman 	 */
1408f2d0a123SEric W. Biederman 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1409f2d0a123SEric W. Biederman 		return true;
1410f2d0a123SEric W. Biederman 
1411f2d0a123SEric W. Biederman 	/* Has it been requested that the mount remain connected? */
1412f2d0a123SEric W. Biederman 	if (how & UMOUNT_CONNECTED)
1413f2d0a123SEric W. Biederman 		return false;
1414f2d0a123SEric W. Biederman 
1415f2d0a123SEric W. Biederman 	/* Is the mount locked such that it needs to remain connected? */
1416f2d0a123SEric W. Biederman 	if (IS_MNT_LOCKED(mnt))
1417f2d0a123SEric W. Biederman 		return false;
1418f2d0a123SEric W. Biederman 
1419f2d0a123SEric W. Biederman 	/* By default disconnect the mount */
1420f2d0a123SEric W. Biederman 	return true;
1421f2d0a123SEric W. Biederman }
1422f2d0a123SEric W. Biederman 
142399b7db7bSNick Piggin /*
142448a066e7SAl Viro  * mount_lock must be held
142599b7db7bSNick Piggin  * namespace_sem must be held for write
142699b7db7bSNick Piggin  */
1427e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
142870fbcdf4SRam Pai {
1429c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1430315fc83eSAl Viro 	struct mount *p;
143170fbcdf4SRam Pai 
14325d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14335d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
14345d88457eSEric W. Biederman 
1435c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1436590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1437590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1438c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1439590ce4bcSEric W. Biederman 	}
1440c003b26fSEric W. Biederman 
1441411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1442c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1443c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
144438129a13SAl Viro 	}
144570fbcdf4SRam Pai 
1446c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1447e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14487b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1449a05964f3SRam Pai 
1450c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1451d2921684SEric W. Biederman 		struct mnt_namespace *ns;
1452ce07d891SEric W. Biederman 		bool disconnect;
1453c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
14546776db3dSAl Viro 		list_del_init(&p->mnt_expire);
14551a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1456d2921684SEric W. Biederman 		ns = p->mnt_ns;
1457d2921684SEric W. Biederman 		if (ns) {
1458d2921684SEric W. Biederman 			ns->mounts--;
1459d2921684SEric W. Biederman 			__touch_mnt_namespace(ns);
1460d2921684SEric W. Biederman 		}
146163d37a84SAl Viro 		p->mnt_ns = NULL;
1462e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
146348a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
146487b95ce0SAl Viro 
1465f2d0a123SEric W. Biederman 		disconnect = disconnect_mount(p, how);
1466676da58dSAl Viro 		if (mnt_has_parent(p)) {
146781b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
1468ce07d891SEric W. Biederman 			if (!disconnect) {
1469ce07d891SEric W. Biederman 				/* Don't forget about p */
1470ce07d891SEric W. Biederman 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1471ce07d891SEric W. Biederman 			} else {
14726a46c573SEric W. Biederman 				umount_mnt(p);
14737c4b93d8SAl Viro 			}
1474ce07d891SEric W. Biederman 		}
14750f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
147619a1c409SAl Viro 		if (disconnect)
147719a1c409SAl Viro 			hlist_add_head(&p->mnt_umount, &unmounted);
147838129a13SAl Viro 	}
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
1481b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1482c35038beSAl Viro 
14838d0347f6SDavid Howells static int do_umount_root(struct super_block *sb)
14848d0347f6SDavid Howells {
14858d0347f6SDavid Howells 	int ret = 0;
14868d0347f6SDavid Howells 
14878d0347f6SDavid Howells 	down_write(&sb->s_umount);
14888d0347f6SDavid Howells 	if (!sb_rdonly(sb)) {
14898d0347f6SDavid Howells 		struct fs_context *fc;
14908d0347f6SDavid Howells 
14918d0347f6SDavid Howells 		fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
14928d0347f6SDavid Howells 						SB_RDONLY);
14938d0347f6SDavid Howells 		if (IS_ERR(fc)) {
14948d0347f6SDavid Howells 			ret = PTR_ERR(fc);
14958d0347f6SDavid Howells 		} else {
14968d0347f6SDavid Howells 			ret = parse_monolithic_mount_data(fc, NULL);
14978d0347f6SDavid Howells 			if (!ret)
14988d0347f6SDavid Howells 				ret = reconfigure_super(fc);
14998d0347f6SDavid Howells 			put_fs_context(fc);
15008d0347f6SDavid Howells 		}
15018d0347f6SDavid Howells 	}
15028d0347f6SDavid Howells 	up_write(&sb->s_umount);
15038d0347f6SDavid Howells 	return ret;
15048d0347f6SDavid Howells }
15058d0347f6SDavid Howells 
15061ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
15071da177e4SLinus Torvalds {
15081ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
15091da177e4SLinus Torvalds 	int retval;
15101da177e4SLinus Torvalds 
15111ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
15121da177e4SLinus Torvalds 	if (retval)
15131da177e4SLinus Torvalds 		return retval;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	/*
15161da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
15171da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
15181da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
15191da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
15201da177e4SLinus Torvalds 	 */
15211da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
15221ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
15231da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
15241da177e4SLinus Torvalds 			return -EINVAL;
15251da177e4SLinus Torvalds 
1526b3e19d92SNick Piggin 		/*
1527b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1528b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1529b3e19d92SNick Piggin 		 */
1530719ea2fbSAl Viro 		lock_mount_hash();
153183adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1532719ea2fbSAl Viro 			unlock_mount_hash();
15331da177e4SLinus Torvalds 			return -EBUSY;
1534b3e19d92SNick Piggin 		}
1535719ea2fbSAl Viro 		unlock_mount_hash();
15361da177e4SLinus Torvalds 
1537863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
15381da177e4SLinus Torvalds 			return -EAGAIN;
15391da177e4SLinus Torvalds 	}
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	/*
15421da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
15431da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
15441da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
15451da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
15461da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
15471da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
15481da177e4SLinus Torvalds 	 * about for the moment.
15491da177e4SLinus Torvalds 	 */
15501da177e4SLinus Torvalds 
155142faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
155242faad99SAl Viro 		sb->s_op->umount_begin(sb);
155342faad99SAl Viro 	}
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	/*
15561da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
15571da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
15581da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
15591da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
15601da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
15611da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
15621da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
15631da177e4SLinus Torvalds 	 */
15641ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
15651da177e4SLinus Torvalds 		/*
15661da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
15671da177e4SLinus Torvalds 		 * we just try to remount it readonly.
15681da177e4SLinus Torvalds 		 */
1569bc6155d1SEric W. Biederman 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1570a1480dccSAndy Lutomirski 			return -EPERM;
15718d0347f6SDavid Howells 		return do_umount_root(sb);
15721da177e4SLinus Torvalds 	}
15731da177e4SLinus Torvalds 
157497216be0SAl Viro 	namespace_lock();
1575719ea2fbSAl Viro 	lock_mount_hash();
15761da177e4SLinus Torvalds 
157725d202edSEric W. Biederman 	/* Recheck MNT_LOCKED with the locks held */
157825d202edSEric W. Biederman 	retval = -EINVAL;
157925d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
158025d202edSEric W. Biederman 		goto out;
158125d202edSEric W. Biederman 
158225d202edSEric W. Biederman 	event++;
158348a066e7SAl Viro 	if (flags & MNT_DETACH) {
158448a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1585e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
158648a066e7SAl Viro 		retval = 0;
158748a066e7SAl Viro 	} else {
1588b54b9be7SAl Viro 		shrink_submounts(mnt);
15891da177e4SLinus Torvalds 		retval = -EBUSY;
159048a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
15911a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1592e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
15931da177e4SLinus Torvalds 			retval = 0;
15941da177e4SLinus Torvalds 		}
159548a066e7SAl Viro 	}
159625d202edSEric W. Biederman out:
1597719ea2fbSAl Viro 	unlock_mount_hash();
1598e3197d83SAl Viro 	namespace_unlock();
15991da177e4SLinus Torvalds 	return retval;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds /*
160380b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
160480b5dce8SEric W. Biederman  *
160580b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
160680b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
160780b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
160880b5dce8SEric W. Biederman  * leaking them.
160980b5dce8SEric W. Biederman  *
161080b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
161180b5dce8SEric W. Biederman  */
161280b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
161380b5dce8SEric W. Biederman {
161480b5dce8SEric W. Biederman 	struct mountpoint *mp;
161580b5dce8SEric W. Biederman 	struct mount *mnt;
161680b5dce8SEric W. Biederman 
161780b5dce8SEric W. Biederman 	namespace_lock();
16183895dbf8SEric W. Biederman 	lock_mount_hash();
161980b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
1620adc9b5c0SAl Viro 	if (!mp)
162180b5dce8SEric W. Biederman 		goto out_unlock;
162280b5dce8SEric W. Biederman 
1623e06b933eSAndrey Ulanov 	event++;
162480b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
162580b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1626ce07d891SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1627fe78fcc8SEric W. Biederman 			umount_mnt(mnt);
162856cbb429SAl Viro 			hlist_add_head(&mnt->mnt_umount, &unmounted);
1629ce07d891SEric W. Biederman 		}
1630e0c9c0afSEric W. Biederman 		else umount_tree(mnt, UMOUNT_CONNECTED);
163180b5dce8SEric W. Biederman 	}
163280b5dce8SEric W. Biederman 	put_mountpoint(mp);
163380b5dce8SEric W. Biederman out_unlock:
16343895dbf8SEric W. Biederman 	unlock_mount_hash();
163580b5dce8SEric W. Biederman 	namespace_unlock();
163680b5dce8SEric W. Biederman }
163780b5dce8SEric W. Biederman 
163880b5dce8SEric W. Biederman /*
16399b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
16409b40bc90SAl Viro  */
16419b40bc90SAl Viro static inline bool may_mount(void)
16429b40bc90SAl Viro {
16439b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
16449b40bc90SAl Viro }
16459b40bc90SAl Viro 
1646df2474a2SJeff Layton #ifdef	CONFIG_MANDATORY_FILE_LOCKING
16479e8925b6SJeff Layton static inline bool may_mandlock(void)
16489e8925b6SJeff Layton {
164995ace754SEric W. Biederman 	return capable(CAP_SYS_ADMIN);
16509e8925b6SJeff Layton }
1651df2474a2SJeff Layton #else
1652df2474a2SJeff Layton static inline bool may_mandlock(void)
1653df2474a2SJeff Layton {
1654df2474a2SJeff Layton 	pr_warn("VFS: \"mand\" mount option not supported");
1655df2474a2SJeff Layton 	return false;
1656df2474a2SJeff Layton }
1657df2474a2SJeff Layton #endif
16589e8925b6SJeff Layton 
16599b40bc90SAl Viro /*
16601da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
16611da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
16621da177e4SLinus Torvalds  *
16631da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
16641da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
16651da177e4SLinus Torvalds  */
16661da177e4SLinus Torvalds 
16673a18ef5cSDominik Brodowski int ksys_umount(char __user *name, int flags)
16681da177e4SLinus Torvalds {
16692d8f3038SAl Viro 	struct path path;
1670900148dcSAl Viro 	struct mount *mnt;
16711da177e4SLinus Torvalds 	int retval;
1672161aff1dSAl Viro 	int lookup_flags = LOOKUP_MOUNTPOINT;
16731da177e4SLinus Torvalds 
1674db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1675db1f05bbSMiklos Szeredi 		return -EINVAL;
1676db1f05bbSMiklos Szeredi 
16779b40bc90SAl Viro 	if (!may_mount())
16789b40bc90SAl Viro 		return -EPERM;
16799b40bc90SAl Viro 
1680db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1681db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1682db1f05bbSMiklos Szeredi 
1683161aff1dSAl Viro 	retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
16841da177e4SLinus Torvalds 	if (retval)
16851da177e4SLinus Torvalds 		goto out;
1686900148dcSAl Viro 	mnt = real_mount(path.mnt);
16871da177e4SLinus Torvalds 	retval = -EINVAL;
16882d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
16891da177e4SLinus Torvalds 		goto dput_and_out;
1690143c8c91SAl Viro 	if (!check_mnt(mnt))
16911da177e4SLinus Torvalds 		goto dput_and_out;
169225d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
16935ff9d8a6SEric W. Biederman 		goto dput_and_out;
1694b2f5d4dcSEric W. Biederman 	retval = -EPERM;
1695b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1696b2f5d4dcSEric W. Biederman 		goto dput_and_out;
16971da177e4SLinus Torvalds 
1698900148dcSAl Viro 	retval = do_umount(mnt, flags);
16991da177e4SLinus Torvalds dput_and_out:
1700429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
17012d8f3038SAl Viro 	dput(path.dentry);
1702900148dcSAl Viro 	mntput_no_expire(mnt);
17031da177e4SLinus Torvalds out:
17041da177e4SLinus Torvalds 	return retval;
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds 
17073a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
17083a18ef5cSDominik Brodowski {
17093a18ef5cSDominik Brodowski 	return ksys_umount(name, flags);
17103a18ef5cSDominik Brodowski }
17113a18ef5cSDominik Brodowski 
17121da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds /*
17151da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
17161da177e4SLinus Torvalds  */
1717bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
17181da177e4SLinus Torvalds {
17193a18ef5cSDominik Brodowski 	return ksys_umount(name, 0);
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds #endif
17231da177e4SLinus Torvalds 
17244ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
17258823c079SEric W. Biederman {
17264ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1727e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1728e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
17294ce5d2b1SEric W. Biederman }
17304ce5d2b1SEric W. Biederman 
1731213921f9SEric Biggers static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
173258be2825SAl Viro {
173358be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
173458be2825SAl Viro }
173558be2825SAl Viro 
17364ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
17374ce5d2b1SEric W. Biederman {
17384ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
17394ce5d2b1SEric W. Biederman 	 * mount namespace loop?
17404ce5d2b1SEric W. Biederman 	 */
17414ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
17424ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
17434ce5d2b1SEric W. Biederman 		return false;
17444ce5d2b1SEric W. Biederman 
1745f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
17468823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
17478823c079SEric W. Biederman }
17488823c079SEric W. Biederman 
174987129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
175036341f64SRam Pai 					int flag)
17511da177e4SLinus Torvalds {
175284d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
17531da177e4SLinus Torvalds 
17544ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
17554ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
17564ce5d2b1SEric W. Biederman 
17574ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1758be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
17599676f0c6SRam Pai 
176036341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1761be34d1a3SDavid Howells 	if (IS_ERR(q))
1762be34d1a3SDavid Howells 		return q;
1763be34d1a3SDavid Howells 
1764a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	p = mnt;
17676b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1768315fc83eSAl Viro 		struct mount *s;
17697ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
17701da177e4SLinus Torvalds 			continue;
17711da177e4SLinus Torvalds 
1772909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
17734ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
17744ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
1775df7342b2SEric W. Biederman 				if (s->mnt.mnt_flags & MNT_LOCKED) {
1776df7342b2SEric W. Biederman 					/* Both unbindable and locked. */
1777df7342b2SEric W. Biederman 					q = ERR_PTR(-EPERM);
1778df7342b2SEric W. Biederman 					goto out;
1779df7342b2SEric W. Biederman 				} else {
17804ce5d2b1SEric W. Biederman 					s = skip_mnt_tree(s);
17814ce5d2b1SEric W. Biederman 					continue;
17824ce5d2b1SEric W. Biederman 				}
1783df7342b2SEric W. Biederman 			}
17844ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
17854ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
17869676f0c6SRam Pai 				s = skip_mnt_tree(s);
17879676f0c6SRam Pai 				continue;
17889676f0c6SRam Pai 			}
17890714a533SAl Viro 			while (p != s->mnt_parent) {
17900714a533SAl Viro 				p = p->mnt_parent;
17910714a533SAl Viro 				q = q->mnt_parent;
17921da177e4SLinus Torvalds 			}
179387129cc0SAl Viro 			p = s;
179484d17192SAl Viro 			parent = q;
179587129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1796be34d1a3SDavid Howells 			if (IS_ERR(q))
1797be34d1a3SDavid Howells 				goto out;
1798719ea2fbSAl Viro 			lock_mount_hash();
17991a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
18001064f874SEric W. Biederman 			attach_mnt(q, parent, p->mnt_mp);
1801719ea2fbSAl Viro 			unlock_mount_hash();
18021da177e4SLinus Torvalds 		}
18031da177e4SLinus Torvalds 	}
18041da177e4SLinus Torvalds 	return res;
1805be34d1a3SDavid Howells out:
18061da177e4SLinus Torvalds 	if (res) {
1807719ea2fbSAl Viro 		lock_mount_hash();
1808e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
1809719ea2fbSAl Viro 		unlock_mount_hash();
18101da177e4SLinus Torvalds 	}
1811be34d1a3SDavid Howells 	return q;
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
1814be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1815be34d1a3SDavid Howells 
1816ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path)
18178aec0809SAl Viro {
1818cb338d06SAl Viro 	struct mount *tree;
181997216be0SAl Viro 	namespace_lock();
1820cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
1821cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
1822cd4a4017SEric W. Biederman 	else
182387129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
182487129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
1825328e6d90SAl Viro 	namespace_unlock();
1826be34d1a3SDavid Howells 	if (IS_ERR(tree))
182752e220d3SDan Carpenter 		return ERR_CAST(tree);
1828be34d1a3SDavid Howells 	return &tree->mnt;
18298aec0809SAl Viro }
18308aec0809SAl Viro 
1831a07b2000SAl Viro static void free_mnt_ns(struct mnt_namespace *);
1832a07b2000SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
1833a07b2000SAl Viro 
1834a07b2000SAl Viro void dissolve_on_fput(struct vfsmount *mnt)
1835a07b2000SAl Viro {
1836a07b2000SAl Viro 	struct mnt_namespace *ns;
1837a07b2000SAl Viro 	namespace_lock();
1838a07b2000SAl Viro 	lock_mount_hash();
1839a07b2000SAl Viro 	ns = real_mount(mnt)->mnt_ns;
184044dfd84aSDavid Howells 	if (ns) {
184144dfd84aSDavid Howells 		if (is_anon_ns(ns))
1842a07b2000SAl Viro 			umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
184344dfd84aSDavid Howells 		else
184444dfd84aSDavid Howells 			ns = NULL;
184544dfd84aSDavid Howells 	}
1846a07b2000SAl Viro 	unlock_mount_hash();
1847a07b2000SAl Viro 	namespace_unlock();
184844dfd84aSDavid Howells 	if (ns)
1849a07b2000SAl Viro 		free_mnt_ns(ns);
1850a07b2000SAl Viro }
1851a07b2000SAl Viro 
18528aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
18538aec0809SAl Viro {
185497216be0SAl Viro 	namespace_lock();
1855719ea2fbSAl Viro 	lock_mount_hash();
18569c8e0a1bSEric W. Biederman 	umount_tree(real_mount(mnt), 0);
1857719ea2fbSAl Viro 	unlock_mount_hash();
18583ab6abeeSAl Viro 	namespace_unlock();
18598aec0809SAl Viro }
18608aec0809SAl Viro 
1861c771d683SMiklos Szeredi /**
1862c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
1863c771d683SMiklos Szeredi  *
1864c771d683SMiklos Szeredi  * This creates a new vfsmount, which will be the clone of @path.  The new will
1865c771d683SMiklos Szeredi  * not be attached anywhere in the namespace and will be private (i.e. changes
1866c771d683SMiklos Szeredi  * to the originating mount won't be propagated into this).
1867c771d683SMiklos Szeredi  *
1868c771d683SMiklos Szeredi  * Release with mntput().
1869c771d683SMiklos Szeredi  */
1870ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path)
1871c771d683SMiklos Szeredi {
1872c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
1873c771d683SMiklos Szeredi 	struct mount *new_mnt;
1874c771d683SMiklos Szeredi 
1875c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
1876c771d683SMiklos Szeredi 		return ERR_PTR(-EINVAL);
1877c771d683SMiklos Szeredi 
1878c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1879c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
1880c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
1881c771d683SMiklos Szeredi 
1882c771d683SMiklos Szeredi 	return &new_mnt->mnt;
1883c771d683SMiklos Szeredi }
1884c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
1885c771d683SMiklos Szeredi 
18861f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
18871f707137SAl Viro 		   struct vfsmount *root)
18881f707137SAl Viro {
18891a4eeaf2SAl Viro 	struct mount *mnt;
18901f707137SAl Viro 	int res = f(root, arg);
18911f707137SAl Viro 	if (res)
18921f707137SAl Viro 		return res;
18931a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
18941a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
18951f707137SAl Viro 		if (res)
18961f707137SAl Viro 			return res;
18971f707137SAl Viro 	}
18981f707137SAl Viro 	return 0;
18991f707137SAl Viro }
19001f707137SAl Viro 
19013bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt)
19023bd045ccSAl Viro {
19033bd045ccSAl Viro 	struct mount *p;
19043bd045ccSAl Viro 
19053bd045ccSAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
19063bd045ccSAl Viro 		int flags = p->mnt.mnt_flags;
19073bd045ccSAl Viro 		/* Don't allow unprivileged users to change mount flags */
19083bd045ccSAl Viro 		flags |= MNT_LOCK_ATIME;
19093bd045ccSAl Viro 
19103bd045ccSAl Viro 		if (flags & MNT_READONLY)
19113bd045ccSAl Viro 			flags |= MNT_LOCK_READONLY;
19123bd045ccSAl Viro 
19133bd045ccSAl Viro 		if (flags & MNT_NODEV)
19143bd045ccSAl Viro 			flags |= MNT_LOCK_NODEV;
19153bd045ccSAl Viro 
19163bd045ccSAl Viro 		if (flags & MNT_NOSUID)
19173bd045ccSAl Viro 			flags |= MNT_LOCK_NOSUID;
19183bd045ccSAl Viro 
19193bd045ccSAl Viro 		if (flags & MNT_NOEXEC)
19203bd045ccSAl Viro 			flags |= MNT_LOCK_NOEXEC;
19213bd045ccSAl Viro 		/* Don't allow unprivileged users to reveal what is under a mount */
19223bd045ccSAl Viro 		if (list_empty(&p->mnt_expire))
19233bd045ccSAl Viro 			flags |= MNT_LOCKED;
19243bd045ccSAl Viro 		p->mnt.mnt_flags = flags;
19253bd045ccSAl Viro 	}
19263bd045ccSAl Viro }
19273bd045ccSAl Viro 
19284b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1929719f5d7fSMiklos Szeredi {
1930315fc83eSAl Viro 	struct mount *p;
1931719f5d7fSMiklos Szeredi 
1932909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1933fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
19344b8b21f4SAl Viro 			mnt_release_group_id(p);
1935719f5d7fSMiklos Szeredi 	}
1936719f5d7fSMiklos Szeredi }
1937719f5d7fSMiklos Szeredi 
19384b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1939719f5d7fSMiklos Szeredi {
1940315fc83eSAl Viro 	struct mount *p;
1941719f5d7fSMiklos Szeredi 
1942909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1943fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
19444b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1945719f5d7fSMiklos Szeredi 			if (err) {
19464b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1947719f5d7fSMiklos Szeredi 				return err;
1948719f5d7fSMiklos Szeredi 			}
1949719f5d7fSMiklos Szeredi 		}
1950719f5d7fSMiklos Szeredi 	}
1951719f5d7fSMiklos Szeredi 
1952719f5d7fSMiklos Szeredi 	return 0;
1953719f5d7fSMiklos Szeredi }
1954719f5d7fSMiklos Szeredi 
1955d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1956d2921684SEric W. Biederman {
1957d2921684SEric W. Biederman 	unsigned int max = READ_ONCE(sysctl_mount_max);
1958d2921684SEric W. Biederman 	unsigned int mounts = 0, old, pending, sum;
1959d2921684SEric W. Biederman 	struct mount *p;
1960d2921684SEric W. Biederman 
1961d2921684SEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt))
1962d2921684SEric W. Biederman 		mounts++;
1963d2921684SEric W. Biederman 
1964d2921684SEric W. Biederman 	old = ns->mounts;
1965d2921684SEric W. Biederman 	pending = ns->pending_mounts;
1966d2921684SEric W. Biederman 	sum = old + pending;
1967d2921684SEric W. Biederman 	if ((old > sum) ||
1968d2921684SEric W. Biederman 	    (pending > sum) ||
1969d2921684SEric W. Biederman 	    (max < sum) ||
1970d2921684SEric W. Biederman 	    (mounts > (max - sum)))
1971d2921684SEric W. Biederman 		return -ENOSPC;
1972d2921684SEric W. Biederman 
1973d2921684SEric W. Biederman 	ns->pending_mounts = pending + mounts;
1974d2921684SEric W. Biederman 	return 0;
1975d2921684SEric W. Biederman }
1976d2921684SEric W. Biederman 
1977b90fa9aeSRam Pai /*
1978b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1979b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
198021444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
198121444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
198221444403SRam Pai  *  		   (done when source_mnt is moved)
1983b90fa9aeSRam Pai  *
1984b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1985b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
19869676f0c6SRam Pai  * ---------------------------------------------------------------------------
1987b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
19889676f0c6SRam Pai  * |**************************************************************************
19899676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
19909676f0c6SRam Pai  * | dest     |               |                |                |            |
19919676f0c6SRam Pai  * |   |      |               |                |                |            |
19929676f0c6SRam Pai  * |   v      |               |                |                |            |
19939676f0c6SRam Pai  * |**************************************************************************
19949676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
19955afe0022SRam Pai  * |          |               |                |                |            |
19969676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
19979676f0c6SRam Pai  * ***************************************************************************
1998b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1999b90fa9aeSRam Pai  * destination mount.
2000b90fa9aeSRam Pai  *
2001b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
2002b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
2003b90fa9aeSRam Pai  * 	 the peer group of the source mount.
2004b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
2005b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
2006b90fa9aeSRam Pai  *       mount.
20075afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
20085afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
20095afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
20105afe0022SRam Pai  *       is marked as 'shared and slave'.
20115afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
20125afe0022SRam Pai  * 	 source mount.
20135afe0022SRam Pai  *
20149676f0c6SRam Pai  * ---------------------------------------------------------------------------
201521444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
20169676f0c6SRam Pai  * |**************************************************************************
20179676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
20189676f0c6SRam Pai  * | dest     |               |                |                |            |
20199676f0c6SRam Pai  * |   |      |               |                |                |            |
20209676f0c6SRam Pai  * |   v      |               |                |                |            |
20219676f0c6SRam Pai  * |**************************************************************************
20229676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
20235afe0022SRam Pai  * |          |               |                |                |            |
20249676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
20259676f0c6SRam Pai  * ***************************************************************************
20265afe0022SRam Pai  *
20275afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
20285afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
202921444403SRam Pai  * (+*)  the mount is moved to the destination.
20305afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
20315afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
20325afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
20335afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
2034b90fa9aeSRam Pai  *
2035b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
2036b90fa9aeSRam Pai  * applied to each mount in the tree.
2037b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
2038b90fa9aeSRam Pai  * in allocations.
2039b90fa9aeSRam Pai  */
20400fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
204184d17192SAl Viro 			struct mount *dest_mnt,
204284d17192SAl Viro 			struct mountpoint *dest_mp,
20432763d119SAl Viro 			bool moving)
2044b90fa9aeSRam Pai {
20453bd045ccSAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
204638129a13SAl Viro 	HLIST_HEAD(tree_list);
2047d2921684SEric W. Biederman 	struct mnt_namespace *ns = dest_mnt->mnt_ns;
20481064f874SEric W. Biederman 	struct mountpoint *smp;
2049315fc83eSAl Viro 	struct mount *child, *p;
205038129a13SAl Viro 	struct hlist_node *n;
2051719f5d7fSMiklos Szeredi 	int err;
2052b90fa9aeSRam Pai 
20531064f874SEric W. Biederman 	/* Preallocate a mountpoint in case the new mounts need
20541064f874SEric W. Biederman 	 * to be tucked under other mounts.
20551064f874SEric W. Biederman 	 */
20561064f874SEric W. Biederman 	smp = get_mountpoint(source_mnt->mnt.mnt_root);
20571064f874SEric W. Biederman 	if (IS_ERR(smp))
20581064f874SEric W. Biederman 		return PTR_ERR(smp);
20591064f874SEric W. Biederman 
2060d2921684SEric W. Biederman 	/* Is there space to add these mounts to the mount namespace? */
20612763d119SAl Viro 	if (!moving) {
2062d2921684SEric W. Biederman 		err = count_mounts(ns, source_mnt);
2063d2921684SEric W. Biederman 		if (err)
2064d2921684SEric W. Biederman 			goto out;
2065d2921684SEric W. Biederman 	}
2066d2921684SEric W. Biederman 
2067fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
20680fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
2069719f5d7fSMiklos Szeredi 		if (err)
2070719f5d7fSMiklos Szeredi 			goto out;
207184d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
2072f2ebb3a9SAl Viro 		lock_mount_hash();
2073719f5d7fSMiklos Szeredi 		if (err)
2074719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
2075909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
20760f0afb1dSAl Viro 			set_mnt_shared(p);
20770b1b901bSAl Viro 	} else {
20780b1b901bSAl Viro 		lock_mount_hash();
2079b90fa9aeSRam Pai 	}
20802763d119SAl Viro 	if (moving) {
20812763d119SAl Viro 		unhash_mnt(source_mnt);
208284d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
2083143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
208421444403SRam Pai 	} else {
208544dfd84aSDavid Howells 		if (source_mnt->mnt_ns) {
208644dfd84aSDavid Howells 			/* move from anon - the caller will destroy */
208744dfd84aSDavid Howells 			list_del_init(&source_mnt->mnt_ns->list);
208844dfd84aSDavid Howells 		}
208984d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
20901064f874SEric W. Biederman 		commit_tree(source_mnt);
209121444403SRam Pai 	}
2092b90fa9aeSRam Pai 
209338129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
20941d6a32acSAl Viro 		struct mount *q;
209538129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
20961064f874SEric W. Biederman 		q = __lookup_mnt(&child->mnt_parent->mnt,
20971d6a32acSAl Viro 				 child->mnt_mountpoint);
20981064f874SEric W. Biederman 		if (q)
20991064f874SEric W. Biederman 			mnt_change_mountpoint(child, smp, q);
21003bd045ccSAl Viro 		/* Notice when we are propagating across user namespaces */
21013bd045ccSAl Viro 		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
21023bd045ccSAl Viro 			lock_mnt_tree(child);
2103d728cf79SChristian Brauner 		child->mnt.mnt_flags &= ~MNT_LOCKED;
21041064f874SEric W. Biederman 		commit_tree(child);
2105b90fa9aeSRam Pai 	}
21061064f874SEric W. Biederman 	put_mountpoint(smp);
2107719ea2fbSAl Viro 	unlock_mount_hash();
210899b7db7bSNick Piggin 
2109b90fa9aeSRam Pai 	return 0;
2110719f5d7fSMiklos Szeredi 
2111719f5d7fSMiklos Szeredi  out_cleanup_ids:
2112f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
2113f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2114d2921684SEric W. Biederman 		child->mnt_parent->mnt_ns->pending_mounts = 0;
2115e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
2116f2ebb3a9SAl Viro 	}
2117f2ebb3a9SAl Viro 	unlock_mount_hash();
21180fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
2119719f5d7fSMiklos Szeredi  out:
2120d2921684SEric W. Biederman 	ns->pending_mounts = 0;
21211064f874SEric W. Biederman 
21221064f874SEric W. Biederman 	read_seqlock_excl(&mount_lock);
21231064f874SEric W. Biederman 	put_mountpoint(smp);
21241064f874SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
21251064f874SEric W. Biederman 
2126719f5d7fSMiklos Szeredi 	return err;
2127b90fa9aeSRam Pai }
2128b90fa9aeSRam Pai 
212984d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
2130b12cea91SAl Viro {
2131b12cea91SAl Viro 	struct vfsmount *mnt;
213284d17192SAl Viro 	struct dentry *dentry = path->dentry;
2133b12cea91SAl Viro retry:
21345955102cSAl Viro 	inode_lock(dentry->d_inode);
213584d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
21365955102cSAl Viro 		inode_unlock(dentry->d_inode);
213784d17192SAl Viro 		return ERR_PTR(-ENOENT);
2138b12cea91SAl Viro 	}
213997216be0SAl Viro 	namespace_lock();
2140b12cea91SAl Viro 	mnt = lookup_mnt(path);
214184d17192SAl Viro 	if (likely(!mnt)) {
21423895dbf8SEric W. Biederman 		struct mountpoint *mp = get_mountpoint(dentry);
214384d17192SAl Viro 		if (IS_ERR(mp)) {
214497216be0SAl Viro 			namespace_unlock();
21455955102cSAl Viro 			inode_unlock(dentry->d_inode);
214684d17192SAl Viro 			return mp;
214784d17192SAl Viro 		}
214884d17192SAl Viro 		return mp;
214984d17192SAl Viro 	}
215097216be0SAl Viro 	namespace_unlock();
21515955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
2152b12cea91SAl Viro 	path_put(path);
2153b12cea91SAl Viro 	path->mnt = mnt;
215484d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
2155b12cea91SAl Viro 	goto retry;
2156b12cea91SAl Viro }
2157b12cea91SAl Viro 
215884d17192SAl Viro static void unlock_mount(struct mountpoint *where)
2159b12cea91SAl Viro {
216084d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
21613895dbf8SEric W. Biederman 
21623895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
216384d17192SAl Viro 	put_mountpoint(where);
21643895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
21653895dbf8SEric W. Biederman 
2166328e6d90SAl Viro 	namespace_unlock();
21675955102cSAl Viro 	inode_unlock(dentry->d_inode);
2168b12cea91SAl Viro }
2169b12cea91SAl Viro 
217084d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
21711da177e4SLinus Torvalds {
2172e462ec50SDavid Howells 	if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
21731da177e4SLinus Torvalds 		return -EINVAL;
21741da177e4SLinus Torvalds 
2175e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
2176e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
21771da177e4SLinus Torvalds 		return -ENOTDIR;
21781da177e4SLinus Torvalds 
21792763d119SAl Viro 	return attach_recursive_mnt(mnt, p, mp, false);
21801da177e4SLinus Torvalds }
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds /*
21837a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
21847a2e8a8fSValerie Aurora  */
21857a2e8a8fSValerie Aurora 
2186e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags)
21877a2e8a8fSValerie Aurora {
2188e462ec50SDavid Howells 	int type = ms_flags & ~(MS_REC | MS_SILENT);
21897a2e8a8fSValerie Aurora 
21907a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
21917a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
21927a2e8a8fSValerie Aurora 		return 0;
21937a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
21947a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
21957a2e8a8fSValerie Aurora 		return 0;
21967a2e8a8fSValerie Aurora 	return type;
21977a2e8a8fSValerie Aurora }
21987a2e8a8fSValerie Aurora 
21997a2e8a8fSValerie Aurora /*
220007b20889SRam Pai  * recursively change the type of the mountpoint.
220107b20889SRam Pai  */
2202e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags)
220307b20889SRam Pai {
2204315fc83eSAl Viro 	struct mount *m;
22054b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
2206e462ec50SDavid Howells 	int recurse = ms_flags & MS_REC;
22077a2e8a8fSValerie Aurora 	int type;
2208719f5d7fSMiklos Szeredi 	int err = 0;
220907b20889SRam Pai 
22102d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
221107b20889SRam Pai 		return -EINVAL;
221207b20889SRam Pai 
2213e462ec50SDavid Howells 	type = flags_to_propagation_type(ms_flags);
22147a2e8a8fSValerie Aurora 	if (!type)
22157a2e8a8fSValerie Aurora 		return -EINVAL;
22167a2e8a8fSValerie Aurora 
221797216be0SAl Viro 	namespace_lock();
2218719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
2219719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
2220719f5d7fSMiklos Szeredi 		if (err)
2221719f5d7fSMiklos Szeredi 			goto out_unlock;
2222719f5d7fSMiklos Szeredi 	}
2223719f5d7fSMiklos Szeredi 
2224719ea2fbSAl Viro 	lock_mount_hash();
2225909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
22260f0afb1dSAl Viro 		change_mnt_propagation(m, type);
2227719ea2fbSAl Viro 	unlock_mount_hash();
2228719f5d7fSMiklos Szeredi 
2229719f5d7fSMiklos Szeredi  out_unlock:
223097216be0SAl Viro 	namespace_unlock();
2231719f5d7fSMiklos Szeredi 	return err;
223207b20889SRam Pai }
223307b20889SRam Pai 
22345ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
22355ff9d8a6SEric W. Biederman {
22365ff9d8a6SEric W. Biederman 	struct mount *child;
22375ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
22385ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
22395ff9d8a6SEric W. Biederman 			continue;
22405ff9d8a6SEric W. Biederman 
22415ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
22425ff9d8a6SEric W. Biederman 			return true;
22435ff9d8a6SEric W. Biederman 	}
22445ff9d8a6SEric W. Biederman 	return false;
22455ff9d8a6SEric W. Biederman }
22465ff9d8a6SEric W. Biederman 
2247a07b2000SAl Viro static struct mount *__do_loopback(struct path *old_path, int recurse)
2248a07b2000SAl Viro {
2249a07b2000SAl Viro 	struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2250a07b2000SAl Viro 
2251a07b2000SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2252a07b2000SAl Viro 		return mnt;
2253a07b2000SAl Viro 
2254a07b2000SAl Viro 	if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2255a07b2000SAl Viro 		return mnt;
2256a07b2000SAl Viro 
2257a07b2000SAl Viro 	if (!recurse && has_locked_children(old, old_path->dentry))
2258a07b2000SAl Viro 		return mnt;
2259a07b2000SAl Viro 
2260a07b2000SAl Viro 	if (recurse)
2261a07b2000SAl Viro 		mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2262a07b2000SAl Viro 	else
2263a07b2000SAl Viro 		mnt = clone_mnt(old, old_path->dentry, 0);
2264a07b2000SAl Viro 
2265a07b2000SAl Viro 	if (!IS_ERR(mnt))
2266a07b2000SAl Viro 		mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2267a07b2000SAl Viro 
2268a07b2000SAl Viro 	return mnt;
2269a07b2000SAl Viro }
2270a07b2000SAl Viro 
227107b20889SRam Pai /*
22721da177e4SLinus Torvalds  * do loopback mount.
22731da177e4SLinus Torvalds  */
2274808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
22752dafe1c4SEric Sandeen 				int recurse)
22761da177e4SLinus Torvalds {
22772d92ab3cSAl Viro 	struct path old_path;
2278a07b2000SAl Viro 	struct mount *mnt = NULL, *parent;
227984d17192SAl Viro 	struct mountpoint *mp;
228057eccb83SAl Viro 	int err;
22811da177e4SLinus Torvalds 	if (!old_name || !*old_name)
22821da177e4SLinus Torvalds 		return -EINVAL;
2283815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
22841da177e4SLinus Torvalds 	if (err)
22851da177e4SLinus Torvalds 		return err;
22861da177e4SLinus Torvalds 
22878823c079SEric W. Biederman 	err = -EINVAL;
22884ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
22898823c079SEric W. Biederman 		goto out;
22908823c079SEric W. Biederman 
229184d17192SAl Viro 	mp = lock_mount(path);
2292a07b2000SAl Viro 	if (IS_ERR(mp)) {
229384d17192SAl Viro 		err = PTR_ERR(mp);
22949676f0c6SRam Pai 		goto out;
2295a07b2000SAl Viro 	}
22969676f0c6SRam Pai 
229784d17192SAl Viro 	parent = real_mount(path->mnt);
2298e149ed2bSAl Viro 	if (!check_mnt(parent))
2299e149ed2bSAl Viro 		goto out2;
2300e149ed2bSAl Viro 
2301a07b2000SAl Viro 	mnt = __do_loopback(&old_path, recurse);
2302be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2303be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2304e9c5d8a5SAndrey Vagin 		goto out2;
2305be34d1a3SDavid Howells 	}
2306ccd48bc7SAl Viro 
230784d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
23081da177e4SLinus Torvalds 	if (err) {
2309719ea2fbSAl Viro 		lock_mount_hash();
2310e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2311719ea2fbSAl Viro 		unlock_mount_hash();
23125b83d2c5SRam Pai 	}
2313b12cea91SAl Viro out2:
231484d17192SAl Viro 	unlock_mount(mp);
2315ccd48bc7SAl Viro out:
23162d92ab3cSAl Viro 	path_put(&old_path);
23171da177e4SLinus Torvalds 	return err;
23181da177e4SLinus Torvalds }
23191da177e4SLinus Torvalds 
2320a07b2000SAl Viro static struct file *open_detached_copy(struct path *path, bool recursive)
2321a07b2000SAl Viro {
2322a07b2000SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2323a07b2000SAl Viro 	struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2324a07b2000SAl Viro 	struct mount *mnt, *p;
2325a07b2000SAl Viro 	struct file *file;
2326a07b2000SAl Viro 
2327a07b2000SAl Viro 	if (IS_ERR(ns))
2328a07b2000SAl Viro 		return ERR_CAST(ns);
2329a07b2000SAl Viro 
2330a07b2000SAl Viro 	namespace_lock();
2331a07b2000SAl Viro 	mnt = __do_loopback(path, recursive);
2332a07b2000SAl Viro 	if (IS_ERR(mnt)) {
2333a07b2000SAl Viro 		namespace_unlock();
2334a07b2000SAl Viro 		free_mnt_ns(ns);
2335a07b2000SAl Viro 		return ERR_CAST(mnt);
2336a07b2000SAl Viro 	}
2337a07b2000SAl Viro 
2338a07b2000SAl Viro 	lock_mount_hash();
2339a07b2000SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2340a07b2000SAl Viro 		p->mnt_ns = ns;
2341a07b2000SAl Viro 		ns->mounts++;
2342a07b2000SAl Viro 	}
2343a07b2000SAl Viro 	ns->root = mnt;
2344a07b2000SAl Viro 	list_add_tail(&ns->list, &mnt->mnt_list);
2345a07b2000SAl Viro 	mntget(&mnt->mnt);
2346a07b2000SAl Viro 	unlock_mount_hash();
2347a07b2000SAl Viro 	namespace_unlock();
2348a07b2000SAl Viro 
2349a07b2000SAl Viro 	mntput(path->mnt);
2350a07b2000SAl Viro 	path->mnt = &mnt->mnt;
2351a07b2000SAl Viro 	file = dentry_open(path, O_PATH, current_cred());
2352a07b2000SAl Viro 	if (IS_ERR(file))
2353a07b2000SAl Viro 		dissolve_on_fput(path->mnt);
2354a07b2000SAl Viro 	else
2355a07b2000SAl Viro 		file->f_mode |= FMODE_NEED_UNMOUNT;
2356a07b2000SAl Viro 	return file;
2357a07b2000SAl Viro }
2358a07b2000SAl Viro 
23592658ce09SBen Dooks SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
2360a07b2000SAl Viro {
2361a07b2000SAl Viro 	struct file *file;
2362a07b2000SAl Viro 	struct path path;
2363a07b2000SAl Viro 	int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2364a07b2000SAl Viro 	bool detached = flags & OPEN_TREE_CLONE;
2365a07b2000SAl Viro 	int error;
2366a07b2000SAl Viro 	int fd;
2367a07b2000SAl Viro 
2368a07b2000SAl Viro 	BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2369a07b2000SAl Viro 
2370a07b2000SAl Viro 	if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2371a07b2000SAl Viro 		      AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2372a07b2000SAl Viro 		      OPEN_TREE_CLOEXEC))
2373a07b2000SAl Viro 		return -EINVAL;
2374a07b2000SAl Viro 
2375a07b2000SAl Viro 	if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2376a07b2000SAl Viro 		return -EINVAL;
2377a07b2000SAl Viro 
2378a07b2000SAl Viro 	if (flags & AT_NO_AUTOMOUNT)
2379a07b2000SAl Viro 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
2380a07b2000SAl Viro 	if (flags & AT_SYMLINK_NOFOLLOW)
2381a07b2000SAl Viro 		lookup_flags &= ~LOOKUP_FOLLOW;
2382a07b2000SAl Viro 	if (flags & AT_EMPTY_PATH)
2383a07b2000SAl Viro 		lookup_flags |= LOOKUP_EMPTY;
2384a07b2000SAl Viro 
2385a07b2000SAl Viro 	if (detached && !may_mount())
2386a07b2000SAl Viro 		return -EPERM;
2387a07b2000SAl Viro 
2388a07b2000SAl Viro 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
2389a07b2000SAl Viro 	if (fd < 0)
2390a07b2000SAl Viro 		return fd;
2391a07b2000SAl Viro 
2392a07b2000SAl Viro 	error = user_path_at(dfd, filename, lookup_flags, &path);
2393a07b2000SAl Viro 	if (unlikely(error)) {
2394a07b2000SAl Viro 		file = ERR_PTR(error);
2395a07b2000SAl Viro 	} else {
2396a07b2000SAl Viro 		if (detached)
2397a07b2000SAl Viro 			file = open_detached_copy(&path, flags & AT_RECURSIVE);
2398a07b2000SAl Viro 		else
2399a07b2000SAl Viro 			file = dentry_open(&path, O_PATH, current_cred());
2400a07b2000SAl Viro 		path_put(&path);
2401a07b2000SAl Viro 	}
2402a07b2000SAl Viro 	if (IS_ERR(file)) {
2403a07b2000SAl Viro 		put_unused_fd(fd);
2404a07b2000SAl Viro 		return PTR_ERR(file);
2405a07b2000SAl Viro 	}
2406a07b2000SAl Viro 	fd_install(fd, file);
2407a07b2000SAl Viro 	return fd;
2408a07b2000SAl Viro }
2409a07b2000SAl Viro 
241043f5e655SDavid Howells /*
241143f5e655SDavid Howells  * Don't allow locked mount flags to be cleared.
241243f5e655SDavid Howells  *
241343f5e655SDavid Howells  * No locks need to be held here while testing the various MNT_LOCK
241443f5e655SDavid Howells  * flags because those flags can never be cleared once they are set.
241543f5e655SDavid Howells  */
241643f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
24172e4b7fcdSDave Hansen {
241843f5e655SDavid Howells 	unsigned int fl = mnt->mnt.mnt_flags;
24192e4b7fcdSDave Hansen 
242043f5e655SDavid Howells 	if ((fl & MNT_LOCK_READONLY) &&
242143f5e655SDavid Howells 	    !(mnt_flags & MNT_READONLY))
242243f5e655SDavid Howells 		return false;
242343f5e655SDavid Howells 
242443f5e655SDavid Howells 	if ((fl & MNT_LOCK_NODEV) &&
242543f5e655SDavid Howells 	    !(mnt_flags & MNT_NODEV))
242643f5e655SDavid Howells 		return false;
242743f5e655SDavid Howells 
242843f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOSUID) &&
242943f5e655SDavid Howells 	    !(mnt_flags & MNT_NOSUID))
243043f5e655SDavid Howells 		return false;
243143f5e655SDavid Howells 
243243f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOEXEC) &&
243343f5e655SDavid Howells 	    !(mnt_flags & MNT_NOEXEC))
243443f5e655SDavid Howells 		return false;
243543f5e655SDavid Howells 
243643f5e655SDavid Howells 	if ((fl & MNT_LOCK_ATIME) &&
243743f5e655SDavid Howells 	    ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
243843f5e655SDavid Howells 		return false;
243943f5e655SDavid Howells 
244043f5e655SDavid Howells 	return true;
244143f5e655SDavid Howells }
244243f5e655SDavid Howells 
244343f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
244443f5e655SDavid Howells {
244543f5e655SDavid Howells 	bool readonly_request = (mnt_flags & MNT_READONLY);
244643f5e655SDavid Howells 
244743f5e655SDavid Howells 	if (readonly_request == __mnt_is_readonly(&mnt->mnt))
24482e4b7fcdSDave Hansen 		return 0;
24492e4b7fcdSDave Hansen 
24502e4b7fcdSDave Hansen 	if (readonly_request)
245143f5e655SDavid Howells 		return mnt_make_readonly(mnt);
245243f5e655SDavid Howells 
245343f5e655SDavid Howells 	return __mnt_unmake_readonly(mnt);
245443f5e655SDavid Howells }
245543f5e655SDavid Howells 
245643f5e655SDavid Howells /*
245743f5e655SDavid Howells  * Update the user-settable attributes on a mount.  The caller must hold
245843f5e655SDavid Howells  * sb->s_umount for writing.
245943f5e655SDavid Howells  */
246043f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
246143f5e655SDavid Howells {
246243f5e655SDavid Howells 	lock_mount_hash();
246343f5e655SDavid Howells 	mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
246443f5e655SDavid Howells 	mnt->mnt.mnt_flags = mnt_flags;
246543f5e655SDavid Howells 	touch_mnt_namespace(mnt->mnt_ns);
246643f5e655SDavid Howells 	unlock_mount_hash();
246743f5e655SDavid Howells }
246843f5e655SDavid Howells 
2469f8b92ba6SDeepa Dinamani static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
2470f8b92ba6SDeepa Dinamani {
2471f8b92ba6SDeepa Dinamani 	struct super_block *sb = mnt->mnt_sb;
2472f8b92ba6SDeepa Dinamani 
2473f8b92ba6SDeepa Dinamani 	if (!__mnt_is_readonly(mnt) &&
2474f8b92ba6SDeepa Dinamani 	   (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
2475f8b92ba6SDeepa Dinamani 		char *buf = (char *)__get_free_page(GFP_KERNEL);
2476f8b92ba6SDeepa Dinamani 		char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM);
2477f8b92ba6SDeepa Dinamani 		struct tm tm;
2478f8b92ba6SDeepa Dinamani 
2479f8b92ba6SDeepa Dinamani 		time64_to_tm(sb->s_time_max, 0, &tm);
2480f8b92ba6SDeepa Dinamani 
24810ecee669SEric Biggers 		pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n",
24820ecee669SEric Biggers 			sb->s_type->name,
24830ecee669SEric Biggers 			is_mounted(mnt) ? "remounted" : "mounted",
24840ecee669SEric Biggers 			mntpath,
2485f8b92ba6SDeepa Dinamani 			tm.tm_year+1900, (unsigned long long)sb->s_time_max);
2486f8b92ba6SDeepa Dinamani 
2487f8b92ba6SDeepa Dinamani 		free_page((unsigned long)buf);
2488f8b92ba6SDeepa Dinamani 	}
2489f8b92ba6SDeepa Dinamani }
2490f8b92ba6SDeepa Dinamani 
249143f5e655SDavid Howells /*
249243f5e655SDavid Howells  * Handle reconfiguration of the mountpoint only without alteration of the
249343f5e655SDavid Howells  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
249443f5e655SDavid Howells  * to mount(2).
249543f5e655SDavid Howells  */
249643f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
249743f5e655SDavid Howells {
249843f5e655SDavid Howells 	struct super_block *sb = path->mnt->mnt_sb;
249943f5e655SDavid Howells 	struct mount *mnt = real_mount(path->mnt);
250043f5e655SDavid Howells 	int ret;
250143f5e655SDavid Howells 
250243f5e655SDavid Howells 	if (!check_mnt(mnt))
250343f5e655SDavid Howells 		return -EINVAL;
250443f5e655SDavid Howells 
250543f5e655SDavid Howells 	if (path->dentry != mnt->mnt.mnt_root)
250643f5e655SDavid Howells 		return -EINVAL;
250743f5e655SDavid Howells 
250843f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
250943f5e655SDavid Howells 		return -EPERM;
251043f5e655SDavid Howells 
251143f5e655SDavid Howells 	down_write(&sb->s_umount);
251243f5e655SDavid Howells 	ret = change_mount_ro_state(mnt, mnt_flags);
251343f5e655SDavid Howells 	if (ret == 0)
251443f5e655SDavid Howells 		set_mount_attributes(mnt, mnt_flags);
251543f5e655SDavid Howells 	up_write(&sb->s_umount);
2516f8b92ba6SDeepa Dinamani 
2517f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2518f8b92ba6SDeepa Dinamani 
251943f5e655SDavid Howells 	return ret;
25202e4b7fcdSDave Hansen }
25212e4b7fcdSDave Hansen 
25221da177e4SLinus Torvalds /*
25231da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
25241da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
25251da177e4SLinus Torvalds  * on it - tough luck.
25261da177e4SLinus Torvalds  */
2527e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags,
2528e462ec50SDavid Howells 		      int mnt_flags, void *data)
25291da177e4SLinus Torvalds {
25301da177e4SLinus Torvalds 	int err;
25312d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2532143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
25338d0347f6SDavid Howells 	struct fs_context *fc;
25341da177e4SLinus Torvalds 
2535143c8c91SAl Viro 	if (!check_mnt(mnt))
25361da177e4SLinus Torvalds 		return -EINVAL;
25371da177e4SLinus Torvalds 
25382d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
25391da177e4SLinus Torvalds 		return -EINVAL;
25401da177e4SLinus Torvalds 
254143f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
254207b64558SEric W. Biederman 		return -EPERM;
25439566d674SEric W. Biederman 
25448d0347f6SDavid Howells 	fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
25458d0347f6SDavid Howells 	if (IS_ERR(fc))
25468d0347f6SDavid Howells 		return PTR_ERR(fc);
2547ff36fe2cSEric Paris 
25488d0347f6SDavid Howells 	err = parse_monolithic_mount_data(fc, data);
25498d0347f6SDavid Howells 	if (!err) {
25501da177e4SLinus Torvalds 		down_write(&sb->s_umount);
255157eccb83SAl Viro 		err = -EPERM;
255243f5e655SDavid Howells 		if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
25538d0347f6SDavid Howells 			err = reconfigure_super(fc);
255443f5e655SDavid Howells 			if (!err)
255543f5e655SDavid Howells 				set_mount_attributes(mnt, mnt_flags);
25560e55a7ccSDan Williams 		}
25576339dab8SAl Viro 		up_write(&sb->s_umount);
25588d0347f6SDavid Howells 	}
2559f8b92ba6SDeepa Dinamani 
2560f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2561f8b92ba6SDeepa Dinamani 
25628d0347f6SDavid Howells 	put_fs_context(fc);
25631da177e4SLinus Torvalds 	return err;
25641da177e4SLinus Torvalds }
25651da177e4SLinus Torvalds 
2566cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
25679676f0c6SRam Pai {
2568315fc83eSAl Viro 	struct mount *p;
2569909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2570fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
25719676f0c6SRam Pai 			return 1;
25729676f0c6SRam Pai 	}
25739676f0c6SRam Pai 	return 0;
25749676f0c6SRam Pai }
25759676f0c6SRam Pai 
257644dfd84aSDavid Howells /*
257744dfd84aSDavid Howells  * Check that there aren't references to earlier/same mount namespaces in the
257844dfd84aSDavid Howells  * specified subtree.  Such references can act as pins for mount namespaces
257944dfd84aSDavid Howells  * that aren't checked by the mount-cycle checking code, thereby allowing
258044dfd84aSDavid Howells  * cycles to be made.
258144dfd84aSDavid Howells  */
258244dfd84aSDavid Howells static bool check_for_nsfs_mounts(struct mount *subtree)
258344dfd84aSDavid Howells {
258444dfd84aSDavid Howells 	struct mount *p;
258544dfd84aSDavid Howells 	bool ret = false;
258644dfd84aSDavid Howells 
258744dfd84aSDavid Howells 	lock_mount_hash();
258844dfd84aSDavid Howells 	for (p = subtree; p; p = next_mnt(p, subtree))
258944dfd84aSDavid Howells 		if (mnt_ns_loop(p->mnt.mnt_root))
259044dfd84aSDavid Howells 			goto out;
259144dfd84aSDavid Howells 
259244dfd84aSDavid Howells 	ret = true;
259344dfd84aSDavid Howells out:
259444dfd84aSDavid Howells 	unlock_mount_hash();
259544dfd84aSDavid Howells 	return ret;
259644dfd84aSDavid Howells }
259744dfd84aSDavid Howells 
25982db154b3SDavid Howells static int do_move_mount(struct path *old_path, struct path *new_path)
25991da177e4SLinus Torvalds {
260044dfd84aSDavid Howells 	struct mnt_namespace *ns;
2601676da58dSAl Viro 	struct mount *p;
26020fb54e50SAl Viro 	struct mount *old;
26032763d119SAl Viro 	struct mount *parent;
26042763d119SAl Viro 	struct mountpoint *mp, *old_mp;
260557eccb83SAl Viro 	int err;
260644dfd84aSDavid Howells 	bool attached;
26071da177e4SLinus Torvalds 
26082db154b3SDavid Howells 	mp = lock_mount(new_path);
260984d17192SAl Viro 	if (IS_ERR(mp))
26102db154b3SDavid Howells 		return PTR_ERR(mp);
2611cc53ce53SDavid Howells 
26122db154b3SDavid Howells 	old = real_mount(old_path->mnt);
26132db154b3SDavid Howells 	p = real_mount(new_path->mnt);
26142763d119SAl Viro 	parent = old->mnt_parent;
261544dfd84aSDavid Howells 	attached = mnt_has_parent(old);
26162763d119SAl Viro 	old_mp = old->mnt_mp;
261744dfd84aSDavid Howells 	ns = old->mnt_ns;
2618143c8c91SAl Viro 
26191da177e4SLinus Torvalds 	err = -EINVAL;
262044dfd84aSDavid Howells 	/* The mountpoint must be in our namespace. */
262144dfd84aSDavid Howells 	if (!check_mnt(p))
26222db154b3SDavid Howells 		goto out;
26231da177e4SLinus Torvalds 
2624570d7a98SEric Biggers 	/* The thing moved must be mounted... */
2625570d7a98SEric Biggers 	if (!is_mounted(&old->mnt))
262644dfd84aSDavid Howells 		goto out;
262744dfd84aSDavid Howells 
2628570d7a98SEric Biggers 	/* ... and either ours or the root of anon namespace */
2629570d7a98SEric Biggers 	if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
26302db154b3SDavid Howells 		goto out;
26311da177e4SLinus Torvalds 
26322db154b3SDavid Howells 	if (old->mnt.mnt_flags & MNT_LOCKED)
26332db154b3SDavid Howells 		goto out;
26342db154b3SDavid Howells 
26352db154b3SDavid Howells 	if (old_path->dentry != old_path->mnt->mnt_root)
26362db154b3SDavid Howells 		goto out;
26372db154b3SDavid Howells 
26382db154b3SDavid Howells 	if (d_is_dir(new_path->dentry) !=
26392db154b3SDavid Howells 	    d_is_dir(old_path->dentry))
26402db154b3SDavid Howells 		goto out;
264121444403SRam Pai 	/*
264221444403SRam Pai 	 * Don't move a mount residing in a shared parent.
264321444403SRam Pai 	 */
26442763d119SAl Viro 	if (attached && IS_MNT_SHARED(parent))
26452db154b3SDavid Howells 		goto out;
26469676f0c6SRam Pai 	/*
26479676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
26489676f0c6SRam Pai 	 * mount which is shared.
26499676f0c6SRam Pai 	 */
2650fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
26512db154b3SDavid Howells 		goto out;
26521da177e4SLinus Torvalds 	err = -ELOOP;
265344dfd84aSDavid Howells 	if (!check_for_nsfs_mounts(old))
265444dfd84aSDavid Howells 		goto out;
2655fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2656676da58dSAl Viro 		if (p == old)
26572db154b3SDavid Howells 			goto out;
26581da177e4SLinus Torvalds 
26592db154b3SDavid Howells 	err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
26602763d119SAl Viro 				   attached);
26614ac91378SJan Blunck 	if (err)
26622db154b3SDavid Howells 		goto out;
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
26651da177e4SLinus Torvalds 	 * automatically */
26666776db3dSAl Viro 	list_del_init(&old->mnt_expire);
26672763d119SAl Viro 	if (attached)
26682763d119SAl Viro 		put_mountpoint(old_mp);
26691da177e4SLinus Torvalds out:
26702db154b3SDavid Howells 	unlock_mount(mp);
267144dfd84aSDavid Howells 	if (!err) {
26722763d119SAl Viro 		if (attached)
26732763d119SAl Viro 			mntput_no_expire(parent);
26742763d119SAl Viro 		else
267544dfd84aSDavid Howells 			free_mnt_ns(ns);
267644dfd84aSDavid Howells 	}
26772db154b3SDavid Howells 	return err;
26782db154b3SDavid Howells }
26792db154b3SDavid Howells 
26802db154b3SDavid Howells static int do_move_mount_old(struct path *path, const char *old_name)
26812db154b3SDavid Howells {
26822db154b3SDavid Howells 	struct path old_path;
26832db154b3SDavid Howells 	int err;
26842db154b3SDavid Howells 
26852db154b3SDavid Howells 	if (!old_name || !*old_name)
26862db154b3SDavid Howells 		return -EINVAL;
26872db154b3SDavid Howells 
26882db154b3SDavid Howells 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
26892db154b3SDavid Howells 	if (err)
26902db154b3SDavid Howells 		return err;
26912db154b3SDavid Howells 
26922db154b3SDavid Howells 	err = do_move_mount(&old_path, path);
26932d92ab3cSAl Viro 	path_put(&old_path);
26941da177e4SLinus Torvalds 	return err;
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds 
26979d412a43SAl Viro /*
26989d412a43SAl Viro  * add a mount into a namespace's mount tree
26999d412a43SAl Viro  */
27008f11538eSAl Viro static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
27018f11538eSAl Viro 			struct path *path, int mnt_flags)
27029d412a43SAl Viro {
27038f11538eSAl Viro 	struct mount *parent = real_mount(path->mnt);
27049d412a43SAl Viro 
2705f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
27069d412a43SAl Viro 
270784d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2708156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2709156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
27108f11538eSAl Viro 			return -EINVAL;
2711156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
271284d17192SAl Viro 		if (!parent->mnt_ns)
27138f11538eSAl Viro 			return -EINVAL;
2714156cacb1SAl Viro 	}
27159d412a43SAl Viro 
27169d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
271795bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
27189d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
27198f11538eSAl Viro 		return -EBUSY;
27209d412a43SAl Viro 
2721e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
27228f11538eSAl Viro 		return -EINVAL;
27239d412a43SAl Viro 
272495bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
27258f11538eSAl Viro 	return graft_tree(newmnt, parent, mp);
27269d412a43SAl Viro }
2727b1e75df4SAl Viro 
2728132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
2729132e4608SDavid Howells 
2730132e4608SDavid Howells /*
2731132e4608SDavid Howells  * Create a new mount using a superblock configuration and request it
2732132e4608SDavid Howells  * be added to the namespace tree.
2733132e4608SDavid Howells  */
2734132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
2735132e4608SDavid Howells 			   unsigned int mnt_flags)
2736132e4608SDavid Howells {
2737132e4608SDavid Howells 	struct vfsmount *mnt;
27388f11538eSAl Viro 	struct mountpoint *mp;
2739132e4608SDavid Howells 	struct super_block *sb = fc->root->d_sb;
2740132e4608SDavid Howells 	int error;
2741132e4608SDavid Howells 
2742c9ce29edSAl Viro 	error = security_sb_kern_mount(sb);
2743c9ce29edSAl Viro 	if (!error && mount_too_revealing(sb, &mnt_flags))
2744c9ce29edSAl Viro 		error = -EPERM;
2745c9ce29edSAl Viro 
2746c9ce29edSAl Viro 	if (unlikely(error)) {
2747c9ce29edSAl Viro 		fc_drop_locked(fc);
2748c9ce29edSAl Viro 		return error;
2749132e4608SDavid Howells 	}
2750132e4608SDavid Howells 
2751132e4608SDavid Howells 	up_write(&sb->s_umount);
2752132e4608SDavid Howells 
2753132e4608SDavid Howells 	mnt = vfs_create_mount(fc);
2754132e4608SDavid Howells 	if (IS_ERR(mnt))
2755132e4608SDavid Howells 		return PTR_ERR(mnt);
2756132e4608SDavid Howells 
2757f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(mountpoint, mnt);
2758f8b92ba6SDeepa Dinamani 
27598f11538eSAl Viro 	mp = lock_mount(mountpoint);
27608f11538eSAl Viro 	if (IS_ERR(mp)) {
27618f11538eSAl Viro 		mntput(mnt);
27628f11538eSAl Viro 		return PTR_ERR(mp);
27638f11538eSAl Viro 	}
27648f11538eSAl Viro 	error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
27658f11538eSAl Viro 	unlock_mount(mp);
27660ecee669SEric Biggers 	if (error < 0)
27670ecee669SEric Biggers 		mntput(mnt);
2768f8b92ba6SDeepa Dinamani 	return error;
2769f8b92ba6SDeepa Dinamani }
2770f8b92ba6SDeepa Dinamani 
27711da177e4SLinus Torvalds /*
27721da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
27731da177e4SLinus Torvalds  * namespace's tree
27741da177e4SLinus Torvalds  */
2775e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
2776808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
27771da177e4SLinus Torvalds {
27780c55cfc4SEric W. Biederman 	struct file_system_type *type;
2779a0c9a8b8SAl Viro 	struct fs_context *fc;
2780a0c9a8b8SAl Viro 	const char *subtype = NULL;
2781a0c9a8b8SAl Viro 	int err = 0;
27821da177e4SLinus Torvalds 
27830c55cfc4SEric W. Biederman 	if (!fstype)
27841da177e4SLinus Torvalds 		return -EINVAL;
27851da177e4SLinus Torvalds 
27860c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
27870c55cfc4SEric W. Biederman 	if (!type)
27880c55cfc4SEric W. Biederman 		return -ENODEV;
27890c55cfc4SEric W. Biederman 
2790a0c9a8b8SAl Viro 	if (type->fs_flags & FS_HAS_SUBTYPE) {
2791a0c9a8b8SAl Viro 		subtype = strchr(fstype, '.');
2792a0c9a8b8SAl Viro 		if (subtype) {
2793a0c9a8b8SAl Viro 			subtype++;
2794a0c9a8b8SAl Viro 			if (!*subtype) {
27950c55cfc4SEric W. Biederman 				put_filesystem(type);
2796a0c9a8b8SAl Viro 				return -EINVAL;
2797a0c9a8b8SAl Viro 			}
2798a0c9a8b8SAl Viro 		}
27998654df4eSEric W. Biederman 	}
28008654df4eSEric W. Biederman 
2801a0c9a8b8SAl Viro 	fc = fs_context_for_mount(type, sb_flags);
2802a0c9a8b8SAl Viro 	put_filesystem(type);
2803a0c9a8b8SAl Viro 	if (IS_ERR(fc))
2804a0c9a8b8SAl Viro 		return PTR_ERR(fc);
2805a0c9a8b8SAl Viro 
28063e1aeb00SDavid Howells 	if (subtype)
28073e1aeb00SDavid Howells 		err = vfs_parse_fs_string(fc, "subtype",
28083e1aeb00SDavid Howells 					  subtype, strlen(subtype));
28093e1aeb00SDavid Howells 	if (!err && name)
28103e1aeb00SDavid Howells 		err = vfs_parse_fs_string(fc, "source", name, strlen(name));
2811a0c9a8b8SAl Viro 	if (!err)
2812a0c9a8b8SAl Viro 		err = parse_monolithic_mount_data(fc, data);
2813c3aabf07SAl Viro 	if (!err && !mount_capable(fc))
2814c3aabf07SAl Viro 		err = -EPERM;
2815a0c9a8b8SAl Viro 	if (!err)
2816a0c9a8b8SAl Viro 		err = vfs_get_tree(fc);
2817132e4608SDavid Howells 	if (!err)
2818132e4608SDavid Howells 		err = do_new_mount_fc(fc, path, mnt_flags);
2819a0c9a8b8SAl Viro 
2820a0c9a8b8SAl Viro 	put_fs_context(fc);
282115f9a3f3SAl Viro 	return err;
28221da177e4SLinus Torvalds }
28231da177e4SLinus Torvalds 
282419a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
282519a167afSAl Viro {
282626df6034SAl Viro 	struct dentry *dentry = path->dentry;
28278f11538eSAl Viro 	struct mountpoint *mp;
282825e195aaSAl Viro 	struct mount *mnt;
282919a167afSAl Viro 	int err;
283025e195aaSAl Viro 
283125e195aaSAl Viro 	if (!m)
283225e195aaSAl Viro 		return 0;
283325e195aaSAl Viro 	if (IS_ERR(m))
283425e195aaSAl Viro 		return PTR_ERR(m);
283525e195aaSAl Viro 
283625e195aaSAl Viro 	mnt = real_mount(m);
283719a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
283819a167afSAl Viro 	 * expired before we get a chance to add it
283919a167afSAl Viro 	 */
28406776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
284119a167afSAl Viro 
284219a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
284326df6034SAl Viro 	    m->mnt_root == dentry) {
2844b1e75df4SAl Viro 		err = -ELOOP;
284526df6034SAl Viro 		goto discard;
284619a167afSAl Viro 	}
284719a167afSAl Viro 
284826df6034SAl Viro 	/*
284926df6034SAl Viro 	 * we don't want to use lock_mount() - in this case finding something
285026df6034SAl Viro 	 * that overmounts our mountpoint to be means "quitely drop what we've
285126df6034SAl Viro 	 * got", not "try to mount it on top".
285226df6034SAl Viro 	 */
285326df6034SAl Viro 	inode_lock(dentry->d_inode);
285426df6034SAl Viro 	namespace_lock();
285526df6034SAl Viro 	if (unlikely(cant_mount(dentry))) {
285626df6034SAl Viro 		err = -ENOENT;
285726df6034SAl Viro 		goto discard_locked;
285826df6034SAl Viro 	}
285926df6034SAl Viro 	rcu_read_lock();
286026df6034SAl Viro 	if (unlikely(__lookup_mnt(path->mnt, dentry))) {
286126df6034SAl Viro 		rcu_read_unlock();
286226df6034SAl Viro 		err = 0;
286326df6034SAl Viro 		goto discard_locked;
286426df6034SAl Viro 	}
286526df6034SAl Viro 	rcu_read_unlock();
286626df6034SAl Viro 	mp = get_mountpoint(dentry);
28678f11538eSAl Viro 	if (IS_ERR(mp)) {
28688f11538eSAl Viro 		err = PTR_ERR(mp);
286926df6034SAl Viro 		goto discard_locked;
28708f11538eSAl Viro 	}
287126df6034SAl Viro 
28728f11538eSAl Viro 	err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
28738f11538eSAl Viro 	unlock_mount(mp);
287426df6034SAl Viro 	if (unlikely(err))
287526df6034SAl Viro 		goto discard;
287626df6034SAl Viro 	mntput(m);
2877b1e75df4SAl Viro 	return 0;
287826df6034SAl Viro 
287926df6034SAl Viro discard_locked:
288026df6034SAl Viro 	namespace_unlock();
288126df6034SAl Viro 	inode_unlock(dentry->d_inode);
288226df6034SAl Viro discard:
2883b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
28846776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
288597216be0SAl Viro 		namespace_lock();
28866776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
288797216be0SAl Viro 		namespace_unlock();
288819a167afSAl Viro 	}
2889b1e75df4SAl Viro 	mntput(m);
2890b1e75df4SAl Viro 	mntput(m);
289119a167afSAl Viro 	return err;
289219a167afSAl Viro }
289319a167afSAl Viro 
2894ea5b778aSDavid Howells /**
2895ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2896ea5b778aSDavid Howells  * @mnt: The mount to list.
2897ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2898ea5b778aSDavid Howells  */
2899ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2900ea5b778aSDavid Howells {
290197216be0SAl Viro 	namespace_lock();
2902ea5b778aSDavid Howells 
29036776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2904ea5b778aSDavid Howells 
290597216be0SAl Viro 	namespace_unlock();
2906ea5b778aSDavid Howells }
2907ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2908ea5b778aSDavid Howells 
2909ea5b778aSDavid Howells /*
29101da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
29111da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
29121da177e4SLinus Torvalds  * here
29131da177e4SLinus Torvalds  */
29141da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
29151da177e4SLinus Torvalds {
2916761d5c38SAl Viro 	struct mount *mnt, *next;
29171da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 	if (list_empty(mounts))
29201da177e4SLinus Torvalds 		return;
29211da177e4SLinus Torvalds 
292297216be0SAl Viro 	namespace_lock();
2923719ea2fbSAl Viro 	lock_mount_hash();
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
29261da177e4SLinus Torvalds 	 * following criteria:
29271da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
29281da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
29291da177e4SLinus Torvalds 	 *   cleared by mntput())
29301da177e4SLinus Torvalds 	 */
29316776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2932863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
29331ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
29341da177e4SLinus Torvalds 			continue;
29356776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
29361da177e4SLinus Torvalds 	}
2937bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
29386776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2939143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2940e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2941bcc5c7d2SAl Viro 	}
2942719ea2fbSAl Viro 	unlock_mount_hash();
29433ab6abeeSAl Viro 	namespace_unlock();
29441da177e4SLinus Torvalds }
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
29471da177e4SLinus Torvalds 
29481da177e4SLinus Torvalds /*
29495528f911STrond Myklebust  * Ripoff of 'select_parent()'
29505528f911STrond Myklebust  *
29515528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
29525528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
29535528f911STrond Myklebust  */
2954692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
29555528f911STrond Myklebust {
2956692afc31SAl Viro 	struct mount *this_parent = parent;
29575528f911STrond Myklebust 	struct list_head *next;
29585528f911STrond Myklebust 	int found = 0;
29595528f911STrond Myklebust 
29605528f911STrond Myklebust repeat:
29616b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
29625528f911STrond Myklebust resume:
29636b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
29645528f911STrond Myklebust 		struct list_head *tmp = next;
29656b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
29665528f911STrond Myklebust 
29675528f911STrond Myklebust 		next = tmp->next;
2968692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
29695528f911STrond Myklebust 			continue;
29705528f911STrond Myklebust 		/*
29715528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
29725528f911STrond Myklebust 		 */
29736b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
29745528f911STrond Myklebust 			this_parent = mnt;
29755528f911STrond Myklebust 			goto repeat;
29765528f911STrond Myklebust 		}
29775528f911STrond Myklebust 
29781ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
29796776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
29805528f911STrond Myklebust 			found++;
29815528f911STrond Myklebust 		}
29825528f911STrond Myklebust 	}
29835528f911STrond Myklebust 	/*
29845528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
29855528f911STrond Myklebust 	 */
29865528f911STrond Myklebust 	if (this_parent != parent) {
29876b41d536SAl Viro 		next = this_parent->mnt_child.next;
29880714a533SAl Viro 		this_parent = this_parent->mnt_parent;
29895528f911STrond Myklebust 		goto resume;
29905528f911STrond Myklebust 	}
29915528f911STrond Myklebust 	return found;
29925528f911STrond Myklebust }
29935528f911STrond Myklebust 
29945528f911STrond Myklebust /*
29955528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
29965528f911STrond Myklebust  * submounts of a specific parent mountpoint
299799b7db7bSNick Piggin  *
299848a066e7SAl Viro  * mount_lock must be held for write
29995528f911STrond Myklebust  */
3000b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
30015528f911STrond Myklebust {
30025528f911STrond Myklebust 	LIST_HEAD(graveyard);
3003761d5c38SAl Viro 	struct mount *m;
30045528f911STrond Myklebust 
30055528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
3006c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
3007bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
3008761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
30096776db3dSAl Viro 						mnt_expire);
3010143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
3011e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3012bcc5c7d2SAl Viro 		}
3013bcc5c7d2SAl Viro 	}
30145528f911STrond Myklebust }
30155528f911STrond Myklebust 
3016b40ef869SAl Viro void *copy_mount_options(const void __user * data)
30171da177e4SLinus Torvalds {
3018b40ef869SAl Viro 	char *copy;
301912efec56SAl Viro 	unsigned size;
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 	if (!data)
3022b40ef869SAl Viro 		return NULL;
30231da177e4SLinus Torvalds 
3024b40ef869SAl Viro 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
3025b40ef869SAl Viro 	if (!copy)
3026b40ef869SAl Viro 		return ERR_PTR(-ENOMEM);
30271da177e4SLinus Torvalds 
302812efec56SAl Viro 	size = PAGE_SIZE - offset_in_page(data);
30291da177e4SLinus Torvalds 
303012efec56SAl Viro 	if (copy_from_user(copy, data, size)) {
3031b40ef869SAl Viro 		kfree(copy);
3032b40ef869SAl Viro 		return ERR_PTR(-EFAULT);
30331da177e4SLinus Torvalds 	}
303412efec56SAl Viro 	if (size != PAGE_SIZE) {
303512efec56SAl Viro 		if (copy_from_user(copy + size, data + size, PAGE_SIZE - size))
303612efec56SAl Viro 			memset(copy + size, 0, PAGE_SIZE - size);
303712efec56SAl Viro 	}
3038b40ef869SAl Viro 	return copy;
30391da177e4SLinus Torvalds }
30401da177e4SLinus Torvalds 
3041b8850d1fSTim Gardner char *copy_mount_string(const void __user *data)
3042eca6f534SVegard Nossum {
3043fbdb4401SChandan Rajendra 	return data ? strndup_user(data, PATH_MAX) : NULL;
3044eca6f534SVegard Nossum }
3045eca6f534SVegard Nossum 
30461da177e4SLinus Torvalds /*
30471da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
30481da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
30491da177e4SLinus Torvalds  *
30501da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
30511da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
30521da177e4SLinus Torvalds  * information (or be NULL).
30531da177e4SLinus Torvalds  *
30541da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
30551da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
30561da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
30571da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
30581da177e4SLinus Torvalds  * and must be discarded.
30591da177e4SLinus Torvalds  */
30605e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name,
3061808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
30621da177e4SLinus Torvalds {
30632d92ab3cSAl Viro 	struct path path;
3064e462ec50SDavid Howells 	unsigned int mnt_flags = 0, sb_flags;
30651da177e4SLinus Torvalds 	int retval = 0;
30661da177e4SLinus Torvalds 
30671da177e4SLinus Torvalds 	/* Discard magic */
30681da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
30691da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds 	/* Basic sanity checks */
30721da177e4SLinus Torvalds 	if (data_page)
30731da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
30741da177e4SLinus Torvalds 
3075e462ec50SDavid Howells 	if (flags & MS_NOUSER)
3076e462ec50SDavid Howells 		return -EINVAL;
3077e462ec50SDavid Howells 
3078a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
3079ce6595a2SAl Viro 	retval = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
3080a27ab9f2STetsuo Handa 	if (retval)
3081a27ab9f2STetsuo Handa 		return retval;
3082a27ab9f2STetsuo Handa 
3083a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
3084a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
30850d5cadb8SAl Viro 	if (!retval && !may_mount())
30860d5cadb8SAl Viro 		retval = -EPERM;
3087e462ec50SDavid Howells 	if (!retval && (flags & SB_MANDLOCK) && !may_mandlock())
30889e8925b6SJeff Layton 		retval = -EPERM;
3089a27ab9f2STetsuo Handa 	if (retval)
3090a27ab9f2STetsuo Handa 		goto dput_out;
3091a27ab9f2STetsuo Handa 
3092613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
3093613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
30940a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
30950a1c01c9SMatthew Garrett 
30961da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
30971da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
30981da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
30991da177e4SLinus Torvalds 	if (flags & MS_NODEV)
31001da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
31011da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
31021da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
3103fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
3104fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
3105fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
3106fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
3107d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
3108d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
3109a9e5b732SDavid Howells 	if (flags & MS_RDONLY)
31102e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
3111fc33a7bbSChristoph Hellwig 
3112ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
3113ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
3114ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3115ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
3116ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
3117ffbc6f0eSEric W. Biederman 		mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
3118ffbc6f0eSEric W. Biederman 	}
3119ffbc6f0eSEric W. Biederman 
3120e462ec50SDavid Howells 	sb_flags = flags & (SB_RDONLY |
3121e462ec50SDavid Howells 			    SB_SYNCHRONOUS |
3122e462ec50SDavid Howells 			    SB_MANDLOCK |
3123e462ec50SDavid Howells 			    SB_DIRSYNC |
3124e462ec50SDavid Howells 			    SB_SILENT |
3125917086ffSMimi Zohar 			    SB_POSIXACL |
3126d7ee9469SMarkus Trippelsdorf 			    SB_LAZYTIME |
3127917086ffSMimi Zohar 			    SB_I_VERSION);
31281da177e4SLinus Torvalds 
312943f5e655SDavid Howells 	if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
313043f5e655SDavid Howells 		retval = do_reconfigure_mnt(&path, mnt_flags);
313143f5e655SDavid Howells 	else if (flags & MS_REMOUNT)
3132e462ec50SDavid Howells 		retval = do_remount(&path, flags, sb_flags, mnt_flags,
31331da177e4SLinus Torvalds 				    data_page);
31341da177e4SLinus Torvalds 	else if (flags & MS_BIND)
31352d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
31369676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
31372d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
31381da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
31392db154b3SDavid Howells 		retval = do_move_mount_old(&path, dev_name);
31401da177e4SLinus Torvalds 	else
3141e462ec50SDavid Howells 		retval = do_new_mount(&path, type_page, sb_flags, mnt_flags,
31421da177e4SLinus Torvalds 				      dev_name, data_page);
31431da177e4SLinus Torvalds dput_out:
31442d92ab3cSAl Viro 	path_put(&path);
31451da177e4SLinus Torvalds 	return retval;
31461da177e4SLinus Torvalds }
31471da177e4SLinus Torvalds 
3148537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3149537f7ccbSEric W. Biederman {
3150537f7ccbSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3151537f7ccbSEric W. Biederman }
3152537f7ccbSEric W. Biederman 
3153537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts)
3154537f7ccbSEric W. Biederman {
3155537f7ccbSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3156537f7ccbSEric W. Biederman }
3157537f7ccbSEric W. Biederman 
3158771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
3159771b1371SEric W. Biederman {
316074e83122SAl Viro 	if (!is_anon_ns(ns))
31616344c433SAl Viro 		ns_free_inum(&ns->ns);
3162537f7ccbSEric W. Biederman 	dec_mnt_namespaces(ns->ucounts);
3163771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
3164771b1371SEric W. Biederman 	kfree(ns);
3165771b1371SEric W. Biederman }
3166771b1371SEric W. Biederman 
31678823c079SEric W. Biederman /*
31688823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
31698823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
31708823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
31718823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
31728823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
31738823c079SEric W. Biederman  */
31748823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
31758823c079SEric W. Biederman 
317674e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
3177cf8d2c11STrond Myklebust {
3178cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
3179537f7ccbSEric W. Biederman 	struct ucounts *ucounts;
318098f842e6SEric W. Biederman 	int ret;
3181cf8d2c11STrond Myklebust 
3182537f7ccbSEric W. Biederman 	ucounts = inc_mnt_namespaces(user_ns);
3183537f7ccbSEric W. Biederman 	if (!ucounts)
3184df75e774SEric W. Biederman 		return ERR_PTR(-ENOSPC);
3185537f7ccbSEric W. Biederman 
318674e83122SAl Viro 	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
3187537f7ccbSEric W. Biederman 	if (!new_ns) {
3188537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
3189cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
3190537f7ccbSEric W. Biederman 	}
319174e83122SAl Viro 	if (!anon) {
31926344c433SAl Viro 		ret = ns_alloc_inum(&new_ns->ns);
319398f842e6SEric W. Biederman 		if (ret) {
319498f842e6SEric W. Biederman 			kfree(new_ns);
3195537f7ccbSEric W. Biederman 			dec_mnt_namespaces(ucounts);
319698f842e6SEric W. Biederman 			return ERR_PTR(ret);
319798f842e6SEric W. Biederman 		}
319874e83122SAl Viro 	}
319933c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
320074e83122SAl Viro 	if (!anon)
32018823c079SEric W. Biederman 		new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
3202cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
3203cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
3204cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
3205771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
3206537f7ccbSEric W. Biederman 	new_ns->ucounts = ucounts;
3207cf8d2c11STrond Myklebust 	return new_ns;
3208cf8d2c11STrond Myklebust }
3209cf8d2c11STrond Myklebust 
32100766f788SEmese Revfy __latent_entropy
32119559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
32129559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
32131da177e4SLinus Torvalds {
32146b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
32157f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
3216315fc83eSAl Viro 	struct mount *p, *q;
32179559f689SAl Viro 	struct mount *old;
3218cb338d06SAl Viro 	struct mount *new;
32197a472ef4SEric W. Biederman 	int copy_flags;
32201da177e4SLinus Torvalds 
32219559f689SAl Viro 	BUG_ON(!ns);
32229559f689SAl Viro 
32239559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
32249559f689SAl Viro 		get_mnt_ns(ns);
32259559f689SAl Viro 		return ns;
32269559f689SAl Viro 	}
32279559f689SAl Viro 
32289559f689SAl Viro 	old = ns->root;
32299559f689SAl Viro 
323074e83122SAl Viro 	new_ns = alloc_mnt_ns(user_ns, false);
3231cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
3232cf8d2c11STrond Myklebust 		return new_ns;
32331da177e4SLinus Torvalds 
323497216be0SAl Viro 	namespace_lock();
32351da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
32364ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
32379559f689SAl Viro 	if (user_ns != ns->user_ns)
32383bd045ccSAl Viro 		copy_flags |= CL_SHARED_TO_SLAVE;
32397a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
3240be34d1a3SDavid Howells 	if (IS_ERR(new)) {
3241328e6d90SAl Viro 		namespace_unlock();
3242771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
3243be34d1a3SDavid Howells 		return ERR_CAST(new);
32441da177e4SLinus Torvalds 	}
32453bd045ccSAl Viro 	if (user_ns != ns->user_ns) {
32463bd045ccSAl Viro 		lock_mount_hash();
32473bd045ccSAl Viro 		lock_mnt_tree(new);
32483bd045ccSAl Viro 		unlock_mount_hash();
32493bd045ccSAl Viro 	}
3250be08d6d2SAl Viro 	new_ns->root = new;
32511a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds 	/*
32541da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
32551da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
32561da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
32571da177e4SLinus Torvalds 	 */
3258909b0a88SAl Viro 	p = old;
3259cb338d06SAl Viro 	q = new;
32601da177e4SLinus Torvalds 	while (p) {
3261143c8c91SAl Viro 		q->mnt_ns = new_ns;
3262d2921684SEric W. Biederman 		new_ns->mounts++;
32639559f689SAl Viro 		if (new_fs) {
32649559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
32659559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
3266315fc83eSAl Viro 				rootmnt = &p->mnt;
32671da177e4SLinus Torvalds 			}
32689559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
32699559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
3270315fc83eSAl Viro 				pwdmnt = &p->mnt;
32711da177e4SLinus Torvalds 			}
32721da177e4SLinus Torvalds 		}
3273909b0a88SAl Viro 		p = next_mnt(p, old);
3274909b0a88SAl Viro 		q = next_mnt(q, new);
32754ce5d2b1SEric W. Biederman 		if (!q)
32764ce5d2b1SEric W. Biederman 			break;
32774ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
32784ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
32791da177e4SLinus Torvalds 	}
3280328e6d90SAl Viro 	namespace_unlock();
32811da177e4SLinus Torvalds 
32821da177e4SLinus Torvalds 	if (rootmnt)
3283f03c6599SAl Viro 		mntput(rootmnt);
32841da177e4SLinus Torvalds 	if (pwdmnt)
3285f03c6599SAl Viro 		mntput(pwdmnt);
32861da177e4SLinus Torvalds 
3287741a2951SJANAK DESAI 	return new_ns;
3288741a2951SJANAK DESAI }
3289741a2951SJANAK DESAI 
329074e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name)
3291cf8d2c11STrond Myklebust {
32921a4eeaf2SAl Viro 	struct mount *mnt = real_mount(m);
3293ea441d11SAl Viro 	struct mnt_namespace *ns;
3294d31da0f0SAl Viro 	struct super_block *s;
3295ea441d11SAl Viro 	struct path path;
3296ea441d11SAl Viro 	int err;
3297ea441d11SAl Viro 
329874e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, true);
329974e83122SAl Viro 	if (IS_ERR(ns)) {
330074e83122SAl Viro 		mntput(m);
3301ea441d11SAl Viro 		return ERR_CAST(ns);
330274e83122SAl Viro 	}
330374e83122SAl Viro 	mnt->mnt_ns = ns;
330474e83122SAl Viro 	ns->root = mnt;
330574e83122SAl Viro 	ns->mounts++;
330674e83122SAl Viro 	list_add(&mnt->mnt_list, &ns->list);
3307ea441d11SAl Viro 
330874e83122SAl Viro 	err = vfs_path_lookup(m->mnt_root, m,
3309ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3310ea441d11SAl Viro 
3311ea441d11SAl Viro 	put_mnt_ns(ns);
3312ea441d11SAl Viro 
3313ea441d11SAl Viro 	if (err)
3314ea441d11SAl Viro 		return ERR_PTR(err);
3315ea441d11SAl Viro 
3316ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
3317d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
3318d31da0f0SAl Viro 	atomic_inc(&s->s_active);
3319ea441d11SAl Viro 	mntput(path.mnt);
3320ea441d11SAl Viro 	/* lock the sucker */
3321d31da0f0SAl Viro 	down_write(&s->s_umount);
3322ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
3323ea441d11SAl Viro 	return path.dentry;
3324ea441d11SAl Viro }
3325ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
3326ea441d11SAl Viro 
3327cccaa5e3SDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3328cccaa5e3SDominik Brodowski 		char __user *, type, unsigned long, flags, void __user *, data)
33291da177e4SLinus Torvalds {
3330eca6f534SVegard Nossum 	int ret;
3331eca6f534SVegard Nossum 	char *kernel_type;
3332eca6f534SVegard Nossum 	char *kernel_dev;
3333b40ef869SAl Viro 	void *options;
33341da177e4SLinus Torvalds 
3335b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
3336b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
3337b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
3338eca6f534SVegard Nossum 		goto out_type;
33391da177e4SLinus Torvalds 
3340b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
3341b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
3342b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
3343eca6f534SVegard Nossum 		goto out_dev;
33441da177e4SLinus Torvalds 
3345b40ef869SAl Viro 	options = copy_mount_options(data);
3346b40ef869SAl Viro 	ret = PTR_ERR(options);
3347b40ef869SAl Viro 	if (IS_ERR(options))
3348eca6f534SVegard Nossum 		goto out_data;
33491da177e4SLinus Torvalds 
3350b40ef869SAl Viro 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
3351eca6f534SVegard Nossum 
3352b40ef869SAl Viro 	kfree(options);
3353eca6f534SVegard Nossum out_data:
3354eca6f534SVegard Nossum 	kfree(kernel_dev);
3355eca6f534SVegard Nossum out_dev:
3356eca6f534SVegard Nossum 	kfree(kernel_type);
3357eca6f534SVegard Nossum out_type:
3358eca6f534SVegard Nossum 	return ret;
33591da177e4SLinus Torvalds }
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds /*
336293766fbdSDavid Howells  * Create a kernel mount representation for a new, prepared superblock
336393766fbdSDavid Howells  * (specified by fs_fd) and attach to an open_tree-like file descriptor.
336493766fbdSDavid Howells  */
336593766fbdSDavid Howells SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
336693766fbdSDavid Howells 		unsigned int, attr_flags)
336793766fbdSDavid Howells {
336893766fbdSDavid Howells 	struct mnt_namespace *ns;
336993766fbdSDavid Howells 	struct fs_context *fc;
337093766fbdSDavid Howells 	struct file *file;
337193766fbdSDavid Howells 	struct path newmount;
337293766fbdSDavid Howells 	struct mount *mnt;
337393766fbdSDavid Howells 	struct fd f;
337493766fbdSDavid Howells 	unsigned int mnt_flags = 0;
337593766fbdSDavid Howells 	long ret;
337693766fbdSDavid Howells 
337793766fbdSDavid Howells 	if (!may_mount())
337893766fbdSDavid Howells 		return -EPERM;
337993766fbdSDavid Howells 
338093766fbdSDavid Howells 	if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
338193766fbdSDavid Howells 		return -EINVAL;
338293766fbdSDavid Howells 
338393766fbdSDavid Howells 	if (attr_flags & ~(MOUNT_ATTR_RDONLY |
338493766fbdSDavid Howells 			   MOUNT_ATTR_NOSUID |
338593766fbdSDavid Howells 			   MOUNT_ATTR_NODEV |
338693766fbdSDavid Howells 			   MOUNT_ATTR_NOEXEC |
338793766fbdSDavid Howells 			   MOUNT_ATTR__ATIME |
338893766fbdSDavid Howells 			   MOUNT_ATTR_NODIRATIME))
338993766fbdSDavid Howells 		return -EINVAL;
339093766fbdSDavid Howells 
339193766fbdSDavid Howells 	if (attr_flags & MOUNT_ATTR_RDONLY)
339293766fbdSDavid Howells 		mnt_flags |= MNT_READONLY;
339393766fbdSDavid Howells 	if (attr_flags & MOUNT_ATTR_NOSUID)
339493766fbdSDavid Howells 		mnt_flags |= MNT_NOSUID;
339593766fbdSDavid Howells 	if (attr_flags & MOUNT_ATTR_NODEV)
339693766fbdSDavid Howells 		mnt_flags |= MNT_NODEV;
339793766fbdSDavid Howells 	if (attr_flags & MOUNT_ATTR_NOEXEC)
339893766fbdSDavid Howells 		mnt_flags |= MNT_NOEXEC;
339993766fbdSDavid Howells 	if (attr_flags & MOUNT_ATTR_NODIRATIME)
340093766fbdSDavid Howells 		mnt_flags |= MNT_NODIRATIME;
340193766fbdSDavid Howells 
340293766fbdSDavid Howells 	switch (attr_flags & MOUNT_ATTR__ATIME) {
340393766fbdSDavid Howells 	case MOUNT_ATTR_STRICTATIME:
340493766fbdSDavid Howells 		break;
340593766fbdSDavid Howells 	case MOUNT_ATTR_NOATIME:
340693766fbdSDavid Howells 		mnt_flags |= MNT_NOATIME;
340793766fbdSDavid Howells 		break;
340893766fbdSDavid Howells 	case MOUNT_ATTR_RELATIME:
340993766fbdSDavid Howells 		mnt_flags |= MNT_RELATIME;
341093766fbdSDavid Howells 		break;
341193766fbdSDavid Howells 	default:
341293766fbdSDavid Howells 		return -EINVAL;
341393766fbdSDavid Howells 	}
341493766fbdSDavid Howells 
341593766fbdSDavid Howells 	f = fdget(fs_fd);
341693766fbdSDavid Howells 	if (!f.file)
341793766fbdSDavid Howells 		return -EBADF;
341893766fbdSDavid Howells 
341993766fbdSDavid Howells 	ret = -EINVAL;
342093766fbdSDavid Howells 	if (f.file->f_op != &fscontext_fops)
342193766fbdSDavid Howells 		goto err_fsfd;
342293766fbdSDavid Howells 
342393766fbdSDavid Howells 	fc = f.file->private_data;
342493766fbdSDavid Howells 
342593766fbdSDavid Howells 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
342693766fbdSDavid Howells 	if (ret < 0)
342793766fbdSDavid Howells 		goto err_fsfd;
342893766fbdSDavid Howells 
342993766fbdSDavid Howells 	/* There must be a valid superblock or we can't mount it */
343093766fbdSDavid Howells 	ret = -EINVAL;
343193766fbdSDavid Howells 	if (!fc->root)
343293766fbdSDavid Howells 		goto err_unlock;
343393766fbdSDavid Howells 
343493766fbdSDavid Howells 	ret = -EPERM;
343593766fbdSDavid Howells 	if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
343693766fbdSDavid Howells 		pr_warn("VFS: Mount too revealing\n");
343793766fbdSDavid Howells 		goto err_unlock;
343893766fbdSDavid Howells 	}
343993766fbdSDavid Howells 
344093766fbdSDavid Howells 	ret = -EBUSY;
344193766fbdSDavid Howells 	if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
344293766fbdSDavid Howells 		goto err_unlock;
344393766fbdSDavid Howells 
344493766fbdSDavid Howells 	ret = -EPERM;
344593766fbdSDavid Howells 	if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock())
344693766fbdSDavid Howells 		goto err_unlock;
344793766fbdSDavid Howells 
344893766fbdSDavid Howells 	newmount.mnt = vfs_create_mount(fc);
344993766fbdSDavid Howells 	if (IS_ERR(newmount.mnt)) {
345093766fbdSDavid Howells 		ret = PTR_ERR(newmount.mnt);
345193766fbdSDavid Howells 		goto err_unlock;
345293766fbdSDavid Howells 	}
345393766fbdSDavid Howells 	newmount.dentry = dget(fc->root);
345493766fbdSDavid Howells 	newmount.mnt->mnt_flags = mnt_flags;
345593766fbdSDavid Howells 
345693766fbdSDavid Howells 	/* We've done the mount bit - now move the file context into more or
345793766fbdSDavid Howells 	 * less the same state as if we'd done an fspick().  We don't want to
345893766fbdSDavid Howells 	 * do any memory allocation or anything like that at this point as we
345993766fbdSDavid Howells 	 * don't want to have to handle any errors incurred.
346093766fbdSDavid Howells 	 */
346193766fbdSDavid Howells 	vfs_clean_context(fc);
346293766fbdSDavid Howells 
346393766fbdSDavid Howells 	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
346493766fbdSDavid Howells 	if (IS_ERR(ns)) {
346593766fbdSDavid Howells 		ret = PTR_ERR(ns);
346693766fbdSDavid Howells 		goto err_path;
346793766fbdSDavid Howells 	}
346893766fbdSDavid Howells 	mnt = real_mount(newmount.mnt);
346993766fbdSDavid Howells 	mnt->mnt_ns = ns;
347093766fbdSDavid Howells 	ns->root = mnt;
347193766fbdSDavid Howells 	ns->mounts = 1;
347293766fbdSDavid Howells 	list_add(&mnt->mnt_list, &ns->list);
34731b0b9cc8SEric Biggers 	mntget(newmount.mnt);
347493766fbdSDavid Howells 
347593766fbdSDavid Howells 	/* Attach to an apparent O_PATH fd with a note that we need to unmount
347693766fbdSDavid Howells 	 * it, not just simply put it.
347793766fbdSDavid Howells 	 */
347893766fbdSDavid Howells 	file = dentry_open(&newmount, O_PATH, fc->cred);
347993766fbdSDavid Howells 	if (IS_ERR(file)) {
348093766fbdSDavid Howells 		dissolve_on_fput(newmount.mnt);
348193766fbdSDavid Howells 		ret = PTR_ERR(file);
348293766fbdSDavid Howells 		goto err_path;
348393766fbdSDavid Howells 	}
348493766fbdSDavid Howells 	file->f_mode |= FMODE_NEED_UNMOUNT;
348593766fbdSDavid Howells 
348693766fbdSDavid Howells 	ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
348793766fbdSDavid Howells 	if (ret >= 0)
348893766fbdSDavid Howells 		fd_install(ret, file);
348993766fbdSDavid Howells 	else
349093766fbdSDavid Howells 		fput(file);
349193766fbdSDavid Howells 
349293766fbdSDavid Howells err_path:
349393766fbdSDavid Howells 	path_put(&newmount);
349493766fbdSDavid Howells err_unlock:
349593766fbdSDavid Howells 	mutex_unlock(&fc->uapi_mutex);
349693766fbdSDavid Howells err_fsfd:
349793766fbdSDavid Howells 	fdput(f);
349893766fbdSDavid Howells 	return ret;
349993766fbdSDavid Howells }
350093766fbdSDavid Howells 
350193766fbdSDavid Howells /*
350293766fbdSDavid Howells  * Move a mount from one place to another.  In combination with
350393766fbdSDavid Howells  * fsopen()/fsmount() this is used to install a new mount and in combination
350493766fbdSDavid Howells  * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
350593766fbdSDavid Howells  * a mount subtree.
35062db154b3SDavid Howells  *
35072db154b3SDavid Howells  * Note the flags value is a combination of MOVE_MOUNT_* flags.
35082db154b3SDavid Howells  */
35092db154b3SDavid Howells SYSCALL_DEFINE5(move_mount,
35102658ce09SBen Dooks 		int, from_dfd, const char __user *, from_pathname,
35112658ce09SBen Dooks 		int, to_dfd, const char __user *, to_pathname,
35122db154b3SDavid Howells 		unsigned int, flags)
35132db154b3SDavid Howells {
35142db154b3SDavid Howells 	struct path from_path, to_path;
35152db154b3SDavid Howells 	unsigned int lflags;
35162db154b3SDavid Howells 	int ret = 0;
35172db154b3SDavid Howells 
35182db154b3SDavid Howells 	if (!may_mount())
35192db154b3SDavid Howells 		return -EPERM;
35202db154b3SDavid Howells 
35212db154b3SDavid Howells 	if (flags & ~MOVE_MOUNT__MASK)
35222db154b3SDavid Howells 		return -EINVAL;
35232db154b3SDavid Howells 
35242db154b3SDavid Howells 	/* If someone gives a pathname, they aren't permitted to move
35252db154b3SDavid Howells 	 * from an fd that requires unmount as we can't get at the flag
35262db154b3SDavid Howells 	 * to clear it afterwards.
35272db154b3SDavid Howells 	 */
35282db154b3SDavid Howells 	lflags = 0;
35292db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
35302db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
35312db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
35322db154b3SDavid Howells 
35332db154b3SDavid Howells 	ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
35342db154b3SDavid Howells 	if (ret < 0)
35352db154b3SDavid Howells 		return ret;
35362db154b3SDavid Howells 
35372db154b3SDavid Howells 	lflags = 0;
35382db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
35392db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
35402db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
35412db154b3SDavid Howells 
35422db154b3SDavid Howells 	ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
35432db154b3SDavid Howells 	if (ret < 0)
35442db154b3SDavid Howells 		goto out_from;
35452db154b3SDavid Howells 
35462db154b3SDavid Howells 	ret = security_move_mount(&from_path, &to_path);
35472db154b3SDavid Howells 	if (ret < 0)
35482db154b3SDavid Howells 		goto out_to;
35492db154b3SDavid Howells 
35502db154b3SDavid Howells 	ret = do_move_mount(&from_path, &to_path);
35512db154b3SDavid Howells 
35522db154b3SDavid Howells out_to:
35532db154b3SDavid Howells 	path_put(&to_path);
35542db154b3SDavid Howells out_from:
35552db154b3SDavid Howells 	path_put(&from_path);
35562db154b3SDavid Howells 	return ret;
35572db154b3SDavid Howells }
35582db154b3SDavid Howells 
35592db154b3SDavid Howells /*
3560afac7cbaSAl Viro  * Return true if path is reachable from root
3561afac7cbaSAl Viro  *
356248a066e7SAl Viro  * namespace_sem or mount_lock is held
3563afac7cbaSAl Viro  */
3564643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
3565afac7cbaSAl Viro 			 const struct path *root)
3566afac7cbaSAl Viro {
3567643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
3568a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
35690714a533SAl Viro 		mnt = mnt->mnt_parent;
3570afac7cbaSAl Viro 	}
3571643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
3572afac7cbaSAl Viro }
3573afac7cbaSAl Viro 
3574640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2)
3575afac7cbaSAl Viro {
357625ab4c9bSYaowei Bai 	bool res;
357748a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
3578643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
357948a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
3580afac7cbaSAl Viro 	return res;
3581afac7cbaSAl Viro }
3582afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
3583afac7cbaSAl Viro 
3584afac7cbaSAl Viro /*
35851da177e4SLinus Torvalds  * pivot_root Semantics:
35861da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
35871da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
35881da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
35891da177e4SLinus Torvalds  *
35901da177e4SLinus Torvalds  * Restrictions:
35911da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
35921da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
35931da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
35941da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
35951da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
35961da177e4SLinus Torvalds  *
35974a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
35984a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
35994a0d11faSNeil Brown  * in this situation.
36004a0d11faSNeil Brown  *
36011da177e4SLinus Torvalds  * Notes:
36021da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
36031da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
36041da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
36051da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
36061da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
36071da177e4SLinus Torvalds  *    first.
36081da177e4SLinus Torvalds  */
36093480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
36103480b257SHeiko Carstens 		const char __user *, put_old)
36111da177e4SLinus Torvalds {
36122763d119SAl Viro 	struct path new, old, root;
36132763d119SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
361484d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
36151da177e4SLinus Torvalds 	int error;
36161da177e4SLinus Torvalds 
36179b40bc90SAl Viro 	if (!may_mount())
36181da177e4SLinus Torvalds 		return -EPERM;
36191da177e4SLinus Torvalds 
3620ce6595a2SAl Viro 	error = user_path_at(AT_FDCWD, new_root,
3621ce6595a2SAl Viro 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
36221da177e4SLinus Torvalds 	if (error)
36231da177e4SLinus Torvalds 		goto out0;
36241da177e4SLinus Torvalds 
3625ce6595a2SAl Viro 	error = user_path_at(AT_FDCWD, put_old,
3626ce6595a2SAl Viro 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
36271da177e4SLinus Torvalds 	if (error)
36281da177e4SLinus Torvalds 		goto out1;
36291da177e4SLinus Torvalds 
36302d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
3631b12cea91SAl Viro 	if (error)
3632b12cea91SAl Viro 		goto out2;
36331da177e4SLinus Torvalds 
3634f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
363584d17192SAl Viro 	old_mp = lock_mount(&old);
363684d17192SAl Viro 	error = PTR_ERR(old_mp);
363784d17192SAl Viro 	if (IS_ERR(old_mp))
3638b12cea91SAl Viro 		goto out3;
3639b12cea91SAl Viro 
36401da177e4SLinus Torvalds 	error = -EINVAL;
3641419148daSAl Viro 	new_mnt = real_mount(new.mnt);
3642419148daSAl Viro 	root_mnt = real_mount(root.mnt);
364384d17192SAl Viro 	old_mnt = real_mount(old.mnt);
36442763d119SAl Viro 	ex_parent = new_mnt->mnt_parent;
36452763d119SAl Viro 	root_parent = root_mnt->mnt_parent;
364684d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
36472763d119SAl Viro 		IS_MNT_SHARED(ex_parent) ||
36482763d119SAl Viro 		IS_MNT_SHARED(root_parent))
3649b12cea91SAl Viro 		goto out4;
3650143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
3651b12cea91SAl Viro 		goto out4;
36525ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
36535ff9d8a6SEric W. Biederman 		goto out4;
36541da177e4SLinus Torvalds 	error = -ENOENT;
3655f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
3656b12cea91SAl Viro 		goto out4;
36571da177e4SLinus Torvalds 	error = -EBUSY;
365884d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
3659b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
36601da177e4SLinus Torvalds 	error = -EINVAL;
36618c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
3662b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3663676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
3664b12cea91SAl Viro 		goto out4; /* not attached */
36652d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
3666b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3667676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
3668b12cea91SAl Viro 		goto out4; /* not attached */
36694ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
367084d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
3671b12cea91SAl Viro 		goto out4;
36720d082601SEric W. Biederman 	/* make certain new is below the root */
36730d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
36740d082601SEric W. Biederman 		goto out4;
3675719ea2fbSAl Viro 	lock_mount_hash();
36762763d119SAl Viro 	umount_mnt(new_mnt);
36772763d119SAl Viro 	root_mp = unhash_mnt(root_mnt);  /* we'll need its mountpoint */
36785ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
36795ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
36805ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
36815ff9d8a6SEric W. Biederman 	}
36824ac91378SJan Blunck 	/* mount old root on put_old */
368384d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
36844ac91378SJan Blunck 	/* mount new_root on / */
36852763d119SAl Viro 	attach_mnt(new_mnt, root_parent, root_mp);
36862763d119SAl Viro 	mnt_add_count(root_parent, -1);
36876b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
36884fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
36894fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
36903895dbf8SEric W. Biederman 	put_mountpoint(root_mp);
3691719ea2fbSAl Viro 	unlock_mount_hash();
36922d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
36931da177e4SLinus Torvalds 	error = 0;
3694b12cea91SAl Viro out4:
369584d17192SAl Viro 	unlock_mount(old_mp);
36962763d119SAl Viro 	if (!error)
36972763d119SAl Viro 		mntput_no_expire(ex_parent);
3698b12cea91SAl Viro out3:
36998c3ee42eSAl Viro 	path_put(&root);
3700b12cea91SAl Viro out2:
37012d8f3038SAl Viro 	path_put(&old);
37021da177e4SLinus Torvalds out1:
37032d8f3038SAl Viro 	path_put(&new);
37041da177e4SLinus Torvalds out0:
37051da177e4SLinus Torvalds 	return error;
37061da177e4SLinus Torvalds }
37071da177e4SLinus Torvalds 
37081da177e4SLinus Torvalds static void __init init_mount_tree(void)
37091da177e4SLinus Torvalds {
37101da177e4SLinus Torvalds 	struct vfsmount *mnt;
371174e83122SAl Viro 	struct mount *m;
37126b3286edSKirill Korotaev 	struct mnt_namespace *ns;
3713ac748a09SJan Blunck 	struct path root;
37141da177e4SLinus Torvalds 
3715fd3e007fSAl Viro 	mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
37161da177e4SLinus Torvalds 	if (IS_ERR(mnt))
37171da177e4SLinus Torvalds 		panic("Can't create rootfs");
3718b3e19d92SNick Piggin 
371974e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, false);
37203b22edc5STrond Myklebust 	if (IS_ERR(ns))
37211da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
372274e83122SAl Viro 	m = real_mount(mnt);
372374e83122SAl Viro 	m->mnt_ns = ns;
372474e83122SAl Viro 	ns->root = m;
372574e83122SAl Viro 	ns->mounts = 1;
372674e83122SAl Viro 	list_add(&m->mnt_list, &ns->list);
37276b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
37286b3286edSKirill Korotaev 	get_mnt_ns(ns);
37291da177e4SLinus Torvalds 
3730be08d6d2SAl Viro 	root.mnt = mnt;
3731be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
3732da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
3733ac748a09SJan Blunck 
3734ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
3735ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
37361da177e4SLinus Torvalds }
37371da177e4SLinus Torvalds 
373874bf17cfSDenis Cheng void __init mnt_init(void)
37391da177e4SLinus Torvalds {
374015a67dd8SRandy Dunlap 	int err;
37411da177e4SLinus Torvalds 
37427d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
374320c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
37441da177e4SLinus Torvalds 
37450818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
374638129a13SAl Viro 				sizeof(struct hlist_head),
37470818bf27SAl Viro 				mhash_entries, 19,
37483d375d78SPavel Tatashin 				HASH_ZERO,
37490818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
37500818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
37510818bf27SAl Viro 				sizeof(struct hlist_head),
37520818bf27SAl Viro 				mphash_entries, 19,
37533d375d78SPavel Tatashin 				HASH_ZERO,
37540818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
37551da177e4SLinus Torvalds 
375684d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
37571da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
37581da177e4SLinus Torvalds 
37594b93dc9bSTejun Heo 	kernfs_init();
37604b93dc9bSTejun Heo 
376115a67dd8SRandy Dunlap 	err = sysfs_init();
376215a67dd8SRandy Dunlap 	if (err)
376315a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
37648e24eea7SHarvey Harrison 			__func__, err);
376500d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
376600d26666SGreg Kroah-Hartman 	if (!fs_kobj)
37678e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
3768037f11b4SAl Viro 	shmem_init();
37691da177e4SLinus Torvalds 	init_rootfs();
37701da177e4SLinus Torvalds 	init_mount_tree();
37711da177e4SLinus Torvalds }
37721da177e4SLinus Torvalds 
3773616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
37741da177e4SLinus Torvalds {
3775d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
3776616511d0STrond Myklebust 		return;
37777b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
3778771b1371SEric W. Biederman 	free_mnt_ns(ns);
37791da177e4SLinus Torvalds }
37809d412a43SAl Viro 
3781d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type)
37829d412a43SAl Viro {
3783423e0ab0STim Chen 	struct vfsmount *mnt;
3784d911b458SDavid Howells 	mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
3785423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
3786423e0ab0STim Chen 		/*
3787423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
3788423e0ab0STim Chen 		 * we unmount before file sys is unregistered
3789423e0ab0STim Chen 		*/
3790f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
3791423e0ab0STim Chen 	}
3792423e0ab0STim Chen 	return mnt;
37939d412a43SAl Viro }
3794d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount);
3795423e0ab0STim Chen 
3796423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
3797423e0ab0STim Chen {
3798423e0ab0STim Chen 	/* release long term mount so mount point can be released */
3799423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
3800f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
380148a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
3802423e0ab0STim Chen 		mntput(mnt);
3803423e0ab0STim Chen 	}
3804423e0ab0STim Chen }
3805423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
380602125a82SAl Viro 
380702125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
380802125a82SAl Viro {
3809143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
381002125a82SAl Viro }
38118823c079SEric W. Biederman 
38123151527eSEric W. Biederman bool current_chrooted(void)
38133151527eSEric W. Biederman {
38143151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
38153151527eSEric W. Biederman 	struct path ns_root;
38163151527eSEric W. Biederman 	struct path fs_root;
38173151527eSEric W. Biederman 	bool chrooted;
38183151527eSEric W. Biederman 
38193151527eSEric W. Biederman 	/* Find the namespace root */
38203151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
38213151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
38223151527eSEric W. Biederman 	path_get(&ns_root);
38233151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
38243151527eSEric W. Biederman 		;
38253151527eSEric W. Biederman 
38263151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
38273151527eSEric W. Biederman 
38283151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
38293151527eSEric W. Biederman 
38303151527eSEric W. Biederman 	path_put(&fs_root);
38313151527eSEric W. Biederman 	path_put(&ns_root);
38323151527eSEric W. Biederman 
38333151527eSEric W. Biederman 	return chrooted;
38343151527eSEric W. Biederman }
38353151527eSEric W. Biederman 
3836132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns,
3837132e4608SDavid Howells 				const struct super_block *sb,
38388654df4eSEric W. Biederman 				int *new_mnt_flags)
383987a8ebd6SEric W. Biederman {
38408c6cf9ccSEric W. Biederman 	int new_flags = *new_mnt_flags;
384187a8ebd6SEric W. Biederman 	struct mount *mnt;
3842e51db735SEric W. Biederman 	bool visible = false;
384387a8ebd6SEric W. Biederman 
384444bb4385SAl Viro 	down_read(&namespace_sem);
384587a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
3846e51db735SEric W. Biederman 		struct mount *child;
384777b1a97dSEric W. Biederman 		int mnt_flags;
384877b1a97dSEric W. Biederman 
3849132e4608SDavid Howells 		if (mnt->mnt.mnt_sb->s_type != sb->s_type)
3850e51db735SEric W. Biederman 			continue;
3851e51db735SEric W. Biederman 
38527e96c1b0SEric W. Biederman 		/* This mount is not fully visible if it's root directory
38537e96c1b0SEric W. Biederman 		 * is not the root directory of the filesystem.
38547e96c1b0SEric W. Biederman 		 */
38557e96c1b0SEric W. Biederman 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
38567e96c1b0SEric W. Biederman 			continue;
38577e96c1b0SEric W. Biederman 
3858a1935c17SEric W. Biederman 		/* A local view of the mount flags */
385977b1a97dSEric W. Biederman 		mnt_flags = mnt->mnt.mnt_flags;
386077b1a97dSEric W. Biederman 
3861695e9df0SEric W. Biederman 		/* Don't miss readonly hidden in the superblock flags */
3862bc98a42cSDavid Howells 		if (sb_rdonly(mnt->mnt.mnt_sb))
3863695e9df0SEric W. Biederman 			mnt_flags |= MNT_LOCK_READONLY;
3864695e9df0SEric W. Biederman 
38658c6cf9ccSEric W. Biederman 		/* Verify the mount flags are equal to or more permissive
38668c6cf9ccSEric W. Biederman 		 * than the proposed new mount.
38678c6cf9ccSEric W. Biederman 		 */
386877b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_READONLY) &&
38698c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_READONLY))
38708c6cf9ccSEric W. Biederman 			continue;
387177b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_ATIME) &&
387277b1a97dSEric W. Biederman 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
38738c6cf9ccSEric W. Biederman 			continue;
38748c6cf9ccSEric W. Biederman 
3875ceeb0e5dSEric W. Biederman 		/* This mount is not fully visible if there are any
3876ceeb0e5dSEric W. Biederman 		 * locked child mounts that cover anything except for
3877ceeb0e5dSEric W. Biederman 		 * empty directories.
3878e51db735SEric W. Biederman 		 */
3879e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3880e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
3881ceeb0e5dSEric W. Biederman 			/* Only worry about locked mounts */
3882d71ed6c9SEric W. Biederman 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
3883ceeb0e5dSEric W. Biederman 				continue;
38847236c85eSEric W. Biederman 			/* Is the directory permanetly empty? */
38857236c85eSEric W. Biederman 			if (!is_empty_dir_inode(inode))
3886e51db735SEric W. Biederman 				goto next;
388787a8ebd6SEric W. Biederman 		}
38888c6cf9ccSEric W. Biederman 		/* Preserve the locked attributes */
388977b1a97dSEric W. Biederman 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
38908c6cf9ccSEric W. Biederman 					       MNT_LOCK_ATIME);
3891e51db735SEric W. Biederman 		visible = true;
3892e51db735SEric W. Biederman 		goto found;
3893e51db735SEric W. Biederman 	next:	;
389487a8ebd6SEric W. Biederman 	}
3895e51db735SEric W. Biederman found:
389644bb4385SAl Viro 	up_read(&namespace_sem);
3897e51db735SEric W. Biederman 	return visible;
389887a8ebd6SEric W. Biederman }
389987a8ebd6SEric W. Biederman 
3900132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
39018654df4eSEric W. Biederman {
3902a1935c17SEric W. Biederman 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
39038654df4eSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
39048654df4eSEric W. Biederman 	unsigned long s_iflags;
39058654df4eSEric W. Biederman 
39068654df4eSEric W. Biederman 	if (ns->user_ns == &init_user_ns)
39078654df4eSEric W. Biederman 		return false;
39088654df4eSEric W. Biederman 
39098654df4eSEric W. Biederman 	/* Can this filesystem be too revealing? */
3910132e4608SDavid Howells 	s_iflags = sb->s_iflags;
39118654df4eSEric W. Biederman 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
39128654df4eSEric W. Biederman 		return false;
39138654df4eSEric W. Biederman 
3914a1935c17SEric W. Biederman 	if ((s_iflags & required_iflags) != required_iflags) {
3915a1935c17SEric W. Biederman 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3916a1935c17SEric W. Biederman 			  required_iflags);
3917a1935c17SEric W. Biederman 		return true;
3918a1935c17SEric W. Biederman 	}
3919a1935c17SEric W. Biederman 
3920132e4608SDavid Howells 	return !mnt_already_visible(ns, sb, new_mnt_flags);
39218654df4eSEric W. Biederman }
39228654df4eSEric W. Biederman 
3923380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt)
3924380cf5baSAndy Lutomirski {
3925380cf5baSAndy Lutomirski 	/*
3926380cf5baSAndy Lutomirski 	 * Foreign mounts (accessed via fchdir or through /proc
3927380cf5baSAndy Lutomirski 	 * symlinks) are always treated as if they are nosuid.  This
3928380cf5baSAndy Lutomirski 	 * prevents namespaces from trusting potentially unsafe
3929380cf5baSAndy Lutomirski 	 * suid/sgid bits, file caps, or security labels that originate
3930380cf5baSAndy Lutomirski 	 * in other namespaces.
3931380cf5baSAndy Lutomirski 	 */
3932380cf5baSAndy Lutomirski 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3933380cf5baSAndy Lutomirski 	       current_in_userns(mnt->mnt_sb->s_user_ns);
3934380cf5baSAndy Lutomirski }
3935380cf5baSAndy Lutomirski 
393664964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
39378823c079SEric W. Biederman {
393858be2825SAl Viro 	struct ns_common *ns = NULL;
39398823c079SEric W. Biederman 	struct nsproxy *nsproxy;
39408823c079SEric W. Biederman 
3941728dba3aSEric W. Biederman 	task_lock(task);
3942728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
39438823c079SEric W. Biederman 	if (nsproxy) {
394458be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
394558be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
39468823c079SEric W. Biederman 	}
3947728dba3aSEric W. Biederman 	task_unlock(task);
39488823c079SEric W. Biederman 
39498823c079SEric W. Biederman 	return ns;
39508823c079SEric W. Biederman }
39518823c079SEric W. Biederman 
395264964528SAl Viro static void mntns_put(struct ns_common *ns)
39538823c079SEric W. Biederman {
395458be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
39558823c079SEric W. Biederman }
39568823c079SEric W. Biederman 
3957f2a8d52eSChristian Brauner static int mntns_install(struct nsset *nsset, struct ns_common *ns)
39588823c079SEric W. Biederman {
3959f2a8d52eSChristian Brauner 	struct nsproxy *nsproxy = nsset->nsproxy;
3960f2a8d52eSChristian Brauner 	struct fs_struct *fs = nsset->fs;
39614f757f3cSAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
3962f2a8d52eSChristian Brauner 	struct user_namespace *user_ns = nsset->cred->user_ns;
39638823c079SEric W. Biederman 	struct path root;
39644f757f3cSAl Viro 	int err;
39658823c079SEric W. Biederman 
39660c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3967f2a8d52eSChristian Brauner 	    !ns_capable(user_ns, CAP_SYS_CHROOT) ||
3968f2a8d52eSChristian Brauner 	    !ns_capable(user_ns, CAP_SYS_ADMIN))
3969ae11e0f1SZhao Hongjiang 		return -EPERM;
39708823c079SEric W. Biederman 
397174e83122SAl Viro 	if (is_anon_ns(mnt_ns))
397274e83122SAl Viro 		return -EINVAL;
397374e83122SAl Viro 
39748823c079SEric W. Biederman 	if (fs->users != 1)
39758823c079SEric W. Biederman 		return -EINVAL;
39768823c079SEric W. Biederman 
39778823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
39784f757f3cSAl Viro 	old_mnt_ns = nsproxy->mnt_ns;
39798823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
39808823c079SEric W. Biederman 
39818823c079SEric W. Biederman 	/* Find the root */
39824f757f3cSAl Viro 	err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
39834f757f3cSAl Viro 				"/", LOOKUP_DOWN, &root);
39844f757f3cSAl Viro 	if (err) {
39854f757f3cSAl Viro 		/* revert to old namespace */
39864f757f3cSAl Viro 		nsproxy->mnt_ns = old_mnt_ns;
39874f757f3cSAl Viro 		put_mnt_ns(mnt_ns);
39884f757f3cSAl Viro 		return err;
39894f757f3cSAl Viro 	}
39908823c079SEric W. Biederman 
39914068367cSAndrei Vagin 	put_mnt_ns(old_mnt_ns);
39924068367cSAndrei Vagin 
39938823c079SEric W. Biederman 	/* Update the pwd and root */
39948823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
39958823c079SEric W. Biederman 	set_fs_root(fs, &root);
39968823c079SEric W. Biederman 
39978823c079SEric W. Biederman 	path_put(&root);
39988823c079SEric W. Biederman 	return 0;
39998823c079SEric W. Biederman }
40008823c079SEric W. Biederman 
4001bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns)
4002bcac25a5SAndrey Vagin {
4003bcac25a5SAndrey Vagin 	return to_mnt_ns(ns)->user_ns;
4004bcac25a5SAndrey Vagin }
4005bcac25a5SAndrey Vagin 
40068823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
40078823c079SEric W. Biederman 	.name		= "mnt",
40088823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
40098823c079SEric W. Biederman 	.get		= mntns_get,
40108823c079SEric W. Biederman 	.put		= mntns_put,
40118823c079SEric W. Biederman 	.install	= mntns_install,
4012bcac25a5SAndrey Vagin 	.owner		= mntns_owner,
40138823c079SEric W. Biederman };
4014