xref: /openbmc/linux/fs/namespace.c (revision 34d6f206a88c2651d216bd3487ac956a40b2ba8e)
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;
set_mhash_entries(char * str)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;
set_mphash_entries(char * str)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;
85256c8aedSChristian Brauner 	struct mnt_idmap *mnt_idmap;
862a186721SChristian Brauner };
872a186721SChristian Brauner 
88f87fd4c2SMiklos Szeredi /* /sys/fs */
8900d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
9000d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
91f87fd4c2SMiklos Szeredi 
9299b7db7bSNick Piggin /*
9399b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
9499b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
9599b7db7bSNick Piggin  * up the tree.
9699b7db7bSNick Piggin  *
9799b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
9899b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
9999b7db7bSNick Piggin  */
10048a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
10199b7db7bSNick Piggin 
lock_mount_hash(void)102d033cb67SChristian Brauner static inline void lock_mount_hash(void)
103d033cb67SChristian Brauner {
104d033cb67SChristian Brauner 	write_seqlock(&mount_lock);
105d033cb67SChristian Brauner }
106d033cb67SChristian Brauner 
unlock_mount_hash(void)107d033cb67SChristian Brauner static inline void unlock_mount_hash(void)
108d033cb67SChristian Brauner {
109d033cb67SChristian Brauner 	write_sequnlock(&mount_lock);
110d033cb67SChristian Brauner }
111d033cb67SChristian Brauner 
m_hash(struct vfsmount * mnt,struct dentry * dentry)11238129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
1151da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
1160818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
1170818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
1180818bf27SAl Viro }
1190818bf27SAl Viro 
mp_hash(struct dentry * dentry)1200818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
1210818bf27SAl Viro {
1220818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
1230818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
1240818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
mnt_alloc_id(struct mount * mnt)127b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
12873cd49ecSMiklos Szeredi {
129169b480eSMatthew Wilcox 	int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
13073cd49ecSMiklos Szeredi 
131169b480eSMatthew Wilcox 	if (res < 0)
13273cd49ecSMiklos Szeredi 		return res;
133169b480eSMatthew Wilcox 	mnt->mnt_id = res;
134169b480eSMatthew Wilcox 	return 0;
13573cd49ecSMiklos Szeredi }
13673cd49ecSMiklos Szeredi 
mnt_free_id(struct mount * mnt)137b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
13873cd49ecSMiklos Szeredi {
139169b480eSMatthew Wilcox 	ida_free(&mnt_id_ida, mnt->mnt_id);
14073cd49ecSMiklos Szeredi }
14173cd49ecSMiklos Szeredi 
142719f5d7fSMiklos Szeredi /*
143719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
144719f5d7fSMiklos Szeredi  */
mnt_alloc_group_id(struct mount * mnt)1454b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
146719f5d7fSMiklos Szeredi {
147169b480eSMatthew Wilcox 	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
148f21f6220SAl Viro 
149169b480eSMatthew Wilcox 	if (res < 0)
150f21f6220SAl Viro 		return res;
151169b480eSMatthew Wilcox 	mnt->mnt_group_id = res;
152169b480eSMatthew Wilcox 	return 0;
153719f5d7fSMiklos Szeredi }
154719f5d7fSMiklos Szeredi 
155719f5d7fSMiklos Szeredi /*
156719f5d7fSMiklos Szeredi  * Release a peer group ID
157719f5d7fSMiklos Szeredi  */
mnt_release_group_id(struct mount * mnt)1584b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
159719f5d7fSMiklos Szeredi {
160169b480eSMatthew Wilcox 	ida_free(&mnt_group_ida, mnt->mnt_group_id);
16115169fe7SAl Viro 	mnt->mnt_group_id = 0;
162719f5d7fSMiklos Szeredi }
163719f5d7fSMiklos Szeredi 
164b3e19d92SNick Piggin /*
165b3e19d92SNick Piggin  * vfsmount lock must be held for read
166b3e19d92SNick Piggin  */
mnt_add_count(struct mount * mnt,int n)16783adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
168b3e19d92SNick Piggin {
169b3e19d92SNick Piggin #ifdef CONFIG_SMP
17068e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
171b3e19d92SNick Piggin #else
172b3e19d92SNick Piggin 	preempt_disable();
17368e8a9feSAl Viro 	mnt->mnt_count += n;
174b3e19d92SNick Piggin 	preempt_enable();
175b3e19d92SNick Piggin #endif
176b3e19d92SNick Piggin }
177b3e19d92SNick Piggin 
178b3e19d92SNick Piggin /*
179b3e19d92SNick Piggin  * vfsmount lock must be held for write
180b3e19d92SNick Piggin  */
mnt_get_count(struct mount * mnt)181edf7ddbfSEric Biggers int mnt_get_count(struct mount *mnt)
182b3e19d92SNick Piggin {
183b3e19d92SNick Piggin #ifdef CONFIG_SMP
184edf7ddbfSEric Biggers 	int count = 0;
185b3e19d92SNick Piggin 	int cpu;
186b3e19d92SNick Piggin 
187b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
18868e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
189b3e19d92SNick Piggin 	}
190b3e19d92SNick Piggin 
191b3e19d92SNick Piggin 	return count;
192b3e19d92SNick Piggin #else
19368e8a9feSAl Viro 	return mnt->mnt_count;
194b3e19d92SNick Piggin #endif
195b3e19d92SNick Piggin }
196b3e19d92SNick Piggin 
alloc_vfsmnt(const char * name)197b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
1981da177e4SLinus Torvalds {
199c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
200c63181e6SAl Viro 	if (mnt) {
20173cd49ecSMiklos Szeredi 		int err;
20273cd49ecSMiklos Szeredi 
203c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
20488b38782SLi Zefan 		if (err)
20588b38782SLi Zefan 			goto out_free_cache;
20688b38782SLi Zefan 
20788b38782SLi Zefan 		if (name) {
20879f6540bSVasily Averin 			mnt->mnt_devname = kstrdup_const(name,
20979f6540bSVasily Averin 							 GFP_KERNEL_ACCOUNT);
210c63181e6SAl Viro 			if (!mnt->mnt_devname)
21188b38782SLi Zefan 				goto out_free_id;
21273cd49ecSMiklos Szeredi 		}
21373cd49ecSMiklos Szeredi 
214b3e19d92SNick Piggin #ifdef CONFIG_SMP
215c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
216c63181e6SAl Viro 		if (!mnt->mnt_pcp)
217b3e19d92SNick Piggin 			goto out_free_devname;
218b3e19d92SNick Piggin 
219c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
220b3e19d92SNick Piggin #else
221c63181e6SAl Viro 		mnt->mnt_count = 1;
222c63181e6SAl Viro 		mnt->mnt_writers = 0;
223b3e19d92SNick Piggin #endif
224b3e19d92SNick Piggin 
22538129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
226c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
227c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
228c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
229c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
230c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
231c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
232c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2330a5eb7c8SEric W. Biederman 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
23499b19d16SEric W. Biederman 		INIT_LIST_HEAD(&mnt->mnt_umounting);
23556cbb429SAl Viro 		INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
236256c8aedSChristian Brauner 		mnt->mnt.mnt_idmap = &nop_mnt_idmap;
2371da177e4SLinus Torvalds 	}
238c63181e6SAl Viro 	return mnt;
23988b38782SLi Zefan 
240d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
241d3ef3d73Snpiggin@suse.de out_free_devname:
242fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
243d3ef3d73Snpiggin@suse.de #endif
24488b38782SLi Zefan out_free_id:
245c63181e6SAl Viro 	mnt_free_id(mnt);
24688b38782SLi Zefan out_free_cache:
247c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
24888b38782SLi Zefan 	return NULL;
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2518366025eSDave Hansen /*
2528366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2538366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2548366025eSDave Hansen  * We must keep track of when those operations start
2558366025eSDave Hansen  * (for permission checks) and when they end, so that
2568366025eSDave Hansen  * we can determine when writes are able to occur to
2578366025eSDave Hansen  * a filesystem.
2588366025eSDave Hansen  */
2593d733633SDave Hansen /*
2603d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2613d733633SDave Hansen  * @mnt: the mount to check for its write status
2623d733633SDave Hansen  *
2633d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2643d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2653d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2663d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2673d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2683d733633SDave Hansen  * r/w.
2693d733633SDave Hansen  */
__mnt_is_readonly(struct vfsmount * mnt)27043f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt)
2713d733633SDave Hansen {
27243f5e655SDavid Howells 	return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
2733d733633SDave Hansen }
2743d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2753d733633SDave Hansen 
mnt_inc_writers(struct mount * mnt)27683adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2773d733633SDave Hansen {
278d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
27968e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
280d3ef3d73Snpiggin@suse.de #else
28168e8a9feSAl Viro 	mnt->mnt_writers++;
282d3ef3d73Snpiggin@suse.de #endif
2833d733633SDave Hansen }
2843d733633SDave Hansen 
mnt_dec_writers(struct mount * mnt)28583adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2863d733633SDave Hansen {
287d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
28868e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
289d3ef3d73Snpiggin@suse.de #else
29068e8a9feSAl Viro 	mnt->mnt_writers--;
291d3ef3d73Snpiggin@suse.de #endif
292d3ef3d73Snpiggin@suse.de }
293d3ef3d73Snpiggin@suse.de 
mnt_get_writers(struct mount * mnt)29483adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
295d3ef3d73Snpiggin@suse.de {
296d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
297d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2983d733633SDave Hansen 	int cpu;
2993d733633SDave Hansen 
3003d733633SDave Hansen 	for_each_possible_cpu(cpu) {
30168e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3023d733633SDave Hansen 	}
3033d733633SDave Hansen 
304d3ef3d73Snpiggin@suse.de 	return count;
305d3ef3d73Snpiggin@suse.de #else
306d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
307d3ef3d73Snpiggin@suse.de #endif
3083d733633SDave Hansen }
3093d733633SDave Hansen 
mnt_is_readonly(struct vfsmount * mnt)3104ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
3114ed5e82fSMiklos Szeredi {
312d7439fb1SJan Kara 	if (READ_ONCE(mnt->mnt_sb->s_readonly_remount))
3134ed5e82fSMiklos Szeredi 		return 1;
314d7439fb1SJan Kara 	/*
315d7439fb1SJan Kara 	 * The barrier pairs with the barrier in sb_start_ro_state_change()
316d7439fb1SJan Kara 	 * making sure if we don't see s_readonly_remount set yet, we also will
317d7439fb1SJan Kara 	 * not see any superblock / mount flag changes done by remount.
318d7439fb1SJan Kara 	 * It also pairs with the barrier in sb_end_ro_state_change()
319d7439fb1SJan Kara 	 * assuring that if we see s_readonly_remount already cleared, we will
320d7439fb1SJan Kara 	 * see the values of superblock / mount flags updated by remount.
321d7439fb1SJan Kara 	 */
3224ed5e82fSMiklos Szeredi 	smp_rmb();
3234ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
3244ed5e82fSMiklos Szeredi }
3254ed5e82fSMiklos Szeredi 
3263d733633SDave Hansen /*
327eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
328eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
329eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
330eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
3313d733633SDave Hansen  */
3328366025eSDave Hansen /**
333eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
33483adc753SAl Viro  * @m: the mount on which to take a write
3358366025eSDave Hansen  *
336eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
337eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
338eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
339eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
340eb04c282SJan Kara  * called. This is effectively a refcount.
3418366025eSDave Hansen  */
__mnt_want_write(struct vfsmount * m)342eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3438366025eSDave Hansen {
34483adc753SAl Viro 	struct mount *mnt = real_mount(m);
3453d733633SDave Hansen 	int ret = 0;
3463d733633SDave Hansen 
347d3ef3d73Snpiggin@suse.de 	preempt_disable();
348c6653a83SNick Piggin 	mnt_inc_writers(mnt);
349d3ef3d73Snpiggin@suse.de 	/*
350c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
351d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
352d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
353d3ef3d73Snpiggin@suse.de 	 */
354d3ef3d73Snpiggin@suse.de 	smp_mb();
3550f8821daSSebastian Andrzej Siewior 	might_lock(&mount_lock.lock);
3560f8821daSSebastian Andrzej Siewior 	while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
3570f8821daSSebastian Andrzej Siewior 		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
358d3ef3d73Snpiggin@suse.de 			cpu_relax();
3590f8821daSSebastian Andrzej Siewior 		} else {
3600f8821daSSebastian Andrzej Siewior 			/*
3610f8821daSSebastian Andrzej Siewior 			 * This prevents priority inversion, if the task
3620f8821daSSebastian Andrzej Siewior 			 * setting MNT_WRITE_HOLD got preempted on a remote
3630f8821daSSebastian Andrzej Siewior 			 * CPU, and it prevents life lock if the task setting
3640f8821daSSebastian Andrzej Siewior 			 * MNT_WRITE_HOLD has a lower priority and is bound to
3650f8821daSSebastian Andrzej Siewior 			 * the same CPU as the task that is spinning here.
3660f8821daSSebastian Andrzej Siewior 			 */
3670f8821daSSebastian Andrzej Siewior 			preempt_enable();
3680f8821daSSebastian Andrzej Siewior 			lock_mount_hash();
3690f8821daSSebastian Andrzej Siewior 			unlock_mount_hash();
3700f8821daSSebastian Andrzej Siewior 			preempt_disable();
3710f8821daSSebastian Andrzej Siewior 		}
3720f8821daSSebastian Andrzej Siewior 	}
373d3ef3d73Snpiggin@suse.de 	/*
374d7439fb1SJan Kara 	 * The barrier pairs with the barrier sb_start_ro_state_change() making
375d7439fb1SJan Kara 	 * sure that if we see MNT_WRITE_HOLD cleared, we will also see
376d7439fb1SJan Kara 	 * s_readonly_remount set (or even SB_RDONLY / MNT_READONLY flags) in
377d7439fb1SJan Kara 	 * mnt_is_readonly() and bail in case we are racing with remount
378d7439fb1SJan Kara 	 * read-only.
379d3ef3d73Snpiggin@suse.de 	 */
380d3ef3d73Snpiggin@suse.de 	smp_rmb();
3814ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
382c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3833d733633SDave Hansen 		ret = -EROFS;
3843d733633SDave Hansen 	}
385d3ef3d73Snpiggin@suse.de 	preempt_enable();
386eb04c282SJan Kara 
387eb04c282SJan Kara 	return ret;
388eb04c282SJan Kara }
389eb04c282SJan Kara 
390eb04c282SJan Kara /**
391eb04c282SJan Kara  * mnt_want_write - get write access to a mount
392eb04c282SJan Kara  * @m: the mount on which to take a write
393eb04c282SJan Kara  *
394eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
395eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
396eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
397eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
398eb04c282SJan Kara  */
mnt_want_write(struct vfsmount * m)399eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
400eb04c282SJan Kara {
401eb04c282SJan Kara 	int ret;
402eb04c282SJan Kara 
403eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
404eb04c282SJan Kara 	ret = __mnt_want_write(m);
405eb04c282SJan Kara 	if (ret)
406eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
4073d733633SDave Hansen 	return ret;
4088366025eSDave Hansen }
4098366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
4108366025eSDave Hansen 
4118366025eSDave Hansen /**
412eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
413eb04c282SJan Kara  * @file: the file who's mount on which to take a write
414eb04c282SJan Kara  *
41514e43bf4SEric Biggers  * This is like __mnt_want_write, but if the file is already open for writing it
41614e43bf4SEric Biggers  * skips incrementing mnt_writers (since the open file already has a reference)
41714e43bf4SEric Biggers  * and instead only does the check for emergency r/o remounts.  This must be
41814e43bf4SEric Biggers  * paired with __mnt_drop_write_file.
419eb04c282SJan Kara  */
__mnt_want_write_file(struct file * file)420eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
421eb04c282SJan Kara {
42214e43bf4SEric Biggers 	if (file->f_mode & FMODE_WRITER) {
42314e43bf4SEric Biggers 		/*
42414e43bf4SEric Biggers 		 * Superblock may have become readonly while there are still
42514e43bf4SEric Biggers 		 * writable fd's, e.g. due to a fs error with errors=remount-ro
42614e43bf4SEric Biggers 		 */
42714e43bf4SEric Biggers 		if (__mnt_is_readonly(file->f_path.mnt))
42814e43bf4SEric Biggers 			return -EROFS;
42914e43bf4SEric Biggers 		return 0;
43014e43bf4SEric Biggers 	}
431eb04c282SJan Kara 	return __mnt_want_write(file->f_path.mnt);
432eb04c282SJan Kara }
433eb04c282SJan Kara 
434eb04c282SJan Kara /**
4357c6893e3SMiklos Szeredi  * mnt_want_write_file - get write access to a file's mount
4367c6893e3SMiklos Szeredi  * @file: the file who's mount on which to take a write
4377c6893e3SMiklos Szeredi  *
43814e43bf4SEric Biggers  * This is like mnt_want_write, but if the file is already open for writing it
43914e43bf4SEric Biggers  * skips incrementing mnt_writers (since the open file already has a reference)
44014e43bf4SEric Biggers  * and instead only does the freeze protection and the check for emergency r/o
44114e43bf4SEric Biggers  * remounts.  This must be paired with mnt_drop_write_file.
4427c6893e3SMiklos Szeredi  */
mnt_want_write_file(struct file * file)4437c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file)
4447c6893e3SMiklos Szeredi {
4457c6893e3SMiklos Szeredi 	int ret;
4467c6893e3SMiklos Szeredi 
4477c6893e3SMiklos Szeredi 	sb_start_write(file_inode(file)->i_sb);
4487c6893e3SMiklos Szeredi 	ret = __mnt_want_write_file(file);
4497c6893e3SMiklos Szeredi 	if (ret)
4507c6893e3SMiklos Szeredi 		sb_end_write(file_inode(file)->i_sb);
4517c6893e3SMiklos Szeredi 	return ret;
4527c6893e3SMiklos Szeredi }
45396029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
45496029c4eSnpiggin@suse.de 
45596029c4eSnpiggin@suse.de /**
456eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4578366025eSDave Hansen  * @mnt: the mount on which to give up write access
4588366025eSDave Hansen  *
4598366025eSDave Hansen  * Tells the low-level filesystem that we are done
4608366025eSDave Hansen  * performing writes to it.  Must be matched with
461eb04c282SJan Kara  * __mnt_want_write() call above.
4628366025eSDave Hansen  */
__mnt_drop_write(struct vfsmount * mnt)463eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4648366025eSDave Hansen {
465d3ef3d73Snpiggin@suse.de 	preempt_disable();
46683adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
467d3ef3d73Snpiggin@suse.de 	preempt_enable();
4688366025eSDave Hansen }
469eb04c282SJan Kara 
470eb04c282SJan Kara /**
471eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
472eb04c282SJan Kara  * @mnt: the mount on which to give up write access
473eb04c282SJan Kara  *
474eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
475eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
476eb04c282SJan Kara  * mnt_want_write() call above.
477eb04c282SJan Kara  */
mnt_drop_write(struct vfsmount * mnt)478eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
479eb04c282SJan Kara {
480eb04c282SJan Kara 	__mnt_drop_write(mnt);
481eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
482eb04c282SJan Kara }
4838366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4848366025eSDave Hansen 
__mnt_drop_write_file(struct file * file)485eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
486eb04c282SJan Kara {
48714e43bf4SEric Biggers 	if (!(file->f_mode & FMODE_WRITER))
488eb04c282SJan Kara 		__mnt_drop_write(file->f_path.mnt);
489eb04c282SJan Kara }
490eb04c282SJan Kara 
mnt_drop_write_file(struct file * file)4917c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file)
4927c6893e3SMiklos Szeredi {
493a6795a58SMiklos Szeredi 	__mnt_drop_write_file(file);
4947c6893e3SMiklos Szeredi 	sb_end_write(file_inode(file)->i_sb);
4957c6893e3SMiklos Szeredi }
4962a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4972a79f17eSAl Viro 
498538f4f02SChristian Brauner /**
499538f4f02SChristian Brauner  * mnt_hold_writers - prevent write access to the given mount
500538f4f02SChristian Brauner  * @mnt: mnt to prevent write access to
501538f4f02SChristian Brauner  *
502538f4f02SChristian Brauner  * Prevents write access to @mnt if there are no active writers for @mnt.
503538f4f02SChristian Brauner  * This function needs to be called and return successfully before changing
504538f4f02SChristian Brauner  * properties of @mnt that need to remain stable for callers with write access
505538f4f02SChristian Brauner  * to @mnt.
506538f4f02SChristian Brauner  *
507538f4f02SChristian Brauner  * After this functions has been called successfully callers must pair it with
508538f4f02SChristian Brauner  * a call to mnt_unhold_writers() in order to stop preventing write access to
509538f4f02SChristian Brauner  * @mnt.
510538f4f02SChristian Brauner  *
511538f4f02SChristian Brauner  * Context: This function expects lock_mount_hash() to be held serializing
512538f4f02SChristian Brauner  *          setting MNT_WRITE_HOLD.
513538f4f02SChristian Brauner  * Return: On success 0 is returned.
514538f4f02SChristian Brauner  *	   On error, -EBUSY is returned.
515538f4f02SChristian Brauner  */
mnt_hold_writers(struct mount * mnt)516fbdc2f6cSChristian Brauner static inline int mnt_hold_writers(struct mount *mnt)
5178366025eSDave Hansen {
51883adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
519d3ef3d73Snpiggin@suse.de 	/*
520d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
521d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
522d3ef3d73Snpiggin@suse.de 	 */
523d3ef3d73Snpiggin@suse.de 	smp_mb();
524d3ef3d73Snpiggin@suse.de 
525d3ef3d73Snpiggin@suse.de 	/*
526d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
527d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
528d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
529d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
530d3ef3d73Snpiggin@suse.de 	 *
531d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
532d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
533d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
534d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
535d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
536d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
537d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
538d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
539d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
540d3ef3d73Snpiggin@suse.de 	 */
541c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
542fbdc2f6cSChristian Brauner 		return -EBUSY;
543fbdc2f6cSChristian Brauner 
544fbdc2f6cSChristian Brauner 	return 0;
545fbdc2f6cSChristian Brauner }
546fbdc2f6cSChristian Brauner 
547538f4f02SChristian Brauner /**
548538f4f02SChristian Brauner  * mnt_unhold_writers - stop preventing write access to the given mount
549538f4f02SChristian Brauner  * @mnt: mnt to stop preventing write access to
550538f4f02SChristian Brauner  *
551538f4f02SChristian Brauner  * Stop preventing write access to @mnt allowing callers to gain write access
552538f4f02SChristian Brauner  * to @mnt again.
553538f4f02SChristian Brauner  *
554538f4f02SChristian Brauner  * This function can only be called after a successful call to
555538f4f02SChristian Brauner  * mnt_hold_writers().
556538f4f02SChristian Brauner  *
557538f4f02SChristian Brauner  * Context: This function expects lock_mount_hash() to be held.
558538f4f02SChristian Brauner  */
mnt_unhold_writers(struct mount * mnt)559fbdc2f6cSChristian Brauner static inline void mnt_unhold_writers(struct mount *mnt)
560fbdc2f6cSChristian Brauner {
561d3ef3d73Snpiggin@suse.de 	/*
562d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
563d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
564d3ef3d73Snpiggin@suse.de 	 */
565d3ef3d73Snpiggin@suse.de 	smp_wmb();
56683adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
567fbdc2f6cSChristian Brauner }
568fbdc2f6cSChristian Brauner 
mnt_make_readonly(struct mount * mnt)569fbdc2f6cSChristian Brauner static int mnt_make_readonly(struct mount *mnt)
570fbdc2f6cSChristian Brauner {
571fbdc2f6cSChristian Brauner 	int ret;
572fbdc2f6cSChristian Brauner 
573fbdc2f6cSChristian Brauner 	ret = mnt_hold_writers(mnt);
574fbdc2f6cSChristian Brauner 	if (!ret)
575fbdc2f6cSChristian Brauner 		mnt->mnt.mnt_flags |= MNT_READONLY;
576fbdc2f6cSChristian Brauner 	mnt_unhold_writers(mnt);
5773d733633SDave Hansen 	return ret;
5783d733633SDave Hansen }
5798366025eSDave Hansen 
sb_prepare_remount_readonly(struct super_block * sb)5804ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5814ed5e82fSMiklos Szeredi {
5824ed5e82fSMiklos Szeredi 	struct mount *mnt;
5834ed5e82fSMiklos Szeredi 	int err = 0;
5844ed5e82fSMiklos Szeredi 
5858e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5868e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5878e8b8796SMiklos Szeredi 		return -EBUSY;
5888e8b8796SMiklos Szeredi 
589719ea2fbSAl Viro 	lock_mount_hash();
5904ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5914ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
592ad1844a0SChristian Brauner 			err = mnt_hold_writers(mnt);
593ad1844a0SChristian Brauner 			if (err)
5944ed5e82fSMiklos Szeredi 				break;
5954ed5e82fSMiklos Szeredi 		}
5964ed5e82fSMiklos Szeredi 	}
5978e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5988e8b8796SMiklos Szeredi 		err = -EBUSY;
5998e8b8796SMiklos Szeredi 
600d7439fb1SJan Kara 	if (!err)
601d7439fb1SJan Kara 		sb_start_ro_state_change(sb);
6024ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
6034ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
6044ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
6054ed5e82fSMiklos Szeredi 	}
606719ea2fbSAl Viro 	unlock_mount_hash();
6074ed5e82fSMiklos Szeredi 
6084ed5e82fSMiklos Szeredi 	return err;
6094ed5e82fSMiklos Szeredi }
6104ed5e82fSMiklos Szeredi 
free_vfsmnt(struct mount * mnt)611b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
6121da177e4SLinus Torvalds {
613256c8aedSChristian Brauner 	mnt_idmap_put(mnt_idmap(&mnt->mnt));
614fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
615d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
61668e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
617d3ef3d73Snpiggin@suse.de #endif
618b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
delayed_free_vfsmnt(struct rcu_head * head)6218ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
6228ffcb32eSDavid Howells {
6238ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
6248ffcb32eSDavid Howells }
6258ffcb32eSDavid Howells 
62648a066e7SAl Viro /* call under rcu_read_lock */
__legitimize_mnt(struct vfsmount * bastard,unsigned seq)627294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
62848a066e7SAl Viro {
62948a066e7SAl Viro 	struct mount *mnt;
63048a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
631294d71ffSAl Viro 		return 1;
63248a066e7SAl Viro 	if (bastard == NULL)
633294d71ffSAl Viro 		return 0;
63448a066e7SAl Viro 	mnt = real_mount(bastard);
63548a066e7SAl Viro 	mnt_add_count(mnt, 1);
636119e1ef8SAl Viro 	smp_mb();			// see mntput_no_expire()
63748a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
638294d71ffSAl Viro 		return 0;
63948a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
64048a066e7SAl Viro 		mnt_add_count(mnt, -1);
641294d71ffSAl Viro 		return 1;
64248a066e7SAl Viro 	}
643119e1ef8SAl Viro 	lock_mount_hash();
644119e1ef8SAl Viro 	if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
645119e1ef8SAl Viro 		mnt_add_count(mnt, -1);
646119e1ef8SAl Viro 		unlock_mount_hash();
647119e1ef8SAl Viro 		return 1;
648119e1ef8SAl Viro 	}
649119e1ef8SAl Viro 	unlock_mount_hash();
650119e1ef8SAl Viro 	/* caller will mntput() */
651294d71ffSAl Viro 	return -1;
652294d71ffSAl Viro }
653294d71ffSAl Viro 
654294d71ffSAl Viro /* call under rcu_read_lock */
legitimize_mnt(struct vfsmount * bastard,unsigned seq)6557e4745a0SAl Viro static bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
656294d71ffSAl Viro {
657294d71ffSAl Viro 	int res = __legitimize_mnt(bastard, seq);
658294d71ffSAl Viro 	if (likely(!res))
659294d71ffSAl Viro 		return true;
660294d71ffSAl Viro 	if (unlikely(res < 0)) {
66148a066e7SAl Viro 		rcu_read_unlock();
66248a066e7SAl Viro 		mntput(bastard);
66348a066e7SAl Viro 		rcu_read_lock();
664294d71ffSAl Viro 	}
66548a066e7SAl Viro 	return false;
66648a066e7SAl Viro }
66748a066e7SAl Viro 
668104026c2SChristian Brauner /**
669104026c2SChristian Brauner  * __lookup_mnt - find first child mount
670104026c2SChristian Brauner  * @mnt:	parent mount
671104026c2SChristian Brauner  * @dentry:	mountpoint
672104026c2SChristian Brauner  *
673104026c2SChristian Brauner  * If @mnt has a child mount @c mounted @dentry find and return it.
674104026c2SChristian Brauner  *
675104026c2SChristian Brauner  * Note that the child mount @c need not be unique. There are cases
676104026c2SChristian Brauner  * where shadow mounts are created. For example, during mount
677104026c2SChristian Brauner  * propagation when a source mount @mnt whose root got overmounted by a
678104026c2SChristian Brauner  * mount @o after path lookup but before @namespace_sem could be
679104026c2SChristian Brauner  * acquired gets copied and propagated. So @mnt gets copied including
680104026c2SChristian Brauner  * @o. When @mnt is propagated to a destination mount @d that already
681104026c2SChristian Brauner  * has another mount @n mounted at the same mountpoint then the source
682104026c2SChristian Brauner  * mount @mnt will be tucked beneath @n, i.e., @n will be mounted on
683104026c2SChristian Brauner  * @mnt and @mnt mounted on @d. Now both @n and @o are mounted at @mnt
684104026c2SChristian Brauner  * on @dentry.
685104026c2SChristian Brauner  *
686104026c2SChristian Brauner  * Return: The first child of @mnt mounted @dentry or NULL.
6871da177e4SLinus Torvalds  */
__lookup_mnt(struct vfsmount * mnt,struct dentry * dentry)688474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6891da177e4SLinus Torvalds {
69038129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
691474279dcSAl Viro 	struct mount *p;
6921da177e4SLinus Torvalds 
69338129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
694474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
695474279dcSAl Viro 			return p;
696474279dcSAl Viro 	return NULL;
6971da177e4SLinus Torvalds }
698474279dcSAl Viro 
699474279dcSAl Viro /*
700f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
701f015f126SDavid Howells  *
702f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
703f015f126SDavid Howells  * following mounts:
704f015f126SDavid Howells  *
705f015f126SDavid Howells  * mount /dev/sda1 /mnt
706f015f126SDavid Howells  * mount /dev/sda2 /mnt
707f015f126SDavid Howells  * mount /dev/sda3 /mnt
708f015f126SDavid Howells  *
709f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
710f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
711f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
712f015f126SDavid Howells  *
713f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
714a05964f3SRam Pai  */
lookup_mnt(const struct path * path)715ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path)
716a05964f3SRam Pai {
717c7105365SAl Viro 	struct mount *child_mnt;
71848a066e7SAl Viro 	struct vfsmount *m;
71948a066e7SAl Viro 	unsigned seq;
72099b7db7bSNick Piggin 
72148a066e7SAl Viro 	rcu_read_lock();
72248a066e7SAl Viro 	do {
72348a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
724474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
72548a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
72648a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
72748a066e7SAl Viro 	rcu_read_unlock();
72848a066e7SAl Viro 	return m;
729a05964f3SRam Pai }
730a05964f3SRam Pai 
lock_ns_list(struct mnt_namespace * ns)7319f6c61f9SMiklos Szeredi static inline void lock_ns_list(struct mnt_namespace *ns)
7329f6c61f9SMiklos Szeredi {
7339f6c61f9SMiklos Szeredi 	spin_lock(&ns->ns_lock);
7349f6c61f9SMiklos Szeredi }
7359f6c61f9SMiklos Szeredi 
unlock_ns_list(struct mnt_namespace * ns)7369f6c61f9SMiklos Szeredi static inline void unlock_ns_list(struct mnt_namespace *ns)
7379f6c61f9SMiklos Szeredi {
7389f6c61f9SMiklos Szeredi 	spin_unlock(&ns->ns_lock);
7399f6c61f9SMiklos Szeredi }
7409f6c61f9SMiklos Szeredi 
mnt_is_cursor(struct mount * mnt)7419f6c61f9SMiklos Szeredi static inline bool mnt_is_cursor(struct mount *mnt)
7429f6c61f9SMiklos Szeredi {
7439f6c61f9SMiklos Szeredi 	return mnt->mnt.mnt_flags & MNT_CURSOR;
7449f6c61f9SMiklos Szeredi }
7459f6c61f9SMiklos Szeredi 
7467af1364fSEric W. Biederman /*
7477af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
7487af1364fSEric W. Biederman  *                         current mount namespace.
7497af1364fSEric W. Biederman  *
7507af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
7517af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
7527af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
7537af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
7547af1364fSEric W. Biederman  * is a mountpoint.
7557af1364fSEric W. Biederman  *
7567af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
7577af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
7587af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
7597af1364fSEric W. Biederman  * parent mount.
7607af1364fSEric W. Biederman  */
__is_local_mountpoint(struct dentry * dentry)7617af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
7627af1364fSEric W. Biederman {
7637af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
7647af1364fSEric W. Biederman 	struct mount *mnt;
7657af1364fSEric W. Biederman 	bool is_covered = false;
7667af1364fSEric W. Biederman 
7677af1364fSEric W. Biederman 	down_read(&namespace_sem);
7689f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
7697af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
7709f6c61f9SMiklos Szeredi 		if (mnt_is_cursor(mnt))
7719f6c61f9SMiklos Szeredi 			continue;
7727af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
7737af1364fSEric W. Biederman 		if (is_covered)
7747af1364fSEric W. Biederman 			break;
7757af1364fSEric W. Biederman 	}
7769f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
7777af1364fSEric W. Biederman 	up_read(&namespace_sem);
7785ad05cc8SNikolay Borisov 
7797af1364fSEric W. Biederman 	return is_covered;
7807af1364fSEric W. Biederman }
7817af1364fSEric W. Biederman 
lookup_mountpoint(struct dentry * dentry)782e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
78384d17192SAl Viro {
7840818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
78584d17192SAl Viro 	struct mountpoint *mp;
78684d17192SAl Viro 
7870818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
78884d17192SAl Viro 		if (mp->m_dentry == dentry) {
78984d17192SAl Viro 			mp->m_count++;
79084d17192SAl Viro 			return mp;
79184d17192SAl Viro 		}
79284d17192SAl Viro 	}
793e2dfa935SEric W. Biederman 	return NULL;
794e2dfa935SEric W. Biederman }
795e2dfa935SEric W. Biederman 
get_mountpoint(struct dentry * dentry)7963895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry)
797e2dfa935SEric W. Biederman {
7983895dbf8SEric W. Biederman 	struct mountpoint *mp, *new = NULL;
799e2dfa935SEric W. Biederman 	int ret;
80084d17192SAl Viro 
8013895dbf8SEric W. Biederman 	if (d_mountpoint(dentry)) {
8021e9c75fbSBenjamin Coddington 		/* might be worth a WARN_ON() */
8031e9c75fbSBenjamin Coddington 		if (d_unlinked(dentry))
8041e9c75fbSBenjamin Coddington 			return ERR_PTR(-ENOENT);
8053895dbf8SEric W. Biederman mountpoint:
8063895dbf8SEric W. Biederman 		read_seqlock_excl(&mount_lock);
8073895dbf8SEric W. Biederman 		mp = lookup_mountpoint(dentry);
8083895dbf8SEric W. Biederman 		read_sequnlock_excl(&mount_lock);
8093895dbf8SEric W. Biederman 		if (mp)
8103895dbf8SEric W. Biederman 			goto done;
81184d17192SAl Viro 	}
812eed81007SMiklos Szeredi 
8133895dbf8SEric W. Biederman 	if (!new)
8143895dbf8SEric W. Biederman 		new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
8153895dbf8SEric W. Biederman 	if (!new)
8163895dbf8SEric W. Biederman 		return ERR_PTR(-ENOMEM);
8173895dbf8SEric W. Biederman 
8183895dbf8SEric W. Biederman 
8193895dbf8SEric W. Biederman 	/* Exactly one processes may set d_mounted */
8203895dbf8SEric W. Biederman 	ret = d_set_mounted(dentry);
8213895dbf8SEric W. Biederman 
8223895dbf8SEric W. Biederman 	/* Someone else set d_mounted? */
8233895dbf8SEric W. Biederman 	if (ret == -EBUSY)
8243895dbf8SEric W. Biederman 		goto mountpoint;
8253895dbf8SEric W. Biederman 
8263895dbf8SEric W. Biederman 	/* The dentry is not available as a mountpoint? */
8273895dbf8SEric W. Biederman 	mp = ERR_PTR(ret);
8283895dbf8SEric W. Biederman 	if (ret)
8293895dbf8SEric W. Biederman 		goto done;
8303895dbf8SEric W. Biederman 
8313895dbf8SEric W. Biederman 	/* Add the new mountpoint to the hash table */
8323895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
8334edbe133SAl Viro 	new->m_dentry = dget(dentry);
8343895dbf8SEric W. Biederman 	new->m_count = 1;
8353895dbf8SEric W. Biederman 	hlist_add_head(&new->m_hash, mp_hash(dentry));
8363895dbf8SEric W. Biederman 	INIT_HLIST_HEAD(&new->m_list);
8373895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
8383895dbf8SEric W. Biederman 
8393895dbf8SEric W. Biederman 	mp = new;
8403895dbf8SEric W. Biederman 	new = NULL;
8413895dbf8SEric W. Biederman done:
8423895dbf8SEric W. Biederman 	kfree(new);
84384d17192SAl Viro 	return mp;
84484d17192SAl Viro }
84584d17192SAl Viro 
8464edbe133SAl Viro /*
8474edbe133SAl Viro  * vfsmount lock must be held.  Additionally, the caller is responsible
8484edbe133SAl Viro  * for serializing calls for given disposal list.
8494edbe133SAl Viro  */
__put_mountpoint(struct mountpoint * mp,struct list_head * list)8504edbe133SAl Viro static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
85184d17192SAl Viro {
85284d17192SAl Viro 	if (!--mp->m_count) {
85384d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
8540a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
85584d17192SAl Viro 		spin_lock(&dentry->d_lock);
85684d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
85784d17192SAl Viro 		spin_unlock(&dentry->d_lock);
8584edbe133SAl Viro 		dput_to_list(dentry, list);
8590818bf27SAl Viro 		hlist_del(&mp->m_hash);
86084d17192SAl Viro 		kfree(mp);
86184d17192SAl Viro 	}
86284d17192SAl Viro }
86384d17192SAl Viro 
8644edbe133SAl Viro /* called with namespace_lock and vfsmount lock */
put_mountpoint(struct mountpoint * mp)8654edbe133SAl Viro static void put_mountpoint(struct mountpoint *mp)
8664edbe133SAl Viro {
8674edbe133SAl Viro 	__put_mountpoint(mp, &ex_mountpoints);
8684edbe133SAl Viro }
8694edbe133SAl Viro 
check_mnt(struct mount * mnt)870143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
8711da177e4SLinus Torvalds {
8726b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
87599b7db7bSNick Piggin /*
87699b7db7bSNick Piggin  * vfsmount lock must be held for write
87799b7db7bSNick Piggin  */
touch_mnt_namespace(struct mnt_namespace * ns)8786b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
8795addc5ddSAl Viro {
8805addc5ddSAl Viro 	if (ns) {
8815addc5ddSAl Viro 		ns->event = ++event;
8825addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
8835addc5ddSAl Viro 	}
8845addc5ddSAl Viro }
8855addc5ddSAl Viro 
88699b7db7bSNick Piggin /*
88799b7db7bSNick Piggin  * vfsmount lock must be held for write
88899b7db7bSNick Piggin  */
__touch_mnt_namespace(struct mnt_namespace * ns)8896b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
8905addc5ddSAl Viro {
8915addc5ddSAl Viro 	if (ns && ns->event != event) {
8925addc5ddSAl Viro 		ns->event = event;
8935addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
8945addc5ddSAl Viro 	}
8955addc5ddSAl Viro }
8965addc5ddSAl Viro 
89799b7db7bSNick Piggin /*
89899b7db7bSNick Piggin  * vfsmount lock must be held for write
89999b7db7bSNick Piggin  */
unhash_mnt(struct mount * mnt)900e4e59906SAl Viro static struct mountpoint *unhash_mnt(struct mount *mnt)
9011da177e4SLinus Torvalds {
902e4e59906SAl Viro 	struct mountpoint *mp;
9030714a533SAl Viro 	mnt->mnt_parent = mnt;
904a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
9056b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
90638129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
9070a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
908e4e59906SAl Viro 	mp = mnt->mnt_mp;
90984d17192SAl Viro 	mnt->mnt_mp = NULL;
910e4e59906SAl Viro 	return mp;
9117bdb11deSEric W. Biederman }
9127bdb11deSEric W. Biederman 
9137bdb11deSEric W. Biederman /*
9147bdb11deSEric W. Biederman  * vfsmount lock must be held for write
9157bdb11deSEric W. Biederman  */
umount_mnt(struct mount * mnt)9166a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt)
9176a46c573SEric W. Biederman {
918e4e59906SAl Viro 	put_mountpoint(unhash_mnt(mnt));
9196a46c573SEric W. Biederman }
9206a46c573SEric W. Biederman 
9216a46c573SEric W. Biederman /*
9226a46c573SEric W. Biederman  * vfsmount lock must be held for write
9236a46c573SEric W. Biederman  */
mnt_set_mountpoint(struct mount * mnt,struct mountpoint * mp,struct mount * child_mnt)92484d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
92584d17192SAl Viro 			struct mountpoint *mp,
92644d964d6SAl Viro 			struct mount *child_mnt)
927b90fa9aeSRam Pai {
92884d17192SAl Viro 	mp->m_count++;
9293a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
9304edbe133SAl Viro 	child_mnt->mnt_mountpoint = mp->m_dentry;
9313a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
93284d17192SAl Viro 	child_mnt->mnt_mp = mp;
9330a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
934b90fa9aeSRam Pai }
935b90fa9aeSRam Pai 
9366ac39281SChristian Brauner /**
9376ac39281SChristian Brauner  * mnt_set_mountpoint_beneath - mount a mount beneath another one
9386ac39281SChristian Brauner  *
9396ac39281SChristian Brauner  * @new_parent: the source mount
9406ac39281SChristian Brauner  * @top_mnt:    the mount beneath which @new_parent is mounted
9416ac39281SChristian Brauner  * @new_mp:     the new mountpoint of @top_mnt on @new_parent
9426ac39281SChristian Brauner  *
9436ac39281SChristian Brauner  * Remove @top_mnt from its current mountpoint @top_mnt->mnt_mp and
9446ac39281SChristian Brauner  * parent @top_mnt->mnt_parent and mount it on top of @new_parent at
9456ac39281SChristian Brauner  * @new_mp. And mount @new_parent on the old parent and old
9466ac39281SChristian Brauner  * mountpoint of @top_mnt.
9476ac39281SChristian Brauner  *
9486ac39281SChristian Brauner  * Context: This function expects namespace_lock() and lock_mount_hash()
9496ac39281SChristian Brauner  *          to have been acquired in that order.
9506ac39281SChristian Brauner  */
mnt_set_mountpoint_beneath(struct mount * new_parent,struct mount * top_mnt,struct mountpoint * new_mp)9516ac39281SChristian Brauner static void mnt_set_mountpoint_beneath(struct mount *new_parent,
9526ac39281SChristian Brauner 				       struct mount *top_mnt,
9536ac39281SChristian Brauner 				       struct mountpoint *new_mp)
9546ac39281SChristian Brauner {
9556ac39281SChristian Brauner 	struct mount *old_top_parent = top_mnt->mnt_parent;
9566ac39281SChristian Brauner 	struct mountpoint *old_top_mp = top_mnt->mnt_mp;
9576ac39281SChristian Brauner 
9586ac39281SChristian Brauner 	mnt_set_mountpoint(old_top_parent, old_top_mp, new_parent);
9596ac39281SChristian Brauner 	mnt_change_mountpoint(new_parent, new_mp, top_mnt);
9606ac39281SChristian Brauner }
9616ac39281SChristian Brauner 
9626ac39281SChristian Brauner 
__attach_mnt(struct mount * mnt,struct mount * parent)9631064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent)
9641064f874SEric W. Biederman {
9651064f874SEric W. Biederman 	hlist_add_head_rcu(&mnt->mnt_hash,
9661064f874SEric W. Biederman 			   m_hash(&parent->mnt, mnt->mnt_mountpoint));
9671064f874SEric W. Biederman 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
9681064f874SEric W. Biederman }
9691064f874SEric W. Biederman 
9706ac39281SChristian Brauner /**
9716ac39281SChristian Brauner  * attach_mnt - mount a mount, attach to @mount_hashtable and parent's
9726ac39281SChristian Brauner  *              list of child mounts
9736ac39281SChristian Brauner  * @parent:  the parent
9746ac39281SChristian Brauner  * @mnt:     the new mount
9756ac39281SChristian Brauner  * @mp:      the new mountpoint
9766ac39281SChristian Brauner  * @beneath: whether to mount @mnt beneath or on top of @parent
9776ac39281SChristian Brauner  *
9786ac39281SChristian Brauner  * If @beneath is false, mount @mnt at @mp on @parent. Then attach @mnt
9796ac39281SChristian Brauner  * to @parent's child mount list and to @mount_hashtable.
9806ac39281SChristian Brauner  *
9816ac39281SChristian Brauner  * If @beneath is true, remove @mnt from its current parent and
9826ac39281SChristian Brauner  * mountpoint and mount it on @mp on @parent, and mount @parent on the
9836ac39281SChristian Brauner  * old parent and old mountpoint of @mnt. Finally, attach @parent to
9846ac39281SChristian Brauner  * @mnt_hashtable and @parent->mnt_parent->mnt_mounts.
9856ac39281SChristian Brauner  *
9866ac39281SChristian Brauner  * Note, when __attach_mnt() is called @mnt->mnt_parent already points
9876ac39281SChristian Brauner  * to the correct parent.
9886ac39281SChristian Brauner  *
9896ac39281SChristian Brauner  * Context: This function expects namespace_lock() and lock_mount_hash()
9906ac39281SChristian Brauner  *          to have been acquired in that order.
99199b7db7bSNick Piggin  */
attach_mnt(struct mount * mnt,struct mount * parent,struct mountpoint * mp,bool beneath)9926ac39281SChristian Brauner static void attach_mnt(struct mount *mnt, struct mount *parent,
9936ac39281SChristian Brauner 		       struct mountpoint *mp, bool beneath)
9941da177e4SLinus Torvalds {
9956ac39281SChristian Brauner 	if (beneath)
9966ac39281SChristian Brauner 		mnt_set_mountpoint_beneath(mnt, parent, mp);
9976ac39281SChristian Brauner 	else
99884d17192SAl Viro 		mnt_set_mountpoint(parent, mp, mnt);
9996ac39281SChristian Brauner 	/*
10006ac39281SChristian Brauner 	 * Note, @mnt->mnt_parent has to be used. If @mnt was mounted
10016ac39281SChristian Brauner 	 * beneath @parent then @mnt will need to be attached to
10026ac39281SChristian Brauner 	 * @parent's old parent, not @parent. IOW, @mnt->mnt_parent
10036ac39281SChristian Brauner 	 * isn't the same mount as @parent.
10046ac39281SChristian Brauner 	 */
10056ac39281SChristian Brauner 	__attach_mnt(mnt, mnt->mnt_parent);
1006b90fa9aeSRam Pai }
1007b90fa9aeSRam Pai 
mnt_change_mountpoint(struct mount * parent,struct mountpoint * mp,struct mount * mnt)10081064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
100912a5b529SAl Viro {
10101064f874SEric W. Biederman 	struct mountpoint *old_mp = mnt->mnt_mp;
10111064f874SEric W. Biederman 	struct mount *old_parent = mnt->mnt_parent;
10121064f874SEric W. Biederman 
10131064f874SEric W. Biederman 	list_del_init(&mnt->mnt_child);
10141064f874SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
10151064f874SEric W. Biederman 	hlist_del_init_rcu(&mnt->mnt_hash);
10161064f874SEric W. Biederman 
10176ac39281SChristian Brauner 	attach_mnt(mnt, parent, mp, false);
10181064f874SEric W. Biederman 
10191064f874SEric W. Biederman 	put_mountpoint(old_mp);
10201064f874SEric W. Biederman 	mnt_add_count(old_parent, -1);
102112a5b529SAl Viro }
102212a5b529SAl Viro 
1023b90fa9aeSRam Pai /*
102499b7db7bSNick Piggin  * vfsmount lock must be held for write
1025b90fa9aeSRam Pai  */
commit_tree(struct mount * mnt)10261064f874SEric W. Biederman static void commit_tree(struct mount *mnt)
1027b90fa9aeSRam Pai {
10280714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
102983adc753SAl Viro 	struct mount *m;
1030b90fa9aeSRam Pai 	LIST_HEAD(head);
1031143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
1032b90fa9aeSRam Pai 
10330714a533SAl Viro 	BUG_ON(parent == mnt);
1034b90fa9aeSRam Pai 
10351a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
1036f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
1037143c8c91SAl Viro 		m->mnt_ns = n;
1038f03c6599SAl Viro 
1039b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
1040b90fa9aeSRam Pai 
1041d2921684SEric W. Biederman 	n->mounts += n->pending_mounts;
1042d2921684SEric W. Biederman 	n->pending_mounts = 0;
1043d2921684SEric W. Biederman 
10441064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
10456b3286edSKirill Korotaev 	touch_mnt_namespace(n);
10461da177e4SLinus Torvalds }
10471da177e4SLinus Torvalds 
next_mnt(struct mount * p,struct mount * root)1048909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
10491da177e4SLinus Torvalds {
10506b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
10516b41d536SAl Viro 	if (next == &p->mnt_mounts) {
10521da177e4SLinus Torvalds 		while (1) {
1053909b0a88SAl Viro 			if (p == root)
10541da177e4SLinus Torvalds 				return NULL;
10556b41d536SAl Viro 			next = p->mnt_child.next;
10566b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
10571da177e4SLinus Torvalds 				break;
10580714a533SAl Viro 			p = p->mnt_parent;
10591da177e4SLinus Torvalds 		}
10601da177e4SLinus Torvalds 	}
10616b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
skip_mnt_tree(struct mount * p)1064315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
10659676f0c6SRam Pai {
10666b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
10676b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
10686b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
10696b41d536SAl Viro 		prev = p->mnt_mounts.prev;
10709676f0c6SRam Pai 	}
10719676f0c6SRam Pai 	return p;
10729676f0c6SRam Pai }
10739676f0c6SRam Pai 
10748f291889SAl Viro /**
10758f291889SAl Viro  * vfs_create_mount - Create a mount for a configured superblock
10768f291889SAl Viro  * @fc: The configuration context with the superblock attached
10778f291889SAl Viro  *
10788f291889SAl Viro  * Create a mount to an already configured superblock.  If necessary, the
10798f291889SAl Viro  * caller should invoke vfs_get_tree() before calling this.
10808f291889SAl Viro  *
10818f291889SAl Viro  * Note that this does not attach the mount to anything.
10828f291889SAl Viro  */
vfs_create_mount(struct fs_context * fc)10838f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc)
10849d412a43SAl Viro {
1085b105e270SAl Viro 	struct mount *mnt;
10869d412a43SAl Viro 
10878f291889SAl Viro 	if (!fc->root)
10888f291889SAl Viro 		return ERR_PTR(-EINVAL);
10899d412a43SAl Viro 
10908f291889SAl Viro 	mnt = alloc_vfsmnt(fc->source ?: "none");
10919d412a43SAl Viro 	if (!mnt)
10929d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
10939d412a43SAl Viro 
10948f291889SAl Viro 	if (fc->sb_flags & SB_KERNMOUNT)
1095b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
10969d412a43SAl Viro 
10978f291889SAl Viro 	atomic_inc(&fc->root->d_sb->s_active);
10988f291889SAl Viro 	mnt->mnt.mnt_sb		= fc->root->d_sb;
10998f291889SAl Viro 	mnt->mnt.mnt_root	= dget(fc->root);
1100a73324daSAl Viro 	mnt->mnt_mountpoint	= mnt->mnt.mnt_root;
11010714a533SAl Viro 	mnt->mnt_parent		= mnt;
11028f291889SAl Viro 
1103719ea2fbSAl Viro 	lock_mount_hash();
11048f291889SAl Viro 	list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
1105719ea2fbSAl Viro 	unlock_mount_hash();
1106b105e270SAl Viro 	return &mnt->mnt;
11079d412a43SAl Viro }
11088f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount);
11098f291889SAl Viro 
fc_mount(struct fs_context * fc)11108f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc)
11118f291889SAl Viro {
11128f291889SAl Viro 	int err = vfs_get_tree(fc);
11138f291889SAl Viro 	if (!err) {
11148f291889SAl Viro 		up_write(&fc->root->d_sb->s_umount);
11158f291889SAl Viro 		return vfs_create_mount(fc);
11168f291889SAl Viro 	}
11178f291889SAl Viro 	return ERR_PTR(err);
11188f291889SAl Viro }
11198f291889SAl Viro EXPORT_SYMBOL(fc_mount);
11208f291889SAl Viro 
vfs_kern_mount(struct file_system_type * type,int flags,const char * name,void * data)11219bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type,
11229bc61ab1SDavid Howells 				int flags, const char *name,
11239bc61ab1SDavid Howells 				void *data)
11241da177e4SLinus Torvalds {
11259bc61ab1SDavid Howells 	struct fs_context *fc;
11268f291889SAl Viro 	struct vfsmount *mnt;
11279bc61ab1SDavid Howells 	int ret = 0;
11289d412a43SAl Viro 
11299d412a43SAl Viro 	if (!type)
11303e1aeb00SDavid Howells 		return ERR_PTR(-EINVAL);
11319d412a43SAl Viro 
11329bc61ab1SDavid Howells 	fc = fs_context_for_mount(type, flags);
11339bc61ab1SDavid Howells 	if (IS_ERR(fc))
11349bc61ab1SDavid Howells 		return ERR_CAST(fc);
11359bc61ab1SDavid Howells 
11363e1aeb00SDavid Howells 	if (name)
11373e1aeb00SDavid Howells 		ret = vfs_parse_fs_string(fc, "source",
11383e1aeb00SDavid Howells 					  name, strlen(name));
11399bc61ab1SDavid Howells 	if (!ret)
11409bc61ab1SDavid Howells 		ret = parse_monolithic_mount_data(fc, data);
11419bc61ab1SDavid Howells 	if (!ret)
11428f291889SAl Viro 		mnt = fc_mount(fc);
11438f291889SAl Viro 	else
11448f291889SAl Viro 		mnt = ERR_PTR(ret);
11459d412a43SAl Viro 
11469bc61ab1SDavid Howells 	put_fs_context(fc);
11478f291889SAl Viro 	return mnt;
11489d412a43SAl Viro }
11499d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
11509d412a43SAl Viro 
115193faccbbSEric W. Biederman struct vfsmount *
vfs_submount(const struct dentry * mountpoint,struct file_system_type * type,const char * name,void * data)115293faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
115393faccbbSEric W. Biederman 	     const char *name, void *data)
115493faccbbSEric W. Biederman {
115593faccbbSEric W. Biederman 	/* Until it is worked out how to pass the user namespace
115693faccbbSEric W. Biederman 	 * through from the parent mount to the submount don't support
115793faccbbSEric W. Biederman 	 * unprivileged mounts with submounts.
115893faccbbSEric W. Biederman 	 */
115993faccbbSEric W. Biederman 	if (mountpoint->d_sb->s_user_ns != &init_user_ns)
116093faccbbSEric W. Biederman 		return ERR_PTR(-EPERM);
116193faccbbSEric W. Biederman 
1162e462ec50SDavid Howells 	return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
116393faccbbSEric W. Biederman }
116493faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount);
116593faccbbSEric W. Biederman 
clone_mnt(struct mount * old,struct dentry * root,int flag)116687129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
116736341f64SRam Pai 					int flag)
11681da177e4SLinus Torvalds {
116987129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
1170be34d1a3SDavid Howells 	struct mount *mnt;
1171be34d1a3SDavid Howells 	int err;
11721da177e4SLinus Torvalds 
1173be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
1174be34d1a3SDavid Howells 	if (!mnt)
1175be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
1176be34d1a3SDavid Howells 
11777a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
117815169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
1179719f5d7fSMiklos Szeredi 	else
118015169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
1181719f5d7fSMiklos Szeredi 
118215169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1183be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
1184719f5d7fSMiklos Szeredi 		if (err)
1185719f5d7fSMiklos Szeredi 			goto out_free;
1186719f5d7fSMiklos Szeredi 	}
1187719f5d7fSMiklos Szeredi 
118816a34adbSAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags;
118916a34adbSAl Viro 	mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
11905ff9d8a6SEric W. Biederman 
11911da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1192256c8aedSChristian Brauner 	mnt->mnt.mnt_idmap = mnt_idmap_get(mnt_idmap(&old->mnt));
1193256c8aedSChristian Brauner 
1194b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1195b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1196a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
11970714a533SAl Viro 	mnt->mnt_parent = mnt;
1198719ea2fbSAl Viro 	lock_mount_hash();
119939f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1200719ea2fbSAl Viro 	unlock_mount_hash();
1201b90fa9aeSRam Pai 
12027a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
12037a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
12046776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
120532301920SAl Viro 		mnt->mnt_master = old;
1206fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
12078aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1208fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
12096776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1210d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
12116776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1212d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
12135235d448SAl Viro 	} else {
12145235d448SAl Viro 		CLEAR_MNT_SHARED(mnt);
12155afe0022SRam Pai 	}
1216b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
12170f0afb1dSAl Viro 		set_mnt_shared(mnt);
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
12201da177e4SLinus Torvalds 	 * as the original if that was on one */
122136341f64SRam Pai 	if (flag & CL_EXPIRE) {
12226776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
12236776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
12241da177e4SLinus Torvalds 	}
1225be34d1a3SDavid Howells 
1226cb338d06SAl Viro 	return mnt;
1227719f5d7fSMiklos Szeredi 
1228719f5d7fSMiklos Szeredi  out_free:
12298ffcb32eSDavid Howells 	mnt_free_id(mnt);
1230719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1231be34d1a3SDavid Howells 	return ERR_PTR(err);
12321da177e4SLinus Torvalds }
12331da177e4SLinus Torvalds 
cleanup_mnt(struct mount * mnt)12349ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
12359ea459e1SAl Viro {
123656cbb429SAl Viro 	struct hlist_node *p;
123756cbb429SAl Viro 	struct mount *m;
12389ea459e1SAl Viro 	/*
123956cbb429SAl Viro 	 * The warning here probably indicates that somebody messed
124056cbb429SAl Viro 	 * up a mnt_want/drop_write() pair.  If this happens, the
124156cbb429SAl Viro 	 * filesystem was probably unable to make r/w->r/o transitions.
12429ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
12439ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
12449ea459e1SAl Viro 	 */
12459ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
12469ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
12479ea459e1SAl Viro 		mnt_pin_kill(mnt);
124856cbb429SAl Viro 	hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
124956cbb429SAl Viro 		hlist_del(&m->mnt_umount);
125056cbb429SAl Viro 		mntput(&m->mnt);
125156cbb429SAl Viro 	}
12529ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
12539ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
12549ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
12559ea459e1SAl Viro 	mnt_free_id(mnt);
12569ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
12579ea459e1SAl Viro }
12589ea459e1SAl Viro 
__cleanup_mnt(struct rcu_head * head)12599ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
12609ea459e1SAl Viro {
12619ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
12629ea459e1SAl Viro }
12639ea459e1SAl Viro 
12649ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
delayed_mntput(struct work_struct * unused)12659ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
12669ea459e1SAl Viro {
12679ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
126829785735SByungchul Park 	struct mount *m, *t;
12699ea459e1SAl Viro 
127029785735SByungchul Park 	llist_for_each_entry_safe(m, t, node, mnt_llist)
127129785735SByungchul Park 		cleanup_mnt(m);
12729ea459e1SAl Viro }
12739ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
12749ea459e1SAl Viro 
mntput_no_expire(struct mount * mnt)1275900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
12767b7b1aceSAl Viro {
12774edbe133SAl Viro 	LIST_HEAD(list);
1278edf7ddbfSEric Biggers 	int count;
12794edbe133SAl Viro 
128048a066e7SAl Viro 	rcu_read_lock();
12819ea0a46cSAl Viro 	if (likely(READ_ONCE(mnt->mnt_ns))) {
12829ea0a46cSAl Viro 		/*
12839ea0a46cSAl Viro 		 * Since we don't do lock_mount_hash() here,
12849ea0a46cSAl Viro 		 * ->mnt_ns can change under us.  However, if it's
12859ea0a46cSAl Viro 		 * non-NULL, then there's a reference that won't
12869ea0a46cSAl Viro 		 * be dropped until after an RCU delay done after
12879ea0a46cSAl Viro 		 * turning ->mnt_ns NULL.  So if we observe it
12889ea0a46cSAl Viro 		 * non-NULL under rcu_read_lock(), the reference
12899ea0a46cSAl Viro 		 * we are dropping is not the final one.
12909ea0a46cSAl Viro 		 */
1291aa9c0e07SAl Viro 		mnt_add_count(mnt, -1);
129248a066e7SAl Viro 		rcu_read_unlock();
129399b7db7bSNick Piggin 		return;
1294b3e19d92SNick Piggin 	}
1295719ea2fbSAl Viro 	lock_mount_hash();
1296119e1ef8SAl Viro 	/*
1297119e1ef8SAl Viro 	 * make sure that if __legitimize_mnt() has not seen us grab
1298119e1ef8SAl Viro 	 * mount_lock, we'll see their refcount increment here.
1299119e1ef8SAl Viro 	 */
1300119e1ef8SAl Viro 	smp_mb();
13019ea0a46cSAl Viro 	mnt_add_count(mnt, -1);
1302edf7ddbfSEric Biggers 	count = mnt_get_count(mnt);
1303edf7ddbfSEric Biggers 	if (count != 0) {
1304edf7ddbfSEric Biggers 		WARN_ON(count < 0);
130548a066e7SAl Viro 		rcu_read_unlock();
1306719ea2fbSAl Viro 		unlock_mount_hash();
130799b7db7bSNick Piggin 		return;
130899b7db7bSNick Piggin 	}
130948a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
131048a066e7SAl Viro 		rcu_read_unlock();
131148a066e7SAl Viro 		unlock_mount_hash();
131248a066e7SAl Viro 		return;
131348a066e7SAl Viro 	}
131448a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
131548a066e7SAl Viro 	rcu_read_unlock();
1316962830dfSAndi Kleen 
131739f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1318ce07d891SEric W. Biederman 
1319ce07d891SEric W. Biederman 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1320ce07d891SEric W. Biederman 		struct mount *p, *tmp;
1321ce07d891SEric W. Biederman 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
13224edbe133SAl Viro 			__put_mountpoint(unhash_mnt(p), &list);
132356cbb429SAl Viro 			hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
1324ce07d891SEric W. Biederman 		}
1325ce07d891SEric W. Biederman 	}
1326719ea2fbSAl Viro 	unlock_mount_hash();
13274edbe133SAl Viro 	shrink_dentry_list(&list);
1328649a795aSAl Viro 
13299ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
13309ea459e1SAl Viro 		struct task_struct *task = current;
13319ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
13329ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
133391989c70SJens Axboe 			if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
13349ea459e1SAl Viro 				return;
13359ea459e1SAl Viro 		}
13369ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
13379ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
13389ea459e1SAl Viro 		return;
13399ea459e1SAl Viro 	}
13409ea459e1SAl Viro 	cleanup_mnt(mnt);
1341b3e19d92SNick Piggin }
1342b3e19d92SNick Piggin 
mntput(struct vfsmount * mnt)1343b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1344b3e19d92SNick Piggin {
1345b3e19d92SNick Piggin 	if (mnt) {
1346863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1347b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1348863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1349863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1350863d684fSAl Viro 		mntput_no_expire(m);
1351b3e19d92SNick Piggin 	}
1352b3e19d92SNick Piggin }
1353b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1354b3e19d92SNick Piggin 
mntget(struct vfsmount * mnt)1355b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1356b3e19d92SNick Piggin {
1357b3e19d92SNick Piggin 	if (mnt)
135883adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1359b3e19d92SNick Piggin 	return mnt;
1360b3e19d92SNick Piggin }
1361b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1362b3e19d92SNick Piggin 
1363da27f796SRik van Riel /*
1364da27f796SRik van Riel  * Make a mount point inaccessible to new lookups.
1365da27f796SRik van Riel  * Because there may still be current users, the caller MUST WAIT
1366da27f796SRik van Riel  * for an RCU grace period before destroying the mount point.
1367da27f796SRik van Riel  */
mnt_make_shortterm(struct vfsmount * mnt)1368da27f796SRik van Riel void mnt_make_shortterm(struct vfsmount *mnt)
1369da27f796SRik van Riel {
1370da27f796SRik van Riel 	if (mnt)
1371da27f796SRik van Riel 		real_mount(mnt)->mnt_ns = NULL;
1372da27f796SRik van Riel }
1373da27f796SRik van Riel 
13741f287bc4SRandy Dunlap /**
13751f287bc4SRandy Dunlap  * path_is_mountpoint() - Check if path is a mount in the current namespace.
13761f287bc4SRandy Dunlap  * @path: path to check
1377c6609c0aSIan Kent  *
1378c6609c0aSIan Kent  *  d_mountpoint() can only be used reliably to establish if a dentry is
1379c6609c0aSIan Kent  *  not mounted in any namespace and that common case is handled inline.
1380c6609c0aSIan Kent  *  d_mountpoint() isn't aware of the possibility there may be multiple
1381c6609c0aSIan Kent  *  mounts using a given dentry in a different namespace. This function
1382c6609c0aSIan Kent  *  checks if the passed in path is a mountpoint rather than the dentry
1383c6609c0aSIan Kent  *  alone.
1384c6609c0aSIan Kent  */
path_is_mountpoint(const struct path * path)1385c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path)
1386c6609c0aSIan Kent {
1387c6609c0aSIan Kent 	unsigned seq;
1388c6609c0aSIan Kent 	bool res;
1389c6609c0aSIan Kent 
1390c6609c0aSIan Kent 	if (!d_mountpoint(path->dentry))
1391c6609c0aSIan Kent 		return false;
1392c6609c0aSIan Kent 
1393c6609c0aSIan Kent 	rcu_read_lock();
1394c6609c0aSIan Kent 	do {
1395c6609c0aSIan Kent 		seq = read_seqbegin(&mount_lock);
1396c6609c0aSIan Kent 		res = __path_is_mountpoint(path);
1397c6609c0aSIan Kent 	} while (read_seqretry(&mount_lock, seq));
1398c6609c0aSIan Kent 	rcu_read_unlock();
1399c6609c0aSIan Kent 
1400c6609c0aSIan Kent 	return res;
1401c6609c0aSIan Kent }
1402c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint);
1403c6609c0aSIan Kent 
mnt_clone_internal(const struct path * path)1404ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path)
14057b7b1aceSAl Viro {
14063064c356SAl Viro 	struct mount *p;
14073064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
14083064c356SAl Viro 	if (IS_ERR(p))
14093064c356SAl Viro 		return ERR_CAST(p);
14103064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
14113064c356SAl Viro 	return &p->mnt;
14127b7b1aceSAl Viro }
14131da177e4SLinus Torvalds 
1414a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
mnt_list_next(struct mnt_namespace * ns,struct list_head * p)14159f6c61f9SMiklos Szeredi static struct mount *mnt_list_next(struct mnt_namespace *ns,
14169f6c61f9SMiklos Szeredi 				   struct list_head *p)
14179f6c61f9SMiklos Szeredi {
14189f6c61f9SMiklos Szeredi 	struct mount *mnt, *ret = NULL;
14199f6c61f9SMiklos Szeredi 
14209f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
14219f6c61f9SMiklos Szeredi 	list_for_each_continue(p, &ns->list) {
14229f6c61f9SMiklos Szeredi 		mnt = list_entry(p, typeof(*mnt), mnt_list);
14239f6c61f9SMiklos Szeredi 		if (!mnt_is_cursor(mnt)) {
14249f6c61f9SMiklos Szeredi 			ret = mnt;
14259f6c61f9SMiklos Szeredi 			break;
14269f6c61f9SMiklos Szeredi 		}
14279f6c61f9SMiklos Szeredi 	}
14289f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
14299f6c61f9SMiklos Szeredi 
14309f6c61f9SMiklos Szeredi 	return ret;
14319f6c61f9SMiklos Szeredi }
14329f6c61f9SMiklos Szeredi 
14330226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
m_start(struct seq_file * m,loff_t * pos)14341da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
14351da177e4SLinus Torvalds {
1436ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
14379f6c61f9SMiklos Szeredi 	struct list_head *prev;
14381da177e4SLinus Torvalds 
1439390c6843SRam Pai 	down_read(&namespace_sem);
14409f6c61f9SMiklos Szeredi 	if (!*pos) {
14419f6c61f9SMiklos Szeredi 		prev = &p->ns->list;
14429f6c61f9SMiklos Szeredi 	} else {
14439f6c61f9SMiklos Szeredi 		prev = &p->cursor.mnt_list;
14449f6c61f9SMiklos Szeredi 
14459f6c61f9SMiklos Szeredi 		/* Read after we'd reached the end? */
14469f6c61f9SMiklos Szeredi 		if (list_empty(prev))
14479f6c61f9SMiklos Szeredi 			return NULL;
1448c7999c36SAl Viro 	}
1449c7999c36SAl Viro 
14509f6c61f9SMiklos Szeredi 	return mnt_list_next(p->ns, prev);
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
m_next(struct seq_file * m,void * v,loff_t * pos)14531da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
14541da177e4SLinus Torvalds {
1455ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
14569f6c61f9SMiklos Szeredi 	struct mount *mnt = v;
1457b0765fb8SPavel Emelianov 
14589f6c61f9SMiklos Szeredi 	++*pos;
14599f6c61f9SMiklos Szeredi 	return mnt_list_next(p->ns, &mnt->mnt_list);
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
m_stop(struct seq_file * m,void * v)14621da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
14631da177e4SLinus Torvalds {
14649f6c61f9SMiklos Szeredi 	struct proc_mounts *p = m->private;
14659f6c61f9SMiklos Szeredi 	struct mount *mnt = v;
14669f6c61f9SMiklos Szeredi 
14679f6c61f9SMiklos Szeredi 	lock_ns_list(p->ns);
14689f6c61f9SMiklos Szeredi 	if (mnt)
14699f6c61f9SMiklos Szeredi 		list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list);
14709f6c61f9SMiklos Szeredi 	else
14719f6c61f9SMiklos Szeredi 		list_del_init(&p->cursor.mnt_list);
14729f6c61f9SMiklos Szeredi 	unlock_ns_list(p->ns);
1473390c6843SRam Pai 	up_read(&namespace_sem);
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
m_show(struct seq_file * m,void * v)14760226f492SAl Viro static int m_show(struct seq_file *m, void *v)
14779f5596afSAl Viro {
1478ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
14799f6c61f9SMiklos Szeredi 	struct mount *r = v;
14800226f492SAl Viro 	return p->show(m, &r->mnt);
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
1483a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
14841da177e4SLinus Torvalds 	.start	= m_start,
14851da177e4SLinus Torvalds 	.next	= m_next,
14861da177e4SLinus Torvalds 	.stop	= m_stop,
14870226f492SAl Viro 	.show	= m_show,
1488b4629fe2SChuck Lever };
14899f6c61f9SMiklos Szeredi 
mnt_cursor_del(struct mnt_namespace * ns,struct mount * cursor)14909f6c61f9SMiklos Szeredi void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor)
14919f6c61f9SMiklos Szeredi {
14929f6c61f9SMiklos Szeredi 	down_read(&namespace_sem);
14939f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
14949f6c61f9SMiklos Szeredi 	list_del(&cursor->mnt_list);
14959f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
14969f6c61f9SMiklos Szeredi 	up_read(&namespace_sem);
14979f6c61f9SMiklos Szeredi }
1498a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1499b4629fe2SChuck Lever 
15001da177e4SLinus Torvalds /**
15011da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
15021f287bc4SRandy Dunlap  * @m: root of mount tree
15031da177e4SLinus Torvalds  *
15041da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
15051da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
15061da177e4SLinus Torvalds  * busy.
15071da177e4SLinus Torvalds  */
may_umount_tree(struct vfsmount * m)1508909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
15091da177e4SLinus Torvalds {
1510909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
151136341f64SRam Pai 	int actual_refs = 0;
151236341f64SRam Pai 	int minimum_refs = 0;
1513315fc83eSAl Viro 	struct mount *p;
1514909b0a88SAl Viro 	BUG_ON(!m);
15151da177e4SLinus Torvalds 
1516b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1517719ea2fbSAl Viro 	lock_mount_hash();
1518909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
151983adc753SAl Viro 		actual_refs += mnt_get_count(p);
15201da177e4SLinus Torvalds 		minimum_refs += 2;
15211da177e4SLinus Torvalds 	}
1522719ea2fbSAl Viro 	unlock_mount_hash();
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
15251da177e4SLinus Torvalds 		return 0;
1526e3474a8eSIan Kent 
1527e3474a8eSIan Kent 	return 1;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds /**
15331da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
15341da177e4SLinus Torvalds  * @mnt: root of mount
15351da177e4SLinus Torvalds  *
15361da177e4SLinus Torvalds  * This is called to check if a mount point has any
15371da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
15381da177e4SLinus Torvalds  * mount has sub mounts this will return busy
15391da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
15401da177e4SLinus Torvalds  *
15411da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
15421da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
15431da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
15441da177e4SLinus Torvalds  */
may_umount(struct vfsmount * mnt)15451da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
15461da177e4SLinus Torvalds {
1547e3474a8eSIan Kent 	int ret = 1;
15488ad08d8aSAl Viro 	down_read(&namespace_sem);
1549719ea2fbSAl Viro 	lock_mount_hash();
15501ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1551e3474a8eSIan Kent 		ret = 0;
1552719ea2fbSAl Viro 	unlock_mount_hash();
15538ad08d8aSAl Viro 	up_read(&namespace_sem);
1554a05964f3SRam Pai 	return ret;
15551da177e4SLinus Torvalds }
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
15581da177e4SLinus Torvalds 
namespace_unlock(void)155997216be0SAl Viro static void namespace_unlock(void)
15601da177e4SLinus Torvalds {
1561a3b3c562SEric W. Biederman 	struct hlist_head head;
156256cbb429SAl Viro 	struct hlist_node *p;
156356cbb429SAl Viro 	struct mount *m;
15644edbe133SAl Viro 	LIST_HEAD(list);
156597216be0SAl Viro 
1566a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
15674edbe133SAl Viro 	list_splice_init(&ex_mountpoints, &list);
1568a3b3c562SEric W. Biederman 
156997216be0SAl Viro 	up_write(&namespace_sem);
1570a3b3c562SEric W. Biederman 
15714edbe133SAl Viro 	shrink_dentry_list(&list);
15724edbe133SAl Viro 
1573a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
157497216be0SAl Viro 		return;
157597216be0SAl Viro 
157622cb7405SNeilBrown 	synchronize_rcu_expedited();
157748a066e7SAl Viro 
157856cbb429SAl Viro 	hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
157956cbb429SAl Viro 		hlist_del(&m->mnt_umount);
158056cbb429SAl Viro 		mntput(&m->mnt);
158156cbb429SAl Viro 	}
158270fbcdf4SRam Pai }
158370fbcdf4SRam Pai 
namespace_lock(void)158497216be0SAl Viro static inline void namespace_lock(void)
1585e3197d83SAl Viro {
158697216be0SAl Viro 	down_write(&namespace_sem);
1587e3197d83SAl Viro }
1588e3197d83SAl Viro 
1589e819f152SEric W. Biederman enum umount_tree_flags {
1590e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1591e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1592e0c9c0afSEric W. Biederman 	UMOUNT_CONNECTED = 4,
1593e819f152SEric W. Biederman };
1594f2d0a123SEric W. Biederman 
disconnect_mount(struct mount * mnt,enum umount_tree_flags how)1595f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1596f2d0a123SEric W. Biederman {
1597f2d0a123SEric W. Biederman 	/* Leaving mounts connected is only valid for lazy umounts */
1598f2d0a123SEric W. Biederman 	if (how & UMOUNT_SYNC)
1599f2d0a123SEric W. Biederman 		return true;
1600f2d0a123SEric W. Biederman 
1601f2d0a123SEric W. Biederman 	/* A mount without a parent has nothing to be connected to */
1602f2d0a123SEric W. Biederman 	if (!mnt_has_parent(mnt))
1603f2d0a123SEric W. Biederman 		return true;
1604f2d0a123SEric W. Biederman 
1605f2d0a123SEric W. Biederman 	/* Because the reference counting rules change when mounts are
1606f2d0a123SEric W. Biederman 	 * unmounted and connected, umounted mounts may not be
1607f2d0a123SEric W. Biederman 	 * connected to mounted mounts.
1608f2d0a123SEric W. Biederman 	 */
1609f2d0a123SEric W. Biederman 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1610f2d0a123SEric W. Biederman 		return true;
1611f2d0a123SEric W. Biederman 
1612f2d0a123SEric W. Biederman 	/* Has it been requested that the mount remain connected? */
1613f2d0a123SEric W. Biederman 	if (how & UMOUNT_CONNECTED)
1614f2d0a123SEric W. Biederman 		return false;
1615f2d0a123SEric W. Biederman 
1616f2d0a123SEric W. Biederman 	/* Is the mount locked such that it needs to remain connected? */
1617f2d0a123SEric W. Biederman 	if (IS_MNT_LOCKED(mnt))
1618f2d0a123SEric W. Biederman 		return false;
1619f2d0a123SEric W. Biederman 
1620f2d0a123SEric W. Biederman 	/* By default disconnect the mount */
1621f2d0a123SEric W. Biederman 	return true;
1622f2d0a123SEric W. Biederman }
1623f2d0a123SEric W. Biederman 
162499b7db7bSNick Piggin /*
162548a066e7SAl Viro  * mount_lock must be held
162699b7db7bSNick Piggin  * namespace_sem must be held for write
162799b7db7bSNick Piggin  */
umount_tree(struct mount * mnt,enum umount_tree_flags how)1628e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
162970fbcdf4SRam Pai {
1630c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1631315fc83eSAl Viro 	struct mount *p;
163270fbcdf4SRam Pai 
16335d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
16345d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
16355d88457eSEric W. Biederman 
1636c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1637590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1638590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1639c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1640590ce4bcSEric W. Biederman 	}
1641c003b26fSEric W. Biederman 
1642411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1643c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1644c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
164538129a13SAl Viro 	}
164670fbcdf4SRam Pai 
1647c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1648e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
16497b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1650a05964f3SRam Pai 
1651c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1652d2921684SEric W. Biederman 		struct mnt_namespace *ns;
1653ce07d891SEric W. Biederman 		bool disconnect;
1654c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
16556776db3dSAl Viro 		list_del_init(&p->mnt_expire);
16561a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1657d2921684SEric W. Biederman 		ns = p->mnt_ns;
1658d2921684SEric W. Biederman 		if (ns) {
1659d2921684SEric W. Biederman 			ns->mounts--;
1660d2921684SEric W. Biederman 			__touch_mnt_namespace(ns);
1661d2921684SEric W. Biederman 		}
166263d37a84SAl Viro 		p->mnt_ns = NULL;
1663e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
166448a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
166587b95ce0SAl Viro 
1666f2d0a123SEric W. Biederman 		disconnect = disconnect_mount(p, how);
1667676da58dSAl Viro 		if (mnt_has_parent(p)) {
166881b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
1669ce07d891SEric W. Biederman 			if (!disconnect) {
1670ce07d891SEric W. Biederman 				/* Don't forget about p */
1671ce07d891SEric W. Biederman 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1672ce07d891SEric W. Biederman 			} else {
16736a46c573SEric W. Biederman 				umount_mnt(p);
16747c4b93d8SAl Viro 			}
1675ce07d891SEric W. Biederman 		}
16760f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
167719a1c409SAl Viro 		if (disconnect)
167819a1c409SAl Viro 			hlist_add_head(&p->mnt_umount, &unmounted);
167938129a13SAl Viro 	}
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
1682b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1683c35038beSAl Viro 
do_umount_root(struct super_block * sb)16848d0347f6SDavid Howells static int do_umount_root(struct super_block *sb)
16858d0347f6SDavid Howells {
16868d0347f6SDavid Howells 	int ret = 0;
16878d0347f6SDavid Howells 
16888d0347f6SDavid Howells 	down_write(&sb->s_umount);
16898d0347f6SDavid Howells 	if (!sb_rdonly(sb)) {
16908d0347f6SDavid Howells 		struct fs_context *fc;
16918d0347f6SDavid Howells 
16928d0347f6SDavid Howells 		fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
16938d0347f6SDavid Howells 						SB_RDONLY);
16948d0347f6SDavid Howells 		if (IS_ERR(fc)) {
16958d0347f6SDavid Howells 			ret = PTR_ERR(fc);
16968d0347f6SDavid Howells 		} else {
16978d0347f6SDavid Howells 			ret = parse_monolithic_mount_data(fc, NULL);
16988d0347f6SDavid Howells 			if (!ret)
16998d0347f6SDavid Howells 				ret = reconfigure_super(fc);
17008d0347f6SDavid Howells 			put_fs_context(fc);
17018d0347f6SDavid Howells 		}
17028d0347f6SDavid Howells 	}
17038d0347f6SDavid Howells 	up_write(&sb->s_umount);
17048d0347f6SDavid Howells 	return ret;
17058d0347f6SDavid Howells }
17068d0347f6SDavid Howells 
do_umount(struct mount * mnt,int flags)17071ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
17081da177e4SLinus Torvalds {
17091ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
17101da177e4SLinus Torvalds 	int retval;
17111da177e4SLinus Torvalds 
17121ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
17131da177e4SLinus Torvalds 	if (retval)
17141da177e4SLinus Torvalds 		return retval;
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	/*
17171da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
17181da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
17191da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
17201da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
17211da177e4SLinus Torvalds 	 */
17221da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
17231ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
17241da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
17251da177e4SLinus Torvalds 			return -EINVAL;
17261da177e4SLinus Torvalds 
1727b3e19d92SNick Piggin 		/*
1728b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1729b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1730b3e19d92SNick Piggin 		 */
1731719ea2fbSAl Viro 		lock_mount_hash();
173283adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1733719ea2fbSAl Viro 			unlock_mount_hash();
17341da177e4SLinus Torvalds 			return -EBUSY;
1735b3e19d92SNick Piggin 		}
1736719ea2fbSAl Viro 		unlock_mount_hash();
17371da177e4SLinus Torvalds 
1738863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
17391da177e4SLinus Torvalds 			return -EAGAIN;
17401da177e4SLinus Torvalds 	}
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 	/*
17431da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
17441da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
17451da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
17461da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
17471da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
17481da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
17491da177e4SLinus Torvalds 	 * about for the moment.
17501da177e4SLinus Torvalds 	 */
17511da177e4SLinus Torvalds 
175242faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
175342faad99SAl Viro 		sb->s_op->umount_begin(sb);
175442faad99SAl Viro 	}
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	/*
17571da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
17581da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
17591da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
17601da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
17611da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
17621da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
17631da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
17641da177e4SLinus Torvalds 	 */
17651ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
17661da177e4SLinus Torvalds 		/*
17671da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
17681da177e4SLinus Torvalds 		 * we just try to remount it readonly.
17691da177e4SLinus Torvalds 		 */
1770bc6155d1SEric W. Biederman 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1771a1480dccSAndy Lutomirski 			return -EPERM;
17728d0347f6SDavid Howells 		return do_umount_root(sb);
17731da177e4SLinus Torvalds 	}
17741da177e4SLinus Torvalds 
177597216be0SAl Viro 	namespace_lock();
1776719ea2fbSAl Viro 	lock_mount_hash();
17771da177e4SLinus Torvalds 
177825d202edSEric W. Biederman 	/* Recheck MNT_LOCKED with the locks held */
177925d202edSEric W. Biederman 	retval = -EINVAL;
178025d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
178125d202edSEric W. Biederman 		goto out;
178225d202edSEric W. Biederman 
178325d202edSEric W. Biederman 	event++;
178448a066e7SAl Viro 	if (flags & MNT_DETACH) {
178548a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1786e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
178748a066e7SAl Viro 		retval = 0;
178848a066e7SAl Viro 	} else {
1789b54b9be7SAl Viro 		shrink_submounts(mnt);
17901da177e4SLinus Torvalds 		retval = -EBUSY;
179148a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
17921a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1793e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
17941da177e4SLinus Torvalds 			retval = 0;
17951da177e4SLinus Torvalds 		}
179648a066e7SAl Viro 	}
179725d202edSEric W. Biederman out:
1798719ea2fbSAl Viro 	unlock_mount_hash();
1799e3197d83SAl Viro 	namespace_unlock();
18001da177e4SLinus Torvalds 	return retval;
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds /*
180480b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
180580b5dce8SEric W. Biederman  *
180680b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
180780b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
180880b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
180980b5dce8SEric W. Biederman  * leaking them.
181080b5dce8SEric W. Biederman  *
181180b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
181280b5dce8SEric W. Biederman  */
__detach_mounts(struct dentry * dentry)181380b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
181480b5dce8SEric W. Biederman {
181580b5dce8SEric W. Biederman 	struct mountpoint *mp;
181680b5dce8SEric W. Biederman 	struct mount *mnt;
181780b5dce8SEric W. Biederman 
181880b5dce8SEric W. Biederman 	namespace_lock();
18193895dbf8SEric W. Biederman 	lock_mount_hash();
182080b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
1821adc9b5c0SAl Viro 	if (!mp)
182280b5dce8SEric W. Biederman 		goto out_unlock;
182380b5dce8SEric W. Biederman 
1824e06b933eSAndrey Ulanov 	event++;
182580b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
182680b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1827ce07d891SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1828fe78fcc8SEric W. Biederman 			umount_mnt(mnt);
182956cbb429SAl Viro 			hlist_add_head(&mnt->mnt_umount, &unmounted);
1830ce07d891SEric W. Biederman 		}
1831e0c9c0afSEric W. Biederman 		else umount_tree(mnt, UMOUNT_CONNECTED);
183280b5dce8SEric W. Biederman 	}
183380b5dce8SEric W. Biederman 	put_mountpoint(mp);
183480b5dce8SEric W. Biederman out_unlock:
18353895dbf8SEric W. Biederman 	unlock_mount_hash();
183680b5dce8SEric W. Biederman 	namespace_unlock();
183780b5dce8SEric W. Biederman }
183880b5dce8SEric W. Biederman 
183980b5dce8SEric W. Biederman /*
18409b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
18419b40bc90SAl Viro  */
may_mount(void)1842a5f85d78SAl Viro bool may_mount(void)
18439b40bc90SAl Viro {
18449b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
18459b40bc90SAl Viro }
18469b40bc90SAl Viro 
184778aa08a8SChristian Brauner /**
184878aa08a8SChristian Brauner  * path_mounted - check whether path is mounted
184978aa08a8SChristian Brauner  * @path: path to check
185078aa08a8SChristian Brauner  *
185178aa08a8SChristian Brauner  * Determine whether @path refers to the root of a mount.
185278aa08a8SChristian Brauner  *
185378aa08a8SChristian Brauner  * Return: true if @path is the root of a mount, false if not.
185478aa08a8SChristian Brauner  */
path_mounted(const struct path * path)185578aa08a8SChristian Brauner static inline bool path_mounted(const struct path *path)
185678aa08a8SChristian Brauner {
185778aa08a8SChristian Brauner 	return path->mnt->mnt_root == path->dentry;
185878aa08a8SChristian Brauner }
185978aa08a8SChristian Brauner 
warn_mandlock(void)1860f7e33bdbSJeff Layton static void warn_mandlock(void)
18619e8925b6SJeff Layton {
1862f7e33bdbSJeff Layton 	pr_warn_once("=======================================================\n"
1863f7e33bdbSJeff Layton 		     "WARNING: The mand mount option has been deprecated and\n"
1864f7e33bdbSJeff Layton 		     "         and is ignored by this kernel. Remove the mand\n"
1865f7e33bdbSJeff Layton 		     "         option from the mount to silence this warning.\n"
1866f7e33bdbSJeff Layton 		     "=======================================================\n");
18679e8925b6SJeff Layton }
18689e8925b6SJeff Layton 
can_umount(const struct path * path,int flags)186925ccd24fSChristoph Hellwig static int can_umount(const struct path *path, int flags)
18701da177e4SLinus Torvalds {
187125ccd24fSChristoph Hellwig 	struct mount *mnt = real_mount(path->mnt);
18721da177e4SLinus Torvalds 
18739b40bc90SAl Viro 	if (!may_mount())
18749b40bc90SAl Viro 		return -EPERM;
187578aa08a8SChristian Brauner 	if (!path_mounted(path))
187625ccd24fSChristoph Hellwig 		return -EINVAL;
1877143c8c91SAl Viro 	if (!check_mnt(mnt))
187825ccd24fSChristoph Hellwig 		return -EINVAL;
187925d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
188025ccd24fSChristoph Hellwig 		return -EINVAL;
1881b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
188225ccd24fSChristoph Hellwig 		return -EPERM;
188325ccd24fSChristoph Hellwig 	return 0;
188425ccd24fSChristoph Hellwig }
18851da177e4SLinus Torvalds 
1886a0a6df9aSAl Viro // caller is responsible for flags being sane
path_umount(struct path * path,int flags)188725ccd24fSChristoph Hellwig int path_umount(struct path *path, int flags)
188825ccd24fSChristoph Hellwig {
188925ccd24fSChristoph Hellwig 	struct mount *mnt = real_mount(path->mnt);
189025ccd24fSChristoph Hellwig 	int ret;
189125ccd24fSChristoph Hellwig 
189225ccd24fSChristoph Hellwig 	ret = can_umount(path, flags);
189325ccd24fSChristoph Hellwig 	if (!ret)
189425ccd24fSChristoph Hellwig 		ret = do_umount(mnt, flags);
189525ccd24fSChristoph Hellwig 
1896429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
189741525f56SChristoph Hellwig 	dput(path->dentry);
1898900148dcSAl Viro 	mntput_no_expire(mnt);
189925ccd24fSChristoph Hellwig 	return ret;
19001da177e4SLinus Torvalds }
19011da177e4SLinus Torvalds 
ksys_umount(char __user * name,int flags)190209267defSChristoph Hellwig static int ksys_umount(char __user *name, int flags)
190341525f56SChristoph Hellwig {
190441525f56SChristoph Hellwig 	int lookup_flags = LOOKUP_MOUNTPOINT;
190541525f56SChristoph Hellwig 	struct path path;
190641525f56SChristoph Hellwig 	int ret;
190741525f56SChristoph Hellwig 
1908a0a6df9aSAl Viro 	// basic validity checks done first
1909a0a6df9aSAl Viro 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1910a0a6df9aSAl Viro 		return -EINVAL;
1911a0a6df9aSAl Viro 
191241525f56SChristoph Hellwig 	if (!(flags & UMOUNT_NOFOLLOW))
191341525f56SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
191441525f56SChristoph Hellwig 	ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
191541525f56SChristoph Hellwig 	if (ret)
191641525f56SChristoph Hellwig 		return ret;
191741525f56SChristoph Hellwig 	return path_umount(&path, flags);
191841525f56SChristoph Hellwig }
191941525f56SChristoph Hellwig 
SYSCALL_DEFINE2(umount,char __user *,name,int,flags)19203a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
19213a18ef5cSDominik Brodowski {
19223a18ef5cSDominik Brodowski 	return ksys_umount(name, flags);
19233a18ef5cSDominik Brodowski }
19243a18ef5cSDominik Brodowski 
19251da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds /*
19281da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
19291da177e4SLinus Torvalds  */
SYSCALL_DEFINE1(oldumount,char __user *,name)1930bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
19311da177e4SLinus Torvalds {
19323a18ef5cSDominik Brodowski 	return ksys_umount(name, 0);
19331da177e4SLinus Torvalds }
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds #endif
19361da177e4SLinus Torvalds 
is_mnt_ns_file(struct dentry * dentry)19374ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
19388823c079SEric W. Biederman {
19394ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1940e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1941e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
19424ce5d2b1SEric W. Biederman }
19434ce5d2b1SEric W. Biederman 
to_mnt_ns(struct ns_common * ns)1944213921f9SEric Biggers static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
194558be2825SAl Viro {
194658be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
194758be2825SAl Viro }
194858be2825SAl Viro 
from_mnt_ns(struct mnt_namespace * mnt)1949303cc571SChristian Brauner struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
1950303cc571SChristian Brauner {
1951303cc571SChristian Brauner 	return &mnt->ns;
1952303cc571SChristian Brauner }
1953303cc571SChristian Brauner 
mnt_ns_loop(struct dentry * dentry)19544ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
19554ce5d2b1SEric W. Biederman {
19564ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
19574ce5d2b1SEric W. Biederman 	 * mount namespace loop?
19584ce5d2b1SEric W. Biederman 	 */
19594ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
19604ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
19614ce5d2b1SEric W. Biederman 		return false;
19624ce5d2b1SEric W. Biederman 
1963f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
19648823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
19658823c079SEric W. Biederman }
19668823c079SEric W. Biederman 
copy_tree(struct mount * mnt,struct dentry * dentry,int flag)196787129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
196836341f64SRam Pai 					int flag)
19691da177e4SLinus Torvalds {
197084d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
19711da177e4SLinus Torvalds 
19724ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
19734ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
19744ce5d2b1SEric W. Biederman 
19754ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1976be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
19779676f0c6SRam Pai 
197836341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1979be34d1a3SDavid Howells 	if (IS_ERR(q))
1980be34d1a3SDavid Howells 		return q;
1981be34d1a3SDavid Howells 
1982a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	p = mnt;
19856b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1986315fc83eSAl Viro 		struct mount *s;
19877ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
19881da177e4SLinus Torvalds 			continue;
19891da177e4SLinus Torvalds 
1990909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
19914ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
19924ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
1993df7342b2SEric W. Biederman 				if (s->mnt.mnt_flags & MNT_LOCKED) {
1994df7342b2SEric W. Biederman 					/* Both unbindable and locked. */
1995df7342b2SEric W. Biederman 					q = ERR_PTR(-EPERM);
1996df7342b2SEric W. Biederman 					goto out;
1997df7342b2SEric W. Biederman 				} else {
19984ce5d2b1SEric W. Biederman 					s = skip_mnt_tree(s);
19994ce5d2b1SEric W. Biederman 					continue;
20004ce5d2b1SEric W. Biederman 				}
2001df7342b2SEric W. Biederman 			}
20024ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
20034ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
20049676f0c6SRam Pai 				s = skip_mnt_tree(s);
20059676f0c6SRam Pai 				continue;
20069676f0c6SRam Pai 			}
20070714a533SAl Viro 			while (p != s->mnt_parent) {
20080714a533SAl Viro 				p = p->mnt_parent;
20090714a533SAl Viro 				q = q->mnt_parent;
20101da177e4SLinus Torvalds 			}
201187129cc0SAl Viro 			p = s;
201284d17192SAl Viro 			parent = q;
201387129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
2014be34d1a3SDavid Howells 			if (IS_ERR(q))
2015be34d1a3SDavid Howells 				goto out;
2016719ea2fbSAl Viro 			lock_mount_hash();
20171a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
20186ac39281SChristian Brauner 			attach_mnt(q, parent, p->mnt_mp, false);
2019719ea2fbSAl Viro 			unlock_mount_hash();
20201da177e4SLinus Torvalds 		}
20211da177e4SLinus Torvalds 	}
20221da177e4SLinus Torvalds 	return res;
2023be34d1a3SDavid Howells out:
20241da177e4SLinus Torvalds 	if (res) {
2025719ea2fbSAl Viro 		lock_mount_hash();
2026e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
2027719ea2fbSAl Viro 		unlock_mount_hash();
20281da177e4SLinus Torvalds 	}
2029be34d1a3SDavid Howells 	return q;
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
2032be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
2033be34d1a3SDavid Howells 
collect_mounts(const struct path * path)2034ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path)
20358aec0809SAl Viro {
2036cb338d06SAl Viro 	struct mount *tree;
203797216be0SAl Viro 	namespace_lock();
2038cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
2039cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
2040cd4a4017SEric W. Biederman 	else
204187129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
204287129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
2043328e6d90SAl Viro 	namespace_unlock();
2044be34d1a3SDavid Howells 	if (IS_ERR(tree))
204552e220d3SDan Carpenter 		return ERR_CAST(tree);
2046be34d1a3SDavid Howells 	return &tree->mnt;
20478aec0809SAl Viro }
20488aec0809SAl Viro 
2049a07b2000SAl Viro static void free_mnt_ns(struct mnt_namespace *);
2050a07b2000SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
2051a07b2000SAl Viro 
dissolve_on_fput(struct vfsmount * mnt)2052a07b2000SAl Viro void dissolve_on_fput(struct vfsmount *mnt)
2053a07b2000SAl Viro {
2054a07b2000SAl Viro 	struct mnt_namespace *ns;
2055a07b2000SAl Viro 	namespace_lock();
2056a07b2000SAl Viro 	lock_mount_hash();
2057a07b2000SAl Viro 	ns = real_mount(mnt)->mnt_ns;
205844dfd84aSDavid Howells 	if (ns) {
205944dfd84aSDavid Howells 		if (is_anon_ns(ns))
2060a07b2000SAl Viro 			umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
206144dfd84aSDavid Howells 		else
206244dfd84aSDavid Howells 			ns = NULL;
206344dfd84aSDavid Howells 	}
2064a07b2000SAl Viro 	unlock_mount_hash();
2065a07b2000SAl Viro 	namespace_unlock();
206644dfd84aSDavid Howells 	if (ns)
2067a07b2000SAl Viro 		free_mnt_ns(ns);
2068a07b2000SAl Viro }
2069a07b2000SAl Viro 
drop_collected_mounts(struct vfsmount * mnt)20708aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
20718aec0809SAl Viro {
207297216be0SAl Viro 	namespace_lock();
2073719ea2fbSAl Viro 	lock_mount_hash();
20749c8e0a1bSEric W. Biederman 	umount_tree(real_mount(mnt), 0);
2075719ea2fbSAl Viro 	unlock_mount_hash();
20763ab6abeeSAl Viro 	namespace_unlock();
20778aec0809SAl Viro }
20788aec0809SAl Viro 
has_locked_children(struct mount * mnt,struct dentry * dentry)2079427215d8SMiklos Szeredi static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2080427215d8SMiklos Szeredi {
2081427215d8SMiklos Szeredi 	struct mount *child;
2082427215d8SMiklos Szeredi 
2083427215d8SMiklos Szeredi 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2084427215d8SMiklos Szeredi 		if (!is_subdir(child->mnt_mountpoint, dentry))
2085427215d8SMiklos Szeredi 			continue;
2086427215d8SMiklos Szeredi 
2087427215d8SMiklos Szeredi 		if (child->mnt.mnt_flags & MNT_LOCKED)
2088427215d8SMiklos Szeredi 			return true;
2089427215d8SMiklos Szeredi 	}
2090427215d8SMiklos Szeredi 	return false;
2091427215d8SMiklos Szeredi }
2092427215d8SMiklos Szeredi 
2093c771d683SMiklos Szeredi /**
2094c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
20951f287bc4SRandy Dunlap  * @path: path to clone
2096c771d683SMiklos Szeredi  *
20971f287bc4SRandy Dunlap  * This creates a new vfsmount, which will be the clone of @path.  The new mount
20981f287bc4SRandy Dunlap  * will not be attached anywhere in the namespace and will be private (i.e.
20991f287bc4SRandy Dunlap  * changes to the originating mount won't be propagated into this).
2100c771d683SMiklos Szeredi  *
2101c771d683SMiklos Szeredi  * Release with mntput().
2102c771d683SMiklos Szeredi  */
clone_private_mount(const struct path * path)2103ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path)
2104c771d683SMiklos Szeredi {
2105c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
2106c771d683SMiklos Szeredi 	struct mount *new_mnt;
2107c771d683SMiklos Szeredi 
2108427215d8SMiklos Szeredi 	down_read(&namespace_sem);
2109c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
2110427215d8SMiklos Szeredi 		goto invalid;
2111427215d8SMiklos Szeredi 
2112427215d8SMiklos Szeredi 	if (!check_mnt(old_mnt))
2113427215d8SMiklos Szeredi 		goto invalid;
2114427215d8SMiklos Szeredi 
2115427215d8SMiklos Szeredi 	if (has_locked_children(old_mnt, path->dentry))
2116427215d8SMiklos Szeredi 		goto invalid;
2117c771d683SMiklos Szeredi 
2118c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
2119427215d8SMiklos Szeredi 	up_read(&namespace_sem);
2120427215d8SMiklos Szeredi 
2121c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
2122c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
2123c771d683SMiklos Szeredi 
2124df820f8dSMiklos Szeredi 	/* Longterm mount to be removed by kern_unmount*() */
2125df820f8dSMiklos Szeredi 	new_mnt->mnt_ns = MNT_NS_INTERNAL;
2126df820f8dSMiklos Szeredi 
2127c771d683SMiklos Szeredi 	return &new_mnt->mnt;
2128427215d8SMiklos Szeredi 
2129427215d8SMiklos Szeredi invalid:
2130427215d8SMiklos Szeredi 	up_read(&namespace_sem);
2131427215d8SMiklos Szeredi 	return ERR_PTR(-EINVAL);
2132c771d683SMiklos Szeredi }
2133c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
2134c771d683SMiklos Szeredi 
iterate_mounts(int (* f)(struct vfsmount *,void *),void * arg,struct vfsmount * root)21351f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
21361f707137SAl Viro 		   struct vfsmount *root)
21371f707137SAl Viro {
21381a4eeaf2SAl Viro 	struct mount *mnt;
21391f707137SAl Viro 	int res = f(root, arg);
21401f707137SAl Viro 	if (res)
21411f707137SAl Viro 		return res;
21421a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
21431a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
21441f707137SAl Viro 		if (res)
21451f707137SAl Viro 			return res;
21461f707137SAl Viro 	}
21471f707137SAl Viro 	return 0;
21481f707137SAl Viro }
21491f707137SAl Viro 
lock_mnt_tree(struct mount * mnt)21503bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt)
21513bd045ccSAl Viro {
21523bd045ccSAl Viro 	struct mount *p;
21533bd045ccSAl Viro 
21543bd045ccSAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
21553bd045ccSAl Viro 		int flags = p->mnt.mnt_flags;
21563bd045ccSAl Viro 		/* Don't allow unprivileged users to change mount flags */
21573bd045ccSAl Viro 		flags |= MNT_LOCK_ATIME;
21583bd045ccSAl Viro 
21593bd045ccSAl Viro 		if (flags & MNT_READONLY)
21603bd045ccSAl Viro 			flags |= MNT_LOCK_READONLY;
21613bd045ccSAl Viro 
21623bd045ccSAl Viro 		if (flags & MNT_NODEV)
21633bd045ccSAl Viro 			flags |= MNT_LOCK_NODEV;
21643bd045ccSAl Viro 
21653bd045ccSAl Viro 		if (flags & MNT_NOSUID)
21663bd045ccSAl Viro 			flags |= MNT_LOCK_NOSUID;
21673bd045ccSAl Viro 
21683bd045ccSAl Viro 		if (flags & MNT_NOEXEC)
21693bd045ccSAl Viro 			flags |= MNT_LOCK_NOEXEC;
21703bd045ccSAl Viro 		/* Don't allow unprivileged users to reveal what is under a mount */
21713bd045ccSAl Viro 		if (list_empty(&p->mnt_expire))
21723bd045ccSAl Viro 			flags |= MNT_LOCKED;
21733bd045ccSAl Viro 		p->mnt.mnt_flags = flags;
21743bd045ccSAl Viro 	}
21753bd045ccSAl Viro }
21763bd045ccSAl Viro 
cleanup_group_ids(struct mount * mnt,struct mount * end)21774b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
2178719f5d7fSMiklos Szeredi {
2179315fc83eSAl Viro 	struct mount *p;
2180719f5d7fSMiklos Szeredi 
2181909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
2182fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
21834b8b21f4SAl Viro 			mnt_release_group_id(p);
2184719f5d7fSMiklos Szeredi 	}
2185719f5d7fSMiklos Szeredi }
2186719f5d7fSMiklos Szeredi 
invent_group_ids(struct mount * mnt,bool recurse)21874b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
2188719f5d7fSMiklos Szeredi {
2189315fc83eSAl Viro 	struct mount *p;
2190719f5d7fSMiklos Szeredi 
2191909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
2192fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
21934b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
2194719f5d7fSMiklos Szeredi 			if (err) {
21954b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
2196719f5d7fSMiklos Szeredi 				return err;
2197719f5d7fSMiklos Szeredi 			}
2198719f5d7fSMiklos Szeredi 		}
2199719f5d7fSMiklos Szeredi 	}
2200719f5d7fSMiklos Szeredi 
2201719f5d7fSMiklos Szeredi 	return 0;
2202719f5d7fSMiklos Szeredi }
2203719f5d7fSMiklos Szeredi 
count_mounts(struct mnt_namespace * ns,struct mount * mnt)2204d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2205d2921684SEric W. Biederman {
2206d2921684SEric W. Biederman 	unsigned int max = READ_ONCE(sysctl_mount_max);
2207124f75f8SAl Viro 	unsigned int mounts = 0;
2208d2921684SEric W. Biederman 	struct mount *p;
2209d2921684SEric W. Biederman 
2210124f75f8SAl Viro 	if (ns->mounts >= max)
2211124f75f8SAl Viro 		return -ENOSPC;
2212124f75f8SAl Viro 	max -= ns->mounts;
2213124f75f8SAl Viro 	if (ns->pending_mounts >= max)
2214124f75f8SAl Viro 		return -ENOSPC;
2215124f75f8SAl Viro 	max -= ns->pending_mounts;
2216124f75f8SAl Viro 
2217d2921684SEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt))
2218d2921684SEric W. Biederman 		mounts++;
2219d2921684SEric W. Biederman 
2220124f75f8SAl Viro 	if (mounts > max)
2221d2921684SEric W. Biederman 		return -ENOSPC;
2222d2921684SEric W. Biederman 
2223124f75f8SAl Viro 	ns->pending_mounts += mounts;
2224d2921684SEric W. Biederman 	return 0;
2225d2921684SEric W. Biederman }
2226d2921684SEric W. Biederman 
22276ac39281SChristian Brauner enum mnt_tree_flags_t {
22286ac39281SChristian Brauner 	MNT_TREE_MOVE = BIT(0),
22296ac39281SChristian Brauner 	MNT_TREE_BENEATH = BIT(1),
22306ac39281SChristian Brauner };
22316ac39281SChristian Brauner 
22326ac39281SChristian Brauner /**
22336ac39281SChristian Brauner  * attach_recursive_mnt - attach a source mount tree
2234b90fa9aeSRam Pai  * @source_mnt: mount tree to be attached
22356ac39281SChristian Brauner  * @top_mnt:    mount that @source_mnt will be mounted on or mounted beneath
22366ac39281SChristian Brauner  * @dest_mp:    the mountpoint @source_mnt will be mounted at
22376ac39281SChristian Brauner  * @flags:      modify how @source_mnt is supposed to be attached
2238b90fa9aeSRam Pai  *
2239b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
2240b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
22419676f0c6SRam Pai  * ---------------------------------------------------------------------------
2242b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
22439676f0c6SRam Pai  * |**************************************************************************
22449676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
22459676f0c6SRam Pai  * | dest     |               |                |                |            |
22469676f0c6SRam Pai  * |   |      |               |                |                |            |
22479676f0c6SRam Pai  * |   v      |               |                |                |            |
22489676f0c6SRam Pai  * |**************************************************************************
22499676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
22505afe0022SRam Pai  * |          |               |                |                |            |
22519676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
22529676f0c6SRam Pai  * ***************************************************************************
2253b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
2254b90fa9aeSRam Pai  * destination mount.
2255b90fa9aeSRam Pai  *
2256b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
2257b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
2258b90fa9aeSRam Pai  * 	 the peer group of the source mount.
2259b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
2260b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
2261b90fa9aeSRam Pai  *       mount.
22625afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
22635afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
22645afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
22655afe0022SRam Pai  *       is marked as 'shared and slave'.
22665afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
22675afe0022SRam Pai  * 	 source mount.
22685afe0022SRam Pai  *
22699676f0c6SRam Pai  * ---------------------------------------------------------------------------
227021444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
22719676f0c6SRam Pai  * |**************************************************************************
22729676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
22739676f0c6SRam Pai  * | dest     |               |                |                |            |
22749676f0c6SRam Pai  * |   |      |               |                |                |            |
22759676f0c6SRam Pai  * |   v      |               |                |                |            |
22769676f0c6SRam Pai  * |**************************************************************************
22779676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
22785afe0022SRam Pai  * |          |               |                |                |            |
22799676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
22809676f0c6SRam Pai  * ***************************************************************************
22815afe0022SRam Pai  *
22825afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
22835afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
228421444403SRam Pai  * (+*)  the mount is moved to the destination.
22855afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
22865afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
22875afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
22885afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
2289b90fa9aeSRam Pai  *
2290b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
2291b90fa9aeSRam Pai  * applied to each mount in the tree.
2292b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
2293b90fa9aeSRam Pai  * in allocations.
22946ac39281SChristian Brauner  *
22956ac39281SChristian Brauner  * Context: The function expects namespace_lock() to be held.
22966ac39281SChristian Brauner  * Return: If @source_mnt was successfully attached 0 is returned.
22976ac39281SChristian Brauner  *         Otherwise a negative error code is returned.
2298b90fa9aeSRam Pai  */
attach_recursive_mnt(struct mount * source_mnt,struct mount * top_mnt,struct mountpoint * dest_mp,enum mnt_tree_flags_t flags)22990fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
23006ac39281SChristian Brauner 				struct mount *top_mnt,
230184d17192SAl Viro 				struct mountpoint *dest_mp,
23026ac39281SChristian Brauner 				enum mnt_tree_flags_t flags)
2303b90fa9aeSRam Pai {
23043bd045ccSAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
230538129a13SAl Viro 	HLIST_HEAD(tree_list);
23066ac39281SChristian Brauner 	struct mnt_namespace *ns = top_mnt->mnt_ns;
23071064f874SEric W. Biederman 	struct mountpoint *smp;
23086ac39281SChristian Brauner 	struct mount *child, *dest_mnt, *p;
230938129a13SAl Viro 	struct hlist_node *n;
23106ac39281SChristian Brauner 	int err = 0;
23116ac39281SChristian Brauner 	bool moving = flags & MNT_TREE_MOVE, beneath = flags & MNT_TREE_BENEATH;
2312b90fa9aeSRam Pai 
23136ac39281SChristian Brauner 	/*
23146ac39281SChristian Brauner 	 * Preallocate a mountpoint in case the new mounts need to be
23156ac39281SChristian Brauner 	 * mounted beneath mounts on the same mountpoint.
23161064f874SEric W. Biederman 	 */
23171064f874SEric W. Biederman 	smp = get_mountpoint(source_mnt->mnt.mnt_root);
23181064f874SEric W. Biederman 	if (IS_ERR(smp))
23191064f874SEric W. Biederman 		return PTR_ERR(smp);
23201064f874SEric W. Biederman 
2321d2921684SEric W. Biederman 	/* Is there space to add these mounts to the mount namespace? */
23222763d119SAl Viro 	if (!moving) {
2323d2921684SEric W. Biederman 		err = count_mounts(ns, source_mnt);
2324d2921684SEric W. Biederman 		if (err)
2325d2921684SEric W. Biederman 			goto out;
2326d2921684SEric W. Biederman 	}
2327d2921684SEric W. Biederman 
23286ac39281SChristian Brauner 	if (beneath)
23296ac39281SChristian Brauner 		dest_mnt = top_mnt->mnt_parent;
23306ac39281SChristian Brauner 	else
23316ac39281SChristian Brauner 		dest_mnt = top_mnt;
23326ac39281SChristian Brauner 
2333fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
23340fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
2335719f5d7fSMiklos Szeredi 		if (err)
2336719f5d7fSMiklos Szeredi 			goto out;
233784d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
23386ac39281SChristian Brauner 	}
2339f2ebb3a9SAl Viro 	lock_mount_hash();
2340719f5d7fSMiklos Szeredi 	if (err)
2341719f5d7fSMiklos Szeredi 		goto out_cleanup_ids;
23426ac39281SChristian Brauner 
23436ac39281SChristian Brauner 	if (IS_MNT_SHARED(dest_mnt)) {
2344909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
23450f0afb1dSAl Viro 			set_mnt_shared(p);
2346b90fa9aeSRam Pai 	}
23476ac39281SChristian Brauner 
23482763d119SAl Viro 	if (moving) {
23496ac39281SChristian Brauner 		if (beneath)
23506ac39281SChristian Brauner 			dest_mp = smp;
23512763d119SAl Viro 		unhash_mnt(source_mnt);
23526ac39281SChristian Brauner 		attach_mnt(source_mnt, top_mnt, dest_mp, beneath);
2353143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
235421444403SRam Pai 	} else {
235544dfd84aSDavid Howells 		if (source_mnt->mnt_ns) {
235644dfd84aSDavid Howells 			/* move from anon - the caller will destroy */
235744dfd84aSDavid Howells 			list_del_init(&source_mnt->mnt_ns->list);
235844dfd84aSDavid Howells 		}
23596ac39281SChristian Brauner 		if (beneath)
23606ac39281SChristian Brauner 			mnt_set_mountpoint_beneath(source_mnt, top_mnt, smp);
23616ac39281SChristian Brauner 		else
236284d17192SAl Viro 			mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
23631064f874SEric W. Biederman 		commit_tree(source_mnt);
236421444403SRam Pai 	}
2365b90fa9aeSRam Pai 
236638129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
23671d6a32acSAl Viro 		struct mount *q;
236838129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
23691064f874SEric W. Biederman 		q = __lookup_mnt(&child->mnt_parent->mnt,
23701d6a32acSAl Viro 				 child->mnt_mountpoint);
23711064f874SEric W. Biederman 		if (q)
23721064f874SEric W. Biederman 			mnt_change_mountpoint(child, smp, q);
23733bd045ccSAl Viro 		/* Notice when we are propagating across user namespaces */
23743bd045ccSAl Viro 		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
23753bd045ccSAl Viro 			lock_mnt_tree(child);
2376d728cf79SChristian Brauner 		child->mnt.mnt_flags &= ~MNT_LOCKED;
23771064f874SEric W. Biederman 		commit_tree(child);
2378b90fa9aeSRam Pai 	}
23791064f874SEric W. Biederman 	put_mountpoint(smp);
2380719ea2fbSAl Viro 	unlock_mount_hash();
238199b7db7bSNick Piggin 
2382b90fa9aeSRam Pai 	return 0;
2383719f5d7fSMiklos Szeredi 
2384719f5d7fSMiklos Szeredi  out_cleanup_ids:
2385f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
2386f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2387d2921684SEric W. Biederman 		child->mnt_parent->mnt_ns->pending_mounts = 0;
2388e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
2389f2ebb3a9SAl Viro 	}
2390f2ebb3a9SAl Viro 	unlock_mount_hash();
23910fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
2392719f5d7fSMiklos Szeredi  out:
2393d2921684SEric W. Biederman 	ns->pending_mounts = 0;
23941064f874SEric W. Biederman 
23951064f874SEric W. Biederman 	read_seqlock_excl(&mount_lock);
23961064f874SEric W. Biederman 	put_mountpoint(smp);
23971064f874SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
23981064f874SEric W. Biederman 
2399719f5d7fSMiklos Szeredi 	return err;
2400b90fa9aeSRam Pai }
2401b90fa9aeSRam Pai 
24026ac39281SChristian Brauner /**
24036ac39281SChristian Brauner  * do_lock_mount - lock mount and mountpoint
24046ac39281SChristian Brauner  * @path:    target path
24056ac39281SChristian Brauner  * @beneath: whether the intention is to mount beneath @path
24066ac39281SChristian Brauner  *
24076ac39281SChristian Brauner  * Follow the mount stack on @path until the top mount @mnt is found. If
24086ac39281SChristian Brauner  * the initial @path->{mnt,dentry} is a mountpoint lookup the first
24096ac39281SChristian Brauner  * mount stacked on top of it. Then simply follow @{mnt,mnt->mnt_root}
24106ac39281SChristian Brauner  * until nothing is stacked on top of it anymore.
24116ac39281SChristian Brauner  *
24126ac39281SChristian Brauner  * Acquire the inode_lock() on the top mount's ->mnt_root to protect
24136ac39281SChristian Brauner  * against concurrent removal of the new mountpoint from another mount
24146ac39281SChristian Brauner  * namespace.
24156ac39281SChristian Brauner  *
24166ac39281SChristian Brauner  * If @beneath is requested, acquire inode_lock() on @mnt's mountpoint
24176ac39281SChristian Brauner  * @mp on @mnt->mnt_parent must be acquired. This protects against a
24186ac39281SChristian Brauner  * concurrent unlink of @mp->mnt_dentry from another mount namespace
24196ac39281SChristian Brauner  * where @mnt doesn't have a child mount mounted @mp. A concurrent
24206ac39281SChristian Brauner  * removal of @mnt->mnt_root doesn't matter as nothing will be mounted
24216ac39281SChristian Brauner  * on top of it for @beneath.
24226ac39281SChristian Brauner  *
24236ac39281SChristian Brauner  * In addition, @beneath needs to make sure that @mnt hasn't been
24246ac39281SChristian Brauner  * unmounted or moved from its current mountpoint in between dropping
24256ac39281SChristian Brauner  * @mount_lock and acquiring @namespace_sem. For the !@beneath case @mnt
24266ac39281SChristian Brauner  * being unmounted would be detected later by e.g., calling
24276ac39281SChristian Brauner  * check_mnt(mnt) in the function it's called from. For the @beneath
24286ac39281SChristian Brauner  * case however, it's useful to detect it directly in do_lock_mount().
24296ac39281SChristian Brauner  * If @mnt hasn't been unmounted then @mnt->mnt_mountpoint still points
24306ac39281SChristian Brauner  * to @mnt->mnt_mp->m_dentry. But if @mnt has been unmounted it will
24316ac39281SChristian Brauner  * point to @mnt->mnt_root and @mnt->mnt_mp will be NULL.
24326ac39281SChristian Brauner  *
24336ac39281SChristian Brauner  * Return: Either the target mountpoint on the top mount or the top
24346ac39281SChristian Brauner  *         mount's mountpoint.
24356ac39281SChristian Brauner  */
do_lock_mount(struct path * path,bool beneath)24366ac39281SChristian Brauner static struct mountpoint *do_lock_mount(struct path *path, bool beneath)
2437b12cea91SAl Viro {
24386ac39281SChristian Brauner 	struct vfsmount *mnt = path->mnt;
243964f44b27SChristian Brauner 	struct dentry *dentry;
24406ac39281SChristian Brauner 	struct mountpoint *mp = ERR_PTR(-ENOENT);
244164f44b27SChristian Brauner 
244264f44b27SChristian Brauner 	for (;;) {
24436ac39281SChristian Brauner 		struct mount *m;
24446ac39281SChristian Brauner 
24456ac39281SChristian Brauner 		if (beneath) {
24466ac39281SChristian Brauner 			m = real_mount(mnt);
24476ac39281SChristian Brauner 			read_seqlock_excl(&mount_lock);
24486ac39281SChristian Brauner 			dentry = dget(m->mnt_mountpoint);
24496ac39281SChristian Brauner 			read_sequnlock_excl(&mount_lock);
24506ac39281SChristian Brauner 		} else {
245164f44b27SChristian Brauner 			dentry = path->dentry;
24526ac39281SChristian Brauner 		}
24536ac39281SChristian Brauner 
24545955102cSAl Viro 		inode_lock(dentry->d_inode);
245584d17192SAl Viro 		if (unlikely(cant_mount(dentry))) {
24565955102cSAl Viro 			inode_unlock(dentry->d_inode);
24576ac39281SChristian Brauner 			goto out;
2458b12cea91SAl Viro 		}
245964f44b27SChristian Brauner 
246097216be0SAl Viro 		namespace_lock();
246164f44b27SChristian Brauner 
24626ac39281SChristian Brauner 		if (beneath && (!is_mounted(mnt) || m->mnt_mountpoint != dentry)) {
24636ac39281SChristian Brauner 			namespace_unlock();
24646ac39281SChristian Brauner 			inode_unlock(dentry->d_inode);
24656ac39281SChristian Brauner 			goto out;
24666ac39281SChristian Brauner 		}
24676ac39281SChristian Brauner 
2468b12cea91SAl Viro 		mnt = lookup_mnt(path);
246964f44b27SChristian Brauner 		if (likely(!mnt))
247064f44b27SChristian Brauner 			break;
247164f44b27SChristian Brauner 
247264f44b27SChristian Brauner 		namespace_unlock();
247364f44b27SChristian Brauner 		inode_unlock(dentry->d_inode);
24746ac39281SChristian Brauner 		if (beneath)
24756ac39281SChristian Brauner 			dput(dentry);
247664f44b27SChristian Brauner 		path_put(path);
247764f44b27SChristian Brauner 		path->mnt = mnt;
247864f44b27SChristian Brauner 		path->dentry = dget(mnt->mnt_root);
247964f44b27SChristian Brauner 	}
248064f44b27SChristian Brauner 
248164f44b27SChristian Brauner 	mp = get_mountpoint(dentry);
248284d17192SAl Viro 	if (IS_ERR(mp)) {
248397216be0SAl Viro 		namespace_unlock();
24845955102cSAl Viro 		inode_unlock(dentry->d_inode);
248584d17192SAl Viro 	}
248664f44b27SChristian Brauner 
24876ac39281SChristian Brauner out:
24886ac39281SChristian Brauner 	if (beneath)
24896ac39281SChristian Brauner 		dput(dentry);
24906ac39281SChristian Brauner 
249184d17192SAl Viro 	return mp;
249284d17192SAl Viro }
2493b12cea91SAl Viro 
lock_mount(struct path * path)24946ac39281SChristian Brauner static inline struct mountpoint *lock_mount(struct path *path)
24956ac39281SChristian Brauner {
24966ac39281SChristian Brauner 	return do_lock_mount(path, false);
2497b12cea91SAl Viro }
2498b12cea91SAl Viro 
unlock_mount(struct mountpoint * where)249984d17192SAl Viro static void unlock_mount(struct mountpoint *where)
2500b12cea91SAl Viro {
250184d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
25023895dbf8SEric W. Biederman 
25033895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
250484d17192SAl Viro 	put_mountpoint(where);
25053895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
25063895dbf8SEric W. Biederman 
2507328e6d90SAl Viro 	namespace_unlock();
25085955102cSAl Viro 	inode_unlock(dentry->d_inode);
2509b12cea91SAl Viro }
2510b12cea91SAl Viro 
graft_tree(struct mount * mnt,struct mount * p,struct mountpoint * mp)251184d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
25121da177e4SLinus Torvalds {
2513e462ec50SDavid Howells 	if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
25141da177e4SLinus Torvalds 		return -EINVAL;
25151da177e4SLinus Torvalds 
2516e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
2517e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
25181da177e4SLinus Torvalds 		return -ENOTDIR;
25191da177e4SLinus Torvalds 
25206ac39281SChristian Brauner 	return attach_recursive_mnt(mnt, p, mp, 0);
25211da177e4SLinus Torvalds }
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds /*
25247a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
25257a2e8a8fSValerie Aurora  */
25267a2e8a8fSValerie Aurora 
flags_to_propagation_type(int ms_flags)2527e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags)
25287a2e8a8fSValerie Aurora {
2529e462ec50SDavid Howells 	int type = ms_flags & ~(MS_REC | MS_SILENT);
25307a2e8a8fSValerie Aurora 
25317a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
25327a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
25337a2e8a8fSValerie Aurora 		return 0;
25347a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
25357a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
25367a2e8a8fSValerie Aurora 		return 0;
25377a2e8a8fSValerie Aurora 	return type;
25387a2e8a8fSValerie Aurora }
25397a2e8a8fSValerie Aurora 
25407a2e8a8fSValerie Aurora /*
254107b20889SRam Pai  * recursively change the type of the mountpoint.
254207b20889SRam Pai  */
do_change_type(struct path * path,int ms_flags)2543e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags)
254407b20889SRam Pai {
2545315fc83eSAl Viro 	struct mount *m;
25464b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
2547e462ec50SDavid Howells 	int recurse = ms_flags & MS_REC;
25487a2e8a8fSValerie Aurora 	int type;
2549719f5d7fSMiklos Szeredi 	int err = 0;
255007b20889SRam Pai 
255178aa08a8SChristian Brauner 	if (!path_mounted(path))
255207b20889SRam Pai 		return -EINVAL;
255307b20889SRam Pai 
2554e462ec50SDavid Howells 	type = flags_to_propagation_type(ms_flags);
25557a2e8a8fSValerie Aurora 	if (!type)
25567a2e8a8fSValerie Aurora 		return -EINVAL;
25577a2e8a8fSValerie Aurora 
255897216be0SAl Viro 	namespace_lock();
2559719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
2560719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
2561719f5d7fSMiklos Szeredi 		if (err)
2562719f5d7fSMiklos Szeredi 			goto out_unlock;
2563719f5d7fSMiklos Szeredi 	}
2564719f5d7fSMiklos Szeredi 
2565719ea2fbSAl Viro 	lock_mount_hash();
2566909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
25670f0afb1dSAl Viro 		change_mnt_propagation(m, type);
2568719ea2fbSAl Viro 	unlock_mount_hash();
2569719f5d7fSMiklos Szeredi 
2570719f5d7fSMiklos Szeredi  out_unlock:
257197216be0SAl Viro 	namespace_unlock();
2572719f5d7fSMiklos Szeredi 	return err;
257307b20889SRam Pai }
257407b20889SRam Pai 
__do_loopback(struct path * old_path,int recurse)2575a07b2000SAl Viro static struct mount *__do_loopback(struct path *old_path, int recurse)
2576a07b2000SAl Viro {
2577a07b2000SAl Viro 	struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2578a07b2000SAl Viro 
2579a07b2000SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2580a07b2000SAl Viro 		return mnt;
2581a07b2000SAl Viro 
2582a07b2000SAl Viro 	if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2583a07b2000SAl Viro 		return mnt;
2584a07b2000SAl Viro 
2585a07b2000SAl Viro 	if (!recurse && has_locked_children(old, old_path->dentry))
2586a07b2000SAl Viro 		return mnt;
2587a07b2000SAl Viro 
2588a07b2000SAl Viro 	if (recurse)
2589a07b2000SAl Viro 		mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2590a07b2000SAl Viro 	else
2591a07b2000SAl Viro 		mnt = clone_mnt(old, old_path->dentry, 0);
2592a07b2000SAl Viro 
2593a07b2000SAl Viro 	if (!IS_ERR(mnt))
2594a07b2000SAl Viro 		mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2595a07b2000SAl Viro 
2596a07b2000SAl Viro 	return mnt;
2597a07b2000SAl Viro }
2598a07b2000SAl Viro 
259907b20889SRam Pai /*
26001da177e4SLinus Torvalds  * do loopback mount.
26011da177e4SLinus Torvalds  */
do_loopback(struct path * path,const char * old_name,int recurse)2602808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
26032dafe1c4SEric Sandeen 				int recurse)
26041da177e4SLinus Torvalds {
26052d92ab3cSAl Viro 	struct path old_path;
2606a07b2000SAl Viro 	struct mount *mnt = NULL, *parent;
260784d17192SAl Viro 	struct mountpoint *mp;
260857eccb83SAl Viro 	int err;
26091da177e4SLinus Torvalds 	if (!old_name || !*old_name)
26101da177e4SLinus Torvalds 		return -EINVAL;
2611815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
26121da177e4SLinus Torvalds 	if (err)
26131da177e4SLinus Torvalds 		return err;
26141da177e4SLinus Torvalds 
26158823c079SEric W. Biederman 	err = -EINVAL;
26164ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
26178823c079SEric W. Biederman 		goto out;
26188823c079SEric W. Biederman 
261984d17192SAl Viro 	mp = lock_mount(path);
2620a07b2000SAl Viro 	if (IS_ERR(mp)) {
262184d17192SAl Viro 		err = PTR_ERR(mp);
26229676f0c6SRam Pai 		goto out;
2623a07b2000SAl Viro 	}
26249676f0c6SRam Pai 
262584d17192SAl Viro 	parent = real_mount(path->mnt);
2626e149ed2bSAl Viro 	if (!check_mnt(parent))
2627e149ed2bSAl Viro 		goto out2;
2628e149ed2bSAl Viro 
2629a07b2000SAl Viro 	mnt = __do_loopback(&old_path, recurse);
2630be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2631be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2632e9c5d8a5SAndrey Vagin 		goto out2;
2633be34d1a3SDavid Howells 	}
2634ccd48bc7SAl Viro 
263584d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
26361da177e4SLinus Torvalds 	if (err) {
2637719ea2fbSAl Viro 		lock_mount_hash();
2638e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2639719ea2fbSAl Viro 		unlock_mount_hash();
26405b83d2c5SRam Pai 	}
2641b12cea91SAl Viro out2:
264284d17192SAl Viro 	unlock_mount(mp);
2643ccd48bc7SAl Viro out:
26442d92ab3cSAl Viro 	path_put(&old_path);
26451da177e4SLinus Torvalds 	return err;
26461da177e4SLinus Torvalds }
26471da177e4SLinus Torvalds 
open_detached_copy(struct path * path,bool recursive)2648a07b2000SAl Viro static struct file *open_detached_copy(struct path *path, bool recursive)
2649a07b2000SAl Viro {
2650a07b2000SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2651a07b2000SAl Viro 	struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2652a07b2000SAl Viro 	struct mount *mnt, *p;
2653a07b2000SAl Viro 	struct file *file;
2654a07b2000SAl Viro 
2655a07b2000SAl Viro 	if (IS_ERR(ns))
2656a07b2000SAl Viro 		return ERR_CAST(ns);
2657a07b2000SAl Viro 
2658a07b2000SAl Viro 	namespace_lock();
2659a07b2000SAl Viro 	mnt = __do_loopback(path, recursive);
2660a07b2000SAl Viro 	if (IS_ERR(mnt)) {
2661a07b2000SAl Viro 		namespace_unlock();
2662a07b2000SAl Viro 		free_mnt_ns(ns);
2663a07b2000SAl Viro 		return ERR_CAST(mnt);
2664a07b2000SAl Viro 	}
2665a07b2000SAl Viro 
2666a07b2000SAl Viro 	lock_mount_hash();
2667a07b2000SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2668a07b2000SAl Viro 		p->mnt_ns = ns;
2669a07b2000SAl Viro 		ns->mounts++;
2670a07b2000SAl Viro 	}
2671a07b2000SAl Viro 	ns->root = mnt;
2672a07b2000SAl Viro 	list_add_tail(&ns->list, &mnt->mnt_list);
2673a07b2000SAl Viro 	mntget(&mnt->mnt);
2674a07b2000SAl Viro 	unlock_mount_hash();
2675a07b2000SAl Viro 	namespace_unlock();
2676a07b2000SAl Viro 
2677a07b2000SAl Viro 	mntput(path->mnt);
2678a07b2000SAl Viro 	path->mnt = &mnt->mnt;
2679a07b2000SAl Viro 	file = dentry_open(path, O_PATH, current_cred());
2680a07b2000SAl Viro 	if (IS_ERR(file))
2681a07b2000SAl Viro 		dissolve_on_fput(path->mnt);
2682a07b2000SAl Viro 	else
2683a07b2000SAl Viro 		file->f_mode |= FMODE_NEED_UNMOUNT;
2684a07b2000SAl Viro 	return file;
2685a07b2000SAl Viro }
2686a07b2000SAl Viro 
SYSCALL_DEFINE3(open_tree,int,dfd,const char __user *,filename,unsigned,flags)26872658ce09SBen Dooks SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
2688a07b2000SAl Viro {
2689a07b2000SAl Viro 	struct file *file;
2690a07b2000SAl Viro 	struct path path;
2691a07b2000SAl Viro 	int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2692a07b2000SAl Viro 	bool detached = flags & OPEN_TREE_CLONE;
2693a07b2000SAl Viro 	int error;
2694a07b2000SAl Viro 	int fd;
2695a07b2000SAl Viro 
2696a07b2000SAl Viro 	BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2697a07b2000SAl Viro 
2698a07b2000SAl Viro 	if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2699a07b2000SAl Viro 		      AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2700a07b2000SAl Viro 		      OPEN_TREE_CLOEXEC))
2701a07b2000SAl Viro 		return -EINVAL;
2702a07b2000SAl Viro 
2703a07b2000SAl Viro 	if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2704a07b2000SAl Viro 		return -EINVAL;
2705a07b2000SAl Viro 
2706a07b2000SAl Viro 	if (flags & AT_NO_AUTOMOUNT)
2707a07b2000SAl Viro 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
2708a07b2000SAl Viro 	if (flags & AT_SYMLINK_NOFOLLOW)
2709a07b2000SAl Viro 		lookup_flags &= ~LOOKUP_FOLLOW;
2710a07b2000SAl Viro 	if (flags & AT_EMPTY_PATH)
2711a07b2000SAl Viro 		lookup_flags |= LOOKUP_EMPTY;
2712a07b2000SAl Viro 
2713a07b2000SAl Viro 	if (detached && !may_mount())
2714a07b2000SAl Viro 		return -EPERM;
2715a07b2000SAl Viro 
2716a07b2000SAl Viro 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
2717a07b2000SAl Viro 	if (fd < 0)
2718a07b2000SAl Viro 		return fd;
2719a07b2000SAl Viro 
2720a07b2000SAl Viro 	error = user_path_at(dfd, filename, lookup_flags, &path);
2721a07b2000SAl Viro 	if (unlikely(error)) {
2722a07b2000SAl Viro 		file = ERR_PTR(error);
2723a07b2000SAl Viro 	} else {
2724a07b2000SAl Viro 		if (detached)
2725a07b2000SAl Viro 			file = open_detached_copy(&path, flags & AT_RECURSIVE);
2726a07b2000SAl Viro 		else
2727a07b2000SAl Viro 			file = dentry_open(&path, O_PATH, current_cred());
2728a07b2000SAl Viro 		path_put(&path);
2729a07b2000SAl Viro 	}
2730a07b2000SAl Viro 	if (IS_ERR(file)) {
2731a07b2000SAl Viro 		put_unused_fd(fd);
2732a07b2000SAl Viro 		return PTR_ERR(file);
2733a07b2000SAl Viro 	}
2734a07b2000SAl Viro 	fd_install(fd, file);
2735a07b2000SAl Viro 	return fd;
2736a07b2000SAl Viro }
2737a07b2000SAl Viro 
273843f5e655SDavid Howells /*
273943f5e655SDavid Howells  * Don't allow locked mount flags to be cleared.
274043f5e655SDavid Howells  *
274143f5e655SDavid Howells  * No locks need to be held here while testing the various MNT_LOCK
274243f5e655SDavid Howells  * flags because those flags can never be cleared once they are set.
274343f5e655SDavid Howells  */
can_change_locked_flags(struct mount * mnt,unsigned int mnt_flags)274443f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
27452e4b7fcdSDave Hansen {
274643f5e655SDavid Howells 	unsigned int fl = mnt->mnt.mnt_flags;
27472e4b7fcdSDave Hansen 
274843f5e655SDavid Howells 	if ((fl & MNT_LOCK_READONLY) &&
274943f5e655SDavid Howells 	    !(mnt_flags & MNT_READONLY))
275043f5e655SDavid Howells 		return false;
275143f5e655SDavid Howells 
275243f5e655SDavid Howells 	if ((fl & MNT_LOCK_NODEV) &&
275343f5e655SDavid Howells 	    !(mnt_flags & MNT_NODEV))
275443f5e655SDavid Howells 		return false;
275543f5e655SDavid Howells 
275643f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOSUID) &&
275743f5e655SDavid Howells 	    !(mnt_flags & MNT_NOSUID))
275843f5e655SDavid Howells 		return false;
275943f5e655SDavid Howells 
276043f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOEXEC) &&
276143f5e655SDavid Howells 	    !(mnt_flags & MNT_NOEXEC))
276243f5e655SDavid Howells 		return false;
276343f5e655SDavid Howells 
276443f5e655SDavid Howells 	if ((fl & MNT_LOCK_ATIME) &&
276543f5e655SDavid Howells 	    ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
276643f5e655SDavid Howells 		return false;
276743f5e655SDavid Howells 
276843f5e655SDavid Howells 	return true;
276943f5e655SDavid Howells }
277043f5e655SDavid Howells 
change_mount_ro_state(struct mount * mnt,unsigned int mnt_flags)277143f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
277243f5e655SDavid Howells {
277343f5e655SDavid Howells 	bool readonly_request = (mnt_flags & MNT_READONLY);
277443f5e655SDavid Howells 
277543f5e655SDavid Howells 	if (readonly_request == __mnt_is_readonly(&mnt->mnt))
27762e4b7fcdSDave Hansen 		return 0;
27772e4b7fcdSDave Hansen 
27782e4b7fcdSDave Hansen 	if (readonly_request)
277943f5e655SDavid Howells 		return mnt_make_readonly(mnt);
278043f5e655SDavid Howells 
278168847c94SChristian Brauner 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
278268847c94SChristian Brauner 	return 0;
278343f5e655SDavid Howells }
278443f5e655SDavid Howells 
set_mount_attributes(struct mount * mnt,unsigned int mnt_flags)278543f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
278643f5e655SDavid Howells {
278743f5e655SDavid Howells 	mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
278843f5e655SDavid Howells 	mnt->mnt.mnt_flags = mnt_flags;
278943f5e655SDavid Howells 	touch_mnt_namespace(mnt->mnt_ns);
279043f5e655SDavid Howells }
279143f5e655SDavid Howells 
mnt_warn_timestamp_expiry(struct path * mountpoint,struct vfsmount * mnt)2792f8b92ba6SDeepa Dinamani static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
2793f8b92ba6SDeepa Dinamani {
2794f8b92ba6SDeepa Dinamani 	struct super_block *sb = mnt->mnt_sb;
2795f8b92ba6SDeepa Dinamani 
2796f8b92ba6SDeepa Dinamani 	if (!__mnt_is_readonly(mnt) &&
2797a128b054SAnthony Iliopoulos 	   (!(sb->s_iflags & SB_I_TS_EXPIRY_WARNED)) &&
2798f8b92ba6SDeepa Dinamani 	   (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
2799*c82ea72dSOlaf Hering 		char *buf, *mntpath;
2800*c82ea72dSOlaf Hering 
2801*c82ea72dSOlaf Hering 		buf = (char *)__get_free_page(GFP_KERNEL);
2802*c82ea72dSOlaf Hering 		if (buf)
2803*c82ea72dSOlaf Hering 			mntpath = d_path(mountpoint, buf, PAGE_SIZE);
2804*c82ea72dSOlaf Hering 		else
2805*c82ea72dSOlaf Hering 			mntpath = ERR_PTR(-ENOMEM);
2806*c82ea72dSOlaf Hering 		if (IS_ERR(mntpath))
2807*c82ea72dSOlaf Hering 			mntpath = "(unknown)";
2808f8b92ba6SDeepa Dinamani 
280974e60b8bSAndy Shevchenko 		pr_warn("%s filesystem being %s at %s supports timestamps until %ptTd (0x%llx)\n",
28100ecee669SEric Biggers 			sb->s_type->name,
28110ecee669SEric Biggers 			is_mounted(mnt) ? "remounted" : "mounted",
281274e60b8bSAndy Shevchenko 			mntpath, &sb->s_time_max,
281374e60b8bSAndy Shevchenko 			(unsigned long long)sb->s_time_max);
2814f8b92ba6SDeepa Dinamani 
2815a128b054SAnthony Iliopoulos 		sb->s_iflags |= SB_I_TS_EXPIRY_WARNED;
2816*c82ea72dSOlaf Hering 		if (buf)
2817*c82ea72dSOlaf Hering 			free_page((unsigned long)buf);
2818f8b92ba6SDeepa Dinamani 	}
2819f8b92ba6SDeepa Dinamani }
2820f8b92ba6SDeepa Dinamani 
282143f5e655SDavid Howells /*
282243f5e655SDavid Howells  * Handle reconfiguration of the mountpoint only without alteration of the
282343f5e655SDavid Howells  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
282443f5e655SDavid Howells  * to mount(2).
282543f5e655SDavid Howells  */
do_reconfigure_mnt(struct path * path,unsigned int mnt_flags)282643f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
282743f5e655SDavid Howells {
282843f5e655SDavid Howells 	struct super_block *sb = path->mnt->mnt_sb;
282943f5e655SDavid Howells 	struct mount *mnt = real_mount(path->mnt);
283043f5e655SDavid Howells 	int ret;
283143f5e655SDavid Howells 
283243f5e655SDavid Howells 	if (!check_mnt(mnt))
283343f5e655SDavid Howells 		return -EINVAL;
283443f5e655SDavid Howells 
283578aa08a8SChristian Brauner 	if (!path_mounted(path))
283643f5e655SDavid Howells 		return -EINVAL;
283743f5e655SDavid Howells 
283843f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
283943f5e655SDavid Howells 		return -EPERM;
284043f5e655SDavid Howells 
2841e58ace1aSChristian Brauner 	/*
2842e58ace1aSChristian Brauner 	 * We're only checking whether the superblock is read-only not
2843e58ace1aSChristian Brauner 	 * changing it, so only take down_read(&sb->s_umount).
2844e58ace1aSChristian Brauner 	 */
2845e58ace1aSChristian Brauner 	down_read(&sb->s_umount);
284668847c94SChristian Brauner 	lock_mount_hash();
284743f5e655SDavid Howells 	ret = change_mount_ro_state(mnt, mnt_flags);
284843f5e655SDavid Howells 	if (ret == 0)
284943f5e655SDavid Howells 		set_mount_attributes(mnt, mnt_flags);
285068847c94SChristian Brauner 	unlock_mount_hash();
2851e58ace1aSChristian Brauner 	up_read(&sb->s_umount);
2852f8b92ba6SDeepa Dinamani 
2853f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2854f8b92ba6SDeepa Dinamani 
285543f5e655SDavid Howells 	return ret;
28562e4b7fcdSDave Hansen }
28572e4b7fcdSDave Hansen 
28581da177e4SLinus Torvalds /*
28591da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
28601da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
28611da177e4SLinus Torvalds  * on it - tough luck.
28621da177e4SLinus Torvalds  */
do_remount(struct path * path,int ms_flags,int sb_flags,int mnt_flags,void * data)2863e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags,
2864e462ec50SDavid Howells 		      int mnt_flags, void *data)
28651da177e4SLinus Torvalds {
28661da177e4SLinus Torvalds 	int err;
28672d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2868143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
28698d0347f6SDavid Howells 	struct fs_context *fc;
28701da177e4SLinus Torvalds 
2871143c8c91SAl Viro 	if (!check_mnt(mnt))
28721da177e4SLinus Torvalds 		return -EINVAL;
28731da177e4SLinus Torvalds 
287478aa08a8SChristian Brauner 	if (!path_mounted(path))
28751da177e4SLinus Torvalds 		return -EINVAL;
28761da177e4SLinus Torvalds 
287743f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
287807b64558SEric W. Biederman 		return -EPERM;
28799566d674SEric W. Biederman 
28808d0347f6SDavid Howells 	fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
28818d0347f6SDavid Howells 	if (IS_ERR(fc))
28828d0347f6SDavid Howells 		return PTR_ERR(fc);
2883ff36fe2cSEric Paris 
28845248b445SChristian Brauner 	/*
28855248b445SChristian Brauner 	 * Indicate to the filesystem that the remount request is coming
28865248b445SChristian Brauner 	 * from the legacy mount system call.
28875248b445SChristian Brauner 	 */
2888b330966fSMiklos Szeredi 	fc->oldapi = true;
28895248b445SChristian Brauner 
28908d0347f6SDavid Howells 	err = parse_monolithic_mount_data(fc, data);
28918d0347f6SDavid Howells 	if (!err) {
28921da177e4SLinus Torvalds 		down_write(&sb->s_umount);
289357eccb83SAl Viro 		err = -EPERM;
289443f5e655SDavid Howells 		if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
28958d0347f6SDavid Howells 			err = reconfigure_super(fc);
289668847c94SChristian Brauner 			if (!err) {
289768847c94SChristian Brauner 				lock_mount_hash();
289843f5e655SDavid Howells 				set_mount_attributes(mnt, mnt_flags);
289968847c94SChristian Brauner 				unlock_mount_hash();
290068847c94SChristian Brauner 			}
29010e55a7ccSDan Williams 		}
29026339dab8SAl Viro 		up_write(&sb->s_umount);
29038d0347f6SDavid Howells 	}
2904f8b92ba6SDeepa Dinamani 
2905f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2906f8b92ba6SDeepa Dinamani 
29078d0347f6SDavid Howells 	put_fs_context(fc);
29081da177e4SLinus Torvalds 	return err;
29091da177e4SLinus Torvalds }
29101da177e4SLinus Torvalds 
tree_contains_unbindable(struct mount * mnt)2911cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
29129676f0c6SRam Pai {
2913315fc83eSAl Viro 	struct mount *p;
2914909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2915fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
29169676f0c6SRam Pai 			return 1;
29179676f0c6SRam Pai 	}
29189676f0c6SRam Pai 	return 0;
29199676f0c6SRam Pai }
29209676f0c6SRam Pai 
292144dfd84aSDavid Howells /*
292244dfd84aSDavid Howells  * Check that there aren't references to earlier/same mount namespaces in the
292344dfd84aSDavid Howells  * specified subtree.  Such references can act as pins for mount namespaces
292444dfd84aSDavid Howells  * that aren't checked by the mount-cycle checking code, thereby allowing
292544dfd84aSDavid Howells  * cycles to be made.
292644dfd84aSDavid Howells  */
check_for_nsfs_mounts(struct mount * subtree)292744dfd84aSDavid Howells static bool check_for_nsfs_mounts(struct mount *subtree)
292844dfd84aSDavid Howells {
292944dfd84aSDavid Howells 	struct mount *p;
293044dfd84aSDavid Howells 	bool ret = false;
293144dfd84aSDavid Howells 
293244dfd84aSDavid Howells 	lock_mount_hash();
293344dfd84aSDavid Howells 	for (p = subtree; p; p = next_mnt(p, subtree))
293444dfd84aSDavid Howells 		if (mnt_ns_loop(p->mnt.mnt_root))
293544dfd84aSDavid Howells 			goto out;
293644dfd84aSDavid Howells 
293744dfd84aSDavid Howells 	ret = true;
293844dfd84aSDavid Howells out:
293944dfd84aSDavid Howells 	unlock_mount_hash();
294044dfd84aSDavid Howells 	return ret;
294144dfd84aSDavid Howells }
294244dfd84aSDavid Howells 
do_set_group(struct path * from_path,struct path * to_path)29439ffb14efSPavel Tikhomirov static int do_set_group(struct path *from_path, struct path *to_path)
29449ffb14efSPavel Tikhomirov {
29459ffb14efSPavel Tikhomirov 	struct mount *from, *to;
29469ffb14efSPavel Tikhomirov 	int err;
29479ffb14efSPavel Tikhomirov 
29489ffb14efSPavel Tikhomirov 	from = real_mount(from_path->mnt);
29499ffb14efSPavel Tikhomirov 	to = real_mount(to_path->mnt);
29509ffb14efSPavel Tikhomirov 
29519ffb14efSPavel Tikhomirov 	namespace_lock();
29529ffb14efSPavel Tikhomirov 
29539ffb14efSPavel Tikhomirov 	err = -EINVAL;
29549ffb14efSPavel Tikhomirov 	/* To and From must be mounted */
29559ffb14efSPavel Tikhomirov 	if (!is_mounted(&from->mnt))
29569ffb14efSPavel Tikhomirov 		goto out;
29579ffb14efSPavel Tikhomirov 	if (!is_mounted(&to->mnt))
29589ffb14efSPavel Tikhomirov 		goto out;
29599ffb14efSPavel Tikhomirov 
29609ffb14efSPavel Tikhomirov 	err = -EPERM;
29619ffb14efSPavel Tikhomirov 	/* We should be allowed to modify mount namespaces of both mounts */
29629ffb14efSPavel Tikhomirov 	if (!ns_capable(from->mnt_ns->user_ns, CAP_SYS_ADMIN))
29639ffb14efSPavel Tikhomirov 		goto out;
29649ffb14efSPavel Tikhomirov 	if (!ns_capable(to->mnt_ns->user_ns, CAP_SYS_ADMIN))
29659ffb14efSPavel Tikhomirov 		goto out;
29669ffb14efSPavel Tikhomirov 
29679ffb14efSPavel Tikhomirov 	err = -EINVAL;
29689ffb14efSPavel Tikhomirov 	/* To and From paths should be mount roots */
296978aa08a8SChristian Brauner 	if (!path_mounted(from_path))
29709ffb14efSPavel Tikhomirov 		goto out;
297178aa08a8SChristian Brauner 	if (!path_mounted(to_path))
29729ffb14efSPavel Tikhomirov 		goto out;
29739ffb14efSPavel Tikhomirov 
29749ffb14efSPavel Tikhomirov 	/* Setting sharing groups is only allowed across same superblock */
29759ffb14efSPavel Tikhomirov 	if (from->mnt.mnt_sb != to->mnt.mnt_sb)
29769ffb14efSPavel Tikhomirov 		goto out;
29779ffb14efSPavel Tikhomirov 
29789ffb14efSPavel Tikhomirov 	/* From mount root should be wider than To mount root */
29799ffb14efSPavel Tikhomirov 	if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root))
29809ffb14efSPavel Tikhomirov 		goto out;
29819ffb14efSPavel Tikhomirov 
29829ffb14efSPavel Tikhomirov 	/* From mount should not have locked children in place of To's root */
29839ffb14efSPavel Tikhomirov 	if (has_locked_children(from, to->mnt.mnt_root))
29849ffb14efSPavel Tikhomirov 		goto out;
29859ffb14efSPavel Tikhomirov 
29869ffb14efSPavel Tikhomirov 	/* Setting sharing groups is only allowed on private mounts */
29879ffb14efSPavel Tikhomirov 	if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to))
29889ffb14efSPavel Tikhomirov 		goto out;
29899ffb14efSPavel Tikhomirov 
29909ffb14efSPavel Tikhomirov 	/* From should not be private */
29919ffb14efSPavel Tikhomirov 	if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from))
29929ffb14efSPavel Tikhomirov 		goto out;
29939ffb14efSPavel Tikhomirov 
29949ffb14efSPavel Tikhomirov 	if (IS_MNT_SLAVE(from)) {
29959ffb14efSPavel Tikhomirov 		struct mount *m = from->mnt_master;
29969ffb14efSPavel Tikhomirov 
29979ffb14efSPavel Tikhomirov 		list_add(&to->mnt_slave, &m->mnt_slave_list);
29989ffb14efSPavel Tikhomirov 		to->mnt_master = m;
29999ffb14efSPavel Tikhomirov 	}
30009ffb14efSPavel Tikhomirov 
30019ffb14efSPavel Tikhomirov 	if (IS_MNT_SHARED(from)) {
30029ffb14efSPavel Tikhomirov 		to->mnt_group_id = from->mnt_group_id;
30039ffb14efSPavel Tikhomirov 		list_add(&to->mnt_share, &from->mnt_share);
30049ffb14efSPavel Tikhomirov 		lock_mount_hash();
30059ffb14efSPavel Tikhomirov 		set_mnt_shared(to);
30069ffb14efSPavel Tikhomirov 		unlock_mount_hash();
30079ffb14efSPavel Tikhomirov 	}
30089ffb14efSPavel Tikhomirov 
30099ffb14efSPavel Tikhomirov 	err = 0;
30109ffb14efSPavel Tikhomirov out:
30119ffb14efSPavel Tikhomirov 	namespace_unlock();
30129ffb14efSPavel Tikhomirov 	return err;
30139ffb14efSPavel Tikhomirov }
30149ffb14efSPavel Tikhomirov 
30156ac39281SChristian Brauner /**
30166ac39281SChristian Brauner  * path_overmounted - check if path is overmounted
30176ac39281SChristian Brauner  * @path: path to check
30186ac39281SChristian Brauner  *
30196ac39281SChristian Brauner  * Check if path is overmounted, i.e., if there's a mount on top of
30206ac39281SChristian Brauner  * @path->mnt with @path->dentry as mountpoint.
30216ac39281SChristian Brauner  *
30226ac39281SChristian Brauner  * Context: This function expects namespace_lock() to be held.
30236ac39281SChristian Brauner  * Return: If path is overmounted true is returned, false if not.
30246ac39281SChristian Brauner  */
path_overmounted(const struct path * path)30256ac39281SChristian Brauner static inline bool path_overmounted(const struct path *path)
30266ac39281SChristian Brauner {
30276ac39281SChristian Brauner 	rcu_read_lock();
30286ac39281SChristian Brauner 	if (unlikely(__lookup_mnt(path->mnt, path->dentry))) {
30296ac39281SChristian Brauner 		rcu_read_unlock();
30306ac39281SChristian Brauner 		return true;
30316ac39281SChristian Brauner 	}
30326ac39281SChristian Brauner 	rcu_read_unlock();
30336ac39281SChristian Brauner 	return false;
30346ac39281SChristian Brauner }
30356ac39281SChristian Brauner 
30366ac39281SChristian Brauner /**
30376ac39281SChristian Brauner  * can_move_mount_beneath - check that we can mount beneath the top mount
30386ac39281SChristian Brauner  * @from: mount to mount beneath
30396ac39281SChristian Brauner  * @to:   mount under which to mount
30406ac39281SChristian Brauner  *
30416ac39281SChristian Brauner  * - Make sure that @to->dentry is actually the root of a mount under
30426ac39281SChristian Brauner  *   which we can mount another mount.
30436ac39281SChristian Brauner  * - Make sure that nothing can be mounted beneath the caller's current
30446ac39281SChristian Brauner  *   root or the rootfs of the namespace.
30456ac39281SChristian Brauner  * - Make sure that the caller can unmount the topmost mount ensuring
30466ac39281SChristian Brauner  *   that the caller could reveal the underlying mountpoint.
30476ac39281SChristian Brauner  * - Ensure that nothing has been mounted on top of @from before we
30486ac39281SChristian Brauner  *   grabbed @namespace_sem to avoid creating pointless shadow mounts.
30496ac39281SChristian Brauner  * - Prevent mounting beneath a mount if the propagation relationship
30506ac39281SChristian Brauner  *   between the source mount, parent mount, and top mount would lead to
30516ac39281SChristian Brauner  *   nonsensical mount trees.
30526ac39281SChristian Brauner  *
30536ac39281SChristian Brauner  * Context: This function expects namespace_lock() to be held.
30546ac39281SChristian Brauner  * Return: On success 0, and on error a negative error code is returned.
30556ac39281SChristian Brauner  */
can_move_mount_beneath(const struct path * from,const struct path * to,const struct mountpoint * mp)30566ac39281SChristian Brauner static int can_move_mount_beneath(const struct path *from,
30576ac39281SChristian Brauner 				  const struct path *to,
30586ac39281SChristian Brauner 				  const struct mountpoint *mp)
30596ac39281SChristian Brauner {
30606ac39281SChristian Brauner 	struct mount *mnt_from = real_mount(from->mnt),
30616ac39281SChristian Brauner 		     *mnt_to = real_mount(to->mnt),
30626ac39281SChristian Brauner 		     *parent_mnt_to = mnt_to->mnt_parent;
30636ac39281SChristian Brauner 
30646ac39281SChristian Brauner 	if (!mnt_has_parent(mnt_to))
30656ac39281SChristian Brauner 		return -EINVAL;
30666ac39281SChristian Brauner 
30676ac39281SChristian Brauner 	if (!path_mounted(to))
30686ac39281SChristian Brauner 		return -EINVAL;
30696ac39281SChristian Brauner 
30706ac39281SChristian Brauner 	if (IS_MNT_LOCKED(mnt_to))
30716ac39281SChristian Brauner 		return -EINVAL;
30726ac39281SChristian Brauner 
30736ac39281SChristian Brauner 	/* Avoid creating shadow mounts during mount propagation. */
30746ac39281SChristian Brauner 	if (path_overmounted(from))
30756ac39281SChristian Brauner 		return -EINVAL;
30766ac39281SChristian Brauner 
30776ac39281SChristian Brauner 	/*
30786ac39281SChristian Brauner 	 * Mounting beneath the rootfs only makes sense when the
30796ac39281SChristian Brauner 	 * semantics of pivot_root(".", ".") are used.
30806ac39281SChristian Brauner 	 */
30816ac39281SChristian Brauner 	if (&mnt_to->mnt == current->fs->root.mnt)
30826ac39281SChristian Brauner 		return -EINVAL;
30836ac39281SChristian Brauner 	if (parent_mnt_to == current->nsproxy->mnt_ns->root)
30846ac39281SChristian Brauner 		return -EINVAL;
30856ac39281SChristian Brauner 
30866ac39281SChristian Brauner 	for (struct mount *p = mnt_from; mnt_has_parent(p); p = p->mnt_parent)
30876ac39281SChristian Brauner 		if (p == mnt_to)
30886ac39281SChristian Brauner 			return -EINVAL;
30896ac39281SChristian Brauner 
30906ac39281SChristian Brauner 	/*
30916ac39281SChristian Brauner 	 * If the parent mount propagates to the child mount this would
30926ac39281SChristian Brauner 	 * mean mounting @mnt_from on @mnt_to->mnt_parent and then
30936ac39281SChristian Brauner 	 * propagating a copy @c of @mnt_from on top of @mnt_to. This
30946ac39281SChristian Brauner 	 * defeats the whole purpose of mounting beneath another mount.
30956ac39281SChristian Brauner 	 */
30966ac39281SChristian Brauner 	if (propagation_would_overmount(parent_mnt_to, mnt_to, mp))
30976ac39281SChristian Brauner 		return -EINVAL;
30986ac39281SChristian Brauner 
30996ac39281SChristian Brauner 	/*
31006ac39281SChristian Brauner 	 * If @mnt_to->mnt_parent propagates to @mnt_from this would
31016ac39281SChristian Brauner 	 * mean propagating a copy @c of @mnt_from on top of @mnt_from.
31026ac39281SChristian Brauner 	 * Afterwards @mnt_from would be mounted on top of
31036ac39281SChristian Brauner 	 * @mnt_to->mnt_parent and @mnt_to would be unmounted from
31046ac39281SChristian Brauner 	 * @mnt->mnt_parent and remounted on @mnt_from. But since @c is
31056ac39281SChristian Brauner 	 * already mounted on @mnt_from, @mnt_to would ultimately be
31066ac39281SChristian Brauner 	 * remounted on top of @c. Afterwards, @mnt_from would be
31076ac39281SChristian Brauner 	 * covered by a copy @c of @mnt_from and @c would be covered by
31086ac39281SChristian Brauner 	 * @mnt_from itself. This defeats the whole purpose of mounting
31096ac39281SChristian Brauner 	 * @mnt_from beneath @mnt_to.
31106ac39281SChristian Brauner 	 */
31116ac39281SChristian Brauner 	if (propagation_would_overmount(parent_mnt_to, mnt_from, mp))
31126ac39281SChristian Brauner 		return -EINVAL;
31136ac39281SChristian Brauner 
31146ac39281SChristian Brauner 	return 0;
31156ac39281SChristian Brauner }
31166ac39281SChristian Brauner 
do_move_mount(struct path * old_path,struct path * new_path,bool beneath)31176ac39281SChristian Brauner static int do_move_mount(struct path *old_path, struct path *new_path,
31186ac39281SChristian Brauner 			 bool beneath)
31191da177e4SLinus Torvalds {
312044dfd84aSDavid Howells 	struct mnt_namespace *ns;
3121676da58dSAl Viro 	struct mount *p;
31220fb54e50SAl Viro 	struct mount *old;
31232763d119SAl Viro 	struct mount *parent;
31242763d119SAl Viro 	struct mountpoint *mp, *old_mp;
312557eccb83SAl Viro 	int err;
312644dfd84aSDavid Howells 	bool attached;
31276ac39281SChristian Brauner 	enum mnt_tree_flags_t flags = 0;
31281da177e4SLinus Torvalds 
31296ac39281SChristian Brauner 	mp = do_lock_mount(new_path, beneath);
313084d17192SAl Viro 	if (IS_ERR(mp))
31312db154b3SDavid Howells 		return PTR_ERR(mp);
3132cc53ce53SDavid Howells 
31332db154b3SDavid Howells 	old = real_mount(old_path->mnt);
31342db154b3SDavid Howells 	p = real_mount(new_path->mnt);
31352763d119SAl Viro 	parent = old->mnt_parent;
313644dfd84aSDavid Howells 	attached = mnt_has_parent(old);
31376ac39281SChristian Brauner 	if (attached)
31386ac39281SChristian Brauner 		flags |= MNT_TREE_MOVE;
31392763d119SAl Viro 	old_mp = old->mnt_mp;
314044dfd84aSDavid Howells 	ns = old->mnt_ns;
3141143c8c91SAl Viro 
31421da177e4SLinus Torvalds 	err = -EINVAL;
314344dfd84aSDavid Howells 	/* The mountpoint must be in our namespace. */
314444dfd84aSDavid Howells 	if (!check_mnt(p))
31452db154b3SDavid Howells 		goto out;
31461da177e4SLinus Torvalds 
3147570d7a98SEric Biggers 	/* The thing moved must be mounted... */
3148570d7a98SEric Biggers 	if (!is_mounted(&old->mnt))
314944dfd84aSDavid Howells 		goto out;
315044dfd84aSDavid Howells 
3151570d7a98SEric Biggers 	/* ... and either ours or the root of anon namespace */
3152570d7a98SEric Biggers 	if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
31532db154b3SDavid Howells 		goto out;
31541da177e4SLinus Torvalds 
31552db154b3SDavid Howells 	if (old->mnt.mnt_flags & MNT_LOCKED)
31562db154b3SDavid Howells 		goto out;
31572db154b3SDavid Howells 
315878aa08a8SChristian Brauner 	if (!path_mounted(old_path))
31592db154b3SDavid Howells 		goto out;
31602db154b3SDavid Howells 
31612db154b3SDavid Howells 	if (d_is_dir(new_path->dentry) !=
31622db154b3SDavid Howells 	    d_is_dir(old_path->dentry))
31632db154b3SDavid Howells 		goto out;
316421444403SRam Pai 	/*
316521444403SRam Pai 	 * Don't move a mount residing in a shared parent.
316621444403SRam Pai 	 */
31672763d119SAl Viro 	if (attached && IS_MNT_SHARED(parent))
31682db154b3SDavid Howells 		goto out;
31696ac39281SChristian Brauner 
31706ac39281SChristian Brauner 	if (beneath) {
31716ac39281SChristian Brauner 		err = can_move_mount_beneath(old_path, new_path, mp);
31726ac39281SChristian Brauner 		if (err)
31736ac39281SChristian Brauner 			goto out;
31746ac39281SChristian Brauner 
31756ac39281SChristian Brauner 		err = -EINVAL;
31766ac39281SChristian Brauner 		p = p->mnt_parent;
31776ac39281SChristian Brauner 		flags |= MNT_TREE_BENEATH;
31786ac39281SChristian Brauner 	}
31796ac39281SChristian Brauner 
31809676f0c6SRam Pai 	/*
31819676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
31829676f0c6SRam Pai 	 * mount which is shared.
31839676f0c6SRam Pai 	 */
3184fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
31852db154b3SDavid Howells 		goto out;
31861da177e4SLinus Torvalds 	err = -ELOOP;
318744dfd84aSDavid Howells 	if (!check_for_nsfs_mounts(old))
318844dfd84aSDavid Howells 		goto out;
3189fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
3190676da58dSAl Viro 		if (p == old)
31912db154b3SDavid Howells 			goto out;
31921da177e4SLinus Torvalds 
31936ac39281SChristian Brauner 	err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp, flags);
31944ac91378SJan Blunck 	if (err)
31952db154b3SDavid Howells 		goto out;
31961da177e4SLinus Torvalds 
31971da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
31981da177e4SLinus Torvalds 	 * automatically */
31996776db3dSAl Viro 	list_del_init(&old->mnt_expire);
32002763d119SAl Viro 	if (attached)
32012763d119SAl Viro 		put_mountpoint(old_mp);
32021da177e4SLinus Torvalds out:
32032db154b3SDavid Howells 	unlock_mount(mp);
320444dfd84aSDavid Howells 	if (!err) {
32052763d119SAl Viro 		if (attached)
32062763d119SAl Viro 			mntput_no_expire(parent);
32072763d119SAl Viro 		else
320844dfd84aSDavid Howells 			free_mnt_ns(ns);
320944dfd84aSDavid Howells 	}
32102db154b3SDavid Howells 	return err;
32112db154b3SDavid Howells }
32122db154b3SDavid Howells 
do_move_mount_old(struct path * path,const char * old_name)32132db154b3SDavid Howells static int do_move_mount_old(struct path *path, const char *old_name)
32142db154b3SDavid Howells {
32152db154b3SDavid Howells 	struct path old_path;
32162db154b3SDavid Howells 	int err;
32172db154b3SDavid Howells 
32182db154b3SDavid Howells 	if (!old_name || !*old_name)
32192db154b3SDavid Howells 		return -EINVAL;
32202db154b3SDavid Howells 
32212db154b3SDavid Howells 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
32222db154b3SDavid Howells 	if (err)
32232db154b3SDavid Howells 		return err;
32242db154b3SDavid Howells 
32256ac39281SChristian Brauner 	err = do_move_mount(&old_path, path, false);
32262d92ab3cSAl Viro 	path_put(&old_path);
32271da177e4SLinus Torvalds 	return err;
32281da177e4SLinus Torvalds }
32291da177e4SLinus Torvalds 
32309d412a43SAl Viro /*
32319d412a43SAl Viro  * add a mount into a namespace's mount tree
32329d412a43SAl Viro  */
do_add_mount(struct mount * newmnt,struct mountpoint * mp,const struct path * path,int mnt_flags)32338f11538eSAl Viro static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
32341e2d8464SAl Viro 			const struct path *path, int mnt_flags)
32359d412a43SAl Viro {
32368f11538eSAl Viro 	struct mount *parent = real_mount(path->mnt);
32379d412a43SAl Viro 
3238f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
32399d412a43SAl Viro 
324084d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
3241156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
3242156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
32438f11538eSAl Viro 			return -EINVAL;
3244156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
324584d17192SAl Viro 		if (!parent->mnt_ns)
32468f11538eSAl Viro 			return -EINVAL;
3247156cacb1SAl Viro 	}
32489d412a43SAl Viro 
32499d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
325078aa08a8SChristian Brauner 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && path_mounted(path))
32518f11538eSAl Viro 		return -EBUSY;
32529d412a43SAl Viro 
3253e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
32548f11538eSAl Viro 		return -EINVAL;
32559d412a43SAl Viro 
325695bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
32578f11538eSAl Viro 	return graft_tree(newmnt, parent, mp);
32589d412a43SAl Viro }
3259b1e75df4SAl Viro 
3260132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
3261132e4608SDavid Howells 
3262132e4608SDavid Howells /*
3263132e4608SDavid Howells  * Create a new mount using a superblock configuration and request it
3264132e4608SDavid Howells  * be added to the namespace tree.
3265132e4608SDavid Howells  */
do_new_mount_fc(struct fs_context * fc,struct path * mountpoint,unsigned int mnt_flags)3266132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
3267132e4608SDavid Howells 			   unsigned int mnt_flags)
3268132e4608SDavid Howells {
3269132e4608SDavid Howells 	struct vfsmount *mnt;
32708f11538eSAl Viro 	struct mountpoint *mp;
3271132e4608SDavid Howells 	struct super_block *sb = fc->root->d_sb;
3272132e4608SDavid Howells 	int error;
3273132e4608SDavid Howells 
3274c9ce29edSAl Viro 	error = security_sb_kern_mount(sb);
3275c9ce29edSAl Viro 	if (!error && mount_too_revealing(sb, &mnt_flags))
3276c9ce29edSAl Viro 		error = -EPERM;
3277c9ce29edSAl Viro 
3278c9ce29edSAl Viro 	if (unlikely(error)) {
3279c9ce29edSAl Viro 		fc_drop_locked(fc);
3280c9ce29edSAl Viro 		return error;
3281132e4608SDavid Howells 	}
3282132e4608SDavid Howells 
3283132e4608SDavid Howells 	up_write(&sb->s_umount);
3284132e4608SDavid Howells 
3285132e4608SDavid Howells 	mnt = vfs_create_mount(fc);
3286132e4608SDavid Howells 	if (IS_ERR(mnt))
3287132e4608SDavid Howells 		return PTR_ERR(mnt);
3288132e4608SDavid Howells 
3289f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(mountpoint, mnt);
3290f8b92ba6SDeepa Dinamani 
32918f11538eSAl Viro 	mp = lock_mount(mountpoint);
32928f11538eSAl Viro 	if (IS_ERR(mp)) {
32938f11538eSAl Viro 		mntput(mnt);
32948f11538eSAl Viro 		return PTR_ERR(mp);
32958f11538eSAl Viro 	}
32968f11538eSAl Viro 	error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
32978f11538eSAl Viro 	unlock_mount(mp);
32980ecee669SEric Biggers 	if (error < 0)
32990ecee669SEric Biggers 		mntput(mnt);
3300f8b92ba6SDeepa Dinamani 	return error;
3301f8b92ba6SDeepa Dinamani }
3302f8b92ba6SDeepa Dinamani 
33031da177e4SLinus Torvalds /*
33041da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
33051da177e4SLinus Torvalds  * namespace's tree
33061da177e4SLinus Torvalds  */
do_new_mount(struct path * path,const char * fstype,int sb_flags,int mnt_flags,const char * name,void * data)3307e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
3308808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
33091da177e4SLinus Torvalds {
33100c55cfc4SEric W. Biederman 	struct file_system_type *type;
3311a0c9a8b8SAl Viro 	struct fs_context *fc;
3312a0c9a8b8SAl Viro 	const char *subtype = NULL;
3313a0c9a8b8SAl Viro 	int err = 0;
33141da177e4SLinus Torvalds 
33150c55cfc4SEric W. Biederman 	if (!fstype)
33161da177e4SLinus Torvalds 		return -EINVAL;
33171da177e4SLinus Torvalds 
33180c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
33190c55cfc4SEric W. Biederman 	if (!type)
33200c55cfc4SEric W. Biederman 		return -ENODEV;
33210c55cfc4SEric W. Biederman 
3322a0c9a8b8SAl Viro 	if (type->fs_flags & FS_HAS_SUBTYPE) {
3323a0c9a8b8SAl Viro 		subtype = strchr(fstype, '.');
3324a0c9a8b8SAl Viro 		if (subtype) {
3325a0c9a8b8SAl Viro 			subtype++;
3326a0c9a8b8SAl Viro 			if (!*subtype) {
33270c55cfc4SEric W. Biederman 				put_filesystem(type);
3328a0c9a8b8SAl Viro 				return -EINVAL;
3329a0c9a8b8SAl Viro 			}
3330a0c9a8b8SAl Viro 		}
33318654df4eSEric W. Biederman 	}
33328654df4eSEric W. Biederman 
3333a0c9a8b8SAl Viro 	fc = fs_context_for_mount(type, sb_flags);
3334a0c9a8b8SAl Viro 	put_filesystem(type);
3335a0c9a8b8SAl Viro 	if (IS_ERR(fc))
3336a0c9a8b8SAl Viro 		return PTR_ERR(fc);
3337a0c9a8b8SAl Viro 
33385248b445SChristian Brauner 	/*
33395248b445SChristian Brauner 	 * Indicate to the filesystem that the mount request is coming
33405248b445SChristian Brauner 	 * from the legacy mount system call.
33415248b445SChristian Brauner 	 */
33425248b445SChristian Brauner 	fc->oldapi = true;
33435248b445SChristian Brauner 
33443e1aeb00SDavid Howells 	if (subtype)
33453e1aeb00SDavid Howells 		err = vfs_parse_fs_string(fc, "subtype",
33463e1aeb00SDavid Howells 					  subtype, strlen(subtype));
33473e1aeb00SDavid Howells 	if (!err && name)
33483e1aeb00SDavid Howells 		err = vfs_parse_fs_string(fc, "source", name, strlen(name));
3349a0c9a8b8SAl Viro 	if (!err)
3350a0c9a8b8SAl Viro 		err = parse_monolithic_mount_data(fc, data);
3351c3aabf07SAl Viro 	if (!err && !mount_capable(fc))
3352c3aabf07SAl Viro 		err = -EPERM;
3353a0c9a8b8SAl Viro 	if (!err)
3354a0c9a8b8SAl Viro 		err = vfs_get_tree(fc);
3355132e4608SDavid Howells 	if (!err)
3356132e4608SDavid Howells 		err = do_new_mount_fc(fc, path, mnt_flags);
3357a0c9a8b8SAl Viro 
3358a0c9a8b8SAl Viro 	put_fs_context(fc);
335915f9a3f3SAl Viro 	return err;
33601da177e4SLinus Torvalds }
33611da177e4SLinus Torvalds 
finish_automount(struct vfsmount * m,const struct path * path)33621e2d8464SAl Viro int finish_automount(struct vfsmount *m, const struct path *path)
336319a167afSAl Viro {
336426df6034SAl Viro 	struct dentry *dentry = path->dentry;
33658f11538eSAl Viro 	struct mountpoint *mp;
336625e195aaSAl Viro 	struct mount *mnt;
336719a167afSAl Viro 	int err;
336825e195aaSAl Viro 
336925e195aaSAl Viro 	if (!m)
337025e195aaSAl Viro 		return 0;
337125e195aaSAl Viro 	if (IS_ERR(m))
337225e195aaSAl Viro 		return PTR_ERR(m);
337325e195aaSAl Viro 
337425e195aaSAl Viro 	mnt = real_mount(m);
337519a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
337619a167afSAl Viro 	 * expired before we get a chance to add it
337719a167afSAl Viro 	 */
33786776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
337919a167afSAl Viro 
338019a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
338126df6034SAl Viro 	    m->mnt_root == dentry) {
3382b1e75df4SAl Viro 		err = -ELOOP;
338326df6034SAl Viro 		goto discard;
338419a167afSAl Viro 	}
338519a167afSAl Viro 
338626df6034SAl Viro 	/*
338726df6034SAl Viro 	 * we don't want to use lock_mount() - in this case finding something
338826df6034SAl Viro 	 * that overmounts our mountpoint to be means "quitely drop what we've
338926df6034SAl Viro 	 * got", not "try to mount it on top".
339026df6034SAl Viro 	 */
339126df6034SAl Viro 	inode_lock(dentry->d_inode);
339226df6034SAl Viro 	namespace_lock();
339326df6034SAl Viro 	if (unlikely(cant_mount(dentry))) {
339426df6034SAl Viro 		err = -ENOENT;
339526df6034SAl Viro 		goto discard_locked;
339626df6034SAl Viro 	}
33976ac39281SChristian Brauner 	if (path_overmounted(path)) {
339826df6034SAl Viro 		err = 0;
339926df6034SAl Viro 		goto discard_locked;
340026df6034SAl Viro 	}
340126df6034SAl Viro 	mp = get_mountpoint(dentry);
34028f11538eSAl Viro 	if (IS_ERR(mp)) {
34038f11538eSAl Viro 		err = PTR_ERR(mp);
340426df6034SAl Viro 		goto discard_locked;
34058f11538eSAl Viro 	}
340626df6034SAl Viro 
34078f11538eSAl Viro 	err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
34088f11538eSAl Viro 	unlock_mount(mp);
340926df6034SAl Viro 	if (unlikely(err))
341026df6034SAl Viro 		goto discard;
341126df6034SAl Viro 	mntput(m);
3412b1e75df4SAl Viro 	return 0;
341326df6034SAl Viro 
341426df6034SAl Viro discard_locked:
341526df6034SAl Viro 	namespace_unlock();
341626df6034SAl Viro 	inode_unlock(dentry->d_inode);
341726df6034SAl Viro discard:
3418b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
34196776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
342097216be0SAl Viro 		namespace_lock();
34216776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
342297216be0SAl Viro 		namespace_unlock();
342319a167afSAl Viro 	}
3424b1e75df4SAl Viro 	mntput(m);
3425b1e75df4SAl Viro 	mntput(m);
342619a167afSAl Viro 	return err;
342719a167afSAl Viro }
342819a167afSAl Viro 
3429ea5b778aSDavid Howells /**
3430ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
3431ea5b778aSDavid Howells  * @mnt: The mount to list.
3432ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
3433ea5b778aSDavid Howells  */
mnt_set_expiry(struct vfsmount * mnt,struct list_head * expiry_list)3434ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
3435ea5b778aSDavid Howells {
343697216be0SAl Viro 	namespace_lock();
3437ea5b778aSDavid Howells 
34386776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
3439ea5b778aSDavid Howells 
344097216be0SAl Viro 	namespace_unlock();
3441ea5b778aSDavid Howells }
3442ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
3443ea5b778aSDavid Howells 
3444ea5b778aSDavid Howells /*
34451da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
34461da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
34471da177e4SLinus Torvalds  * here
34481da177e4SLinus Torvalds  */
mark_mounts_for_expiry(struct list_head * mounts)34491da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
34501da177e4SLinus Torvalds {
3451761d5c38SAl Viro 	struct mount *mnt, *next;
34521da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
34531da177e4SLinus Torvalds 
34541da177e4SLinus Torvalds 	if (list_empty(mounts))
34551da177e4SLinus Torvalds 		return;
34561da177e4SLinus Torvalds 
345797216be0SAl Viro 	namespace_lock();
3458719ea2fbSAl Viro 	lock_mount_hash();
34591da177e4SLinus Torvalds 
34601da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
34611da177e4SLinus Torvalds 	 * following criteria:
34621da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
34631da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
34641da177e4SLinus Torvalds 	 *   cleared by mntput())
34651da177e4SLinus Torvalds 	 */
34666776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
3467863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
34681ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
34691da177e4SLinus Torvalds 			continue;
34706776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
34711da177e4SLinus Torvalds 	}
3472bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
34736776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
3474143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
3475e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3476bcc5c7d2SAl Viro 	}
3477719ea2fbSAl Viro 	unlock_mount_hash();
34783ab6abeeSAl Viro 	namespace_unlock();
34791da177e4SLinus Torvalds }
34801da177e4SLinus Torvalds 
34811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
34821da177e4SLinus Torvalds 
34831da177e4SLinus Torvalds /*
34845528f911STrond Myklebust  * Ripoff of 'select_parent()'
34855528f911STrond Myklebust  *
34865528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
34875528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
34885528f911STrond Myklebust  */
select_submounts(struct mount * parent,struct list_head * graveyard)3489692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
34905528f911STrond Myklebust {
3491692afc31SAl Viro 	struct mount *this_parent = parent;
34925528f911STrond Myklebust 	struct list_head *next;
34935528f911STrond Myklebust 	int found = 0;
34945528f911STrond Myklebust 
34955528f911STrond Myklebust repeat:
34966b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
34975528f911STrond Myklebust resume:
34986b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
34995528f911STrond Myklebust 		struct list_head *tmp = next;
35006b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
35015528f911STrond Myklebust 
35025528f911STrond Myklebust 		next = tmp->next;
3503692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
35045528f911STrond Myklebust 			continue;
35055528f911STrond Myklebust 		/*
35065528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
35075528f911STrond Myklebust 		 */
35086b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
35095528f911STrond Myklebust 			this_parent = mnt;
35105528f911STrond Myklebust 			goto repeat;
35115528f911STrond Myklebust 		}
35125528f911STrond Myklebust 
35131ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
35146776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
35155528f911STrond Myklebust 			found++;
35165528f911STrond Myklebust 		}
35175528f911STrond Myklebust 	}
35185528f911STrond Myklebust 	/*
35195528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
35205528f911STrond Myklebust 	 */
35215528f911STrond Myklebust 	if (this_parent != parent) {
35226b41d536SAl Viro 		next = this_parent->mnt_child.next;
35230714a533SAl Viro 		this_parent = this_parent->mnt_parent;
35245528f911STrond Myklebust 		goto resume;
35255528f911STrond Myklebust 	}
35265528f911STrond Myklebust 	return found;
35275528f911STrond Myklebust }
35285528f911STrond Myklebust 
35295528f911STrond Myklebust /*
35305528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
35315528f911STrond Myklebust  * submounts of a specific parent mountpoint
353299b7db7bSNick Piggin  *
353348a066e7SAl Viro  * mount_lock must be held for write
35345528f911STrond Myklebust  */
shrink_submounts(struct mount * mnt)3535b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
35365528f911STrond Myklebust {
35375528f911STrond Myklebust 	LIST_HEAD(graveyard);
3538761d5c38SAl Viro 	struct mount *m;
35395528f911STrond Myklebust 
35405528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
3541c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
3542bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
3543761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
35446776db3dSAl Viro 						mnt_expire);
3545143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
3546e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3547bcc5c7d2SAl Viro 		}
3548bcc5c7d2SAl Viro 	}
35495528f911STrond Myklebust }
35505528f911STrond Myklebust 
copy_mount_options(const void __user * data)3551028abd92SChristoph Hellwig static void *copy_mount_options(const void __user * data)
35521da177e4SLinus Torvalds {
3553b40ef869SAl Viro 	char *copy;
3554d563d678SCatalin Marinas 	unsigned left, offset;
35551da177e4SLinus Torvalds 
35561da177e4SLinus Torvalds 	if (!data)
3557b40ef869SAl Viro 		return NULL;
35581da177e4SLinus Torvalds 
3559b40ef869SAl Viro 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
3560b40ef869SAl Viro 	if (!copy)
3561b40ef869SAl Viro 		return ERR_PTR(-ENOMEM);
35621da177e4SLinus Torvalds 
3563d563d678SCatalin Marinas 	left = copy_from_user(copy, data, PAGE_SIZE);
35641da177e4SLinus Torvalds 
3565d563d678SCatalin Marinas 	/*
3566d563d678SCatalin Marinas 	 * Not all architectures have an exact copy_from_user(). Resort to
3567d563d678SCatalin Marinas 	 * byte at a time.
3568d563d678SCatalin Marinas 	 */
3569d563d678SCatalin Marinas 	offset = PAGE_SIZE - left;
3570d563d678SCatalin Marinas 	while (left) {
3571d563d678SCatalin Marinas 		char c;
3572d563d678SCatalin Marinas 		if (get_user(c, (const char __user *)data + offset))
3573d563d678SCatalin Marinas 			break;
3574d563d678SCatalin Marinas 		copy[offset] = c;
3575d563d678SCatalin Marinas 		left--;
3576d563d678SCatalin Marinas 		offset++;
3577d563d678SCatalin Marinas 	}
3578d563d678SCatalin Marinas 
3579d563d678SCatalin Marinas 	if (left == PAGE_SIZE) {
3580b40ef869SAl Viro 		kfree(copy);
3581b40ef869SAl Viro 		return ERR_PTR(-EFAULT);
35821da177e4SLinus Torvalds 	}
3583d563d678SCatalin Marinas 
3584b40ef869SAl Viro 	return copy;
35851da177e4SLinus Torvalds }
35861da177e4SLinus Torvalds 
copy_mount_string(const void __user * data)3587028abd92SChristoph Hellwig static char *copy_mount_string(const void __user *data)
3588eca6f534SVegard Nossum {
3589fbdb4401SChandan Rajendra 	return data ? strndup_user(data, PATH_MAX) : NULL;
3590eca6f534SVegard Nossum }
3591eca6f534SVegard Nossum 
35921da177e4SLinus Torvalds /*
35931da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
35941da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
35951da177e4SLinus Torvalds  *
35961da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
35971da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
35981da177e4SLinus Torvalds  * information (or be NULL).
35991da177e4SLinus Torvalds  *
36001da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
36011da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
36021da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
36031da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
36041da177e4SLinus Torvalds  * and must be discarded.
36051da177e4SLinus Torvalds  */
path_mount(const char * dev_name,struct path * path,const char * type_page,unsigned long flags,void * data_page)3606c60166f0SChristoph Hellwig int path_mount(const char *dev_name, struct path *path,
3607808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
36081da177e4SLinus Torvalds {
3609e462ec50SDavid Howells 	unsigned int mnt_flags = 0, sb_flags;
3610a1e6aaa3SChristoph Hellwig 	int ret;
36111da177e4SLinus Torvalds 
36121da177e4SLinus Torvalds 	/* Discard magic */
36131da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
36141da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
36151da177e4SLinus Torvalds 
36161da177e4SLinus Torvalds 	/* Basic sanity checks */
36171da177e4SLinus Torvalds 	if (data_page)
36181da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
36191da177e4SLinus Torvalds 
3620e462ec50SDavid Howells 	if (flags & MS_NOUSER)
3621e462ec50SDavid Howells 		return -EINVAL;
3622e462ec50SDavid Howells 
3623a1e6aaa3SChristoph Hellwig 	ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
3624a1e6aaa3SChristoph Hellwig 	if (ret)
3625a1e6aaa3SChristoph Hellwig 		return ret;
3626a1e6aaa3SChristoph Hellwig 	if (!may_mount())
3627a1e6aaa3SChristoph Hellwig 		return -EPERM;
3628f7e33bdbSJeff Layton 	if (flags & SB_MANDLOCK)
3629f7e33bdbSJeff Layton 		warn_mandlock();
3630a27ab9f2STetsuo Handa 
3631613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
3632613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
36330a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
36340a1c01c9SMatthew Garrett 
36351da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
36361da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
36371da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
36381da177e4SLinus Torvalds 	if (flags & MS_NODEV)
36391da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
36401da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
36411da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
3642fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
3643fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
3644fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
3645fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
3646d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
3647d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
3648a9e5b732SDavid Howells 	if (flags & MS_RDONLY)
36492e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
3650dab741e0SMattias Nissler 	if (flags & MS_NOSYMFOLLOW)
3651dab741e0SMattias Nissler 		mnt_flags |= MNT_NOSYMFOLLOW;
3652fc33a7bbSChristoph Hellwig 
3653ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
3654ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
3655ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3656ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
3657ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
3658a1e6aaa3SChristoph Hellwig 		mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
3659ffbc6f0eSEric W. Biederman 	}
3660ffbc6f0eSEric W. Biederman 
3661e462ec50SDavid Howells 	sb_flags = flags & (SB_RDONLY |
3662e462ec50SDavid Howells 			    SB_SYNCHRONOUS |
3663e462ec50SDavid Howells 			    SB_MANDLOCK |
3664e462ec50SDavid Howells 			    SB_DIRSYNC |
3665e462ec50SDavid Howells 			    SB_SILENT |
3666917086ffSMimi Zohar 			    SB_POSIXACL |
3667d7ee9469SMarkus Trippelsdorf 			    SB_LAZYTIME |
3668917086ffSMimi Zohar 			    SB_I_VERSION);
36691da177e4SLinus Torvalds 
367043f5e655SDavid Howells 	if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
3671a1e6aaa3SChristoph Hellwig 		return do_reconfigure_mnt(path, mnt_flags);
3672a1e6aaa3SChristoph Hellwig 	if (flags & MS_REMOUNT)
3673a1e6aaa3SChristoph Hellwig 		return do_remount(path, flags, sb_flags, mnt_flags, data_page);
3674a1e6aaa3SChristoph Hellwig 	if (flags & MS_BIND)
3675a1e6aaa3SChristoph Hellwig 		return do_loopback(path, dev_name, flags & MS_REC);
3676a1e6aaa3SChristoph Hellwig 	if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
3677a1e6aaa3SChristoph Hellwig 		return do_change_type(path, flags);
3678a1e6aaa3SChristoph Hellwig 	if (flags & MS_MOVE)
3679a1e6aaa3SChristoph Hellwig 		return do_move_mount_old(path, dev_name);
3680a1e6aaa3SChristoph Hellwig 
3681a1e6aaa3SChristoph Hellwig 	return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
36821da177e4SLinus Torvalds 			    data_page);
3683a1e6aaa3SChristoph Hellwig }
3684a1e6aaa3SChristoph Hellwig 
do_mount(const char * dev_name,const char __user * dir_name,const char * type_page,unsigned long flags,void * data_page)3685a1e6aaa3SChristoph Hellwig long do_mount(const char *dev_name, const char __user *dir_name,
3686a1e6aaa3SChristoph Hellwig 		const char *type_page, unsigned long flags, void *data_page)
3687a1e6aaa3SChristoph Hellwig {
3688a1e6aaa3SChristoph Hellwig 	struct path path;
3689a1e6aaa3SChristoph Hellwig 	int ret;
3690a1e6aaa3SChristoph Hellwig 
3691a1e6aaa3SChristoph Hellwig 	ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
3692a1e6aaa3SChristoph Hellwig 	if (ret)
3693a1e6aaa3SChristoph Hellwig 		return ret;
3694a1e6aaa3SChristoph Hellwig 	ret = path_mount(dev_name, &path, type_page, flags, data_page);
36952d92ab3cSAl Viro 	path_put(&path);
3696a1e6aaa3SChristoph Hellwig 	return ret;
36971da177e4SLinus Torvalds }
36981da177e4SLinus Torvalds 
inc_mnt_namespaces(struct user_namespace * ns)3699537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3700537f7ccbSEric W. Biederman {
3701537f7ccbSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3702537f7ccbSEric W. Biederman }
3703537f7ccbSEric W. Biederman 
dec_mnt_namespaces(struct ucounts * ucounts)3704537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts)
3705537f7ccbSEric W. Biederman {
3706537f7ccbSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3707537f7ccbSEric W. Biederman }
3708537f7ccbSEric W. Biederman 
free_mnt_ns(struct mnt_namespace * ns)3709771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
3710771b1371SEric W. Biederman {
371174e83122SAl Viro 	if (!is_anon_ns(ns))
37126344c433SAl Viro 		ns_free_inum(&ns->ns);
3713537f7ccbSEric W. Biederman 	dec_mnt_namespaces(ns->ucounts);
3714771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
3715771b1371SEric W. Biederman 	kfree(ns);
3716771b1371SEric W. Biederman }
3717771b1371SEric W. Biederman 
37188823c079SEric W. Biederman /*
37198823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
37208823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
37218823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
37228823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
37238823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
37248823c079SEric W. Biederman  */
37258823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
37268823c079SEric W. Biederman 
alloc_mnt_ns(struct user_namespace * user_ns,bool anon)372774e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
3728cf8d2c11STrond Myklebust {
3729cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
3730537f7ccbSEric W. Biederman 	struct ucounts *ucounts;
373198f842e6SEric W. Biederman 	int ret;
3732cf8d2c11STrond Myklebust 
3733537f7ccbSEric W. Biederman 	ucounts = inc_mnt_namespaces(user_ns);
3734537f7ccbSEric W. Biederman 	if (!ucounts)
3735df75e774SEric W. Biederman 		return ERR_PTR(-ENOSPC);
3736537f7ccbSEric W. Biederman 
373730acd0bdSVasily Averin 	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
3738537f7ccbSEric W. Biederman 	if (!new_ns) {
3739537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
3740cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
3741537f7ccbSEric W. Biederman 	}
374274e83122SAl Viro 	if (!anon) {
37436344c433SAl Viro 		ret = ns_alloc_inum(&new_ns->ns);
374498f842e6SEric W. Biederman 		if (ret) {
374598f842e6SEric W. Biederman 			kfree(new_ns);
3746537f7ccbSEric W. Biederman 			dec_mnt_namespaces(ucounts);
374798f842e6SEric W. Biederman 			return ERR_PTR(ret);
374898f842e6SEric W. Biederman 		}
374974e83122SAl Viro 	}
375033c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
375174e83122SAl Viro 	if (!anon)
37528823c079SEric W. Biederman 		new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
37531a7b8969SKirill Tkhai 	refcount_set(&new_ns->ns.count, 1);
3754cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
3755cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
37569f6c61f9SMiklos Szeredi 	spin_lock_init(&new_ns->ns_lock);
3757771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
3758537f7ccbSEric W. Biederman 	new_ns->ucounts = ucounts;
3759cf8d2c11STrond Myklebust 	return new_ns;
3760cf8d2c11STrond Myklebust }
3761cf8d2c11STrond Myklebust 
37620766f788SEmese Revfy __latent_entropy
copy_mnt_ns(unsigned long flags,struct mnt_namespace * ns,struct user_namespace * user_ns,struct fs_struct * new_fs)37639559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
37649559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
37651da177e4SLinus Torvalds {
37666b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
37677f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
3768315fc83eSAl Viro 	struct mount *p, *q;
37699559f689SAl Viro 	struct mount *old;
3770cb338d06SAl Viro 	struct mount *new;
37717a472ef4SEric W. Biederman 	int copy_flags;
37721da177e4SLinus Torvalds 
37739559f689SAl Viro 	BUG_ON(!ns);
37749559f689SAl Viro 
37759559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
37769559f689SAl Viro 		get_mnt_ns(ns);
37779559f689SAl Viro 		return ns;
37789559f689SAl Viro 	}
37799559f689SAl Viro 
37809559f689SAl Viro 	old = ns->root;
37819559f689SAl Viro 
378274e83122SAl Viro 	new_ns = alloc_mnt_ns(user_ns, false);
3783cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
3784cf8d2c11STrond Myklebust 		return new_ns;
37851da177e4SLinus Torvalds 
378697216be0SAl Viro 	namespace_lock();
37871da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
37884ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
37899559f689SAl Viro 	if (user_ns != ns->user_ns)
37903bd045ccSAl Viro 		copy_flags |= CL_SHARED_TO_SLAVE;
37917a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
3792be34d1a3SDavid Howells 	if (IS_ERR(new)) {
3793328e6d90SAl Viro 		namespace_unlock();
3794771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
3795be34d1a3SDavid Howells 		return ERR_CAST(new);
37961da177e4SLinus Torvalds 	}
37973bd045ccSAl Viro 	if (user_ns != ns->user_ns) {
37983bd045ccSAl Viro 		lock_mount_hash();
37993bd045ccSAl Viro 		lock_mnt_tree(new);
38003bd045ccSAl Viro 		unlock_mount_hash();
38013bd045ccSAl Viro 	}
3802be08d6d2SAl Viro 	new_ns->root = new;
38031a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
38041da177e4SLinus Torvalds 
38051da177e4SLinus Torvalds 	/*
38061da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
38071da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
38081da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
38091da177e4SLinus Torvalds 	 */
3810909b0a88SAl Viro 	p = old;
3811cb338d06SAl Viro 	q = new;
38121da177e4SLinus Torvalds 	while (p) {
3813143c8c91SAl Viro 		q->mnt_ns = new_ns;
3814d2921684SEric W. Biederman 		new_ns->mounts++;
38159559f689SAl Viro 		if (new_fs) {
38169559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
38179559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
3818315fc83eSAl Viro 				rootmnt = &p->mnt;
38191da177e4SLinus Torvalds 			}
38209559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
38219559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
3822315fc83eSAl Viro 				pwdmnt = &p->mnt;
38231da177e4SLinus Torvalds 			}
38241da177e4SLinus Torvalds 		}
3825909b0a88SAl Viro 		p = next_mnt(p, old);
3826909b0a88SAl Viro 		q = next_mnt(q, new);
38274ce5d2b1SEric W. Biederman 		if (!q)
38284ce5d2b1SEric W. Biederman 			break;
382961d8e426SAl Viro 		// an mntns binding we'd skipped?
38304ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
383161d8e426SAl Viro 			p = next_mnt(skip_mnt_tree(p), old);
38321da177e4SLinus Torvalds 	}
3833328e6d90SAl Viro 	namespace_unlock();
38341da177e4SLinus Torvalds 
38351da177e4SLinus Torvalds 	if (rootmnt)
3836f03c6599SAl Viro 		mntput(rootmnt);
38371da177e4SLinus Torvalds 	if (pwdmnt)
3838f03c6599SAl Viro 		mntput(pwdmnt);
38391da177e4SLinus Torvalds 
3840741a2951SJANAK DESAI 	return new_ns;
3841741a2951SJANAK DESAI }
3842741a2951SJANAK DESAI 
mount_subtree(struct vfsmount * m,const char * name)384374e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name)
3844cf8d2c11STrond Myklebust {
38451a4eeaf2SAl Viro 	struct mount *mnt = real_mount(m);
3846ea441d11SAl Viro 	struct mnt_namespace *ns;
3847d31da0f0SAl Viro 	struct super_block *s;
3848ea441d11SAl Viro 	struct path path;
3849ea441d11SAl Viro 	int err;
3850ea441d11SAl Viro 
385174e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, true);
385274e83122SAl Viro 	if (IS_ERR(ns)) {
385374e83122SAl Viro 		mntput(m);
3854ea441d11SAl Viro 		return ERR_CAST(ns);
385574e83122SAl Viro 	}
385674e83122SAl Viro 	mnt->mnt_ns = ns;
385774e83122SAl Viro 	ns->root = mnt;
385874e83122SAl Viro 	ns->mounts++;
385974e83122SAl Viro 	list_add(&mnt->mnt_list, &ns->list);
3860ea441d11SAl Viro 
386174e83122SAl Viro 	err = vfs_path_lookup(m->mnt_root, m,
3862ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3863ea441d11SAl Viro 
3864ea441d11SAl Viro 	put_mnt_ns(ns);
3865ea441d11SAl Viro 
3866ea441d11SAl Viro 	if (err)
3867ea441d11SAl Viro 		return ERR_PTR(err);
3868ea441d11SAl Viro 
3869ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
3870d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
3871d31da0f0SAl Viro 	atomic_inc(&s->s_active);
3872ea441d11SAl Viro 	mntput(path.mnt);
3873ea441d11SAl Viro 	/* lock the sucker */
3874d31da0f0SAl Viro 	down_write(&s->s_umount);
3875ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
3876ea441d11SAl Viro 	return path.dentry;
3877ea441d11SAl Viro }
3878ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
3879ea441d11SAl Viro 
SYSCALL_DEFINE5(mount,char __user *,dev_name,char __user *,dir_name,char __user *,type,unsigned long,flags,void __user *,data)3880cccaa5e3SDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3881cccaa5e3SDominik Brodowski 		char __user *, type, unsigned long, flags, void __user *, data)
38821da177e4SLinus Torvalds {
3883eca6f534SVegard Nossum 	int ret;
3884eca6f534SVegard Nossum 	char *kernel_type;
3885eca6f534SVegard Nossum 	char *kernel_dev;
3886b40ef869SAl Viro 	void *options;
38871da177e4SLinus Torvalds 
3888b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
3889b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
3890b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
3891eca6f534SVegard Nossum 		goto out_type;
38921da177e4SLinus Torvalds 
3893b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
3894b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
3895b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
3896eca6f534SVegard Nossum 		goto out_dev;
38971da177e4SLinus Torvalds 
3898b40ef869SAl Viro 	options = copy_mount_options(data);
3899b40ef869SAl Viro 	ret = PTR_ERR(options);
3900b40ef869SAl Viro 	if (IS_ERR(options))
3901eca6f534SVegard Nossum 		goto out_data;
39021da177e4SLinus Torvalds 
3903b40ef869SAl Viro 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
3904eca6f534SVegard Nossum 
3905b40ef869SAl Viro 	kfree(options);
3906eca6f534SVegard Nossum out_data:
3907eca6f534SVegard Nossum 	kfree(kernel_dev);
3908eca6f534SVegard Nossum out_dev:
3909eca6f534SVegard Nossum 	kfree(kernel_type);
3910eca6f534SVegard Nossum out_type:
3911eca6f534SVegard Nossum 	return ret;
39121da177e4SLinus Torvalds }
39131da177e4SLinus Torvalds 
39145b490500SChristian Brauner #define FSMOUNT_VALID_FLAGS                                                    \
39155b490500SChristian Brauner 	(MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV |            \
3916dd8b477fSChristian Brauner 	 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME |       \
3917dd8b477fSChristian Brauner 	 MOUNT_ATTR_NOSYMFOLLOW)
39185b490500SChristian Brauner 
39199caccd41SChristian Brauner #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
39202a186721SChristian Brauner 
39212a186721SChristian Brauner #define MOUNT_SETATTR_PROPAGATION_FLAGS \
39222a186721SChristian Brauner 	(MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
39232a186721SChristian Brauner 
attr_flags_to_mnt_flags(u64 attr_flags)39245b490500SChristian Brauner static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
39255b490500SChristian Brauner {
39265b490500SChristian Brauner 	unsigned int mnt_flags = 0;
39275b490500SChristian Brauner 
39285b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_RDONLY)
39295b490500SChristian Brauner 		mnt_flags |= MNT_READONLY;
39305b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOSUID)
39315b490500SChristian Brauner 		mnt_flags |= MNT_NOSUID;
39325b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NODEV)
39335b490500SChristian Brauner 		mnt_flags |= MNT_NODEV;
39345b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOEXEC)
39355b490500SChristian Brauner 		mnt_flags |= MNT_NOEXEC;
39365b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NODIRATIME)
39375b490500SChristian Brauner 		mnt_flags |= MNT_NODIRATIME;
3938dd8b477fSChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
3939dd8b477fSChristian Brauner 		mnt_flags |= MNT_NOSYMFOLLOW;
39405b490500SChristian Brauner 
39415b490500SChristian Brauner 	return mnt_flags;
39425b490500SChristian Brauner }
39435b490500SChristian Brauner 
39441da177e4SLinus Torvalds /*
394593766fbdSDavid Howells  * Create a kernel mount representation for a new, prepared superblock
394693766fbdSDavid Howells  * (specified by fs_fd) and attach to an open_tree-like file descriptor.
394793766fbdSDavid Howells  */
SYSCALL_DEFINE3(fsmount,int,fs_fd,unsigned int,flags,unsigned int,attr_flags)394893766fbdSDavid Howells SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
394993766fbdSDavid Howells 		unsigned int, attr_flags)
395093766fbdSDavid Howells {
395193766fbdSDavid Howells 	struct mnt_namespace *ns;
395293766fbdSDavid Howells 	struct fs_context *fc;
395393766fbdSDavid Howells 	struct file *file;
395493766fbdSDavid Howells 	struct path newmount;
395593766fbdSDavid Howells 	struct mount *mnt;
395693766fbdSDavid Howells 	struct fd f;
395793766fbdSDavid Howells 	unsigned int mnt_flags = 0;
395893766fbdSDavid Howells 	long ret;
395993766fbdSDavid Howells 
396093766fbdSDavid Howells 	if (!may_mount())
396193766fbdSDavid Howells 		return -EPERM;
396293766fbdSDavid Howells 
396393766fbdSDavid Howells 	if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
396493766fbdSDavid Howells 		return -EINVAL;
396593766fbdSDavid Howells 
39665b490500SChristian Brauner 	if (attr_flags & ~FSMOUNT_VALID_FLAGS)
396793766fbdSDavid Howells 		return -EINVAL;
396893766fbdSDavid Howells 
39695b490500SChristian Brauner 	mnt_flags = attr_flags_to_mnt_flags(attr_flags);
397093766fbdSDavid Howells 
397193766fbdSDavid Howells 	switch (attr_flags & MOUNT_ATTR__ATIME) {
397293766fbdSDavid Howells 	case MOUNT_ATTR_STRICTATIME:
397393766fbdSDavid Howells 		break;
397493766fbdSDavid Howells 	case MOUNT_ATTR_NOATIME:
397593766fbdSDavid Howells 		mnt_flags |= MNT_NOATIME;
397693766fbdSDavid Howells 		break;
397793766fbdSDavid Howells 	case MOUNT_ATTR_RELATIME:
397893766fbdSDavid Howells 		mnt_flags |= MNT_RELATIME;
397993766fbdSDavid Howells 		break;
398093766fbdSDavid Howells 	default:
398193766fbdSDavid Howells 		return -EINVAL;
398293766fbdSDavid Howells 	}
398393766fbdSDavid Howells 
398493766fbdSDavid Howells 	f = fdget(fs_fd);
398593766fbdSDavid Howells 	if (!f.file)
398693766fbdSDavid Howells 		return -EBADF;
398793766fbdSDavid Howells 
398893766fbdSDavid Howells 	ret = -EINVAL;
398993766fbdSDavid Howells 	if (f.file->f_op != &fscontext_fops)
399093766fbdSDavid Howells 		goto err_fsfd;
399193766fbdSDavid Howells 
399293766fbdSDavid Howells 	fc = f.file->private_data;
399393766fbdSDavid Howells 
399493766fbdSDavid Howells 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
399593766fbdSDavid Howells 	if (ret < 0)
399693766fbdSDavid Howells 		goto err_fsfd;
399793766fbdSDavid Howells 
399893766fbdSDavid Howells 	/* There must be a valid superblock or we can't mount it */
399993766fbdSDavid Howells 	ret = -EINVAL;
400093766fbdSDavid Howells 	if (!fc->root)
400193766fbdSDavid Howells 		goto err_unlock;
400293766fbdSDavid Howells 
400393766fbdSDavid Howells 	ret = -EPERM;
400493766fbdSDavid Howells 	if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
400593766fbdSDavid Howells 		pr_warn("VFS: Mount too revealing\n");
400693766fbdSDavid Howells 		goto err_unlock;
400793766fbdSDavid Howells 	}
400893766fbdSDavid Howells 
400993766fbdSDavid Howells 	ret = -EBUSY;
401093766fbdSDavid Howells 	if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
401193766fbdSDavid Howells 		goto err_unlock;
401293766fbdSDavid Howells 
4013f7e33bdbSJeff Layton 	if (fc->sb_flags & SB_MANDLOCK)
4014f7e33bdbSJeff Layton 		warn_mandlock();
401593766fbdSDavid Howells 
401693766fbdSDavid Howells 	newmount.mnt = vfs_create_mount(fc);
401793766fbdSDavid Howells 	if (IS_ERR(newmount.mnt)) {
401893766fbdSDavid Howells 		ret = PTR_ERR(newmount.mnt);
401993766fbdSDavid Howells 		goto err_unlock;
402093766fbdSDavid Howells 	}
402193766fbdSDavid Howells 	newmount.dentry = dget(fc->root);
402293766fbdSDavid Howells 	newmount.mnt->mnt_flags = mnt_flags;
402393766fbdSDavid Howells 
402493766fbdSDavid Howells 	/* We've done the mount bit - now move the file context into more or
402593766fbdSDavid Howells 	 * less the same state as if we'd done an fspick().  We don't want to
402693766fbdSDavid Howells 	 * do any memory allocation or anything like that at this point as we
402793766fbdSDavid Howells 	 * don't want to have to handle any errors incurred.
402893766fbdSDavid Howells 	 */
402993766fbdSDavid Howells 	vfs_clean_context(fc);
403093766fbdSDavid Howells 
403193766fbdSDavid Howells 	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
403293766fbdSDavid Howells 	if (IS_ERR(ns)) {
403393766fbdSDavid Howells 		ret = PTR_ERR(ns);
403493766fbdSDavid Howells 		goto err_path;
403593766fbdSDavid Howells 	}
403693766fbdSDavid Howells 	mnt = real_mount(newmount.mnt);
403793766fbdSDavid Howells 	mnt->mnt_ns = ns;
403893766fbdSDavid Howells 	ns->root = mnt;
403993766fbdSDavid Howells 	ns->mounts = 1;
404093766fbdSDavid Howells 	list_add(&mnt->mnt_list, &ns->list);
40411b0b9cc8SEric Biggers 	mntget(newmount.mnt);
404293766fbdSDavid Howells 
404393766fbdSDavid Howells 	/* Attach to an apparent O_PATH fd with a note that we need to unmount
404493766fbdSDavid Howells 	 * it, not just simply put it.
404593766fbdSDavid Howells 	 */
404693766fbdSDavid Howells 	file = dentry_open(&newmount, O_PATH, fc->cred);
404793766fbdSDavid Howells 	if (IS_ERR(file)) {
404893766fbdSDavid Howells 		dissolve_on_fput(newmount.mnt);
404993766fbdSDavid Howells 		ret = PTR_ERR(file);
405093766fbdSDavid Howells 		goto err_path;
405193766fbdSDavid Howells 	}
405293766fbdSDavid Howells 	file->f_mode |= FMODE_NEED_UNMOUNT;
405393766fbdSDavid Howells 
405493766fbdSDavid Howells 	ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
405593766fbdSDavid Howells 	if (ret >= 0)
405693766fbdSDavid Howells 		fd_install(ret, file);
405793766fbdSDavid Howells 	else
405893766fbdSDavid Howells 		fput(file);
405993766fbdSDavid Howells 
406093766fbdSDavid Howells err_path:
406193766fbdSDavid Howells 	path_put(&newmount);
406293766fbdSDavid Howells err_unlock:
406393766fbdSDavid Howells 	mutex_unlock(&fc->uapi_mutex);
406493766fbdSDavid Howells err_fsfd:
406593766fbdSDavid Howells 	fdput(f);
406693766fbdSDavid Howells 	return ret;
406793766fbdSDavid Howells }
406893766fbdSDavid Howells 
406993766fbdSDavid Howells /*
407093766fbdSDavid Howells  * Move a mount from one place to another.  In combination with
407193766fbdSDavid Howells  * fsopen()/fsmount() this is used to install a new mount and in combination
407293766fbdSDavid Howells  * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
407393766fbdSDavid Howells  * a mount subtree.
40742db154b3SDavid Howells  *
40752db154b3SDavid Howells  * Note the flags value is a combination of MOVE_MOUNT_* flags.
40762db154b3SDavid Howells  */
SYSCALL_DEFINE5(move_mount,int,from_dfd,const char __user *,from_pathname,int,to_dfd,const char __user *,to_pathname,unsigned int,flags)40772db154b3SDavid Howells SYSCALL_DEFINE5(move_mount,
40782658ce09SBen Dooks 		int, from_dfd, const char __user *, from_pathname,
40792658ce09SBen Dooks 		int, to_dfd, const char __user *, to_pathname,
40802db154b3SDavid Howells 		unsigned int, flags)
40812db154b3SDavid Howells {
40822db154b3SDavid Howells 	struct path from_path, to_path;
40832db154b3SDavid Howells 	unsigned int lflags;
40842db154b3SDavid Howells 	int ret = 0;
40852db154b3SDavid Howells 
40862db154b3SDavid Howells 	if (!may_mount())
40872db154b3SDavid Howells 		return -EPERM;
40882db154b3SDavid Howells 
40892db154b3SDavid Howells 	if (flags & ~MOVE_MOUNT__MASK)
40902db154b3SDavid Howells 		return -EINVAL;
40912db154b3SDavid Howells 
40926ac39281SChristian Brauner 	if ((flags & (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP)) ==
40936ac39281SChristian Brauner 	    (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP))
40946ac39281SChristian Brauner 		return -EINVAL;
40956ac39281SChristian Brauner 
40962db154b3SDavid Howells 	/* If someone gives a pathname, they aren't permitted to move
40972db154b3SDavid Howells 	 * from an fd that requires unmount as we can't get at the flag
40982db154b3SDavid Howells 	 * to clear it afterwards.
40992db154b3SDavid Howells 	 */
41002db154b3SDavid Howells 	lflags = 0;
41012db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
41022db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
41032db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
41042db154b3SDavid Howells 
41052db154b3SDavid Howells 	ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
41062db154b3SDavid Howells 	if (ret < 0)
41072db154b3SDavid Howells 		return ret;
41082db154b3SDavid Howells 
41092db154b3SDavid Howells 	lflags = 0;
41102db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
41112db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
41122db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
41132db154b3SDavid Howells 
41142db154b3SDavid Howells 	ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
41152db154b3SDavid Howells 	if (ret < 0)
41162db154b3SDavid Howells 		goto out_from;
41172db154b3SDavid Howells 
41182db154b3SDavid Howells 	ret = security_move_mount(&from_path, &to_path);
41192db154b3SDavid Howells 	if (ret < 0)
41202db154b3SDavid Howells 		goto out_to;
41212db154b3SDavid Howells 
41229ffb14efSPavel Tikhomirov 	if (flags & MOVE_MOUNT_SET_GROUP)
41239ffb14efSPavel Tikhomirov 		ret = do_set_group(&from_path, &to_path);
41249ffb14efSPavel Tikhomirov 	else
41256ac39281SChristian Brauner 		ret = do_move_mount(&from_path, &to_path,
41266ac39281SChristian Brauner 				    (flags & MOVE_MOUNT_BENEATH));
41272db154b3SDavid Howells 
41282db154b3SDavid Howells out_to:
41292db154b3SDavid Howells 	path_put(&to_path);
41302db154b3SDavid Howells out_from:
41312db154b3SDavid Howells 	path_put(&from_path);
41322db154b3SDavid Howells 	return ret;
41332db154b3SDavid Howells }
41342db154b3SDavid Howells 
41352db154b3SDavid Howells /*
4136afac7cbaSAl Viro  * Return true if path is reachable from root
4137afac7cbaSAl Viro  *
413848a066e7SAl Viro  * namespace_sem or mount_lock is held
4139afac7cbaSAl Viro  */
is_path_reachable(struct mount * mnt,struct dentry * dentry,const struct path * root)4140643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
4141afac7cbaSAl Viro 			 const struct path *root)
4142afac7cbaSAl Viro {
4143643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
4144a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
41450714a533SAl Viro 		mnt = mnt->mnt_parent;
4146afac7cbaSAl Viro 	}
4147643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
4148afac7cbaSAl Viro }
4149afac7cbaSAl Viro 
path_is_under(const struct path * path1,const struct path * path2)4150640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2)
4151afac7cbaSAl Viro {
415225ab4c9bSYaowei Bai 	bool res;
415348a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
4154643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
415548a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
4156afac7cbaSAl Viro 	return res;
4157afac7cbaSAl Viro }
4158afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
4159afac7cbaSAl Viro 
4160afac7cbaSAl Viro /*
41611da177e4SLinus Torvalds  * pivot_root Semantics:
41621da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
41631da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
41641da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
41651da177e4SLinus Torvalds  *
41661da177e4SLinus Torvalds  * Restrictions:
41671da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
41681da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
41691da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
41701da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
41711da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
41721da177e4SLinus Torvalds  *
41734a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
41740c1bc6b8SMauro Carvalho Chehab  * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
41754a0d11faSNeil Brown  * in this situation.
41764a0d11faSNeil Brown  *
41771da177e4SLinus Torvalds  * Notes:
41781da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
41791da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
41801da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
41811da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
41821da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
41831da177e4SLinus Torvalds  *    first.
41841da177e4SLinus Torvalds  */
SYSCALL_DEFINE2(pivot_root,const char __user *,new_root,const char __user *,put_old)41853480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
41863480b257SHeiko Carstens 		const char __user *, put_old)
41871da177e4SLinus Torvalds {
41882763d119SAl Viro 	struct path new, old, root;
41892763d119SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
419084d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
41911da177e4SLinus Torvalds 	int error;
41921da177e4SLinus Torvalds 
41939b40bc90SAl Viro 	if (!may_mount())
41941da177e4SLinus Torvalds 		return -EPERM;
41951da177e4SLinus Torvalds 
4196ce6595a2SAl Viro 	error = user_path_at(AT_FDCWD, new_root,
4197ce6595a2SAl Viro 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
41981da177e4SLinus Torvalds 	if (error)
41991da177e4SLinus Torvalds 		goto out0;
42001da177e4SLinus Torvalds 
4201ce6595a2SAl Viro 	error = user_path_at(AT_FDCWD, put_old,
4202ce6595a2SAl Viro 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
42031da177e4SLinus Torvalds 	if (error)
42041da177e4SLinus Torvalds 		goto out1;
42051da177e4SLinus Torvalds 
42062d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
4207b12cea91SAl Viro 	if (error)
4208b12cea91SAl Viro 		goto out2;
42091da177e4SLinus Torvalds 
4210f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
421184d17192SAl Viro 	old_mp = lock_mount(&old);
421284d17192SAl Viro 	error = PTR_ERR(old_mp);
421384d17192SAl Viro 	if (IS_ERR(old_mp))
4214b12cea91SAl Viro 		goto out3;
4215b12cea91SAl Viro 
42161da177e4SLinus Torvalds 	error = -EINVAL;
4217419148daSAl Viro 	new_mnt = real_mount(new.mnt);
4218419148daSAl Viro 	root_mnt = real_mount(root.mnt);
421984d17192SAl Viro 	old_mnt = real_mount(old.mnt);
42202763d119SAl Viro 	ex_parent = new_mnt->mnt_parent;
42212763d119SAl Viro 	root_parent = root_mnt->mnt_parent;
422284d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
42232763d119SAl Viro 		IS_MNT_SHARED(ex_parent) ||
42242763d119SAl Viro 		IS_MNT_SHARED(root_parent))
4225b12cea91SAl Viro 		goto out4;
4226143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
4227b12cea91SAl Viro 		goto out4;
42285ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
42295ff9d8a6SEric W. Biederman 		goto out4;
42301da177e4SLinus Torvalds 	error = -ENOENT;
4231f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
4232b12cea91SAl Viro 		goto out4;
42331da177e4SLinus Torvalds 	error = -EBUSY;
423484d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
4235b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
42361da177e4SLinus Torvalds 	error = -EINVAL;
423778aa08a8SChristian Brauner 	if (!path_mounted(&root))
4238b12cea91SAl Viro 		goto out4; /* not a mountpoint */
4239676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
4240b12cea91SAl Viro 		goto out4; /* not attached */
424178aa08a8SChristian Brauner 	if (!path_mounted(&new))
4242b12cea91SAl Viro 		goto out4; /* not a mountpoint */
4243676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
4244b12cea91SAl Viro 		goto out4; /* not attached */
42454ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
424684d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
4247b12cea91SAl Viro 		goto out4;
42480d082601SEric W. Biederman 	/* make certain new is below the root */
42490d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
42500d082601SEric W. Biederman 		goto out4;
4251719ea2fbSAl Viro 	lock_mount_hash();
42522763d119SAl Viro 	umount_mnt(new_mnt);
42532763d119SAl Viro 	root_mp = unhash_mnt(root_mnt);  /* we'll need its mountpoint */
42545ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
42555ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
42565ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
42575ff9d8a6SEric W. Biederman 	}
42584ac91378SJan Blunck 	/* mount old root on put_old */
42596ac39281SChristian Brauner 	attach_mnt(root_mnt, old_mnt, old_mp, false);
42604ac91378SJan Blunck 	/* mount new_root on / */
42616ac39281SChristian Brauner 	attach_mnt(new_mnt, root_parent, root_mp, false);
42622763d119SAl Viro 	mnt_add_count(root_parent, -1);
42636b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
42644fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
42654fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
42663895dbf8SEric W. Biederman 	put_mountpoint(root_mp);
4267719ea2fbSAl Viro 	unlock_mount_hash();
42682d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
42691da177e4SLinus Torvalds 	error = 0;
4270b12cea91SAl Viro out4:
427184d17192SAl Viro 	unlock_mount(old_mp);
42722763d119SAl Viro 	if (!error)
42732763d119SAl Viro 		mntput_no_expire(ex_parent);
4274b12cea91SAl Viro out3:
42758c3ee42eSAl Viro 	path_put(&root);
4276b12cea91SAl Viro out2:
42772d8f3038SAl Viro 	path_put(&old);
42781da177e4SLinus Torvalds out1:
42792d8f3038SAl Viro 	path_put(&new);
42801da177e4SLinus Torvalds out0:
42811da177e4SLinus Torvalds 	return error;
42821da177e4SLinus Torvalds }
42831da177e4SLinus Torvalds 
recalc_flags(struct mount_kattr * kattr,struct mount * mnt)42842a186721SChristian Brauner static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
42852a186721SChristian Brauner {
42862a186721SChristian Brauner 	unsigned int flags = mnt->mnt.mnt_flags;
42872a186721SChristian Brauner 
42882a186721SChristian Brauner 	/*  flags to clear */
42892a186721SChristian Brauner 	flags &= ~kattr->attr_clr;
42902a186721SChristian Brauner 	/* flags to raise */
42912a186721SChristian Brauner 	flags |= kattr->attr_set;
42922a186721SChristian Brauner 
42932a186721SChristian Brauner 	return flags;
42942a186721SChristian Brauner }
42952a186721SChristian Brauner 
can_idmap_mount(const struct mount_kattr * kattr,struct mount * mnt)42969caccd41SChristian Brauner static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
42979caccd41SChristian Brauner {
42989caccd41SChristian Brauner 	struct vfsmount *m = &mnt->mnt;
4299bd303368SChristian Brauner 	struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
43009caccd41SChristian Brauner 
4301256c8aedSChristian Brauner 	if (!kattr->mnt_idmap)
43029caccd41SChristian Brauner 		return 0;
43039caccd41SChristian Brauner 
43049caccd41SChristian Brauner 	/*
4305bd303368SChristian Brauner 	 * Creating an idmapped mount with the filesystem wide idmapping
4306bd303368SChristian Brauner 	 * doesn't make sense so block that. We don't allow mushy semantics.
4307bd303368SChristian Brauner 	 */
43083707d84cSChristian Brauner 	if (!check_fsmapping(kattr->mnt_idmap, m->mnt_sb))
4309bd303368SChristian Brauner 		return -EINVAL;
4310bd303368SChristian Brauner 
4311bd303368SChristian Brauner 	/*
43129caccd41SChristian Brauner 	 * Once a mount has been idmapped we don't allow it to change its
43139caccd41SChristian Brauner 	 * mapping. It makes things simpler and callers can just create
43149caccd41SChristian Brauner 	 * another bind-mount they can idmap if they want to.
43159caccd41SChristian Brauner 	 */
4316bb49e9e7SChristian Brauner 	if (is_idmapped_mnt(m))
43179caccd41SChristian Brauner 		return -EPERM;
43189caccd41SChristian Brauner 
43199caccd41SChristian Brauner 	/* The underlying filesystem doesn't support idmapped mounts yet. */
43209caccd41SChristian Brauner 	if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
43219caccd41SChristian Brauner 		return -EINVAL;
43229caccd41SChristian Brauner 
43239caccd41SChristian Brauner 	/* We're not controlling the superblock. */
4324bd303368SChristian Brauner 	if (!ns_capable(fs_userns, CAP_SYS_ADMIN))
43259caccd41SChristian Brauner 		return -EPERM;
43269caccd41SChristian Brauner 
43279caccd41SChristian Brauner 	/* Mount has already been visible in the filesystem hierarchy. */
43289caccd41SChristian Brauner 	if (!is_anon_ns(mnt->mnt_ns))
43299caccd41SChristian Brauner 		return -EINVAL;
43309caccd41SChristian Brauner 
43319caccd41SChristian Brauner 	return 0;
43329caccd41SChristian Brauner }
43339caccd41SChristian Brauner 
4334a26f788bSChristian Brauner /**
4335a26f788bSChristian Brauner  * mnt_allow_writers() - check whether the attribute change allows writers
4336a26f788bSChristian Brauner  * @kattr: the new mount attributes
4337a26f788bSChristian Brauner  * @mnt: the mount to which @kattr will be applied
4338a26f788bSChristian Brauner  *
4339a26f788bSChristian Brauner  * Check whether thew new mount attributes in @kattr allow concurrent writers.
4340a26f788bSChristian Brauner  *
4341a26f788bSChristian Brauner  * Return: true if writers need to be held, false if not
4342a26f788bSChristian Brauner  */
mnt_allow_writers(const struct mount_kattr * kattr,const struct mount * mnt)4343a26f788bSChristian Brauner static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
4344a26f788bSChristian Brauner 				     const struct mount *mnt)
43452a186721SChristian Brauner {
4346e1bbcd27SChristian Brauner 	return (!(kattr->attr_set & MNT_READONLY) ||
4347e1bbcd27SChristian Brauner 		(mnt->mnt.mnt_flags & MNT_READONLY)) &&
4348256c8aedSChristian Brauner 	       !kattr->mnt_idmap;
43492a186721SChristian Brauner }
43502a186721SChristian Brauner 
mount_setattr_prepare(struct mount_kattr * kattr,struct mount * mnt)435187bb5b60SChristian Brauner static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
43522a186721SChristian Brauner {
4353e257039fSAl Viro 	struct mount *m;
4354e257039fSAl Viro 	int err;
43552a186721SChristian Brauner 
4356e257039fSAl Viro 	for (m = mnt; m; m = next_mnt(m, mnt)) {
4357e257039fSAl Viro 		if (!can_change_locked_flags(m, recalc_flags(kattr, m))) {
4358e257039fSAl Viro 			err = -EPERM;
4359e257039fSAl Viro 			break;
43602a186721SChristian Brauner 		}
43612a186721SChristian Brauner 
436287bb5b60SChristian Brauner 		err = can_idmap_mount(kattr, m);
436387bb5b60SChristian Brauner 		if (err)
4364e257039fSAl Viro 			break;
43652a186721SChristian Brauner 
4366e257039fSAl Viro 		if (!mnt_allow_writers(kattr, m)) {
436787bb5b60SChristian Brauner 			err = mnt_hold_writers(m);
436887bb5b60SChristian Brauner 			if (err)
4369e257039fSAl Viro 				break;
43702a186721SChristian Brauner 		}
43712a186721SChristian Brauner 
4372e257039fSAl Viro 		if (!kattr->recurse)
437387bb5b60SChristian Brauner 			return 0;
43742a186721SChristian Brauner 	}
43752a186721SChristian Brauner 
4376e257039fSAl Viro 	if (err) {
4377e257039fSAl Viro 		struct mount *p;
4378e257039fSAl Viro 
43790014edaeSChristian Brauner 		/*
43800014edaeSChristian Brauner 		 * If we had to call mnt_hold_writers() MNT_WRITE_HOLD will
43810014edaeSChristian Brauner 		 * be set in @mnt_flags. The loop unsets MNT_WRITE_HOLD for all
43820014edaeSChristian Brauner 		 * mounts and needs to take care to include the first mount.
43830014edaeSChristian Brauner 		 */
43840014edaeSChristian Brauner 		for (p = mnt; p; p = next_mnt(p, mnt)) {
4385e257039fSAl Viro 			/* If we had to hold writers unblock them. */
4386e257039fSAl Viro 			if (p->mnt.mnt_flags & MNT_WRITE_HOLD)
4387e257039fSAl Viro 				mnt_unhold_writers(p);
43880014edaeSChristian Brauner 
43890014edaeSChristian Brauner 			/*
43900014edaeSChristian Brauner 			 * We're done once the first mount we changed got
43910014edaeSChristian Brauner 			 * MNT_WRITE_HOLD unset.
43920014edaeSChristian Brauner 			 */
43930014edaeSChristian Brauner 			if (p == m)
43940014edaeSChristian Brauner 				break;
4395e257039fSAl Viro 		}
4396e257039fSAl Viro 	}
4397e257039fSAl Viro 	return err;
43982a186721SChristian Brauner }
43992a186721SChristian Brauner 
do_idmap_mount(const struct mount_kattr * kattr,struct mount * mnt)44009caccd41SChristian Brauner static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
44019caccd41SChristian Brauner {
4402256c8aedSChristian Brauner 	if (!kattr->mnt_idmap)
44039caccd41SChristian Brauner 		return;
44049caccd41SChristian Brauner 
4405bd303368SChristian Brauner 	/*
4406256c8aedSChristian Brauner 	 * Pairs with smp_load_acquire() in mnt_idmap().
4407256c8aedSChristian Brauner 	 *
4408256c8aedSChristian Brauner 	 * Since we only allow a mount to change the idmapping once and
4409256c8aedSChristian Brauner 	 * verified this in can_idmap_mount() we know that the mount has
4410256c8aedSChristian Brauner 	 * @nop_mnt_idmap attached to it. So there's no need to drop any
4411256c8aedSChristian Brauner 	 * references.
4412bd303368SChristian Brauner 	 */
4413256c8aedSChristian Brauner 	smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap));
44149caccd41SChristian Brauner }
44159caccd41SChristian Brauner 
mount_setattr_commit(struct mount_kattr * kattr,struct mount * mnt)4416e257039fSAl Viro static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
44172a186721SChristian Brauner {
4418e257039fSAl Viro 	struct mount *m;
44192a186721SChristian Brauner 
4420e257039fSAl Viro 	for (m = mnt; m; m = next_mnt(m, mnt)) {
44212a186721SChristian Brauner 		unsigned int flags;
44222a186721SChristian Brauner 
44239caccd41SChristian Brauner 		do_idmap_mount(kattr, m);
44242a186721SChristian Brauner 		flags = recalc_flags(kattr, m);
44252a186721SChristian Brauner 		WRITE_ONCE(m->mnt.mnt_flags, flags);
44262a186721SChristian Brauner 
442703b6abeeSChristian Brauner 		/* If we had to hold writers unblock them. */
442803b6abeeSChristian Brauner 		if (m->mnt.mnt_flags & MNT_WRITE_HOLD)
44292a186721SChristian Brauner 			mnt_unhold_writers(m);
44302a186721SChristian Brauner 
4431e257039fSAl Viro 		if (kattr->propagation)
44322a186721SChristian Brauner 			change_mnt_propagation(m, kattr->propagation);
4433e257039fSAl Viro 		if (!kattr->recurse)
44342a186721SChristian Brauner 			break;
4435e257039fSAl Viro 	}
44362a186721SChristian Brauner 	touch_mnt_namespace(mnt->mnt_ns);
44372a186721SChristian Brauner }
44382a186721SChristian Brauner 
do_mount_setattr(struct path * path,struct mount_kattr * kattr)44392a186721SChristian Brauner static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
44402a186721SChristian Brauner {
444187bb5b60SChristian Brauner 	struct mount *mnt = real_mount(path->mnt);
44422a186721SChristian Brauner 	int err = 0;
44432a186721SChristian Brauner 
444478aa08a8SChristian Brauner 	if (!path_mounted(path))
44452a186721SChristian Brauner 		return -EINVAL;
44462a186721SChristian Brauner 
4447256c8aedSChristian Brauner 	if (kattr->mnt_userns) {
4448256c8aedSChristian Brauner 		struct mnt_idmap *mnt_idmap;
4449256c8aedSChristian Brauner 
4450256c8aedSChristian Brauner 		mnt_idmap = alloc_mnt_idmap(kattr->mnt_userns);
4451256c8aedSChristian Brauner 		if (IS_ERR(mnt_idmap))
4452256c8aedSChristian Brauner 			return PTR_ERR(mnt_idmap);
4453256c8aedSChristian Brauner 		kattr->mnt_idmap = mnt_idmap;
4454256c8aedSChristian Brauner 	}
4455256c8aedSChristian Brauner 
44562a186721SChristian Brauner 	if (kattr->propagation) {
44572a186721SChristian Brauner 		/*
44582a186721SChristian Brauner 		 * Only take namespace_lock() if we're actually changing
44592a186721SChristian Brauner 		 * propagation.
44602a186721SChristian Brauner 		 */
44612a186721SChristian Brauner 		namespace_lock();
44622a186721SChristian Brauner 		if (kattr->propagation == MS_SHARED) {
44632a186721SChristian Brauner 			err = invent_group_ids(mnt, kattr->recurse);
44642a186721SChristian Brauner 			if (err) {
44652a186721SChristian Brauner 				namespace_unlock();
44662a186721SChristian Brauner 				return err;
44672a186721SChristian Brauner 			}
44682a186721SChristian Brauner 		}
44692a186721SChristian Brauner 	}
44702a186721SChristian Brauner 
447187bb5b60SChristian Brauner 	err = -EINVAL;
44722a186721SChristian Brauner 	lock_mount_hash();
44732a186721SChristian Brauner 
447487bb5b60SChristian Brauner 	/* Ensure that this isn't anything purely vfs internal. */
447587bb5b60SChristian Brauner 	if (!is_mounted(&mnt->mnt))
447687bb5b60SChristian Brauner 		goto out;
44772a186721SChristian Brauner 
447887bb5b60SChristian Brauner 	/*
447987bb5b60SChristian Brauner 	 * If this is an attached mount make sure it's located in the callers
448087bb5b60SChristian Brauner 	 * mount namespace. If it's not don't let the caller interact with it.
448131f71f2dSChristian Brauner 	 *
448231f71f2dSChristian Brauner 	 * If this mount doesn't have a parent it's most often simply a
448331f71f2dSChristian Brauner 	 * detached mount with an anonymous mount namespace. IOW, something
448431f71f2dSChristian Brauner 	 * that's simply not attached yet. But there are apparently also users
448531f71f2dSChristian Brauner 	 * that do change mount properties on the rootfs itself. That obviously
448631f71f2dSChristian Brauner 	 * neither has a parent nor is it a detached mount so we cannot
448731f71f2dSChristian Brauner 	 * unconditionally check for detached mounts.
448887bb5b60SChristian Brauner 	 */
448931f71f2dSChristian Brauner 	if ((mnt_has_parent(mnt) || !is_anon_ns(mnt->mnt_ns)) && !check_mnt(mnt))
449087bb5b60SChristian Brauner 		goto out;
449187bb5b60SChristian Brauner 
449287bb5b60SChristian Brauner 	/*
449387bb5b60SChristian Brauner 	 * First, we get the mount tree in a shape where we can change mount
449487bb5b60SChristian Brauner 	 * properties without failure. If we succeeded to do so we commit all
449587bb5b60SChristian Brauner 	 * changes and if we failed we clean up.
449687bb5b60SChristian Brauner 	 */
449787bb5b60SChristian Brauner 	err = mount_setattr_prepare(kattr, mnt);
4498e257039fSAl Viro 	if (!err)
4499e257039fSAl Viro 		mount_setattr_commit(kattr, mnt);
450087bb5b60SChristian Brauner 
450187bb5b60SChristian Brauner out:
45022a186721SChristian Brauner 	unlock_mount_hash();
45032a186721SChristian Brauner 
45042a186721SChristian Brauner 	if (kattr->propagation) {
45052a186721SChristian Brauner 		if (err)
45062a186721SChristian Brauner 			cleanup_group_ids(mnt, NULL);
4507cb2239c1SChristian Brauner 		namespace_unlock();
45082a186721SChristian Brauner 	}
45092a186721SChristian Brauner 
45102a186721SChristian Brauner 	return err;
45112a186721SChristian Brauner }
45122a186721SChristian Brauner 
build_mount_idmapped(const struct mount_attr * attr,size_t usize,struct mount_kattr * kattr,unsigned int flags)45139caccd41SChristian Brauner static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
45149caccd41SChristian Brauner 				struct mount_kattr *kattr, unsigned int flags)
45159caccd41SChristian Brauner {
45169caccd41SChristian Brauner 	int err = 0;
45179caccd41SChristian Brauner 	struct ns_common *ns;
45189caccd41SChristian Brauner 	struct user_namespace *mnt_userns;
451996e85e95SAl Viro 	struct fd f;
45209caccd41SChristian Brauner 
45219caccd41SChristian Brauner 	if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
45229caccd41SChristian Brauner 		return 0;
45239caccd41SChristian Brauner 
45249caccd41SChristian Brauner 	/*
45259caccd41SChristian Brauner 	 * We currently do not support clearing an idmapped mount. If this ever
45269caccd41SChristian Brauner 	 * is a use-case we can revisit this but for now let's keep it simple
45279caccd41SChristian Brauner 	 * and not allow it.
45289caccd41SChristian Brauner 	 */
45299caccd41SChristian Brauner 	if (attr->attr_clr & MOUNT_ATTR_IDMAP)
45309caccd41SChristian Brauner 		return -EINVAL;
45319caccd41SChristian Brauner 
45329caccd41SChristian Brauner 	if (attr->userns_fd > INT_MAX)
45339caccd41SChristian Brauner 		return -EINVAL;
45349caccd41SChristian Brauner 
453596e85e95SAl Viro 	f = fdget(attr->userns_fd);
453696e85e95SAl Viro 	if (!f.file)
45379caccd41SChristian Brauner 		return -EBADF;
45389caccd41SChristian Brauner 
453996e85e95SAl Viro 	if (!proc_ns_file(f.file)) {
45409caccd41SChristian Brauner 		err = -EINVAL;
45419caccd41SChristian Brauner 		goto out_fput;
45429caccd41SChristian Brauner 	}
45439caccd41SChristian Brauner 
454496e85e95SAl Viro 	ns = get_proc_ns(file_inode(f.file));
45459caccd41SChristian Brauner 	if (ns->ops->type != CLONE_NEWUSER) {
45469caccd41SChristian Brauner 		err = -EINVAL;
45479caccd41SChristian Brauner 		goto out_fput;
45489caccd41SChristian Brauner 	}
45499caccd41SChristian Brauner 
45509caccd41SChristian Brauner 	/*
4551bd303368SChristian Brauner 	 * The initial idmapping cannot be used to create an idmapped
4552bd303368SChristian Brauner 	 * mount. We use the initial idmapping as an indicator of a mount
4553bd303368SChristian Brauner 	 * that is not idmapped. It can simply be passed into helpers that
4554bd303368SChristian Brauner 	 * are aware of idmapped mounts as a convenient shortcut. A user
4555bd303368SChristian Brauner 	 * can just create a dedicated identity mapping to achieve the same
4556bd303368SChristian Brauner 	 * result.
45579caccd41SChristian Brauner 	 */
45589caccd41SChristian Brauner 	mnt_userns = container_of(ns, struct user_namespace, ns);
45593707d84cSChristian Brauner 	if (mnt_userns == &init_user_ns) {
45609caccd41SChristian Brauner 		err = -EPERM;
45619caccd41SChristian Brauner 		goto out_fput;
45629caccd41SChristian Brauner 	}
4563bf1ac16eSSeth Forshee 
4564bf1ac16eSSeth Forshee 	/* We're not controlling the target namespace. */
4565bf1ac16eSSeth Forshee 	if (!ns_capable(mnt_userns, CAP_SYS_ADMIN)) {
4566bf1ac16eSSeth Forshee 		err = -EPERM;
4567bf1ac16eSSeth Forshee 		goto out_fput;
4568bf1ac16eSSeth Forshee 	}
4569bf1ac16eSSeth Forshee 
45709caccd41SChristian Brauner 	kattr->mnt_userns = get_user_ns(mnt_userns);
45719caccd41SChristian Brauner 
45729caccd41SChristian Brauner out_fput:
457396e85e95SAl Viro 	fdput(f);
45749caccd41SChristian Brauner 	return err;
45759caccd41SChristian Brauner }
45769caccd41SChristian Brauner 
build_mount_kattr(const struct mount_attr * attr,size_t usize,struct mount_kattr * kattr,unsigned int flags)45779caccd41SChristian Brauner static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
45782a186721SChristian Brauner 			     struct mount_kattr *kattr, unsigned int flags)
45792a186721SChristian Brauner {
45802a186721SChristian Brauner 	unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
45812a186721SChristian Brauner 
45822a186721SChristian Brauner 	if (flags & AT_NO_AUTOMOUNT)
45832a186721SChristian Brauner 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
45842a186721SChristian Brauner 	if (flags & AT_SYMLINK_NOFOLLOW)
45852a186721SChristian Brauner 		lookup_flags &= ~LOOKUP_FOLLOW;
45862a186721SChristian Brauner 	if (flags & AT_EMPTY_PATH)
45872a186721SChristian Brauner 		lookup_flags |= LOOKUP_EMPTY;
45882a186721SChristian Brauner 
45892a186721SChristian Brauner 	*kattr = (struct mount_kattr) {
45902a186721SChristian Brauner 		.lookup_flags	= lookup_flags,
45912a186721SChristian Brauner 		.recurse	= !!(flags & AT_RECURSIVE),
45922a186721SChristian Brauner 	};
45932a186721SChristian Brauner 
45942a186721SChristian Brauner 	if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
45952a186721SChristian Brauner 		return -EINVAL;
45962a186721SChristian Brauner 	if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
45972a186721SChristian Brauner 		return -EINVAL;
45982a186721SChristian Brauner 	kattr->propagation = attr->propagation;
45992a186721SChristian Brauner 
46002a186721SChristian Brauner 	if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
46012a186721SChristian Brauner 		return -EINVAL;
46022a186721SChristian Brauner 
46032a186721SChristian Brauner 	kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
46042a186721SChristian Brauner 	kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
46052a186721SChristian Brauner 
46062a186721SChristian Brauner 	/*
46072a186721SChristian Brauner 	 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
46082a186721SChristian Brauner 	 * users wanting to transition to a different atime setting cannot
46092a186721SChristian Brauner 	 * simply specify the atime setting in @attr_set, but must also
46102a186721SChristian Brauner 	 * specify MOUNT_ATTR__ATIME in the @attr_clr field.
46112a186721SChristian Brauner 	 * So ensure that MOUNT_ATTR__ATIME can't be partially set in
46122a186721SChristian Brauner 	 * @attr_clr and that @attr_set can't have any atime bits set if
46132a186721SChristian Brauner 	 * MOUNT_ATTR__ATIME isn't set in @attr_clr.
46142a186721SChristian Brauner 	 */
46152a186721SChristian Brauner 	if (attr->attr_clr & MOUNT_ATTR__ATIME) {
46162a186721SChristian Brauner 		if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
46172a186721SChristian Brauner 			return -EINVAL;
46182a186721SChristian Brauner 
46192a186721SChristian Brauner 		/*
46202a186721SChristian Brauner 		 * Clear all previous time settings as they are mutually
46212a186721SChristian Brauner 		 * exclusive.
46222a186721SChristian Brauner 		 */
46232a186721SChristian Brauner 		kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
46242a186721SChristian Brauner 		switch (attr->attr_set & MOUNT_ATTR__ATIME) {
46252a186721SChristian Brauner 		case MOUNT_ATTR_RELATIME:
46262a186721SChristian Brauner 			kattr->attr_set |= MNT_RELATIME;
46272a186721SChristian Brauner 			break;
46282a186721SChristian Brauner 		case MOUNT_ATTR_NOATIME:
46292a186721SChristian Brauner 			kattr->attr_set |= MNT_NOATIME;
46302a186721SChristian Brauner 			break;
46312a186721SChristian Brauner 		case MOUNT_ATTR_STRICTATIME:
46322a186721SChristian Brauner 			break;
46332a186721SChristian Brauner 		default:
46342a186721SChristian Brauner 			return -EINVAL;
46352a186721SChristian Brauner 		}
46362a186721SChristian Brauner 	} else {
46372a186721SChristian Brauner 		if (attr->attr_set & MOUNT_ATTR__ATIME)
46382a186721SChristian Brauner 			return -EINVAL;
46392a186721SChristian Brauner 	}
46402a186721SChristian Brauner 
46419caccd41SChristian Brauner 	return build_mount_idmapped(attr, usize, kattr, flags);
46429caccd41SChristian Brauner }
46439caccd41SChristian Brauner 
finish_mount_kattr(struct mount_kattr * kattr)46449caccd41SChristian Brauner static void finish_mount_kattr(struct mount_kattr *kattr)
46459caccd41SChristian Brauner {
46469caccd41SChristian Brauner 	put_user_ns(kattr->mnt_userns);
46479caccd41SChristian Brauner 	kattr->mnt_userns = NULL;
4648256c8aedSChristian Brauner 
4649256c8aedSChristian Brauner 	if (kattr->mnt_idmap)
4650256c8aedSChristian Brauner 		mnt_idmap_put(kattr->mnt_idmap);
46512a186721SChristian Brauner }
46522a186721SChristian Brauner 
SYSCALL_DEFINE5(mount_setattr,int,dfd,const char __user *,path,unsigned int,flags,struct mount_attr __user *,uattr,size_t,usize)46532a186721SChristian Brauner SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
46542a186721SChristian Brauner 		unsigned int, flags, struct mount_attr __user *, uattr,
46552a186721SChristian Brauner 		size_t, usize)
46562a186721SChristian Brauner {
46572a186721SChristian Brauner 	int err;
46582a186721SChristian Brauner 	struct path target;
46592a186721SChristian Brauner 	struct mount_attr attr;
46602a186721SChristian Brauner 	struct mount_kattr kattr;
46612a186721SChristian Brauner 
46622a186721SChristian Brauner 	BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
46632a186721SChristian Brauner 
46642a186721SChristian Brauner 	if (flags & ~(AT_EMPTY_PATH |
46652a186721SChristian Brauner 		      AT_RECURSIVE |
46662a186721SChristian Brauner 		      AT_SYMLINK_NOFOLLOW |
46672a186721SChristian Brauner 		      AT_NO_AUTOMOUNT))
46682a186721SChristian Brauner 		return -EINVAL;
46692a186721SChristian Brauner 
46702a186721SChristian Brauner 	if (unlikely(usize > PAGE_SIZE))
46712a186721SChristian Brauner 		return -E2BIG;
46722a186721SChristian Brauner 	if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
46732a186721SChristian Brauner 		return -EINVAL;
46742a186721SChristian Brauner 
46752a186721SChristian Brauner 	if (!may_mount())
46762a186721SChristian Brauner 		return -EPERM;
46772a186721SChristian Brauner 
46782a186721SChristian Brauner 	err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
46792a186721SChristian Brauner 	if (err)
46802a186721SChristian Brauner 		return err;
46812a186721SChristian Brauner 
46822a186721SChristian Brauner 	/* Don't bother walking through the mounts if this is a nop. */
46832a186721SChristian Brauner 	if (attr.attr_set == 0 &&
46842a186721SChristian Brauner 	    attr.attr_clr == 0 &&
46852a186721SChristian Brauner 	    attr.propagation == 0)
46862a186721SChristian Brauner 		return 0;
46872a186721SChristian Brauner 
46889caccd41SChristian Brauner 	err = build_mount_kattr(&attr, usize, &kattr, flags);
46892a186721SChristian Brauner 	if (err)
46902a186721SChristian Brauner 		return err;
46912a186721SChristian Brauner 
46922a186721SChristian Brauner 	err = user_path_at(dfd, path, kattr.lookup_flags, &target);
4693012e3322SChristian Brauner 	if (!err) {
46942a186721SChristian Brauner 		err = do_mount_setattr(&target, &kattr);
46952a186721SChristian Brauner 		path_put(&target);
4696012e3322SChristian Brauner 	}
4697012e3322SChristian Brauner 	finish_mount_kattr(&kattr);
46982a186721SChristian Brauner 	return err;
46992a186721SChristian Brauner }
47002a186721SChristian Brauner 
init_mount_tree(void)47011da177e4SLinus Torvalds static void __init init_mount_tree(void)
47021da177e4SLinus Torvalds {
47031da177e4SLinus Torvalds 	struct vfsmount *mnt;
470474e83122SAl Viro 	struct mount *m;
47056b3286edSKirill Korotaev 	struct mnt_namespace *ns;
4706ac748a09SJan Blunck 	struct path root;
47071da177e4SLinus Torvalds 
4708fd3e007fSAl Viro 	mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
47091da177e4SLinus Torvalds 	if (IS_ERR(mnt))
47101da177e4SLinus Torvalds 		panic("Can't create rootfs");
4711b3e19d92SNick Piggin 
471274e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, false);
47133b22edc5STrond Myklebust 	if (IS_ERR(ns))
47141da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
471574e83122SAl Viro 	m = real_mount(mnt);
471674e83122SAl Viro 	m->mnt_ns = ns;
471774e83122SAl Viro 	ns->root = m;
471874e83122SAl Viro 	ns->mounts = 1;
471974e83122SAl Viro 	list_add(&m->mnt_list, &ns->list);
47206b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
47216b3286edSKirill Korotaev 	get_mnt_ns(ns);
47221da177e4SLinus Torvalds 
4723be08d6d2SAl Viro 	root.mnt = mnt;
4724be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
4725da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
4726ac748a09SJan Blunck 
4727ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
4728ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
47291da177e4SLinus Torvalds }
47301da177e4SLinus Torvalds 
mnt_init(void)473174bf17cfSDenis Cheng void __init mnt_init(void)
47321da177e4SLinus Torvalds {
473315a67dd8SRandy Dunlap 	int err;
47341da177e4SLinus Torvalds 
47357d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
473679f6540bSVasily Averin 			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
47371da177e4SLinus Torvalds 
47380818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
473938129a13SAl Viro 				sizeof(struct hlist_head),
47400818bf27SAl Viro 				mhash_entries, 19,
47413d375d78SPavel Tatashin 				HASH_ZERO,
47420818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
47430818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
47440818bf27SAl Viro 				sizeof(struct hlist_head),
47450818bf27SAl Viro 				mphash_entries, 19,
47463d375d78SPavel Tatashin 				HASH_ZERO,
47470818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
47481da177e4SLinus Torvalds 
474984d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
47501da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
47511da177e4SLinus Torvalds 
47524b93dc9bSTejun Heo 	kernfs_init();
47534b93dc9bSTejun Heo 
475415a67dd8SRandy Dunlap 	err = sysfs_init();
475515a67dd8SRandy Dunlap 	if (err)
475615a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
47578e24eea7SHarvey Harrison 			__func__, err);
475800d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
475900d26666SGreg Kroah-Hartman 	if (!fs_kobj)
47608e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
4761037f11b4SAl Viro 	shmem_init();
47621da177e4SLinus Torvalds 	init_rootfs();
47631da177e4SLinus Torvalds 	init_mount_tree();
47641da177e4SLinus Torvalds }
47651da177e4SLinus Torvalds 
put_mnt_ns(struct mnt_namespace * ns)4766616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
47671da177e4SLinus Torvalds {
47681a7b8969SKirill Tkhai 	if (!refcount_dec_and_test(&ns->ns.count))
4769616511d0STrond Myklebust 		return;
47707b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
4771771b1371SEric W. Biederman 	free_mnt_ns(ns);
47721da177e4SLinus Torvalds }
47739d412a43SAl Viro 
kern_mount(struct file_system_type * type)4774d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type)
47759d412a43SAl Viro {
4776423e0ab0STim Chen 	struct vfsmount *mnt;
4777d911b458SDavid Howells 	mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
4778423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
4779423e0ab0STim Chen 		/*
4780423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
4781423e0ab0STim Chen 		 * we unmount before file sys is unregistered
4782423e0ab0STim Chen 		*/
4783f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
4784423e0ab0STim Chen 	}
4785423e0ab0STim Chen 	return mnt;
47869d412a43SAl Viro }
4787d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount);
4788423e0ab0STim Chen 
kern_unmount(struct vfsmount * mnt)4789423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
4790423e0ab0STim Chen {
4791423e0ab0STim Chen 	/* release long term mount so mount point can be released */
4792da27f796SRik van Riel 	if (!IS_ERR(mnt)) {
4793da27f796SRik van Riel 		mnt_make_shortterm(mnt);
479448a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
4795423e0ab0STim Chen 		mntput(mnt);
4796423e0ab0STim Chen 	}
4797423e0ab0STim Chen }
4798423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
479902125a82SAl Viro 
kern_unmount_array(struct vfsmount * mnt[],unsigned int num)4800df820f8dSMiklos Szeredi void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
4801df820f8dSMiklos Szeredi {
4802df820f8dSMiklos Szeredi 	unsigned int i;
4803df820f8dSMiklos Szeredi 
4804df820f8dSMiklos Szeredi 	for (i = 0; i < num; i++)
4805da27f796SRik van Riel 		mnt_make_shortterm(mnt[i]);
4806df820f8dSMiklos Szeredi 	synchronize_rcu_expedited();
4807df820f8dSMiklos Szeredi 	for (i = 0; i < num; i++)
4808df820f8dSMiklos Szeredi 		mntput(mnt[i]);
4809df820f8dSMiklos Szeredi }
4810df820f8dSMiklos Szeredi EXPORT_SYMBOL(kern_unmount_array);
4811df820f8dSMiklos Szeredi 
our_mnt(struct vfsmount * mnt)481202125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
481302125a82SAl Viro {
4814143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
481502125a82SAl Viro }
48168823c079SEric W. Biederman 
current_chrooted(void)48173151527eSEric W. Biederman bool current_chrooted(void)
48183151527eSEric W. Biederman {
48193151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
48203151527eSEric W. Biederman 	struct path ns_root;
48213151527eSEric W. Biederman 	struct path fs_root;
48223151527eSEric W. Biederman 	bool chrooted;
48233151527eSEric W. Biederman 
48243151527eSEric W. Biederman 	/* Find the namespace root */
48253151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
48263151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
48273151527eSEric W. Biederman 	path_get(&ns_root);
48283151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
48293151527eSEric W. Biederman 		;
48303151527eSEric W. Biederman 
48313151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
48323151527eSEric W. Biederman 
48333151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
48343151527eSEric W. Biederman 
48353151527eSEric W. Biederman 	path_put(&fs_root);
48363151527eSEric W. Biederman 	path_put(&ns_root);
48373151527eSEric W. Biederman 
48383151527eSEric W. Biederman 	return chrooted;
48393151527eSEric W. Biederman }
48403151527eSEric W. Biederman 
mnt_already_visible(struct mnt_namespace * ns,const struct super_block * sb,int * new_mnt_flags)4841132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns,
4842132e4608SDavid Howells 				const struct super_block *sb,
48438654df4eSEric W. Biederman 				int *new_mnt_flags)
484487a8ebd6SEric W. Biederman {
48458c6cf9ccSEric W. Biederman 	int new_flags = *new_mnt_flags;
484687a8ebd6SEric W. Biederman 	struct mount *mnt;
4847e51db735SEric W. Biederman 	bool visible = false;
484887a8ebd6SEric W. Biederman 
484944bb4385SAl Viro 	down_read(&namespace_sem);
48509f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
485187a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
4852e51db735SEric W. Biederman 		struct mount *child;
485377b1a97dSEric W. Biederman 		int mnt_flags;
485477b1a97dSEric W. Biederman 
48559f6c61f9SMiklos Szeredi 		if (mnt_is_cursor(mnt))
48569f6c61f9SMiklos Szeredi 			continue;
48579f6c61f9SMiklos Szeredi 
4858132e4608SDavid Howells 		if (mnt->mnt.mnt_sb->s_type != sb->s_type)
4859e51db735SEric W. Biederman 			continue;
4860e51db735SEric W. Biederman 
48617e96c1b0SEric W. Biederman 		/* This mount is not fully visible if it's root directory
48627e96c1b0SEric W. Biederman 		 * is not the root directory of the filesystem.
48637e96c1b0SEric W. Biederman 		 */
48647e96c1b0SEric W. Biederman 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
48657e96c1b0SEric W. Biederman 			continue;
48667e96c1b0SEric W. Biederman 
4867a1935c17SEric W. Biederman 		/* A local view of the mount flags */
486877b1a97dSEric W. Biederman 		mnt_flags = mnt->mnt.mnt_flags;
486977b1a97dSEric W. Biederman 
4870695e9df0SEric W. Biederman 		/* Don't miss readonly hidden in the superblock flags */
4871bc98a42cSDavid Howells 		if (sb_rdonly(mnt->mnt.mnt_sb))
4872695e9df0SEric W. Biederman 			mnt_flags |= MNT_LOCK_READONLY;
4873695e9df0SEric W. Biederman 
48748c6cf9ccSEric W. Biederman 		/* Verify the mount flags are equal to or more permissive
48758c6cf9ccSEric W. Biederman 		 * than the proposed new mount.
48768c6cf9ccSEric W. Biederman 		 */
487777b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_READONLY) &&
48788c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_READONLY))
48798c6cf9ccSEric W. Biederman 			continue;
488077b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_ATIME) &&
488177b1a97dSEric W. Biederman 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
48828c6cf9ccSEric W. Biederman 			continue;
48838c6cf9ccSEric W. Biederman 
4884ceeb0e5dSEric W. Biederman 		/* This mount is not fully visible if there are any
4885ceeb0e5dSEric W. Biederman 		 * locked child mounts that cover anything except for
4886ceeb0e5dSEric W. Biederman 		 * empty directories.
4887e51db735SEric W. Biederman 		 */
4888e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
4889e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
4890ceeb0e5dSEric W. Biederman 			/* Only worry about locked mounts */
4891d71ed6c9SEric W. Biederman 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
4892ceeb0e5dSEric W. Biederman 				continue;
48937236c85eSEric W. Biederman 			/* Is the directory permanetly empty? */
48947236c85eSEric W. Biederman 			if (!is_empty_dir_inode(inode))
4895e51db735SEric W. Biederman 				goto next;
489687a8ebd6SEric W. Biederman 		}
48978c6cf9ccSEric W. Biederman 		/* Preserve the locked attributes */
489877b1a97dSEric W. Biederman 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
48998c6cf9ccSEric W. Biederman 					       MNT_LOCK_ATIME);
4900e51db735SEric W. Biederman 		visible = true;
4901e51db735SEric W. Biederman 		goto found;
4902e51db735SEric W. Biederman 	next:	;
490387a8ebd6SEric W. Biederman 	}
4904e51db735SEric W. Biederman found:
49059f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
490644bb4385SAl Viro 	up_read(&namespace_sem);
4907e51db735SEric W. Biederman 	return visible;
490887a8ebd6SEric W. Biederman }
490987a8ebd6SEric W. Biederman 
mount_too_revealing(const struct super_block * sb,int * new_mnt_flags)4910132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
49118654df4eSEric W. Biederman {
4912a1935c17SEric W. Biederman 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
49138654df4eSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
49148654df4eSEric W. Biederman 	unsigned long s_iflags;
49158654df4eSEric W. Biederman 
49168654df4eSEric W. Biederman 	if (ns->user_ns == &init_user_ns)
49178654df4eSEric W. Biederman 		return false;
49188654df4eSEric W. Biederman 
49198654df4eSEric W. Biederman 	/* Can this filesystem be too revealing? */
4920132e4608SDavid Howells 	s_iflags = sb->s_iflags;
49218654df4eSEric W. Biederman 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
49228654df4eSEric W. Biederman 		return false;
49238654df4eSEric W. Biederman 
4924a1935c17SEric W. Biederman 	if ((s_iflags & required_iflags) != required_iflags) {
4925a1935c17SEric W. Biederman 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
4926a1935c17SEric W. Biederman 			  required_iflags);
4927a1935c17SEric W. Biederman 		return true;
4928a1935c17SEric W. Biederman 	}
4929a1935c17SEric W. Biederman 
4930132e4608SDavid Howells 	return !mnt_already_visible(ns, sb, new_mnt_flags);
49318654df4eSEric W. Biederman }
49328654df4eSEric W. Biederman 
mnt_may_suid(struct vfsmount * mnt)4933380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt)
4934380cf5baSAndy Lutomirski {
4935380cf5baSAndy Lutomirski 	/*
4936380cf5baSAndy Lutomirski 	 * Foreign mounts (accessed via fchdir or through /proc
4937380cf5baSAndy Lutomirski 	 * symlinks) are always treated as if they are nosuid.  This
4938380cf5baSAndy Lutomirski 	 * prevents namespaces from trusting potentially unsafe
4939380cf5baSAndy Lutomirski 	 * suid/sgid bits, file caps, or security labels that originate
4940380cf5baSAndy Lutomirski 	 * in other namespaces.
4941380cf5baSAndy Lutomirski 	 */
4942380cf5baSAndy Lutomirski 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
4943380cf5baSAndy Lutomirski 	       current_in_userns(mnt->mnt_sb->s_user_ns);
4944380cf5baSAndy Lutomirski }
4945380cf5baSAndy Lutomirski 
mntns_get(struct task_struct * task)494664964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
49478823c079SEric W. Biederman {
494858be2825SAl Viro 	struct ns_common *ns = NULL;
49498823c079SEric W. Biederman 	struct nsproxy *nsproxy;
49508823c079SEric W. Biederman 
4951728dba3aSEric W. Biederman 	task_lock(task);
4952728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
49538823c079SEric W. Biederman 	if (nsproxy) {
495458be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
495558be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
49568823c079SEric W. Biederman 	}
4957728dba3aSEric W. Biederman 	task_unlock(task);
49588823c079SEric W. Biederman 
49598823c079SEric W. Biederman 	return ns;
49608823c079SEric W. Biederman }
49618823c079SEric W. Biederman 
mntns_put(struct ns_common * ns)496264964528SAl Viro static void mntns_put(struct ns_common *ns)
49638823c079SEric W. Biederman {
496458be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
49658823c079SEric W. Biederman }
49668823c079SEric W. Biederman 
mntns_install(struct nsset * nsset,struct ns_common * ns)4967f2a8d52eSChristian Brauner static int mntns_install(struct nsset *nsset, struct ns_common *ns)
49688823c079SEric W. Biederman {
4969f2a8d52eSChristian Brauner 	struct nsproxy *nsproxy = nsset->nsproxy;
4970f2a8d52eSChristian Brauner 	struct fs_struct *fs = nsset->fs;
49714f757f3cSAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
4972f2a8d52eSChristian Brauner 	struct user_namespace *user_ns = nsset->cred->user_ns;
49738823c079SEric W. Biederman 	struct path root;
49744f757f3cSAl Viro 	int err;
49758823c079SEric W. Biederman 
49760c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
4977f2a8d52eSChristian Brauner 	    !ns_capable(user_ns, CAP_SYS_CHROOT) ||
4978f2a8d52eSChristian Brauner 	    !ns_capable(user_ns, CAP_SYS_ADMIN))
4979ae11e0f1SZhao Hongjiang 		return -EPERM;
49808823c079SEric W. Biederman 
498174e83122SAl Viro 	if (is_anon_ns(mnt_ns))
498274e83122SAl Viro 		return -EINVAL;
498374e83122SAl Viro 
49848823c079SEric W. Biederman 	if (fs->users != 1)
49858823c079SEric W. Biederman 		return -EINVAL;
49868823c079SEric W. Biederman 
49878823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
49884f757f3cSAl Viro 	old_mnt_ns = nsproxy->mnt_ns;
49898823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
49908823c079SEric W. Biederman 
49918823c079SEric W. Biederman 	/* Find the root */
49924f757f3cSAl Viro 	err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
49934f757f3cSAl Viro 				"/", LOOKUP_DOWN, &root);
49944f757f3cSAl Viro 	if (err) {
49954f757f3cSAl Viro 		/* revert to old namespace */
49964f757f3cSAl Viro 		nsproxy->mnt_ns = old_mnt_ns;
49974f757f3cSAl Viro 		put_mnt_ns(mnt_ns);
49984f757f3cSAl Viro 		return err;
49994f757f3cSAl Viro 	}
50008823c079SEric W. Biederman 
50014068367cSAndrei Vagin 	put_mnt_ns(old_mnt_ns);
50024068367cSAndrei Vagin 
50038823c079SEric W. Biederman 	/* Update the pwd and root */
50048823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
50058823c079SEric W. Biederman 	set_fs_root(fs, &root);
50068823c079SEric W. Biederman 
50078823c079SEric W. Biederman 	path_put(&root);
50088823c079SEric W. Biederman 	return 0;
50098823c079SEric W. Biederman }
50108823c079SEric W. Biederman 
mntns_owner(struct ns_common * ns)5011bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns)
5012bcac25a5SAndrey Vagin {
5013bcac25a5SAndrey Vagin 	return to_mnt_ns(ns)->user_ns;
5014bcac25a5SAndrey Vagin }
5015bcac25a5SAndrey Vagin 
50168823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
50178823c079SEric W. Biederman 	.name		= "mnt",
50188823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
50198823c079SEric W. Biederman 	.get		= mntns_get,
50208823c079SEric W. Biederman 	.put		= mntns_put,
50218823c079SEric W. Biederman 	.install	= mntns_install,
5022bcac25a5SAndrey Vagin 	.owner		= mntns_owner,
50238823c079SEric W. Biederman };
5024ab171b95SLuis Chamberlain 
5025ab171b95SLuis Chamberlain #ifdef CONFIG_SYSCTL
5026ab171b95SLuis Chamberlain static struct ctl_table fs_namespace_sysctls[] = {
5027ab171b95SLuis Chamberlain 	{
5028ab171b95SLuis Chamberlain 		.procname	= "mount-max",
5029ab171b95SLuis Chamberlain 		.data		= &sysctl_mount_max,
5030ab171b95SLuis Chamberlain 		.maxlen		= sizeof(unsigned int),
5031ab171b95SLuis Chamberlain 		.mode		= 0644,
5032ab171b95SLuis Chamberlain 		.proc_handler	= proc_dointvec_minmax,
5033ab171b95SLuis Chamberlain 		.extra1		= SYSCTL_ONE,
5034ab171b95SLuis Chamberlain 	},
5035ab171b95SLuis Chamberlain 	{ }
5036ab171b95SLuis Chamberlain };
5037ab171b95SLuis Chamberlain 
init_fs_namespace_sysctls(void)5038ab171b95SLuis Chamberlain static int __init init_fs_namespace_sysctls(void)
5039ab171b95SLuis Chamberlain {
5040ab171b95SLuis Chamberlain 	register_sysctl_init("fs", fs_namespace_sysctls);
5041ab171b95SLuis Chamberlain 	return 0;
5042ab171b95SLuis Chamberlain }
5043ab171b95SLuis Chamberlain fs_initcall(init_fs_namespace_sysctls);
5044ab171b95SLuis Chamberlain 
5045ab171b95SLuis Chamberlain #endif /* CONFIG_SYSCTL */
5046