xref: /openbmc/linux/fs/namespace.c (revision bf1ac16e)
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>
289caccd41SChristian Brauner #include <linux/proc_fs.h>
299ea459e1SAl Viro #include <linux/task_work.h>
309164bb4aSIngo Molnar #include <linux/sched/task.h>
31e262e32dSDavid Howells #include <uapi/linux/mount.h>
329bc61ab1SDavid Howells #include <linux/fs_context.h>
33037f11b4SAl Viro #include <linux/shmem_fs.h>
34bd303368SChristian Brauner #include <linux/mnt_idmapping.h>
359164bb4aSIngo Molnar 
3607b20889SRam Pai #include "pnode.h"
37948730b0SAdrian Bunk #include "internal.h"
381da177e4SLinus Torvalds 
39d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */
40ab171b95SLuis Chamberlain static unsigned int sysctl_mount_max __read_mostly = 100000;
41d2921684SEric W. Biederman 
420818bf27SAl Viro static unsigned int m_hash_mask __read_mostly;
430818bf27SAl Viro static unsigned int m_hash_shift __read_mostly;
440818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly;
450818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly;
460818bf27SAl Viro 
470818bf27SAl Viro static __initdata unsigned long mhash_entries;
480818bf27SAl Viro static int __init set_mhash_entries(char *str)
490818bf27SAl Viro {
500818bf27SAl Viro 	if (!str)
510818bf27SAl Viro 		return 0;
520818bf27SAl Viro 	mhash_entries = simple_strtoul(str, &str, 0);
530818bf27SAl Viro 	return 1;
540818bf27SAl Viro }
550818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries);
560818bf27SAl Viro 
570818bf27SAl Viro static __initdata unsigned long mphash_entries;
580818bf27SAl Viro static int __init set_mphash_entries(char *str)
590818bf27SAl Viro {
600818bf27SAl Viro 	if (!str)
610818bf27SAl Viro 		return 0;
620818bf27SAl Viro 	mphash_entries = simple_strtoul(str, &str, 0);
630818bf27SAl Viro 	return 1;
640818bf27SAl Viro }
650818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries);
6613f14b4dSEric Dumazet 
67c7999c36SAl Viro static u64 event;
6873cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
69719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
705addc5ddSAl Viro 
7138129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly;
720818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly;
73e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
7459aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem);
754edbe133SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
764edbe133SAl Viro static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
771da177e4SLinus Torvalds 
782a186721SChristian Brauner struct mount_kattr {
792a186721SChristian Brauner 	unsigned int attr_set;
802a186721SChristian Brauner 	unsigned int attr_clr;
812a186721SChristian Brauner 	unsigned int propagation;
822a186721SChristian Brauner 	unsigned int lookup_flags;
832a186721SChristian Brauner 	bool recurse;
849caccd41SChristian Brauner 	struct user_namespace *mnt_userns;
852a186721SChristian Brauner };
862a186721SChristian Brauner 
87f87fd4c2SMiklos Szeredi /* /sys/fs */
8800d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
8900d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
90f87fd4c2SMiklos Szeredi 
9199b7db7bSNick Piggin /*
9299b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
9399b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
9499b7db7bSNick Piggin  * up the tree.
9599b7db7bSNick Piggin  *
9699b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
9799b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
9899b7db7bSNick Piggin  */
9948a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
10099b7db7bSNick Piggin 
101d033cb67SChristian Brauner static inline void lock_mount_hash(void)
102d033cb67SChristian Brauner {
103d033cb67SChristian Brauner 	write_seqlock(&mount_lock);
104d033cb67SChristian Brauner }
105d033cb67SChristian Brauner 
106d033cb67SChristian Brauner static inline void unlock_mount_hash(void)
107d033cb67SChristian Brauner {
108d033cb67SChristian Brauner 	write_sequnlock(&mount_lock);
109d033cb67SChristian Brauner }
110d033cb67SChristian Brauner 
11138129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
1141da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
1150818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
1160818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
1170818bf27SAl Viro }
1180818bf27SAl Viro 
1190818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
1200818bf27SAl Viro {
1210818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
1220818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
1230818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
126b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
12773cd49ecSMiklos Szeredi {
128169b480eSMatthew Wilcox 	int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
12973cd49ecSMiklos Szeredi 
130169b480eSMatthew Wilcox 	if (res < 0)
13173cd49ecSMiklos Szeredi 		return res;
132169b480eSMatthew Wilcox 	mnt->mnt_id = res;
133169b480eSMatthew Wilcox 	return 0;
13473cd49ecSMiklos Szeredi }
13573cd49ecSMiklos Szeredi 
136b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
13773cd49ecSMiklos Szeredi {
138169b480eSMatthew Wilcox 	ida_free(&mnt_id_ida, mnt->mnt_id);
13973cd49ecSMiklos Szeredi }
14073cd49ecSMiklos Szeredi 
141719f5d7fSMiklos Szeredi /*
142719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
143719f5d7fSMiklos Szeredi  */
1444b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
145719f5d7fSMiklos Szeredi {
146169b480eSMatthew Wilcox 	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
147f21f6220SAl Viro 
148169b480eSMatthew Wilcox 	if (res < 0)
149f21f6220SAl Viro 		return res;
150169b480eSMatthew Wilcox 	mnt->mnt_group_id = res;
151169b480eSMatthew Wilcox 	return 0;
152719f5d7fSMiklos Szeredi }
153719f5d7fSMiklos Szeredi 
154719f5d7fSMiklos Szeredi /*
155719f5d7fSMiklos Szeredi  * Release a peer group ID
156719f5d7fSMiklos Szeredi  */
1574b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
158719f5d7fSMiklos Szeredi {
159169b480eSMatthew Wilcox 	ida_free(&mnt_group_ida, mnt->mnt_group_id);
16015169fe7SAl Viro 	mnt->mnt_group_id = 0;
161719f5d7fSMiklos Szeredi }
162719f5d7fSMiklos Szeredi 
163b3e19d92SNick Piggin /*
164b3e19d92SNick Piggin  * vfsmount lock must be held for read
165b3e19d92SNick Piggin  */
16683adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
167b3e19d92SNick Piggin {
168b3e19d92SNick Piggin #ifdef CONFIG_SMP
16968e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
170b3e19d92SNick Piggin #else
171b3e19d92SNick Piggin 	preempt_disable();
17268e8a9feSAl Viro 	mnt->mnt_count += n;
173b3e19d92SNick Piggin 	preempt_enable();
174b3e19d92SNick Piggin #endif
175b3e19d92SNick Piggin }
176b3e19d92SNick Piggin 
177b3e19d92SNick Piggin /*
178b3e19d92SNick Piggin  * vfsmount lock must be held for write
179b3e19d92SNick Piggin  */
180edf7ddbfSEric Biggers int mnt_get_count(struct mount *mnt)
181b3e19d92SNick Piggin {
182b3e19d92SNick Piggin #ifdef CONFIG_SMP
183edf7ddbfSEric Biggers 	int count = 0;
184b3e19d92SNick Piggin 	int cpu;
185b3e19d92SNick Piggin 
186b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
18768e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
188b3e19d92SNick Piggin 	}
189b3e19d92SNick Piggin 
190b3e19d92SNick Piggin 	return count;
191b3e19d92SNick Piggin #else
19268e8a9feSAl Viro 	return mnt->mnt_count;
193b3e19d92SNick Piggin #endif
194b3e19d92SNick Piggin }
195b3e19d92SNick Piggin 
196b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
1971da177e4SLinus Torvalds {
198c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
199c63181e6SAl Viro 	if (mnt) {
20073cd49ecSMiklos Szeredi 		int err;
20173cd49ecSMiklos Szeredi 
202c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
20388b38782SLi Zefan 		if (err)
20488b38782SLi Zefan 			goto out_free_cache;
20588b38782SLi Zefan 
20688b38782SLi Zefan 		if (name) {
20779f6540bSVasily Averin 			mnt->mnt_devname = kstrdup_const(name,
20879f6540bSVasily Averin 							 GFP_KERNEL_ACCOUNT);
209c63181e6SAl Viro 			if (!mnt->mnt_devname)
21088b38782SLi Zefan 				goto out_free_id;
21173cd49ecSMiklos Szeredi 		}
21273cd49ecSMiklos Szeredi 
213b3e19d92SNick Piggin #ifdef CONFIG_SMP
214c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
215c63181e6SAl Viro 		if (!mnt->mnt_pcp)
216b3e19d92SNick Piggin 			goto out_free_devname;
217b3e19d92SNick Piggin 
218c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
219b3e19d92SNick Piggin #else
220c63181e6SAl Viro 		mnt->mnt_count = 1;
221c63181e6SAl Viro 		mnt->mnt_writers = 0;
222b3e19d92SNick Piggin #endif
223b3e19d92SNick Piggin 
22438129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
225c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
226c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
227c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
228c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
229c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
230c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
231c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2320a5eb7c8SEric W. Biederman 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
23399b19d16SEric W. Biederman 		INIT_LIST_HEAD(&mnt->mnt_umounting);
23456cbb429SAl Viro 		INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
235a6435940SChristian Brauner 		mnt->mnt.mnt_userns = &init_user_ns;
2361da177e4SLinus Torvalds 	}
237c63181e6SAl Viro 	return mnt;
23888b38782SLi Zefan 
239d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
240d3ef3d73Snpiggin@suse.de out_free_devname:
241fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
242d3ef3d73Snpiggin@suse.de #endif
24388b38782SLi Zefan out_free_id:
244c63181e6SAl Viro 	mnt_free_id(mnt);
24588b38782SLi Zefan out_free_cache:
246c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
24788b38782SLi Zefan 	return NULL;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2508366025eSDave Hansen /*
2518366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2528366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2538366025eSDave Hansen  * We must keep track of when those operations start
2548366025eSDave Hansen  * (for permission checks) and when they end, so that
2558366025eSDave Hansen  * we can determine when writes are able to occur to
2568366025eSDave Hansen  * a filesystem.
2578366025eSDave Hansen  */
2583d733633SDave Hansen /*
2593d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2603d733633SDave Hansen  * @mnt: the mount to check for its write status
2613d733633SDave Hansen  *
2623d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2633d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2643d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2653d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2663d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2673d733633SDave Hansen  * r/w.
2683d733633SDave Hansen  */
26943f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt)
2703d733633SDave Hansen {
27143f5e655SDavid Howells 	return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
2723d733633SDave Hansen }
2733d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2743d733633SDave Hansen 
27583adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2763d733633SDave Hansen {
277d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
27868e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
279d3ef3d73Snpiggin@suse.de #else
28068e8a9feSAl Viro 	mnt->mnt_writers++;
281d3ef3d73Snpiggin@suse.de #endif
2823d733633SDave Hansen }
2833d733633SDave Hansen 
28483adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2853d733633SDave Hansen {
286d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
28768e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
288d3ef3d73Snpiggin@suse.de #else
28968e8a9feSAl Viro 	mnt->mnt_writers--;
290d3ef3d73Snpiggin@suse.de #endif
291d3ef3d73Snpiggin@suse.de }
292d3ef3d73Snpiggin@suse.de 
29383adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
294d3ef3d73Snpiggin@suse.de {
295d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
296d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2973d733633SDave Hansen 	int cpu;
2983d733633SDave Hansen 
2993d733633SDave Hansen 	for_each_possible_cpu(cpu) {
30068e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3013d733633SDave Hansen 	}
3023d733633SDave Hansen 
303d3ef3d73Snpiggin@suse.de 	return count;
304d3ef3d73Snpiggin@suse.de #else
305d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
306d3ef3d73Snpiggin@suse.de #endif
3073d733633SDave Hansen }
3083d733633SDave Hansen 
3094ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
3104ed5e82fSMiklos Szeredi {
3114ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
3124ed5e82fSMiklos Szeredi 		return 1;
3134ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
3144ed5e82fSMiklos Szeredi 	smp_rmb();
3154ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
3164ed5e82fSMiklos Szeredi }
3174ed5e82fSMiklos Szeredi 
3183d733633SDave Hansen /*
319eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
320eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
321eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
322eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
3233d733633SDave Hansen  */
3248366025eSDave Hansen /**
325eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
32683adc753SAl Viro  * @m: the mount on which to take a write
3278366025eSDave Hansen  *
328eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
329eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
330eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
331eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
332eb04c282SJan Kara  * called. This is effectively a refcount.
3338366025eSDave Hansen  */
334eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3358366025eSDave Hansen {
33683adc753SAl Viro 	struct mount *mnt = real_mount(m);
3373d733633SDave Hansen 	int ret = 0;
3383d733633SDave Hansen 
339d3ef3d73Snpiggin@suse.de 	preempt_disable();
340c6653a83SNick Piggin 	mnt_inc_writers(mnt);
341d3ef3d73Snpiggin@suse.de 	/*
342c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
343d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
344d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
345d3ef3d73Snpiggin@suse.de 	 */
346d3ef3d73Snpiggin@suse.de 	smp_mb();
3470f8821daSSebastian Andrzej Siewior 	might_lock(&mount_lock.lock);
3480f8821daSSebastian Andrzej Siewior 	while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
3490f8821daSSebastian Andrzej Siewior 		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
350d3ef3d73Snpiggin@suse.de 			cpu_relax();
3510f8821daSSebastian Andrzej Siewior 		} else {
3520f8821daSSebastian Andrzej Siewior 			/*
3530f8821daSSebastian Andrzej Siewior 			 * This prevents priority inversion, if the task
3540f8821daSSebastian Andrzej Siewior 			 * setting MNT_WRITE_HOLD got preempted on a remote
3550f8821daSSebastian Andrzej Siewior 			 * CPU, and it prevents life lock if the task setting
3560f8821daSSebastian Andrzej Siewior 			 * MNT_WRITE_HOLD has a lower priority and is bound to
3570f8821daSSebastian Andrzej Siewior 			 * the same CPU as the task that is spinning here.
3580f8821daSSebastian Andrzej Siewior 			 */
3590f8821daSSebastian Andrzej Siewior 			preempt_enable();
3600f8821daSSebastian Andrzej Siewior 			lock_mount_hash();
3610f8821daSSebastian Andrzej Siewior 			unlock_mount_hash();
3620f8821daSSebastian Andrzej Siewior 			preempt_disable();
3630f8821daSSebastian Andrzej Siewior 		}
3640f8821daSSebastian Andrzej Siewior 	}
365d3ef3d73Snpiggin@suse.de 	/*
366d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
367d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
368d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
369d3ef3d73Snpiggin@suse.de 	 */
370d3ef3d73Snpiggin@suse.de 	smp_rmb();
3714ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
372c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3733d733633SDave Hansen 		ret = -EROFS;
3743d733633SDave Hansen 	}
375d3ef3d73Snpiggin@suse.de 	preempt_enable();
376eb04c282SJan Kara 
377eb04c282SJan Kara 	return ret;
378eb04c282SJan Kara }
379eb04c282SJan Kara 
380eb04c282SJan Kara /**
381eb04c282SJan Kara  * mnt_want_write - get write access to a mount
382eb04c282SJan Kara  * @m: the mount on which to take a write
383eb04c282SJan Kara  *
384eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
385eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
386eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
387eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
388eb04c282SJan Kara  */
389eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
390eb04c282SJan Kara {
391eb04c282SJan Kara 	int ret;
392eb04c282SJan Kara 
393eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
394eb04c282SJan Kara 	ret = __mnt_want_write(m);
395eb04c282SJan Kara 	if (ret)
396eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3973d733633SDave Hansen 	return ret;
3988366025eSDave Hansen }
3998366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
4008366025eSDave Hansen 
4018366025eSDave Hansen /**
402eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
403eb04c282SJan Kara  * @file: the file who's mount on which to take a write
404eb04c282SJan Kara  *
40514e43bf4SEric Biggers  * This is like __mnt_want_write, but if the file is already open for writing it
40614e43bf4SEric Biggers  * skips incrementing mnt_writers (since the open file already has a reference)
40714e43bf4SEric Biggers  * and instead only does the check for emergency r/o remounts.  This must be
40814e43bf4SEric Biggers  * paired with __mnt_drop_write_file.
409eb04c282SJan Kara  */
410eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
411eb04c282SJan Kara {
41214e43bf4SEric Biggers 	if (file->f_mode & FMODE_WRITER) {
41314e43bf4SEric Biggers 		/*
41414e43bf4SEric Biggers 		 * Superblock may have become readonly while there are still
41514e43bf4SEric Biggers 		 * writable fd's, e.g. due to a fs error with errors=remount-ro
41614e43bf4SEric Biggers 		 */
41714e43bf4SEric Biggers 		if (__mnt_is_readonly(file->f_path.mnt))
41814e43bf4SEric Biggers 			return -EROFS;
41914e43bf4SEric Biggers 		return 0;
42014e43bf4SEric Biggers 	}
421eb04c282SJan Kara 	return __mnt_want_write(file->f_path.mnt);
422eb04c282SJan Kara }
423eb04c282SJan Kara 
424eb04c282SJan Kara /**
4257c6893e3SMiklos Szeredi  * mnt_want_write_file - get write access to a file's mount
4267c6893e3SMiklos Szeredi  * @file: the file who's mount on which to take a write
4277c6893e3SMiklos Szeredi  *
42814e43bf4SEric Biggers  * This is like mnt_want_write, but if the file is already open for writing it
42914e43bf4SEric Biggers  * skips incrementing mnt_writers (since the open file already has a reference)
43014e43bf4SEric Biggers  * and instead only does the freeze protection and the check for emergency r/o
43114e43bf4SEric Biggers  * remounts.  This must be paired with mnt_drop_write_file.
4327c6893e3SMiklos Szeredi  */
4337c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file)
4347c6893e3SMiklos Szeredi {
4357c6893e3SMiklos Szeredi 	int ret;
4367c6893e3SMiklos Szeredi 
4377c6893e3SMiklos Szeredi 	sb_start_write(file_inode(file)->i_sb);
4387c6893e3SMiklos Szeredi 	ret = __mnt_want_write_file(file);
4397c6893e3SMiklos Szeredi 	if (ret)
4407c6893e3SMiklos Szeredi 		sb_end_write(file_inode(file)->i_sb);
4417c6893e3SMiklos Szeredi 	return ret;
4427c6893e3SMiklos Szeredi }
44396029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
44496029c4eSnpiggin@suse.de 
44596029c4eSnpiggin@suse.de /**
446eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4478366025eSDave Hansen  * @mnt: the mount on which to give up write access
4488366025eSDave Hansen  *
4498366025eSDave Hansen  * Tells the low-level filesystem that we are done
4508366025eSDave Hansen  * performing writes to it.  Must be matched with
451eb04c282SJan Kara  * __mnt_want_write() call above.
4528366025eSDave Hansen  */
453eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4548366025eSDave Hansen {
455d3ef3d73Snpiggin@suse.de 	preempt_disable();
45683adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
457d3ef3d73Snpiggin@suse.de 	preempt_enable();
4588366025eSDave Hansen }
459eb04c282SJan Kara 
460eb04c282SJan Kara /**
461eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
462eb04c282SJan Kara  * @mnt: the mount on which to give up write access
463eb04c282SJan Kara  *
464eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
465eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
466eb04c282SJan Kara  * mnt_want_write() call above.
467eb04c282SJan Kara  */
468eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
469eb04c282SJan Kara {
470eb04c282SJan Kara 	__mnt_drop_write(mnt);
471eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
472eb04c282SJan Kara }
4738366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4748366025eSDave Hansen 
475eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
476eb04c282SJan Kara {
47714e43bf4SEric Biggers 	if (!(file->f_mode & FMODE_WRITER))
478eb04c282SJan Kara 		__mnt_drop_write(file->f_path.mnt);
479eb04c282SJan Kara }
480eb04c282SJan Kara 
4817c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file)
4827c6893e3SMiklos Szeredi {
483a6795a58SMiklos Szeredi 	__mnt_drop_write_file(file);
4847c6893e3SMiklos Szeredi 	sb_end_write(file_inode(file)->i_sb);
4857c6893e3SMiklos Szeredi }
4862a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4872a79f17eSAl Viro 
488538f4f02SChristian Brauner /**
489538f4f02SChristian Brauner  * mnt_hold_writers - prevent write access to the given mount
490538f4f02SChristian Brauner  * @mnt: mnt to prevent write access to
491538f4f02SChristian Brauner  *
492538f4f02SChristian Brauner  * Prevents write access to @mnt if there are no active writers for @mnt.
493538f4f02SChristian Brauner  * This function needs to be called and return successfully before changing
494538f4f02SChristian Brauner  * properties of @mnt that need to remain stable for callers with write access
495538f4f02SChristian Brauner  * to @mnt.
496538f4f02SChristian Brauner  *
497538f4f02SChristian Brauner  * After this functions has been called successfully callers must pair it with
498538f4f02SChristian Brauner  * a call to mnt_unhold_writers() in order to stop preventing write access to
499538f4f02SChristian Brauner  * @mnt.
500538f4f02SChristian Brauner  *
501538f4f02SChristian Brauner  * Context: This function expects lock_mount_hash() to be held serializing
502538f4f02SChristian Brauner  *          setting MNT_WRITE_HOLD.
503538f4f02SChristian Brauner  * Return: On success 0 is returned.
504538f4f02SChristian Brauner  *	   On error, -EBUSY is returned.
505538f4f02SChristian Brauner  */
506fbdc2f6cSChristian Brauner static inline int mnt_hold_writers(struct mount *mnt)
5078366025eSDave Hansen {
50883adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
509d3ef3d73Snpiggin@suse.de 	/*
510d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
511d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
512d3ef3d73Snpiggin@suse.de 	 */
513d3ef3d73Snpiggin@suse.de 	smp_mb();
514d3ef3d73Snpiggin@suse.de 
515d3ef3d73Snpiggin@suse.de 	/*
516d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
517d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
518d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
519d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
520d3ef3d73Snpiggin@suse.de 	 *
521d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
522d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
523d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
524d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
525d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
526d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
527d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
528d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
529d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
530d3ef3d73Snpiggin@suse.de 	 */
531c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
532fbdc2f6cSChristian Brauner 		return -EBUSY;
533fbdc2f6cSChristian Brauner 
534fbdc2f6cSChristian Brauner 	return 0;
535fbdc2f6cSChristian Brauner }
536fbdc2f6cSChristian Brauner 
537538f4f02SChristian Brauner /**
538538f4f02SChristian Brauner  * mnt_unhold_writers - stop preventing write access to the given mount
539538f4f02SChristian Brauner  * @mnt: mnt to stop preventing write access to
540538f4f02SChristian Brauner  *
541538f4f02SChristian Brauner  * Stop preventing write access to @mnt allowing callers to gain write access
542538f4f02SChristian Brauner  * to @mnt again.
543538f4f02SChristian Brauner  *
544538f4f02SChristian Brauner  * This function can only be called after a successful call to
545538f4f02SChristian Brauner  * mnt_hold_writers().
546538f4f02SChristian Brauner  *
547538f4f02SChristian Brauner  * Context: This function expects lock_mount_hash() to be held.
548538f4f02SChristian Brauner  */
549fbdc2f6cSChristian Brauner static inline void mnt_unhold_writers(struct mount *mnt)
550fbdc2f6cSChristian Brauner {
551d3ef3d73Snpiggin@suse.de 	/*
552d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
553d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
554d3ef3d73Snpiggin@suse.de 	 */
555d3ef3d73Snpiggin@suse.de 	smp_wmb();
55683adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
557fbdc2f6cSChristian Brauner }
558fbdc2f6cSChristian Brauner 
559fbdc2f6cSChristian Brauner static int mnt_make_readonly(struct mount *mnt)
560fbdc2f6cSChristian Brauner {
561fbdc2f6cSChristian Brauner 	int ret;
562fbdc2f6cSChristian Brauner 
563fbdc2f6cSChristian Brauner 	ret = mnt_hold_writers(mnt);
564fbdc2f6cSChristian Brauner 	if (!ret)
565fbdc2f6cSChristian Brauner 		mnt->mnt.mnt_flags |= MNT_READONLY;
566fbdc2f6cSChristian Brauner 	mnt_unhold_writers(mnt);
5673d733633SDave Hansen 	return ret;
5683d733633SDave Hansen }
5698366025eSDave Hansen 
5704ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5714ed5e82fSMiklos Szeredi {
5724ed5e82fSMiklos Szeredi 	struct mount *mnt;
5734ed5e82fSMiklos Szeredi 	int err = 0;
5744ed5e82fSMiklos Szeredi 
5758e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5768e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5778e8b8796SMiklos Szeredi 		return -EBUSY;
5788e8b8796SMiklos Szeredi 
579719ea2fbSAl Viro 	lock_mount_hash();
5804ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5814ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
582ad1844a0SChristian Brauner 			err = mnt_hold_writers(mnt);
583ad1844a0SChristian Brauner 			if (err)
5844ed5e82fSMiklos Szeredi 				break;
5854ed5e82fSMiklos Szeredi 		}
5864ed5e82fSMiklos Szeredi 	}
5878e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5888e8b8796SMiklos Szeredi 		err = -EBUSY;
5898e8b8796SMiklos Szeredi 
5904ed5e82fSMiklos Szeredi 	if (!err) {
5914ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
5924ed5e82fSMiklos Szeredi 		smp_wmb();
5934ed5e82fSMiklos Szeredi 	}
5944ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5954ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
5964ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
5974ed5e82fSMiklos Szeredi 	}
598719ea2fbSAl Viro 	unlock_mount_hash();
5994ed5e82fSMiklos Szeredi 
6004ed5e82fSMiklos Szeredi 	return err;
6014ed5e82fSMiklos Szeredi }
6024ed5e82fSMiklos Szeredi 
603b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
6041da177e4SLinus Torvalds {
605a6435940SChristian Brauner 	struct user_namespace *mnt_userns;
606a6435940SChristian Brauner 
607a6435940SChristian Brauner 	mnt_userns = mnt_user_ns(&mnt->mnt);
608bd303368SChristian Brauner 	if (!initial_idmapping(mnt_userns))
609a6435940SChristian Brauner 		put_user_ns(mnt_userns);
610fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
611d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
61268e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
613d3ef3d73Snpiggin@suse.de #endif
614b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6178ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
6188ffcb32eSDavid Howells {
6198ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
6208ffcb32eSDavid Howells }
6218ffcb32eSDavid Howells 
62248a066e7SAl Viro /* call under rcu_read_lock */
623294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
62448a066e7SAl Viro {
62548a066e7SAl Viro 	struct mount *mnt;
62648a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
627294d71ffSAl Viro 		return 1;
62848a066e7SAl Viro 	if (bastard == NULL)
629294d71ffSAl Viro 		return 0;
63048a066e7SAl Viro 	mnt = real_mount(bastard);
63148a066e7SAl Viro 	mnt_add_count(mnt, 1);
632119e1ef8SAl Viro 	smp_mb();			// see mntput_no_expire()
63348a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
634294d71ffSAl Viro 		return 0;
63548a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
63648a066e7SAl Viro 		mnt_add_count(mnt, -1);
637294d71ffSAl Viro 		return 1;
63848a066e7SAl Viro 	}
639119e1ef8SAl Viro 	lock_mount_hash();
640119e1ef8SAl Viro 	if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
641119e1ef8SAl Viro 		mnt_add_count(mnt, -1);
642119e1ef8SAl Viro 		unlock_mount_hash();
643119e1ef8SAl Viro 		return 1;
644119e1ef8SAl Viro 	}
645119e1ef8SAl Viro 	unlock_mount_hash();
646119e1ef8SAl Viro 	/* caller will mntput() */
647294d71ffSAl Viro 	return -1;
648294d71ffSAl Viro }
649294d71ffSAl Viro 
650294d71ffSAl Viro /* call under rcu_read_lock */
6517e4745a0SAl Viro static bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
652294d71ffSAl Viro {
653294d71ffSAl Viro 	int res = __legitimize_mnt(bastard, seq);
654294d71ffSAl Viro 	if (likely(!res))
655294d71ffSAl Viro 		return true;
656294d71ffSAl Viro 	if (unlikely(res < 0)) {
65748a066e7SAl Viro 		rcu_read_unlock();
65848a066e7SAl Viro 		mntput(bastard);
65948a066e7SAl Viro 		rcu_read_lock();
660294d71ffSAl Viro 	}
66148a066e7SAl Viro 	return false;
66248a066e7SAl Viro }
66348a066e7SAl Viro 
6641da177e4SLinus Torvalds /*
665474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
66648a066e7SAl Viro  * call under rcu_read_lock()
6671da177e4SLinus Torvalds  */
668474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6691da177e4SLinus Torvalds {
67038129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
671474279dcSAl Viro 	struct mount *p;
6721da177e4SLinus Torvalds 
67338129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
674474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
675474279dcSAl Viro 			return p;
676474279dcSAl Viro 	return NULL;
6771da177e4SLinus Torvalds }
678474279dcSAl Viro 
679474279dcSAl Viro /*
680f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
681f015f126SDavid Howells  *
682f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
683f015f126SDavid Howells  * following mounts:
684f015f126SDavid Howells  *
685f015f126SDavid Howells  * mount /dev/sda1 /mnt
686f015f126SDavid Howells  * mount /dev/sda2 /mnt
687f015f126SDavid Howells  * mount /dev/sda3 /mnt
688f015f126SDavid Howells  *
689f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
690f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
691f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
692f015f126SDavid Howells  *
693f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
694a05964f3SRam Pai  */
695ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path)
696a05964f3SRam Pai {
697c7105365SAl Viro 	struct mount *child_mnt;
69848a066e7SAl Viro 	struct vfsmount *m;
69948a066e7SAl Viro 	unsigned seq;
70099b7db7bSNick Piggin 
70148a066e7SAl Viro 	rcu_read_lock();
70248a066e7SAl Viro 	do {
70348a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
704474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
70548a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
70648a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
70748a066e7SAl Viro 	rcu_read_unlock();
70848a066e7SAl Viro 	return m;
709a05964f3SRam Pai }
710a05964f3SRam Pai 
7119f6c61f9SMiklos Szeredi static inline void lock_ns_list(struct mnt_namespace *ns)
7129f6c61f9SMiklos Szeredi {
7139f6c61f9SMiklos Szeredi 	spin_lock(&ns->ns_lock);
7149f6c61f9SMiklos Szeredi }
7159f6c61f9SMiklos Szeredi 
7169f6c61f9SMiklos Szeredi static inline void unlock_ns_list(struct mnt_namespace *ns)
7179f6c61f9SMiklos Szeredi {
7189f6c61f9SMiklos Szeredi 	spin_unlock(&ns->ns_lock);
7199f6c61f9SMiklos Szeredi }
7209f6c61f9SMiklos Szeredi 
7219f6c61f9SMiklos Szeredi static inline bool mnt_is_cursor(struct mount *mnt)
7229f6c61f9SMiklos Szeredi {
7239f6c61f9SMiklos Szeredi 	return mnt->mnt.mnt_flags & MNT_CURSOR;
7249f6c61f9SMiklos Szeredi }
7259f6c61f9SMiklos Szeredi 
7267af1364fSEric W. Biederman /*
7277af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
7287af1364fSEric W. Biederman  *                         current mount namespace.
7297af1364fSEric W. Biederman  *
7307af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
7317af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
7327af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
7337af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
7347af1364fSEric W. Biederman  * is a mountpoint.
7357af1364fSEric W. Biederman  *
7367af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
7377af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
7387af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
7397af1364fSEric W. Biederman  * parent mount.
7407af1364fSEric W. Biederman  */
7417af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
7427af1364fSEric W. Biederman {
7437af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
7447af1364fSEric W. Biederman 	struct mount *mnt;
7457af1364fSEric W. Biederman 	bool is_covered = false;
7467af1364fSEric W. Biederman 
7477af1364fSEric W. Biederman 	down_read(&namespace_sem);
7489f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
7497af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
7509f6c61f9SMiklos Szeredi 		if (mnt_is_cursor(mnt))
7519f6c61f9SMiklos Szeredi 			continue;
7527af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
7537af1364fSEric W. Biederman 		if (is_covered)
7547af1364fSEric W. Biederman 			break;
7557af1364fSEric W. Biederman 	}
7569f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
7577af1364fSEric W. Biederman 	up_read(&namespace_sem);
7585ad05cc8SNikolay Borisov 
7597af1364fSEric W. Biederman 	return is_covered;
7607af1364fSEric W. Biederman }
7617af1364fSEric W. Biederman 
762e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
76384d17192SAl Viro {
7640818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
76584d17192SAl Viro 	struct mountpoint *mp;
76684d17192SAl Viro 
7670818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
76884d17192SAl Viro 		if (mp->m_dentry == dentry) {
76984d17192SAl Viro 			mp->m_count++;
77084d17192SAl Viro 			return mp;
77184d17192SAl Viro 		}
77284d17192SAl Viro 	}
773e2dfa935SEric W. Biederman 	return NULL;
774e2dfa935SEric W. Biederman }
775e2dfa935SEric W. Biederman 
7763895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry)
777e2dfa935SEric W. Biederman {
7783895dbf8SEric W. Biederman 	struct mountpoint *mp, *new = NULL;
779e2dfa935SEric W. Biederman 	int ret;
78084d17192SAl Viro 
7813895dbf8SEric W. Biederman 	if (d_mountpoint(dentry)) {
7821e9c75fbSBenjamin Coddington 		/* might be worth a WARN_ON() */
7831e9c75fbSBenjamin Coddington 		if (d_unlinked(dentry))
7841e9c75fbSBenjamin Coddington 			return ERR_PTR(-ENOENT);
7853895dbf8SEric W. Biederman mountpoint:
7863895dbf8SEric W. Biederman 		read_seqlock_excl(&mount_lock);
7873895dbf8SEric W. Biederman 		mp = lookup_mountpoint(dentry);
7883895dbf8SEric W. Biederman 		read_sequnlock_excl(&mount_lock);
7893895dbf8SEric W. Biederman 		if (mp)
7903895dbf8SEric W. Biederman 			goto done;
79184d17192SAl Viro 	}
792eed81007SMiklos Szeredi 
7933895dbf8SEric W. Biederman 	if (!new)
7943895dbf8SEric W. Biederman 		new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
7953895dbf8SEric W. Biederman 	if (!new)
7963895dbf8SEric W. Biederman 		return ERR_PTR(-ENOMEM);
7973895dbf8SEric W. Biederman 
7983895dbf8SEric W. Biederman 
7993895dbf8SEric W. Biederman 	/* Exactly one processes may set d_mounted */
8003895dbf8SEric W. Biederman 	ret = d_set_mounted(dentry);
8013895dbf8SEric W. Biederman 
8023895dbf8SEric W. Biederman 	/* Someone else set d_mounted? */
8033895dbf8SEric W. Biederman 	if (ret == -EBUSY)
8043895dbf8SEric W. Biederman 		goto mountpoint;
8053895dbf8SEric W. Biederman 
8063895dbf8SEric W. Biederman 	/* The dentry is not available as a mountpoint? */
8073895dbf8SEric W. Biederman 	mp = ERR_PTR(ret);
8083895dbf8SEric W. Biederman 	if (ret)
8093895dbf8SEric W. Biederman 		goto done;
8103895dbf8SEric W. Biederman 
8113895dbf8SEric W. Biederman 	/* Add the new mountpoint to the hash table */
8123895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
8134edbe133SAl Viro 	new->m_dentry = dget(dentry);
8143895dbf8SEric W. Biederman 	new->m_count = 1;
8153895dbf8SEric W. Biederman 	hlist_add_head(&new->m_hash, mp_hash(dentry));
8163895dbf8SEric W. Biederman 	INIT_HLIST_HEAD(&new->m_list);
8173895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
8183895dbf8SEric W. Biederman 
8193895dbf8SEric W. Biederman 	mp = new;
8203895dbf8SEric W. Biederman 	new = NULL;
8213895dbf8SEric W. Biederman done:
8223895dbf8SEric W. Biederman 	kfree(new);
82384d17192SAl Viro 	return mp;
82484d17192SAl Viro }
82584d17192SAl Viro 
8264edbe133SAl Viro /*
8274edbe133SAl Viro  * vfsmount lock must be held.  Additionally, the caller is responsible
8284edbe133SAl Viro  * for serializing calls for given disposal list.
8294edbe133SAl Viro  */
8304edbe133SAl Viro static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
83184d17192SAl Viro {
83284d17192SAl Viro 	if (!--mp->m_count) {
83384d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
8340a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
83584d17192SAl Viro 		spin_lock(&dentry->d_lock);
83684d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
83784d17192SAl Viro 		spin_unlock(&dentry->d_lock);
8384edbe133SAl Viro 		dput_to_list(dentry, list);
8390818bf27SAl Viro 		hlist_del(&mp->m_hash);
84084d17192SAl Viro 		kfree(mp);
84184d17192SAl Viro 	}
84284d17192SAl Viro }
84384d17192SAl Viro 
8444edbe133SAl Viro /* called with namespace_lock and vfsmount lock */
8454edbe133SAl Viro static void put_mountpoint(struct mountpoint *mp)
8464edbe133SAl Viro {
8474edbe133SAl Viro 	__put_mountpoint(mp, &ex_mountpoints);
8484edbe133SAl Viro }
8494edbe133SAl Viro 
850143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
8511da177e4SLinus Torvalds {
8526b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
85599b7db7bSNick Piggin /*
85699b7db7bSNick Piggin  * vfsmount lock must be held for write
85799b7db7bSNick Piggin  */
8586b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
8595addc5ddSAl Viro {
8605addc5ddSAl Viro 	if (ns) {
8615addc5ddSAl Viro 		ns->event = ++event;
8625addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
8635addc5ddSAl Viro 	}
8645addc5ddSAl Viro }
8655addc5ddSAl Viro 
86699b7db7bSNick Piggin /*
86799b7db7bSNick Piggin  * vfsmount lock must be held for write
86899b7db7bSNick Piggin  */
8696b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
8705addc5ddSAl Viro {
8715addc5ddSAl Viro 	if (ns && ns->event != event) {
8725addc5ddSAl Viro 		ns->event = event;
8735addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
8745addc5ddSAl Viro 	}
8755addc5ddSAl Viro }
8765addc5ddSAl Viro 
87799b7db7bSNick Piggin /*
87899b7db7bSNick Piggin  * vfsmount lock must be held for write
87999b7db7bSNick Piggin  */
880e4e59906SAl Viro static struct mountpoint *unhash_mnt(struct mount *mnt)
8811da177e4SLinus Torvalds {
882e4e59906SAl Viro 	struct mountpoint *mp;
8830714a533SAl Viro 	mnt->mnt_parent = mnt;
884a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8856b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
88638129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
8870a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
888e4e59906SAl Viro 	mp = mnt->mnt_mp;
88984d17192SAl Viro 	mnt->mnt_mp = NULL;
890e4e59906SAl Viro 	return mp;
8917bdb11deSEric W. Biederman }
8927bdb11deSEric W. Biederman 
8937bdb11deSEric W. Biederman /*
8947bdb11deSEric W. Biederman  * vfsmount lock must be held for write
8957bdb11deSEric W. Biederman  */
8966a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt)
8976a46c573SEric W. Biederman {
898e4e59906SAl Viro 	put_mountpoint(unhash_mnt(mnt));
8996a46c573SEric W. Biederman }
9006a46c573SEric W. Biederman 
9016a46c573SEric W. Biederman /*
9026a46c573SEric W. Biederman  * vfsmount lock must be held for write
9036a46c573SEric W. Biederman  */
90484d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
90584d17192SAl Viro 			struct mountpoint *mp,
90644d964d6SAl Viro 			struct mount *child_mnt)
907b90fa9aeSRam Pai {
90884d17192SAl Viro 	mp->m_count++;
9093a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
9104edbe133SAl Viro 	child_mnt->mnt_mountpoint = mp->m_dentry;
9113a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
91284d17192SAl Viro 	child_mnt->mnt_mp = mp;
9130a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
914b90fa9aeSRam Pai }
915b90fa9aeSRam Pai 
9161064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent)
9171064f874SEric W. Biederman {
9181064f874SEric W. Biederman 	hlist_add_head_rcu(&mnt->mnt_hash,
9191064f874SEric W. Biederman 			   m_hash(&parent->mnt, mnt->mnt_mountpoint));
9201064f874SEric W. Biederman 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
9211064f874SEric W. Biederman }
9221064f874SEric W. Biederman 
92399b7db7bSNick Piggin /*
92499b7db7bSNick Piggin  * vfsmount lock must be held for write
92599b7db7bSNick Piggin  */
92684d17192SAl Viro static void attach_mnt(struct mount *mnt,
92784d17192SAl Viro 			struct mount *parent,
92884d17192SAl Viro 			struct mountpoint *mp)
9291da177e4SLinus Torvalds {
93084d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
9311064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
932b90fa9aeSRam Pai }
933b90fa9aeSRam Pai 
9341064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
93512a5b529SAl Viro {
9361064f874SEric W. Biederman 	struct mountpoint *old_mp = mnt->mnt_mp;
9371064f874SEric W. Biederman 	struct mount *old_parent = mnt->mnt_parent;
9381064f874SEric W. Biederman 
9391064f874SEric W. Biederman 	list_del_init(&mnt->mnt_child);
9401064f874SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
9411064f874SEric W. Biederman 	hlist_del_init_rcu(&mnt->mnt_hash);
9421064f874SEric W. Biederman 
9431064f874SEric W. Biederman 	attach_mnt(mnt, parent, mp);
9441064f874SEric W. Biederman 
9451064f874SEric W. Biederman 	put_mountpoint(old_mp);
9461064f874SEric W. Biederman 	mnt_add_count(old_parent, -1);
94712a5b529SAl Viro }
94812a5b529SAl Viro 
949b90fa9aeSRam Pai /*
95099b7db7bSNick Piggin  * vfsmount lock must be held for write
951b90fa9aeSRam Pai  */
9521064f874SEric W. Biederman static void commit_tree(struct mount *mnt)
953b90fa9aeSRam Pai {
9540714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
95583adc753SAl Viro 	struct mount *m;
956b90fa9aeSRam Pai 	LIST_HEAD(head);
957143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
958b90fa9aeSRam Pai 
9590714a533SAl Viro 	BUG_ON(parent == mnt);
960b90fa9aeSRam Pai 
9611a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
962f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
963143c8c91SAl Viro 		m->mnt_ns = n;
964f03c6599SAl Viro 
965b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
966b90fa9aeSRam Pai 
967d2921684SEric W. Biederman 	n->mounts += n->pending_mounts;
968d2921684SEric W. Biederman 	n->pending_mounts = 0;
969d2921684SEric W. Biederman 
9701064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
9716b3286edSKirill Korotaev 	touch_mnt_namespace(n);
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
974909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
9751da177e4SLinus Torvalds {
9766b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
9776b41d536SAl Viro 	if (next == &p->mnt_mounts) {
9781da177e4SLinus Torvalds 		while (1) {
979909b0a88SAl Viro 			if (p == root)
9801da177e4SLinus Torvalds 				return NULL;
9816b41d536SAl Viro 			next = p->mnt_child.next;
9826b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
9831da177e4SLinus Torvalds 				break;
9840714a533SAl Viro 			p = p->mnt_parent;
9851da177e4SLinus Torvalds 		}
9861da177e4SLinus Torvalds 	}
9876b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
990315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
9919676f0c6SRam Pai {
9926b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
9936b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
9946b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
9956b41d536SAl Viro 		prev = p->mnt_mounts.prev;
9969676f0c6SRam Pai 	}
9979676f0c6SRam Pai 	return p;
9989676f0c6SRam Pai }
9999676f0c6SRam Pai 
10008f291889SAl Viro /**
10018f291889SAl Viro  * vfs_create_mount - Create a mount for a configured superblock
10028f291889SAl Viro  * @fc: The configuration context with the superblock attached
10038f291889SAl Viro  *
10048f291889SAl Viro  * Create a mount to an already configured superblock.  If necessary, the
10058f291889SAl Viro  * caller should invoke vfs_get_tree() before calling this.
10068f291889SAl Viro  *
10078f291889SAl Viro  * Note that this does not attach the mount to anything.
10088f291889SAl Viro  */
10098f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc)
10109d412a43SAl Viro {
1011b105e270SAl Viro 	struct mount *mnt;
1012bd303368SChristian Brauner 	struct user_namespace *fs_userns;
10139d412a43SAl Viro 
10148f291889SAl Viro 	if (!fc->root)
10158f291889SAl Viro 		return ERR_PTR(-EINVAL);
10169d412a43SAl Viro 
10178f291889SAl Viro 	mnt = alloc_vfsmnt(fc->source ?: "none");
10189d412a43SAl Viro 	if (!mnt)
10199d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
10209d412a43SAl Viro 
10218f291889SAl Viro 	if (fc->sb_flags & SB_KERNMOUNT)
1022b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
10239d412a43SAl Viro 
10248f291889SAl Viro 	atomic_inc(&fc->root->d_sb->s_active);
10258f291889SAl Viro 	mnt->mnt.mnt_sb		= fc->root->d_sb;
10268f291889SAl Viro 	mnt->mnt.mnt_root	= dget(fc->root);
1027a73324daSAl Viro 	mnt->mnt_mountpoint	= mnt->mnt.mnt_root;
10280714a533SAl Viro 	mnt->mnt_parent		= mnt;
10298f291889SAl Viro 
1030bd303368SChristian Brauner 	fs_userns = mnt->mnt.mnt_sb->s_user_ns;
1031bd303368SChristian Brauner 	if (!initial_idmapping(fs_userns))
1032bd303368SChristian Brauner 		mnt->mnt.mnt_userns = get_user_ns(fs_userns);
1033bd303368SChristian Brauner 
1034719ea2fbSAl Viro 	lock_mount_hash();
10358f291889SAl Viro 	list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
1036719ea2fbSAl Viro 	unlock_mount_hash();
1037b105e270SAl Viro 	return &mnt->mnt;
10389d412a43SAl Viro }
10398f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount);
10408f291889SAl Viro 
10418f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc)
10428f291889SAl Viro {
10438f291889SAl Viro 	int err = vfs_get_tree(fc);
10448f291889SAl Viro 	if (!err) {
10458f291889SAl Viro 		up_write(&fc->root->d_sb->s_umount);
10468f291889SAl Viro 		return vfs_create_mount(fc);
10478f291889SAl Viro 	}
10488f291889SAl Viro 	return ERR_PTR(err);
10498f291889SAl Viro }
10508f291889SAl Viro EXPORT_SYMBOL(fc_mount);
10518f291889SAl Viro 
10529bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type,
10539bc61ab1SDavid Howells 				int flags, const char *name,
10549bc61ab1SDavid Howells 				void *data)
10551da177e4SLinus Torvalds {
10569bc61ab1SDavid Howells 	struct fs_context *fc;
10578f291889SAl Viro 	struct vfsmount *mnt;
10589bc61ab1SDavid Howells 	int ret = 0;
10599d412a43SAl Viro 
10609d412a43SAl Viro 	if (!type)
10613e1aeb00SDavid Howells 		return ERR_PTR(-EINVAL);
10629d412a43SAl Viro 
10639bc61ab1SDavid Howells 	fc = fs_context_for_mount(type, flags);
10649bc61ab1SDavid Howells 	if (IS_ERR(fc))
10659bc61ab1SDavid Howells 		return ERR_CAST(fc);
10669bc61ab1SDavid Howells 
10673e1aeb00SDavid Howells 	if (name)
10683e1aeb00SDavid Howells 		ret = vfs_parse_fs_string(fc, "source",
10693e1aeb00SDavid Howells 					  name, strlen(name));
10709bc61ab1SDavid Howells 	if (!ret)
10719bc61ab1SDavid Howells 		ret = parse_monolithic_mount_data(fc, data);
10729bc61ab1SDavid Howells 	if (!ret)
10738f291889SAl Viro 		mnt = fc_mount(fc);
10748f291889SAl Viro 	else
10758f291889SAl Viro 		mnt = ERR_PTR(ret);
10769d412a43SAl Viro 
10779bc61ab1SDavid Howells 	put_fs_context(fc);
10788f291889SAl Viro 	return mnt;
10799d412a43SAl Viro }
10809d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
10819d412a43SAl Viro 
108293faccbbSEric W. Biederman struct vfsmount *
108393faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
108493faccbbSEric W. Biederman 	     const char *name, void *data)
108593faccbbSEric W. Biederman {
108693faccbbSEric W. Biederman 	/* Until it is worked out how to pass the user namespace
108793faccbbSEric W. Biederman 	 * through from the parent mount to the submount don't support
108893faccbbSEric W. Biederman 	 * unprivileged mounts with submounts.
108993faccbbSEric W. Biederman 	 */
109093faccbbSEric W. Biederman 	if (mountpoint->d_sb->s_user_ns != &init_user_ns)
109193faccbbSEric W. Biederman 		return ERR_PTR(-EPERM);
109293faccbbSEric W. Biederman 
1093e462ec50SDavid Howells 	return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
109493faccbbSEric W. Biederman }
109593faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount);
109693faccbbSEric W. Biederman 
109787129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
109836341f64SRam Pai 					int flag)
10991da177e4SLinus Torvalds {
110087129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
1101be34d1a3SDavid Howells 	struct mount *mnt;
1102be34d1a3SDavid Howells 	int err;
11031da177e4SLinus Torvalds 
1104be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
1105be34d1a3SDavid Howells 	if (!mnt)
1106be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
1107be34d1a3SDavid Howells 
11087a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
110915169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
1110719f5d7fSMiklos Szeredi 	else
111115169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
1112719f5d7fSMiklos Szeredi 
111315169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1114be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
1115719f5d7fSMiklos Szeredi 		if (err)
1116719f5d7fSMiklos Szeredi 			goto out_free;
1117719f5d7fSMiklos Szeredi 	}
1118719f5d7fSMiklos Szeredi 
111916a34adbSAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags;
112016a34adbSAl Viro 	mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
11215ff9d8a6SEric W. Biederman 
11221da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1123a6435940SChristian Brauner 	mnt->mnt.mnt_userns = mnt_user_ns(&old->mnt);
1124bd303368SChristian Brauner 	if (!initial_idmapping(mnt->mnt.mnt_userns))
1125a6435940SChristian Brauner 		mnt->mnt.mnt_userns = get_user_ns(mnt->mnt.mnt_userns);
1126b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1127b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1128a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
11290714a533SAl Viro 	mnt->mnt_parent = mnt;
1130719ea2fbSAl Viro 	lock_mount_hash();
113139f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1132719ea2fbSAl Viro 	unlock_mount_hash();
1133b90fa9aeSRam Pai 
11347a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
11357a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
11366776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
113732301920SAl Viro 		mnt->mnt_master = old;
1138fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
11398aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1140fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
11416776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1142d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
11436776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1144d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
11455235d448SAl Viro 	} else {
11465235d448SAl Viro 		CLEAR_MNT_SHARED(mnt);
11475afe0022SRam Pai 	}
1148b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
11490f0afb1dSAl Viro 		set_mnt_shared(mnt);
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
11521da177e4SLinus Torvalds 	 * as the original if that was on one */
115336341f64SRam Pai 	if (flag & CL_EXPIRE) {
11546776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
11556776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
11561da177e4SLinus Torvalds 	}
1157be34d1a3SDavid Howells 
1158cb338d06SAl Viro 	return mnt;
1159719f5d7fSMiklos Szeredi 
1160719f5d7fSMiklos Szeredi  out_free:
11618ffcb32eSDavid Howells 	mnt_free_id(mnt);
1162719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1163be34d1a3SDavid Howells 	return ERR_PTR(err);
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
11669ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
11679ea459e1SAl Viro {
116856cbb429SAl Viro 	struct hlist_node *p;
116956cbb429SAl Viro 	struct mount *m;
11709ea459e1SAl Viro 	/*
117156cbb429SAl Viro 	 * The warning here probably indicates that somebody messed
117256cbb429SAl Viro 	 * up a mnt_want/drop_write() pair.  If this happens, the
117356cbb429SAl Viro 	 * filesystem was probably unable to make r/w->r/o transitions.
11749ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
11759ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
11769ea459e1SAl Viro 	 */
11779ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
11789ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
11799ea459e1SAl Viro 		mnt_pin_kill(mnt);
118056cbb429SAl Viro 	hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
118156cbb429SAl Viro 		hlist_del(&m->mnt_umount);
118256cbb429SAl Viro 		mntput(&m->mnt);
118356cbb429SAl Viro 	}
11849ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
11859ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
11869ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
11879ea459e1SAl Viro 	mnt_free_id(mnt);
11889ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
11899ea459e1SAl Viro }
11909ea459e1SAl Viro 
11919ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
11929ea459e1SAl Viro {
11939ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
11949ea459e1SAl Viro }
11959ea459e1SAl Viro 
11969ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
11979ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
11989ea459e1SAl Viro {
11999ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
120029785735SByungchul Park 	struct mount *m, *t;
12019ea459e1SAl Viro 
120229785735SByungchul Park 	llist_for_each_entry_safe(m, t, node, mnt_llist)
120329785735SByungchul Park 		cleanup_mnt(m);
12049ea459e1SAl Viro }
12059ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
12069ea459e1SAl Viro 
1207900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
12087b7b1aceSAl Viro {
12094edbe133SAl Viro 	LIST_HEAD(list);
1210edf7ddbfSEric Biggers 	int count;
12114edbe133SAl Viro 
121248a066e7SAl Viro 	rcu_read_lock();
12139ea0a46cSAl Viro 	if (likely(READ_ONCE(mnt->mnt_ns))) {
12149ea0a46cSAl Viro 		/*
12159ea0a46cSAl Viro 		 * Since we don't do lock_mount_hash() here,
12169ea0a46cSAl Viro 		 * ->mnt_ns can change under us.  However, if it's
12179ea0a46cSAl Viro 		 * non-NULL, then there's a reference that won't
12189ea0a46cSAl Viro 		 * be dropped until after an RCU delay done after
12199ea0a46cSAl Viro 		 * turning ->mnt_ns NULL.  So if we observe it
12209ea0a46cSAl Viro 		 * non-NULL under rcu_read_lock(), the reference
12219ea0a46cSAl Viro 		 * we are dropping is not the final one.
12229ea0a46cSAl Viro 		 */
1223aa9c0e07SAl Viro 		mnt_add_count(mnt, -1);
122448a066e7SAl Viro 		rcu_read_unlock();
122599b7db7bSNick Piggin 		return;
1226b3e19d92SNick Piggin 	}
1227719ea2fbSAl Viro 	lock_mount_hash();
1228119e1ef8SAl Viro 	/*
1229119e1ef8SAl Viro 	 * make sure that if __legitimize_mnt() has not seen us grab
1230119e1ef8SAl Viro 	 * mount_lock, we'll see their refcount increment here.
1231119e1ef8SAl Viro 	 */
1232119e1ef8SAl Viro 	smp_mb();
12339ea0a46cSAl Viro 	mnt_add_count(mnt, -1);
1234edf7ddbfSEric Biggers 	count = mnt_get_count(mnt);
1235edf7ddbfSEric Biggers 	if (count != 0) {
1236edf7ddbfSEric Biggers 		WARN_ON(count < 0);
123748a066e7SAl Viro 		rcu_read_unlock();
1238719ea2fbSAl Viro 		unlock_mount_hash();
123999b7db7bSNick Piggin 		return;
124099b7db7bSNick Piggin 	}
124148a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
124248a066e7SAl Viro 		rcu_read_unlock();
124348a066e7SAl Viro 		unlock_mount_hash();
124448a066e7SAl Viro 		return;
124548a066e7SAl Viro 	}
124648a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
124748a066e7SAl Viro 	rcu_read_unlock();
1248962830dfSAndi Kleen 
124939f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1250ce07d891SEric W. Biederman 
1251ce07d891SEric W. Biederman 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1252ce07d891SEric W. Biederman 		struct mount *p, *tmp;
1253ce07d891SEric W. Biederman 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
12544edbe133SAl Viro 			__put_mountpoint(unhash_mnt(p), &list);
125556cbb429SAl Viro 			hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
1256ce07d891SEric W. Biederman 		}
1257ce07d891SEric W. Biederman 	}
1258719ea2fbSAl Viro 	unlock_mount_hash();
12594edbe133SAl Viro 	shrink_dentry_list(&list);
1260649a795aSAl Viro 
12619ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
12629ea459e1SAl Viro 		struct task_struct *task = current;
12639ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
12649ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
126591989c70SJens Axboe 			if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
12669ea459e1SAl Viro 				return;
12679ea459e1SAl Viro 		}
12689ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
12699ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
12709ea459e1SAl Viro 		return;
12719ea459e1SAl Viro 	}
12729ea459e1SAl Viro 	cleanup_mnt(mnt);
1273b3e19d92SNick Piggin }
1274b3e19d92SNick Piggin 
1275b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1276b3e19d92SNick Piggin {
1277b3e19d92SNick Piggin 	if (mnt) {
1278863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1279b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1280863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1281863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1282863d684fSAl Viro 		mntput_no_expire(m);
1283b3e19d92SNick Piggin 	}
1284b3e19d92SNick Piggin }
1285b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1286b3e19d92SNick Piggin 
1287b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1288b3e19d92SNick Piggin {
1289b3e19d92SNick Piggin 	if (mnt)
129083adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1291b3e19d92SNick Piggin 	return mnt;
1292b3e19d92SNick Piggin }
1293b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1294b3e19d92SNick Piggin 
12951f287bc4SRandy Dunlap /**
12961f287bc4SRandy Dunlap  * path_is_mountpoint() - Check if path is a mount in the current namespace.
12971f287bc4SRandy Dunlap  * @path: path to check
1298c6609c0aSIan Kent  *
1299c6609c0aSIan Kent  *  d_mountpoint() can only be used reliably to establish if a dentry is
1300c6609c0aSIan Kent  *  not mounted in any namespace and that common case is handled inline.
1301c6609c0aSIan Kent  *  d_mountpoint() isn't aware of the possibility there may be multiple
1302c6609c0aSIan Kent  *  mounts using a given dentry in a different namespace. This function
1303c6609c0aSIan Kent  *  checks if the passed in path is a mountpoint rather than the dentry
1304c6609c0aSIan Kent  *  alone.
1305c6609c0aSIan Kent  */
1306c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path)
1307c6609c0aSIan Kent {
1308c6609c0aSIan Kent 	unsigned seq;
1309c6609c0aSIan Kent 	bool res;
1310c6609c0aSIan Kent 
1311c6609c0aSIan Kent 	if (!d_mountpoint(path->dentry))
1312c6609c0aSIan Kent 		return false;
1313c6609c0aSIan Kent 
1314c6609c0aSIan Kent 	rcu_read_lock();
1315c6609c0aSIan Kent 	do {
1316c6609c0aSIan Kent 		seq = read_seqbegin(&mount_lock);
1317c6609c0aSIan Kent 		res = __path_is_mountpoint(path);
1318c6609c0aSIan Kent 	} while (read_seqretry(&mount_lock, seq));
1319c6609c0aSIan Kent 	rcu_read_unlock();
1320c6609c0aSIan Kent 
1321c6609c0aSIan Kent 	return res;
1322c6609c0aSIan Kent }
1323c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint);
1324c6609c0aSIan Kent 
1325ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path)
13267b7b1aceSAl Viro {
13273064c356SAl Viro 	struct mount *p;
13283064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
13293064c356SAl Viro 	if (IS_ERR(p))
13303064c356SAl Viro 		return ERR_CAST(p);
13313064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
13323064c356SAl Viro 	return &p->mnt;
13337b7b1aceSAl Viro }
13341da177e4SLinus Torvalds 
1335a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
13369f6c61f9SMiklos Szeredi static struct mount *mnt_list_next(struct mnt_namespace *ns,
13379f6c61f9SMiklos Szeredi 				   struct list_head *p)
13389f6c61f9SMiklos Szeredi {
13399f6c61f9SMiklos Szeredi 	struct mount *mnt, *ret = NULL;
13409f6c61f9SMiklos Szeredi 
13419f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
13429f6c61f9SMiklos Szeredi 	list_for_each_continue(p, &ns->list) {
13439f6c61f9SMiklos Szeredi 		mnt = list_entry(p, typeof(*mnt), mnt_list);
13449f6c61f9SMiklos Szeredi 		if (!mnt_is_cursor(mnt)) {
13459f6c61f9SMiklos Szeredi 			ret = mnt;
13469f6c61f9SMiklos Szeredi 			break;
13479f6c61f9SMiklos Szeredi 		}
13489f6c61f9SMiklos Szeredi 	}
13499f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
13509f6c61f9SMiklos Szeredi 
13519f6c61f9SMiklos Szeredi 	return ret;
13529f6c61f9SMiklos Szeredi }
13539f6c61f9SMiklos Szeredi 
13540226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
13551da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
13561da177e4SLinus Torvalds {
1357ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
13589f6c61f9SMiklos Szeredi 	struct list_head *prev;
13591da177e4SLinus Torvalds 
1360390c6843SRam Pai 	down_read(&namespace_sem);
13619f6c61f9SMiklos Szeredi 	if (!*pos) {
13629f6c61f9SMiklos Szeredi 		prev = &p->ns->list;
13639f6c61f9SMiklos Szeredi 	} else {
13649f6c61f9SMiklos Szeredi 		prev = &p->cursor.mnt_list;
13659f6c61f9SMiklos Szeredi 
13669f6c61f9SMiklos Szeredi 		/* Read after we'd reached the end? */
13679f6c61f9SMiklos Szeredi 		if (list_empty(prev))
13689f6c61f9SMiklos Szeredi 			return NULL;
1369c7999c36SAl Viro 	}
1370c7999c36SAl Viro 
13719f6c61f9SMiklos Szeredi 	return mnt_list_next(p->ns, prev);
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
13751da177e4SLinus Torvalds {
1376ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
13779f6c61f9SMiklos Szeredi 	struct mount *mnt = v;
1378b0765fb8SPavel Emelianov 
13799f6c61f9SMiklos Szeredi 	++*pos;
13809f6c61f9SMiklos Szeredi 	return mnt_list_next(p->ns, &mnt->mnt_list);
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
13841da177e4SLinus Torvalds {
13859f6c61f9SMiklos Szeredi 	struct proc_mounts *p = m->private;
13869f6c61f9SMiklos Szeredi 	struct mount *mnt = v;
13879f6c61f9SMiklos Szeredi 
13889f6c61f9SMiklos Szeredi 	lock_ns_list(p->ns);
13899f6c61f9SMiklos Szeredi 	if (mnt)
13909f6c61f9SMiklos Szeredi 		list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list);
13919f6c61f9SMiklos Szeredi 	else
13929f6c61f9SMiklos Szeredi 		list_del_init(&p->cursor.mnt_list);
13939f6c61f9SMiklos Szeredi 	unlock_ns_list(p->ns);
1394390c6843SRam Pai 	up_read(&namespace_sem);
13951da177e4SLinus Torvalds }
13961da177e4SLinus Torvalds 
13970226f492SAl Viro static int m_show(struct seq_file *m, void *v)
13989f5596afSAl Viro {
1399ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
14009f6c61f9SMiklos Szeredi 	struct mount *r = v;
14010226f492SAl Viro 	return p->show(m, &r->mnt);
14021da177e4SLinus Torvalds }
14031da177e4SLinus Torvalds 
1404a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
14051da177e4SLinus Torvalds 	.start	= m_start,
14061da177e4SLinus Torvalds 	.next	= m_next,
14071da177e4SLinus Torvalds 	.stop	= m_stop,
14080226f492SAl Viro 	.show	= m_show,
1409b4629fe2SChuck Lever };
14109f6c61f9SMiklos Szeredi 
14119f6c61f9SMiklos Szeredi void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor)
14129f6c61f9SMiklos Szeredi {
14139f6c61f9SMiklos Szeredi 	down_read(&namespace_sem);
14149f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
14159f6c61f9SMiklos Szeredi 	list_del(&cursor->mnt_list);
14169f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
14179f6c61f9SMiklos Szeredi 	up_read(&namespace_sem);
14189f6c61f9SMiklos Szeredi }
1419a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1420b4629fe2SChuck Lever 
14211da177e4SLinus Torvalds /**
14221da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
14231f287bc4SRandy Dunlap  * @m: root of mount tree
14241da177e4SLinus Torvalds  *
14251da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
14261da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
14271da177e4SLinus Torvalds  * busy.
14281da177e4SLinus Torvalds  */
1429909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
14301da177e4SLinus Torvalds {
1431909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
143236341f64SRam Pai 	int actual_refs = 0;
143336341f64SRam Pai 	int minimum_refs = 0;
1434315fc83eSAl Viro 	struct mount *p;
1435909b0a88SAl Viro 	BUG_ON(!m);
14361da177e4SLinus Torvalds 
1437b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1438719ea2fbSAl Viro 	lock_mount_hash();
1439909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
144083adc753SAl Viro 		actual_refs += mnt_get_count(p);
14411da177e4SLinus Torvalds 		minimum_refs += 2;
14421da177e4SLinus Torvalds 	}
1443719ea2fbSAl Viro 	unlock_mount_hash();
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
14461da177e4SLinus Torvalds 		return 0;
1447e3474a8eSIan Kent 
1448e3474a8eSIan Kent 	return 1;
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds /**
14541da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
14551da177e4SLinus Torvalds  * @mnt: root of mount
14561da177e4SLinus Torvalds  *
14571da177e4SLinus Torvalds  * This is called to check if a mount point has any
14581da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
14591da177e4SLinus Torvalds  * mount has sub mounts this will return busy
14601da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
14611da177e4SLinus Torvalds  *
14621da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
14631da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
14641da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
14651da177e4SLinus Torvalds  */
14661da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
14671da177e4SLinus Torvalds {
1468e3474a8eSIan Kent 	int ret = 1;
14698ad08d8aSAl Viro 	down_read(&namespace_sem);
1470719ea2fbSAl Viro 	lock_mount_hash();
14711ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1472e3474a8eSIan Kent 		ret = 0;
1473719ea2fbSAl Viro 	unlock_mount_hash();
14748ad08d8aSAl Viro 	up_read(&namespace_sem);
1475a05964f3SRam Pai 	return ret;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
14791da177e4SLinus Torvalds 
148097216be0SAl Viro static void namespace_unlock(void)
14811da177e4SLinus Torvalds {
1482a3b3c562SEric W. Biederman 	struct hlist_head head;
148356cbb429SAl Viro 	struct hlist_node *p;
148456cbb429SAl Viro 	struct mount *m;
14854edbe133SAl Viro 	LIST_HEAD(list);
148697216be0SAl Viro 
1487a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
14884edbe133SAl Viro 	list_splice_init(&ex_mountpoints, &list);
1489a3b3c562SEric W. Biederman 
149097216be0SAl Viro 	up_write(&namespace_sem);
1491a3b3c562SEric W. Biederman 
14924edbe133SAl Viro 	shrink_dentry_list(&list);
14934edbe133SAl Viro 
1494a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
149597216be0SAl Viro 		return;
149697216be0SAl Viro 
149722cb7405SNeilBrown 	synchronize_rcu_expedited();
149848a066e7SAl Viro 
149956cbb429SAl Viro 	hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
150056cbb429SAl Viro 		hlist_del(&m->mnt_umount);
150156cbb429SAl Viro 		mntput(&m->mnt);
150256cbb429SAl Viro 	}
150370fbcdf4SRam Pai }
150470fbcdf4SRam Pai 
150597216be0SAl Viro static inline void namespace_lock(void)
1506e3197d83SAl Viro {
150797216be0SAl Viro 	down_write(&namespace_sem);
1508e3197d83SAl Viro }
1509e3197d83SAl Viro 
1510e819f152SEric W. Biederman enum umount_tree_flags {
1511e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1512e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1513e0c9c0afSEric W. Biederman 	UMOUNT_CONNECTED = 4,
1514e819f152SEric W. Biederman };
1515f2d0a123SEric W. Biederman 
1516f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1517f2d0a123SEric W. Biederman {
1518f2d0a123SEric W. Biederman 	/* Leaving mounts connected is only valid for lazy umounts */
1519f2d0a123SEric W. Biederman 	if (how & UMOUNT_SYNC)
1520f2d0a123SEric W. Biederman 		return true;
1521f2d0a123SEric W. Biederman 
1522f2d0a123SEric W. Biederman 	/* A mount without a parent has nothing to be connected to */
1523f2d0a123SEric W. Biederman 	if (!mnt_has_parent(mnt))
1524f2d0a123SEric W. Biederman 		return true;
1525f2d0a123SEric W. Biederman 
1526f2d0a123SEric W. Biederman 	/* Because the reference counting rules change when mounts are
1527f2d0a123SEric W. Biederman 	 * unmounted and connected, umounted mounts may not be
1528f2d0a123SEric W. Biederman 	 * connected to mounted mounts.
1529f2d0a123SEric W. Biederman 	 */
1530f2d0a123SEric W. Biederman 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1531f2d0a123SEric W. Biederman 		return true;
1532f2d0a123SEric W. Biederman 
1533f2d0a123SEric W. Biederman 	/* Has it been requested that the mount remain connected? */
1534f2d0a123SEric W. Biederman 	if (how & UMOUNT_CONNECTED)
1535f2d0a123SEric W. Biederman 		return false;
1536f2d0a123SEric W. Biederman 
1537f2d0a123SEric W. Biederman 	/* Is the mount locked such that it needs to remain connected? */
1538f2d0a123SEric W. Biederman 	if (IS_MNT_LOCKED(mnt))
1539f2d0a123SEric W. Biederman 		return false;
1540f2d0a123SEric W. Biederman 
1541f2d0a123SEric W. Biederman 	/* By default disconnect the mount */
1542f2d0a123SEric W. Biederman 	return true;
1543f2d0a123SEric W. Biederman }
1544f2d0a123SEric W. Biederman 
154599b7db7bSNick Piggin /*
154648a066e7SAl Viro  * mount_lock must be held
154799b7db7bSNick Piggin  * namespace_sem must be held for write
154899b7db7bSNick Piggin  */
1549e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
155070fbcdf4SRam Pai {
1551c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1552315fc83eSAl Viro 	struct mount *p;
155370fbcdf4SRam Pai 
15545d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
15555d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
15565d88457eSEric W. Biederman 
1557c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1558590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1559590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1560c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1561590ce4bcSEric W. Biederman 	}
1562c003b26fSEric W. Biederman 
1563411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1564c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1565c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
156638129a13SAl Viro 	}
156770fbcdf4SRam Pai 
1568c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1569e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
15707b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1571a05964f3SRam Pai 
1572c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1573d2921684SEric W. Biederman 		struct mnt_namespace *ns;
1574ce07d891SEric W. Biederman 		bool disconnect;
1575c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
15766776db3dSAl Viro 		list_del_init(&p->mnt_expire);
15771a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1578d2921684SEric W. Biederman 		ns = p->mnt_ns;
1579d2921684SEric W. Biederman 		if (ns) {
1580d2921684SEric W. Biederman 			ns->mounts--;
1581d2921684SEric W. Biederman 			__touch_mnt_namespace(ns);
1582d2921684SEric W. Biederman 		}
158363d37a84SAl Viro 		p->mnt_ns = NULL;
1584e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
158548a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
158687b95ce0SAl Viro 
1587f2d0a123SEric W. Biederman 		disconnect = disconnect_mount(p, how);
1588676da58dSAl Viro 		if (mnt_has_parent(p)) {
158981b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
1590ce07d891SEric W. Biederman 			if (!disconnect) {
1591ce07d891SEric W. Biederman 				/* Don't forget about p */
1592ce07d891SEric W. Biederman 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1593ce07d891SEric W. Biederman 			} else {
15946a46c573SEric W. Biederman 				umount_mnt(p);
15957c4b93d8SAl Viro 			}
1596ce07d891SEric W. Biederman 		}
15970f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
159819a1c409SAl Viro 		if (disconnect)
159919a1c409SAl Viro 			hlist_add_head(&p->mnt_umount, &unmounted);
160038129a13SAl Viro 	}
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds 
1603b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1604c35038beSAl Viro 
16058d0347f6SDavid Howells static int do_umount_root(struct super_block *sb)
16068d0347f6SDavid Howells {
16078d0347f6SDavid Howells 	int ret = 0;
16088d0347f6SDavid Howells 
16098d0347f6SDavid Howells 	down_write(&sb->s_umount);
16108d0347f6SDavid Howells 	if (!sb_rdonly(sb)) {
16118d0347f6SDavid Howells 		struct fs_context *fc;
16128d0347f6SDavid Howells 
16138d0347f6SDavid Howells 		fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
16148d0347f6SDavid Howells 						SB_RDONLY);
16158d0347f6SDavid Howells 		if (IS_ERR(fc)) {
16168d0347f6SDavid Howells 			ret = PTR_ERR(fc);
16178d0347f6SDavid Howells 		} else {
16188d0347f6SDavid Howells 			ret = parse_monolithic_mount_data(fc, NULL);
16198d0347f6SDavid Howells 			if (!ret)
16208d0347f6SDavid Howells 				ret = reconfigure_super(fc);
16218d0347f6SDavid Howells 			put_fs_context(fc);
16228d0347f6SDavid Howells 		}
16238d0347f6SDavid Howells 	}
16248d0347f6SDavid Howells 	up_write(&sb->s_umount);
16258d0347f6SDavid Howells 	return ret;
16268d0347f6SDavid Howells }
16278d0347f6SDavid Howells 
16281ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
16291da177e4SLinus Torvalds {
16301ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
16311da177e4SLinus Torvalds 	int retval;
16321da177e4SLinus Torvalds 
16331ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
16341da177e4SLinus Torvalds 	if (retval)
16351da177e4SLinus Torvalds 		return retval;
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	/*
16381da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
16391da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
16401da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
16411da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
16421da177e4SLinus Torvalds 	 */
16431da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
16441ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
16451da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
16461da177e4SLinus Torvalds 			return -EINVAL;
16471da177e4SLinus Torvalds 
1648b3e19d92SNick Piggin 		/*
1649b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1650b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1651b3e19d92SNick Piggin 		 */
1652719ea2fbSAl Viro 		lock_mount_hash();
165383adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1654719ea2fbSAl Viro 			unlock_mount_hash();
16551da177e4SLinus Torvalds 			return -EBUSY;
1656b3e19d92SNick Piggin 		}
1657719ea2fbSAl Viro 		unlock_mount_hash();
16581da177e4SLinus Torvalds 
1659863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
16601da177e4SLinus Torvalds 			return -EAGAIN;
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	/*
16641da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
16651da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
16661da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
16671da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
16681da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
16691da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
16701da177e4SLinus Torvalds 	 * about for the moment.
16711da177e4SLinus Torvalds 	 */
16721da177e4SLinus Torvalds 
167342faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
167442faad99SAl Viro 		sb->s_op->umount_begin(sb);
167542faad99SAl Viro 	}
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	/*
16781da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
16791da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
16801da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
16811da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
16821da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
16831da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
16841da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
16851da177e4SLinus Torvalds 	 */
16861ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
16871da177e4SLinus Torvalds 		/*
16881da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
16891da177e4SLinus Torvalds 		 * we just try to remount it readonly.
16901da177e4SLinus Torvalds 		 */
1691bc6155d1SEric W. Biederman 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1692a1480dccSAndy Lutomirski 			return -EPERM;
16938d0347f6SDavid Howells 		return do_umount_root(sb);
16941da177e4SLinus Torvalds 	}
16951da177e4SLinus Torvalds 
169697216be0SAl Viro 	namespace_lock();
1697719ea2fbSAl Viro 	lock_mount_hash();
16981da177e4SLinus Torvalds 
169925d202edSEric W. Biederman 	/* Recheck MNT_LOCKED with the locks held */
170025d202edSEric W. Biederman 	retval = -EINVAL;
170125d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
170225d202edSEric W. Biederman 		goto out;
170325d202edSEric W. Biederman 
170425d202edSEric W. Biederman 	event++;
170548a066e7SAl Viro 	if (flags & MNT_DETACH) {
170648a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1707e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
170848a066e7SAl Viro 		retval = 0;
170948a066e7SAl Viro 	} else {
1710b54b9be7SAl Viro 		shrink_submounts(mnt);
17111da177e4SLinus Torvalds 		retval = -EBUSY;
171248a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
17131a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1714e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
17151da177e4SLinus Torvalds 			retval = 0;
17161da177e4SLinus Torvalds 		}
171748a066e7SAl Viro 	}
171825d202edSEric W. Biederman out:
1719719ea2fbSAl Viro 	unlock_mount_hash();
1720e3197d83SAl Viro 	namespace_unlock();
17211da177e4SLinus Torvalds 	return retval;
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds /*
172580b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
172680b5dce8SEric W. Biederman  *
172780b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
172880b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
172980b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
173080b5dce8SEric W. Biederman  * leaking them.
173180b5dce8SEric W. Biederman  *
173280b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
173380b5dce8SEric W. Biederman  */
173480b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
173580b5dce8SEric W. Biederman {
173680b5dce8SEric W. Biederman 	struct mountpoint *mp;
173780b5dce8SEric W. Biederman 	struct mount *mnt;
173880b5dce8SEric W. Biederman 
173980b5dce8SEric W. Biederman 	namespace_lock();
17403895dbf8SEric W. Biederman 	lock_mount_hash();
174180b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
1742adc9b5c0SAl Viro 	if (!mp)
174380b5dce8SEric W. Biederman 		goto out_unlock;
174480b5dce8SEric W. Biederman 
1745e06b933eSAndrey Ulanov 	event++;
174680b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
174780b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1748ce07d891SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1749fe78fcc8SEric W. Biederman 			umount_mnt(mnt);
175056cbb429SAl Viro 			hlist_add_head(&mnt->mnt_umount, &unmounted);
1751ce07d891SEric W. Biederman 		}
1752e0c9c0afSEric W. Biederman 		else umount_tree(mnt, UMOUNT_CONNECTED);
175380b5dce8SEric W. Biederman 	}
175480b5dce8SEric W. Biederman 	put_mountpoint(mp);
175580b5dce8SEric W. Biederman out_unlock:
17563895dbf8SEric W. Biederman 	unlock_mount_hash();
175780b5dce8SEric W. Biederman 	namespace_unlock();
175880b5dce8SEric W. Biederman }
175980b5dce8SEric W. Biederman 
176080b5dce8SEric W. Biederman /*
17619b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
17629b40bc90SAl Viro  */
1763a5f85d78SAl Viro bool may_mount(void)
17649b40bc90SAl Viro {
17659b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
17669b40bc90SAl Viro }
17679b40bc90SAl Viro 
1768f7e33bdbSJeff Layton static void warn_mandlock(void)
17699e8925b6SJeff Layton {
1770f7e33bdbSJeff Layton 	pr_warn_once("=======================================================\n"
1771f7e33bdbSJeff Layton 		     "WARNING: The mand mount option has been deprecated and\n"
1772f7e33bdbSJeff Layton 		     "         and is ignored by this kernel. Remove the mand\n"
1773f7e33bdbSJeff Layton 		     "         option from the mount to silence this warning.\n"
1774f7e33bdbSJeff Layton 		     "=======================================================\n");
17759e8925b6SJeff Layton }
17769e8925b6SJeff Layton 
177725ccd24fSChristoph Hellwig static int can_umount(const struct path *path, int flags)
17781da177e4SLinus Torvalds {
177925ccd24fSChristoph Hellwig 	struct mount *mnt = real_mount(path->mnt);
17801da177e4SLinus Torvalds 
17819b40bc90SAl Viro 	if (!may_mount())
17829b40bc90SAl Viro 		return -EPERM;
178341525f56SChristoph Hellwig 	if (path->dentry != path->mnt->mnt_root)
178425ccd24fSChristoph Hellwig 		return -EINVAL;
1785143c8c91SAl Viro 	if (!check_mnt(mnt))
178625ccd24fSChristoph Hellwig 		return -EINVAL;
178725d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
178825ccd24fSChristoph Hellwig 		return -EINVAL;
1789b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
179025ccd24fSChristoph Hellwig 		return -EPERM;
179125ccd24fSChristoph Hellwig 	return 0;
179225ccd24fSChristoph Hellwig }
17931da177e4SLinus Torvalds 
1794a0a6df9aSAl Viro // caller is responsible for flags being sane
179525ccd24fSChristoph Hellwig int path_umount(struct path *path, int flags)
179625ccd24fSChristoph Hellwig {
179725ccd24fSChristoph Hellwig 	struct mount *mnt = real_mount(path->mnt);
179825ccd24fSChristoph Hellwig 	int ret;
179925ccd24fSChristoph Hellwig 
180025ccd24fSChristoph Hellwig 	ret = can_umount(path, flags);
180125ccd24fSChristoph Hellwig 	if (!ret)
180225ccd24fSChristoph Hellwig 		ret = do_umount(mnt, flags);
180325ccd24fSChristoph Hellwig 
1804429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
180541525f56SChristoph Hellwig 	dput(path->dentry);
1806900148dcSAl Viro 	mntput_no_expire(mnt);
180725ccd24fSChristoph Hellwig 	return ret;
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds 
181009267defSChristoph Hellwig static int ksys_umount(char __user *name, int flags)
181141525f56SChristoph Hellwig {
181241525f56SChristoph Hellwig 	int lookup_flags = LOOKUP_MOUNTPOINT;
181341525f56SChristoph Hellwig 	struct path path;
181441525f56SChristoph Hellwig 	int ret;
181541525f56SChristoph Hellwig 
1816a0a6df9aSAl Viro 	// basic validity checks done first
1817a0a6df9aSAl Viro 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1818a0a6df9aSAl Viro 		return -EINVAL;
1819a0a6df9aSAl Viro 
182041525f56SChristoph Hellwig 	if (!(flags & UMOUNT_NOFOLLOW))
182141525f56SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
182241525f56SChristoph Hellwig 	ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
182341525f56SChristoph Hellwig 	if (ret)
182441525f56SChristoph Hellwig 		return ret;
182541525f56SChristoph Hellwig 	return path_umount(&path, flags);
182641525f56SChristoph Hellwig }
182741525f56SChristoph Hellwig 
18283a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
18293a18ef5cSDominik Brodowski {
18303a18ef5cSDominik Brodowski 	return ksys_umount(name, flags);
18313a18ef5cSDominik Brodowski }
18323a18ef5cSDominik Brodowski 
18331da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds /*
18361da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
18371da177e4SLinus Torvalds  */
1838bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
18391da177e4SLinus Torvalds {
18403a18ef5cSDominik Brodowski 	return ksys_umount(name, 0);
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds #endif
18441da177e4SLinus Torvalds 
18454ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
18468823c079SEric W. Biederman {
18474ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1848e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1849e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
18504ce5d2b1SEric W. Biederman }
18514ce5d2b1SEric W. Biederman 
1852213921f9SEric Biggers static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
185358be2825SAl Viro {
185458be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
185558be2825SAl Viro }
185658be2825SAl Viro 
1857303cc571SChristian Brauner struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
1858303cc571SChristian Brauner {
1859303cc571SChristian Brauner 	return &mnt->ns;
1860303cc571SChristian Brauner }
1861303cc571SChristian Brauner 
18624ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
18634ce5d2b1SEric W. Biederman {
18644ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
18654ce5d2b1SEric W. Biederman 	 * mount namespace loop?
18664ce5d2b1SEric W. Biederman 	 */
18674ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
18684ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
18694ce5d2b1SEric W. Biederman 		return false;
18704ce5d2b1SEric W. Biederman 
1871f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
18728823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
18738823c079SEric W. Biederman }
18748823c079SEric W. Biederman 
187587129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
187636341f64SRam Pai 					int flag)
18771da177e4SLinus Torvalds {
187884d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
18791da177e4SLinus Torvalds 
18804ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
18814ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
18824ce5d2b1SEric W. Biederman 
18834ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1884be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
18859676f0c6SRam Pai 
188636341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1887be34d1a3SDavid Howells 	if (IS_ERR(q))
1888be34d1a3SDavid Howells 		return q;
1889be34d1a3SDavid Howells 
1890a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds 	p = mnt;
18936b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1894315fc83eSAl Viro 		struct mount *s;
18957ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
18961da177e4SLinus Torvalds 			continue;
18971da177e4SLinus Torvalds 
1898909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
18994ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
19004ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
1901df7342b2SEric W. Biederman 				if (s->mnt.mnt_flags & MNT_LOCKED) {
1902df7342b2SEric W. Biederman 					/* Both unbindable and locked. */
1903df7342b2SEric W. Biederman 					q = ERR_PTR(-EPERM);
1904df7342b2SEric W. Biederman 					goto out;
1905df7342b2SEric W. Biederman 				} else {
19064ce5d2b1SEric W. Biederman 					s = skip_mnt_tree(s);
19074ce5d2b1SEric W. Biederman 					continue;
19084ce5d2b1SEric W. Biederman 				}
1909df7342b2SEric W. Biederman 			}
19104ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
19114ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
19129676f0c6SRam Pai 				s = skip_mnt_tree(s);
19139676f0c6SRam Pai 				continue;
19149676f0c6SRam Pai 			}
19150714a533SAl Viro 			while (p != s->mnt_parent) {
19160714a533SAl Viro 				p = p->mnt_parent;
19170714a533SAl Viro 				q = q->mnt_parent;
19181da177e4SLinus Torvalds 			}
191987129cc0SAl Viro 			p = s;
192084d17192SAl Viro 			parent = q;
192187129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1922be34d1a3SDavid Howells 			if (IS_ERR(q))
1923be34d1a3SDavid Howells 				goto out;
1924719ea2fbSAl Viro 			lock_mount_hash();
19251a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
19261064f874SEric W. Biederman 			attach_mnt(q, parent, p->mnt_mp);
1927719ea2fbSAl Viro 			unlock_mount_hash();
19281da177e4SLinus Torvalds 		}
19291da177e4SLinus Torvalds 	}
19301da177e4SLinus Torvalds 	return res;
1931be34d1a3SDavid Howells out:
19321da177e4SLinus Torvalds 	if (res) {
1933719ea2fbSAl Viro 		lock_mount_hash();
1934e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
1935719ea2fbSAl Viro 		unlock_mount_hash();
19361da177e4SLinus Torvalds 	}
1937be34d1a3SDavid Howells 	return q;
19381da177e4SLinus Torvalds }
19391da177e4SLinus Torvalds 
1940be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1941be34d1a3SDavid Howells 
1942ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path)
19438aec0809SAl Viro {
1944cb338d06SAl Viro 	struct mount *tree;
194597216be0SAl Viro 	namespace_lock();
1946cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
1947cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
1948cd4a4017SEric W. Biederman 	else
194987129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
195087129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
1951328e6d90SAl Viro 	namespace_unlock();
1952be34d1a3SDavid Howells 	if (IS_ERR(tree))
195352e220d3SDan Carpenter 		return ERR_CAST(tree);
1954be34d1a3SDavid Howells 	return &tree->mnt;
19558aec0809SAl Viro }
19568aec0809SAl Viro 
1957a07b2000SAl Viro static void free_mnt_ns(struct mnt_namespace *);
1958a07b2000SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
1959a07b2000SAl Viro 
1960a07b2000SAl Viro void dissolve_on_fput(struct vfsmount *mnt)
1961a07b2000SAl Viro {
1962a07b2000SAl Viro 	struct mnt_namespace *ns;
1963a07b2000SAl Viro 	namespace_lock();
1964a07b2000SAl Viro 	lock_mount_hash();
1965a07b2000SAl Viro 	ns = real_mount(mnt)->mnt_ns;
196644dfd84aSDavid Howells 	if (ns) {
196744dfd84aSDavid Howells 		if (is_anon_ns(ns))
1968a07b2000SAl Viro 			umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
196944dfd84aSDavid Howells 		else
197044dfd84aSDavid Howells 			ns = NULL;
197144dfd84aSDavid Howells 	}
1972a07b2000SAl Viro 	unlock_mount_hash();
1973a07b2000SAl Viro 	namespace_unlock();
197444dfd84aSDavid Howells 	if (ns)
1975a07b2000SAl Viro 		free_mnt_ns(ns);
1976a07b2000SAl Viro }
1977a07b2000SAl Viro 
19788aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
19798aec0809SAl Viro {
198097216be0SAl Viro 	namespace_lock();
1981719ea2fbSAl Viro 	lock_mount_hash();
19829c8e0a1bSEric W. Biederman 	umount_tree(real_mount(mnt), 0);
1983719ea2fbSAl Viro 	unlock_mount_hash();
19843ab6abeeSAl Viro 	namespace_unlock();
19858aec0809SAl Viro }
19868aec0809SAl Viro 
1987427215d8SMiklos Szeredi static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
1988427215d8SMiklos Szeredi {
1989427215d8SMiklos Szeredi 	struct mount *child;
1990427215d8SMiklos Szeredi 
1991427215d8SMiklos Szeredi 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
1992427215d8SMiklos Szeredi 		if (!is_subdir(child->mnt_mountpoint, dentry))
1993427215d8SMiklos Szeredi 			continue;
1994427215d8SMiklos Szeredi 
1995427215d8SMiklos Szeredi 		if (child->mnt.mnt_flags & MNT_LOCKED)
1996427215d8SMiklos Szeredi 			return true;
1997427215d8SMiklos Szeredi 	}
1998427215d8SMiklos Szeredi 	return false;
1999427215d8SMiklos Szeredi }
2000427215d8SMiklos Szeredi 
2001c771d683SMiklos Szeredi /**
2002c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
20031f287bc4SRandy Dunlap  * @path: path to clone
2004c771d683SMiklos Szeredi  *
20051f287bc4SRandy Dunlap  * This creates a new vfsmount, which will be the clone of @path.  The new mount
20061f287bc4SRandy Dunlap  * will not be attached anywhere in the namespace and will be private (i.e.
20071f287bc4SRandy Dunlap  * changes to the originating mount won't be propagated into this).
2008c771d683SMiklos Szeredi  *
2009c771d683SMiklos Szeredi  * Release with mntput().
2010c771d683SMiklos Szeredi  */
2011ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path)
2012c771d683SMiklos Szeredi {
2013c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
2014c771d683SMiklos Szeredi 	struct mount *new_mnt;
2015c771d683SMiklos Szeredi 
2016427215d8SMiklos Szeredi 	down_read(&namespace_sem);
2017c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
2018427215d8SMiklos Szeredi 		goto invalid;
2019427215d8SMiklos Szeredi 
2020427215d8SMiklos Szeredi 	if (!check_mnt(old_mnt))
2021427215d8SMiklos Szeredi 		goto invalid;
2022427215d8SMiklos Szeredi 
2023427215d8SMiklos Szeredi 	if (has_locked_children(old_mnt, path->dentry))
2024427215d8SMiklos Szeredi 		goto invalid;
2025c771d683SMiklos Szeredi 
2026c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
2027427215d8SMiklos Szeredi 	up_read(&namespace_sem);
2028427215d8SMiklos Szeredi 
2029c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
2030c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
2031c771d683SMiklos Szeredi 
2032df820f8dSMiklos Szeredi 	/* Longterm mount to be removed by kern_unmount*() */
2033df820f8dSMiklos Szeredi 	new_mnt->mnt_ns = MNT_NS_INTERNAL;
2034df820f8dSMiklos Szeredi 
2035c771d683SMiklos Szeredi 	return &new_mnt->mnt;
2036427215d8SMiklos Szeredi 
2037427215d8SMiklos Szeredi invalid:
2038427215d8SMiklos Szeredi 	up_read(&namespace_sem);
2039427215d8SMiklos Szeredi 	return ERR_PTR(-EINVAL);
2040c771d683SMiklos Szeredi }
2041c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
2042c771d683SMiklos Szeredi 
20431f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
20441f707137SAl Viro 		   struct vfsmount *root)
20451f707137SAl Viro {
20461a4eeaf2SAl Viro 	struct mount *mnt;
20471f707137SAl Viro 	int res = f(root, arg);
20481f707137SAl Viro 	if (res)
20491f707137SAl Viro 		return res;
20501a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
20511a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
20521f707137SAl Viro 		if (res)
20531f707137SAl Viro 			return res;
20541f707137SAl Viro 	}
20551f707137SAl Viro 	return 0;
20561f707137SAl Viro }
20571f707137SAl Viro 
20583bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt)
20593bd045ccSAl Viro {
20603bd045ccSAl Viro 	struct mount *p;
20613bd045ccSAl Viro 
20623bd045ccSAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
20633bd045ccSAl Viro 		int flags = p->mnt.mnt_flags;
20643bd045ccSAl Viro 		/* Don't allow unprivileged users to change mount flags */
20653bd045ccSAl Viro 		flags |= MNT_LOCK_ATIME;
20663bd045ccSAl Viro 
20673bd045ccSAl Viro 		if (flags & MNT_READONLY)
20683bd045ccSAl Viro 			flags |= MNT_LOCK_READONLY;
20693bd045ccSAl Viro 
20703bd045ccSAl Viro 		if (flags & MNT_NODEV)
20713bd045ccSAl Viro 			flags |= MNT_LOCK_NODEV;
20723bd045ccSAl Viro 
20733bd045ccSAl Viro 		if (flags & MNT_NOSUID)
20743bd045ccSAl Viro 			flags |= MNT_LOCK_NOSUID;
20753bd045ccSAl Viro 
20763bd045ccSAl Viro 		if (flags & MNT_NOEXEC)
20773bd045ccSAl Viro 			flags |= MNT_LOCK_NOEXEC;
20783bd045ccSAl Viro 		/* Don't allow unprivileged users to reveal what is under a mount */
20793bd045ccSAl Viro 		if (list_empty(&p->mnt_expire))
20803bd045ccSAl Viro 			flags |= MNT_LOCKED;
20813bd045ccSAl Viro 		p->mnt.mnt_flags = flags;
20823bd045ccSAl Viro 	}
20833bd045ccSAl Viro }
20843bd045ccSAl Viro 
20854b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
2086719f5d7fSMiklos Szeredi {
2087315fc83eSAl Viro 	struct mount *p;
2088719f5d7fSMiklos Szeredi 
2089909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
2090fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
20914b8b21f4SAl Viro 			mnt_release_group_id(p);
2092719f5d7fSMiklos Szeredi 	}
2093719f5d7fSMiklos Szeredi }
2094719f5d7fSMiklos Szeredi 
20954b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
2096719f5d7fSMiklos Szeredi {
2097315fc83eSAl Viro 	struct mount *p;
2098719f5d7fSMiklos Szeredi 
2099909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
2100fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
21014b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
2102719f5d7fSMiklos Szeredi 			if (err) {
21034b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
2104719f5d7fSMiklos Szeredi 				return err;
2105719f5d7fSMiklos Szeredi 			}
2106719f5d7fSMiklos Szeredi 		}
2107719f5d7fSMiklos Szeredi 	}
2108719f5d7fSMiklos Szeredi 
2109719f5d7fSMiklos Szeredi 	return 0;
2110719f5d7fSMiklos Szeredi }
2111719f5d7fSMiklos Szeredi 
2112d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2113d2921684SEric W. Biederman {
2114d2921684SEric W. Biederman 	unsigned int max = READ_ONCE(sysctl_mount_max);
2115124f75f8SAl Viro 	unsigned int mounts = 0;
2116d2921684SEric W. Biederman 	struct mount *p;
2117d2921684SEric W. Biederman 
2118124f75f8SAl Viro 	if (ns->mounts >= max)
2119124f75f8SAl Viro 		return -ENOSPC;
2120124f75f8SAl Viro 	max -= ns->mounts;
2121124f75f8SAl Viro 	if (ns->pending_mounts >= max)
2122124f75f8SAl Viro 		return -ENOSPC;
2123124f75f8SAl Viro 	max -= ns->pending_mounts;
2124124f75f8SAl Viro 
2125d2921684SEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt))
2126d2921684SEric W. Biederman 		mounts++;
2127d2921684SEric W. Biederman 
2128124f75f8SAl Viro 	if (mounts > max)
2129d2921684SEric W. Biederman 		return -ENOSPC;
2130d2921684SEric W. Biederman 
2131124f75f8SAl Viro 	ns->pending_mounts += mounts;
2132d2921684SEric W. Biederman 	return 0;
2133d2921684SEric W. Biederman }
2134d2921684SEric W. Biederman 
2135b90fa9aeSRam Pai /*
2136b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
2137b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
213821444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
213921444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
214021444403SRam Pai  *  		   (done when source_mnt is moved)
2141b90fa9aeSRam Pai  *
2142b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
2143b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
21449676f0c6SRam Pai  * ---------------------------------------------------------------------------
2145b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
21469676f0c6SRam Pai  * |**************************************************************************
21479676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
21489676f0c6SRam Pai  * | dest     |               |                |                |            |
21499676f0c6SRam Pai  * |   |      |               |                |                |            |
21509676f0c6SRam Pai  * |   v      |               |                |                |            |
21519676f0c6SRam Pai  * |**************************************************************************
21529676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
21535afe0022SRam Pai  * |          |               |                |                |            |
21549676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
21559676f0c6SRam Pai  * ***************************************************************************
2156b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
2157b90fa9aeSRam Pai  * destination mount.
2158b90fa9aeSRam Pai  *
2159b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
2160b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
2161b90fa9aeSRam Pai  * 	 the peer group of the source mount.
2162b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
2163b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
2164b90fa9aeSRam Pai  *       mount.
21655afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
21665afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
21675afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
21685afe0022SRam Pai  *       is marked as 'shared and slave'.
21695afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
21705afe0022SRam Pai  * 	 source mount.
21715afe0022SRam Pai  *
21729676f0c6SRam Pai  * ---------------------------------------------------------------------------
217321444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
21749676f0c6SRam Pai  * |**************************************************************************
21759676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
21769676f0c6SRam Pai  * | dest     |               |                |                |            |
21779676f0c6SRam Pai  * |   |      |               |                |                |            |
21789676f0c6SRam Pai  * |   v      |               |                |                |            |
21799676f0c6SRam Pai  * |**************************************************************************
21809676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
21815afe0022SRam Pai  * |          |               |                |                |            |
21829676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
21839676f0c6SRam Pai  * ***************************************************************************
21845afe0022SRam Pai  *
21855afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
21865afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
218721444403SRam Pai  * (+*)  the mount is moved to the destination.
21885afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
21895afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
21905afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
21915afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
2192b90fa9aeSRam Pai  *
2193b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
2194b90fa9aeSRam Pai  * applied to each mount in the tree.
2195b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
2196b90fa9aeSRam Pai  * in allocations.
2197b90fa9aeSRam Pai  */
21980fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
219984d17192SAl Viro 			struct mount *dest_mnt,
220084d17192SAl Viro 			struct mountpoint *dest_mp,
22012763d119SAl Viro 			bool moving)
2202b90fa9aeSRam Pai {
22033bd045ccSAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
220438129a13SAl Viro 	HLIST_HEAD(tree_list);
2205d2921684SEric W. Biederman 	struct mnt_namespace *ns = dest_mnt->mnt_ns;
22061064f874SEric W. Biederman 	struct mountpoint *smp;
2207315fc83eSAl Viro 	struct mount *child, *p;
220838129a13SAl Viro 	struct hlist_node *n;
2209719f5d7fSMiklos Szeredi 	int err;
2210b90fa9aeSRam Pai 
22111064f874SEric W. Biederman 	/* Preallocate a mountpoint in case the new mounts need
22121064f874SEric W. Biederman 	 * to be tucked under other mounts.
22131064f874SEric W. Biederman 	 */
22141064f874SEric W. Biederman 	smp = get_mountpoint(source_mnt->mnt.mnt_root);
22151064f874SEric W. Biederman 	if (IS_ERR(smp))
22161064f874SEric W. Biederman 		return PTR_ERR(smp);
22171064f874SEric W. Biederman 
2218d2921684SEric W. Biederman 	/* Is there space to add these mounts to the mount namespace? */
22192763d119SAl Viro 	if (!moving) {
2220d2921684SEric W. Biederman 		err = count_mounts(ns, source_mnt);
2221d2921684SEric W. Biederman 		if (err)
2222d2921684SEric W. Biederman 			goto out;
2223d2921684SEric W. Biederman 	}
2224d2921684SEric W. Biederman 
2225fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
22260fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
2227719f5d7fSMiklos Szeredi 		if (err)
2228719f5d7fSMiklos Szeredi 			goto out;
222984d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
2230f2ebb3a9SAl Viro 		lock_mount_hash();
2231719f5d7fSMiklos Szeredi 		if (err)
2232719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
2233909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
22340f0afb1dSAl Viro 			set_mnt_shared(p);
22350b1b901bSAl Viro 	} else {
22360b1b901bSAl Viro 		lock_mount_hash();
2237b90fa9aeSRam Pai 	}
22382763d119SAl Viro 	if (moving) {
22392763d119SAl Viro 		unhash_mnt(source_mnt);
224084d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
2241143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
224221444403SRam Pai 	} else {
224344dfd84aSDavid Howells 		if (source_mnt->mnt_ns) {
224444dfd84aSDavid Howells 			/* move from anon - the caller will destroy */
224544dfd84aSDavid Howells 			list_del_init(&source_mnt->mnt_ns->list);
224644dfd84aSDavid Howells 		}
224784d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
22481064f874SEric W. Biederman 		commit_tree(source_mnt);
224921444403SRam Pai 	}
2250b90fa9aeSRam Pai 
225138129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
22521d6a32acSAl Viro 		struct mount *q;
225338129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
22541064f874SEric W. Biederman 		q = __lookup_mnt(&child->mnt_parent->mnt,
22551d6a32acSAl Viro 				 child->mnt_mountpoint);
22561064f874SEric W. Biederman 		if (q)
22571064f874SEric W. Biederman 			mnt_change_mountpoint(child, smp, q);
22583bd045ccSAl Viro 		/* Notice when we are propagating across user namespaces */
22593bd045ccSAl Viro 		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
22603bd045ccSAl Viro 			lock_mnt_tree(child);
2261d728cf79SChristian Brauner 		child->mnt.mnt_flags &= ~MNT_LOCKED;
22621064f874SEric W. Biederman 		commit_tree(child);
2263b90fa9aeSRam Pai 	}
22641064f874SEric W. Biederman 	put_mountpoint(smp);
2265719ea2fbSAl Viro 	unlock_mount_hash();
226699b7db7bSNick Piggin 
2267b90fa9aeSRam Pai 	return 0;
2268719f5d7fSMiklos Szeredi 
2269719f5d7fSMiklos Szeredi  out_cleanup_ids:
2270f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
2271f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2272d2921684SEric W. Biederman 		child->mnt_parent->mnt_ns->pending_mounts = 0;
2273e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
2274f2ebb3a9SAl Viro 	}
2275f2ebb3a9SAl Viro 	unlock_mount_hash();
22760fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
2277719f5d7fSMiklos Szeredi  out:
2278d2921684SEric W. Biederman 	ns->pending_mounts = 0;
22791064f874SEric W. Biederman 
22801064f874SEric W. Biederman 	read_seqlock_excl(&mount_lock);
22811064f874SEric W. Biederman 	put_mountpoint(smp);
22821064f874SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
22831064f874SEric W. Biederman 
2284719f5d7fSMiklos Szeredi 	return err;
2285b90fa9aeSRam Pai }
2286b90fa9aeSRam Pai 
228784d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
2288b12cea91SAl Viro {
2289b12cea91SAl Viro 	struct vfsmount *mnt;
229084d17192SAl Viro 	struct dentry *dentry = path->dentry;
2291b12cea91SAl Viro retry:
22925955102cSAl Viro 	inode_lock(dentry->d_inode);
229384d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
22945955102cSAl Viro 		inode_unlock(dentry->d_inode);
229584d17192SAl Viro 		return ERR_PTR(-ENOENT);
2296b12cea91SAl Viro 	}
229797216be0SAl Viro 	namespace_lock();
2298b12cea91SAl Viro 	mnt = lookup_mnt(path);
229984d17192SAl Viro 	if (likely(!mnt)) {
23003895dbf8SEric W. Biederman 		struct mountpoint *mp = get_mountpoint(dentry);
230184d17192SAl Viro 		if (IS_ERR(mp)) {
230297216be0SAl Viro 			namespace_unlock();
23035955102cSAl Viro 			inode_unlock(dentry->d_inode);
230484d17192SAl Viro 			return mp;
230584d17192SAl Viro 		}
230684d17192SAl Viro 		return mp;
230784d17192SAl Viro 	}
230897216be0SAl Viro 	namespace_unlock();
23095955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
2310b12cea91SAl Viro 	path_put(path);
2311b12cea91SAl Viro 	path->mnt = mnt;
231284d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
2313b12cea91SAl Viro 	goto retry;
2314b12cea91SAl Viro }
2315b12cea91SAl Viro 
231684d17192SAl Viro static void unlock_mount(struct mountpoint *where)
2317b12cea91SAl Viro {
231884d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
23193895dbf8SEric W. Biederman 
23203895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
232184d17192SAl Viro 	put_mountpoint(where);
23223895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
23233895dbf8SEric W. Biederman 
2324328e6d90SAl Viro 	namespace_unlock();
23255955102cSAl Viro 	inode_unlock(dentry->d_inode);
2326b12cea91SAl Viro }
2327b12cea91SAl Viro 
232884d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
23291da177e4SLinus Torvalds {
2330e462ec50SDavid Howells 	if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
23311da177e4SLinus Torvalds 		return -EINVAL;
23321da177e4SLinus Torvalds 
2333e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
2334e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
23351da177e4SLinus Torvalds 		return -ENOTDIR;
23361da177e4SLinus Torvalds 
23372763d119SAl Viro 	return attach_recursive_mnt(mnt, p, mp, false);
23381da177e4SLinus Torvalds }
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds /*
23417a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
23427a2e8a8fSValerie Aurora  */
23437a2e8a8fSValerie Aurora 
2344e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags)
23457a2e8a8fSValerie Aurora {
2346e462ec50SDavid Howells 	int type = ms_flags & ~(MS_REC | MS_SILENT);
23477a2e8a8fSValerie Aurora 
23487a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
23497a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
23507a2e8a8fSValerie Aurora 		return 0;
23517a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
23527a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
23537a2e8a8fSValerie Aurora 		return 0;
23547a2e8a8fSValerie Aurora 	return type;
23557a2e8a8fSValerie Aurora }
23567a2e8a8fSValerie Aurora 
23577a2e8a8fSValerie Aurora /*
235807b20889SRam Pai  * recursively change the type of the mountpoint.
235907b20889SRam Pai  */
2360e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags)
236107b20889SRam Pai {
2362315fc83eSAl Viro 	struct mount *m;
23634b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
2364e462ec50SDavid Howells 	int recurse = ms_flags & MS_REC;
23657a2e8a8fSValerie Aurora 	int type;
2366719f5d7fSMiklos Szeredi 	int err = 0;
236707b20889SRam Pai 
23682d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
236907b20889SRam Pai 		return -EINVAL;
237007b20889SRam Pai 
2371e462ec50SDavid Howells 	type = flags_to_propagation_type(ms_flags);
23727a2e8a8fSValerie Aurora 	if (!type)
23737a2e8a8fSValerie Aurora 		return -EINVAL;
23747a2e8a8fSValerie Aurora 
237597216be0SAl Viro 	namespace_lock();
2376719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
2377719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
2378719f5d7fSMiklos Szeredi 		if (err)
2379719f5d7fSMiklos Szeredi 			goto out_unlock;
2380719f5d7fSMiklos Szeredi 	}
2381719f5d7fSMiklos Szeredi 
2382719ea2fbSAl Viro 	lock_mount_hash();
2383909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
23840f0afb1dSAl Viro 		change_mnt_propagation(m, type);
2385719ea2fbSAl Viro 	unlock_mount_hash();
2386719f5d7fSMiklos Szeredi 
2387719f5d7fSMiklos Szeredi  out_unlock:
238897216be0SAl Viro 	namespace_unlock();
2389719f5d7fSMiklos Szeredi 	return err;
239007b20889SRam Pai }
239107b20889SRam Pai 
2392a07b2000SAl Viro static struct mount *__do_loopback(struct path *old_path, int recurse)
2393a07b2000SAl Viro {
2394a07b2000SAl Viro 	struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2395a07b2000SAl Viro 
2396a07b2000SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2397a07b2000SAl Viro 		return mnt;
2398a07b2000SAl Viro 
2399a07b2000SAl Viro 	if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2400a07b2000SAl Viro 		return mnt;
2401a07b2000SAl Viro 
2402a07b2000SAl Viro 	if (!recurse && has_locked_children(old, old_path->dentry))
2403a07b2000SAl Viro 		return mnt;
2404a07b2000SAl Viro 
2405a07b2000SAl Viro 	if (recurse)
2406a07b2000SAl Viro 		mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2407a07b2000SAl Viro 	else
2408a07b2000SAl Viro 		mnt = clone_mnt(old, old_path->dentry, 0);
2409a07b2000SAl Viro 
2410a07b2000SAl Viro 	if (!IS_ERR(mnt))
2411a07b2000SAl Viro 		mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2412a07b2000SAl Viro 
2413a07b2000SAl Viro 	return mnt;
2414a07b2000SAl Viro }
2415a07b2000SAl Viro 
241607b20889SRam Pai /*
24171da177e4SLinus Torvalds  * do loopback mount.
24181da177e4SLinus Torvalds  */
2419808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
24202dafe1c4SEric Sandeen 				int recurse)
24211da177e4SLinus Torvalds {
24222d92ab3cSAl Viro 	struct path old_path;
2423a07b2000SAl Viro 	struct mount *mnt = NULL, *parent;
242484d17192SAl Viro 	struct mountpoint *mp;
242557eccb83SAl Viro 	int err;
24261da177e4SLinus Torvalds 	if (!old_name || !*old_name)
24271da177e4SLinus Torvalds 		return -EINVAL;
2428815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
24291da177e4SLinus Torvalds 	if (err)
24301da177e4SLinus Torvalds 		return err;
24311da177e4SLinus Torvalds 
24328823c079SEric W. Biederman 	err = -EINVAL;
24334ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
24348823c079SEric W. Biederman 		goto out;
24358823c079SEric W. Biederman 
243684d17192SAl Viro 	mp = lock_mount(path);
2437a07b2000SAl Viro 	if (IS_ERR(mp)) {
243884d17192SAl Viro 		err = PTR_ERR(mp);
24399676f0c6SRam Pai 		goto out;
2440a07b2000SAl Viro 	}
24419676f0c6SRam Pai 
244284d17192SAl Viro 	parent = real_mount(path->mnt);
2443e149ed2bSAl Viro 	if (!check_mnt(parent))
2444e149ed2bSAl Viro 		goto out2;
2445e149ed2bSAl Viro 
2446a07b2000SAl Viro 	mnt = __do_loopback(&old_path, recurse);
2447be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2448be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2449e9c5d8a5SAndrey Vagin 		goto out2;
2450be34d1a3SDavid Howells 	}
2451ccd48bc7SAl Viro 
245284d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
24531da177e4SLinus Torvalds 	if (err) {
2454719ea2fbSAl Viro 		lock_mount_hash();
2455e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2456719ea2fbSAl Viro 		unlock_mount_hash();
24575b83d2c5SRam Pai 	}
2458b12cea91SAl Viro out2:
245984d17192SAl Viro 	unlock_mount(mp);
2460ccd48bc7SAl Viro out:
24612d92ab3cSAl Viro 	path_put(&old_path);
24621da177e4SLinus Torvalds 	return err;
24631da177e4SLinus Torvalds }
24641da177e4SLinus Torvalds 
2465a07b2000SAl Viro static struct file *open_detached_copy(struct path *path, bool recursive)
2466a07b2000SAl Viro {
2467a07b2000SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2468a07b2000SAl Viro 	struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2469a07b2000SAl Viro 	struct mount *mnt, *p;
2470a07b2000SAl Viro 	struct file *file;
2471a07b2000SAl Viro 
2472a07b2000SAl Viro 	if (IS_ERR(ns))
2473a07b2000SAl Viro 		return ERR_CAST(ns);
2474a07b2000SAl Viro 
2475a07b2000SAl Viro 	namespace_lock();
2476a07b2000SAl Viro 	mnt = __do_loopback(path, recursive);
2477a07b2000SAl Viro 	if (IS_ERR(mnt)) {
2478a07b2000SAl Viro 		namespace_unlock();
2479a07b2000SAl Viro 		free_mnt_ns(ns);
2480a07b2000SAl Viro 		return ERR_CAST(mnt);
2481a07b2000SAl Viro 	}
2482a07b2000SAl Viro 
2483a07b2000SAl Viro 	lock_mount_hash();
2484a07b2000SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2485a07b2000SAl Viro 		p->mnt_ns = ns;
2486a07b2000SAl Viro 		ns->mounts++;
2487a07b2000SAl Viro 	}
2488a07b2000SAl Viro 	ns->root = mnt;
2489a07b2000SAl Viro 	list_add_tail(&ns->list, &mnt->mnt_list);
2490a07b2000SAl Viro 	mntget(&mnt->mnt);
2491a07b2000SAl Viro 	unlock_mount_hash();
2492a07b2000SAl Viro 	namespace_unlock();
2493a07b2000SAl Viro 
2494a07b2000SAl Viro 	mntput(path->mnt);
2495a07b2000SAl Viro 	path->mnt = &mnt->mnt;
2496a07b2000SAl Viro 	file = dentry_open(path, O_PATH, current_cred());
2497a07b2000SAl Viro 	if (IS_ERR(file))
2498a07b2000SAl Viro 		dissolve_on_fput(path->mnt);
2499a07b2000SAl Viro 	else
2500a07b2000SAl Viro 		file->f_mode |= FMODE_NEED_UNMOUNT;
2501a07b2000SAl Viro 	return file;
2502a07b2000SAl Viro }
2503a07b2000SAl Viro 
25042658ce09SBen Dooks SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
2505a07b2000SAl Viro {
2506a07b2000SAl Viro 	struct file *file;
2507a07b2000SAl Viro 	struct path path;
2508a07b2000SAl Viro 	int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2509a07b2000SAl Viro 	bool detached = flags & OPEN_TREE_CLONE;
2510a07b2000SAl Viro 	int error;
2511a07b2000SAl Viro 	int fd;
2512a07b2000SAl Viro 
2513a07b2000SAl Viro 	BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2514a07b2000SAl Viro 
2515a07b2000SAl Viro 	if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2516a07b2000SAl Viro 		      AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2517a07b2000SAl Viro 		      OPEN_TREE_CLOEXEC))
2518a07b2000SAl Viro 		return -EINVAL;
2519a07b2000SAl Viro 
2520a07b2000SAl Viro 	if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2521a07b2000SAl Viro 		return -EINVAL;
2522a07b2000SAl Viro 
2523a07b2000SAl Viro 	if (flags & AT_NO_AUTOMOUNT)
2524a07b2000SAl Viro 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
2525a07b2000SAl Viro 	if (flags & AT_SYMLINK_NOFOLLOW)
2526a07b2000SAl Viro 		lookup_flags &= ~LOOKUP_FOLLOW;
2527a07b2000SAl Viro 	if (flags & AT_EMPTY_PATH)
2528a07b2000SAl Viro 		lookup_flags |= LOOKUP_EMPTY;
2529a07b2000SAl Viro 
2530a07b2000SAl Viro 	if (detached && !may_mount())
2531a07b2000SAl Viro 		return -EPERM;
2532a07b2000SAl Viro 
2533a07b2000SAl Viro 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
2534a07b2000SAl Viro 	if (fd < 0)
2535a07b2000SAl Viro 		return fd;
2536a07b2000SAl Viro 
2537a07b2000SAl Viro 	error = user_path_at(dfd, filename, lookup_flags, &path);
2538a07b2000SAl Viro 	if (unlikely(error)) {
2539a07b2000SAl Viro 		file = ERR_PTR(error);
2540a07b2000SAl Viro 	} else {
2541a07b2000SAl Viro 		if (detached)
2542a07b2000SAl Viro 			file = open_detached_copy(&path, flags & AT_RECURSIVE);
2543a07b2000SAl Viro 		else
2544a07b2000SAl Viro 			file = dentry_open(&path, O_PATH, current_cred());
2545a07b2000SAl Viro 		path_put(&path);
2546a07b2000SAl Viro 	}
2547a07b2000SAl Viro 	if (IS_ERR(file)) {
2548a07b2000SAl Viro 		put_unused_fd(fd);
2549a07b2000SAl Viro 		return PTR_ERR(file);
2550a07b2000SAl Viro 	}
2551a07b2000SAl Viro 	fd_install(fd, file);
2552a07b2000SAl Viro 	return fd;
2553a07b2000SAl Viro }
2554a07b2000SAl Viro 
255543f5e655SDavid Howells /*
255643f5e655SDavid Howells  * Don't allow locked mount flags to be cleared.
255743f5e655SDavid Howells  *
255843f5e655SDavid Howells  * No locks need to be held here while testing the various MNT_LOCK
255943f5e655SDavid Howells  * flags because those flags can never be cleared once they are set.
256043f5e655SDavid Howells  */
256143f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
25622e4b7fcdSDave Hansen {
256343f5e655SDavid Howells 	unsigned int fl = mnt->mnt.mnt_flags;
25642e4b7fcdSDave Hansen 
256543f5e655SDavid Howells 	if ((fl & MNT_LOCK_READONLY) &&
256643f5e655SDavid Howells 	    !(mnt_flags & MNT_READONLY))
256743f5e655SDavid Howells 		return false;
256843f5e655SDavid Howells 
256943f5e655SDavid Howells 	if ((fl & MNT_LOCK_NODEV) &&
257043f5e655SDavid Howells 	    !(mnt_flags & MNT_NODEV))
257143f5e655SDavid Howells 		return false;
257243f5e655SDavid Howells 
257343f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOSUID) &&
257443f5e655SDavid Howells 	    !(mnt_flags & MNT_NOSUID))
257543f5e655SDavid Howells 		return false;
257643f5e655SDavid Howells 
257743f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOEXEC) &&
257843f5e655SDavid Howells 	    !(mnt_flags & MNT_NOEXEC))
257943f5e655SDavid Howells 		return false;
258043f5e655SDavid Howells 
258143f5e655SDavid Howells 	if ((fl & MNT_LOCK_ATIME) &&
258243f5e655SDavid Howells 	    ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
258343f5e655SDavid Howells 		return false;
258443f5e655SDavid Howells 
258543f5e655SDavid Howells 	return true;
258643f5e655SDavid Howells }
258743f5e655SDavid Howells 
258843f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
258943f5e655SDavid Howells {
259043f5e655SDavid Howells 	bool readonly_request = (mnt_flags & MNT_READONLY);
259143f5e655SDavid Howells 
259243f5e655SDavid Howells 	if (readonly_request == __mnt_is_readonly(&mnt->mnt))
25932e4b7fcdSDave Hansen 		return 0;
25942e4b7fcdSDave Hansen 
25952e4b7fcdSDave Hansen 	if (readonly_request)
259643f5e655SDavid Howells 		return mnt_make_readonly(mnt);
259743f5e655SDavid Howells 
259868847c94SChristian Brauner 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
259968847c94SChristian Brauner 	return 0;
260043f5e655SDavid Howells }
260143f5e655SDavid Howells 
260243f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
260343f5e655SDavid Howells {
260443f5e655SDavid Howells 	mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
260543f5e655SDavid Howells 	mnt->mnt.mnt_flags = mnt_flags;
260643f5e655SDavid Howells 	touch_mnt_namespace(mnt->mnt_ns);
260743f5e655SDavid Howells }
260843f5e655SDavid Howells 
2609f8b92ba6SDeepa Dinamani static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
2610f8b92ba6SDeepa Dinamani {
2611f8b92ba6SDeepa Dinamani 	struct super_block *sb = mnt->mnt_sb;
2612f8b92ba6SDeepa Dinamani 
2613f8b92ba6SDeepa Dinamani 	if (!__mnt_is_readonly(mnt) &&
2614a128b054SAnthony Iliopoulos 	   (!(sb->s_iflags & SB_I_TS_EXPIRY_WARNED)) &&
2615f8b92ba6SDeepa Dinamani 	   (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
2616f8b92ba6SDeepa Dinamani 		char *buf = (char *)__get_free_page(GFP_KERNEL);
2617f8b92ba6SDeepa Dinamani 		char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM);
2618f8b92ba6SDeepa Dinamani 		struct tm tm;
2619f8b92ba6SDeepa Dinamani 
2620f8b92ba6SDeepa Dinamani 		time64_to_tm(sb->s_time_max, 0, &tm);
2621f8b92ba6SDeepa Dinamani 
26220ecee669SEric Biggers 		pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n",
26230ecee669SEric Biggers 			sb->s_type->name,
26240ecee669SEric Biggers 			is_mounted(mnt) ? "remounted" : "mounted",
26250ecee669SEric Biggers 			mntpath,
2626f8b92ba6SDeepa Dinamani 			tm.tm_year+1900, (unsigned long long)sb->s_time_max);
2627f8b92ba6SDeepa Dinamani 
2628f8b92ba6SDeepa Dinamani 		free_page((unsigned long)buf);
2629a128b054SAnthony Iliopoulos 		sb->s_iflags |= SB_I_TS_EXPIRY_WARNED;
2630f8b92ba6SDeepa Dinamani 	}
2631f8b92ba6SDeepa Dinamani }
2632f8b92ba6SDeepa Dinamani 
263343f5e655SDavid Howells /*
263443f5e655SDavid Howells  * Handle reconfiguration of the mountpoint only without alteration of the
263543f5e655SDavid Howells  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
263643f5e655SDavid Howells  * to mount(2).
263743f5e655SDavid Howells  */
263843f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
263943f5e655SDavid Howells {
264043f5e655SDavid Howells 	struct super_block *sb = path->mnt->mnt_sb;
264143f5e655SDavid Howells 	struct mount *mnt = real_mount(path->mnt);
264243f5e655SDavid Howells 	int ret;
264343f5e655SDavid Howells 
264443f5e655SDavid Howells 	if (!check_mnt(mnt))
264543f5e655SDavid Howells 		return -EINVAL;
264643f5e655SDavid Howells 
264743f5e655SDavid Howells 	if (path->dentry != mnt->mnt.mnt_root)
264843f5e655SDavid Howells 		return -EINVAL;
264943f5e655SDavid Howells 
265043f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
265143f5e655SDavid Howells 		return -EPERM;
265243f5e655SDavid Howells 
2653e58ace1aSChristian Brauner 	/*
2654e58ace1aSChristian Brauner 	 * We're only checking whether the superblock is read-only not
2655e58ace1aSChristian Brauner 	 * changing it, so only take down_read(&sb->s_umount).
2656e58ace1aSChristian Brauner 	 */
2657e58ace1aSChristian Brauner 	down_read(&sb->s_umount);
265868847c94SChristian Brauner 	lock_mount_hash();
265943f5e655SDavid Howells 	ret = change_mount_ro_state(mnt, mnt_flags);
266043f5e655SDavid Howells 	if (ret == 0)
266143f5e655SDavid Howells 		set_mount_attributes(mnt, mnt_flags);
266268847c94SChristian Brauner 	unlock_mount_hash();
2663e58ace1aSChristian Brauner 	up_read(&sb->s_umount);
2664f8b92ba6SDeepa Dinamani 
2665f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2666f8b92ba6SDeepa Dinamani 
266743f5e655SDavid Howells 	return ret;
26682e4b7fcdSDave Hansen }
26692e4b7fcdSDave Hansen 
26701da177e4SLinus Torvalds /*
26711da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
26721da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
26731da177e4SLinus Torvalds  * on it - tough luck.
26741da177e4SLinus Torvalds  */
2675e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags,
2676e462ec50SDavid Howells 		      int mnt_flags, void *data)
26771da177e4SLinus Torvalds {
26781da177e4SLinus Torvalds 	int err;
26792d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2680143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
26818d0347f6SDavid Howells 	struct fs_context *fc;
26821da177e4SLinus Torvalds 
2683143c8c91SAl Viro 	if (!check_mnt(mnt))
26841da177e4SLinus Torvalds 		return -EINVAL;
26851da177e4SLinus Torvalds 
26862d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
26871da177e4SLinus Torvalds 		return -EINVAL;
26881da177e4SLinus Torvalds 
268943f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
269007b64558SEric W. Biederman 		return -EPERM;
26919566d674SEric W. Biederman 
26928d0347f6SDavid Howells 	fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
26938d0347f6SDavid Howells 	if (IS_ERR(fc))
26948d0347f6SDavid Howells 		return PTR_ERR(fc);
2695ff36fe2cSEric Paris 
2696b330966fSMiklos Szeredi 	fc->oldapi = true;
26978d0347f6SDavid Howells 	err = parse_monolithic_mount_data(fc, data);
26988d0347f6SDavid Howells 	if (!err) {
26991da177e4SLinus Torvalds 		down_write(&sb->s_umount);
270057eccb83SAl Viro 		err = -EPERM;
270143f5e655SDavid Howells 		if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
27028d0347f6SDavid Howells 			err = reconfigure_super(fc);
270368847c94SChristian Brauner 			if (!err) {
270468847c94SChristian Brauner 				lock_mount_hash();
270543f5e655SDavid Howells 				set_mount_attributes(mnt, mnt_flags);
270668847c94SChristian Brauner 				unlock_mount_hash();
270768847c94SChristian Brauner 			}
27080e55a7ccSDan Williams 		}
27096339dab8SAl Viro 		up_write(&sb->s_umount);
27108d0347f6SDavid Howells 	}
2711f8b92ba6SDeepa Dinamani 
2712f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2713f8b92ba6SDeepa Dinamani 
27148d0347f6SDavid Howells 	put_fs_context(fc);
27151da177e4SLinus Torvalds 	return err;
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
2718cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
27199676f0c6SRam Pai {
2720315fc83eSAl Viro 	struct mount *p;
2721909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2722fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
27239676f0c6SRam Pai 			return 1;
27249676f0c6SRam Pai 	}
27259676f0c6SRam Pai 	return 0;
27269676f0c6SRam Pai }
27279676f0c6SRam Pai 
272844dfd84aSDavid Howells /*
272944dfd84aSDavid Howells  * Check that there aren't references to earlier/same mount namespaces in the
273044dfd84aSDavid Howells  * specified subtree.  Such references can act as pins for mount namespaces
273144dfd84aSDavid Howells  * that aren't checked by the mount-cycle checking code, thereby allowing
273244dfd84aSDavid Howells  * cycles to be made.
273344dfd84aSDavid Howells  */
273444dfd84aSDavid Howells static bool check_for_nsfs_mounts(struct mount *subtree)
273544dfd84aSDavid Howells {
273644dfd84aSDavid Howells 	struct mount *p;
273744dfd84aSDavid Howells 	bool ret = false;
273844dfd84aSDavid Howells 
273944dfd84aSDavid Howells 	lock_mount_hash();
274044dfd84aSDavid Howells 	for (p = subtree; p; p = next_mnt(p, subtree))
274144dfd84aSDavid Howells 		if (mnt_ns_loop(p->mnt.mnt_root))
274244dfd84aSDavid Howells 			goto out;
274344dfd84aSDavid Howells 
274444dfd84aSDavid Howells 	ret = true;
274544dfd84aSDavid Howells out:
274644dfd84aSDavid Howells 	unlock_mount_hash();
274744dfd84aSDavid Howells 	return ret;
274844dfd84aSDavid Howells }
274944dfd84aSDavid Howells 
27509ffb14efSPavel Tikhomirov static int do_set_group(struct path *from_path, struct path *to_path)
27519ffb14efSPavel Tikhomirov {
27529ffb14efSPavel Tikhomirov 	struct mount *from, *to;
27539ffb14efSPavel Tikhomirov 	int err;
27549ffb14efSPavel Tikhomirov 
27559ffb14efSPavel Tikhomirov 	from = real_mount(from_path->mnt);
27569ffb14efSPavel Tikhomirov 	to = real_mount(to_path->mnt);
27579ffb14efSPavel Tikhomirov 
27589ffb14efSPavel Tikhomirov 	namespace_lock();
27599ffb14efSPavel Tikhomirov 
27609ffb14efSPavel Tikhomirov 	err = -EINVAL;
27619ffb14efSPavel Tikhomirov 	/* To and From must be mounted */
27629ffb14efSPavel Tikhomirov 	if (!is_mounted(&from->mnt))
27639ffb14efSPavel Tikhomirov 		goto out;
27649ffb14efSPavel Tikhomirov 	if (!is_mounted(&to->mnt))
27659ffb14efSPavel Tikhomirov 		goto out;
27669ffb14efSPavel Tikhomirov 
27679ffb14efSPavel Tikhomirov 	err = -EPERM;
27689ffb14efSPavel Tikhomirov 	/* We should be allowed to modify mount namespaces of both mounts */
27699ffb14efSPavel Tikhomirov 	if (!ns_capable(from->mnt_ns->user_ns, CAP_SYS_ADMIN))
27709ffb14efSPavel Tikhomirov 		goto out;
27719ffb14efSPavel Tikhomirov 	if (!ns_capable(to->mnt_ns->user_ns, CAP_SYS_ADMIN))
27729ffb14efSPavel Tikhomirov 		goto out;
27739ffb14efSPavel Tikhomirov 
27749ffb14efSPavel Tikhomirov 	err = -EINVAL;
27759ffb14efSPavel Tikhomirov 	/* To and From paths should be mount roots */
27769ffb14efSPavel Tikhomirov 	if (from_path->dentry != from_path->mnt->mnt_root)
27779ffb14efSPavel Tikhomirov 		goto out;
27789ffb14efSPavel Tikhomirov 	if (to_path->dentry != to_path->mnt->mnt_root)
27799ffb14efSPavel Tikhomirov 		goto out;
27809ffb14efSPavel Tikhomirov 
27819ffb14efSPavel Tikhomirov 	/* Setting sharing groups is only allowed across same superblock */
27829ffb14efSPavel Tikhomirov 	if (from->mnt.mnt_sb != to->mnt.mnt_sb)
27839ffb14efSPavel Tikhomirov 		goto out;
27849ffb14efSPavel Tikhomirov 
27859ffb14efSPavel Tikhomirov 	/* From mount root should be wider than To mount root */
27869ffb14efSPavel Tikhomirov 	if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root))
27879ffb14efSPavel Tikhomirov 		goto out;
27889ffb14efSPavel Tikhomirov 
27899ffb14efSPavel Tikhomirov 	/* From mount should not have locked children in place of To's root */
27909ffb14efSPavel Tikhomirov 	if (has_locked_children(from, to->mnt.mnt_root))
27919ffb14efSPavel Tikhomirov 		goto out;
27929ffb14efSPavel Tikhomirov 
27939ffb14efSPavel Tikhomirov 	/* Setting sharing groups is only allowed on private mounts */
27949ffb14efSPavel Tikhomirov 	if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to))
27959ffb14efSPavel Tikhomirov 		goto out;
27969ffb14efSPavel Tikhomirov 
27979ffb14efSPavel Tikhomirov 	/* From should not be private */
27989ffb14efSPavel Tikhomirov 	if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from))
27999ffb14efSPavel Tikhomirov 		goto out;
28009ffb14efSPavel Tikhomirov 
28019ffb14efSPavel Tikhomirov 	if (IS_MNT_SLAVE(from)) {
28029ffb14efSPavel Tikhomirov 		struct mount *m = from->mnt_master;
28039ffb14efSPavel Tikhomirov 
28049ffb14efSPavel Tikhomirov 		list_add(&to->mnt_slave, &m->mnt_slave_list);
28059ffb14efSPavel Tikhomirov 		to->mnt_master = m;
28069ffb14efSPavel Tikhomirov 	}
28079ffb14efSPavel Tikhomirov 
28089ffb14efSPavel Tikhomirov 	if (IS_MNT_SHARED(from)) {
28099ffb14efSPavel Tikhomirov 		to->mnt_group_id = from->mnt_group_id;
28109ffb14efSPavel Tikhomirov 		list_add(&to->mnt_share, &from->mnt_share);
28119ffb14efSPavel Tikhomirov 		lock_mount_hash();
28129ffb14efSPavel Tikhomirov 		set_mnt_shared(to);
28139ffb14efSPavel Tikhomirov 		unlock_mount_hash();
28149ffb14efSPavel Tikhomirov 	}
28159ffb14efSPavel Tikhomirov 
28169ffb14efSPavel Tikhomirov 	err = 0;
28179ffb14efSPavel Tikhomirov out:
28189ffb14efSPavel Tikhomirov 	namespace_unlock();
28199ffb14efSPavel Tikhomirov 	return err;
28209ffb14efSPavel Tikhomirov }
28219ffb14efSPavel Tikhomirov 
28222db154b3SDavid Howells static int do_move_mount(struct path *old_path, struct path *new_path)
28231da177e4SLinus Torvalds {
282444dfd84aSDavid Howells 	struct mnt_namespace *ns;
2825676da58dSAl Viro 	struct mount *p;
28260fb54e50SAl Viro 	struct mount *old;
28272763d119SAl Viro 	struct mount *parent;
28282763d119SAl Viro 	struct mountpoint *mp, *old_mp;
282957eccb83SAl Viro 	int err;
283044dfd84aSDavid Howells 	bool attached;
28311da177e4SLinus Torvalds 
28322db154b3SDavid Howells 	mp = lock_mount(new_path);
283384d17192SAl Viro 	if (IS_ERR(mp))
28342db154b3SDavid Howells 		return PTR_ERR(mp);
2835cc53ce53SDavid Howells 
28362db154b3SDavid Howells 	old = real_mount(old_path->mnt);
28372db154b3SDavid Howells 	p = real_mount(new_path->mnt);
28382763d119SAl Viro 	parent = old->mnt_parent;
283944dfd84aSDavid Howells 	attached = mnt_has_parent(old);
28402763d119SAl Viro 	old_mp = old->mnt_mp;
284144dfd84aSDavid Howells 	ns = old->mnt_ns;
2842143c8c91SAl Viro 
28431da177e4SLinus Torvalds 	err = -EINVAL;
284444dfd84aSDavid Howells 	/* The mountpoint must be in our namespace. */
284544dfd84aSDavid Howells 	if (!check_mnt(p))
28462db154b3SDavid Howells 		goto out;
28471da177e4SLinus Torvalds 
2848570d7a98SEric Biggers 	/* The thing moved must be mounted... */
2849570d7a98SEric Biggers 	if (!is_mounted(&old->mnt))
285044dfd84aSDavid Howells 		goto out;
285144dfd84aSDavid Howells 
2852570d7a98SEric Biggers 	/* ... and either ours or the root of anon namespace */
2853570d7a98SEric Biggers 	if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
28542db154b3SDavid Howells 		goto out;
28551da177e4SLinus Torvalds 
28562db154b3SDavid Howells 	if (old->mnt.mnt_flags & MNT_LOCKED)
28572db154b3SDavid Howells 		goto out;
28582db154b3SDavid Howells 
28592db154b3SDavid Howells 	if (old_path->dentry != old_path->mnt->mnt_root)
28602db154b3SDavid Howells 		goto out;
28612db154b3SDavid Howells 
28622db154b3SDavid Howells 	if (d_is_dir(new_path->dentry) !=
28632db154b3SDavid Howells 	    d_is_dir(old_path->dentry))
28642db154b3SDavid Howells 		goto out;
286521444403SRam Pai 	/*
286621444403SRam Pai 	 * Don't move a mount residing in a shared parent.
286721444403SRam Pai 	 */
28682763d119SAl Viro 	if (attached && IS_MNT_SHARED(parent))
28692db154b3SDavid Howells 		goto out;
28709676f0c6SRam Pai 	/*
28719676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
28729676f0c6SRam Pai 	 * mount which is shared.
28739676f0c6SRam Pai 	 */
2874fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
28752db154b3SDavid Howells 		goto out;
28761da177e4SLinus Torvalds 	err = -ELOOP;
287744dfd84aSDavid Howells 	if (!check_for_nsfs_mounts(old))
287844dfd84aSDavid Howells 		goto out;
2879fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2880676da58dSAl Viro 		if (p == old)
28812db154b3SDavid Howells 			goto out;
28821da177e4SLinus Torvalds 
28832db154b3SDavid Howells 	err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
28842763d119SAl Viro 				   attached);
28854ac91378SJan Blunck 	if (err)
28862db154b3SDavid Howells 		goto out;
28871da177e4SLinus Torvalds 
28881da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
28891da177e4SLinus Torvalds 	 * automatically */
28906776db3dSAl Viro 	list_del_init(&old->mnt_expire);
28912763d119SAl Viro 	if (attached)
28922763d119SAl Viro 		put_mountpoint(old_mp);
28931da177e4SLinus Torvalds out:
28942db154b3SDavid Howells 	unlock_mount(mp);
289544dfd84aSDavid Howells 	if (!err) {
28962763d119SAl Viro 		if (attached)
28972763d119SAl Viro 			mntput_no_expire(parent);
28982763d119SAl Viro 		else
289944dfd84aSDavid Howells 			free_mnt_ns(ns);
290044dfd84aSDavid Howells 	}
29012db154b3SDavid Howells 	return err;
29022db154b3SDavid Howells }
29032db154b3SDavid Howells 
29042db154b3SDavid Howells static int do_move_mount_old(struct path *path, const char *old_name)
29052db154b3SDavid Howells {
29062db154b3SDavid Howells 	struct path old_path;
29072db154b3SDavid Howells 	int err;
29082db154b3SDavid Howells 
29092db154b3SDavid Howells 	if (!old_name || !*old_name)
29102db154b3SDavid Howells 		return -EINVAL;
29112db154b3SDavid Howells 
29122db154b3SDavid Howells 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
29132db154b3SDavid Howells 	if (err)
29142db154b3SDavid Howells 		return err;
29152db154b3SDavid Howells 
29162db154b3SDavid Howells 	err = do_move_mount(&old_path, path);
29172d92ab3cSAl Viro 	path_put(&old_path);
29181da177e4SLinus Torvalds 	return err;
29191da177e4SLinus Torvalds }
29201da177e4SLinus Torvalds 
29219d412a43SAl Viro /*
29229d412a43SAl Viro  * add a mount into a namespace's mount tree
29239d412a43SAl Viro  */
29248f11538eSAl Viro static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
29251e2d8464SAl Viro 			const struct path *path, int mnt_flags)
29269d412a43SAl Viro {
29278f11538eSAl Viro 	struct mount *parent = real_mount(path->mnt);
29289d412a43SAl Viro 
2929f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
29309d412a43SAl Viro 
293184d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2932156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2933156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
29348f11538eSAl Viro 			return -EINVAL;
2935156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
293684d17192SAl Viro 		if (!parent->mnt_ns)
29378f11538eSAl Viro 			return -EINVAL;
2938156cacb1SAl Viro 	}
29399d412a43SAl Viro 
29409d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
294195bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
29429d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
29438f11538eSAl Viro 		return -EBUSY;
29449d412a43SAl Viro 
2945e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
29468f11538eSAl Viro 		return -EINVAL;
29479d412a43SAl Viro 
294895bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
29498f11538eSAl Viro 	return graft_tree(newmnt, parent, mp);
29509d412a43SAl Viro }
2951b1e75df4SAl Viro 
2952132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
2953132e4608SDavid Howells 
2954132e4608SDavid Howells /*
2955132e4608SDavid Howells  * Create a new mount using a superblock configuration and request it
2956132e4608SDavid Howells  * be added to the namespace tree.
2957132e4608SDavid Howells  */
2958132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
2959132e4608SDavid Howells 			   unsigned int mnt_flags)
2960132e4608SDavid Howells {
2961132e4608SDavid Howells 	struct vfsmount *mnt;
29628f11538eSAl Viro 	struct mountpoint *mp;
2963132e4608SDavid Howells 	struct super_block *sb = fc->root->d_sb;
2964132e4608SDavid Howells 	int error;
2965132e4608SDavid Howells 
2966c9ce29edSAl Viro 	error = security_sb_kern_mount(sb);
2967c9ce29edSAl Viro 	if (!error && mount_too_revealing(sb, &mnt_flags))
2968c9ce29edSAl Viro 		error = -EPERM;
2969c9ce29edSAl Viro 
2970c9ce29edSAl Viro 	if (unlikely(error)) {
2971c9ce29edSAl Viro 		fc_drop_locked(fc);
2972c9ce29edSAl Viro 		return error;
2973132e4608SDavid Howells 	}
2974132e4608SDavid Howells 
2975132e4608SDavid Howells 	up_write(&sb->s_umount);
2976132e4608SDavid Howells 
2977132e4608SDavid Howells 	mnt = vfs_create_mount(fc);
2978132e4608SDavid Howells 	if (IS_ERR(mnt))
2979132e4608SDavid Howells 		return PTR_ERR(mnt);
2980132e4608SDavid Howells 
2981f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(mountpoint, mnt);
2982f8b92ba6SDeepa Dinamani 
29838f11538eSAl Viro 	mp = lock_mount(mountpoint);
29848f11538eSAl Viro 	if (IS_ERR(mp)) {
29858f11538eSAl Viro 		mntput(mnt);
29868f11538eSAl Viro 		return PTR_ERR(mp);
29878f11538eSAl Viro 	}
29888f11538eSAl Viro 	error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
29898f11538eSAl Viro 	unlock_mount(mp);
29900ecee669SEric Biggers 	if (error < 0)
29910ecee669SEric Biggers 		mntput(mnt);
2992f8b92ba6SDeepa Dinamani 	return error;
2993f8b92ba6SDeepa Dinamani }
2994f8b92ba6SDeepa Dinamani 
29951da177e4SLinus Torvalds /*
29961da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
29971da177e4SLinus Torvalds  * namespace's tree
29981da177e4SLinus Torvalds  */
2999e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
3000808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
30011da177e4SLinus Torvalds {
30020c55cfc4SEric W. Biederman 	struct file_system_type *type;
3003a0c9a8b8SAl Viro 	struct fs_context *fc;
3004a0c9a8b8SAl Viro 	const char *subtype = NULL;
3005a0c9a8b8SAl Viro 	int err = 0;
30061da177e4SLinus Torvalds 
30070c55cfc4SEric W. Biederman 	if (!fstype)
30081da177e4SLinus Torvalds 		return -EINVAL;
30091da177e4SLinus Torvalds 
30100c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
30110c55cfc4SEric W. Biederman 	if (!type)
30120c55cfc4SEric W. Biederman 		return -ENODEV;
30130c55cfc4SEric W. Biederman 
3014a0c9a8b8SAl Viro 	if (type->fs_flags & FS_HAS_SUBTYPE) {
3015a0c9a8b8SAl Viro 		subtype = strchr(fstype, '.');
3016a0c9a8b8SAl Viro 		if (subtype) {
3017a0c9a8b8SAl Viro 			subtype++;
3018a0c9a8b8SAl Viro 			if (!*subtype) {
30190c55cfc4SEric W. Biederman 				put_filesystem(type);
3020a0c9a8b8SAl Viro 				return -EINVAL;
3021a0c9a8b8SAl Viro 			}
3022a0c9a8b8SAl Viro 		}
30238654df4eSEric W. Biederman 	}
30248654df4eSEric W. Biederman 
3025a0c9a8b8SAl Viro 	fc = fs_context_for_mount(type, sb_flags);
3026a0c9a8b8SAl Viro 	put_filesystem(type);
3027a0c9a8b8SAl Viro 	if (IS_ERR(fc))
3028a0c9a8b8SAl Viro 		return PTR_ERR(fc);
3029a0c9a8b8SAl Viro 
30303e1aeb00SDavid Howells 	if (subtype)
30313e1aeb00SDavid Howells 		err = vfs_parse_fs_string(fc, "subtype",
30323e1aeb00SDavid Howells 					  subtype, strlen(subtype));
30333e1aeb00SDavid Howells 	if (!err && name)
30343e1aeb00SDavid Howells 		err = vfs_parse_fs_string(fc, "source", name, strlen(name));
3035a0c9a8b8SAl Viro 	if (!err)
3036a0c9a8b8SAl Viro 		err = parse_monolithic_mount_data(fc, data);
3037c3aabf07SAl Viro 	if (!err && !mount_capable(fc))
3038c3aabf07SAl Viro 		err = -EPERM;
3039a0c9a8b8SAl Viro 	if (!err)
3040a0c9a8b8SAl Viro 		err = vfs_get_tree(fc);
3041132e4608SDavid Howells 	if (!err)
3042132e4608SDavid Howells 		err = do_new_mount_fc(fc, path, mnt_flags);
3043a0c9a8b8SAl Viro 
3044a0c9a8b8SAl Viro 	put_fs_context(fc);
304515f9a3f3SAl Viro 	return err;
30461da177e4SLinus Torvalds }
30471da177e4SLinus Torvalds 
30481e2d8464SAl Viro int finish_automount(struct vfsmount *m, const struct path *path)
304919a167afSAl Viro {
305026df6034SAl Viro 	struct dentry *dentry = path->dentry;
30518f11538eSAl Viro 	struct mountpoint *mp;
305225e195aaSAl Viro 	struct mount *mnt;
305319a167afSAl Viro 	int err;
305425e195aaSAl Viro 
305525e195aaSAl Viro 	if (!m)
305625e195aaSAl Viro 		return 0;
305725e195aaSAl Viro 	if (IS_ERR(m))
305825e195aaSAl Viro 		return PTR_ERR(m);
305925e195aaSAl Viro 
306025e195aaSAl Viro 	mnt = real_mount(m);
306119a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
306219a167afSAl Viro 	 * expired before we get a chance to add it
306319a167afSAl Viro 	 */
30646776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
306519a167afSAl Viro 
306619a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
306726df6034SAl Viro 	    m->mnt_root == dentry) {
3068b1e75df4SAl Viro 		err = -ELOOP;
306926df6034SAl Viro 		goto discard;
307019a167afSAl Viro 	}
307119a167afSAl Viro 
307226df6034SAl Viro 	/*
307326df6034SAl Viro 	 * we don't want to use lock_mount() - in this case finding something
307426df6034SAl Viro 	 * that overmounts our mountpoint to be means "quitely drop what we've
307526df6034SAl Viro 	 * got", not "try to mount it on top".
307626df6034SAl Viro 	 */
307726df6034SAl Viro 	inode_lock(dentry->d_inode);
307826df6034SAl Viro 	namespace_lock();
307926df6034SAl Viro 	if (unlikely(cant_mount(dentry))) {
308026df6034SAl Viro 		err = -ENOENT;
308126df6034SAl Viro 		goto discard_locked;
308226df6034SAl Viro 	}
308326df6034SAl Viro 	rcu_read_lock();
308426df6034SAl Viro 	if (unlikely(__lookup_mnt(path->mnt, dentry))) {
308526df6034SAl Viro 		rcu_read_unlock();
308626df6034SAl Viro 		err = 0;
308726df6034SAl Viro 		goto discard_locked;
308826df6034SAl Viro 	}
308926df6034SAl Viro 	rcu_read_unlock();
309026df6034SAl Viro 	mp = get_mountpoint(dentry);
30918f11538eSAl Viro 	if (IS_ERR(mp)) {
30928f11538eSAl Viro 		err = PTR_ERR(mp);
309326df6034SAl Viro 		goto discard_locked;
30948f11538eSAl Viro 	}
309526df6034SAl Viro 
30968f11538eSAl Viro 	err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
30978f11538eSAl Viro 	unlock_mount(mp);
309826df6034SAl Viro 	if (unlikely(err))
309926df6034SAl Viro 		goto discard;
310026df6034SAl Viro 	mntput(m);
3101b1e75df4SAl Viro 	return 0;
310226df6034SAl Viro 
310326df6034SAl Viro discard_locked:
310426df6034SAl Viro 	namespace_unlock();
310526df6034SAl Viro 	inode_unlock(dentry->d_inode);
310626df6034SAl Viro discard:
3107b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
31086776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
310997216be0SAl Viro 		namespace_lock();
31106776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
311197216be0SAl Viro 		namespace_unlock();
311219a167afSAl Viro 	}
3113b1e75df4SAl Viro 	mntput(m);
3114b1e75df4SAl Viro 	mntput(m);
311519a167afSAl Viro 	return err;
311619a167afSAl Viro }
311719a167afSAl Viro 
3118ea5b778aSDavid Howells /**
3119ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
3120ea5b778aSDavid Howells  * @mnt: The mount to list.
3121ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
3122ea5b778aSDavid Howells  */
3123ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
3124ea5b778aSDavid Howells {
312597216be0SAl Viro 	namespace_lock();
3126ea5b778aSDavid Howells 
31276776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
3128ea5b778aSDavid Howells 
312997216be0SAl Viro 	namespace_unlock();
3130ea5b778aSDavid Howells }
3131ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
3132ea5b778aSDavid Howells 
3133ea5b778aSDavid Howells /*
31341da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
31351da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
31361da177e4SLinus Torvalds  * here
31371da177e4SLinus Torvalds  */
31381da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
31391da177e4SLinus Torvalds {
3140761d5c38SAl Viro 	struct mount *mnt, *next;
31411da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds 	if (list_empty(mounts))
31441da177e4SLinus Torvalds 		return;
31451da177e4SLinus Torvalds 
314697216be0SAl Viro 	namespace_lock();
3147719ea2fbSAl Viro 	lock_mount_hash();
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
31501da177e4SLinus Torvalds 	 * following criteria:
31511da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
31521da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
31531da177e4SLinus Torvalds 	 *   cleared by mntput())
31541da177e4SLinus Torvalds 	 */
31556776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
3156863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
31571ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
31581da177e4SLinus Torvalds 			continue;
31596776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
31601da177e4SLinus Torvalds 	}
3161bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
31626776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
3163143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
3164e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3165bcc5c7d2SAl Viro 	}
3166719ea2fbSAl Viro 	unlock_mount_hash();
31673ab6abeeSAl Viro 	namespace_unlock();
31681da177e4SLinus Torvalds }
31691da177e4SLinus Torvalds 
31701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
31711da177e4SLinus Torvalds 
31721da177e4SLinus Torvalds /*
31735528f911STrond Myklebust  * Ripoff of 'select_parent()'
31745528f911STrond Myklebust  *
31755528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
31765528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
31775528f911STrond Myklebust  */
3178692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
31795528f911STrond Myklebust {
3180692afc31SAl Viro 	struct mount *this_parent = parent;
31815528f911STrond Myklebust 	struct list_head *next;
31825528f911STrond Myklebust 	int found = 0;
31835528f911STrond Myklebust 
31845528f911STrond Myklebust repeat:
31856b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
31865528f911STrond Myklebust resume:
31876b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
31885528f911STrond Myklebust 		struct list_head *tmp = next;
31896b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
31905528f911STrond Myklebust 
31915528f911STrond Myklebust 		next = tmp->next;
3192692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
31935528f911STrond Myklebust 			continue;
31945528f911STrond Myklebust 		/*
31955528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
31965528f911STrond Myklebust 		 */
31976b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
31985528f911STrond Myklebust 			this_parent = mnt;
31995528f911STrond Myklebust 			goto repeat;
32005528f911STrond Myklebust 		}
32015528f911STrond Myklebust 
32021ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
32036776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
32045528f911STrond Myklebust 			found++;
32055528f911STrond Myklebust 		}
32065528f911STrond Myklebust 	}
32075528f911STrond Myklebust 	/*
32085528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
32095528f911STrond Myklebust 	 */
32105528f911STrond Myklebust 	if (this_parent != parent) {
32116b41d536SAl Viro 		next = this_parent->mnt_child.next;
32120714a533SAl Viro 		this_parent = this_parent->mnt_parent;
32135528f911STrond Myklebust 		goto resume;
32145528f911STrond Myklebust 	}
32155528f911STrond Myklebust 	return found;
32165528f911STrond Myklebust }
32175528f911STrond Myklebust 
32185528f911STrond Myklebust /*
32195528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
32205528f911STrond Myklebust  * submounts of a specific parent mountpoint
322199b7db7bSNick Piggin  *
322248a066e7SAl Viro  * mount_lock must be held for write
32235528f911STrond Myklebust  */
3224b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
32255528f911STrond Myklebust {
32265528f911STrond Myklebust 	LIST_HEAD(graveyard);
3227761d5c38SAl Viro 	struct mount *m;
32285528f911STrond Myklebust 
32295528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
3230c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
3231bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
3232761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
32336776db3dSAl Viro 						mnt_expire);
3234143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
3235e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3236bcc5c7d2SAl Viro 		}
3237bcc5c7d2SAl Viro 	}
32385528f911STrond Myklebust }
32395528f911STrond Myklebust 
3240028abd92SChristoph Hellwig static void *copy_mount_options(const void __user * data)
32411da177e4SLinus Torvalds {
3242b40ef869SAl Viro 	char *copy;
3243d563d678SCatalin Marinas 	unsigned left, offset;
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds 	if (!data)
3246b40ef869SAl Viro 		return NULL;
32471da177e4SLinus Torvalds 
3248b40ef869SAl Viro 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
3249b40ef869SAl Viro 	if (!copy)
3250b40ef869SAl Viro 		return ERR_PTR(-ENOMEM);
32511da177e4SLinus Torvalds 
3252d563d678SCatalin Marinas 	left = copy_from_user(copy, data, PAGE_SIZE);
32531da177e4SLinus Torvalds 
3254d563d678SCatalin Marinas 	/*
3255d563d678SCatalin Marinas 	 * Not all architectures have an exact copy_from_user(). Resort to
3256d563d678SCatalin Marinas 	 * byte at a time.
3257d563d678SCatalin Marinas 	 */
3258d563d678SCatalin Marinas 	offset = PAGE_SIZE - left;
3259d563d678SCatalin Marinas 	while (left) {
3260d563d678SCatalin Marinas 		char c;
3261d563d678SCatalin Marinas 		if (get_user(c, (const char __user *)data + offset))
3262d563d678SCatalin Marinas 			break;
3263d563d678SCatalin Marinas 		copy[offset] = c;
3264d563d678SCatalin Marinas 		left--;
3265d563d678SCatalin Marinas 		offset++;
3266d563d678SCatalin Marinas 	}
3267d563d678SCatalin Marinas 
3268d563d678SCatalin Marinas 	if (left == PAGE_SIZE) {
3269b40ef869SAl Viro 		kfree(copy);
3270b40ef869SAl Viro 		return ERR_PTR(-EFAULT);
32711da177e4SLinus Torvalds 	}
3272d563d678SCatalin Marinas 
3273b40ef869SAl Viro 	return copy;
32741da177e4SLinus Torvalds }
32751da177e4SLinus Torvalds 
3276028abd92SChristoph Hellwig static char *copy_mount_string(const void __user *data)
3277eca6f534SVegard Nossum {
3278fbdb4401SChandan Rajendra 	return data ? strndup_user(data, PATH_MAX) : NULL;
3279eca6f534SVegard Nossum }
3280eca6f534SVegard Nossum 
32811da177e4SLinus Torvalds /*
32821da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
32831da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
32841da177e4SLinus Torvalds  *
32851da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
32861da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
32871da177e4SLinus Torvalds  * information (or be NULL).
32881da177e4SLinus Torvalds  *
32891da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
32901da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
32911da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
32921da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
32931da177e4SLinus Torvalds  * and must be discarded.
32941da177e4SLinus Torvalds  */
3295c60166f0SChristoph Hellwig int path_mount(const char *dev_name, struct path *path,
3296808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
32971da177e4SLinus Torvalds {
3298e462ec50SDavid Howells 	unsigned int mnt_flags = 0, sb_flags;
3299a1e6aaa3SChristoph Hellwig 	int ret;
33001da177e4SLinus Torvalds 
33011da177e4SLinus Torvalds 	/* Discard magic */
33021da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
33031da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
33041da177e4SLinus Torvalds 
33051da177e4SLinus Torvalds 	/* Basic sanity checks */
33061da177e4SLinus Torvalds 	if (data_page)
33071da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
33081da177e4SLinus Torvalds 
3309e462ec50SDavid Howells 	if (flags & MS_NOUSER)
3310e462ec50SDavid Howells 		return -EINVAL;
3311e462ec50SDavid Howells 
3312a1e6aaa3SChristoph Hellwig 	ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
3313a1e6aaa3SChristoph Hellwig 	if (ret)
3314a1e6aaa3SChristoph Hellwig 		return ret;
3315a1e6aaa3SChristoph Hellwig 	if (!may_mount())
3316a1e6aaa3SChristoph Hellwig 		return -EPERM;
3317f7e33bdbSJeff Layton 	if (flags & SB_MANDLOCK)
3318f7e33bdbSJeff Layton 		warn_mandlock();
3319a27ab9f2STetsuo Handa 
3320613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
3321613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
33220a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
33230a1c01c9SMatthew Garrett 
33241da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
33251da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
33261da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
33271da177e4SLinus Torvalds 	if (flags & MS_NODEV)
33281da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
33291da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
33301da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
3331fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
3332fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
3333fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
3334fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
3335d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
3336d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
3337a9e5b732SDavid Howells 	if (flags & MS_RDONLY)
33382e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
3339dab741e0SMattias Nissler 	if (flags & MS_NOSYMFOLLOW)
3340dab741e0SMattias Nissler 		mnt_flags |= MNT_NOSYMFOLLOW;
3341fc33a7bbSChristoph Hellwig 
3342ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
3343ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
3344ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3345ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
3346ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
3347a1e6aaa3SChristoph Hellwig 		mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
3348ffbc6f0eSEric W. Biederman 	}
3349ffbc6f0eSEric W. Biederman 
3350e462ec50SDavid Howells 	sb_flags = flags & (SB_RDONLY |
3351e462ec50SDavid Howells 			    SB_SYNCHRONOUS |
3352e462ec50SDavid Howells 			    SB_MANDLOCK |
3353e462ec50SDavid Howells 			    SB_DIRSYNC |
3354e462ec50SDavid Howells 			    SB_SILENT |
3355917086ffSMimi Zohar 			    SB_POSIXACL |
3356d7ee9469SMarkus Trippelsdorf 			    SB_LAZYTIME |
3357917086ffSMimi Zohar 			    SB_I_VERSION);
33581da177e4SLinus Torvalds 
335943f5e655SDavid Howells 	if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
3360a1e6aaa3SChristoph Hellwig 		return do_reconfigure_mnt(path, mnt_flags);
3361a1e6aaa3SChristoph Hellwig 	if (flags & MS_REMOUNT)
3362a1e6aaa3SChristoph Hellwig 		return do_remount(path, flags, sb_flags, mnt_flags, data_page);
3363a1e6aaa3SChristoph Hellwig 	if (flags & MS_BIND)
3364a1e6aaa3SChristoph Hellwig 		return do_loopback(path, dev_name, flags & MS_REC);
3365a1e6aaa3SChristoph Hellwig 	if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
3366a1e6aaa3SChristoph Hellwig 		return do_change_type(path, flags);
3367a1e6aaa3SChristoph Hellwig 	if (flags & MS_MOVE)
3368a1e6aaa3SChristoph Hellwig 		return do_move_mount_old(path, dev_name);
3369a1e6aaa3SChristoph Hellwig 
3370a1e6aaa3SChristoph Hellwig 	return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
33711da177e4SLinus Torvalds 			    data_page);
3372a1e6aaa3SChristoph Hellwig }
3373a1e6aaa3SChristoph Hellwig 
3374a1e6aaa3SChristoph Hellwig long do_mount(const char *dev_name, const char __user *dir_name,
3375a1e6aaa3SChristoph Hellwig 		const char *type_page, unsigned long flags, void *data_page)
3376a1e6aaa3SChristoph Hellwig {
3377a1e6aaa3SChristoph Hellwig 	struct path path;
3378a1e6aaa3SChristoph Hellwig 	int ret;
3379a1e6aaa3SChristoph Hellwig 
3380a1e6aaa3SChristoph Hellwig 	ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
3381a1e6aaa3SChristoph Hellwig 	if (ret)
3382a1e6aaa3SChristoph Hellwig 		return ret;
3383a1e6aaa3SChristoph Hellwig 	ret = path_mount(dev_name, &path, type_page, flags, data_page);
33842d92ab3cSAl Viro 	path_put(&path);
3385a1e6aaa3SChristoph Hellwig 	return ret;
33861da177e4SLinus Torvalds }
33871da177e4SLinus Torvalds 
3388537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3389537f7ccbSEric W. Biederman {
3390537f7ccbSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3391537f7ccbSEric W. Biederman }
3392537f7ccbSEric W. Biederman 
3393537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts)
3394537f7ccbSEric W. Biederman {
3395537f7ccbSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3396537f7ccbSEric W. Biederman }
3397537f7ccbSEric W. Biederman 
3398771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
3399771b1371SEric W. Biederman {
340074e83122SAl Viro 	if (!is_anon_ns(ns))
34016344c433SAl Viro 		ns_free_inum(&ns->ns);
3402537f7ccbSEric W. Biederman 	dec_mnt_namespaces(ns->ucounts);
3403771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
3404771b1371SEric W. Biederman 	kfree(ns);
3405771b1371SEric W. Biederman }
3406771b1371SEric W. Biederman 
34078823c079SEric W. Biederman /*
34088823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
34098823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
34108823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
34118823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
34128823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
34138823c079SEric W. Biederman  */
34148823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
34158823c079SEric W. Biederman 
341674e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
3417cf8d2c11STrond Myklebust {
3418cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
3419537f7ccbSEric W. Biederman 	struct ucounts *ucounts;
342098f842e6SEric W. Biederman 	int ret;
3421cf8d2c11STrond Myklebust 
3422537f7ccbSEric W. Biederman 	ucounts = inc_mnt_namespaces(user_ns);
3423537f7ccbSEric W. Biederman 	if (!ucounts)
3424df75e774SEric W. Biederman 		return ERR_PTR(-ENOSPC);
3425537f7ccbSEric W. Biederman 
342630acd0bdSVasily Averin 	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
3427537f7ccbSEric W. Biederman 	if (!new_ns) {
3428537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
3429cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
3430537f7ccbSEric W. Biederman 	}
343174e83122SAl Viro 	if (!anon) {
34326344c433SAl Viro 		ret = ns_alloc_inum(&new_ns->ns);
343398f842e6SEric W. Biederman 		if (ret) {
343498f842e6SEric W. Biederman 			kfree(new_ns);
3435537f7ccbSEric W. Biederman 			dec_mnt_namespaces(ucounts);
343698f842e6SEric W. Biederman 			return ERR_PTR(ret);
343798f842e6SEric W. Biederman 		}
343874e83122SAl Viro 	}
343933c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
344074e83122SAl Viro 	if (!anon)
34418823c079SEric W. Biederman 		new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
34421a7b8969SKirill Tkhai 	refcount_set(&new_ns->ns.count, 1);
3443cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
3444cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
34459f6c61f9SMiklos Szeredi 	spin_lock_init(&new_ns->ns_lock);
3446771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
3447537f7ccbSEric W. Biederman 	new_ns->ucounts = ucounts;
3448cf8d2c11STrond Myklebust 	return new_ns;
3449cf8d2c11STrond Myklebust }
3450cf8d2c11STrond Myklebust 
34510766f788SEmese Revfy __latent_entropy
34529559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
34539559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
34541da177e4SLinus Torvalds {
34556b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
34567f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
3457315fc83eSAl Viro 	struct mount *p, *q;
34589559f689SAl Viro 	struct mount *old;
3459cb338d06SAl Viro 	struct mount *new;
34607a472ef4SEric W. Biederman 	int copy_flags;
34611da177e4SLinus Torvalds 
34629559f689SAl Viro 	BUG_ON(!ns);
34639559f689SAl Viro 
34649559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
34659559f689SAl Viro 		get_mnt_ns(ns);
34669559f689SAl Viro 		return ns;
34679559f689SAl Viro 	}
34689559f689SAl Viro 
34699559f689SAl Viro 	old = ns->root;
34709559f689SAl Viro 
347174e83122SAl Viro 	new_ns = alloc_mnt_ns(user_ns, false);
3472cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
3473cf8d2c11STrond Myklebust 		return new_ns;
34741da177e4SLinus Torvalds 
347597216be0SAl Viro 	namespace_lock();
34761da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
34774ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
34789559f689SAl Viro 	if (user_ns != ns->user_ns)
34793bd045ccSAl Viro 		copy_flags |= CL_SHARED_TO_SLAVE;
34807a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
3481be34d1a3SDavid Howells 	if (IS_ERR(new)) {
3482328e6d90SAl Viro 		namespace_unlock();
3483771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
3484be34d1a3SDavid Howells 		return ERR_CAST(new);
34851da177e4SLinus Torvalds 	}
34863bd045ccSAl Viro 	if (user_ns != ns->user_ns) {
34873bd045ccSAl Viro 		lock_mount_hash();
34883bd045ccSAl Viro 		lock_mnt_tree(new);
34893bd045ccSAl Viro 		unlock_mount_hash();
34903bd045ccSAl Viro 	}
3491be08d6d2SAl Viro 	new_ns->root = new;
34921a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
34931da177e4SLinus Torvalds 
34941da177e4SLinus Torvalds 	/*
34951da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
34961da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
34971da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
34981da177e4SLinus Torvalds 	 */
3499909b0a88SAl Viro 	p = old;
3500cb338d06SAl Viro 	q = new;
35011da177e4SLinus Torvalds 	while (p) {
3502143c8c91SAl Viro 		q->mnt_ns = new_ns;
3503d2921684SEric W. Biederman 		new_ns->mounts++;
35049559f689SAl Viro 		if (new_fs) {
35059559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
35069559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
3507315fc83eSAl Viro 				rootmnt = &p->mnt;
35081da177e4SLinus Torvalds 			}
35099559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
35109559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
3511315fc83eSAl Viro 				pwdmnt = &p->mnt;
35121da177e4SLinus Torvalds 			}
35131da177e4SLinus Torvalds 		}
3514909b0a88SAl Viro 		p = next_mnt(p, old);
3515909b0a88SAl Viro 		q = next_mnt(q, new);
35164ce5d2b1SEric W. Biederman 		if (!q)
35174ce5d2b1SEric W. Biederman 			break;
35184ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
35194ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
35201da177e4SLinus Torvalds 	}
3521328e6d90SAl Viro 	namespace_unlock();
35221da177e4SLinus Torvalds 
35231da177e4SLinus Torvalds 	if (rootmnt)
3524f03c6599SAl Viro 		mntput(rootmnt);
35251da177e4SLinus Torvalds 	if (pwdmnt)
3526f03c6599SAl Viro 		mntput(pwdmnt);
35271da177e4SLinus Torvalds 
3528741a2951SJANAK DESAI 	return new_ns;
3529741a2951SJANAK DESAI }
3530741a2951SJANAK DESAI 
353174e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name)
3532cf8d2c11STrond Myklebust {
35331a4eeaf2SAl Viro 	struct mount *mnt = real_mount(m);
3534ea441d11SAl Viro 	struct mnt_namespace *ns;
3535d31da0f0SAl Viro 	struct super_block *s;
3536ea441d11SAl Viro 	struct path path;
3537ea441d11SAl Viro 	int err;
3538ea441d11SAl Viro 
353974e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, true);
354074e83122SAl Viro 	if (IS_ERR(ns)) {
354174e83122SAl Viro 		mntput(m);
3542ea441d11SAl Viro 		return ERR_CAST(ns);
354374e83122SAl Viro 	}
354474e83122SAl Viro 	mnt->mnt_ns = ns;
354574e83122SAl Viro 	ns->root = mnt;
354674e83122SAl Viro 	ns->mounts++;
354774e83122SAl Viro 	list_add(&mnt->mnt_list, &ns->list);
3548ea441d11SAl Viro 
354974e83122SAl Viro 	err = vfs_path_lookup(m->mnt_root, m,
3550ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3551ea441d11SAl Viro 
3552ea441d11SAl Viro 	put_mnt_ns(ns);
3553ea441d11SAl Viro 
3554ea441d11SAl Viro 	if (err)
3555ea441d11SAl Viro 		return ERR_PTR(err);
3556ea441d11SAl Viro 
3557ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
3558d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
3559d31da0f0SAl Viro 	atomic_inc(&s->s_active);
3560ea441d11SAl Viro 	mntput(path.mnt);
3561ea441d11SAl Viro 	/* lock the sucker */
3562d31da0f0SAl Viro 	down_write(&s->s_umount);
3563ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
3564ea441d11SAl Viro 	return path.dentry;
3565ea441d11SAl Viro }
3566ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
3567ea441d11SAl Viro 
3568cccaa5e3SDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3569cccaa5e3SDominik Brodowski 		char __user *, type, unsigned long, flags, void __user *, data)
35701da177e4SLinus Torvalds {
3571eca6f534SVegard Nossum 	int ret;
3572eca6f534SVegard Nossum 	char *kernel_type;
3573eca6f534SVegard Nossum 	char *kernel_dev;
3574b40ef869SAl Viro 	void *options;
35751da177e4SLinus Torvalds 
3576b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
3577b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
3578b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
3579eca6f534SVegard Nossum 		goto out_type;
35801da177e4SLinus Torvalds 
3581b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
3582b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
3583b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
3584eca6f534SVegard Nossum 		goto out_dev;
35851da177e4SLinus Torvalds 
3586b40ef869SAl Viro 	options = copy_mount_options(data);
3587b40ef869SAl Viro 	ret = PTR_ERR(options);
3588b40ef869SAl Viro 	if (IS_ERR(options))
3589eca6f534SVegard Nossum 		goto out_data;
35901da177e4SLinus Torvalds 
3591b40ef869SAl Viro 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
3592eca6f534SVegard Nossum 
3593b40ef869SAl Viro 	kfree(options);
3594eca6f534SVegard Nossum out_data:
3595eca6f534SVegard Nossum 	kfree(kernel_dev);
3596eca6f534SVegard Nossum out_dev:
3597eca6f534SVegard Nossum 	kfree(kernel_type);
3598eca6f534SVegard Nossum out_type:
3599eca6f534SVegard Nossum 	return ret;
36001da177e4SLinus Torvalds }
36011da177e4SLinus Torvalds 
36025b490500SChristian Brauner #define FSMOUNT_VALID_FLAGS                                                    \
36035b490500SChristian Brauner 	(MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV |            \
3604dd8b477fSChristian Brauner 	 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME |       \
3605dd8b477fSChristian Brauner 	 MOUNT_ATTR_NOSYMFOLLOW)
36065b490500SChristian Brauner 
36079caccd41SChristian Brauner #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
36082a186721SChristian Brauner 
36092a186721SChristian Brauner #define MOUNT_SETATTR_PROPAGATION_FLAGS \
36102a186721SChristian Brauner 	(MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
36112a186721SChristian Brauner 
36125b490500SChristian Brauner static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
36135b490500SChristian Brauner {
36145b490500SChristian Brauner 	unsigned int mnt_flags = 0;
36155b490500SChristian Brauner 
36165b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_RDONLY)
36175b490500SChristian Brauner 		mnt_flags |= MNT_READONLY;
36185b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOSUID)
36195b490500SChristian Brauner 		mnt_flags |= MNT_NOSUID;
36205b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NODEV)
36215b490500SChristian Brauner 		mnt_flags |= MNT_NODEV;
36225b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOEXEC)
36235b490500SChristian Brauner 		mnt_flags |= MNT_NOEXEC;
36245b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NODIRATIME)
36255b490500SChristian Brauner 		mnt_flags |= MNT_NODIRATIME;
3626dd8b477fSChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
3627dd8b477fSChristian Brauner 		mnt_flags |= MNT_NOSYMFOLLOW;
36285b490500SChristian Brauner 
36295b490500SChristian Brauner 	return mnt_flags;
36305b490500SChristian Brauner }
36315b490500SChristian Brauner 
36321da177e4SLinus Torvalds /*
363393766fbdSDavid Howells  * Create a kernel mount representation for a new, prepared superblock
363493766fbdSDavid Howells  * (specified by fs_fd) and attach to an open_tree-like file descriptor.
363593766fbdSDavid Howells  */
363693766fbdSDavid Howells SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
363793766fbdSDavid Howells 		unsigned int, attr_flags)
363893766fbdSDavid Howells {
363993766fbdSDavid Howells 	struct mnt_namespace *ns;
364093766fbdSDavid Howells 	struct fs_context *fc;
364193766fbdSDavid Howells 	struct file *file;
364293766fbdSDavid Howells 	struct path newmount;
364393766fbdSDavid Howells 	struct mount *mnt;
364493766fbdSDavid Howells 	struct fd f;
364593766fbdSDavid Howells 	unsigned int mnt_flags = 0;
364693766fbdSDavid Howells 	long ret;
364793766fbdSDavid Howells 
364893766fbdSDavid Howells 	if (!may_mount())
364993766fbdSDavid Howells 		return -EPERM;
365093766fbdSDavid Howells 
365193766fbdSDavid Howells 	if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
365293766fbdSDavid Howells 		return -EINVAL;
365393766fbdSDavid Howells 
36545b490500SChristian Brauner 	if (attr_flags & ~FSMOUNT_VALID_FLAGS)
365593766fbdSDavid Howells 		return -EINVAL;
365693766fbdSDavid Howells 
36575b490500SChristian Brauner 	mnt_flags = attr_flags_to_mnt_flags(attr_flags);
365893766fbdSDavid Howells 
365993766fbdSDavid Howells 	switch (attr_flags & MOUNT_ATTR__ATIME) {
366093766fbdSDavid Howells 	case MOUNT_ATTR_STRICTATIME:
366193766fbdSDavid Howells 		break;
366293766fbdSDavid Howells 	case MOUNT_ATTR_NOATIME:
366393766fbdSDavid Howells 		mnt_flags |= MNT_NOATIME;
366493766fbdSDavid Howells 		break;
366593766fbdSDavid Howells 	case MOUNT_ATTR_RELATIME:
366693766fbdSDavid Howells 		mnt_flags |= MNT_RELATIME;
366793766fbdSDavid Howells 		break;
366893766fbdSDavid Howells 	default:
366993766fbdSDavid Howells 		return -EINVAL;
367093766fbdSDavid Howells 	}
367193766fbdSDavid Howells 
367293766fbdSDavid Howells 	f = fdget(fs_fd);
367393766fbdSDavid Howells 	if (!f.file)
367493766fbdSDavid Howells 		return -EBADF;
367593766fbdSDavid Howells 
367693766fbdSDavid Howells 	ret = -EINVAL;
367793766fbdSDavid Howells 	if (f.file->f_op != &fscontext_fops)
367893766fbdSDavid Howells 		goto err_fsfd;
367993766fbdSDavid Howells 
368093766fbdSDavid Howells 	fc = f.file->private_data;
368193766fbdSDavid Howells 
368293766fbdSDavid Howells 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
368393766fbdSDavid Howells 	if (ret < 0)
368493766fbdSDavid Howells 		goto err_fsfd;
368593766fbdSDavid Howells 
368693766fbdSDavid Howells 	/* There must be a valid superblock or we can't mount it */
368793766fbdSDavid Howells 	ret = -EINVAL;
368893766fbdSDavid Howells 	if (!fc->root)
368993766fbdSDavid Howells 		goto err_unlock;
369093766fbdSDavid Howells 
369193766fbdSDavid Howells 	ret = -EPERM;
369293766fbdSDavid Howells 	if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
369393766fbdSDavid Howells 		pr_warn("VFS: Mount too revealing\n");
369493766fbdSDavid Howells 		goto err_unlock;
369593766fbdSDavid Howells 	}
369693766fbdSDavid Howells 
369793766fbdSDavid Howells 	ret = -EBUSY;
369893766fbdSDavid Howells 	if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
369993766fbdSDavid Howells 		goto err_unlock;
370093766fbdSDavid Howells 
3701f7e33bdbSJeff Layton 	if (fc->sb_flags & SB_MANDLOCK)
3702f7e33bdbSJeff Layton 		warn_mandlock();
370393766fbdSDavid Howells 
370493766fbdSDavid Howells 	newmount.mnt = vfs_create_mount(fc);
370593766fbdSDavid Howells 	if (IS_ERR(newmount.mnt)) {
370693766fbdSDavid Howells 		ret = PTR_ERR(newmount.mnt);
370793766fbdSDavid Howells 		goto err_unlock;
370893766fbdSDavid Howells 	}
370993766fbdSDavid Howells 	newmount.dentry = dget(fc->root);
371093766fbdSDavid Howells 	newmount.mnt->mnt_flags = mnt_flags;
371193766fbdSDavid Howells 
371293766fbdSDavid Howells 	/* We've done the mount bit - now move the file context into more or
371393766fbdSDavid Howells 	 * less the same state as if we'd done an fspick().  We don't want to
371493766fbdSDavid Howells 	 * do any memory allocation or anything like that at this point as we
371593766fbdSDavid Howells 	 * don't want to have to handle any errors incurred.
371693766fbdSDavid Howells 	 */
371793766fbdSDavid Howells 	vfs_clean_context(fc);
371893766fbdSDavid Howells 
371993766fbdSDavid Howells 	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
372093766fbdSDavid Howells 	if (IS_ERR(ns)) {
372193766fbdSDavid Howells 		ret = PTR_ERR(ns);
372293766fbdSDavid Howells 		goto err_path;
372393766fbdSDavid Howells 	}
372493766fbdSDavid Howells 	mnt = real_mount(newmount.mnt);
372593766fbdSDavid Howells 	mnt->mnt_ns = ns;
372693766fbdSDavid Howells 	ns->root = mnt;
372793766fbdSDavid Howells 	ns->mounts = 1;
372893766fbdSDavid Howells 	list_add(&mnt->mnt_list, &ns->list);
37291b0b9cc8SEric Biggers 	mntget(newmount.mnt);
373093766fbdSDavid Howells 
373193766fbdSDavid Howells 	/* Attach to an apparent O_PATH fd with a note that we need to unmount
373293766fbdSDavid Howells 	 * it, not just simply put it.
373393766fbdSDavid Howells 	 */
373493766fbdSDavid Howells 	file = dentry_open(&newmount, O_PATH, fc->cred);
373593766fbdSDavid Howells 	if (IS_ERR(file)) {
373693766fbdSDavid Howells 		dissolve_on_fput(newmount.mnt);
373793766fbdSDavid Howells 		ret = PTR_ERR(file);
373893766fbdSDavid Howells 		goto err_path;
373993766fbdSDavid Howells 	}
374093766fbdSDavid Howells 	file->f_mode |= FMODE_NEED_UNMOUNT;
374193766fbdSDavid Howells 
374293766fbdSDavid Howells 	ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
374393766fbdSDavid Howells 	if (ret >= 0)
374493766fbdSDavid Howells 		fd_install(ret, file);
374593766fbdSDavid Howells 	else
374693766fbdSDavid Howells 		fput(file);
374793766fbdSDavid Howells 
374893766fbdSDavid Howells err_path:
374993766fbdSDavid Howells 	path_put(&newmount);
375093766fbdSDavid Howells err_unlock:
375193766fbdSDavid Howells 	mutex_unlock(&fc->uapi_mutex);
375293766fbdSDavid Howells err_fsfd:
375393766fbdSDavid Howells 	fdput(f);
375493766fbdSDavid Howells 	return ret;
375593766fbdSDavid Howells }
375693766fbdSDavid Howells 
375793766fbdSDavid Howells /*
375893766fbdSDavid Howells  * Move a mount from one place to another.  In combination with
375993766fbdSDavid Howells  * fsopen()/fsmount() this is used to install a new mount and in combination
376093766fbdSDavid Howells  * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
376193766fbdSDavid Howells  * a mount subtree.
37622db154b3SDavid Howells  *
37632db154b3SDavid Howells  * Note the flags value is a combination of MOVE_MOUNT_* flags.
37642db154b3SDavid Howells  */
37652db154b3SDavid Howells SYSCALL_DEFINE5(move_mount,
37662658ce09SBen Dooks 		int, from_dfd, const char __user *, from_pathname,
37672658ce09SBen Dooks 		int, to_dfd, const char __user *, to_pathname,
37682db154b3SDavid Howells 		unsigned int, flags)
37692db154b3SDavid Howells {
37702db154b3SDavid Howells 	struct path from_path, to_path;
37712db154b3SDavid Howells 	unsigned int lflags;
37722db154b3SDavid Howells 	int ret = 0;
37732db154b3SDavid Howells 
37742db154b3SDavid Howells 	if (!may_mount())
37752db154b3SDavid Howells 		return -EPERM;
37762db154b3SDavid Howells 
37772db154b3SDavid Howells 	if (flags & ~MOVE_MOUNT__MASK)
37782db154b3SDavid Howells 		return -EINVAL;
37792db154b3SDavid Howells 
37802db154b3SDavid Howells 	/* If someone gives a pathname, they aren't permitted to move
37812db154b3SDavid Howells 	 * from an fd that requires unmount as we can't get at the flag
37822db154b3SDavid Howells 	 * to clear it afterwards.
37832db154b3SDavid Howells 	 */
37842db154b3SDavid Howells 	lflags = 0;
37852db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
37862db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
37872db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
37882db154b3SDavid Howells 
37892db154b3SDavid Howells 	ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
37902db154b3SDavid Howells 	if (ret < 0)
37912db154b3SDavid Howells 		return ret;
37922db154b3SDavid Howells 
37932db154b3SDavid Howells 	lflags = 0;
37942db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
37952db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
37962db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
37972db154b3SDavid Howells 
37982db154b3SDavid Howells 	ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
37992db154b3SDavid Howells 	if (ret < 0)
38002db154b3SDavid Howells 		goto out_from;
38012db154b3SDavid Howells 
38022db154b3SDavid Howells 	ret = security_move_mount(&from_path, &to_path);
38032db154b3SDavid Howells 	if (ret < 0)
38042db154b3SDavid Howells 		goto out_to;
38052db154b3SDavid Howells 
38069ffb14efSPavel Tikhomirov 	if (flags & MOVE_MOUNT_SET_GROUP)
38079ffb14efSPavel Tikhomirov 		ret = do_set_group(&from_path, &to_path);
38089ffb14efSPavel Tikhomirov 	else
38092db154b3SDavid Howells 		ret = do_move_mount(&from_path, &to_path);
38102db154b3SDavid Howells 
38112db154b3SDavid Howells out_to:
38122db154b3SDavid Howells 	path_put(&to_path);
38132db154b3SDavid Howells out_from:
38142db154b3SDavid Howells 	path_put(&from_path);
38152db154b3SDavid Howells 	return ret;
38162db154b3SDavid Howells }
38172db154b3SDavid Howells 
38182db154b3SDavid Howells /*
3819afac7cbaSAl Viro  * Return true if path is reachable from root
3820afac7cbaSAl Viro  *
382148a066e7SAl Viro  * namespace_sem or mount_lock is held
3822afac7cbaSAl Viro  */
3823643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
3824afac7cbaSAl Viro 			 const struct path *root)
3825afac7cbaSAl Viro {
3826643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
3827a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
38280714a533SAl Viro 		mnt = mnt->mnt_parent;
3829afac7cbaSAl Viro 	}
3830643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
3831afac7cbaSAl Viro }
3832afac7cbaSAl Viro 
3833640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2)
3834afac7cbaSAl Viro {
383525ab4c9bSYaowei Bai 	bool res;
383648a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
3837643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
383848a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
3839afac7cbaSAl Viro 	return res;
3840afac7cbaSAl Viro }
3841afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
3842afac7cbaSAl Viro 
3843afac7cbaSAl Viro /*
38441da177e4SLinus Torvalds  * pivot_root Semantics:
38451da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
38461da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
38471da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
38481da177e4SLinus Torvalds  *
38491da177e4SLinus Torvalds  * Restrictions:
38501da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
38511da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
38521da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
38531da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
38541da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
38551da177e4SLinus Torvalds  *
38564a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
38570c1bc6b8SMauro Carvalho Chehab  * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
38584a0d11faSNeil Brown  * in this situation.
38594a0d11faSNeil Brown  *
38601da177e4SLinus Torvalds  * Notes:
38611da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
38621da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
38631da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
38641da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
38651da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
38661da177e4SLinus Torvalds  *    first.
38671da177e4SLinus Torvalds  */
38683480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
38693480b257SHeiko Carstens 		const char __user *, put_old)
38701da177e4SLinus Torvalds {
38712763d119SAl Viro 	struct path new, old, root;
38722763d119SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
387384d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
38741da177e4SLinus Torvalds 	int error;
38751da177e4SLinus Torvalds 
38769b40bc90SAl Viro 	if (!may_mount())
38771da177e4SLinus Torvalds 		return -EPERM;
38781da177e4SLinus Torvalds 
3879ce6595a2SAl Viro 	error = user_path_at(AT_FDCWD, new_root,
3880ce6595a2SAl Viro 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
38811da177e4SLinus Torvalds 	if (error)
38821da177e4SLinus Torvalds 		goto out0;
38831da177e4SLinus Torvalds 
3884ce6595a2SAl Viro 	error = user_path_at(AT_FDCWD, put_old,
3885ce6595a2SAl Viro 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
38861da177e4SLinus Torvalds 	if (error)
38871da177e4SLinus Torvalds 		goto out1;
38881da177e4SLinus Torvalds 
38892d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
3890b12cea91SAl Viro 	if (error)
3891b12cea91SAl Viro 		goto out2;
38921da177e4SLinus Torvalds 
3893f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
389484d17192SAl Viro 	old_mp = lock_mount(&old);
389584d17192SAl Viro 	error = PTR_ERR(old_mp);
389684d17192SAl Viro 	if (IS_ERR(old_mp))
3897b12cea91SAl Viro 		goto out3;
3898b12cea91SAl Viro 
38991da177e4SLinus Torvalds 	error = -EINVAL;
3900419148daSAl Viro 	new_mnt = real_mount(new.mnt);
3901419148daSAl Viro 	root_mnt = real_mount(root.mnt);
390284d17192SAl Viro 	old_mnt = real_mount(old.mnt);
39032763d119SAl Viro 	ex_parent = new_mnt->mnt_parent;
39042763d119SAl Viro 	root_parent = root_mnt->mnt_parent;
390584d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
39062763d119SAl Viro 		IS_MNT_SHARED(ex_parent) ||
39072763d119SAl Viro 		IS_MNT_SHARED(root_parent))
3908b12cea91SAl Viro 		goto out4;
3909143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
3910b12cea91SAl Viro 		goto out4;
39115ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
39125ff9d8a6SEric W. Biederman 		goto out4;
39131da177e4SLinus Torvalds 	error = -ENOENT;
3914f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
3915b12cea91SAl Viro 		goto out4;
39161da177e4SLinus Torvalds 	error = -EBUSY;
391784d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
3918b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
39191da177e4SLinus Torvalds 	error = -EINVAL;
39208c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
3921b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3922676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
3923b12cea91SAl Viro 		goto out4; /* not attached */
39242d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
3925b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3926676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
3927b12cea91SAl Viro 		goto out4; /* not attached */
39284ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
392984d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
3930b12cea91SAl Viro 		goto out4;
39310d082601SEric W. Biederman 	/* make certain new is below the root */
39320d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
39330d082601SEric W. Biederman 		goto out4;
3934719ea2fbSAl Viro 	lock_mount_hash();
39352763d119SAl Viro 	umount_mnt(new_mnt);
39362763d119SAl Viro 	root_mp = unhash_mnt(root_mnt);  /* we'll need its mountpoint */
39375ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
39385ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
39395ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
39405ff9d8a6SEric W. Biederman 	}
39414ac91378SJan Blunck 	/* mount old root on put_old */
394284d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
39434ac91378SJan Blunck 	/* mount new_root on / */
39442763d119SAl Viro 	attach_mnt(new_mnt, root_parent, root_mp);
39452763d119SAl Viro 	mnt_add_count(root_parent, -1);
39466b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
39474fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
39484fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
39493895dbf8SEric W. Biederman 	put_mountpoint(root_mp);
3950719ea2fbSAl Viro 	unlock_mount_hash();
39512d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
39521da177e4SLinus Torvalds 	error = 0;
3953b12cea91SAl Viro out4:
395484d17192SAl Viro 	unlock_mount(old_mp);
39552763d119SAl Viro 	if (!error)
39562763d119SAl Viro 		mntput_no_expire(ex_parent);
3957b12cea91SAl Viro out3:
39588c3ee42eSAl Viro 	path_put(&root);
3959b12cea91SAl Viro out2:
39602d8f3038SAl Viro 	path_put(&old);
39611da177e4SLinus Torvalds out1:
39622d8f3038SAl Viro 	path_put(&new);
39631da177e4SLinus Torvalds out0:
39641da177e4SLinus Torvalds 	return error;
39651da177e4SLinus Torvalds }
39661da177e4SLinus Torvalds 
39672a186721SChristian Brauner static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
39682a186721SChristian Brauner {
39692a186721SChristian Brauner 	unsigned int flags = mnt->mnt.mnt_flags;
39702a186721SChristian Brauner 
39712a186721SChristian Brauner 	/*  flags to clear */
39722a186721SChristian Brauner 	flags &= ~kattr->attr_clr;
39732a186721SChristian Brauner 	/* flags to raise */
39742a186721SChristian Brauner 	flags |= kattr->attr_set;
39752a186721SChristian Brauner 
39762a186721SChristian Brauner 	return flags;
39772a186721SChristian Brauner }
39782a186721SChristian Brauner 
39799caccd41SChristian Brauner static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
39809caccd41SChristian Brauner {
39819caccd41SChristian Brauner 	struct vfsmount *m = &mnt->mnt;
3982bd303368SChristian Brauner 	struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
39839caccd41SChristian Brauner 
39849caccd41SChristian Brauner 	if (!kattr->mnt_userns)
39859caccd41SChristian Brauner 		return 0;
39869caccd41SChristian Brauner 
39879caccd41SChristian Brauner 	/*
3988bd303368SChristian Brauner 	 * Creating an idmapped mount with the filesystem wide idmapping
3989bd303368SChristian Brauner 	 * doesn't make sense so block that. We don't allow mushy semantics.
3990bd303368SChristian Brauner 	 */
3991bd303368SChristian Brauner 	if (kattr->mnt_userns == fs_userns)
3992bd303368SChristian Brauner 		return -EINVAL;
3993bd303368SChristian Brauner 
3994bd303368SChristian Brauner 	/*
39959caccd41SChristian Brauner 	 * Once a mount has been idmapped we don't allow it to change its
39969caccd41SChristian Brauner 	 * mapping. It makes things simpler and callers can just create
39979caccd41SChristian Brauner 	 * another bind-mount they can idmap if they want to.
39989caccd41SChristian Brauner 	 */
3999bb49e9e7SChristian Brauner 	if (is_idmapped_mnt(m))
40009caccd41SChristian Brauner 		return -EPERM;
40019caccd41SChristian Brauner 
40029caccd41SChristian Brauner 	/* The underlying filesystem doesn't support idmapped mounts yet. */
40039caccd41SChristian Brauner 	if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
40049caccd41SChristian Brauner 		return -EINVAL;
40059caccd41SChristian Brauner 
40069caccd41SChristian Brauner 	/* We're not controlling the superblock. */
4007bd303368SChristian Brauner 	if (!ns_capable(fs_userns, CAP_SYS_ADMIN))
40089caccd41SChristian Brauner 		return -EPERM;
40099caccd41SChristian Brauner 
40109caccd41SChristian Brauner 	/* Mount has already been visible in the filesystem hierarchy. */
40119caccd41SChristian Brauner 	if (!is_anon_ns(mnt->mnt_ns))
40129caccd41SChristian Brauner 		return -EINVAL;
40139caccd41SChristian Brauner 
40149caccd41SChristian Brauner 	return 0;
40159caccd41SChristian Brauner }
40169caccd41SChristian Brauner 
4017a26f788bSChristian Brauner /**
4018a26f788bSChristian Brauner  * mnt_allow_writers() - check whether the attribute change allows writers
4019a26f788bSChristian Brauner  * @kattr: the new mount attributes
4020a26f788bSChristian Brauner  * @mnt: the mount to which @kattr will be applied
4021a26f788bSChristian Brauner  *
4022a26f788bSChristian Brauner  * Check whether thew new mount attributes in @kattr allow concurrent writers.
4023a26f788bSChristian Brauner  *
4024a26f788bSChristian Brauner  * Return: true if writers need to be held, false if not
4025a26f788bSChristian Brauner  */
4026a26f788bSChristian Brauner static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
4027a26f788bSChristian Brauner 				     const struct mount *mnt)
40282a186721SChristian Brauner {
4029e1bbcd27SChristian Brauner 	return (!(kattr->attr_set & MNT_READONLY) ||
4030e1bbcd27SChristian Brauner 		(mnt->mnt.mnt_flags & MNT_READONLY)) &&
4031e1bbcd27SChristian Brauner 	       !kattr->mnt_userns;
40322a186721SChristian Brauner }
40332a186721SChristian Brauner 
403487bb5b60SChristian Brauner static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
40352a186721SChristian Brauner {
4036e257039fSAl Viro 	struct mount *m;
4037e257039fSAl Viro 	int err;
40382a186721SChristian Brauner 
4039e257039fSAl Viro 	for (m = mnt; m; m = next_mnt(m, mnt)) {
4040e257039fSAl Viro 		if (!can_change_locked_flags(m, recalc_flags(kattr, m))) {
4041e257039fSAl Viro 			err = -EPERM;
4042e257039fSAl Viro 			break;
40432a186721SChristian Brauner 		}
40442a186721SChristian Brauner 
404587bb5b60SChristian Brauner 		err = can_idmap_mount(kattr, m);
404687bb5b60SChristian Brauner 		if (err)
4047e257039fSAl Viro 			break;
40482a186721SChristian Brauner 
4049e257039fSAl Viro 		if (!mnt_allow_writers(kattr, m)) {
405087bb5b60SChristian Brauner 			err = mnt_hold_writers(m);
405187bb5b60SChristian Brauner 			if (err)
4052e257039fSAl Viro 				break;
40532a186721SChristian Brauner 		}
40542a186721SChristian Brauner 
4055e257039fSAl Viro 		if (!kattr->recurse)
405687bb5b60SChristian Brauner 			return 0;
40572a186721SChristian Brauner 	}
40582a186721SChristian Brauner 
4059e257039fSAl Viro 	if (err) {
4060e257039fSAl Viro 		struct mount *p;
4061e257039fSAl Viro 
40620014edaeSChristian Brauner 		/*
40630014edaeSChristian Brauner 		 * If we had to call mnt_hold_writers() MNT_WRITE_HOLD will
40640014edaeSChristian Brauner 		 * be set in @mnt_flags. The loop unsets MNT_WRITE_HOLD for all
40650014edaeSChristian Brauner 		 * mounts and needs to take care to include the first mount.
40660014edaeSChristian Brauner 		 */
40670014edaeSChristian Brauner 		for (p = mnt; p; p = next_mnt(p, mnt)) {
4068e257039fSAl Viro 			/* If we had to hold writers unblock them. */
4069e257039fSAl Viro 			if (p->mnt.mnt_flags & MNT_WRITE_HOLD)
4070e257039fSAl Viro 				mnt_unhold_writers(p);
40710014edaeSChristian Brauner 
40720014edaeSChristian Brauner 			/*
40730014edaeSChristian Brauner 			 * We're done once the first mount we changed got
40740014edaeSChristian Brauner 			 * MNT_WRITE_HOLD unset.
40750014edaeSChristian Brauner 			 */
40760014edaeSChristian Brauner 			if (p == m)
40770014edaeSChristian Brauner 				break;
4078e257039fSAl Viro 		}
4079e257039fSAl Viro 	}
4080e257039fSAl Viro 	return err;
40812a186721SChristian Brauner }
40822a186721SChristian Brauner 
40839caccd41SChristian Brauner static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
40849caccd41SChristian Brauner {
4085bd303368SChristian Brauner 	struct user_namespace *mnt_userns, *old_mnt_userns;
40869caccd41SChristian Brauner 
40879caccd41SChristian Brauner 	if (!kattr->mnt_userns)
40889caccd41SChristian Brauner 		return;
40899caccd41SChristian Brauner 
4090bd303368SChristian Brauner 	/*
4091bd303368SChristian Brauner 	 * We're the only ones able to change the mount's idmapping. So
4092bd303368SChristian Brauner 	 * mnt->mnt.mnt_userns is stable and we can retrieve it directly.
4093bd303368SChristian Brauner 	 */
4094bd303368SChristian Brauner 	old_mnt_userns = mnt->mnt.mnt_userns;
4095bd303368SChristian Brauner 
40969caccd41SChristian Brauner 	mnt_userns = get_user_ns(kattr->mnt_userns);
40979caccd41SChristian Brauner 	/* Pairs with smp_load_acquire() in mnt_user_ns(). */
40989caccd41SChristian Brauner 	smp_store_release(&mnt->mnt.mnt_userns, mnt_userns);
4099bd303368SChristian Brauner 
4100bd303368SChristian Brauner 	/*
4101bd303368SChristian Brauner 	 * If this is an idmapped filesystem drop the reference we've taken
4102bd303368SChristian Brauner 	 * in vfs_create_mount() before.
4103bd303368SChristian Brauner 	 */
4104bd303368SChristian Brauner 	if (!initial_idmapping(old_mnt_userns))
4105bd303368SChristian Brauner 		put_user_ns(old_mnt_userns);
41069caccd41SChristian Brauner }
41079caccd41SChristian Brauner 
4108e257039fSAl Viro static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
41092a186721SChristian Brauner {
4110e257039fSAl Viro 	struct mount *m;
41112a186721SChristian Brauner 
4112e257039fSAl Viro 	for (m = mnt; m; m = next_mnt(m, mnt)) {
41132a186721SChristian Brauner 		unsigned int flags;
41142a186721SChristian Brauner 
41159caccd41SChristian Brauner 		do_idmap_mount(kattr, m);
41162a186721SChristian Brauner 		flags = recalc_flags(kattr, m);
41172a186721SChristian Brauner 		WRITE_ONCE(m->mnt.mnt_flags, flags);
41182a186721SChristian Brauner 
411903b6abeeSChristian Brauner 		/* If we had to hold writers unblock them. */
412003b6abeeSChristian Brauner 		if (m->mnt.mnt_flags & MNT_WRITE_HOLD)
41212a186721SChristian Brauner 			mnt_unhold_writers(m);
41222a186721SChristian Brauner 
4123e257039fSAl Viro 		if (kattr->propagation)
41242a186721SChristian Brauner 			change_mnt_propagation(m, kattr->propagation);
4125e257039fSAl Viro 		if (!kattr->recurse)
41262a186721SChristian Brauner 			break;
4127e257039fSAl Viro 	}
41282a186721SChristian Brauner 	touch_mnt_namespace(mnt->mnt_ns);
41292a186721SChristian Brauner }
41302a186721SChristian Brauner 
41312a186721SChristian Brauner static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
41322a186721SChristian Brauner {
413387bb5b60SChristian Brauner 	struct mount *mnt = real_mount(path->mnt);
41342a186721SChristian Brauner 	int err = 0;
41352a186721SChristian Brauner 
41362a186721SChristian Brauner 	if (path->dentry != mnt->mnt.mnt_root)
41372a186721SChristian Brauner 		return -EINVAL;
41382a186721SChristian Brauner 
41392a186721SChristian Brauner 	if (kattr->propagation) {
41402a186721SChristian Brauner 		/*
41412a186721SChristian Brauner 		 * Only take namespace_lock() if we're actually changing
41422a186721SChristian Brauner 		 * propagation.
41432a186721SChristian Brauner 		 */
41442a186721SChristian Brauner 		namespace_lock();
41452a186721SChristian Brauner 		if (kattr->propagation == MS_SHARED) {
41462a186721SChristian Brauner 			err = invent_group_ids(mnt, kattr->recurse);
41472a186721SChristian Brauner 			if (err) {
41482a186721SChristian Brauner 				namespace_unlock();
41492a186721SChristian Brauner 				return err;
41502a186721SChristian Brauner 			}
41512a186721SChristian Brauner 		}
41522a186721SChristian Brauner 	}
41532a186721SChristian Brauner 
415487bb5b60SChristian Brauner 	err = -EINVAL;
41552a186721SChristian Brauner 	lock_mount_hash();
41562a186721SChristian Brauner 
415787bb5b60SChristian Brauner 	/* Ensure that this isn't anything purely vfs internal. */
415887bb5b60SChristian Brauner 	if (!is_mounted(&mnt->mnt))
415987bb5b60SChristian Brauner 		goto out;
41602a186721SChristian Brauner 
416187bb5b60SChristian Brauner 	/*
416287bb5b60SChristian Brauner 	 * If this is an attached mount make sure it's located in the callers
416387bb5b60SChristian Brauner 	 * mount namespace. If it's not don't let the caller interact with it.
416487bb5b60SChristian Brauner 	 * If this is a detached mount make sure it has an anonymous mount
416587bb5b60SChristian Brauner 	 * namespace attached to it, i.e. we've created it via OPEN_TREE_CLONE.
416687bb5b60SChristian Brauner 	 */
416787bb5b60SChristian Brauner 	if (!(mnt_has_parent(mnt) ? check_mnt(mnt) : is_anon_ns(mnt->mnt_ns)))
416887bb5b60SChristian Brauner 		goto out;
416987bb5b60SChristian Brauner 
417087bb5b60SChristian Brauner 	/*
417187bb5b60SChristian Brauner 	 * First, we get the mount tree in a shape where we can change mount
417287bb5b60SChristian Brauner 	 * properties without failure. If we succeeded to do so we commit all
417387bb5b60SChristian Brauner 	 * changes and if we failed we clean up.
417487bb5b60SChristian Brauner 	 */
417587bb5b60SChristian Brauner 	err = mount_setattr_prepare(kattr, mnt);
4176e257039fSAl Viro 	if (!err)
4177e257039fSAl Viro 		mount_setattr_commit(kattr, mnt);
417887bb5b60SChristian Brauner 
417987bb5b60SChristian Brauner out:
41802a186721SChristian Brauner 	unlock_mount_hash();
41812a186721SChristian Brauner 
41822a186721SChristian Brauner 	if (kattr->propagation) {
41832a186721SChristian Brauner 		namespace_unlock();
41842a186721SChristian Brauner 		if (err)
41852a186721SChristian Brauner 			cleanup_group_ids(mnt, NULL);
41862a186721SChristian Brauner 	}
41872a186721SChristian Brauner 
41882a186721SChristian Brauner 	return err;
41892a186721SChristian Brauner }
41902a186721SChristian Brauner 
41919caccd41SChristian Brauner static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
41929caccd41SChristian Brauner 				struct mount_kattr *kattr, unsigned int flags)
41939caccd41SChristian Brauner {
41949caccd41SChristian Brauner 	int err = 0;
41959caccd41SChristian Brauner 	struct ns_common *ns;
41969caccd41SChristian Brauner 	struct user_namespace *mnt_userns;
41979caccd41SChristian Brauner 	struct file *file;
41989caccd41SChristian Brauner 
41999caccd41SChristian Brauner 	if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
42009caccd41SChristian Brauner 		return 0;
42019caccd41SChristian Brauner 
42029caccd41SChristian Brauner 	/*
42039caccd41SChristian Brauner 	 * We currently do not support clearing an idmapped mount. If this ever
42049caccd41SChristian Brauner 	 * is a use-case we can revisit this but for now let's keep it simple
42059caccd41SChristian Brauner 	 * and not allow it.
42069caccd41SChristian Brauner 	 */
42079caccd41SChristian Brauner 	if (attr->attr_clr & MOUNT_ATTR_IDMAP)
42089caccd41SChristian Brauner 		return -EINVAL;
42099caccd41SChristian Brauner 
42109caccd41SChristian Brauner 	if (attr->userns_fd > INT_MAX)
42119caccd41SChristian Brauner 		return -EINVAL;
42129caccd41SChristian Brauner 
42139caccd41SChristian Brauner 	file = fget(attr->userns_fd);
42149caccd41SChristian Brauner 	if (!file)
42159caccd41SChristian Brauner 		return -EBADF;
42169caccd41SChristian Brauner 
42179caccd41SChristian Brauner 	if (!proc_ns_file(file)) {
42189caccd41SChristian Brauner 		err = -EINVAL;
42199caccd41SChristian Brauner 		goto out_fput;
42209caccd41SChristian Brauner 	}
42219caccd41SChristian Brauner 
42229caccd41SChristian Brauner 	ns = get_proc_ns(file_inode(file));
42239caccd41SChristian Brauner 	if (ns->ops->type != CLONE_NEWUSER) {
42249caccd41SChristian Brauner 		err = -EINVAL;
42259caccd41SChristian Brauner 		goto out_fput;
42269caccd41SChristian Brauner 	}
42279caccd41SChristian Brauner 
42289caccd41SChristian Brauner 	/*
4229bd303368SChristian Brauner 	 * The initial idmapping cannot be used to create an idmapped
4230bd303368SChristian Brauner 	 * mount. We use the initial idmapping as an indicator of a mount
4231bd303368SChristian Brauner 	 * that is not idmapped. It can simply be passed into helpers that
4232bd303368SChristian Brauner 	 * are aware of idmapped mounts as a convenient shortcut. A user
4233bd303368SChristian Brauner 	 * can just create a dedicated identity mapping to achieve the same
4234bd303368SChristian Brauner 	 * result.
42359caccd41SChristian Brauner 	 */
42369caccd41SChristian Brauner 	mnt_userns = container_of(ns, struct user_namespace, ns);
4237bd303368SChristian Brauner 	if (initial_idmapping(mnt_userns)) {
42389caccd41SChristian Brauner 		err = -EPERM;
42399caccd41SChristian Brauner 		goto out_fput;
42409caccd41SChristian Brauner 	}
4241*bf1ac16eSSeth Forshee 
4242*bf1ac16eSSeth Forshee 	/* We're not controlling the target namespace. */
4243*bf1ac16eSSeth Forshee 	if (!ns_capable(mnt_userns, CAP_SYS_ADMIN)) {
4244*bf1ac16eSSeth Forshee 		err = -EPERM;
4245*bf1ac16eSSeth Forshee 		goto out_fput;
4246*bf1ac16eSSeth Forshee 	}
4247*bf1ac16eSSeth Forshee 
42489caccd41SChristian Brauner 	kattr->mnt_userns = get_user_ns(mnt_userns);
42499caccd41SChristian Brauner 
42509caccd41SChristian Brauner out_fput:
42519caccd41SChristian Brauner 	fput(file);
42529caccd41SChristian Brauner 	return err;
42539caccd41SChristian Brauner }
42549caccd41SChristian Brauner 
42559caccd41SChristian Brauner static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
42562a186721SChristian Brauner 			     struct mount_kattr *kattr, unsigned int flags)
42572a186721SChristian Brauner {
42582a186721SChristian Brauner 	unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
42592a186721SChristian Brauner 
42602a186721SChristian Brauner 	if (flags & AT_NO_AUTOMOUNT)
42612a186721SChristian Brauner 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
42622a186721SChristian Brauner 	if (flags & AT_SYMLINK_NOFOLLOW)
42632a186721SChristian Brauner 		lookup_flags &= ~LOOKUP_FOLLOW;
42642a186721SChristian Brauner 	if (flags & AT_EMPTY_PATH)
42652a186721SChristian Brauner 		lookup_flags |= LOOKUP_EMPTY;
42662a186721SChristian Brauner 
42672a186721SChristian Brauner 	*kattr = (struct mount_kattr) {
42682a186721SChristian Brauner 		.lookup_flags	= lookup_flags,
42692a186721SChristian Brauner 		.recurse	= !!(flags & AT_RECURSIVE),
42702a186721SChristian Brauner 	};
42712a186721SChristian Brauner 
42722a186721SChristian Brauner 	if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
42732a186721SChristian Brauner 		return -EINVAL;
42742a186721SChristian Brauner 	if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
42752a186721SChristian Brauner 		return -EINVAL;
42762a186721SChristian Brauner 	kattr->propagation = attr->propagation;
42772a186721SChristian Brauner 
42782a186721SChristian Brauner 	if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
42792a186721SChristian Brauner 		return -EINVAL;
42802a186721SChristian Brauner 
42812a186721SChristian Brauner 	kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
42822a186721SChristian Brauner 	kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
42832a186721SChristian Brauner 
42842a186721SChristian Brauner 	/*
42852a186721SChristian Brauner 	 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
42862a186721SChristian Brauner 	 * users wanting to transition to a different atime setting cannot
42872a186721SChristian Brauner 	 * simply specify the atime setting in @attr_set, but must also
42882a186721SChristian Brauner 	 * specify MOUNT_ATTR__ATIME in the @attr_clr field.
42892a186721SChristian Brauner 	 * So ensure that MOUNT_ATTR__ATIME can't be partially set in
42902a186721SChristian Brauner 	 * @attr_clr and that @attr_set can't have any atime bits set if
42912a186721SChristian Brauner 	 * MOUNT_ATTR__ATIME isn't set in @attr_clr.
42922a186721SChristian Brauner 	 */
42932a186721SChristian Brauner 	if (attr->attr_clr & MOUNT_ATTR__ATIME) {
42942a186721SChristian Brauner 		if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
42952a186721SChristian Brauner 			return -EINVAL;
42962a186721SChristian Brauner 
42972a186721SChristian Brauner 		/*
42982a186721SChristian Brauner 		 * Clear all previous time settings as they are mutually
42992a186721SChristian Brauner 		 * exclusive.
43002a186721SChristian Brauner 		 */
43012a186721SChristian Brauner 		kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
43022a186721SChristian Brauner 		switch (attr->attr_set & MOUNT_ATTR__ATIME) {
43032a186721SChristian Brauner 		case MOUNT_ATTR_RELATIME:
43042a186721SChristian Brauner 			kattr->attr_set |= MNT_RELATIME;
43052a186721SChristian Brauner 			break;
43062a186721SChristian Brauner 		case MOUNT_ATTR_NOATIME:
43072a186721SChristian Brauner 			kattr->attr_set |= MNT_NOATIME;
43082a186721SChristian Brauner 			break;
43092a186721SChristian Brauner 		case MOUNT_ATTR_STRICTATIME:
43102a186721SChristian Brauner 			break;
43112a186721SChristian Brauner 		default:
43122a186721SChristian Brauner 			return -EINVAL;
43132a186721SChristian Brauner 		}
43142a186721SChristian Brauner 	} else {
43152a186721SChristian Brauner 		if (attr->attr_set & MOUNT_ATTR__ATIME)
43162a186721SChristian Brauner 			return -EINVAL;
43172a186721SChristian Brauner 	}
43182a186721SChristian Brauner 
43199caccd41SChristian Brauner 	return build_mount_idmapped(attr, usize, kattr, flags);
43209caccd41SChristian Brauner }
43219caccd41SChristian Brauner 
43229caccd41SChristian Brauner static void finish_mount_kattr(struct mount_kattr *kattr)
43239caccd41SChristian Brauner {
43249caccd41SChristian Brauner 	put_user_ns(kattr->mnt_userns);
43259caccd41SChristian Brauner 	kattr->mnt_userns = NULL;
43262a186721SChristian Brauner }
43272a186721SChristian Brauner 
43282a186721SChristian Brauner SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
43292a186721SChristian Brauner 		unsigned int, flags, struct mount_attr __user *, uattr,
43302a186721SChristian Brauner 		size_t, usize)
43312a186721SChristian Brauner {
43322a186721SChristian Brauner 	int err;
43332a186721SChristian Brauner 	struct path target;
43342a186721SChristian Brauner 	struct mount_attr attr;
43352a186721SChristian Brauner 	struct mount_kattr kattr;
43362a186721SChristian Brauner 
43372a186721SChristian Brauner 	BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
43382a186721SChristian Brauner 
43392a186721SChristian Brauner 	if (flags & ~(AT_EMPTY_PATH |
43402a186721SChristian Brauner 		      AT_RECURSIVE |
43412a186721SChristian Brauner 		      AT_SYMLINK_NOFOLLOW |
43422a186721SChristian Brauner 		      AT_NO_AUTOMOUNT))
43432a186721SChristian Brauner 		return -EINVAL;
43442a186721SChristian Brauner 
43452a186721SChristian Brauner 	if (unlikely(usize > PAGE_SIZE))
43462a186721SChristian Brauner 		return -E2BIG;
43472a186721SChristian Brauner 	if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
43482a186721SChristian Brauner 		return -EINVAL;
43492a186721SChristian Brauner 
43502a186721SChristian Brauner 	if (!may_mount())
43512a186721SChristian Brauner 		return -EPERM;
43522a186721SChristian Brauner 
43532a186721SChristian Brauner 	err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
43542a186721SChristian Brauner 	if (err)
43552a186721SChristian Brauner 		return err;
43562a186721SChristian Brauner 
43572a186721SChristian Brauner 	/* Don't bother walking through the mounts if this is a nop. */
43582a186721SChristian Brauner 	if (attr.attr_set == 0 &&
43592a186721SChristian Brauner 	    attr.attr_clr == 0 &&
43602a186721SChristian Brauner 	    attr.propagation == 0)
43612a186721SChristian Brauner 		return 0;
43622a186721SChristian Brauner 
43639caccd41SChristian Brauner 	err = build_mount_kattr(&attr, usize, &kattr, flags);
43642a186721SChristian Brauner 	if (err)
43652a186721SChristian Brauner 		return err;
43662a186721SChristian Brauner 
43672a186721SChristian Brauner 	err = user_path_at(dfd, path, kattr.lookup_flags, &target);
4368012e3322SChristian Brauner 	if (!err) {
43692a186721SChristian Brauner 		err = do_mount_setattr(&target, &kattr);
43702a186721SChristian Brauner 		path_put(&target);
4371012e3322SChristian Brauner 	}
4372012e3322SChristian Brauner 	finish_mount_kattr(&kattr);
43732a186721SChristian Brauner 	return err;
43742a186721SChristian Brauner }
43752a186721SChristian Brauner 
43761da177e4SLinus Torvalds static void __init init_mount_tree(void)
43771da177e4SLinus Torvalds {
43781da177e4SLinus Torvalds 	struct vfsmount *mnt;
437974e83122SAl Viro 	struct mount *m;
43806b3286edSKirill Korotaev 	struct mnt_namespace *ns;
4381ac748a09SJan Blunck 	struct path root;
43821da177e4SLinus Torvalds 
4383fd3e007fSAl Viro 	mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
43841da177e4SLinus Torvalds 	if (IS_ERR(mnt))
43851da177e4SLinus Torvalds 		panic("Can't create rootfs");
4386b3e19d92SNick Piggin 
438774e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, false);
43883b22edc5STrond Myklebust 	if (IS_ERR(ns))
43891da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
439074e83122SAl Viro 	m = real_mount(mnt);
439174e83122SAl Viro 	m->mnt_ns = ns;
439274e83122SAl Viro 	ns->root = m;
439374e83122SAl Viro 	ns->mounts = 1;
439474e83122SAl Viro 	list_add(&m->mnt_list, &ns->list);
43956b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
43966b3286edSKirill Korotaev 	get_mnt_ns(ns);
43971da177e4SLinus Torvalds 
4398be08d6d2SAl Viro 	root.mnt = mnt;
4399be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
4400da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
4401ac748a09SJan Blunck 
4402ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
4403ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
44041da177e4SLinus Torvalds }
44051da177e4SLinus Torvalds 
440674bf17cfSDenis Cheng void __init mnt_init(void)
44071da177e4SLinus Torvalds {
440815a67dd8SRandy Dunlap 	int err;
44091da177e4SLinus Torvalds 
44107d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
441179f6540bSVasily Averin 			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
44121da177e4SLinus Torvalds 
44130818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
441438129a13SAl Viro 				sizeof(struct hlist_head),
44150818bf27SAl Viro 				mhash_entries, 19,
44163d375d78SPavel Tatashin 				HASH_ZERO,
44170818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
44180818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
44190818bf27SAl Viro 				sizeof(struct hlist_head),
44200818bf27SAl Viro 				mphash_entries, 19,
44213d375d78SPavel Tatashin 				HASH_ZERO,
44220818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
44231da177e4SLinus Torvalds 
442484d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
44251da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
44261da177e4SLinus Torvalds 
44274b93dc9bSTejun Heo 	kernfs_init();
44284b93dc9bSTejun Heo 
442915a67dd8SRandy Dunlap 	err = sysfs_init();
443015a67dd8SRandy Dunlap 	if (err)
443115a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
44328e24eea7SHarvey Harrison 			__func__, err);
443300d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
443400d26666SGreg Kroah-Hartman 	if (!fs_kobj)
44358e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
4436037f11b4SAl Viro 	shmem_init();
44371da177e4SLinus Torvalds 	init_rootfs();
44381da177e4SLinus Torvalds 	init_mount_tree();
44391da177e4SLinus Torvalds }
44401da177e4SLinus Torvalds 
4441616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
44421da177e4SLinus Torvalds {
44431a7b8969SKirill Tkhai 	if (!refcount_dec_and_test(&ns->ns.count))
4444616511d0STrond Myklebust 		return;
44457b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
4446771b1371SEric W. Biederman 	free_mnt_ns(ns);
44471da177e4SLinus Torvalds }
44489d412a43SAl Viro 
4449d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type)
44509d412a43SAl Viro {
4451423e0ab0STim Chen 	struct vfsmount *mnt;
4452d911b458SDavid Howells 	mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
4453423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
4454423e0ab0STim Chen 		/*
4455423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
4456423e0ab0STim Chen 		 * we unmount before file sys is unregistered
4457423e0ab0STim Chen 		*/
4458f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
4459423e0ab0STim Chen 	}
4460423e0ab0STim Chen 	return mnt;
44619d412a43SAl Viro }
4462d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount);
4463423e0ab0STim Chen 
4464423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
4465423e0ab0STim Chen {
4466423e0ab0STim Chen 	/* release long term mount so mount point can be released */
4467423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
4468f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
446948a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
4470423e0ab0STim Chen 		mntput(mnt);
4471423e0ab0STim Chen 	}
4472423e0ab0STim Chen }
4473423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
447402125a82SAl Viro 
4475df820f8dSMiklos Szeredi void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
4476df820f8dSMiklos Szeredi {
4477df820f8dSMiklos Szeredi 	unsigned int i;
4478df820f8dSMiklos Szeredi 
4479df820f8dSMiklos Szeredi 	for (i = 0; i < num; i++)
4480df820f8dSMiklos Szeredi 		if (mnt[i])
4481df820f8dSMiklos Szeredi 			real_mount(mnt[i])->mnt_ns = NULL;
4482df820f8dSMiklos Szeredi 	synchronize_rcu_expedited();
4483df820f8dSMiklos Szeredi 	for (i = 0; i < num; i++)
4484df820f8dSMiklos Szeredi 		mntput(mnt[i]);
4485df820f8dSMiklos Szeredi }
4486df820f8dSMiklos Szeredi EXPORT_SYMBOL(kern_unmount_array);
4487df820f8dSMiklos Szeredi 
448802125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
448902125a82SAl Viro {
4490143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
449102125a82SAl Viro }
44928823c079SEric W. Biederman 
44933151527eSEric W. Biederman bool current_chrooted(void)
44943151527eSEric W. Biederman {
44953151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
44963151527eSEric W. Biederman 	struct path ns_root;
44973151527eSEric W. Biederman 	struct path fs_root;
44983151527eSEric W. Biederman 	bool chrooted;
44993151527eSEric W. Biederman 
45003151527eSEric W. Biederman 	/* Find the namespace root */
45013151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
45023151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
45033151527eSEric W. Biederman 	path_get(&ns_root);
45043151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
45053151527eSEric W. Biederman 		;
45063151527eSEric W. Biederman 
45073151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
45083151527eSEric W. Biederman 
45093151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
45103151527eSEric W. Biederman 
45113151527eSEric W. Biederman 	path_put(&fs_root);
45123151527eSEric W. Biederman 	path_put(&ns_root);
45133151527eSEric W. Biederman 
45143151527eSEric W. Biederman 	return chrooted;
45153151527eSEric W. Biederman }
45163151527eSEric W. Biederman 
4517132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns,
4518132e4608SDavid Howells 				const struct super_block *sb,
45198654df4eSEric W. Biederman 				int *new_mnt_flags)
452087a8ebd6SEric W. Biederman {
45218c6cf9ccSEric W. Biederman 	int new_flags = *new_mnt_flags;
452287a8ebd6SEric W. Biederman 	struct mount *mnt;
4523e51db735SEric W. Biederman 	bool visible = false;
452487a8ebd6SEric W. Biederman 
452544bb4385SAl Viro 	down_read(&namespace_sem);
45269f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
452787a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
4528e51db735SEric W. Biederman 		struct mount *child;
452977b1a97dSEric W. Biederman 		int mnt_flags;
453077b1a97dSEric W. Biederman 
45319f6c61f9SMiklos Szeredi 		if (mnt_is_cursor(mnt))
45329f6c61f9SMiklos Szeredi 			continue;
45339f6c61f9SMiklos Szeredi 
4534132e4608SDavid Howells 		if (mnt->mnt.mnt_sb->s_type != sb->s_type)
4535e51db735SEric W. Biederman 			continue;
4536e51db735SEric W. Biederman 
45377e96c1b0SEric W. Biederman 		/* This mount is not fully visible if it's root directory
45387e96c1b0SEric W. Biederman 		 * is not the root directory of the filesystem.
45397e96c1b0SEric W. Biederman 		 */
45407e96c1b0SEric W. Biederman 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
45417e96c1b0SEric W. Biederman 			continue;
45427e96c1b0SEric W. Biederman 
4543a1935c17SEric W. Biederman 		/* A local view of the mount flags */
454477b1a97dSEric W. Biederman 		mnt_flags = mnt->mnt.mnt_flags;
454577b1a97dSEric W. Biederman 
4546695e9df0SEric W. Biederman 		/* Don't miss readonly hidden in the superblock flags */
4547bc98a42cSDavid Howells 		if (sb_rdonly(mnt->mnt.mnt_sb))
4548695e9df0SEric W. Biederman 			mnt_flags |= MNT_LOCK_READONLY;
4549695e9df0SEric W. Biederman 
45508c6cf9ccSEric W. Biederman 		/* Verify the mount flags are equal to or more permissive
45518c6cf9ccSEric W. Biederman 		 * than the proposed new mount.
45528c6cf9ccSEric W. Biederman 		 */
455377b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_READONLY) &&
45548c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_READONLY))
45558c6cf9ccSEric W. Biederman 			continue;
455677b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_ATIME) &&
455777b1a97dSEric W. Biederman 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
45588c6cf9ccSEric W. Biederman 			continue;
45598c6cf9ccSEric W. Biederman 
4560ceeb0e5dSEric W. Biederman 		/* This mount is not fully visible if there are any
4561ceeb0e5dSEric W. Biederman 		 * locked child mounts that cover anything except for
4562ceeb0e5dSEric W. Biederman 		 * empty directories.
4563e51db735SEric W. Biederman 		 */
4564e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
4565e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
4566ceeb0e5dSEric W. Biederman 			/* Only worry about locked mounts */
4567d71ed6c9SEric W. Biederman 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
4568ceeb0e5dSEric W. Biederman 				continue;
45697236c85eSEric W. Biederman 			/* Is the directory permanetly empty? */
45707236c85eSEric W. Biederman 			if (!is_empty_dir_inode(inode))
4571e51db735SEric W. Biederman 				goto next;
457287a8ebd6SEric W. Biederman 		}
45738c6cf9ccSEric W. Biederman 		/* Preserve the locked attributes */
457477b1a97dSEric W. Biederman 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
45758c6cf9ccSEric W. Biederman 					       MNT_LOCK_ATIME);
4576e51db735SEric W. Biederman 		visible = true;
4577e51db735SEric W. Biederman 		goto found;
4578e51db735SEric W. Biederman 	next:	;
457987a8ebd6SEric W. Biederman 	}
4580e51db735SEric W. Biederman found:
45819f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
458244bb4385SAl Viro 	up_read(&namespace_sem);
4583e51db735SEric W. Biederman 	return visible;
458487a8ebd6SEric W. Biederman }
458587a8ebd6SEric W. Biederman 
4586132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
45878654df4eSEric W. Biederman {
4588a1935c17SEric W. Biederman 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
45898654df4eSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
45908654df4eSEric W. Biederman 	unsigned long s_iflags;
45918654df4eSEric W. Biederman 
45928654df4eSEric W. Biederman 	if (ns->user_ns == &init_user_ns)
45938654df4eSEric W. Biederman 		return false;
45948654df4eSEric W. Biederman 
45958654df4eSEric W. Biederman 	/* Can this filesystem be too revealing? */
4596132e4608SDavid Howells 	s_iflags = sb->s_iflags;
45978654df4eSEric W. Biederman 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
45988654df4eSEric W. Biederman 		return false;
45998654df4eSEric W. Biederman 
4600a1935c17SEric W. Biederman 	if ((s_iflags & required_iflags) != required_iflags) {
4601a1935c17SEric W. Biederman 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
4602a1935c17SEric W. Biederman 			  required_iflags);
4603a1935c17SEric W. Biederman 		return true;
4604a1935c17SEric W. Biederman 	}
4605a1935c17SEric W. Biederman 
4606132e4608SDavid Howells 	return !mnt_already_visible(ns, sb, new_mnt_flags);
46078654df4eSEric W. Biederman }
46088654df4eSEric W. Biederman 
4609380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt)
4610380cf5baSAndy Lutomirski {
4611380cf5baSAndy Lutomirski 	/*
4612380cf5baSAndy Lutomirski 	 * Foreign mounts (accessed via fchdir or through /proc
4613380cf5baSAndy Lutomirski 	 * symlinks) are always treated as if they are nosuid.  This
4614380cf5baSAndy Lutomirski 	 * prevents namespaces from trusting potentially unsafe
4615380cf5baSAndy Lutomirski 	 * suid/sgid bits, file caps, or security labels that originate
4616380cf5baSAndy Lutomirski 	 * in other namespaces.
4617380cf5baSAndy Lutomirski 	 */
4618380cf5baSAndy Lutomirski 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
4619380cf5baSAndy Lutomirski 	       current_in_userns(mnt->mnt_sb->s_user_ns);
4620380cf5baSAndy Lutomirski }
4621380cf5baSAndy Lutomirski 
462264964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
46238823c079SEric W. Biederman {
462458be2825SAl Viro 	struct ns_common *ns = NULL;
46258823c079SEric W. Biederman 	struct nsproxy *nsproxy;
46268823c079SEric W. Biederman 
4627728dba3aSEric W. Biederman 	task_lock(task);
4628728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
46298823c079SEric W. Biederman 	if (nsproxy) {
463058be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
463158be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
46328823c079SEric W. Biederman 	}
4633728dba3aSEric W. Biederman 	task_unlock(task);
46348823c079SEric W. Biederman 
46358823c079SEric W. Biederman 	return ns;
46368823c079SEric W. Biederman }
46378823c079SEric W. Biederman 
463864964528SAl Viro static void mntns_put(struct ns_common *ns)
46398823c079SEric W. Biederman {
464058be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
46418823c079SEric W. Biederman }
46428823c079SEric W. Biederman 
4643f2a8d52eSChristian Brauner static int mntns_install(struct nsset *nsset, struct ns_common *ns)
46448823c079SEric W. Biederman {
4645f2a8d52eSChristian Brauner 	struct nsproxy *nsproxy = nsset->nsproxy;
4646f2a8d52eSChristian Brauner 	struct fs_struct *fs = nsset->fs;
46474f757f3cSAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
4648f2a8d52eSChristian Brauner 	struct user_namespace *user_ns = nsset->cred->user_ns;
46498823c079SEric W. Biederman 	struct path root;
46504f757f3cSAl Viro 	int err;
46518823c079SEric W. Biederman 
46520c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
4653f2a8d52eSChristian Brauner 	    !ns_capable(user_ns, CAP_SYS_CHROOT) ||
4654f2a8d52eSChristian Brauner 	    !ns_capable(user_ns, CAP_SYS_ADMIN))
4655ae11e0f1SZhao Hongjiang 		return -EPERM;
46568823c079SEric W. Biederman 
465774e83122SAl Viro 	if (is_anon_ns(mnt_ns))
465874e83122SAl Viro 		return -EINVAL;
465974e83122SAl Viro 
46608823c079SEric W. Biederman 	if (fs->users != 1)
46618823c079SEric W. Biederman 		return -EINVAL;
46628823c079SEric W. Biederman 
46638823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
46644f757f3cSAl Viro 	old_mnt_ns = nsproxy->mnt_ns;
46658823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
46668823c079SEric W. Biederman 
46678823c079SEric W. Biederman 	/* Find the root */
46684f757f3cSAl Viro 	err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
46694f757f3cSAl Viro 				"/", LOOKUP_DOWN, &root);
46704f757f3cSAl Viro 	if (err) {
46714f757f3cSAl Viro 		/* revert to old namespace */
46724f757f3cSAl Viro 		nsproxy->mnt_ns = old_mnt_ns;
46734f757f3cSAl Viro 		put_mnt_ns(mnt_ns);
46744f757f3cSAl Viro 		return err;
46754f757f3cSAl Viro 	}
46768823c079SEric W. Biederman 
46774068367cSAndrei Vagin 	put_mnt_ns(old_mnt_ns);
46784068367cSAndrei Vagin 
46798823c079SEric W. Biederman 	/* Update the pwd and root */
46808823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
46818823c079SEric W. Biederman 	set_fs_root(fs, &root);
46828823c079SEric W. Biederman 
46838823c079SEric W. Biederman 	path_put(&root);
46848823c079SEric W. Biederman 	return 0;
46858823c079SEric W. Biederman }
46868823c079SEric W. Biederman 
4687bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns)
4688bcac25a5SAndrey Vagin {
4689bcac25a5SAndrey Vagin 	return to_mnt_ns(ns)->user_ns;
4690bcac25a5SAndrey Vagin }
4691bcac25a5SAndrey Vagin 
46928823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
46938823c079SEric W. Biederman 	.name		= "mnt",
46948823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
46958823c079SEric W. Biederman 	.get		= mntns_get,
46968823c079SEric W. Biederman 	.put		= mntns_put,
46978823c079SEric W. Biederman 	.install	= mntns_install,
4698bcac25a5SAndrey Vagin 	.owner		= mntns_owner,
46998823c079SEric W. Biederman };
4700ab171b95SLuis Chamberlain 
4701ab171b95SLuis Chamberlain #ifdef CONFIG_SYSCTL
4702ab171b95SLuis Chamberlain static struct ctl_table fs_namespace_sysctls[] = {
4703ab171b95SLuis Chamberlain 	{
4704ab171b95SLuis Chamberlain 		.procname	= "mount-max",
4705ab171b95SLuis Chamberlain 		.data		= &sysctl_mount_max,
4706ab171b95SLuis Chamberlain 		.maxlen		= sizeof(unsigned int),
4707ab171b95SLuis Chamberlain 		.mode		= 0644,
4708ab171b95SLuis Chamberlain 		.proc_handler	= proc_dointvec_minmax,
4709ab171b95SLuis Chamberlain 		.extra1		= SYSCTL_ONE,
4710ab171b95SLuis Chamberlain 	},
4711ab171b95SLuis Chamberlain 	{ }
4712ab171b95SLuis Chamberlain };
4713ab171b95SLuis Chamberlain 
4714ab171b95SLuis Chamberlain static int __init init_fs_namespace_sysctls(void)
4715ab171b95SLuis Chamberlain {
4716ab171b95SLuis Chamberlain 	register_sysctl_init("fs", fs_namespace_sysctls);
4717ab171b95SLuis Chamberlain 	return 0;
4718ab171b95SLuis Chamberlain }
4719ab171b95SLuis Chamberlain fs_initcall(init_fs_namespace_sysctls);
4720ab171b95SLuis Chamberlain 
4721ab171b95SLuis Chamberlain #endif /* CONFIG_SYSCTL */
4722