xref: /openbmc/linux/fs/namespace.c (revision da27f796)
159bd9dedSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/namespace.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * (C) Copyright Al Viro 2000, 2001
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Based on code from fs/super.c, copyright Linus Torvalds and others.
81da177e4SLinus Torvalds  * Heavily rewritten.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/syscalls.h>
12d10577a8SAl Viro #include <linux/export.h>
1316f7e0feSRandy Dunlap #include <linux/capability.h>
146b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
15771b1371SEric W. Biederman #include <linux/user_namespace.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
171da177e4SLinus Torvalds #include <linux/security.h>
185b825c3aSIngo Molnar #include <linux/cred.h>
1973cd49ecSMiklos Szeredi #include <linux/idr.h>
2057f150a5SRob Landley #include <linux/init.h>		/* init_rootfs */
21d10577a8SAl Viro #include <linux/fs_struct.h>	/* get_fs_root et.al. */
22d10577a8SAl Viro #include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
23a07b2000SAl Viro #include <linux/file.h>
24d10577a8SAl Viro #include <linux/uaccess.h>
250bb80f24SDavid Howells #include <linux/proc_ns.h>
2620b4fb48SLinus Torvalds #include <linux/magic.h>
2757c8a661SMike Rapoport #include <linux/memblock.h>
289caccd41SChristian Brauner #include <linux/proc_fs.h>
299ea459e1SAl Viro #include <linux/task_work.h>
309164bb4aSIngo Molnar #include <linux/sched/task.h>
31e262e32dSDavid Howells #include <uapi/linux/mount.h>
329bc61ab1SDavid Howells #include <linux/fs_context.h>
33037f11b4SAl Viro #include <linux/shmem_fs.h>
34bd303368SChristian Brauner #include <linux/mnt_idmapping.h>
359164bb4aSIngo Molnar 
3607b20889SRam Pai #include "pnode.h"
37948730b0SAdrian Bunk #include "internal.h"
381da177e4SLinus Torvalds 
39d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */
40ab171b95SLuis Chamberlain static unsigned int sysctl_mount_max __read_mostly = 100000;
41d2921684SEric W. Biederman 
420818bf27SAl Viro static unsigned int m_hash_mask __read_mostly;
430818bf27SAl Viro static unsigned int m_hash_shift __read_mostly;
440818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly;
450818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly;
460818bf27SAl Viro 
470818bf27SAl Viro static __initdata unsigned long mhash_entries;
480818bf27SAl Viro static int __init set_mhash_entries(char *str)
490818bf27SAl Viro {
500818bf27SAl Viro 	if (!str)
510818bf27SAl Viro 		return 0;
520818bf27SAl Viro 	mhash_entries = simple_strtoul(str, &str, 0);
530818bf27SAl Viro 	return 1;
540818bf27SAl Viro }
550818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries);
560818bf27SAl Viro 
570818bf27SAl Viro static __initdata unsigned long mphash_entries;
580818bf27SAl Viro static int __init set_mphash_entries(char *str)
590818bf27SAl Viro {
600818bf27SAl Viro 	if (!str)
610818bf27SAl Viro 		return 0;
620818bf27SAl Viro 	mphash_entries = simple_strtoul(str, &str, 0);
630818bf27SAl Viro 	return 1;
640818bf27SAl Viro }
650818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries);
6613f14b4dSEric Dumazet 
67c7999c36SAl Viro static u64 event;
6873cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
69719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
705addc5ddSAl Viro 
7138129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly;
720818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly;
73e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
7459aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem);
754edbe133SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
764edbe133SAl Viro static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
771da177e4SLinus Torvalds 
78256c8aedSChristian Brauner struct mnt_idmap {
79256c8aedSChristian Brauner 	struct user_namespace *owner;
80256c8aedSChristian Brauner 	refcount_t count;
81256c8aedSChristian Brauner };
82256c8aedSChristian Brauner 
83256c8aedSChristian Brauner /*
84256c8aedSChristian Brauner  * Carries the initial idmapping of 0:0:4294967295 which is an identity
85256c8aedSChristian Brauner  * mapping. This means that {g,u}id 0 is mapped to {g,u}id 0, {g,u}id 1 is
86256c8aedSChristian Brauner  * mapped to {g,u}id 1, [...], {g,u}id 1000 to {g,u}id 1000, [...].
87256c8aedSChristian Brauner  */
88256c8aedSChristian Brauner struct mnt_idmap nop_mnt_idmap = {
89256c8aedSChristian Brauner 	.owner	= &init_user_ns,
90256c8aedSChristian Brauner 	.count	= REFCOUNT_INIT(1),
91256c8aedSChristian Brauner };
92256c8aedSChristian Brauner EXPORT_SYMBOL_GPL(nop_mnt_idmap);
93256c8aedSChristian Brauner 
942a186721SChristian Brauner struct mount_kattr {
952a186721SChristian Brauner 	unsigned int attr_set;
962a186721SChristian Brauner 	unsigned int attr_clr;
972a186721SChristian Brauner 	unsigned int propagation;
982a186721SChristian Brauner 	unsigned int lookup_flags;
992a186721SChristian Brauner 	bool recurse;
1009caccd41SChristian Brauner 	struct user_namespace *mnt_userns;
101256c8aedSChristian Brauner 	struct mnt_idmap *mnt_idmap;
1022a186721SChristian Brauner };
1032a186721SChristian Brauner 
104f87fd4c2SMiklos Szeredi /* /sys/fs */
10500d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
10600d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
107f87fd4c2SMiklos Szeredi 
10899b7db7bSNick Piggin /*
10999b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
11099b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
11199b7db7bSNick Piggin  * up the tree.
11299b7db7bSNick Piggin  *
11399b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
11499b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
11599b7db7bSNick Piggin  */
11648a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
11799b7db7bSNick Piggin 
118d033cb67SChristian Brauner static inline void lock_mount_hash(void)
119d033cb67SChristian Brauner {
120d033cb67SChristian Brauner 	write_seqlock(&mount_lock);
121d033cb67SChristian Brauner }
122d033cb67SChristian Brauner 
123d033cb67SChristian Brauner static inline void unlock_mount_hash(void)
124d033cb67SChristian Brauner {
125d033cb67SChristian Brauner 	write_sequnlock(&mount_lock);
126d033cb67SChristian Brauner }
127d033cb67SChristian Brauner 
12838129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
1311da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
1320818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
1330818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
1340818bf27SAl Viro }
1350818bf27SAl Viro 
1360818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
1370818bf27SAl Viro {
1380818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
1390818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
1400818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
143b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
14473cd49ecSMiklos Szeredi {
145169b480eSMatthew Wilcox 	int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
14673cd49ecSMiklos Szeredi 
147169b480eSMatthew Wilcox 	if (res < 0)
14873cd49ecSMiklos Szeredi 		return res;
149169b480eSMatthew Wilcox 	mnt->mnt_id = res;
150169b480eSMatthew Wilcox 	return 0;
15173cd49ecSMiklos Szeredi }
15273cd49ecSMiklos Szeredi 
153b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
15473cd49ecSMiklos Szeredi {
155169b480eSMatthew Wilcox 	ida_free(&mnt_id_ida, mnt->mnt_id);
15673cd49ecSMiklos Szeredi }
15773cd49ecSMiklos Szeredi 
158719f5d7fSMiklos Szeredi /*
159719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
160719f5d7fSMiklos Szeredi  */
1614b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
162719f5d7fSMiklos Szeredi {
163169b480eSMatthew Wilcox 	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
164f21f6220SAl Viro 
165169b480eSMatthew Wilcox 	if (res < 0)
166f21f6220SAl Viro 		return res;
167169b480eSMatthew Wilcox 	mnt->mnt_group_id = res;
168169b480eSMatthew Wilcox 	return 0;
169719f5d7fSMiklos Szeredi }
170719f5d7fSMiklos Szeredi 
171719f5d7fSMiklos Szeredi /*
172719f5d7fSMiklos Szeredi  * Release a peer group ID
173719f5d7fSMiklos Szeredi  */
1744b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
175719f5d7fSMiklos Szeredi {
176169b480eSMatthew Wilcox 	ida_free(&mnt_group_ida, mnt->mnt_group_id);
17715169fe7SAl Viro 	mnt->mnt_group_id = 0;
178719f5d7fSMiklos Szeredi }
179719f5d7fSMiklos Szeredi 
180b3e19d92SNick Piggin /*
181b3e19d92SNick Piggin  * vfsmount lock must be held for read
182b3e19d92SNick Piggin  */
18383adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
184b3e19d92SNick Piggin {
185b3e19d92SNick Piggin #ifdef CONFIG_SMP
18668e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
187b3e19d92SNick Piggin #else
188b3e19d92SNick Piggin 	preempt_disable();
18968e8a9feSAl Viro 	mnt->mnt_count += n;
190b3e19d92SNick Piggin 	preempt_enable();
191b3e19d92SNick Piggin #endif
192b3e19d92SNick Piggin }
193b3e19d92SNick Piggin 
194b3e19d92SNick Piggin /*
195b3e19d92SNick Piggin  * vfsmount lock must be held for write
196b3e19d92SNick Piggin  */
197edf7ddbfSEric Biggers int mnt_get_count(struct mount *mnt)
198b3e19d92SNick Piggin {
199b3e19d92SNick Piggin #ifdef CONFIG_SMP
200edf7ddbfSEric Biggers 	int count = 0;
201b3e19d92SNick Piggin 	int cpu;
202b3e19d92SNick Piggin 
203b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
20468e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
205b3e19d92SNick Piggin 	}
206b3e19d92SNick Piggin 
207b3e19d92SNick Piggin 	return count;
208b3e19d92SNick Piggin #else
20968e8a9feSAl Viro 	return mnt->mnt_count;
210b3e19d92SNick Piggin #endif
211b3e19d92SNick Piggin }
212b3e19d92SNick Piggin 
213256c8aedSChristian Brauner /**
214256c8aedSChristian Brauner  * mnt_idmap_owner - retrieve owner of the mount's idmapping
215256c8aedSChristian Brauner  * @idmap: mount idmapping
216256c8aedSChristian Brauner  *
217256c8aedSChristian Brauner  * This helper will go away once the conversion to use struct mnt_idmap
218256c8aedSChristian Brauner  * everywhere has finished at which point the helper will be unexported.
219256c8aedSChristian Brauner  *
220256c8aedSChristian Brauner  * Only code that needs to perform permission checks based on the owner of the
221256c8aedSChristian Brauner  * idmapping will get access to it. All other code will solely rely on
222256c8aedSChristian Brauner  * idmappings. This will get us type safety so it's impossible to conflate
223256c8aedSChristian Brauner  * filesystems idmappings with mount idmappings.
224256c8aedSChristian Brauner  *
225256c8aedSChristian Brauner  * Return: The owner of the idmapping.
226256c8aedSChristian Brauner  */
227256c8aedSChristian Brauner struct user_namespace *mnt_idmap_owner(const struct mnt_idmap *idmap)
228256c8aedSChristian Brauner {
229256c8aedSChristian Brauner 	return idmap->owner;
230256c8aedSChristian Brauner }
231256c8aedSChristian Brauner EXPORT_SYMBOL_GPL(mnt_idmap_owner);
232256c8aedSChristian Brauner 
233256c8aedSChristian Brauner /**
234256c8aedSChristian Brauner  * mnt_user_ns - retrieve owner of an idmapped mount
235256c8aedSChristian Brauner  * @mnt: the relevant vfsmount
236256c8aedSChristian Brauner  *
237256c8aedSChristian Brauner  * This helper will go away once the conversion to use struct mnt_idmap
238256c8aedSChristian Brauner  * everywhere has finished at which point the helper will be unexported.
239256c8aedSChristian Brauner  *
240256c8aedSChristian Brauner  * Only code that needs to perform permission checks based on the owner of the
241256c8aedSChristian Brauner  * idmapping will get access to it. All other code will solely rely on
242256c8aedSChristian Brauner  * idmappings. This will get us type safety so it's impossible to conflate
243256c8aedSChristian Brauner  * filesystems idmappings with mount idmappings.
244256c8aedSChristian Brauner  *
245256c8aedSChristian Brauner  * Return: The owner of the idmapped.
246256c8aedSChristian Brauner  */
247256c8aedSChristian Brauner struct user_namespace *mnt_user_ns(const struct vfsmount *mnt)
248256c8aedSChristian Brauner {
249256c8aedSChristian Brauner 	struct mnt_idmap *idmap = mnt_idmap(mnt);
250256c8aedSChristian Brauner 
251256c8aedSChristian Brauner 	/* Return the actual owner of the filesystem instead of the nop. */
252256c8aedSChristian Brauner 	if (idmap == &nop_mnt_idmap &&
253256c8aedSChristian Brauner 	    !initial_idmapping(mnt->mnt_sb->s_user_ns))
254256c8aedSChristian Brauner 		return mnt->mnt_sb->s_user_ns;
255256c8aedSChristian Brauner 	return mnt_idmap_owner(idmap);
256256c8aedSChristian Brauner }
257256c8aedSChristian Brauner EXPORT_SYMBOL_GPL(mnt_user_ns);
258256c8aedSChristian Brauner 
259256c8aedSChristian Brauner /**
260256c8aedSChristian Brauner  * alloc_mnt_idmap - allocate a new idmapping for the mount
261256c8aedSChristian Brauner  * @mnt_userns: owning userns of the idmapping
262256c8aedSChristian Brauner  *
263256c8aedSChristian Brauner  * Allocate a new struct mnt_idmap which carries the idmapping of the mount.
264256c8aedSChristian Brauner  *
265256c8aedSChristian Brauner  * Return: On success a new idmap, on error an error pointer is returned.
266256c8aedSChristian Brauner  */
267256c8aedSChristian Brauner static struct mnt_idmap *alloc_mnt_idmap(struct user_namespace *mnt_userns)
268256c8aedSChristian Brauner {
269256c8aedSChristian Brauner 	struct mnt_idmap *idmap;
270256c8aedSChristian Brauner 
271256c8aedSChristian Brauner 	idmap = kzalloc(sizeof(struct mnt_idmap), GFP_KERNEL_ACCOUNT);
272256c8aedSChristian Brauner 	if (!idmap)
273256c8aedSChristian Brauner 		return ERR_PTR(-ENOMEM);
274256c8aedSChristian Brauner 
275256c8aedSChristian Brauner 	idmap->owner = get_user_ns(mnt_userns);
276256c8aedSChristian Brauner 	refcount_set(&idmap->count, 1);
277256c8aedSChristian Brauner 	return idmap;
278256c8aedSChristian Brauner }
279256c8aedSChristian Brauner 
280256c8aedSChristian Brauner /**
281256c8aedSChristian Brauner  * mnt_idmap_get - get a reference to an idmapping
282256c8aedSChristian Brauner  * @idmap: the idmap to bump the reference on
283256c8aedSChristian Brauner  *
284256c8aedSChristian Brauner  * If @idmap is not the @nop_mnt_idmap bump the reference count.
285256c8aedSChristian Brauner  *
286256c8aedSChristian Brauner  * Return: @idmap with reference count bumped if @not_mnt_idmap isn't passed.
287256c8aedSChristian Brauner  */
288256c8aedSChristian Brauner static inline struct mnt_idmap *mnt_idmap_get(struct mnt_idmap *idmap)
289256c8aedSChristian Brauner {
290256c8aedSChristian Brauner 	if (idmap != &nop_mnt_idmap)
291256c8aedSChristian Brauner 		refcount_inc(&idmap->count);
292256c8aedSChristian Brauner 
293256c8aedSChristian Brauner 	return idmap;
294256c8aedSChristian Brauner }
295256c8aedSChristian Brauner 
296256c8aedSChristian Brauner /**
297256c8aedSChristian Brauner  * mnt_idmap_put - put a reference to an idmapping
298256c8aedSChristian Brauner  * @idmap: the idmap to put the reference on
299256c8aedSChristian Brauner  *
300256c8aedSChristian Brauner  * If this is a non-initial idmapping, put the reference count when a mount is
301256c8aedSChristian Brauner  * released and free it if we're the last user.
302256c8aedSChristian Brauner  */
303256c8aedSChristian Brauner static inline void mnt_idmap_put(struct mnt_idmap *idmap)
304256c8aedSChristian Brauner {
305256c8aedSChristian Brauner 	if (idmap != &nop_mnt_idmap && refcount_dec_and_test(&idmap->count)) {
306256c8aedSChristian Brauner 		put_user_ns(idmap->owner);
307256c8aedSChristian Brauner 		kfree(idmap);
308256c8aedSChristian Brauner 	}
309256c8aedSChristian Brauner }
310256c8aedSChristian Brauner 
311b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
3121da177e4SLinus Torvalds {
313c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
314c63181e6SAl Viro 	if (mnt) {
31573cd49ecSMiklos Szeredi 		int err;
31673cd49ecSMiklos Szeredi 
317c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
31888b38782SLi Zefan 		if (err)
31988b38782SLi Zefan 			goto out_free_cache;
32088b38782SLi Zefan 
32188b38782SLi Zefan 		if (name) {
32279f6540bSVasily Averin 			mnt->mnt_devname = kstrdup_const(name,
32379f6540bSVasily Averin 							 GFP_KERNEL_ACCOUNT);
324c63181e6SAl Viro 			if (!mnt->mnt_devname)
32588b38782SLi Zefan 				goto out_free_id;
32673cd49ecSMiklos Szeredi 		}
32773cd49ecSMiklos Szeredi 
328b3e19d92SNick Piggin #ifdef CONFIG_SMP
329c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
330c63181e6SAl Viro 		if (!mnt->mnt_pcp)
331b3e19d92SNick Piggin 			goto out_free_devname;
332b3e19d92SNick Piggin 
333c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
334b3e19d92SNick Piggin #else
335c63181e6SAl Viro 		mnt->mnt_count = 1;
336c63181e6SAl Viro 		mnt->mnt_writers = 0;
337b3e19d92SNick Piggin #endif
338b3e19d92SNick Piggin 
33938129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
340c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
341c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
342c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
343c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
344c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
345c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
346c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
3470a5eb7c8SEric W. Biederman 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
34899b19d16SEric W. Biederman 		INIT_LIST_HEAD(&mnt->mnt_umounting);
34956cbb429SAl Viro 		INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
350256c8aedSChristian Brauner 		mnt->mnt.mnt_idmap = &nop_mnt_idmap;
3511da177e4SLinus Torvalds 	}
352c63181e6SAl Viro 	return mnt;
35388b38782SLi Zefan 
354d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
355d3ef3d73Snpiggin@suse.de out_free_devname:
356fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
357d3ef3d73Snpiggin@suse.de #endif
35888b38782SLi Zefan out_free_id:
359c63181e6SAl Viro 	mnt_free_id(mnt);
36088b38782SLi Zefan out_free_cache:
361c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
36288b38782SLi Zefan 	return NULL;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3658366025eSDave Hansen /*
3668366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
3678366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
3688366025eSDave Hansen  * We must keep track of when those operations start
3698366025eSDave Hansen  * (for permission checks) and when they end, so that
3708366025eSDave Hansen  * we can determine when writes are able to occur to
3718366025eSDave Hansen  * a filesystem.
3728366025eSDave Hansen  */
3733d733633SDave Hansen /*
3743d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
3753d733633SDave Hansen  * @mnt: the mount to check for its write status
3763d733633SDave Hansen  *
3773d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
3783d733633SDave Hansen  * It does not guarantee that the filesystem will stay
3793d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
3803d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
3813d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
3823d733633SDave Hansen  * r/w.
3833d733633SDave Hansen  */
38443f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt)
3853d733633SDave Hansen {
38643f5e655SDavid Howells 	return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
3873d733633SDave Hansen }
3883d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
3893d733633SDave Hansen 
39083adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
3913d733633SDave Hansen {
392d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
39368e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
394d3ef3d73Snpiggin@suse.de #else
39568e8a9feSAl Viro 	mnt->mnt_writers++;
396d3ef3d73Snpiggin@suse.de #endif
3973d733633SDave Hansen }
3983d733633SDave Hansen 
39983adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
4003d733633SDave Hansen {
401d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
40268e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
403d3ef3d73Snpiggin@suse.de #else
40468e8a9feSAl Viro 	mnt->mnt_writers--;
405d3ef3d73Snpiggin@suse.de #endif
406d3ef3d73Snpiggin@suse.de }
407d3ef3d73Snpiggin@suse.de 
40883adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
409d3ef3d73Snpiggin@suse.de {
410d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
411d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
4123d733633SDave Hansen 	int cpu;
4133d733633SDave Hansen 
4143d733633SDave Hansen 	for_each_possible_cpu(cpu) {
41568e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
4163d733633SDave Hansen 	}
4173d733633SDave Hansen 
418d3ef3d73Snpiggin@suse.de 	return count;
419d3ef3d73Snpiggin@suse.de #else
420d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
421d3ef3d73Snpiggin@suse.de #endif
4223d733633SDave Hansen }
4233d733633SDave Hansen 
4244ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
4254ed5e82fSMiklos Szeredi {
4264ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
4274ed5e82fSMiklos Szeredi 		return 1;
4284ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
4294ed5e82fSMiklos Szeredi 	smp_rmb();
4304ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
4314ed5e82fSMiklos Szeredi }
4324ed5e82fSMiklos Szeredi 
4333d733633SDave Hansen /*
434eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
435eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
436eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
437eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
4383d733633SDave Hansen  */
4398366025eSDave Hansen /**
440eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
44183adc753SAl Viro  * @m: the mount on which to take a write
4428366025eSDave Hansen  *
443eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
444eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
445eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
446eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
447eb04c282SJan Kara  * called. This is effectively a refcount.
4488366025eSDave Hansen  */
449eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
4508366025eSDave Hansen {
45183adc753SAl Viro 	struct mount *mnt = real_mount(m);
4523d733633SDave Hansen 	int ret = 0;
4533d733633SDave Hansen 
454d3ef3d73Snpiggin@suse.de 	preempt_disable();
455c6653a83SNick Piggin 	mnt_inc_writers(mnt);
456d3ef3d73Snpiggin@suse.de 	/*
457c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
458d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
459d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
460d3ef3d73Snpiggin@suse.de 	 */
461d3ef3d73Snpiggin@suse.de 	smp_mb();
4620f8821daSSebastian Andrzej Siewior 	might_lock(&mount_lock.lock);
4630f8821daSSebastian Andrzej Siewior 	while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
4640f8821daSSebastian Andrzej Siewior 		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
465d3ef3d73Snpiggin@suse.de 			cpu_relax();
4660f8821daSSebastian Andrzej Siewior 		} else {
4670f8821daSSebastian Andrzej Siewior 			/*
4680f8821daSSebastian Andrzej Siewior 			 * This prevents priority inversion, if the task
4690f8821daSSebastian Andrzej Siewior 			 * setting MNT_WRITE_HOLD got preempted on a remote
4700f8821daSSebastian Andrzej Siewior 			 * CPU, and it prevents life lock if the task setting
4710f8821daSSebastian Andrzej Siewior 			 * MNT_WRITE_HOLD has a lower priority and is bound to
4720f8821daSSebastian Andrzej Siewior 			 * the same CPU as the task that is spinning here.
4730f8821daSSebastian Andrzej Siewior 			 */
4740f8821daSSebastian Andrzej Siewior 			preempt_enable();
4750f8821daSSebastian Andrzej Siewior 			lock_mount_hash();
4760f8821daSSebastian Andrzej Siewior 			unlock_mount_hash();
4770f8821daSSebastian Andrzej Siewior 			preempt_disable();
4780f8821daSSebastian Andrzej Siewior 		}
4790f8821daSSebastian Andrzej Siewior 	}
480d3ef3d73Snpiggin@suse.de 	/*
481d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
482d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
483d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
484d3ef3d73Snpiggin@suse.de 	 */
485d3ef3d73Snpiggin@suse.de 	smp_rmb();
4864ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
487c6653a83SNick Piggin 		mnt_dec_writers(mnt);
4883d733633SDave Hansen 		ret = -EROFS;
4893d733633SDave Hansen 	}
490d3ef3d73Snpiggin@suse.de 	preempt_enable();
491eb04c282SJan Kara 
492eb04c282SJan Kara 	return ret;
493eb04c282SJan Kara }
494eb04c282SJan Kara 
495eb04c282SJan Kara /**
496eb04c282SJan Kara  * mnt_want_write - get write access to a mount
497eb04c282SJan Kara  * @m: the mount on which to take a write
498eb04c282SJan Kara  *
499eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
500eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
501eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
502eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
503eb04c282SJan Kara  */
504eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
505eb04c282SJan Kara {
506eb04c282SJan Kara 	int ret;
507eb04c282SJan Kara 
508eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
509eb04c282SJan Kara 	ret = __mnt_want_write(m);
510eb04c282SJan Kara 	if (ret)
511eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
5123d733633SDave Hansen 	return ret;
5138366025eSDave Hansen }
5148366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
5158366025eSDave Hansen 
5168366025eSDave Hansen /**
517eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
518eb04c282SJan Kara  * @file: the file who's mount on which to take a write
519eb04c282SJan Kara  *
52014e43bf4SEric Biggers  * This is like __mnt_want_write, but if the file is already open for writing it
52114e43bf4SEric Biggers  * skips incrementing mnt_writers (since the open file already has a reference)
52214e43bf4SEric Biggers  * and instead only does the check for emergency r/o remounts.  This must be
52314e43bf4SEric Biggers  * paired with __mnt_drop_write_file.
524eb04c282SJan Kara  */
525eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
526eb04c282SJan Kara {
52714e43bf4SEric Biggers 	if (file->f_mode & FMODE_WRITER) {
52814e43bf4SEric Biggers 		/*
52914e43bf4SEric Biggers 		 * Superblock may have become readonly while there are still
53014e43bf4SEric Biggers 		 * writable fd's, e.g. due to a fs error with errors=remount-ro
53114e43bf4SEric Biggers 		 */
53214e43bf4SEric Biggers 		if (__mnt_is_readonly(file->f_path.mnt))
53314e43bf4SEric Biggers 			return -EROFS;
53414e43bf4SEric Biggers 		return 0;
53514e43bf4SEric Biggers 	}
536eb04c282SJan Kara 	return __mnt_want_write(file->f_path.mnt);
537eb04c282SJan Kara }
538eb04c282SJan Kara 
539eb04c282SJan Kara /**
5407c6893e3SMiklos Szeredi  * mnt_want_write_file - get write access to a file's mount
5417c6893e3SMiklos Szeredi  * @file: the file who's mount on which to take a write
5427c6893e3SMiklos Szeredi  *
54314e43bf4SEric Biggers  * This is like mnt_want_write, but if the file is already open for writing it
54414e43bf4SEric Biggers  * skips incrementing mnt_writers (since the open file already has a reference)
54514e43bf4SEric Biggers  * and instead only does the freeze protection and the check for emergency r/o
54614e43bf4SEric Biggers  * remounts.  This must be paired with mnt_drop_write_file.
5477c6893e3SMiklos Szeredi  */
5487c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file)
5497c6893e3SMiklos Szeredi {
5507c6893e3SMiklos Szeredi 	int ret;
5517c6893e3SMiklos Szeredi 
5527c6893e3SMiklos Szeredi 	sb_start_write(file_inode(file)->i_sb);
5537c6893e3SMiklos Szeredi 	ret = __mnt_want_write_file(file);
5547c6893e3SMiklos Szeredi 	if (ret)
5557c6893e3SMiklos Szeredi 		sb_end_write(file_inode(file)->i_sb);
5567c6893e3SMiklos Szeredi 	return ret;
5577c6893e3SMiklos Szeredi }
55896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
55996029c4eSnpiggin@suse.de 
56096029c4eSnpiggin@suse.de /**
561eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
5628366025eSDave Hansen  * @mnt: the mount on which to give up write access
5638366025eSDave Hansen  *
5648366025eSDave Hansen  * Tells the low-level filesystem that we are done
5658366025eSDave Hansen  * performing writes to it.  Must be matched with
566eb04c282SJan Kara  * __mnt_want_write() call above.
5678366025eSDave Hansen  */
568eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
5698366025eSDave Hansen {
570d3ef3d73Snpiggin@suse.de 	preempt_disable();
57183adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
572d3ef3d73Snpiggin@suse.de 	preempt_enable();
5738366025eSDave Hansen }
574eb04c282SJan Kara 
575eb04c282SJan Kara /**
576eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
577eb04c282SJan Kara  * @mnt: the mount on which to give up write access
578eb04c282SJan Kara  *
579eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
580eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
581eb04c282SJan Kara  * mnt_want_write() call above.
582eb04c282SJan Kara  */
583eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
584eb04c282SJan Kara {
585eb04c282SJan Kara 	__mnt_drop_write(mnt);
586eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
587eb04c282SJan Kara }
5888366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
5898366025eSDave Hansen 
590eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
591eb04c282SJan Kara {
59214e43bf4SEric Biggers 	if (!(file->f_mode & FMODE_WRITER))
593eb04c282SJan Kara 		__mnt_drop_write(file->f_path.mnt);
594eb04c282SJan Kara }
595eb04c282SJan Kara 
5967c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file)
5977c6893e3SMiklos Szeredi {
598a6795a58SMiklos Szeredi 	__mnt_drop_write_file(file);
5997c6893e3SMiklos Szeredi 	sb_end_write(file_inode(file)->i_sb);
6007c6893e3SMiklos Szeredi }
6012a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
6022a79f17eSAl Viro 
603538f4f02SChristian Brauner /**
604538f4f02SChristian Brauner  * mnt_hold_writers - prevent write access to the given mount
605538f4f02SChristian Brauner  * @mnt: mnt to prevent write access to
606538f4f02SChristian Brauner  *
607538f4f02SChristian Brauner  * Prevents write access to @mnt if there are no active writers for @mnt.
608538f4f02SChristian Brauner  * This function needs to be called and return successfully before changing
609538f4f02SChristian Brauner  * properties of @mnt that need to remain stable for callers with write access
610538f4f02SChristian Brauner  * to @mnt.
611538f4f02SChristian Brauner  *
612538f4f02SChristian Brauner  * After this functions has been called successfully callers must pair it with
613538f4f02SChristian Brauner  * a call to mnt_unhold_writers() in order to stop preventing write access to
614538f4f02SChristian Brauner  * @mnt.
615538f4f02SChristian Brauner  *
616538f4f02SChristian Brauner  * Context: This function expects lock_mount_hash() to be held serializing
617538f4f02SChristian Brauner  *          setting MNT_WRITE_HOLD.
618538f4f02SChristian Brauner  * Return: On success 0 is returned.
619538f4f02SChristian Brauner  *	   On error, -EBUSY is returned.
620538f4f02SChristian Brauner  */
621fbdc2f6cSChristian Brauner static inline int mnt_hold_writers(struct mount *mnt)
6228366025eSDave Hansen {
62383adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
624d3ef3d73Snpiggin@suse.de 	/*
625d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
626d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
627d3ef3d73Snpiggin@suse.de 	 */
628d3ef3d73Snpiggin@suse.de 	smp_mb();
629d3ef3d73Snpiggin@suse.de 
630d3ef3d73Snpiggin@suse.de 	/*
631d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
632d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
633d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
634d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
635d3ef3d73Snpiggin@suse.de 	 *
636d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
637d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
638d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
639d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
640d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
641d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
642d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
643d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
644d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
645d3ef3d73Snpiggin@suse.de 	 */
646c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
647fbdc2f6cSChristian Brauner 		return -EBUSY;
648fbdc2f6cSChristian Brauner 
649fbdc2f6cSChristian Brauner 	return 0;
650fbdc2f6cSChristian Brauner }
651fbdc2f6cSChristian Brauner 
652538f4f02SChristian Brauner /**
653538f4f02SChristian Brauner  * mnt_unhold_writers - stop preventing write access to the given mount
654538f4f02SChristian Brauner  * @mnt: mnt to stop preventing write access to
655538f4f02SChristian Brauner  *
656538f4f02SChristian Brauner  * Stop preventing write access to @mnt allowing callers to gain write access
657538f4f02SChristian Brauner  * to @mnt again.
658538f4f02SChristian Brauner  *
659538f4f02SChristian Brauner  * This function can only be called after a successful call to
660538f4f02SChristian Brauner  * mnt_hold_writers().
661538f4f02SChristian Brauner  *
662538f4f02SChristian Brauner  * Context: This function expects lock_mount_hash() to be held.
663538f4f02SChristian Brauner  */
664fbdc2f6cSChristian Brauner static inline void mnt_unhold_writers(struct mount *mnt)
665fbdc2f6cSChristian Brauner {
666d3ef3d73Snpiggin@suse.de 	/*
667d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
668d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
669d3ef3d73Snpiggin@suse.de 	 */
670d3ef3d73Snpiggin@suse.de 	smp_wmb();
67183adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
672fbdc2f6cSChristian Brauner }
673fbdc2f6cSChristian Brauner 
674fbdc2f6cSChristian Brauner static int mnt_make_readonly(struct mount *mnt)
675fbdc2f6cSChristian Brauner {
676fbdc2f6cSChristian Brauner 	int ret;
677fbdc2f6cSChristian Brauner 
678fbdc2f6cSChristian Brauner 	ret = mnt_hold_writers(mnt);
679fbdc2f6cSChristian Brauner 	if (!ret)
680fbdc2f6cSChristian Brauner 		mnt->mnt.mnt_flags |= MNT_READONLY;
681fbdc2f6cSChristian Brauner 	mnt_unhold_writers(mnt);
6823d733633SDave Hansen 	return ret;
6833d733633SDave Hansen }
6848366025eSDave Hansen 
6854ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
6864ed5e82fSMiklos Szeredi {
6874ed5e82fSMiklos Szeredi 	struct mount *mnt;
6884ed5e82fSMiklos Szeredi 	int err = 0;
6894ed5e82fSMiklos Szeredi 
6908e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
6918e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
6928e8b8796SMiklos Szeredi 		return -EBUSY;
6938e8b8796SMiklos Szeredi 
694719ea2fbSAl Viro 	lock_mount_hash();
6954ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
6964ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
697ad1844a0SChristian Brauner 			err = mnt_hold_writers(mnt);
698ad1844a0SChristian Brauner 			if (err)
6994ed5e82fSMiklos Szeredi 				break;
7004ed5e82fSMiklos Szeredi 		}
7014ed5e82fSMiklos Szeredi 	}
7028e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
7038e8b8796SMiklos Szeredi 		err = -EBUSY;
7048e8b8796SMiklos Szeredi 
7054ed5e82fSMiklos Szeredi 	if (!err) {
7064ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
7074ed5e82fSMiklos Szeredi 		smp_wmb();
7084ed5e82fSMiklos Szeredi 	}
7094ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
7104ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
7114ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
7124ed5e82fSMiklos Szeredi 	}
713719ea2fbSAl Viro 	unlock_mount_hash();
7144ed5e82fSMiklos Szeredi 
7154ed5e82fSMiklos Szeredi 	return err;
7164ed5e82fSMiklos Szeredi }
7174ed5e82fSMiklos Szeredi 
718b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
7191da177e4SLinus Torvalds {
720256c8aedSChristian Brauner 	mnt_idmap_put(mnt_idmap(&mnt->mnt));
721fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
722d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
72368e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
724d3ef3d73Snpiggin@suse.de #endif
725b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7288ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
7298ffcb32eSDavid Howells {
7308ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
7318ffcb32eSDavid Howells }
7328ffcb32eSDavid Howells 
73348a066e7SAl Viro /* call under rcu_read_lock */
734294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
73548a066e7SAl Viro {
73648a066e7SAl Viro 	struct mount *mnt;
73748a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
738294d71ffSAl Viro 		return 1;
73948a066e7SAl Viro 	if (bastard == NULL)
740294d71ffSAl Viro 		return 0;
74148a066e7SAl Viro 	mnt = real_mount(bastard);
74248a066e7SAl Viro 	mnt_add_count(mnt, 1);
743119e1ef8SAl Viro 	smp_mb();			// see mntput_no_expire()
74448a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
745294d71ffSAl Viro 		return 0;
74648a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
74748a066e7SAl Viro 		mnt_add_count(mnt, -1);
748294d71ffSAl Viro 		return 1;
74948a066e7SAl Viro 	}
750119e1ef8SAl Viro 	lock_mount_hash();
751119e1ef8SAl Viro 	if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
752119e1ef8SAl Viro 		mnt_add_count(mnt, -1);
753119e1ef8SAl Viro 		unlock_mount_hash();
754119e1ef8SAl Viro 		return 1;
755119e1ef8SAl Viro 	}
756119e1ef8SAl Viro 	unlock_mount_hash();
757119e1ef8SAl Viro 	/* caller will mntput() */
758294d71ffSAl Viro 	return -1;
759294d71ffSAl Viro }
760294d71ffSAl Viro 
761294d71ffSAl Viro /* call under rcu_read_lock */
7627e4745a0SAl Viro static bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
763294d71ffSAl Viro {
764294d71ffSAl Viro 	int res = __legitimize_mnt(bastard, seq);
765294d71ffSAl Viro 	if (likely(!res))
766294d71ffSAl Viro 		return true;
767294d71ffSAl Viro 	if (unlikely(res < 0)) {
76848a066e7SAl Viro 		rcu_read_unlock();
76948a066e7SAl Viro 		mntput(bastard);
77048a066e7SAl Viro 		rcu_read_lock();
771294d71ffSAl Viro 	}
77248a066e7SAl Viro 	return false;
77348a066e7SAl Viro }
77448a066e7SAl Viro 
7751da177e4SLinus Torvalds /*
776474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
77748a066e7SAl Viro  * call under rcu_read_lock()
7781da177e4SLinus Torvalds  */
779474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
7801da177e4SLinus Torvalds {
78138129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
782474279dcSAl Viro 	struct mount *p;
7831da177e4SLinus Torvalds 
78438129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
785474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
786474279dcSAl Viro 			return p;
787474279dcSAl Viro 	return NULL;
7881da177e4SLinus Torvalds }
789474279dcSAl Viro 
790474279dcSAl Viro /*
791f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
792f015f126SDavid Howells  *
793f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
794f015f126SDavid Howells  * following mounts:
795f015f126SDavid Howells  *
796f015f126SDavid Howells  * mount /dev/sda1 /mnt
797f015f126SDavid Howells  * mount /dev/sda2 /mnt
798f015f126SDavid Howells  * mount /dev/sda3 /mnt
799f015f126SDavid Howells  *
800f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
801f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
802f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
803f015f126SDavid Howells  *
804f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
805a05964f3SRam Pai  */
806ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path)
807a05964f3SRam Pai {
808c7105365SAl Viro 	struct mount *child_mnt;
80948a066e7SAl Viro 	struct vfsmount *m;
81048a066e7SAl Viro 	unsigned seq;
81199b7db7bSNick Piggin 
81248a066e7SAl Viro 	rcu_read_lock();
81348a066e7SAl Viro 	do {
81448a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
815474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
81648a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
81748a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
81848a066e7SAl Viro 	rcu_read_unlock();
81948a066e7SAl Viro 	return m;
820a05964f3SRam Pai }
821a05964f3SRam Pai 
8229f6c61f9SMiklos Szeredi static inline void lock_ns_list(struct mnt_namespace *ns)
8239f6c61f9SMiklos Szeredi {
8249f6c61f9SMiklos Szeredi 	spin_lock(&ns->ns_lock);
8259f6c61f9SMiklos Szeredi }
8269f6c61f9SMiklos Szeredi 
8279f6c61f9SMiklos Szeredi static inline void unlock_ns_list(struct mnt_namespace *ns)
8289f6c61f9SMiklos Szeredi {
8299f6c61f9SMiklos Szeredi 	spin_unlock(&ns->ns_lock);
8309f6c61f9SMiklos Szeredi }
8319f6c61f9SMiklos Szeredi 
8329f6c61f9SMiklos Szeredi static inline bool mnt_is_cursor(struct mount *mnt)
8339f6c61f9SMiklos Szeredi {
8349f6c61f9SMiklos Szeredi 	return mnt->mnt.mnt_flags & MNT_CURSOR;
8359f6c61f9SMiklos Szeredi }
8369f6c61f9SMiklos Szeredi 
8377af1364fSEric W. Biederman /*
8387af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
8397af1364fSEric W. Biederman  *                         current mount namespace.
8407af1364fSEric W. Biederman  *
8417af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
8427af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
8437af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
8447af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
8457af1364fSEric W. Biederman  * is a mountpoint.
8467af1364fSEric W. Biederman  *
8477af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
8487af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
8497af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
8507af1364fSEric W. Biederman  * parent mount.
8517af1364fSEric W. Biederman  */
8527af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
8537af1364fSEric W. Biederman {
8547af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
8557af1364fSEric W. Biederman 	struct mount *mnt;
8567af1364fSEric W. Biederman 	bool is_covered = false;
8577af1364fSEric W. Biederman 
8587af1364fSEric W. Biederman 	down_read(&namespace_sem);
8599f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
8607af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
8619f6c61f9SMiklos Szeredi 		if (mnt_is_cursor(mnt))
8629f6c61f9SMiklos Szeredi 			continue;
8637af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
8647af1364fSEric W. Biederman 		if (is_covered)
8657af1364fSEric W. Biederman 			break;
8667af1364fSEric W. Biederman 	}
8679f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
8687af1364fSEric W. Biederman 	up_read(&namespace_sem);
8695ad05cc8SNikolay Borisov 
8707af1364fSEric W. Biederman 	return is_covered;
8717af1364fSEric W. Biederman }
8727af1364fSEric W. Biederman 
873e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
87484d17192SAl Viro {
8750818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
87684d17192SAl Viro 	struct mountpoint *mp;
87784d17192SAl Viro 
8780818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
87984d17192SAl Viro 		if (mp->m_dentry == dentry) {
88084d17192SAl Viro 			mp->m_count++;
88184d17192SAl Viro 			return mp;
88284d17192SAl Viro 		}
88384d17192SAl Viro 	}
884e2dfa935SEric W. Biederman 	return NULL;
885e2dfa935SEric W. Biederman }
886e2dfa935SEric W. Biederman 
8873895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry)
888e2dfa935SEric W. Biederman {
8893895dbf8SEric W. Biederman 	struct mountpoint *mp, *new = NULL;
890e2dfa935SEric W. Biederman 	int ret;
89184d17192SAl Viro 
8923895dbf8SEric W. Biederman 	if (d_mountpoint(dentry)) {
8931e9c75fbSBenjamin Coddington 		/* might be worth a WARN_ON() */
8941e9c75fbSBenjamin Coddington 		if (d_unlinked(dentry))
8951e9c75fbSBenjamin Coddington 			return ERR_PTR(-ENOENT);
8963895dbf8SEric W. Biederman mountpoint:
8973895dbf8SEric W. Biederman 		read_seqlock_excl(&mount_lock);
8983895dbf8SEric W. Biederman 		mp = lookup_mountpoint(dentry);
8993895dbf8SEric W. Biederman 		read_sequnlock_excl(&mount_lock);
9003895dbf8SEric W. Biederman 		if (mp)
9013895dbf8SEric W. Biederman 			goto done;
90284d17192SAl Viro 	}
903eed81007SMiklos Szeredi 
9043895dbf8SEric W. Biederman 	if (!new)
9053895dbf8SEric W. Biederman 		new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
9063895dbf8SEric W. Biederman 	if (!new)
9073895dbf8SEric W. Biederman 		return ERR_PTR(-ENOMEM);
9083895dbf8SEric W. Biederman 
9093895dbf8SEric W. Biederman 
9103895dbf8SEric W. Biederman 	/* Exactly one processes may set d_mounted */
9113895dbf8SEric W. Biederman 	ret = d_set_mounted(dentry);
9123895dbf8SEric W. Biederman 
9133895dbf8SEric W. Biederman 	/* Someone else set d_mounted? */
9143895dbf8SEric W. Biederman 	if (ret == -EBUSY)
9153895dbf8SEric W. Biederman 		goto mountpoint;
9163895dbf8SEric W. Biederman 
9173895dbf8SEric W. Biederman 	/* The dentry is not available as a mountpoint? */
9183895dbf8SEric W. Biederman 	mp = ERR_PTR(ret);
9193895dbf8SEric W. Biederman 	if (ret)
9203895dbf8SEric W. Biederman 		goto done;
9213895dbf8SEric W. Biederman 
9223895dbf8SEric W. Biederman 	/* Add the new mountpoint to the hash table */
9233895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
9244edbe133SAl Viro 	new->m_dentry = dget(dentry);
9253895dbf8SEric W. Biederman 	new->m_count = 1;
9263895dbf8SEric W. Biederman 	hlist_add_head(&new->m_hash, mp_hash(dentry));
9273895dbf8SEric W. Biederman 	INIT_HLIST_HEAD(&new->m_list);
9283895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
9293895dbf8SEric W. Biederman 
9303895dbf8SEric W. Biederman 	mp = new;
9313895dbf8SEric W. Biederman 	new = NULL;
9323895dbf8SEric W. Biederman done:
9333895dbf8SEric W. Biederman 	kfree(new);
93484d17192SAl Viro 	return mp;
93584d17192SAl Viro }
93684d17192SAl Viro 
9374edbe133SAl Viro /*
9384edbe133SAl Viro  * vfsmount lock must be held.  Additionally, the caller is responsible
9394edbe133SAl Viro  * for serializing calls for given disposal list.
9404edbe133SAl Viro  */
9414edbe133SAl Viro static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
94284d17192SAl Viro {
94384d17192SAl Viro 	if (!--mp->m_count) {
94484d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
9450a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
94684d17192SAl Viro 		spin_lock(&dentry->d_lock);
94784d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
94884d17192SAl Viro 		spin_unlock(&dentry->d_lock);
9494edbe133SAl Viro 		dput_to_list(dentry, list);
9500818bf27SAl Viro 		hlist_del(&mp->m_hash);
95184d17192SAl Viro 		kfree(mp);
95284d17192SAl Viro 	}
95384d17192SAl Viro }
95484d17192SAl Viro 
9554edbe133SAl Viro /* called with namespace_lock and vfsmount lock */
9564edbe133SAl Viro static void put_mountpoint(struct mountpoint *mp)
9574edbe133SAl Viro {
9584edbe133SAl Viro 	__put_mountpoint(mp, &ex_mountpoints);
9594edbe133SAl Viro }
9604edbe133SAl Viro 
961143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
9621da177e4SLinus Torvalds {
9636b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
96699b7db7bSNick Piggin /*
96799b7db7bSNick Piggin  * vfsmount lock must be held for write
96899b7db7bSNick Piggin  */
9696b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
9705addc5ddSAl Viro {
9715addc5ddSAl Viro 	if (ns) {
9725addc5ddSAl Viro 		ns->event = ++event;
9735addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
9745addc5ddSAl Viro 	}
9755addc5ddSAl Viro }
9765addc5ddSAl Viro 
97799b7db7bSNick Piggin /*
97899b7db7bSNick Piggin  * vfsmount lock must be held for write
97999b7db7bSNick Piggin  */
9806b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
9815addc5ddSAl Viro {
9825addc5ddSAl Viro 	if (ns && ns->event != event) {
9835addc5ddSAl Viro 		ns->event = event;
9845addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
9855addc5ddSAl Viro 	}
9865addc5ddSAl Viro }
9875addc5ddSAl Viro 
98899b7db7bSNick Piggin /*
98999b7db7bSNick Piggin  * vfsmount lock must be held for write
99099b7db7bSNick Piggin  */
991e4e59906SAl Viro static struct mountpoint *unhash_mnt(struct mount *mnt)
9921da177e4SLinus Torvalds {
993e4e59906SAl Viro 	struct mountpoint *mp;
9940714a533SAl Viro 	mnt->mnt_parent = mnt;
995a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
9966b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
99738129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
9980a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
999e4e59906SAl Viro 	mp = mnt->mnt_mp;
100084d17192SAl Viro 	mnt->mnt_mp = NULL;
1001e4e59906SAl Viro 	return mp;
10027bdb11deSEric W. Biederman }
10037bdb11deSEric W. Biederman 
10047bdb11deSEric W. Biederman /*
10057bdb11deSEric W. Biederman  * vfsmount lock must be held for write
10067bdb11deSEric W. Biederman  */
10076a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt)
10086a46c573SEric W. Biederman {
1009e4e59906SAl Viro 	put_mountpoint(unhash_mnt(mnt));
10106a46c573SEric W. Biederman }
10116a46c573SEric W. Biederman 
10126a46c573SEric W. Biederman /*
10136a46c573SEric W. Biederman  * vfsmount lock must be held for write
10146a46c573SEric W. Biederman  */
101584d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
101684d17192SAl Viro 			struct mountpoint *mp,
101744d964d6SAl Viro 			struct mount *child_mnt)
1018b90fa9aeSRam Pai {
101984d17192SAl Viro 	mp->m_count++;
10203a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
10214edbe133SAl Viro 	child_mnt->mnt_mountpoint = mp->m_dentry;
10223a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
102384d17192SAl Viro 	child_mnt->mnt_mp = mp;
10240a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
1025b90fa9aeSRam Pai }
1026b90fa9aeSRam Pai 
10271064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent)
10281064f874SEric W. Biederman {
10291064f874SEric W. Biederman 	hlist_add_head_rcu(&mnt->mnt_hash,
10301064f874SEric W. Biederman 			   m_hash(&parent->mnt, mnt->mnt_mountpoint));
10311064f874SEric W. Biederman 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
10321064f874SEric W. Biederman }
10331064f874SEric W. Biederman 
103499b7db7bSNick Piggin /*
103599b7db7bSNick Piggin  * vfsmount lock must be held for write
103699b7db7bSNick Piggin  */
103784d17192SAl Viro static void attach_mnt(struct mount *mnt,
103884d17192SAl Viro 			struct mount *parent,
103984d17192SAl Viro 			struct mountpoint *mp)
10401da177e4SLinus Torvalds {
104184d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
10421064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
1043b90fa9aeSRam Pai }
1044b90fa9aeSRam Pai 
10451064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
104612a5b529SAl Viro {
10471064f874SEric W. Biederman 	struct mountpoint *old_mp = mnt->mnt_mp;
10481064f874SEric W. Biederman 	struct mount *old_parent = mnt->mnt_parent;
10491064f874SEric W. Biederman 
10501064f874SEric W. Biederman 	list_del_init(&mnt->mnt_child);
10511064f874SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
10521064f874SEric W. Biederman 	hlist_del_init_rcu(&mnt->mnt_hash);
10531064f874SEric W. Biederman 
10541064f874SEric W. Biederman 	attach_mnt(mnt, parent, mp);
10551064f874SEric W. Biederman 
10561064f874SEric W. Biederman 	put_mountpoint(old_mp);
10571064f874SEric W. Biederman 	mnt_add_count(old_parent, -1);
105812a5b529SAl Viro }
105912a5b529SAl Viro 
1060b90fa9aeSRam Pai /*
106199b7db7bSNick Piggin  * vfsmount lock must be held for write
1062b90fa9aeSRam Pai  */
10631064f874SEric W. Biederman static void commit_tree(struct mount *mnt)
1064b90fa9aeSRam Pai {
10650714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
106683adc753SAl Viro 	struct mount *m;
1067b90fa9aeSRam Pai 	LIST_HEAD(head);
1068143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
1069b90fa9aeSRam Pai 
10700714a533SAl Viro 	BUG_ON(parent == mnt);
1071b90fa9aeSRam Pai 
10721a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
1073f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
1074143c8c91SAl Viro 		m->mnt_ns = n;
1075f03c6599SAl Viro 
1076b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
1077b90fa9aeSRam Pai 
1078d2921684SEric W. Biederman 	n->mounts += n->pending_mounts;
1079d2921684SEric W. Biederman 	n->pending_mounts = 0;
1080d2921684SEric W. Biederman 
10811064f874SEric W. Biederman 	__attach_mnt(mnt, parent);
10826b3286edSKirill Korotaev 	touch_mnt_namespace(n);
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds 
1085909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
10861da177e4SLinus Torvalds {
10876b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
10886b41d536SAl Viro 	if (next == &p->mnt_mounts) {
10891da177e4SLinus Torvalds 		while (1) {
1090909b0a88SAl Viro 			if (p == root)
10911da177e4SLinus Torvalds 				return NULL;
10926b41d536SAl Viro 			next = p->mnt_child.next;
10936b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
10941da177e4SLinus Torvalds 				break;
10950714a533SAl Viro 			p = p->mnt_parent;
10961da177e4SLinus Torvalds 		}
10971da177e4SLinus Torvalds 	}
10986b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
1101315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
11029676f0c6SRam Pai {
11036b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
11046b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
11056b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
11066b41d536SAl Viro 		prev = p->mnt_mounts.prev;
11079676f0c6SRam Pai 	}
11089676f0c6SRam Pai 	return p;
11099676f0c6SRam Pai }
11109676f0c6SRam Pai 
11118f291889SAl Viro /**
11128f291889SAl Viro  * vfs_create_mount - Create a mount for a configured superblock
11138f291889SAl Viro  * @fc: The configuration context with the superblock attached
11148f291889SAl Viro  *
11158f291889SAl Viro  * Create a mount to an already configured superblock.  If necessary, the
11168f291889SAl Viro  * caller should invoke vfs_get_tree() before calling this.
11178f291889SAl Viro  *
11188f291889SAl Viro  * Note that this does not attach the mount to anything.
11198f291889SAl Viro  */
11208f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc)
11219d412a43SAl Viro {
1122b105e270SAl Viro 	struct mount *mnt;
11239d412a43SAl Viro 
11248f291889SAl Viro 	if (!fc->root)
11258f291889SAl Viro 		return ERR_PTR(-EINVAL);
11269d412a43SAl Viro 
11278f291889SAl Viro 	mnt = alloc_vfsmnt(fc->source ?: "none");
11289d412a43SAl Viro 	if (!mnt)
11299d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
11309d412a43SAl Viro 
11318f291889SAl Viro 	if (fc->sb_flags & SB_KERNMOUNT)
1132b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
11339d412a43SAl Viro 
11348f291889SAl Viro 	atomic_inc(&fc->root->d_sb->s_active);
11358f291889SAl Viro 	mnt->mnt.mnt_sb		= fc->root->d_sb;
11368f291889SAl Viro 	mnt->mnt.mnt_root	= dget(fc->root);
1137a73324daSAl Viro 	mnt->mnt_mountpoint	= mnt->mnt.mnt_root;
11380714a533SAl Viro 	mnt->mnt_parent		= mnt;
11398f291889SAl Viro 
1140719ea2fbSAl Viro 	lock_mount_hash();
11418f291889SAl Viro 	list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
1142719ea2fbSAl Viro 	unlock_mount_hash();
1143b105e270SAl Viro 	return &mnt->mnt;
11449d412a43SAl Viro }
11458f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount);
11468f291889SAl Viro 
11478f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc)
11488f291889SAl Viro {
11498f291889SAl Viro 	int err = vfs_get_tree(fc);
11508f291889SAl Viro 	if (!err) {
11518f291889SAl Viro 		up_write(&fc->root->d_sb->s_umount);
11528f291889SAl Viro 		return vfs_create_mount(fc);
11538f291889SAl Viro 	}
11548f291889SAl Viro 	return ERR_PTR(err);
11558f291889SAl Viro }
11568f291889SAl Viro EXPORT_SYMBOL(fc_mount);
11578f291889SAl Viro 
11589bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type,
11599bc61ab1SDavid Howells 				int flags, const char *name,
11609bc61ab1SDavid Howells 				void *data)
11611da177e4SLinus Torvalds {
11629bc61ab1SDavid Howells 	struct fs_context *fc;
11638f291889SAl Viro 	struct vfsmount *mnt;
11649bc61ab1SDavid Howells 	int ret = 0;
11659d412a43SAl Viro 
11669d412a43SAl Viro 	if (!type)
11673e1aeb00SDavid Howells 		return ERR_PTR(-EINVAL);
11689d412a43SAl Viro 
11699bc61ab1SDavid Howells 	fc = fs_context_for_mount(type, flags);
11709bc61ab1SDavid Howells 	if (IS_ERR(fc))
11719bc61ab1SDavid Howells 		return ERR_CAST(fc);
11729bc61ab1SDavid Howells 
11733e1aeb00SDavid Howells 	if (name)
11743e1aeb00SDavid Howells 		ret = vfs_parse_fs_string(fc, "source",
11753e1aeb00SDavid Howells 					  name, strlen(name));
11769bc61ab1SDavid Howells 	if (!ret)
11779bc61ab1SDavid Howells 		ret = parse_monolithic_mount_data(fc, data);
11789bc61ab1SDavid Howells 	if (!ret)
11798f291889SAl Viro 		mnt = fc_mount(fc);
11808f291889SAl Viro 	else
11818f291889SAl Viro 		mnt = ERR_PTR(ret);
11829d412a43SAl Viro 
11839bc61ab1SDavid Howells 	put_fs_context(fc);
11848f291889SAl Viro 	return mnt;
11859d412a43SAl Viro }
11869d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
11879d412a43SAl Viro 
118893faccbbSEric W. Biederman struct vfsmount *
118993faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
119093faccbbSEric W. Biederman 	     const char *name, void *data)
119193faccbbSEric W. Biederman {
119293faccbbSEric W. Biederman 	/* Until it is worked out how to pass the user namespace
119393faccbbSEric W. Biederman 	 * through from the parent mount to the submount don't support
119493faccbbSEric W. Biederman 	 * unprivileged mounts with submounts.
119593faccbbSEric W. Biederman 	 */
119693faccbbSEric W. Biederman 	if (mountpoint->d_sb->s_user_ns != &init_user_ns)
119793faccbbSEric W. Biederman 		return ERR_PTR(-EPERM);
119893faccbbSEric W. Biederman 
1199e462ec50SDavid Howells 	return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
120093faccbbSEric W. Biederman }
120193faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount);
120293faccbbSEric W. Biederman 
120387129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
120436341f64SRam Pai 					int flag)
12051da177e4SLinus Torvalds {
120687129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
1207be34d1a3SDavid Howells 	struct mount *mnt;
1208be34d1a3SDavid Howells 	int err;
12091da177e4SLinus Torvalds 
1210be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
1211be34d1a3SDavid Howells 	if (!mnt)
1212be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
1213be34d1a3SDavid Howells 
12147a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
121515169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
1216719f5d7fSMiklos Szeredi 	else
121715169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
1218719f5d7fSMiklos Szeredi 
121915169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1220be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
1221719f5d7fSMiklos Szeredi 		if (err)
1222719f5d7fSMiklos Szeredi 			goto out_free;
1223719f5d7fSMiklos Szeredi 	}
1224719f5d7fSMiklos Szeredi 
122516a34adbSAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags;
122616a34adbSAl Viro 	mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
12275ff9d8a6SEric W. Biederman 
12281da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1229256c8aedSChristian Brauner 	mnt->mnt.mnt_idmap = mnt_idmap_get(mnt_idmap(&old->mnt));
1230256c8aedSChristian Brauner 
1231b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1232b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1233a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
12340714a533SAl Viro 	mnt->mnt_parent = mnt;
1235719ea2fbSAl Viro 	lock_mount_hash();
123639f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1237719ea2fbSAl Viro 	unlock_mount_hash();
1238b90fa9aeSRam Pai 
12397a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
12407a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
12416776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
124232301920SAl Viro 		mnt->mnt_master = old;
1243fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
12448aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1245fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
12466776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1247d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
12486776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1249d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
12505235d448SAl Viro 	} else {
12515235d448SAl Viro 		CLEAR_MNT_SHARED(mnt);
12525afe0022SRam Pai 	}
1253b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
12540f0afb1dSAl Viro 		set_mnt_shared(mnt);
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
12571da177e4SLinus Torvalds 	 * as the original if that was on one */
125836341f64SRam Pai 	if (flag & CL_EXPIRE) {
12596776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
12606776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
12611da177e4SLinus Torvalds 	}
1262be34d1a3SDavid Howells 
1263cb338d06SAl Viro 	return mnt;
1264719f5d7fSMiklos Szeredi 
1265719f5d7fSMiklos Szeredi  out_free:
12668ffcb32eSDavid Howells 	mnt_free_id(mnt);
1267719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1268be34d1a3SDavid Howells 	return ERR_PTR(err);
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12719ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
12729ea459e1SAl Viro {
127356cbb429SAl Viro 	struct hlist_node *p;
127456cbb429SAl Viro 	struct mount *m;
12759ea459e1SAl Viro 	/*
127656cbb429SAl Viro 	 * The warning here probably indicates that somebody messed
127756cbb429SAl Viro 	 * up a mnt_want/drop_write() pair.  If this happens, the
127856cbb429SAl Viro 	 * filesystem was probably unable to make r/w->r/o transitions.
12799ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
12809ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
12819ea459e1SAl Viro 	 */
12829ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
12839ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
12849ea459e1SAl Viro 		mnt_pin_kill(mnt);
128556cbb429SAl Viro 	hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
128656cbb429SAl Viro 		hlist_del(&m->mnt_umount);
128756cbb429SAl Viro 		mntput(&m->mnt);
128856cbb429SAl Viro 	}
12899ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
12909ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
12919ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
12929ea459e1SAl Viro 	mnt_free_id(mnt);
12939ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
12949ea459e1SAl Viro }
12959ea459e1SAl Viro 
12969ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
12979ea459e1SAl Viro {
12989ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
12999ea459e1SAl Viro }
13009ea459e1SAl Viro 
13019ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
13029ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
13039ea459e1SAl Viro {
13049ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
130529785735SByungchul Park 	struct mount *m, *t;
13069ea459e1SAl Viro 
130729785735SByungchul Park 	llist_for_each_entry_safe(m, t, node, mnt_llist)
130829785735SByungchul Park 		cleanup_mnt(m);
13099ea459e1SAl Viro }
13109ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
13119ea459e1SAl Viro 
1312900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
13137b7b1aceSAl Viro {
13144edbe133SAl Viro 	LIST_HEAD(list);
1315edf7ddbfSEric Biggers 	int count;
13164edbe133SAl Viro 
131748a066e7SAl Viro 	rcu_read_lock();
13189ea0a46cSAl Viro 	if (likely(READ_ONCE(mnt->mnt_ns))) {
13199ea0a46cSAl Viro 		/*
13209ea0a46cSAl Viro 		 * Since we don't do lock_mount_hash() here,
13219ea0a46cSAl Viro 		 * ->mnt_ns can change under us.  However, if it's
13229ea0a46cSAl Viro 		 * non-NULL, then there's a reference that won't
13239ea0a46cSAl Viro 		 * be dropped until after an RCU delay done after
13249ea0a46cSAl Viro 		 * turning ->mnt_ns NULL.  So if we observe it
13259ea0a46cSAl Viro 		 * non-NULL under rcu_read_lock(), the reference
13269ea0a46cSAl Viro 		 * we are dropping is not the final one.
13279ea0a46cSAl Viro 		 */
1328aa9c0e07SAl Viro 		mnt_add_count(mnt, -1);
132948a066e7SAl Viro 		rcu_read_unlock();
133099b7db7bSNick Piggin 		return;
1331b3e19d92SNick Piggin 	}
1332719ea2fbSAl Viro 	lock_mount_hash();
1333119e1ef8SAl Viro 	/*
1334119e1ef8SAl Viro 	 * make sure that if __legitimize_mnt() has not seen us grab
1335119e1ef8SAl Viro 	 * mount_lock, we'll see their refcount increment here.
1336119e1ef8SAl Viro 	 */
1337119e1ef8SAl Viro 	smp_mb();
13389ea0a46cSAl Viro 	mnt_add_count(mnt, -1);
1339edf7ddbfSEric Biggers 	count = mnt_get_count(mnt);
1340edf7ddbfSEric Biggers 	if (count != 0) {
1341edf7ddbfSEric Biggers 		WARN_ON(count < 0);
134248a066e7SAl Viro 		rcu_read_unlock();
1343719ea2fbSAl Viro 		unlock_mount_hash();
134499b7db7bSNick Piggin 		return;
134599b7db7bSNick Piggin 	}
134648a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
134748a066e7SAl Viro 		rcu_read_unlock();
134848a066e7SAl Viro 		unlock_mount_hash();
134948a066e7SAl Viro 		return;
135048a066e7SAl Viro 	}
135148a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
135248a066e7SAl Viro 	rcu_read_unlock();
1353962830dfSAndi Kleen 
135439f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1355ce07d891SEric W. Biederman 
1356ce07d891SEric W. Biederman 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1357ce07d891SEric W. Biederman 		struct mount *p, *tmp;
1358ce07d891SEric W. Biederman 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
13594edbe133SAl Viro 			__put_mountpoint(unhash_mnt(p), &list);
136056cbb429SAl Viro 			hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
1361ce07d891SEric W. Biederman 		}
1362ce07d891SEric W. Biederman 	}
1363719ea2fbSAl Viro 	unlock_mount_hash();
13644edbe133SAl Viro 	shrink_dentry_list(&list);
1365649a795aSAl Viro 
13669ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
13679ea459e1SAl Viro 		struct task_struct *task = current;
13689ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
13699ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
137091989c70SJens Axboe 			if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
13719ea459e1SAl Viro 				return;
13729ea459e1SAl Viro 		}
13739ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
13749ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
13759ea459e1SAl Viro 		return;
13769ea459e1SAl Viro 	}
13779ea459e1SAl Viro 	cleanup_mnt(mnt);
1378b3e19d92SNick Piggin }
1379b3e19d92SNick Piggin 
1380b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1381b3e19d92SNick Piggin {
1382b3e19d92SNick Piggin 	if (mnt) {
1383863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1384b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1385863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1386863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1387863d684fSAl Viro 		mntput_no_expire(m);
1388b3e19d92SNick Piggin 	}
1389b3e19d92SNick Piggin }
1390b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1391b3e19d92SNick Piggin 
1392b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1393b3e19d92SNick Piggin {
1394b3e19d92SNick Piggin 	if (mnt)
139583adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1396b3e19d92SNick Piggin 	return mnt;
1397b3e19d92SNick Piggin }
1398b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1399b3e19d92SNick Piggin 
1400*da27f796SRik van Riel /*
1401*da27f796SRik van Riel  * Make a mount point inaccessible to new lookups.
1402*da27f796SRik van Riel  * Because there may still be current users, the caller MUST WAIT
1403*da27f796SRik van Riel  * for an RCU grace period before destroying the mount point.
1404*da27f796SRik van Riel  */
1405*da27f796SRik van Riel void mnt_make_shortterm(struct vfsmount *mnt)
1406*da27f796SRik van Riel {
1407*da27f796SRik van Riel 	if (mnt)
1408*da27f796SRik van Riel 		real_mount(mnt)->mnt_ns = NULL;
1409*da27f796SRik van Riel }
1410*da27f796SRik van Riel 
14111f287bc4SRandy Dunlap /**
14121f287bc4SRandy Dunlap  * path_is_mountpoint() - Check if path is a mount in the current namespace.
14131f287bc4SRandy Dunlap  * @path: path to check
1414c6609c0aSIan Kent  *
1415c6609c0aSIan Kent  *  d_mountpoint() can only be used reliably to establish if a dentry is
1416c6609c0aSIan Kent  *  not mounted in any namespace and that common case is handled inline.
1417c6609c0aSIan Kent  *  d_mountpoint() isn't aware of the possibility there may be multiple
1418c6609c0aSIan Kent  *  mounts using a given dentry in a different namespace. This function
1419c6609c0aSIan Kent  *  checks if the passed in path is a mountpoint rather than the dentry
1420c6609c0aSIan Kent  *  alone.
1421c6609c0aSIan Kent  */
1422c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path)
1423c6609c0aSIan Kent {
1424c6609c0aSIan Kent 	unsigned seq;
1425c6609c0aSIan Kent 	bool res;
1426c6609c0aSIan Kent 
1427c6609c0aSIan Kent 	if (!d_mountpoint(path->dentry))
1428c6609c0aSIan Kent 		return false;
1429c6609c0aSIan Kent 
1430c6609c0aSIan Kent 	rcu_read_lock();
1431c6609c0aSIan Kent 	do {
1432c6609c0aSIan Kent 		seq = read_seqbegin(&mount_lock);
1433c6609c0aSIan Kent 		res = __path_is_mountpoint(path);
1434c6609c0aSIan Kent 	} while (read_seqretry(&mount_lock, seq));
1435c6609c0aSIan Kent 	rcu_read_unlock();
1436c6609c0aSIan Kent 
1437c6609c0aSIan Kent 	return res;
1438c6609c0aSIan Kent }
1439c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint);
1440c6609c0aSIan Kent 
1441ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path)
14427b7b1aceSAl Viro {
14433064c356SAl Viro 	struct mount *p;
14443064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
14453064c356SAl Viro 	if (IS_ERR(p))
14463064c356SAl Viro 		return ERR_CAST(p);
14473064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
14483064c356SAl Viro 	return &p->mnt;
14497b7b1aceSAl Viro }
14501da177e4SLinus Torvalds 
1451a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
14529f6c61f9SMiklos Szeredi static struct mount *mnt_list_next(struct mnt_namespace *ns,
14539f6c61f9SMiklos Szeredi 				   struct list_head *p)
14549f6c61f9SMiklos Szeredi {
14559f6c61f9SMiklos Szeredi 	struct mount *mnt, *ret = NULL;
14569f6c61f9SMiklos Szeredi 
14579f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
14589f6c61f9SMiklos Szeredi 	list_for_each_continue(p, &ns->list) {
14599f6c61f9SMiklos Szeredi 		mnt = list_entry(p, typeof(*mnt), mnt_list);
14609f6c61f9SMiklos Szeredi 		if (!mnt_is_cursor(mnt)) {
14619f6c61f9SMiklos Szeredi 			ret = mnt;
14629f6c61f9SMiklos Szeredi 			break;
14639f6c61f9SMiklos Szeredi 		}
14649f6c61f9SMiklos Szeredi 	}
14659f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
14669f6c61f9SMiklos Szeredi 
14679f6c61f9SMiklos Szeredi 	return ret;
14689f6c61f9SMiklos Szeredi }
14699f6c61f9SMiklos Szeredi 
14700226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
14711da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
14721da177e4SLinus Torvalds {
1473ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
14749f6c61f9SMiklos Szeredi 	struct list_head *prev;
14751da177e4SLinus Torvalds 
1476390c6843SRam Pai 	down_read(&namespace_sem);
14779f6c61f9SMiklos Szeredi 	if (!*pos) {
14789f6c61f9SMiklos Szeredi 		prev = &p->ns->list;
14799f6c61f9SMiklos Szeredi 	} else {
14809f6c61f9SMiklos Szeredi 		prev = &p->cursor.mnt_list;
14819f6c61f9SMiklos Szeredi 
14829f6c61f9SMiklos Szeredi 		/* Read after we'd reached the end? */
14839f6c61f9SMiklos Szeredi 		if (list_empty(prev))
14849f6c61f9SMiklos Szeredi 			return NULL;
1485c7999c36SAl Viro 	}
1486c7999c36SAl Viro 
14879f6c61f9SMiklos Szeredi 	return mnt_list_next(p->ns, prev);
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
14911da177e4SLinus Torvalds {
1492ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
14939f6c61f9SMiklos Szeredi 	struct mount *mnt = v;
1494b0765fb8SPavel Emelianov 
14959f6c61f9SMiklos Szeredi 	++*pos;
14969f6c61f9SMiklos Szeredi 	return mnt_list_next(p->ns, &mnt->mnt_list);
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
15001da177e4SLinus Torvalds {
15019f6c61f9SMiklos Szeredi 	struct proc_mounts *p = m->private;
15029f6c61f9SMiklos Szeredi 	struct mount *mnt = v;
15039f6c61f9SMiklos Szeredi 
15049f6c61f9SMiklos Szeredi 	lock_ns_list(p->ns);
15059f6c61f9SMiklos Szeredi 	if (mnt)
15069f6c61f9SMiklos Szeredi 		list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list);
15079f6c61f9SMiklos Szeredi 	else
15089f6c61f9SMiklos Szeredi 		list_del_init(&p->cursor.mnt_list);
15099f6c61f9SMiklos Szeredi 	unlock_ns_list(p->ns);
1510390c6843SRam Pai 	up_read(&namespace_sem);
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
15130226f492SAl Viro static int m_show(struct seq_file *m, void *v)
15149f5596afSAl Viro {
1515ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
15169f6c61f9SMiklos Szeredi 	struct mount *r = v;
15170226f492SAl Viro 	return p->show(m, &r->mnt);
15181da177e4SLinus Torvalds }
15191da177e4SLinus Torvalds 
1520a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
15211da177e4SLinus Torvalds 	.start	= m_start,
15221da177e4SLinus Torvalds 	.next	= m_next,
15231da177e4SLinus Torvalds 	.stop	= m_stop,
15240226f492SAl Viro 	.show	= m_show,
1525b4629fe2SChuck Lever };
15269f6c61f9SMiklos Szeredi 
15279f6c61f9SMiklos Szeredi void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor)
15289f6c61f9SMiklos Szeredi {
15299f6c61f9SMiklos Szeredi 	down_read(&namespace_sem);
15309f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
15319f6c61f9SMiklos Szeredi 	list_del(&cursor->mnt_list);
15329f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
15339f6c61f9SMiklos Szeredi 	up_read(&namespace_sem);
15349f6c61f9SMiklos Szeredi }
1535a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1536b4629fe2SChuck Lever 
15371da177e4SLinus Torvalds /**
15381da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
15391f287bc4SRandy Dunlap  * @m: root of mount tree
15401da177e4SLinus Torvalds  *
15411da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
15421da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
15431da177e4SLinus Torvalds  * busy.
15441da177e4SLinus Torvalds  */
1545909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
15461da177e4SLinus Torvalds {
1547909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
154836341f64SRam Pai 	int actual_refs = 0;
154936341f64SRam Pai 	int minimum_refs = 0;
1550315fc83eSAl Viro 	struct mount *p;
1551909b0a88SAl Viro 	BUG_ON(!m);
15521da177e4SLinus Torvalds 
1553b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1554719ea2fbSAl Viro 	lock_mount_hash();
1555909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
155683adc753SAl Viro 		actual_refs += mnt_get_count(p);
15571da177e4SLinus Torvalds 		minimum_refs += 2;
15581da177e4SLinus Torvalds 	}
1559719ea2fbSAl Viro 	unlock_mount_hash();
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
15621da177e4SLinus Torvalds 		return 0;
1563e3474a8eSIan Kent 
1564e3474a8eSIan Kent 	return 1;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds /**
15701da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
15711da177e4SLinus Torvalds  * @mnt: root of mount
15721da177e4SLinus Torvalds  *
15731da177e4SLinus Torvalds  * This is called to check if a mount point has any
15741da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
15751da177e4SLinus Torvalds  * mount has sub mounts this will return busy
15761da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
15771da177e4SLinus Torvalds  *
15781da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
15791da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
15801da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
15811da177e4SLinus Torvalds  */
15821da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
15831da177e4SLinus Torvalds {
1584e3474a8eSIan Kent 	int ret = 1;
15858ad08d8aSAl Viro 	down_read(&namespace_sem);
1586719ea2fbSAl Viro 	lock_mount_hash();
15871ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1588e3474a8eSIan Kent 		ret = 0;
1589719ea2fbSAl Viro 	unlock_mount_hash();
15908ad08d8aSAl Viro 	up_read(&namespace_sem);
1591a05964f3SRam Pai 	return ret;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
15951da177e4SLinus Torvalds 
159697216be0SAl Viro static void namespace_unlock(void)
15971da177e4SLinus Torvalds {
1598a3b3c562SEric W. Biederman 	struct hlist_head head;
159956cbb429SAl Viro 	struct hlist_node *p;
160056cbb429SAl Viro 	struct mount *m;
16014edbe133SAl Viro 	LIST_HEAD(list);
160297216be0SAl Viro 
1603a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
16044edbe133SAl Viro 	list_splice_init(&ex_mountpoints, &list);
1605a3b3c562SEric W. Biederman 
160697216be0SAl Viro 	up_write(&namespace_sem);
1607a3b3c562SEric W. Biederman 
16084edbe133SAl Viro 	shrink_dentry_list(&list);
16094edbe133SAl Viro 
1610a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
161197216be0SAl Viro 		return;
161297216be0SAl Viro 
161322cb7405SNeilBrown 	synchronize_rcu_expedited();
161448a066e7SAl Viro 
161556cbb429SAl Viro 	hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
161656cbb429SAl Viro 		hlist_del(&m->mnt_umount);
161756cbb429SAl Viro 		mntput(&m->mnt);
161856cbb429SAl Viro 	}
161970fbcdf4SRam Pai }
162070fbcdf4SRam Pai 
162197216be0SAl Viro static inline void namespace_lock(void)
1622e3197d83SAl Viro {
162397216be0SAl Viro 	down_write(&namespace_sem);
1624e3197d83SAl Viro }
1625e3197d83SAl Viro 
1626e819f152SEric W. Biederman enum umount_tree_flags {
1627e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1628e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1629e0c9c0afSEric W. Biederman 	UMOUNT_CONNECTED = 4,
1630e819f152SEric W. Biederman };
1631f2d0a123SEric W. Biederman 
1632f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1633f2d0a123SEric W. Biederman {
1634f2d0a123SEric W. Biederman 	/* Leaving mounts connected is only valid for lazy umounts */
1635f2d0a123SEric W. Biederman 	if (how & UMOUNT_SYNC)
1636f2d0a123SEric W. Biederman 		return true;
1637f2d0a123SEric W. Biederman 
1638f2d0a123SEric W. Biederman 	/* A mount without a parent has nothing to be connected to */
1639f2d0a123SEric W. Biederman 	if (!mnt_has_parent(mnt))
1640f2d0a123SEric W. Biederman 		return true;
1641f2d0a123SEric W. Biederman 
1642f2d0a123SEric W. Biederman 	/* Because the reference counting rules change when mounts are
1643f2d0a123SEric W. Biederman 	 * unmounted and connected, umounted mounts may not be
1644f2d0a123SEric W. Biederman 	 * connected to mounted mounts.
1645f2d0a123SEric W. Biederman 	 */
1646f2d0a123SEric W. Biederman 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1647f2d0a123SEric W. Biederman 		return true;
1648f2d0a123SEric W. Biederman 
1649f2d0a123SEric W. Biederman 	/* Has it been requested that the mount remain connected? */
1650f2d0a123SEric W. Biederman 	if (how & UMOUNT_CONNECTED)
1651f2d0a123SEric W. Biederman 		return false;
1652f2d0a123SEric W. Biederman 
1653f2d0a123SEric W. Biederman 	/* Is the mount locked such that it needs to remain connected? */
1654f2d0a123SEric W. Biederman 	if (IS_MNT_LOCKED(mnt))
1655f2d0a123SEric W. Biederman 		return false;
1656f2d0a123SEric W. Biederman 
1657f2d0a123SEric W. Biederman 	/* By default disconnect the mount */
1658f2d0a123SEric W. Biederman 	return true;
1659f2d0a123SEric W. Biederman }
1660f2d0a123SEric W. Biederman 
166199b7db7bSNick Piggin /*
166248a066e7SAl Viro  * mount_lock must be held
166399b7db7bSNick Piggin  * namespace_sem must be held for write
166499b7db7bSNick Piggin  */
1665e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
166670fbcdf4SRam Pai {
1667c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1668315fc83eSAl Viro 	struct mount *p;
166970fbcdf4SRam Pai 
16705d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
16715d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
16725d88457eSEric W. Biederman 
1673c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1674590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1675590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1676c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1677590ce4bcSEric W. Biederman 	}
1678c003b26fSEric W. Biederman 
1679411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1680c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1681c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
168238129a13SAl Viro 	}
168370fbcdf4SRam Pai 
1684c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1685e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
16867b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1687a05964f3SRam Pai 
1688c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1689d2921684SEric W. Biederman 		struct mnt_namespace *ns;
1690ce07d891SEric W. Biederman 		bool disconnect;
1691c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
16926776db3dSAl Viro 		list_del_init(&p->mnt_expire);
16931a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1694d2921684SEric W. Biederman 		ns = p->mnt_ns;
1695d2921684SEric W. Biederman 		if (ns) {
1696d2921684SEric W. Biederman 			ns->mounts--;
1697d2921684SEric W. Biederman 			__touch_mnt_namespace(ns);
1698d2921684SEric W. Biederman 		}
169963d37a84SAl Viro 		p->mnt_ns = NULL;
1700e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
170148a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
170287b95ce0SAl Viro 
1703f2d0a123SEric W. Biederman 		disconnect = disconnect_mount(p, how);
1704676da58dSAl Viro 		if (mnt_has_parent(p)) {
170581b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
1706ce07d891SEric W. Biederman 			if (!disconnect) {
1707ce07d891SEric W. Biederman 				/* Don't forget about p */
1708ce07d891SEric W. Biederman 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1709ce07d891SEric W. Biederman 			} else {
17106a46c573SEric W. Biederman 				umount_mnt(p);
17117c4b93d8SAl Viro 			}
1712ce07d891SEric W. Biederman 		}
17130f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
171419a1c409SAl Viro 		if (disconnect)
171519a1c409SAl Viro 			hlist_add_head(&p->mnt_umount, &unmounted);
171638129a13SAl Viro 	}
17171da177e4SLinus Torvalds }
17181da177e4SLinus Torvalds 
1719b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1720c35038beSAl Viro 
17218d0347f6SDavid Howells static int do_umount_root(struct super_block *sb)
17228d0347f6SDavid Howells {
17238d0347f6SDavid Howells 	int ret = 0;
17248d0347f6SDavid Howells 
17258d0347f6SDavid Howells 	down_write(&sb->s_umount);
17268d0347f6SDavid Howells 	if (!sb_rdonly(sb)) {
17278d0347f6SDavid Howells 		struct fs_context *fc;
17288d0347f6SDavid Howells 
17298d0347f6SDavid Howells 		fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
17308d0347f6SDavid Howells 						SB_RDONLY);
17318d0347f6SDavid Howells 		if (IS_ERR(fc)) {
17328d0347f6SDavid Howells 			ret = PTR_ERR(fc);
17338d0347f6SDavid Howells 		} else {
17348d0347f6SDavid Howells 			ret = parse_monolithic_mount_data(fc, NULL);
17358d0347f6SDavid Howells 			if (!ret)
17368d0347f6SDavid Howells 				ret = reconfigure_super(fc);
17378d0347f6SDavid Howells 			put_fs_context(fc);
17388d0347f6SDavid Howells 		}
17398d0347f6SDavid Howells 	}
17408d0347f6SDavid Howells 	up_write(&sb->s_umount);
17418d0347f6SDavid Howells 	return ret;
17428d0347f6SDavid Howells }
17438d0347f6SDavid Howells 
17441ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
17451da177e4SLinus Torvalds {
17461ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
17471da177e4SLinus Torvalds 	int retval;
17481da177e4SLinus Torvalds 
17491ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
17501da177e4SLinus Torvalds 	if (retval)
17511da177e4SLinus Torvalds 		return retval;
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds 	/*
17541da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
17551da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
17561da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
17571da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
17581da177e4SLinus Torvalds 	 */
17591da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
17601ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
17611da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
17621da177e4SLinus Torvalds 			return -EINVAL;
17631da177e4SLinus Torvalds 
1764b3e19d92SNick Piggin 		/*
1765b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1766b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1767b3e19d92SNick Piggin 		 */
1768719ea2fbSAl Viro 		lock_mount_hash();
176983adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1770719ea2fbSAl Viro 			unlock_mount_hash();
17711da177e4SLinus Torvalds 			return -EBUSY;
1772b3e19d92SNick Piggin 		}
1773719ea2fbSAl Viro 		unlock_mount_hash();
17741da177e4SLinus Torvalds 
1775863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
17761da177e4SLinus Torvalds 			return -EAGAIN;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	/*
17801da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
17811da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
17821da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
17831da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
17841da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
17851da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
17861da177e4SLinus Torvalds 	 * about for the moment.
17871da177e4SLinus Torvalds 	 */
17881da177e4SLinus Torvalds 
178942faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
179042faad99SAl Viro 		sb->s_op->umount_begin(sb);
179142faad99SAl Viro 	}
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	/*
17941da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
17951da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
17961da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
17971da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
17981da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
17991da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
18001da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
18011da177e4SLinus Torvalds 	 */
18021ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
18031da177e4SLinus Torvalds 		/*
18041da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
18051da177e4SLinus Torvalds 		 * we just try to remount it readonly.
18061da177e4SLinus Torvalds 		 */
1807bc6155d1SEric W. Biederman 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1808a1480dccSAndy Lutomirski 			return -EPERM;
18098d0347f6SDavid Howells 		return do_umount_root(sb);
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 
181297216be0SAl Viro 	namespace_lock();
1813719ea2fbSAl Viro 	lock_mount_hash();
18141da177e4SLinus Torvalds 
181525d202edSEric W. Biederman 	/* Recheck MNT_LOCKED with the locks held */
181625d202edSEric W. Biederman 	retval = -EINVAL;
181725d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
181825d202edSEric W. Biederman 		goto out;
181925d202edSEric W. Biederman 
182025d202edSEric W. Biederman 	event++;
182148a066e7SAl Viro 	if (flags & MNT_DETACH) {
182248a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1823e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
182448a066e7SAl Viro 		retval = 0;
182548a066e7SAl Viro 	} else {
1826b54b9be7SAl Viro 		shrink_submounts(mnt);
18271da177e4SLinus Torvalds 		retval = -EBUSY;
182848a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
18291a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1830e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
18311da177e4SLinus Torvalds 			retval = 0;
18321da177e4SLinus Torvalds 		}
183348a066e7SAl Viro 	}
183425d202edSEric W. Biederman out:
1835719ea2fbSAl Viro 	unlock_mount_hash();
1836e3197d83SAl Viro 	namespace_unlock();
18371da177e4SLinus Torvalds 	return retval;
18381da177e4SLinus Torvalds }
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds /*
184180b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
184280b5dce8SEric W. Biederman  *
184380b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
184480b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
184580b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
184680b5dce8SEric W. Biederman  * leaking them.
184780b5dce8SEric W. Biederman  *
184880b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
184980b5dce8SEric W. Biederman  */
185080b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
185180b5dce8SEric W. Biederman {
185280b5dce8SEric W. Biederman 	struct mountpoint *mp;
185380b5dce8SEric W. Biederman 	struct mount *mnt;
185480b5dce8SEric W. Biederman 
185580b5dce8SEric W. Biederman 	namespace_lock();
18563895dbf8SEric W. Biederman 	lock_mount_hash();
185780b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
1858adc9b5c0SAl Viro 	if (!mp)
185980b5dce8SEric W. Biederman 		goto out_unlock;
186080b5dce8SEric W. Biederman 
1861e06b933eSAndrey Ulanov 	event++;
186280b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
186380b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1864ce07d891SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1865fe78fcc8SEric W. Biederman 			umount_mnt(mnt);
186656cbb429SAl Viro 			hlist_add_head(&mnt->mnt_umount, &unmounted);
1867ce07d891SEric W. Biederman 		}
1868e0c9c0afSEric W. Biederman 		else umount_tree(mnt, UMOUNT_CONNECTED);
186980b5dce8SEric W. Biederman 	}
187080b5dce8SEric W. Biederman 	put_mountpoint(mp);
187180b5dce8SEric W. Biederman out_unlock:
18723895dbf8SEric W. Biederman 	unlock_mount_hash();
187380b5dce8SEric W. Biederman 	namespace_unlock();
187480b5dce8SEric W. Biederman }
187580b5dce8SEric W. Biederman 
187680b5dce8SEric W. Biederman /*
18779b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
18789b40bc90SAl Viro  */
1879a5f85d78SAl Viro bool may_mount(void)
18809b40bc90SAl Viro {
18819b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
18829b40bc90SAl Viro }
18839b40bc90SAl Viro 
1884f7e33bdbSJeff Layton static void warn_mandlock(void)
18859e8925b6SJeff Layton {
1886f7e33bdbSJeff Layton 	pr_warn_once("=======================================================\n"
1887f7e33bdbSJeff Layton 		     "WARNING: The mand mount option has been deprecated and\n"
1888f7e33bdbSJeff Layton 		     "         and is ignored by this kernel. Remove the mand\n"
1889f7e33bdbSJeff Layton 		     "         option from the mount to silence this warning.\n"
1890f7e33bdbSJeff Layton 		     "=======================================================\n");
18919e8925b6SJeff Layton }
18929e8925b6SJeff Layton 
189325ccd24fSChristoph Hellwig static int can_umount(const struct path *path, int flags)
18941da177e4SLinus Torvalds {
189525ccd24fSChristoph Hellwig 	struct mount *mnt = real_mount(path->mnt);
18961da177e4SLinus Torvalds 
18979b40bc90SAl Viro 	if (!may_mount())
18989b40bc90SAl Viro 		return -EPERM;
189941525f56SChristoph Hellwig 	if (path->dentry != path->mnt->mnt_root)
190025ccd24fSChristoph Hellwig 		return -EINVAL;
1901143c8c91SAl Viro 	if (!check_mnt(mnt))
190225ccd24fSChristoph Hellwig 		return -EINVAL;
190325d202edSEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
190425ccd24fSChristoph Hellwig 		return -EINVAL;
1905b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
190625ccd24fSChristoph Hellwig 		return -EPERM;
190725ccd24fSChristoph Hellwig 	return 0;
190825ccd24fSChristoph Hellwig }
19091da177e4SLinus Torvalds 
1910a0a6df9aSAl Viro // caller is responsible for flags being sane
191125ccd24fSChristoph Hellwig int path_umount(struct path *path, int flags)
191225ccd24fSChristoph Hellwig {
191325ccd24fSChristoph Hellwig 	struct mount *mnt = real_mount(path->mnt);
191425ccd24fSChristoph Hellwig 	int ret;
191525ccd24fSChristoph Hellwig 
191625ccd24fSChristoph Hellwig 	ret = can_umount(path, flags);
191725ccd24fSChristoph Hellwig 	if (!ret)
191825ccd24fSChristoph Hellwig 		ret = do_umount(mnt, flags);
191925ccd24fSChristoph Hellwig 
1920429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
192141525f56SChristoph Hellwig 	dput(path->dentry);
1922900148dcSAl Viro 	mntput_no_expire(mnt);
192325ccd24fSChristoph Hellwig 	return ret;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds 
192609267defSChristoph Hellwig static int ksys_umount(char __user *name, int flags)
192741525f56SChristoph Hellwig {
192841525f56SChristoph Hellwig 	int lookup_flags = LOOKUP_MOUNTPOINT;
192941525f56SChristoph Hellwig 	struct path path;
193041525f56SChristoph Hellwig 	int ret;
193141525f56SChristoph Hellwig 
1932a0a6df9aSAl Viro 	// basic validity checks done first
1933a0a6df9aSAl Viro 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1934a0a6df9aSAl Viro 		return -EINVAL;
1935a0a6df9aSAl Viro 
193641525f56SChristoph Hellwig 	if (!(flags & UMOUNT_NOFOLLOW))
193741525f56SChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
193841525f56SChristoph Hellwig 	ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
193941525f56SChristoph Hellwig 	if (ret)
194041525f56SChristoph Hellwig 		return ret;
194141525f56SChristoph Hellwig 	return path_umount(&path, flags);
194241525f56SChristoph Hellwig }
194341525f56SChristoph Hellwig 
19443a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
19453a18ef5cSDominik Brodowski {
19463a18ef5cSDominik Brodowski 	return ksys_umount(name, flags);
19473a18ef5cSDominik Brodowski }
19483a18ef5cSDominik Brodowski 
19491da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds /*
19521da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
19531da177e4SLinus Torvalds  */
1954bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
19551da177e4SLinus Torvalds {
19563a18ef5cSDominik Brodowski 	return ksys_umount(name, 0);
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds #endif
19601da177e4SLinus Torvalds 
19614ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
19628823c079SEric W. Biederman {
19634ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1964e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1965e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
19664ce5d2b1SEric W. Biederman }
19674ce5d2b1SEric W. Biederman 
1968213921f9SEric Biggers static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
196958be2825SAl Viro {
197058be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
197158be2825SAl Viro }
197258be2825SAl Viro 
1973303cc571SChristian Brauner struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
1974303cc571SChristian Brauner {
1975303cc571SChristian Brauner 	return &mnt->ns;
1976303cc571SChristian Brauner }
1977303cc571SChristian Brauner 
19784ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
19794ce5d2b1SEric W. Biederman {
19804ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
19814ce5d2b1SEric W. Biederman 	 * mount namespace loop?
19824ce5d2b1SEric W. Biederman 	 */
19834ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
19844ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
19854ce5d2b1SEric W. Biederman 		return false;
19864ce5d2b1SEric W. Biederman 
1987f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
19888823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
19898823c079SEric W. Biederman }
19908823c079SEric W. Biederman 
199187129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
199236341f64SRam Pai 					int flag)
19931da177e4SLinus Torvalds {
199484d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
19951da177e4SLinus Torvalds 
19964ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
19974ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
19984ce5d2b1SEric W. Biederman 
19994ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
2000be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
20019676f0c6SRam Pai 
200236341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
2003be34d1a3SDavid Howells 	if (IS_ERR(q))
2004be34d1a3SDavid Howells 		return q;
2005be34d1a3SDavid Howells 
2006a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds 	p = mnt;
20096b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
2010315fc83eSAl Viro 		struct mount *s;
20117ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
20121da177e4SLinus Torvalds 			continue;
20131da177e4SLinus Torvalds 
2014909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
20154ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
20164ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
2017df7342b2SEric W. Biederman 				if (s->mnt.mnt_flags & MNT_LOCKED) {
2018df7342b2SEric W. Biederman 					/* Both unbindable and locked. */
2019df7342b2SEric W. Biederman 					q = ERR_PTR(-EPERM);
2020df7342b2SEric W. Biederman 					goto out;
2021df7342b2SEric W. Biederman 				} else {
20224ce5d2b1SEric W. Biederman 					s = skip_mnt_tree(s);
20234ce5d2b1SEric W. Biederman 					continue;
20244ce5d2b1SEric W. Biederman 				}
2025df7342b2SEric W. Biederman 			}
20264ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
20274ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
20289676f0c6SRam Pai 				s = skip_mnt_tree(s);
20299676f0c6SRam Pai 				continue;
20309676f0c6SRam Pai 			}
20310714a533SAl Viro 			while (p != s->mnt_parent) {
20320714a533SAl Viro 				p = p->mnt_parent;
20330714a533SAl Viro 				q = q->mnt_parent;
20341da177e4SLinus Torvalds 			}
203587129cc0SAl Viro 			p = s;
203684d17192SAl Viro 			parent = q;
203787129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
2038be34d1a3SDavid Howells 			if (IS_ERR(q))
2039be34d1a3SDavid Howells 				goto out;
2040719ea2fbSAl Viro 			lock_mount_hash();
20411a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
20421064f874SEric W. Biederman 			attach_mnt(q, parent, p->mnt_mp);
2043719ea2fbSAl Viro 			unlock_mount_hash();
20441da177e4SLinus Torvalds 		}
20451da177e4SLinus Torvalds 	}
20461da177e4SLinus Torvalds 	return res;
2047be34d1a3SDavid Howells out:
20481da177e4SLinus Torvalds 	if (res) {
2049719ea2fbSAl Viro 		lock_mount_hash();
2050e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
2051719ea2fbSAl Viro 		unlock_mount_hash();
20521da177e4SLinus Torvalds 	}
2053be34d1a3SDavid Howells 	return q;
20541da177e4SLinus Torvalds }
20551da177e4SLinus Torvalds 
2056be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
2057be34d1a3SDavid Howells 
2058ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path)
20598aec0809SAl Viro {
2060cb338d06SAl Viro 	struct mount *tree;
206197216be0SAl Viro 	namespace_lock();
2062cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
2063cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
2064cd4a4017SEric W. Biederman 	else
206587129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
206687129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
2067328e6d90SAl Viro 	namespace_unlock();
2068be34d1a3SDavid Howells 	if (IS_ERR(tree))
206952e220d3SDan Carpenter 		return ERR_CAST(tree);
2070be34d1a3SDavid Howells 	return &tree->mnt;
20718aec0809SAl Viro }
20728aec0809SAl Viro 
2073a07b2000SAl Viro static void free_mnt_ns(struct mnt_namespace *);
2074a07b2000SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
2075a07b2000SAl Viro 
2076a07b2000SAl Viro void dissolve_on_fput(struct vfsmount *mnt)
2077a07b2000SAl Viro {
2078a07b2000SAl Viro 	struct mnt_namespace *ns;
2079a07b2000SAl Viro 	namespace_lock();
2080a07b2000SAl Viro 	lock_mount_hash();
2081a07b2000SAl Viro 	ns = real_mount(mnt)->mnt_ns;
208244dfd84aSDavid Howells 	if (ns) {
208344dfd84aSDavid Howells 		if (is_anon_ns(ns))
2084a07b2000SAl Viro 			umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
208544dfd84aSDavid Howells 		else
208644dfd84aSDavid Howells 			ns = NULL;
208744dfd84aSDavid Howells 	}
2088a07b2000SAl Viro 	unlock_mount_hash();
2089a07b2000SAl Viro 	namespace_unlock();
209044dfd84aSDavid Howells 	if (ns)
2091a07b2000SAl Viro 		free_mnt_ns(ns);
2092a07b2000SAl Viro }
2093a07b2000SAl Viro 
20948aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
20958aec0809SAl Viro {
209697216be0SAl Viro 	namespace_lock();
2097719ea2fbSAl Viro 	lock_mount_hash();
20989c8e0a1bSEric W. Biederman 	umount_tree(real_mount(mnt), 0);
2099719ea2fbSAl Viro 	unlock_mount_hash();
21003ab6abeeSAl Viro 	namespace_unlock();
21018aec0809SAl Viro }
21028aec0809SAl Viro 
2103427215d8SMiklos Szeredi static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2104427215d8SMiklos Szeredi {
2105427215d8SMiklos Szeredi 	struct mount *child;
2106427215d8SMiklos Szeredi 
2107427215d8SMiklos Szeredi 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2108427215d8SMiklos Szeredi 		if (!is_subdir(child->mnt_mountpoint, dentry))
2109427215d8SMiklos Szeredi 			continue;
2110427215d8SMiklos Szeredi 
2111427215d8SMiklos Szeredi 		if (child->mnt.mnt_flags & MNT_LOCKED)
2112427215d8SMiklos Szeredi 			return true;
2113427215d8SMiklos Szeredi 	}
2114427215d8SMiklos Szeredi 	return false;
2115427215d8SMiklos Szeredi }
2116427215d8SMiklos Szeredi 
2117c771d683SMiklos Szeredi /**
2118c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
21191f287bc4SRandy Dunlap  * @path: path to clone
2120c771d683SMiklos Szeredi  *
21211f287bc4SRandy Dunlap  * This creates a new vfsmount, which will be the clone of @path.  The new mount
21221f287bc4SRandy Dunlap  * will not be attached anywhere in the namespace and will be private (i.e.
21231f287bc4SRandy Dunlap  * changes to the originating mount won't be propagated into this).
2124c771d683SMiklos Szeredi  *
2125c771d683SMiklos Szeredi  * Release with mntput().
2126c771d683SMiklos Szeredi  */
2127ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path)
2128c771d683SMiklos Szeredi {
2129c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
2130c771d683SMiklos Szeredi 	struct mount *new_mnt;
2131c771d683SMiklos Szeredi 
2132427215d8SMiklos Szeredi 	down_read(&namespace_sem);
2133c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
2134427215d8SMiklos Szeredi 		goto invalid;
2135427215d8SMiklos Szeredi 
2136427215d8SMiklos Szeredi 	if (!check_mnt(old_mnt))
2137427215d8SMiklos Szeredi 		goto invalid;
2138427215d8SMiklos Szeredi 
2139427215d8SMiklos Szeredi 	if (has_locked_children(old_mnt, path->dentry))
2140427215d8SMiklos Szeredi 		goto invalid;
2141c771d683SMiklos Szeredi 
2142c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
2143427215d8SMiklos Szeredi 	up_read(&namespace_sem);
2144427215d8SMiklos Szeredi 
2145c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
2146c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
2147c771d683SMiklos Szeredi 
2148df820f8dSMiklos Szeredi 	/* Longterm mount to be removed by kern_unmount*() */
2149df820f8dSMiklos Szeredi 	new_mnt->mnt_ns = MNT_NS_INTERNAL;
2150df820f8dSMiklos Szeredi 
2151c771d683SMiklos Szeredi 	return &new_mnt->mnt;
2152427215d8SMiklos Szeredi 
2153427215d8SMiklos Szeredi invalid:
2154427215d8SMiklos Szeredi 	up_read(&namespace_sem);
2155427215d8SMiklos Szeredi 	return ERR_PTR(-EINVAL);
2156c771d683SMiklos Szeredi }
2157c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
2158c771d683SMiklos Szeredi 
21591f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
21601f707137SAl Viro 		   struct vfsmount *root)
21611f707137SAl Viro {
21621a4eeaf2SAl Viro 	struct mount *mnt;
21631f707137SAl Viro 	int res = f(root, arg);
21641f707137SAl Viro 	if (res)
21651f707137SAl Viro 		return res;
21661a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
21671a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
21681f707137SAl Viro 		if (res)
21691f707137SAl Viro 			return res;
21701f707137SAl Viro 	}
21711f707137SAl Viro 	return 0;
21721f707137SAl Viro }
21731f707137SAl Viro 
21743bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt)
21753bd045ccSAl Viro {
21763bd045ccSAl Viro 	struct mount *p;
21773bd045ccSAl Viro 
21783bd045ccSAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
21793bd045ccSAl Viro 		int flags = p->mnt.mnt_flags;
21803bd045ccSAl Viro 		/* Don't allow unprivileged users to change mount flags */
21813bd045ccSAl Viro 		flags |= MNT_LOCK_ATIME;
21823bd045ccSAl Viro 
21833bd045ccSAl Viro 		if (flags & MNT_READONLY)
21843bd045ccSAl Viro 			flags |= MNT_LOCK_READONLY;
21853bd045ccSAl Viro 
21863bd045ccSAl Viro 		if (flags & MNT_NODEV)
21873bd045ccSAl Viro 			flags |= MNT_LOCK_NODEV;
21883bd045ccSAl Viro 
21893bd045ccSAl Viro 		if (flags & MNT_NOSUID)
21903bd045ccSAl Viro 			flags |= MNT_LOCK_NOSUID;
21913bd045ccSAl Viro 
21923bd045ccSAl Viro 		if (flags & MNT_NOEXEC)
21933bd045ccSAl Viro 			flags |= MNT_LOCK_NOEXEC;
21943bd045ccSAl Viro 		/* Don't allow unprivileged users to reveal what is under a mount */
21953bd045ccSAl Viro 		if (list_empty(&p->mnt_expire))
21963bd045ccSAl Viro 			flags |= MNT_LOCKED;
21973bd045ccSAl Viro 		p->mnt.mnt_flags = flags;
21983bd045ccSAl Viro 	}
21993bd045ccSAl Viro }
22003bd045ccSAl Viro 
22014b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
2202719f5d7fSMiklos Szeredi {
2203315fc83eSAl Viro 	struct mount *p;
2204719f5d7fSMiklos Szeredi 
2205909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
2206fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
22074b8b21f4SAl Viro 			mnt_release_group_id(p);
2208719f5d7fSMiklos Szeredi 	}
2209719f5d7fSMiklos Szeredi }
2210719f5d7fSMiklos Szeredi 
22114b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
2212719f5d7fSMiklos Szeredi {
2213315fc83eSAl Viro 	struct mount *p;
2214719f5d7fSMiklos Szeredi 
2215909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
2216fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
22174b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
2218719f5d7fSMiklos Szeredi 			if (err) {
22194b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
2220719f5d7fSMiklos Szeredi 				return err;
2221719f5d7fSMiklos Szeredi 			}
2222719f5d7fSMiklos Szeredi 		}
2223719f5d7fSMiklos Szeredi 	}
2224719f5d7fSMiklos Szeredi 
2225719f5d7fSMiklos Szeredi 	return 0;
2226719f5d7fSMiklos Szeredi }
2227719f5d7fSMiklos Szeredi 
2228d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2229d2921684SEric W. Biederman {
2230d2921684SEric W. Biederman 	unsigned int max = READ_ONCE(sysctl_mount_max);
2231124f75f8SAl Viro 	unsigned int mounts = 0;
2232d2921684SEric W. Biederman 	struct mount *p;
2233d2921684SEric W. Biederman 
2234124f75f8SAl Viro 	if (ns->mounts >= max)
2235124f75f8SAl Viro 		return -ENOSPC;
2236124f75f8SAl Viro 	max -= ns->mounts;
2237124f75f8SAl Viro 	if (ns->pending_mounts >= max)
2238124f75f8SAl Viro 		return -ENOSPC;
2239124f75f8SAl Viro 	max -= ns->pending_mounts;
2240124f75f8SAl Viro 
2241d2921684SEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt))
2242d2921684SEric W. Biederman 		mounts++;
2243d2921684SEric W. Biederman 
2244124f75f8SAl Viro 	if (mounts > max)
2245d2921684SEric W. Biederman 		return -ENOSPC;
2246d2921684SEric W. Biederman 
2247124f75f8SAl Viro 	ns->pending_mounts += mounts;
2248d2921684SEric W. Biederman 	return 0;
2249d2921684SEric W. Biederman }
2250d2921684SEric W. Biederman 
2251b90fa9aeSRam Pai /*
2252b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
2253b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
225421444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
225521444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
225621444403SRam Pai  *  		   (done when source_mnt is moved)
2257b90fa9aeSRam Pai  *
2258b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
2259b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
22609676f0c6SRam Pai  * ---------------------------------------------------------------------------
2261b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
22629676f0c6SRam Pai  * |**************************************************************************
22639676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
22649676f0c6SRam Pai  * | dest     |               |                |                |            |
22659676f0c6SRam Pai  * |   |      |               |                |                |            |
22669676f0c6SRam Pai  * |   v      |               |                |                |            |
22679676f0c6SRam Pai  * |**************************************************************************
22689676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
22695afe0022SRam Pai  * |          |               |                |                |            |
22709676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
22719676f0c6SRam Pai  * ***************************************************************************
2272b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
2273b90fa9aeSRam Pai  * destination mount.
2274b90fa9aeSRam Pai  *
2275b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
2276b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
2277b90fa9aeSRam Pai  * 	 the peer group of the source mount.
2278b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
2279b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
2280b90fa9aeSRam Pai  *       mount.
22815afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
22825afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
22835afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
22845afe0022SRam Pai  *       is marked as 'shared and slave'.
22855afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
22865afe0022SRam Pai  * 	 source mount.
22875afe0022SRam Pai  *
22889676f0c6SRam Pai  * ---------------------------------------------------------------------------
228921444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
22909676f0c6SRam Pai  * |**************************************************************************
22919676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
22929676f0c6SRam Pai  * | dest     |               |                |                |            |
22939676f0c6SRam Pai  * |   |      |               |                |                |            |
22949676f0c6SRam Pai  * |   v      |               |                |                |            |
22959676f0c6SRam Pai  * |**************************************************************************
22969676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
22975afe0022SRam Pai  * |          |               |                |                |            |
22989676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
22999676f0c6SRam Pai  * ***************************************************************************
23005afe0022SRam Pai  *
23015afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
23025afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
230321444403SRam Pai  * (+*)  the mount is moved to the destination.
23045afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
23055afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
23065afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
23075afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
2308b90fa9aeSRam Pai  *
2309b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
2310b90fa9aeSRam Pai  * applied to each mount in the tree.
2311b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
2312b90fa9aeSRam Pai  * in allocations.
2313b90fa9aeSRam Pai  */
23140fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
231584d17192SAl Viro 			struct mount *dest_mnt,
231684d17192SAl Viro 			struct mountpoint *dest_mp,
23172763d119SAl Viro 			bool moving)
2318b90fa9aeSRam Pai {
23193bd045ccSAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
232038129a13SAl Viro 	HLIST_HEAD(tree_list);
2321d2921684SEric W. Biederman 	struct mnt_namespace *ns = dest_mnt->mnt_ns;
23221064f874SEric W. Biederman 	struct mountpoint *smp;
2323315fc83eSAl Viro 	struct mount *child, *p;
232438129a13SAl Viro 	struct hlist_node *n;
2325719f5d7fSMiklos Szeredi 	int err;
2326b90fa9aeSRam Pai 
23271064f874SEric W. Biederman 	/* Preallocate a mountpoint in case the new mounts need
23281064f874SEric W. Biederman 	 * to be tucked under other mounts.
23291064f874SEric W. Biederman 	 */
23301064f874SEric W. Biederman 	smp = get_mountpoint(source_mnt->mnt.mnt_root);
23311064f874SEric W. Biederman 	if (IS_ERR(smp))
23321064f874SEric W. Biederman 		return PTR_ERR(smp);
23331064f874SEric W. Biederman 
2334d2921684SEric W. Biederman 	/* Is there space to add these mounts to the mount namespace? */
23352763d119SAl Viro 	if (!moving) {
2336d2921684SEric W. Biederman 		err = count_mounts(ns, source_mnt);
2337d2921684SEric W. Biederman 		if (err)
2338d2921684SEric W. Biederman 			goto out;
2339d2921684SEric W. Biederman 	}
2340d2921684SEric W. Biederman 
2341fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
23420fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
2343719f5d7fSMiklos Szeredi 		if (err)
2344719f5d7fSMiklos Szeredi 			goto out;
234584d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
2346f2ebb3a9SAl Viro 		lock_mount_hash();
2347719f5d7fSMiklos Szeredi 		if (err)
2348719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
2349909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
23500f0afb1dSAl Viro 			set_mnt_shared(p);
23510b1b901bSAl Viro 	} else {
23520b1b901bSAl Viro 		lock_mount_hash();
2353b90fa9aeSRam Pai 	}
23542763d119SAl Viro 	if (moving) {
23552763d119SAl Viro 		unhash_mnt(source_mnt);
235684d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
2357143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
235821444403SRam Pai 	} else {
235944dfd84aSDavid Howells 		if (source_mnt->mnt_ns) {
236044dfd84aSDavid Howells 			/* move from anon - the caller will destroy */
236144dfd84aSDavid Howells 			list_del_init(&source_mnt->mnt_ns->list);
236244dfd84aSDavid Howells 		}
236384d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
23641064f874SEric W. Biederman 		commit_tree(source_mnt);
236521444403SRam Pai 	}
2366b90fa9aeSRam Pai 
236738129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
23681d6a32acSAl Viro 		struct mount *q;
236938129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
23701064f874SEric W. Biederman 		q = __lookup_mnt(&child->mnt_parent->mnt,
23711d6a32acSAl Viro 				 child->mnt_mountpoint);
23721064f874SEric W. Biederman 		if (q)
23731064f874SEric W. Biederman 			mnt_change_mountpoint(child, smp, q);
23743bd045ccSAl Viro 		/* Notice when we are propagating across user namespaces */
23753bd045ccSAl Viro 		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
23763bd045ccSAl Viro 			lock_mnt_tree(child);
2377d728cf79SChristian Brauner 		child->mnt.mnt_flags &= ~MNT_LOCKED;
23781064f874SEric W. Biederman 		commit_tree(child);
2379b90fa9aeSRam Pai 	}
23801064f874SEric W. Biederman 	put_mountpoint(smp);
2381719ea2fbSAl Viro 	unlock_mount_hash();
238299b7db7bSNick Piggin 
2383b90fa9aeSRam Pai 	return 0;
2384719f5d7fSMiklos Szeredi 
2385719f5d7fSMiklos Szeredi  out_cleanup_ids:
2386f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
2387f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2388d2921684SEric W. Biederman 		child->mnt_parent->mnt_ns->pending_mounts = 0;
2389e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
2390f2ebb3a9SAl Viro 	}
2391f2ebb3a9SAl Viro 	unlock_mount_hash();
23920fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
2393719f5d7fSMiklos Szeredi  out:
2394d2921684SEric W. Biederman 	ns->pending_mounts = 0;
23951064f874SEric W. Biederman 
23961064f874SEric W. Biederman 	read_seqlock_excl(&mount_lock);
23971064f874SEric W. Biederman 	put_mountpoint(smp);
23981064f874SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
23991064f874SEric W. Biederman 
2400719f5d7fSMiklos Szeredi 	return err;
2401b90fa9aeSRam Pai }
2402b90fa9aeSRam Pai 
240384d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
2404b12cea91SAl Viro {
2405b12cea91SAl Viro 	struct vfsmount *mnt;
240684d17192SAl Viro 	struct dentry *dentry = path->dentry;
2407b12cea91SAl Viro retry:
24085955102cSAl Viro 	inode_lock(dentry->d_inode);
240984d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
24105955102cSAl Viro 		inode_unlock(dentry->d_inode);
241184d17192SAl Viro 		return ERR_PTR(-ENOENT);
2412b12cea91SAl Viro 	}
241397216be0SAl Viro 	namespace_lock();
2414b12cea91SAl Viro 	mnt = lookup_mnt(path);
241584d17192SAl Viro 	if (likely(!mnt)) {
24163895dbf8SEric W. Biederman 		struct mountpoint *mp = get_mountpoint(dentry);
241784d17192SAl Viro 		if (IS_ERR(mp)) {
241897216be0SAl Viro 			namespace_unlock();
24195955102cSAl Viro 			inode_unlock(dentry->d_inode);
242084d17192SAl Viro 			return mp;
242184d17192SAl Viro 		}
242284d17192SAl Viro 		return mp;
242384d17192SAl Viro 	}
242497216be0SAl Viro 	namespace_unlock();
24255955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
2426b12cea91SAl Viro 	path_put(path);
2427b12cea91SAl Viro 	path->mnt = mnt;
242884d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
2429b12cea91SAl Viro 	goto retry;
2430b12cea91SAl Viro }
2431b12cea91SAl Viro 
243284d17192SAl Viro static void unlock_mount(struct mountpoint *where)
2433b12cea91SAl Viro {
243484d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
24353895dbf8SEric W. Biederman 
24363895dbf8SEric W. Biederman 	read_seqlock_excl(&mount_lock);
243784d17192SAl Viro 	put_mountpoint(where);
24383895dbf8SEric W. Biederman 	read_sequnlock_excl(&mount_lock);
24393895dbf8SEric W. Biederman 
2440328e6d90SAl Viro 	namespace_unlock();
24415955102cSAl Viro 	inode_unlock(dentry->d_inode);
2442b12cea91SAl Viro }
2443b12cea91SAl Viro 
244484d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
24451da177e4SLinus Torvalds {
2446e462ec50SDavid Howells 	if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
24471da177e4SLinus Torvalds 		return -EINVAL;
24481da177e4SLinus Torvalds 
2449e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
2450e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
24511da177e4SLinus Torvalds 		return -ENOTDIR;
24521da177e4SLinus Torvalds 
24532763d119SAl Viro 	return attach_recursive_mnt(mnt, p, mp, false);
24541da177e4SLinus Torvalds }
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds /*
24577a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
24587a2e8a8fSValerie Aurora  */
24597a2e8a8fSValerie Aurora 
2460e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags)
24617a2e8a8fSValerie Aurora {
2462e462ec50SDavid Howells 	int type = ms_flags & ~(MS_REC | MS_SILENT);
24637a2e8a8fSValerie Aurora 
24647a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
24657a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
24667a2e8a8fSValerie Aurora 		return 0;
24677a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
24687a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
24697a2e8a8fSValerie Aurora 		return 0;
24707a2e8a8fSValerie Aurora 	return type;
24717a2e8a8fSValerie Aurora }
24727a2e8a8fSValerie Aurora 
24737a2e8a8fSValerie Aurora /*
247407b20889SRam Pai  * recursively change the type of the mountpoint.
247507b20889SRam Pai  */
2476e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags)
247707b20889SRam Pai {
2478315fc83eSAl Viro 	struct mount *m;
24794b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
2480e462ec50SDavid Howells 	int recurse = ms_flags & MS_REC;
24817a2e8a8fSValerie Aurora 	int type;
2482719f5d7fSMiklos Szeredi 	int err = 0;
248307b20889SRam Pai 
24842d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
248507b20889SRam Pai 		return -EINVAL;
248607b20889SRam Pai 
2487e462ec50SDavid Howells 	type = flags_to_propagation_type(ms_flags);
24887a2e8a8fSValerie Aurora 	if (!type)
24897a2e8a8fSValerie Aurora 		return -EINVAL;
24907a2e8a8fSValerie Aurora 
249197216be0SAl Viro 	namespace_lock();
2492719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
2493719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
2494719f5d7fSMiklos Szeredi 		if (err)
2495719f5d7fSMiklos Szeredi 			goto out_unlock;
2496719f5d7fSMiklos Szeredi 	}
2497719f5d7fSMiklos Szeredi 
2498719ea2fbSAl Viro 	lock_mount_hash();
2499909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
25000f0afb1dSAl Viro 		change_mnt_propagation(m, type);
2501719ea2fbSAl Viro 	unlock_mount_hash();
2502719f5d7fSMiklos Szeredi 
2503719f5d7fSMiklos Szeredi  out_unlock:
250497216be0SAl Viro 	namespace_unlock();
2505719f5d7fSMiklos Szeredi 	return err;
250607b20889SRam Pai }
250707b20889SRam Pai 
2508a07b2000SAl Viro static struct mount *__do_loopback(struct path *old_path, int recurse)
2509a07b2000SAl Viro {
2510a07b2000SAl Viro 	struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2511a07b2000SAl Viro 
2512a07b2000SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2513a07b2000SAl Viro 		return mnt;
2514a07b2000SAl Viro 
2515a07b2000SAl Viro 	if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2516a07b2000SAl Viro 		return mnt;
2517a07b2000SAl Viro 
2518a07b2000SAl Viro 	if (!recurse && has_locked_children(old, old_path->dentry))
2519a07b2000SAl Viro 		return mnt;
2520a07b2000SAl Viro 
2521a07b2000SAl Viro 	if (recurse)
2522a07b2000SAl Viro 		mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2523a07b2000SAl Viro 	else
2524a07b2000SAl Viro 		mnt = clone_mnt(old, old_path->dentry, 0);
2525a07b2000SAl Viro 
2526a07b2000SAl Viro 	if (!IS_ERR(mnt))
2527a07b2000SAl Viro 		mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2528a07b2000SAl Viro 
2529a07b2000SAl Viro 	return mnt;
2530a07b2000SAl Viro }
2531a07b2000SAl Viro 
253207b20889SRam Pai /*
25331da177e4SLinus Torvalds  * do loopback mount.
25341da177e4SLinus Torvalds  */
2535808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
25362dafe1c4SEric Sandeen 				int recurse)
25371da177e4SLinus Torvalds {
25382d92ab3cSAl Viro 	struct path old_path;
2539a07b2000SAl Viro 	struct mount *mnt = NULL, *parent;
254084d17192SAl Viro 	struct mountpoint *mp;
254157eccb83SAl Viro 	int err;
25421da177e4SLinus Torvalds 	if (!old_name || !*old_name)
25431da177e4SLinus Torvalds 		return -EINVAL;
2544815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
25451da177e4SLinus Torvalds 	if (err)
25461da177e4SLinus Torvalds 		return err;
25471da177e4SLinus Torvalds 
25488823c079SEric W. Biederman 	err = -EINVAL;
25494ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
25508823c079SEric W. Biederman 		goto out;
25518823c079SEric W. Biederman 
255284d17192SAl Viro 	mp = lock_mount(path);
2553a07b2000SAl Viro 	if (IS_ERR(mp)) {
255484d17192SAl Viro 		err = PTR_ERR(mp);
25559676f0c6SRam Pai 		goto out;
2556a07b2000SAl Viro 	}
25579676f0c6SRam Pai 
255884d17192SAl Viro 	parent = real_mount(path->mnt);
2559e149ed2bSAl Viro 	if (!check_mnt(parent))
2560e149ed2bSAl Viro 		goto out2;
2561e149ed2bSAl Viro 
2562a07b2000SAl Viro 	mnt = __do_loopback(&old_path, recurse);
2563be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2564be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2565e9c5d8a5SAndrey Vagin 		goto out2;
2566be34d1a3SDavid Howells 	}
2567ccd48bc7SAl Viro 
256884d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
25691da177e4SLinus Torvalds 	if (err) {
2570719ea2fbSAl Viro 		lock_mount_hash();
2571e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2572719ea2fbSAl Viro 		unlock_mount_hash();
25735b83d2c5SRam Pai 	}
2574b12cea91SAl Viro out2:
257584d17192SAl Viro 	unlock_mount(mp);
2576ccd48bc7SAl Viro out:
25772d92ab3cSAl Viro 	path_put(&old_path);
25781da177e4SLinus Torvalds 	return err;
25791da177e4SLinus Torvalds }
25801da177e4SLinus Torvalds 
2581a07b2000SAl Viro static struct file *open_detached_copy(struct path *path, bool recursive)
2582a07b2000SAl Viro {
2583a07b2000SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2584a07b2000SAl Viro 	struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2585a07b2000SAl Viro 	struct mount *mnt, *p;
2586a07b2000SAl Viro 	struct file *file;
2587a07b2000SAl Viro 
2588a07b2000SAl Viro 	if (IS_ERR(ns))
2589a07b2000SAl Viro 		return ERR_CAST(ns);
2590a07b2000SAl Viro 
2591a07b2000SAl Viro 	namespace_lock();
2592a07b2000SAl Viro 	mnt = __do_loopback(path, recursive);
2593a07b2000SAl Viro 	if (IS_ERR(mnt)) {
2594a07b2000SAl Viro 		namespace_unlock();
2595a07b2000SAl Viro 		free_mnt_ns(ns);
2596a07b2000SAl Viro 		return ERR_CAST(mnt);
2597a07b2000SAl Viro 	}
2598a07b2000SAl Viro 
2599a07b2000SAl Viro 	lock_mount_hash();
2600a07b2000SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2601a07b2000SAl Viro 		p->mnt_ns = ns;
2602a07b2000SAl Viro 		ns->mounts++;
2603a07b2000SAl Viro 	}
2604a07b2000SAl Viro 	ns->root = mnt;
2605a07b2000SAl Viro 	list_add_tail(&ns->list, &mnt->mnt_list);
2606a07b2000SAl Viro 	mntget(&mnt->mnt);
2607a07b2000SAl Viro 	unlock_mount_hash();
2608a07b2000SAl Viro 	namespace_unlock();
2609a07b2000SAl Viro 
2610a07b2000SAl Viro 	mntput(path->mnt);
2611a07b2000SAl Viro 	path->mnt = &mnt->mnt;
2612a07b2000SAl Viro 	file = dentry_open(path, O_PATH, current_cred());
2613a07b2000SAl Viro 	if (IS_ERR(file))
2614a07b2000SAl Viro 		dissolve_on_fput(path->mnt);
2615a07b2000SAl Viro 	else
2616a07b2000SAl Viro 		file->f_mode |= FMODE_NEED_UNMOUNT;
2617a07b2000SAl Viro 	return file;
2618a07b2000SAl Viro }
2619a07b2000SAl Viro 
26202658ce09SBen Dooks SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
2621a07b2000SAl Viro {
2622a07b2000SAl Viro 	struct file *file;
2623a07b2000SAl Viro 	struct path path;
2624a07b2000SAl Viro 	int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2625a07b2000SAl Viro 	bool detached = flags & OPEN_TREE_CLONE;
2626a07b2000SAl Viro 	int error;
2627a07b2000SAl Viro 	int fd;
2628a07b2000SAl Viro 
2629a07b2000SAl Viro 	BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2630a07b2000SAl Viro 
2631a07b2000SAl Viro 	if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2632a07b2000SAl Viro 		      AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2633a07b2000SAl Viro 		      OPEN_TREE_CLOEXEC))
2634a07b2000SAl Viro 		return -EINVAL;
2635a07b2000SAl Viro 
2636a07b2000SAl Viro 	if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2637a07b2000SAl Viro 		return -EINVAL;
2638a07b2000SAl Viro 
2639a07b2000SAl Viro 	if (flags & AT_NO_AUTOMOUNT)
2640a07b2000SAl Viro 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
2641a07b2000SAl Viro 	if (flags & AT_SYMLINK_NOFOLLOW)
2642a07b2000SAl Viro 		lookup_flags &= ~LOOKUP_FOLLOW;
2643a07b2000SAl Viro 	if (flags & AT_EMPTY_PATH)
2644a07b2000SAl Viro 		lookup_flags |= LOOKUP_EMPTY;
2645a07b2000SAl Viro 
2646a07b2000SAl Viro 	if (detached && !may_mount())
2647a07b2000SAl Viro 		return -EPERM;
2648a07b2000SAl Viro 
2649a07b2000SAl Viro 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
2650a07b2000SAl Viro 	if (fd < 0)
2651a07b2000SAl Viro 		return fd;
2652a07b2000SAl Viro 
2653a07b2000SAl Viro 	error = user_path_at(dfd, filename, lookup_flags, &path);
2654a07b2000SAl Viro 	if (unlikely(error)) {
2655a07b2000SAl Viro 		file = ERR_PTR(error);
2656a07b2000SAl Viro 	} else {
2657a07b2000SAl Viro 		if (detached)
2658a07b2000SAl Viro 			file = open_detached_copy(&path, flags & AT_RECURSIVE);
2659a07b2000SAl Viro 		else
2660a07b2000SAl Viro 			file = dentry_open(&path, O_PATH, current_cred());
2661a07b2000SAl Viro 		path_put(&path);
2662a07b2000SAl Viro 	}
2663a07b2000SAl Viro 	if (IS_ERR(file)) {
2664a07b2000SAl Viro 		put_unused_fd(fd);
2665a07b2000SAl Viro 		return PTR_ERR(file);
2666a07b2000SAl Viro 	}
2667a07b2000SAl Viro 	fd_install(fd, file);
2668a07b2000SAl Viro 	return fd;
2669a07b2000SAl Viro }
2670a07b2000SAl Viro 
267143f5e655SDavid Howells /*
267243f5e655SDavid Howells  * Don't allow locked mount flags to be cleared.
267343f5e655SDavid Howells  *
267443f5e655SDavid Howells  * No locks need to be held here while testing the various MNT_LOCK
267543f5e655SDavid Howells  * flags because those flags can never be cleared once they are set.
267643f5e655SDavid Howells  */
267743f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
26782e4b7fcdSDave Hansen {
267943f5e655SDavid Howells 	unsigned int fl = mnt->mnt.mnt_flags;
26802e4b7fcdSDave Hansen 
268143f5e655SDavid Howells 	if ((fl & MNT_LOCK_READONLY) &&
268243f5e655SDavid Howells 	    !(mnt_flags & MNT_READONLY))
268343f5e655SDavid Howells 		return false;
268443f5e655SDavid Howells 
268543f5e655SDavid Howells 	if ((fl & MNT_LOCK_NODEV) &&
268643f5e655SDavid Howells 	    !(mnt_flags & MNT_NODEV))
268743f5e655SDavid Howells 		return false;
268843f5e655SDavid Howells 
268943f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOSUID) &&
269043f5e655SDavid Howells 	    !(mnt_flags & MNT_NOSUID))
269143f5e655SDavid Howells 		return false;
269243f5e655SDavid Howells 
269343f5e655SDavid Howells 	if ((fl & MNT_LOCK_NOEXEC) &&
269443f5e655SDavid Howells 	    !(mnt_flags & MNT_NOEXEC))
269543f5e655SDavid Howells 		return false;
269643f5e655SDavid Howells 
269743f5e655SDavid Howells 	if ((fl & MNT_LOCK_ATIME) &&
269843f5e655SDavid Howells 	    ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
269943f5e655SDavid Howells 		return false;
270043f5e655SDavid Howells 
270143f5e655SDavid Howells 	return true;
270243f5e655SDavid Howells }
270343f5e655SDavid Howells 
270443f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
270543f5e655SDavid Howells {
270643f5e655SDavid Howells 	bool readonly_request = (mnt_flags & MNT_READONLY);
270743f5e655SDavid Howells 
270843f5e655SDavid Howells 	if (readonly_request == __mnt_is_readonly(&mnt->mnt))
27092e4b7fcdSDave Hansen 		return 0;
27102e4b7fcdSDave Hansen 
27112e4b7fcdSDave Hansen 	if (readonly_request)
271243f5e655SDavid Howells 		return mnt_make_readonly(mnt);
271343f5e655SDavid Howells 
271468847c94SChristian Brauner 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
271568847c94SChristian Brauner 	return 0;
271643f5e655SDavid Howells }
271743f5e655SDavid Howells 
271843f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
271943f5e655SDavid Howells {
272043f5e655SDavid Howells 	mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
272143f5e655SDavid Howells 	mnt->mnt.mnt_flags = mnt_flags;
272243f5e655SDavid Howells 	touch_mnt_namespace(mnt->mnt_ns);
272343f5e655SDavid Howells }
272443f5e655SDavid Howells 
2725f8b92ba6SDeepa Dinamani static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
2726f8b92ba6SDeepa Dinamani {
2727f8b92ba6SDeepa Dinamani 	struct super_block *sb = mnt->mnt_sb;
2728f8b92ba6SDeepa Dinamani 
2729f8b92ba6SDeepa Dinamani 	if (!__mnt_is_readonly(mnt) &&
2730a128b054SAnthony Iliopoulos 	   (!(sb->s_iflags & SB_I_TS_EXPIRY_WARNED)) &&
2731f8b92ba6SDeepa Dinamani 	   (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
2732f8b92ba6SDeepa Dinamani 		char *buf = (char *)__get_free_page(GFP_KERNEL);
2733f8b92ba6SDeepa Dinamani 		char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM);
2734f8b92ba6SDeepa Dinamani 		struct tm tm;
2735f8b92ba6SDeepa Dinamani 
2736f8b92ba6SDeepa Dinamani 		time64_to_tm(sb->s_time_max, 0, &tm);
2737f8b92ba6SDeepa Dinamani 
27380ecee669SEric Biggers 		pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n",
27390ecee669SEric Biggers 			sb->s_type->name,
27400ecee669SEric Biggers 			is_mounted(mnt) ? "remounted" : "mounted",
27410ecee669SEric Biggers 			mntpath,
2742f8b92ba6SDeepa Dinamani 			tm.tm_year+1900, (unsigned long long)sb->s_time_max);
2743f8b92ba6SDeepa Dinamani 
2744f8b92ba6SDeepa Dinamani 		free_page((unsigned long)buf);
2745a128b054SAnthony Iliopoulos 		sb->s_iflags |= SB_I_TS_EXPIRY_WARNED;
2746f8b92ba6SDeepa Dinamani 	}
2747f8b92ba6SDeepa Dinamani }
2748f8b92ba6SDeepa Dinamani 
274943f5e655SDavid Howells /*
275043f5e655SDavid Howells  * Handle reconfiguration of the mountpoint only without alteration of the
275143f5e655SDavid Howells  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
275243f5e655SDavid Howells  * to mount(2).
275343f5e655SDavid Howells  */
275443f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
275543f5e655SDavid Howells {
275643f5e655SDavid Howells 	struct super_block *sb = path->mnt->mnt_sb;
275743f5e655SDavid Howells 	struct mount *mnt = real_mount(path->mnt);
275843f5e655SDavid Howells 	int ret;
275943f5e655SDavid Howells 
276043f5e655SDavid Howells 	if (!check_mnt(mnt))
276143f5e655SDavid Howells 		return -EINVAL;
276243f5e655SDavid Howells 
276343f5e655SDavid Howells 	if (path->dentry != mnt->mnt.mnt_root)
276443f5e655SDavid Howells 		return -EINVAL;
276543f5e655SDavid Howells 
276643f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
276743f5e655SDavid Howells 		return -EPERM;
276843f5e655SDavid Howells 
2769e58ace1aSChristian Brauner 	/*
2770e58ace1aSChristian Brauner 	 * We're only checking whether the superblock is read-only not
2771e58ace1aSChristian Brauner 	 * changing it, so only take down_read(&sb->s_umount).
2772e58ace1aSChristian Brauner 	 */
2773e58ace1aSChristian Brauner 	down_read(&sb->s_umount);
277468847c94SChristian Brauner 	lock_mount_hash();
277543f5e655SDavid Howells 	ret = change_mount_ro_state(mnt, mnt_flags);
277643f5e655SDavid Howells 	if (ret == 0)
277743f5e655SDavid Howells 		set_mount_attributes(mnt, mnt_flags);
277868847c94SChristian Brauner 	unlock_mount_hash();
2779e58ace1aSChristian Brauner 	up_read(&sb->s_umount);
2780f8b92ba6SDeepa Dinamani 
2781f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2782f8b92ba6SDeepa Dinamani 
278343f5e655SDavid Howells 	return ret;
27842e4b7fcdSDave Hansen }
27852e4b7fcdSDave Hansen 
27861da177e4SLinus Torvalds /*
27871da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
27881da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
27891da177e4SLinus Torvalds  * on it - tough luck.
27901da177e4SLinus Torvalds  */
2791e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags,
2792e462ec50SDavid Howells 		      int mnt_flags, void *data)
27931da177e4SLinus Torvalds {
27941da177e4SLinus Torvalds 	int err;
27952d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2796143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
27978d0347f6SDavid Howells 	struct fs_context *fc;
27981da177e4SLinus Torvalds 
2799143c8c91SAl Viro 	if (!check_mnt(mnt))
28001da177e4SLinus Torvalds 		return -EINVAL;
28011da177e4SLinus Torvalds 
28022d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
28031da177e4SLinus Torvalds 		return -EINVAL;
28041da177e4SLinus Torvalds 
280543f5e655SDavid Howells 	if (!can_change_locked_flags(mnt, mnt_flags))
280607b64558SEric W. Biederman 		return -EPERM;
28079566d674SEric W. Biederman 
28088d0347f6SDavid Howells 	fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
28098d0347f6SDavid Howells 	if (IS_ERR(fc))
28108d0347f6SDavid Howells 		return PTR_ERR(fc);
2811ff36fe2cSEric Paris 
2812b330966fSMiklos Szeredi 	fc->oldapi = true;
28138d0347f6SDavid Howells 	err = parse_monolithic_mount_data(fc, data);
28148d0347f6SDavid Howells 	if (!err) {
28151da177e4SLinus Torvalds 		down_write(&sb->s_umount);
281657eccb83SAl Viro 		err = -EPERM;
281743f5e655SDavid Howells 		if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
28188d0347f6SDavid Howells 			err = reconfigure_super(fc);
281968847c94SChristian Brauner 			if (!err) {
282068847c94SChristian Brauner 				lock_mount_hash();
282143f5e655SDavid Howells 				set_mount_attributes(mnt, mnt_flags);
282268847c94SChristian Brauner 				unlock_mount_hash();
282368847c94SChristian Brauner 			}
28240e55a7ccSDan Williams 		}
28256339dab8SAl Viro 		up_write(&sb->s_umount);
28268d0347f6SDavid Howells 	}
2827f8b92ba6SDeepa Dinamani 
2828f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2829f8b92ba6SDeepa Dinamani 
28308d0347f6SDavid Howells 	put_fs_context(fc);
28311da177e4SLinus Torvalds 	return err;
28321da177e4SLinus Torvalds }
28331da177e4SLinus Torvalds 
2834cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
28359676f0c6SRam Pai {
2836315fc83eSAl Viro 	struct mount *p;
2837909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2838fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
28399676f0c6SRam Pai 			return 1;
28409676f0c6SRam Pai 	}
28419676f0c6SRam Pai 	return 0;
28429676f0c6SRam Pai }
28439676f0c6SRam Pai 
284444dfd84aSDavid Howells /*
284544dfd84aSDavid Howells  * Check that there aren't references to earlier/same mount namespaces in the
284644dfd84aSDavid Howells  * specified subtree.  Such references can act as pins for mount namespaces
284744dfd84aSDavid Howells  * that aren't checked by the mount-cycle checking code, thereby allowing
284844dfd84aSDavid Howells  * cycles to be made.
284944dfd84aSDavid Howells  */
285044dfd84aSDavid Howells static bool check_for_nsfs_mounts(struct mount *subtree)
285144dfd84aSDavid Howells {
285244dfd84aSDavid Howells 	struct mount *p;
285344dfd84aSDavid Howells 	bool ret = false;
285444dfd84aSDavid Howells 
285544dfd84aSDavid Howells 	lock_mount_hash();
285644dfd84aSDavid Howells 	for (p = subtree; p; p = next_mnt(p, subtree))
285744dfd84aSDavid Howells 		if (mnt_ns_loop(p->mnt.mnt_root))
285844dfd84aSDavid Howells 			goto out;
285944dfd84aSDavid Howells 
286044dfd84aSDavid Howells 	ret = true;
286144dfd84aSDavid Howells out:
286244dfd84aSDavid Howells 	unlock_mount_hash();
286344dfd84aSDavid Howells 	return ret;
286444dfd84aSDavid Howells }
286544dfd84aSDavid Howells 
28669ffb14efSPavel Tikhomirov static int do_set_group(struct path *from_path, struct path *to_path)
28679ffb14efSPavel Tikhomirov {
28689ffb14efSPavel Tikhomirov 	struct mount *from, *to;
28699ffb14efSPavel Tikhomirov 	int err;
28709ffb14efSPavel Tikhomirov 
28719ffb14efSPavel Tikhomirov 	from = real_mount(from_path->mnt);
28729ffb14efSPavel Tikhomirov 	to = real_mount(to_path->mnt);
28739ffb14efSPavel Tikhomirov 
28749ffb14efSPavel Tikhomirov 	namespace_lock();
28759ffb14efSPavel Tikhomirov 
28769ffb14efSPavel Tikhomirov 	err = -EINVAL;
28779ffb14efSPavel Tikhomirov 	/* To and From must be mounted */
28789ffb14efSPavel Tikhomirov 	if (!is_mounted(&from->mnt))
28799ffb14efSPavel Tikhomirov 		goto out;
28809ffb14efSPavel Tikhomirov 	if (!is_mounted(&to->mnt))
28819ffb14efSPavel Tikhomirov 		goto out;
28829ffb14efSPavel Tikhomirov 
28839ffb14efSPavel Tikhomirov 	err = -EPERM;
28849ffb14efSPavel Tikhomirov 	/* We should be allowed to modify mount namespaces of both mounts */
28859ffb14efSPavel Tikhomirov 	if (!ns_capable(from->mnt_ns->user_ns, CAP_SYS_ADMIN))
28869ffb14efSPavel Tikhomirov 		goto out;
28879ffb14efSPavel Tikhomirov 	if (!ns_capable(to->mnt_ns->user_ns, CAP_SYS_ADMIN))
28889ffb14efSPavel Tikhomirov 		goto out;
28899ffb14efSPavel Tikhomirov 
28909ffb14efSPavel Tikhomirov 	err = -EINVAL;
28919ffb14efSPavel Tikhomirov 	/* To and From paths should be mount roots */
28929ffb14efSPavel Tikhomirov 	if (from_path->dentry != from_path->mnt->mnt_root)
28939ffb14efSPavel Tikhomirov 		goto out;
28949ffb14efSPavel Tikhomirov 	if (to_path->dentry != to_path->mnt->mnt_root)
28959ffb14efSPavel Tikhomirov 		goto out;
28969ffb14efSPavel Tikhomirov 
28979ffb14efSPavel Tikhomirov 	/* Setting sharing groups is only allowed across same superblock */
28989ffb14efSPavel Tikhomirov 	if (from->mnt.mnt_sb != to->mnt.mnt_sb)
28999ffb14efSPavel Tikhomirov 		goto out;
29009ffb14efSPavel Tikhomirov 
29019ffb14efSPavel Tikhomirov 	/* From mount root should be wider than To mount root */
29029ffb14efSPavel Tikhomirov 	if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root))
29039ffb14efSPavel Tikhomirov 		goto out;
29049ffb14efSPavel Tikhomirov 
29059ffb14efSPavel Tikhomirov 	/* From mount should not have locked children in place of To's root */
29069ffb14efSPavel Tikhomirov 	if (has_locked_children(from, to->mnt.mnt_root))
29079ffb14efSPavel Tikhomirov 		goto out;
29089ffb14efSPavel Tikhomirov 
29099ffb14efSPavel Tikhomirov 	/* Setting sharing groups is only allowed on private mounts */
29109ffb14efSPavel Tikhomirov 	if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to))
29119ffb14efSPavel Tikhomirov 		goto out;
29129ffb14efSPavel Tikhomirov 
29139ffb14efSPavel Tikhomirov 	/* From should not be private */
29149ffb14efSPavel Tikhomirov 	if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from))
29159ffb14efSPavel Tikhomirov 		goto out;
29169ffb14efSPavel Tikhomirov 
29179ffb14efSPavel Tikhomirov 	if (IS_MNT_SLAVE(from)) {
29189ffb14efSPavel Tikhomirov 		struct mount *m = from->mnt_master;
29199ffb14efSPavel Tikhomirov 
29209ffb14efSPavel Tikhomirov 		list_add(&to->mnt_slave, &m->mnt_slave_list);
29219ffb14efSPavel Tikhomirov 		to->mnt_master = m;
29229ffb14efSPavel Tikhomirov 	}
29239ffb14efSPavel Tikhomirov 
29249ffb14efSPavel Tikhomirov 	if (IS_MNT_SHARED(from)) {
29259ffb14efSPavel Tikhomirov 		to->mnt_group_id = from->mnt_group_id;
29269ffb14efSPavel Tikhomirov 		list_add(&to->mnt_share, &from->mnt_share);
29279ffb14efSPavel Tikhomirov 		lock_mount_hash();
29289ffb14efSPavel Tikhomirov 		set_mnt_shared(to);
29299ffb14efSPavel Tikhomirov 		unlock_mount_hash();
29309ffb14efSPavel Tikhomirov 	}
29319ffb14efSPavel Tikhomirov 
29329ffb14efSPavel Tikhomirov 	err = 0;
29339ffb14efSPavel Tikhomirov out:
29349ffb14efSPavel Tikhomirov 	namespace_unlock();
29359ffb14efSPavel Tikhomirov 	return err;
29369ffb14efSPavel Tikhomirov }
29379ffb14efSPavel Tikhomirov 
29382db154b3SDavid Howells static int do_move_mount(struct path *old_path, struct path *new_path)
29391da177e4SLinus Torvalds {
294044dfd84aSDavid Howells 	struct mnt_namespace *ns;
2941676da58dSAl Viro 	struct mount *p;
29420fb54e50SAl Viro 	struct mount *old;
29432763d119SAl Viro 	struct mount *parent;
29442763d119SAl Viro 	struct mountpoint *mp, *old_mp;
294557eccb83SAl Viro 	int err;
294644dfd84aSDavid Howells 	bool attached;
29471da177e4SLinus Torvalds 
29482db154b3SDavid Howells 	mp = lock_mount(new_path);
294984d17192SAl Viro 	if (IS_ERR(mp))
29502db154b3SDavid Howells 		return PTR_ERR(mp);
2951cc53ce53SDavid Howells 
29522db154b3SDavid Howells 	old = real_mount(old_path->mnt);
29532db154b3SDavid Howells 	p = real_mount(new_path->mnt);
29542763d119SAl Viro 	parent = old->mnt_parent;
295544dfd84aSDavid Howells 	attached = mnt_has_parent(old);
29562763d119SAl Viro 	old_mp = old->mnt_mp;
295744dfd84aSDavid Howells 	ns = old->mnt_ns;
2958143c8c91SAl Viro 
29591da177e4SLinus Torvalds 	err = -EINVAL;
296044dfd84aSDavid Howells 	/* The mountpoint must be in our namespace. */
296144dfd84aSDavid Howells 	if (!check_mnt(p))
29622db154b3SDavid Howells 		goto out;
29631da177e4SLinus Torvalds 
2964570d7a98SEric Biggers 	/* The thing moved must be mounted... */
2965570d7a98SEric Biggers 	if (!is_mounted(&old->mnt))
296644dfd84aSDavid Howells 		goto out;
296744dfd84aSDavid Howells 
2968570d7a98SEric Biggers 	/* ... and either ours or the root of anon namespace */
2969570d7a98SEric Biggers 	if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
29702db154b3SDavid Howells 		goto out;
29711da177e4SLinus Torvalds 
29722db154b3SDavid Howells 	if (old->mnt.mnt_flags & MNT_LOCKED)
29732db154b3SDavid Howells 		goto out;
29742db154b3SDavid Howells 
29752db154b3SDavid Howells 	if (old_path->dentry != old_path->mnt->mnt_root)
29762db154b3SDavid Howells 		goto out;
29772db154b3SDavid Howells 
29782db154b3SDavid Howells 	if (d_is_dir(new_path->dentry) !=
29792db154b3SDavid Howells 	    d_is_dir(old_path->dentry))
29802db154b3SDavid Howells 		goto out;
298121444403SRam Pai 	/*
298221444403SRam Pai 	 * Don't move a mount residing in a shared parent.
298321444403SRam Pai 	 */
29842763d119SAl Viro 	if (attached && IS_MNT_SHARED(parent))
29852db154b3SDavid Howells 		goto out;
29869676f0c6SRam Pai 	/*
29879676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
29889676f0c6SRam Pai 	 * mount which is shared.
29899676f0c6SRam Pai 	 */
2990fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
29912db154b3SDavid Howells 		goto out;
29921da177e4SLinus Torvalds 	err = -ELOOP;
299344dfd84aSDavid Howells 	if (!check_for_nsfs_mounts(old))
299444dfd84aSDavid Howells 		goto out;
2995fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2996676da58dSAl Viro 		if (p == old)
29972db154b3SDavid Howells 			goto out;
29981da177e4SLinus Torvalds 
29992db154b3SDavid Howells 	err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
30002763d119SAl Viro 				   attached);
30014ac91378SJan Blunck 	if (err)
30022db154b3SDavid Howells 		goto out;
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
30051da177e4SLinus Torvalds 	 * automatically */
30066776db3dSAl Viro 	list_del_init(&old->mnt_expire);
30072763d119SAl Viro 	if (attached)
30082763d119SAl Viro 		put_mountpoint(old_mp);
30091da177e4SLinus Torvalds out:
30102db154b3SDavid Howells 	unlock_mount(mp);
301144dfd84aSDavid Howells 	if (!err) {
30122763d119SAl Viro 		if (attached)
30132763d119SAl Viro 			mntput_no_expire(parent);
30142763d119SAl Viro 		else
301544dfd84aSDavid Howells 			free_mnt_ns(ns);
301644dfd84aSDavid Howells 	}
30172db154b3SDavid Howells 	return err;
30182db154b3SDavid Howells }
30192db154b3SDavid Howells 
30202db154b3SDavid Howells static int do_move_mount_old(struct path *path, const char *old_name)
30212db154b3SDavid Howells {
30222db154b3SDavid Howells 	struct path old_path;
30232db154b3SDavid Howells 	int err;
30242db154b3SDavid Howells 
30252db154b3SDavid Howells 	if (!old_name || !*old_name)
30262db154b3SDavid Howells 		return -EINVAL;
30272db154b3SDavid Howells 
30282db154b3SDavid Howells 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
30292db154b3SDavid Howells 	if (err)
30302db154b3SDavid Howells 		return err;
30312db154b3SDavid Howells 
30322db154b3SDavid Howells 	err = do_move_mount(&old_path, path);
30332d92ab3cSAl Viro 	path_put(&old_path);
30341da177e4SLinus Torvalds 	return err;
30351da177e4SLinus Torvalds }
30361da177e4SLinus Torvalds 
30379d412a43SAl Viro /*
30389d412a43SAl Viro  * add a mount into a namespace's mount tree
30399d412a43SAl Viro  */
30408f11538eSAl Viro static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
30411e2d8464SAl Viro 			const struct path *path, int mnt_flags)
30429d412a43SAl Viro {
30438f11538eSAl Viro 	struct mount *parent = real_mount(path->mnt);
30449d412a43SAl Viro 
3045f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
30469d412a43SAl Viro 
304784d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
3048156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
3049156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
30508f11538eSAl Viro 			return -EINVAL;
3051156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
305284d17192SAl Viro 		if (!parent->mnt_ns)
30538f11538eSAl Viro 			return -EINVAL;
3054156cacb1SAl Viro 	}
30559d412a43SAl Viro 
30569d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
305795bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
30589d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
30598f11538eSAl Viro 		return -EBUSY;
30609d412a43SAl Viro 
3061e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
30628f11538eSAl Viro 		return -EINVAL;
30639d412a43SAl Viro 
306495bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
30658f11538eSAl Viro 	return graft_tree(newmnt, parent, mp);
30669d412a43SAl Viro }
3067b1e75df4SAl Viro 
3068132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
3069132e4608SDavid Howells 
3070132e4608SDavid Howells /*
3071132e4608SDavid Howells  * Create a new mount using a superblock configuration and request it
3072132e4608SDavid Howells  * be added to the namespace tree.
3073132e4608SDavid Howells  */
3074132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
3075132e4608SDavid Howells 			   unsigned int mnt_flags)
3076132e4608SDavid Howells {
3077132e4608SDavid Howells 	struct vfsmount *mnt;
30788f11538eSAl Viro 	struct mountpoint *mp;
3079132e4608SDavid Howells 	struct super_block *sb = fc->root->d_sb;
3080132e4608SDavid Howells 	int error;
3081132e4608SDavid Howells 
3082c9ce29edSAl Viro 	error = security_sb_kern_mount(sb);
3083c9ce29edSAl Viro 	if (!error && mount_too_revealing(sb, &mnt_flags))
3084c9ce29edSAl Viro 		error = -EPERM;
3085c9ce29edSAl Viro 
3086c9ce29edSAl Viro 	if (unlikely(error)) {
3087c9ce29edSAl Viro 		fc_drop_locked(fc);
3088c9ce29edSAl Viro 		return error;
3089132e4608SDavid Howells 	}
3090132e4608SDavid Howells 
3091132e4608SDavid Howells 	up_write(&sb->s_umount);
3092132e4608SDavid Howells 
3093132e4608SDavid Howells 	mnt = vfs_create_mount(fc);
3094132e4608SDavid Howells 	if (IS_ERR(mnt))
3095132e4608SDavid Howells 		return PTR_ERR(mnt);
3096132e4608SDavid Howells 
3097f8b92ba6SDeepa Dinamani 	mnt_warn_timestamp_expiry(mountpoint, mnt);
3098f8b92ba6SDeepa Dinamani 
30998f11538eSAl Viro 	mp = lock_mount(mountpoint);
31008f11538eSAl Viro 	if (IS_ERR(mp)) {
31018f11538eSAl Viro 		mntput(mnt);
31028f11538eSAl Viro 		return PTR_ERR(mp);
31038f11538eSAl Viro 	}
31048f11538eSAl Viro 	error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
31058f11538eSAl Viro 	unlock_mount(mp);
31060ecee669SEric Biggers 	if (error < 0)
31070ecee669SEric Biggers 		mntput(mnt);
3108f8b92ba6SDeepa Dinamani 	return error;
3109f8b92ba6SDeepa Dinamani }
3110f8b92ba6SDeepa Dinamani 
31111da177e4SLinus Torvalds /*
31121da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
31131da177e4SLinus Torvalds  * namespace's tree
31141da177e4SLinus Torvalds  */
3115e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
3116808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
31171da177e4SLinus Torvalds {
31180c55cfc4SEric W. Biederman 	struct file_system_type *type;
3119a0c9a8b8SAl Viro 	struct fs_context *fc;
3120a0c9a8b8SAl Viro 	const char *subtype = NULL;
3121a0c9a8b8SAl Viro 	int err = 0;
31221da177e4SLinus Torvalds 
31230c55cfc4SEric W. Biederman 	if (!fstype)
31241da177e4SLinus Torvalds 		return -EINVAL;
31251da177e4SLinus Torvalds 
31260c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
31270c55cfc4SEric W. Biederman 	if (!type)
31280c55cfc4SEric W. Biederman 		return -ENODEV;
31290c55cfc4SEric W. Biederman 
3130a0c9a8b8SAl Viro 	if (type->fs_flags & FS_HAS_SUBTYPE) {
3131a0c9a8b8SAl Viro 		subtype = strchr(fstype, '.');
3132a0c9a8b8SAl Viro 		if (subtype) {
3133a0c9a8b8SAl Viro 			subtype++;
3134a0c9a8b8SAl Viro 			if (!*subtype) {
31350c55cfc4SEric W. Biederman 				put_filesystem(type);
3136a0c9a8b8SAl Viro 				return -EINVAL;
3137a0c9a8b8SAl Viro 			}
3138a0c9a8b8SAl Viro 		}
31398654df4eSEric W. Biederman 	}
31408654df4eSEric W. Biederman 
3141a0c9a8b8SAl Viro 	fc = fs_context_for_mount(type, sb_flags);
3142a0c9a8b8SAl Viro 	put_filesystem(type);
3143a0c9a8b8SAl Viro 	if (IS_ERR(fc))
3144a0c9a8b8SAl Viro 		return PTR_ERR(fc);
3145a0c9a8b8SAl Viro 
31463e1aeb00SDavid Howells 	if (subtype)
31473e1aeb00SDavid Howells 		err = vfs_parse_fs_string(fc, "subtype",
31483e1aeb00SDavid Howells 					  subtype, strlen(subtype));
31493e1aeb00SDavid Howells 	if (!err && name)
31503e1aeb00SDavid Howells 		err = vfs_parse_fs_string(fc, "source", name, strlen(name));
3151a0c9a8b8SAl Viro 	if (!err)
3152a0c9a8b8SAl Viro 		err = parse_monolithic_mount_data(fc, data);
3153c3aabf07SAl Viro 	if (!err && !mount_capable(fc))
3154c3aabf07SAl Viro 		err = -EPERM;
3155a0c9a8b8SAl Viro 	if (!err)
3156a0c9a8b8SAl Viro 		err = vfs_get_tree(fc);
3157132e4608SDavid Howells 	if (!err)
3158132e4608SDavid Howells 		err = do_new_mount_fc(fc, path, mnt_flags);
3159a0c9a8b8SAl Viro 
3160a0c9a8b8SAl Viro 	put_fs_context(fc);
316115f9a3f3SAl Viro 	return err;
31621da177e4SLinus Torvalds }
31631da177e4SLinus Torvalds 
31641e2d8464SAl Viro int finish_automount(struct vfsmount *m, const struct path *path)
316519a167afSAl Viro {
316626df6034SAl Viro 	struct dentry *dentry = path->dentry;
31678f11538eSAl Viro 	struct mountpoint *mp;
316825e195aaSAl Viro 	struct mount *mnt;
316919a167afSAl Viro 	int err;
317025e195aaSAl Viro 
317125e195aaSAl Viro 	if (!m)
317225e195aaSAl Viro 		return 0;
317325e195aaSAl Viro 	if (IS_ERR(m))
317425e195aaSAl Viro 		return PTR_ERR(m);
317525e195aaSAl Viro 
317625e195aaSAl Viro 	mnt = real_mount(m);
317719a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
317819a167afSAl Viro 	 * expired before we get a chance to add it
317919a167afSAl Viro 	 */
31806776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
318119a167afSAl Viro 
318219a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
318326df6034SAl Viro 	    m->mnt_root == dentry) {
3184b1e75df4SAl Viro 		err = -ELOOP;
318526df6034SAl Viro 		goto discard;
318619a167afSAl Viro 	}
318719a167afSAl Viro 
318826df6034SAl Viro 	/*
318926df6034SAl Viro 	 * we don't want to use lock_mount() - in this case finding something
319026df6034SAl Viro 	 * that overmounts our mountpoint to be means "quitely drop what we've
319126df6034SAl Viro 	 * got", not "try to mount it on top".
319226df6034SAl Viro 	 */
319326df6034SAl Viro 	inode_lock(dentry->d_inode);
319426df6034SAl Viro 	namespace_lock();
319526df6034SAl Viro 	if (unlikely(cant_mount(dentry))) {
319626df6034SAl Viro 		err = -ENOENT;
319726df6034SAl Viro 		goto discard_locked;
319826df6034SAl Viro 	}
319926df6034SAl Viro 	rcu_read_lock();
320026df6034SAl Viro 	if (unlikely(__lookup_mnt(path->mnt, dentry))) {
320126df6034SAl Viro 		rcu_read_unlock();
320226df6034SAl Viro 		err = 0;
320326df6034SAl Viro 		goto discard_locked;
320426df6034SAl Viro 	}
320526df6034SAl Viro 	rcu_read_unlock();
320626df6034SAl Viro 	mp = get_mountpoint(dentry);
32078f11538eSAl Viro 	if (IS_ERR(mp)) {
32088f11538eSAl Viro 		err = PTR_ERR(mp);
320926df6034SAl Viro 		goto discard_locked;
32108f11538eSAl Viro 	}
321126df6034SAl Viro 
32128f11538eSAl Viro 	err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
32138f11538eSAl Viro 	unlock_mount(mp);
321426df6034SAl Viro 	if (unlikely(err))
321526df6034SAl Viro 		goto discard;
321626df6034SAl Viro 	mntput(m);
3217b1e75df4SAl Viro 	return 0;
321826df6034SAl Viro 
321926df6034SAl Viro discard_locked:
322026df6034SAl Viro 	namespace_unlock();
322126df6034SAl Viro 	inode_unlock(dentry->d_inode);
322226df6034SAl Viro discard:
3223b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
32246776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
322597216be0SAl Viro 		namespace_lock();
32266776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
322797216be0SAl Viro 		namespace_unlock();
322819a167afSAl Viro 	}
3229b1e75df4SAl Viro 	mntput(m);
3230b1e75df4SAl Viro 	mntput(m);
323119a167afSAl Viro 	return err;
323219a167afSAl Viro }
323319a167afSAl Viro 
3234ea5b778aSDavid Howells /**
3235ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
3236ea5b778aSDavid Howells  * @mnt: The mount to list.
3237ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
3238ea5b778aSDavid Howells  */
3239ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
3240ea5b778aSDavid Howells {
324197216be0SAl Viro 	namespace_lock();
3242ea5b778aSDavid Howells 
32436776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
3244ea5b778aSDavid Howells 
324597216be0SAl Viro 	namespace_unlock();
3246ea5b778aSDavid Howells }
3247ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
3248ea5b778aSDavid Howells 
3249ea5b778aSDavid Howells /*
32501da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
32511da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
32521da177e4SLinus Torvalds  * here
32531da177e4SLinus Torvalds  */
32541da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
32551da177e4SLinus Torvalds {
3256761d5c38SAl Viro 	struct mount *mnt, *next;
32571da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds 	if (list_empty(mounts))
32601da177e4SLinus Torvalds 		return;
32611da177e4SLinus Torvalds 
326297216be0SAl Viro 	namespace_lock();
3263719ea2fbSAl Viro 	lock_mount_hash();
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
32661da177e4SLinus Torvalds 	 * following criteria:
32671da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
32681da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
32691da177e4SLinus Torvalds 	 *   cleared by mntput())
32701da177e4SLinus Torvalds 	 */
32716776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
3272863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
32731ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
32741da177e4SLinus Torvalds 			continue;
32756776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
32761da177e4SLinus Torvalds 	}
3277bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
32786776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
3279143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
3280e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3281bcc5c7d2SAl Viro 	}
3282719ea2fbSAl Viro 	unlock_mount_hash();
32833ab6abeeSAl Viro 	namespace_unlock();
32841da177e4SLinus Torvalds }
32851da177e4SLinus Torvalds 
32861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
32871da177e4SLinus Torvalds 
32881da177e4SLinus Torvalds /*
32895528f911STrond Myklebust  * Ripoff of 'select_parent()'
32905528f911STrond Myklebust  *
32915528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
32925528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
32935528f911STrond Myklebust  */
3294692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
32955528f911STrond Myklebust {
3296692afc31SAl Viro 	struct mount *this_parent = parent;
32975528f911STrond Myklebust 	struct list_head *next;
32985528f911STrond Myklebust 	int found = 0;
32995528f911STrond Myklebust 
33005528f911STrond Myklebust repeat:
33016b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
33025528f911STrond Myklebust resume:
33036b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
33045528f911STrond Myklebust 		struct list_head *tmp = next;
33056b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
33065528f911STrond Myklebust 
33075528f911STrond Myklebust 		next = tmp->next;
3308692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
33095528f911STrond Myklebust 			continue;
33105528f911STrond Myklebust 		/*
33115528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
33125528f911STrond Myklebust 		 */
33136b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
33145528f911STrond Myklebust 			this_parent = mnt;
33155528f911STrond Myklebust 			goto repeat;
33165528f911STrond Myklebust 		}
33175528f911STrond Myklebust 
33181ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
33196776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
33205528f911STrond Myklebust 			found++;
33215528f911STrond Myklebust 		}
33225528f911STrond Myklebust 	}
33235528f911STrond Myklebust 	/*
33245528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
33255528f911STrond Myklebust 	 */
33265528f911STrond Myklebust 	if (this_parent != parent) {
33276b41d536SAl Viro 		next = this_parent->mnt_child.next;
33280714a533SAl Viro 		this_parent = this_parent->mnt_parent;
33295528f911STrond Myklebust 		goto resume;
33305528f911STrond Myklebust 	}
33315528f911STrond Myklebust 	return found;
33325528f911STrond Myklebust }
33335528f911STrond Myklebust 
33345528f911STrond Myklebust /*
33355528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
33365528f911STrond Myklebust  * submounts of a specific parent mountpoint
333799b7db7bSNick Piggin  *
333848a066e7SAl Viro  * mount_lock must be held for write
33395528f911STrond Myklebust  */
3340b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
33415528f911STrond Myklebust {
33425528f911STrond Myklebust 	LIST_HEAD(graveyard);
3343761d5c38SAl Viro 	struct mount *m;
33445528f911STrond Myklebust 
33455528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
3346c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
3347bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
3348761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
33496776db3dSAl Viro 						mnt_expire);
3350143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
3351e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3352bcc5c7d2SAl Viro 		}
3353bcc5c7d2SAl Viro 	}
33545528f911STrond Myklebust }
33555528f911STrond Myklebust 
3356028abd92SChristoph Hellwig static void *copy_mount_options(const void __user * data)
33571da177e4SLinus Torvalds {
3358b40ef869SAl Viro 	char *copy;
3359d563d678SCatalin Marinas 	unsigned left, offset;
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds 	if (!data)
3362b40ef869SAl Viro 		return NULL;
33631da177e4SLinus Torvalds 
3364b40ef869SAl Viro 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
3365b40ef869SAl Viro 	if (!copy)
3366b40ef869SAl Viro 		return ERR_PTR(-ENOMEM);
33671da177e4SLinus Torvalds 
3368d563d678SCatalin Marinas 	left = copy_from_user(copy, data, PAGE_SIZE);
33691da177e4SLinus Torvalds 
3370d563d678SCatalin Marinas 	/*
3371d563d678SCatalin Marinas 	 * Not all architectures have an exact copy_from_user(). Resort to
3372d563d678SCatalin Marinas 	 * byte at a time.
3373d563d678SCatalin Marinas 	 */
3374d563d678SCatalin Marinas 	offset = PAGE_SIZE - left;
3375d563d678SCatalin Marinas 	while (left) {
3376d563d678SCatalin Marinas 		char c;
3377d563d678SCatalin Marinas 		if (get_user(c, (const char __user *)data + offset))
3378d563d678SCatalin Marinas 			break;
3379d563d678SCatalin Marinas 		copy[offset] = c;
3380d563d678SCatalin Marinas 		left--;
3381d563d678SCatalin Marinas 		offset++;
3382d563d678SCatalin Marinas 	}
3383d563d678SCatalin Marinas 
3384d563d678SCatalin Marinas 	if (left == PAGE_SIZE) {
3385b40ef869SAl Viro 		kfree(copy);
3386b40ef869SAl Viro 		return ERR_PTR(-EFAULT);
33871da177e4SLinus Torvalds 	}
3388d563d678SCatalin Marinas 
3389b40ef869SAl Viro 	return copy;
33901da177e4SLinus Torvalds }
33911da177e4SLinus Torvalds 
3392028abd92SChristoph Hellwig static char *copy_mount_string(const void __user *data)
3393eca6f534SVegard Nossum {
3394fbdb4401SChandan Rajendra 	return data ? strndup_user(data, PATH_MAX) : NULL;
3395eca6f534SVegard Nossum }
3396eca6f534SVegard Nossum 
33971da177e4SLinus Torvalds /*
33981da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
33991da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
34001da177e4SLinus Torvalds  *
34011da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
34021da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
34031da177e4SLinus Torvalds  * information (or be NULL).
34041da177e4SLinus Torvalds  *
34051da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
34061da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
34071da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
34081da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
34091da177e4SLinus Torvalds  * and must be discarded.
34101da177e4SLinus Torvalds  */
3411c60166f0SChristoph Hellwig int path_mount(const char *dev_name, struct path *path,
3412808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
34131da177e4SLinus Torvalds {
3414e462ec50SDavid Howells 	unsigned int mnt_flags = 0, sb_flags;
3415a1e6aaa3SChristoph Hellwig 	int ret;
34161da177e4SLinus Torvalds 
34171da177e4SLinus Torvalds 	/* Discard magic */
34181da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
34191da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds 	/* Basic sanity checks */
34221da177e4SLinus Torvalds 	if (data_page)
34231da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
34241da177e4SLinus Torvalds 
3425e462ec50SDavid Howells 	if (flags & MS_NOUSER)
3426e462ec50SDavid Howells 		return -EINVAL;
3427e462ec50SDavid Howells 
3428a1e6aaa3SChristoph Hellwig 	ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
3429a1e6aaa3SChristoph Hellwig 	if (ret)
3430a1e6aaa3SChristoph Hellwig 		return ret;
3431a1e6aaa3SChristoph Hellwig 	if (!may_mount())
3432a1e6aaa3SChristoph Hellwig 		return -EPERM;
3433f7e33bdbSJeff Layton 	if (flags & SB_MANDLOCK)
3434f7e33bdbSJeff Layton 		warn_mandlock();
3435a27ab9f2STetsuo Handa 
3436613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
3437613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
34380a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
34390a1c01c9SMatthew Garrett 
34401da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
34411da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
34421da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
34431da177e4SLinus Torvalds 	if (flags & MS_NODEV)
34441da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
34451da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
34461da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
3447fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
3448fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
3449fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
3450fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
3451d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
3452d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
3453a9e5b732SDavid Howells 	if (flags & MS_RDONLY)
34542e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
3455dab741e0SMattias Nissler 	if (flags & MS_NOSYMFOLLOW)
3456dab741e0SMattias Nissler 		mnt_flags |= MNT_NOSYMFOLLOW;
3457fc33a7bbSChristoph Hellwig 
3458ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
3459ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
3460ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3461ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
3462ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
3463a1e6aaa3SChristoph Hellwig 		mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
3464ffbc6f0eSEric W. Biederman 	}
3465ffbc6f0eSEric W. Biederman 
3466e462ec50SDavid Howells 	sb_flags = flags & (SB_RDONLY |
3467e462ec50SDavid Howells 			    SB_SYNCHRONOUS |
3468e462ec50SDavid Howells 			    SB_MANDLOCK |
3469e462ec50SDavid Howells 			    SB_DIRSYNC |
3470e462ec50SDavid Howells 			    SB_SILENT |
3471917086ffSMimi Zohar 			    SB_POSIXACL |
3472d7ee9469SMarkus Trippelsdorf 			    SB_LAZYTIME |
3473917086ffSMimi Zohar 			    SB_I_VERSION);
34741da177e4SLinus Torvalds 
347543f5e655SDavid Howells 	if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
3476a1e6aaa3SChristoph Hellwig 		return do_reconfigure_mnt(path, mnt_flags);
3477a1e6aaa3SChristoph Hellwig 	if (flags & MS_REMOUNT)
3478a1e6aaa3SChristoph Hellwig 		return do_remount(path, flags, sb_flags, mnt_flags, data_page);
3479a1e6aaa3SChristoph Hellwig 	if (flags & MS_BIND)
3480a1e6aaa3SChristoph Hellwig 		return do_loopback(path, dev_name, flags & MS_REC);
3481a1e6aaa3SChristoph Hellwig 	if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
3482a1e6aaa3SChristoph Hellwig 		return do_change_type(path, flags);
3483a1e6aaa3SChristoph Hellwig 	if (flags & MS_MOVE)
3484a1e6aaa3SChristoph Hellwig 		return do_move_mount_old(path, dev_name);
3485a1e6aaa3SChristoph Hellwig 
3486a1e6aaa3SChristoph Hellwig 	return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
34871da177e4SLinus Torvalds 			    data_page);
3488a1e6aaa3SChristoph Hellwig }
3489a1e6aaa3SChristoph Hellwig 
3490a1e6aaa3SChristoph Hellwig long do_mount(const char *dev_name, const char __user *dir_name,
3491a1e6aaa3SChristoph Hellwig 		const char *type_page, unsigned long flags, void *data_page)
3492a1e6aaa3SChristoph Hellwig {
3493a1e6aaa3SChristoph Hellwig 	struct path path;
3494a1e6aaa3SChristoph Hellwig 	int ret;
3495a1e6aaa3SChristoph Hellwig 
3496a1e6aaa3SChristoph Hellwig 	ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
3497a1e6aaa3SChristoph Hellwig 	if (ret)
3498a1e6aaa3SChristoph Hellwig 		return ret;
3499a1e6aaa3SChristoph Hellwig 	ret = path_mount(dev_name, &path, type_page, flags, data_page);
35002d92ab3cSAl Viro 	path_put(&path);
3501a1e6aaa3SChristoph Hellwig 	return ret;
35021da177e4SLinus Torvalds }
35031da177e4SLinus Torvalds 
3504537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3505537f7ccbSEric W. Biederman {
3506537f7ccbSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3507537f7ccbSEric W. Biederman }
3508537f7ccbSEric W. Biederman 
3509537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts)
3510537f7ccbSEric W. Biederman {
3511537f7ccbSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3512537f7ccbSEric W. Biederman }
3513537f7ccbSEric W. Biederman 
3514771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
3515771b1371SEric W. Biederman {
351674e83122SAl Viro 	if (!is_anon_ns(ns))
35176344c433SAl Viro 		ns_free_inum(&ns->ns);
3518537f7ccbSEric W. Biederman 	dec_mnt_namespaces(ns->ucounts);
3519771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
3520771b1371SEric W. Biederman 	kfree(ns);
3521771b1371SEric W. Biederman }
3522771b1371SEric W. Biederman 
35238823c079SEric W. Biederman /*
35248823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
35258823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
35268823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
35278823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
35288823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
35298823c079SEric W. Biederman  */
35308823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
35318823c079SEric W. Biederman 
353274e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
3533cf8d2c11STrond Myklebust {
3534cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
3535537f7ccbSEric W. Biederman 	struct ucounts *ucounts;
353698f842e6SEric W. Biederman 	int ret;
3537cf8d2c11STrond Myklebust 
3538537f7ccbSEric W. Biederman 	ucounts = inc_mnt_namespaces(user_ns);
3539537f7ccbSEric W. Biederman 	if (!ucounts)
3540df75e774SEric W. Biederman 		return ERR_PTR(-ENOSPC);
3541537f7ccbSEric W. Biederman 
354230acd0bdSVasily Averin 	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
3543537f7ccbSEric W. Biederman 	if (!new_ns) {
3544537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
3545cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
3546537f7ccbSEric W. Biederman 	}
354774e83122SAl Viro 	if (!anon) {
35486344c433SAl Viro 		ret = ns_alloc_inum(&new_ns->ns);
354998f842e6SEric W. Biederman 		if (ret) {
355098f842e6SEric W. Biederman 			kfree(new_ns);
3551537f7ccbSEric W. Biederman 			dec_mnt_namespaces(ucounts);
355298f842e6SEric W. Biederman 			return ERR_PTR(ret);
355398f842e6SEric W. Biederman 		}
355474e83122SAl Viro 	}
355533c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
355674e83122SAl Viro 	if (!anon)
35578823c079SEric W. Biederman 		new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
35581a7b8969SKirill Tkhai 	refcount_set(&new_ns->ns.count, 1);
3559cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
3560cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
35619f6c61f9SMiklos Szeredi 	spin_lock_init(&new_ns->ns_lock);
3562771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
3563537f7ccbSEric W. Biederman 	new_ns->ucounts = ucounts;
3564cf8d2c11STrond Myklebust 	return new_ns;
3565cf8d2c11STrond Myklebust }
3566cf8d2c11STrond Myklebust 
35670766f788SEmese Revfy __latent_entropy
35689559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
35699559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
35701da177e4SLinus Torvalds {
35716b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
35727f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
3573315fc83eSAl Viro 	struct mount *p, *q;
35749559f689SAl Viro 	struct mount *old;
3575cb338d06SAl Viro 	struct mount *new;
35767a472ef4SEric W. Biederman 	int copy_flags;
35771da177e4SLinus Torvalds 
35789559f689SAl Viro 	BUG_ON(!ns);
35799559f689SAl Viro 
35809559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
35819559f689SAl Viro 		get_mnt_ns(ns);
35829559f689SAl Viro 		return ns;
35839559f689SAl Viro 	}
35849559f689SAl Viro 
35859559f689SAl Viro 	old = ns->root;
35869559f689SAl Viro 
358774e83122SAl Viro 	new_ns = alloc_mnt_ns(user_ns, false);
3588cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
3589cf8d2c11STrond Myklebust 		return new_ns;
35901da177e4SLinus Torvalds 
359197216be0SAl Viro 	namespace_lock();
35921da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
35934ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
35949559f689SAl Viro 	if (user_ns != ns->user_ns)
35953bd045ccSAl Viro 		copy_flags |= CL_SHARED_TO_SLAVE;
35967a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
3597be34d1a3SDavid Howells 	if (IS_ERR(new)) {
3598328e6d90SAl Viro 		namespace_unlock();
3599771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
3600be34d1a3SDavid Howells 		return ERR_CAST(new);
36011da177e4SLinus Torvalds 	}
36023bd045ccSAl Viro 	if (user_ns != ns->user_ns) {
36033bd045ccSAl Viro 		lock_mount_hash();
36043bd045ccSAl Viro 		lock_mnt_tree(new);
36053bd045ccSAl Viro 		unlock_mount_hash();
36063bd045ccSAl Viro 	}
3607be08d6d2SAl Viro 	new_ns->root = new;
36081a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
36091da177e4SLinus Torvalds 
36101da177e4SLinus Torvalds 	/*
36111da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
36121da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
36131da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
36141da177e4SLinus Torvalds 	 */
3615909b0a88SAl Viro 	p = old;
3616cb338d06SAl Viro 	q = new;
36171da177e4SLinus Torvalds 	while (p) {
3618143c8c91SAl Viro 		q->mnt_ns = new_ns;
3619d2921684SEric W. Biederman 		new_ns->mounts++;
36209559f689SAl Viro 		if (new_fs) {
36219559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
36229559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
3623315fc83eSAl Viro 				rootmnt = &p->mnt;
36241da177e4SLinus Torvalds 			}
36259559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
36269559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
3627315fc83eSAl Viro 				pwdmnt = &p->mnt;
36281da177e4SLinus Torvalds 			}
36291da177e4SLinus Torvalds 		}
3630909b0a88SAl Viro 		p = next_mnt(p, old);
3631909b0a88SAl Viro 		q = next_mnt(q, new);
36324ce5d2b1SEric W. Biederman 		if (!q)
36334ce5d2b1SEric W. Biederman 			break;
363461d8e426SAl Viro 		// an mntns binding we'd skipped?
36354ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
363661d8e426SAl Viro 			p = next_mnt(skip_mnt_tree(p), old);
36371da177e4SLinus Torvalds 	}
3638328e6d90SAl Viro 	namespace_unlock();
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds 	if (rootmnt)
3641f03c6599SAl Viro 		mntput(rootmnt);
36421da177e4SLinus Torvalds 	if (pwdmnt)
3643f03c6599SAl Viro 		mntput(pwdmnt);
36441da177e4SLinus Torvalds 
3645741a2951SJANAK DESAI 	return new_ns;
3646741a2951SJANAK DESAI }
3647741a2951SJANAK DESAI 
364874e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name)
3649cf8d2c11STrond Myklebust {
36501a4eeaf2SAl Viro 	struct mount *mnt = real_mount(m);
3651ea441d11SAl Viro 	struct mnt_namespace *ns;
3652d31da0f0SAl Viro 	struct super_block *s;
3653ea441d11SAl Viro 	struct path path;
3654ea441d11SAl Viro 	int err;
3655ea441d11SAl Viro 
365674e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, true);
365774e83122SAl Viro 	if (IS_ERR(ns)) {
365874e83122SAl Viro 		mntput(m);
3659ea441d11SAl Viro 		return ERR_CAST(ns);
366074e83122SAl Viro 	}
366174e83122SAl Viro 	mnt->mnt_ns = ns;
366274e83122SAl Viro 	ns->root = mnt;
366374e83122SAl Viro 	ns->mounts++;
366474e83122SAl Viro 	list_add(&mnt->mnt_list, &ns->list);
3665ea441d11SAl Viro 
366674e83122SAl Viro 	err = vfs_path_lookup(m->mnt_root, m,
3667ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3668ea441d11SAl Viro 
3669ea441d11SAl Viro 	put_mnt_ns(ns);
3670ea441d11SAl Viro 
3671ea441d11SAl Viro 	if (err)
3672ea441d11SAl Viro 		return ERR_PTR(err);
3673ea441d11SAl Viro 
3674ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
3675d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
3676d31da0f0SAl Viro 	atomic_inc(&s->s_active);
3677ea441d11SAl Viro 	mntput(path.mnt);
3678ea441d11SAl Viro 	/* lock the sucker */
3679d31da0f0SAl Viro 	down_write(&s->s_umount);
3680ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
3681ea441d11SAl Viro 	return path.dentry;
3682ea441d11SAl Viro }
3683ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
3684ea441d11SAl Viro 
3685cccaa5e3SDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3686cccaa5e3SDominik Brodowski 		char __user *, type, unsigned long, flags, void __user *, data)
36871da177e4SLinus Torvalds {
3688eca6f534SVegard Nossum 	int ret;
3689eca6f534SVegard Nossum 	char *kernel_type;
3690eca6f534SVegard Nossum 	char *kernel_dev;
3691b40ef869SAl Viro 	void *options;
36921da177e4SLinus Torvalds 
3693b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
3694b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
3695b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
3696eca6f534SVegard Nossum 		goto out_type;
36971da177e4SLinus Torvalds 
3698b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
3699b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
3700b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
3701eca6f534SVegard Nossum 		goto out_dev;
37021da177e4SLinus Torvalds 
3703b40ef869SAl Viro 	options = copy_mount_options(data);
3704b40ef869SAl Viro 	ret = PTR_ERR(options);
3705b40ef869SAl Viro 	if (IS_ERR(options))
3706eca6f534SVegard Nossum 		goto out_data;
37071da177e4SLinus Torvalds 
3708b40ef869SAl Viro 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
3709eca6f534SVegard Nossum 
3710b40ef869SAl Viro 	kfree(options);
3711eca6f534SVegard Nossum out_data:
3712eca6f534SVegard Nossum 	kfree(kernel_dev);
3713eca6f534SVegard Nossum out_dev:
3714eca6f534SVegard Nossum 	kfree(kernel_type);
3715eca6f534SVegard Nossum out_type:
3716eca6f534SVegard Nossum 	return ret;
37171da177e4SLinus Torvalds }
37181da177e4SLinus Torvalds 
37195b490500SChristian Brauner #define FSMOUNT_VALID_FLAGS                                                    \
37205b490500SChristian Brauner 	(MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV |            \
3721dd8b477fSChristian Brauner 	 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME |       \
3722dd8b477fSChristian Brauner 	 MOUNT_ATTR_NOSYMFOLLOW)
37235b490500SChristian Brauner 
37249caccd41SChristian Brauner #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
37252a186721SChristian Brauner 
37262a186721SChristian Brauner #define MOUNT_SETATTR_PROPAGATION_FLAGS \
37272a186721SChristian Brauner 	(MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
37282a186721SChristian Brauner 
37295b490500SChristian Brauner static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
37305b490500SChristian Brauner {
37315b490500SChristian Brauner 	unsigned int mnt_flags = 0;
37325b490500SChristian Brauner 
37335b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_RDONLY)
37345b490500SChristian Brauner 		mnt_flags |= MNT_READONLY;
37355b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOSUID)
37365b490500SChristian Brauner 		mnt_flags |= MNT_NOSUID;
37375b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NODEV)
37385b490500SChristian Brauner 		mnt_flags |= MNT_NODEV;
37395b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOEXEC)
37405b490500SChristian Brauner 		mnt_flags |= MNT_NOEXEC;
37415b490500SChristian Brauner 	if (attr_flags & MOUNT_ATTR_NODIRATIME)
37425b490500SChristian Brauner 		mnt_flags |= MNT_NODIRATIME;
3743dd8b477fSChristian Brauner 	if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
3744dd8b477fSChristian Brauner 		mnt_flags |= MNT_NOSYMFOLLOW;
37455b490500SChristian Brauner 
37465b490500SChristian Brauner 	return mnt_flags;
37475b490500SChristian Brauner }
37485b490500SChristian Brauner 
37491da177e4SLinus Torvalds /*
375093766fbdSDavid Howells  * Create a kernel mount representation for a new, prepared superblock
375193766fbdSDavid Howells  * (specified by fs_fd) and attach to an open_tree-like file descriptor.
375293766fbdSDavid Howells  */
375393766fbdSDavid Howells SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
375493766fbdSDavid Howells 		unsigned int, attr_flags)
375593766fbdSDavid Howells {
375693766fbdSDavid Howells 	struct mnt_namespace *ns;
375793766fbdSDavid Howells 	struct fs_context *fc;
375893766fbdSDavid Howells 	struct file *file;
375993766fbdSDavid Howells 	struct path newmount;
376093766fbdSDavid Howells 	struct mount *mnt;
376193766fbdSDavid Howells 	struct fd f;
376293766fbdSDavid Howells 	unsigned int mnt_flags = 0;
376393766fbdSDavid Howells 	long ret;
376493766fbdSDavid Howells 
376593766fbdSDavid Howells 	if (!may_mount())
376693766fbdSDavid Howells 		return -EPERM;
376793766fbdSDavid Howells 
376893766fbdSDavid Howells 	if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
376993766fbdSDavid Howells 		return -EINVAL;
377093766fbdSDavid Howells 
37715b490500SChristian Brauner 	if (attr_flags & ~FSMOUNT_VALID_FLAGS)
377293766fbdSDavid Howells 		return -EINVAL;
377393766fbdSDavid Howells 
37745b490500SChristian Brauner 	mnt_flags = attr_flags_to_mnt_flags(attr_flags);
377593766fbdSDavid Howells 
377693766fbdSDavid Howells 	switch (attr_flags & MOUNT_ATTR__ATIME) {
377793766fbdSDavid Howells 	case MOUNT_ATTR_STRICTATIME:
377893766fbdSDavid Howells 		break;
377993766fbdSDavid Howells 	case MOUNT_ATTR_NOATIME:
378093766fbdSDavid Howells 		mnt_flags |= MNT_NOATIME;
378193766fbdSDavid Howells 		break;
378293766fbdSDavid Howells 	case MOUNT_ATTR_RELATIME:
378393766fbdSDavid Howells 		mnt_flags |= MNT_RELATIME;
378493766fbdSDavid Howells 		break;
378593766fbdSDavid Howells 	default:
378693766fbdSDavid Howells 		return -EINVAL;
378793766fbdSDavid Howells 	}
378893766fbdSDavid Howells 
378993766fbdSDavid Howells 	f = fdget(fs_fd);
379093766fbdSDavid Howells 	if (!f.file)
379193766fbdSDavid Howells 		return -EBADF;
379293766fbdSDavid Howells 
379393766fbdSDavid Howells 	ret = -EINVAL;
379493766fbdSDavid Howells 	if (f.file->f_op != &fscontext_fops)
379593766fbdSDavid Howells 		goto err_fsfd;
379693766fbdSDavid Howells 
379793766fbdSDavid Howells 	fc = f.file->private_data;
379893766fbdSDavid Howells 
379993766fbdSDavid Howells 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
380093766fbdSDavid Howells 	if (ret < 0)
380193766fbdSDavid Howells 		goto err_fsfd;
380293766fbdSDavid Howells 
380393766fbdSDavid Howells 	/* There must be a valid superblock or we can't mount it */
380493766fbdSDavid Howells 	ret = -EINVAL;
380593766fbdSDavid Howells 	if (!fc->root)
380693766fbdSDavid Howells 		goto err_unlock;
380793766fbdSDavid Howells 
380893766fbdSDavid Howells 	ret = -EPERM;
380993766fbdSDavid Howells 	if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
381093766fbdSDavid Howells 		pr_warn("VFS: Mount too revealing\n");
381193766fbdSDavid Howells 		goto err_unlock;
381293766fbdSDavid Howells 	}
381393766fbdSDavid Howells 
381493766fbdSDavid Howells 	ret = -EBUSY;
381593766fbdSDavid Howells 	if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
381693766fbdSDavid Howells 		goto err_unlock;
381793766fbdSDavid Howells 
3818f7e33bdbSJeff Layton 	if (fc->sb_flags & SB_MANDLOCK)
3819f7e33bdbSJeff Layton 		warn_mandlock();
382093766fbdSDavid Howells 
382193766fbdSDavid Howells 	newmount.mnt = vfs_create_mount(fc);
382293766fbdSDavid Howells 	if (IS_ERR(newmount.mnt)) {
382393766fbdSDavid Howells 		ret = PTR_ERR(newmount.mnt);
382493766fbdSDavid Howells 		goto err_unlock;
382593766fbdSDavid Howells 	}
382693766fbdSDavid Howells 	newmount.dentry = dget(fc->root);
382793766fbdSDavid Howells 	newmount.mnt->mnt_flags = mnt_flags;
382893766fbdSDavid Howells 
382993766fbdSDavid Howells 	/* We've done the mount bit - now move the file context into more or
383093766fbdSDavid Howells 	 * less the same state as if we'd done an fspick().  We don't want to
383193766fbdSDavid Howells 	 * do any memory allocation or anything like that at this point as we
383293766fbdSDavid Howells 	 * don't want to have to handle any errors incurred.
383393766fbdSDavid Howells 	 */
383493766fbdSDavid Howells 	vfs_clean_context(fc);
383593766fbdSDavid Howells 
383693766fbdSDavid Howells 	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
383793766fbdSDavid Howells 	if (IS_ERR(ns)) {
383893766fbdSDavid Howells 		ret = PTR_ERR(ns);
383993766fbdSDavid Howells 		goto err_path;
384093766fbdSDavid Howells 	}
384193766fbdSDavid Howells 	mnt = real_mount(newmount.mnt);
384293766fbdSDavid Howells 	mnt->mnt_ns = ns;
384393766fbdSDavid Howells 	ns->root = mnt;
384493766fbdSDavid Howells 	ns->mounts = 1;
384593766fbdSDavid Howells 	list_add(&mnt->mnt_list, &ns->list);
38461b0b9cc8SEric Biggers 	mntget(newmount.mnt);
384793766fbdSDavid Howells 
384893766fbdSDavid Howells 	/* Attach to an apparent O_PATH fd with a note that we need to unmount
384993766fbdSDavid Howells 	 * it, not just simply put it.
385093766fbdSDavid Howells 	 */
385193766fbdSDavid Howells 	file = dentry_open(&newmount, O_PATH, fc->cred);
385293766fbdSDavid Howells 	if (IS_ERR(file)) {
385393766fbdSDavid Howells 		dissolve_on_fput(newmount.mnt);
385493766fbdSDavid Howells 		ret = PTR_ERR(file);
385593766fbdSDavid Howells 		goto err_path;
385693766fbdSDavid Howells 	}
385793766fbdSDavid Howells 	file->f_mode |= FMODE_NEED_UNMOUNT;
385893766fbdSDavid Howells 
385993766fbdSDavid Howells 	ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
386093766fbdSDavid Howells 	if (ret >= 0)
386193766fbdSDavid Howells 		fd_install(ret, file);
386293766fbdSDavid Howells 	else
386393766fbdSDavid Howells 		fput(file);
386493766fbdSDavid Howells 
386593766fbdSDavid Howells err_path:
386693766fbdSDavid Howells 	path_put(&newmount);
386793766fbdSDavid Howells err_unlock:
386893766fbdSDavid Howells 	mutex_unlock(&fc->uapi_mutex);
386993766fbdSDavid Howells err_fsfd:
387093766fbdSDavid Howells 	fdput(f);
387193766fbdSDavid Howells 	return ret;
387293766fbdSDavid Howells }
387393766fbdSDavid Howells 
387493766fbdSDavid Howells /*
387593766fbdSDavid Howells  * Move a mount from one place to another.  In combination with
387693766fbdSDavid Howells  * fsopen()/fsmount() this is used to install a new mount and in combination
387793766fbdSDavid Howells  * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
387893766fbdSDavid Howells  * a mount subtree.
38792db154b3SDavid Howells  *
38802db154b3SDavid Howells  * Note the flags value is a combination of MOVE_MOUNT_* flags.
38812db154b3SDavid Howells  */
38822db154b3SDavid Howells SYSCALL_DEFINE5(move_mount,
38832658ce09SBen Dooks 		int, from_dfd, const char __user *, from_pathname,
38842658ce09SBen Dooks 		int, to_dfd, const char __user *, to_pathname,
38852db154b3SDavid Howells 		unsigned int, flags)
38862db154b3SDavid Howells {
38872db154b3SDavid Howells 	struct path from_path, to_path;
38882db154b3SDavid Howells 	unsigned int lflags;
38892db154b3SDavid Howells 	int ret = 0;
38902db154b3SDavid Howells 
38912db154b3SDavid Howells 	if (!may_mount())
38922db154b3SDavid Howells 		return -EPERM;
38932db154b3SDavid Howells 
38942db154b3SDavid Howells 	if (flags & ~MOVE_MOUNT__MASK)
38952db154b3SDavid Howells 		return -EINVAL;
38962db154b3SDavid Howells 
38972db154b3SDavid Howells 	/* If someone gives a pathname, they aren't permitted to move
38982db154b3SDavid Howells 	 * from an fd that requires unmount as we can't get at the flag
38992db154b3SDavid Howells 	 * to clear it afterwards.
39002db154b3SDavid Howells 	 */
39012db154b3SDavid Howells 	lflags = 0;
39022db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
39032db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
39042db154b3SDavid Howells 	if (flags & MOVE_MOUNT_F_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
39052db154b3SDavid Howells 
39062db154b3SDavid Howells 	ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
39072db154b3SDavid Howells 	if (ret < 0)
39082db154b3SDavid Howells 		return ret;
39092db154b3SDavid Howells 
39102db154b3SDavid Howells 	lflags = 0;
39112db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
39122db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
39132db154b3SDavid Howells 	if (flags & MOVE_MOUNT_T_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
39142db154b3SDavid Howells 
39152db154b3SDavid Howells 	ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
39162db154b3SDavid Howells 	if (ret < 0)
39172db154b3SDavid Howells 		goto out_from;
39182db154b3SDavid Howells 
39192db154b3SDavid Howells 	ret = security_move_mount(&from_path, &to_path);
39202db154b3SDavid Howells 	if (ret < 0)
39212db154b3SDavid Howells 		goto out_to;
39222db154b3SDavid Howells 
39239ffb14efSPavel Tikhomirov 	if (flags & MOVE_MOUNT_SET_GROUP)
39249ffb14efSPavel Tikhomirov 		ret = do_set_group(&from_path, &to_path);
39259ffb14efSPavel Tikhomirov 	else
39262db154b3SDavid Howells 		ret = do_move_mount(&from_path, &to_path);
39272db154b3SDavid Howells 
39282db154b3SDavid Howells out_to:
39292db154b3SDavid Howells 	path_put(&to_path);
39302db154b3SDavid Howells out_from:
39312db154b3SDavid Howells 	path_put(&from_path);
39322db154b3SDavid Howells 	return ret;
39332db154b3SDavid Howells }
39342db154b3SDavid Howells 
39352db154b3SDavid Howells /*
3936afac7cbaSAl Viro  * Return true if path is reachable from root
3937afac7cbaSAl Viro  *
393848a066e7SAl Viro  * namespace_sem or mount_lock is held
3939afac7cbaSAl Viro  */
3940643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
3941afac7cbaSAl Viro 			 const struct path *root)
3942afac7cbaSAl Viro {
3943643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
3944a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
39450714a533SAl Viro 		mnt = mnt->mnt_parent;
3946afac7cbaSAl Viro 	}
3947643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
3948afac7cbaSAl Viro }
3949afac7cbaSAl Viro 
3950640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2)
3951afac7cbaSAl Viro {
395225ab4c9bSYaowei Bai 	bool res;
395348a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
3954643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
395548a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
3956afac7cbaSAl Viro 	return res;
3957afac7cbaSAl Viro }
3958afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
3959afac7cbaSAl Viro 
3960afac7cbaSAl Viro /*
39611da177e4SLinus Torvalds  * pivot_root Semantics:
39621da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
39631da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
39641da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
39651da177e4SLinus Torvalds  *
39661da177e4SLinus Torvalds  * Restrictions:
39671da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
39681da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
39691da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
39701da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
39711da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
39721da177e4SLinus Torvalds  *
39734a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
39740c1bc6b8SMauro Carvalho Chehab  * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
39754a0d11faSNeil Brown  * in this situation.
39764a0d11faSNeil Brown  *
39771da177e4SLinus Torvalds  * Notes:
39781da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
39791da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
39801da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
39811da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
39821da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
39831da177e4SLinus Torvalds  *    first.
39841da177e4SLinus Torvalds  */
39853480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
39863480b257SHeiko Carstens 		const char __user *, put_old)
39871da177e4SLinus Torvalds {
39882763d119SAl Viro 	struct path new, old, root;
39892763d119SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
399084d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
39911da177e4SLinus Torvalds 	int error;
39921da177e4SLinus Torvalds 
39939b40bc90SAl Viro 	if (!may_mount())
39941da177e4SLinus Torvalds 		return -EPERM;
39951da177e4SLinus Torvalds 
3996ce6595a2SAl Viro 	error = user_path_at(AT_FDCWD, new_root,
3997ce6595a2SAl Viro 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
39981da177e4SLinus Torvalds 	if (error)
39991da177e4SLinus Torvalds 		goto out0;
40001da177e4SLinus Torvalds 
4001ce6595a2SAl Viro 	error = user_path_at(AT_FDCWD, put_old,
4002ce6595a2SAl Viro 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
40031da177e4SLinus Torvalds 	if (error)
40041da177e4SLinus Torvalds 		goto out1;
40051da177e4SLinus Torvalds 
40062d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
4007b12cea91SAl Viro 	if (error)
4008b12cea91SAl Viro 		goto out2;
40091da177e4SLinus Torvalds 
4010f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
401184d17192SAl Viro 	old_mp = lock_mount(&old);
401284d17192SAl Viro 	error = PTR_ERR(old_mp);
401384d17192SAl Viro 	if (IS_ERR(old_mp))
4014b12cea91SAl Viro 		goto out3;
4015b12cea91SAl Viro 
40161da177e4SLinus Torvalds 	error = -EINVAL;
4017419148daSAl Viro 	new_mnt = real_mount(new.mnt);
4018419148daSAl Viro 	root_mnt = real_mount(root.mnt);
401984d17192SAl Viro 	old_mnt = real_mount(old.mnt);
40202763d119SAl Viro 	ex_parent = new_mnt->mnt_parent;
40212763d119SAl Viro 	root_parent = root_mnt->mnt_parent;
402284d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
40232763d119SAl Viro 		IS_MNT_SHARED(ex_parent) ||
40242763d119SAl Viro 		IS_MNT_SHARED(root_parent))
4025b12cea91SAl Viro 		goto out4;
4026143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
4027b12cea91SAl Viro 		goto out4;
40285ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
40295ff9d8a6SEric W. Biederman 		goto out4;
40301da177e4SLinus Torvalds 	error = -ENOENT;
4031f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
4032b12cea91SAl Viro 		goto out4;
40331da177e4SLinus Torvalds 	error = -EBUSY;
403484d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
4035b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
40361da177e4SLinus Torvalds 	error = -EINVAL;
40378c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
4038b12cea91SAl Viro 		goto out4; /* not a mountpoint */
4039676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
4040b12cea91SAl Viro 		goto out4; /* not attached */
40412d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
4042b12cea91SAl Viro 		goto out4; /* not a mountpoint */
4043676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
4044b12cea91SAl Viro 		goto out4; /* not attached */
40454ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
404684d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
4047b12cea91SAl Viro 		goto out4;
40480d082601SEric W. Biederman 	/* make certain new is below the root */
40490d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
40500d082601SEric W. Biederman 		goto out4;
4051719ea2fbSAl Viro 	lock_mount_hash();
40522763d119SAl Viro 	umount_mnt(new_mnt);
40532763d119SAl Viro 	root_mp = unhash_mnt(root_mnt);  /* we'll need its mountpoint */
40545ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
40555ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
40565ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
40575ff9d8a6SEric W. Biederman 	}
40584ac91378SJan Blunck 	/* mount old root on put_old */
405984d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
40604ac91378SJan Blunck 	/* mount new_root on / */
40612763d119SAl Viro 	attach_mnt(new_mnt, root_parent, root_mp);
40622763d119SAl Viro 	mnt_add_count(root_parent, -1);
40636b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
40644fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
40654fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
40663895dbf8SEric W. Biederman 	put_mountpoint(root_mp);
4067719ea2fbSAl Viro 	unlock_mount_hash();
40682d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
40691da177e4SLinus Torvalds 	error = 0;
4070b12cea91SAl Viro out4:
407184d17192SAl Viro 	unlock_mount(old_mp);
40722763d119SAl Viro 	if (!error)
40732763d119SAl Viro 		mntput_no_expire(ex_parent);
4074b12cea91SAl Viro out3:
40758c3ee42eSAl Viro 	path_put(&root);
4076b12cea91SAl Viro out2:
40772d8f3038SAl Viro 	path_put(&old);
40781da177e4SLinus Torvalds out1:
40792d8f3038SAl Viro 	path_put(&new);
40801da177e4SLinus Torvalds out0:
40811da177e4SLinus Torvalds 	return error;
40821da177e4SLinus Torvalds }
40831da177e4SLinus Torvalds 
40842a186721SChristian Brauner static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
40852a186721SChristian Brauner {
40862a186721SChristian Brauner 	unsigned int flags = mnt->mnt.mnt_flags;
40872a186721SChristian Brauner 
40882a186721SChristian Brauner 	/*  flags to clear */
40892a186721SChristian Brauner 	flags &= ~kattr->attr_clr;
40902a186721SChristian Brauner 	/* flags to raise */
40912a186721SChristian Brauner 	flags |= kattr->attr_set;
40922a186721SChristian Brauner 
40932a186721SChristian Brauner 	return flags;
40942a186721SChristian Brauner }
40952a186721SChristian Brauner 
40969caccd41SChristian Brauner static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
40979caccd41SChristian Brauner {
40989caccd41SChristian Brauner 	struct vfsmount *m = &mnt->mnt;
4099bd303368SChristian Brauner 	struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
41009caccd41SChristian Brauner 
4101256c8aedSChristian Brauner 	if (!kattr->mnt_idmap)
41029caccd41SChristian Brauner 		return 0;
41039caccd41SChristian Brauner 
41049caccd41SChristian Brauner 	/*
4105bd303368SChristian Brauner 	 * Creating an idmapped mount with the filesystem wide idmapping
4106bd303368SChristian Brauner 	 * doesn't make sense so block that. We don't allow mushy semantics.
4107bd303368SChristian Brauner 	 */
4108256c8aedSChristian Brauner 	if (mnt_idmap_owner(kattr->mnt_idmap) == fs_userns)
4109bd303368SChristian Brauner 		return -EINVAL;
4110bd303368SChristian Brauner 
4111bd303368SChristian Brauner 	/*
41129caccd41SChristian Brauner 	 * Once a mount has been idmapped we don't allow it to change its
41139caccd41SChristian Brauner 	 * mapping. It makes things simpler and callers can just create
41149caccd41SChristian Brauner 	 * another bind-mount they can idmap if they want to.
41159caccd41SChristian Brauner 	 */
4116bb49e9e7SChristian Brauner 	if (is_idmapped_mnt(m))
41179caccd41SChristian Brauner 		return -EPERM;
41189caccd41SChristian Brauner 
41199caccd41SChristian Brauner 	/* The underlying filesystem doesn't support idmapped mounts yet. */
41209caccd41SChristian Brauner 	if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
41219caccd41SChristian Brauner 		return -EINVAL;
41229caccd41SChristian Brauner 
41239caccd41SChristian Brauner 	/* We're not controlling the superblock. */
4124bd303368SChristian Brauner 	if (!ns_capable(fs_userns, CAP_SYS_ADMIN))
41259caccd41SChristian Brauner 		return -EPERM;
41269caccd41SChristian Brauner 
41279caccd41SChristian Brauner 	/* Mount has already been visible in the filesystem hierarchy. */
41289caccd41SChristian Brauner 	if (!is_anon_ns(mnt->mnt_ns))
41299caccd41SChristian Brauner 		return -EINVAL;
41309caccd41SChristian Brauner 
41319caccd41SChristian Brauner 	return 0;
41329caccd41SChristian Brauner }
41339caccd41SChristian Brauner 
4134a26f788bSChristian Brauner /**
4135a26f788bSChristian Brauner  * mnt_allow_writers() - check whether the attribute change allows writers
4136a26f788bSChristian Brauner  * @kattr: the new mount attributes
4137a26f788bSChristian Brauner  * @mnt: the mount to which @kattr will be applied
4138a26f788bSChristian Brauner  *
4139a26f788bSChristian Brauner  * Check whether thew new mount attributes in @kattr allow concurrent writers.
4140a26f788bSChristian Brauner  *
4141a26f788bSChristian Brauner  * Return: true if writers need to be held, false if not
4142a26f788bSChristian Brauner  */
4143a26f788bSChristian Brauner static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
4144a26f788bSChristian Brauner 				     const struct mount *mnt)
41452a186721SChristian Brauner {
4146e1bbcd27SChristian Brauner 	return (!(kattr->attr_set & MNT_READONLY) ||
4147e1bbcd27SChristian Brauner 		(mnt->mnt.mnt_flags & MNT_READONLY)) &&
4148256c8aedSChristian Brauner 	       !kattr->mnt_idmap;
41492a186721SChristian Brauner }
41502a186721SChristian Brauner 
415187bb5b60SChristian Brauner static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
41522a186721SChristian Brauner {
4153e257039fSAl Viro 	struct mount *m;
4154e257039fSAl Viro 	int err;
41552a186721SChristian Brauner 
4156e257039fSAl Viro 	for (m = mnt; m; m = next_mnt(m, mnt)) {
4157e257039fSAl Viro 		if (!can_change_locked_flags(m, recalc_flags(kattr, m))) {
4158e257039fSAl Viro 			err = -EPERM;
4159e257039fSAl Viro 			break;
41602a186721SChristian Brauner 		}
41612a186721SChristian Brauner 
416287bb5b60SChristian Brauner 		err = can_idmap_mount(kattr, m);
416387bb5b60SChristian Brauner 		if (err)
4164e257039fSAl Viro 			break;
41652a186721SChristian Brauner 
4166e257039fSAl Viro 		if (!mnt_allow_writers(kattr, m)) {
416787bb5b60SChristian Brauner 			err = mnt_hold_writers(m);
416887bb5b60SChristian Brauner 			if (err)
4169e257039fSAl Viro 				break;
41702a186721SChristian Brauner 		}
41712a186721SChristian Brauner 
4172e257039fSAl Viro 		if (!kattr->recurse)
417387bb5b60SChristian Brauner 			return 0;
41742a186721SChristian Brauner 	}
41752a186721SChristian Brauner 
4176e257039fSAl Viro 	if (err) {
4177e257039fSAl Viro 		struct mount *p;
4178e257039fSAl Viro 
41790014edaeSChristian Brauner 		/*
41800014edaeSChristian Brauner 		 * If we had to call mnt_hold_writers() MNT_WRITE_HOLD will
41810014edaeSChristian Brauner 		 * be set in @mnt_flags. The loop unsets MNT_WRITE_HOLD for all
41820014edaeSChristian Brauner 		 * mounts and needs to take care to include the first mount.
41830014edaeSChristian Brauner 		 */
41840014edaeSChristian Brauner 		for (p = mnt; p; p = next_mnt(p, mnt)) {
4185e257039fSAl Viro 			/* If we had to hold writers unblock them. */
4186e257039fSAl Viro 			if (p->mnt.mnt_flags & MNT_WRITE_HOLD)
4187e257039fSAl Viro 				mnt_unhold_writers(p);
41880014edaeSChristian Brauner 
41890014edaeSChristian Brauner 			/*
41900014edaeSChristian Brauner 			 * We're done once the first mount we changed got
41910014edaeSChristian Brauner 			 * MNT_WRITE_HOLD unset.
41920014edaeSChristian Brauner 			 */
41930014edaeSChristian Brauner 			if (p == m)
41940014edaeSChristian Brauner 				break;
4195e257039fSAl Viro 		}
4196e257039fSAl Viro 	}
4197e257039fSAl Viro 	return err;
41982a186721SChristian Brauner }
41992a186721SChristian Brauner 
42009caccd41SChristian Brauner static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
42019caccd41SChristian Brauner {
4202256c8aedSChristian Brauner 	if (!kattr->mnt_idmap)
42039caccd41SChristian Brauner 		return;
42049caccd41SChristian Brauner 
4205bd303368SChristian Brauner 	/*
4206256c8aedSChristian Brauner 	 * Pairs with smp_load_acquire() in mnt_idmap().
4207256c8aedSChristian Brauner 	 *
4208256c8aedSChristian Brauner 	 * Since we only allow a mount to change the idmapping once and
4209256c8aedSChristian Brauner 	 * verified this in can_idmap_mount() we know that the mount has
4210256c8aedSChristian Brauner 	 * @nop_mnt_idmap attached to it. So there's no need to drop any
4211256c8aedSChristian Brauner 	 * references.
4212bd303368SChristian Brauner 	 */
4213256c8aedSChristian Brauner 	smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap));
42149caccd41SChristian Brauner }
42159caccd41SChristian Brauner 
4216e257039fSAl Viro static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
42172a186721SChristian Brauner {
4218e257039fSAl Viro 	struct mount *m;
42192a186721SChristian Brauner 
4220e257039fSAl Viro 	for (m = mnt; m; m = next_mnt(m, mnt)) {
42212a186721SChristian Brauner 		unsigned int flags;
42222a186721SChristian Brauner 
42239caccd41SChristian Brauner 		do_idmap_mount(kattr, m);
42242a186721SChristian Brauner 		flags = recalc_flags(kattr, m);
42252a186721SChristian Brauner 		WRITE_ONCE(m->mnt.mnt_flags, flags);
42262a186721SChristian Brauner 
422703b6abeeSChristian Brauner 		/* If we had to hold writers unblock them. */
422803b6abeeSChristian Brauner 		if (m->mnt.mnt_flags & MNT_WRITE_HOLD)
42292a186721SChristian Brauner 			mnt_unhold_writers(m);
42302a186721SChristian Brauner 
4231e257039fSAl Viro 		if (kattr->propagation)
42322a186721SChristian Brauner 			change_mnt_propagation(m, kattr->propagation);
4233e257039fSAl Viro 		if (!kattr->recurse)
42342a186721SChristian Brauner 			break;
4235e257039fSAl Viro 	}
42362a186721SChristian Brauner 	touch_mnt_namespace(mnt->mnt_ns);
42372a186721SChristian Brauner }
42382a186721SChristian Brauner 
42392a186721SChristian Brauner static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
42402a186721SChristian Brauner {
424187bb5b60SChristian Brauner 	struct mount *mnt = real_mount(path->mnt);
42422a186721SChristian Brauner 	int err = 0;
42432a186721SChristian Brauner 
42442a186721SChristian Brauner 	if (path->dentry != mnt->mnt.mnt_root)
42452a186721SChristian Brauner 		return -EINVAL;
42462a186721SChristian Brauner 
4247256c8aedSChristian Brauner 	if (kattr->mnt_userns) {
4248256c8aedSChristian Brauner 		struct mnt_idmap *mnt_idmap;
4249256c8aedSChristian Brauner 
4250256c8aedSChristian Brauner 		mnt_idmap = alloc_mnt_idmap(kattr->mnt_userns);
4251256c8aedSChristian Brauner 		if (IS_ERR(mnt_idmap))
4252256c8aedSChristian Brauner 			return PTR_ERR(mnt_idmap);
4253256c8aedSChristian Brauner 		kattr->mnt_idmap = mnt_idmap;
4254256c8aedSChristian Brauner 	}
4255256c8aedSChristian Brauner 
42562a186721SChristian Brauner 	if (kattr->propagation) {
42572a186721SChristian Brauner 		/*
42582a186721SChristian Brauner 		 * Only take namespace_lock() if we're actually changing
42592a186721SChristian Brauner 		 * propagation.
42602a186721SChristian Brauner 		 */
42612a186721SChristian Brauner 		namespace_lock();
42622a186721SChristian Brauner 		if (kattr->propagation == MS_SHARED) {
42632a186721SChristian Brauner 			err = invent_group_ids(mnt, kattr->recurse);
42642a186721SChristian Brauner 			if (err) {
42652a186721SChristian Brauner 				namespace_unlock();
42662a186721SChristian Brauner 				return err;
42672a186721SChristian Brauner 			}
42682a186721SChristian Brauner 		}
42692a186721SChristian Brauner 	}
42702a186721SChristian Brauner 
427187bb5b60SChristian Brauner 	err = -EINVAL;
42722a186721SChristian Brauner 	lock_mount_hash();
42732a186721SChristian Brauner 
427487bb5b60SChristian Brauner 	/* Ensure that this isn't anything purely vfs internal. */
427587bb5b60SChristian Brauner 	if (!is_mounted(&mnt->mnt))
427687bb5b60SChristian Brauner 		goto out;
42772a186721SChristian Brauner 
427887bb5b60SChristian Brauner 	/*
427987bb5b60SChristian Brauner 	 * If this is an attached mount make sure it's located in the callers
428087bb5b60SChristian Brauner 	 * mount namespace. If it's not don't let the caller interact with it.
428187bb5b60SChristian Brauner 	 * If this is a detached mount make sure it has an anonymous mount
428287bb5b60SChristian Brauner 	 * namespace attached to it, i.e. we've created it via OPEN_TREE_CLONE.
428387bb5b60SChristian Brauner 	 */
428487bb5b60SChristian Brauner 	if (!(mnt_has_parent(mnt) ? check_mnt(mnt) : is_anon_ns(mnt->mnt_ns)))
428587bb5b60SChristian Brauner 		goto out;
428687bb5b60SChristian Brauner 
428787bb5b60SChristian Brauner 	/*
428887bb5b60SChristian Brauner 	 * First, we get the mount tree in a shape where we can change mount
428987bb5b60SChristian Brauner 	 * properties without failure. If we succeeded to do so we commit all
429087bb5b60SChristian Brauner 	 * changes and if we failed we clean up.
429187bb5b60SChristian Brauner 	 */
429287bb5b60SChristian Brauner 	err = mount_setattr_prepare(kattr, mnt);
4293e257039fSAl Viro 	if (!err)
4294e257039fSAl Viro 		mount_setattr_commit(kattr, mnt);
429587bb5b60SChristian Brauner 
429687bb5b60SChristian Brauner out:
42972a186721SChristian Brauner 	unlock_mount_hash();
42982a186721SChristian Brauner 
42992a186721SChristian Brauner 	if (kattr->propagation) {
43002a186721SChristian Brauner 		namespace_unlock();
43012a186721SChristian Brauner 		if (err)
43022a186721SChristian Brauner 			cleanup_group_ids(mnt, NULL);
43032a186721SChristian Brauner 	}
43042a186721SChristian Brauner 
43052a186721SChristian Brauner 	return err;
43062a186721SChristian Brauner }
43072a186721SChristian Brauner 
43089caccd41SChristian Brauner static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
43099caccd41SChristian Brauner 				struct mount_kattr *kattr, unsigned int flags)
43109caccd41SChristian Brauner {
43119caccd41SChristian Brauner 	int err = 0;
43129caccd41SChristian Brauner 	struct ns_common *ns;
43139caccd41SChristian Brauner 	struct user_namespace *mnt_userns;
43149caccd41SChristian Brauner 	struct file *file;
43159caccd41SChristian Brauner 
43169caccd41SChristian Brauner 	if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
43179caccd41SChristian Brauner 		return 0;
43189caccd41SChristian Brauner 
43199caccd41SChristian Brauner 	/*
43209caccd41SChristian Brauner 	 * We currently do not support clearing an idmapped mount. If this ever
43219caccd41SChristian Brauner 	 * is a use-case we can revisit this but for now let's keep it simple
43229caccd41SChristian Brauner 	 * and not allow it.
43239caccd41SChristian Brauner 	 */
43249caccd41SChristian Brauner 	if (attr->attr_clr & MOUNT_ATTR_IDMAP)
43259caccd41SChristian Brauner 		return -EINVAL;
43269caccd41SChristian Brauner 
43279caccd41SChristian Brauner 	if (attr->userns_fd > INT_MAX)
43289caccd41SChristian Brauner 		return -EINVAL;
43299caccd41SChristian Brauner 
43309caccd41SChristian Brauner 	file = fget(attr->userns_fd);
43319caccd41SChristian Brauner 	if (!file)
43329caccd41SChristian Brauner 		return -EBADF;
43339caccd41SChristian Brauner 
43349caccd41SChristian Brauner 	if (!proc_ns_file(file)) {
43359caccd41SChristian Brauner 		err = -EINVAL;
43369caccd41SChristian Brauner 		goto out_fput;
43379caccd41SChristian Brauner 	}
43389caccd41SChristian Brauner 
43399caccd41SChristian Brauner 	ns = get_proc_ns(file_inode(file));
43409caccd41SChristian Brauner 	if (ns->ops->type != CLONE_NEWUSER) {
43419caccd41SChristian Brauner 		err = -EINVAL;
43429caccd41SChristian Brauner 		goto out_fput;
43439caccd41SChristian Brauner 	}
43449caccd41SChristian Brauner 
43459caccd41SChristian Brauner 	/*
4346bd303368SChristian Brauner 	 * The initial idmapping cannot be used to create an idmapped
4347bd303368SChristian Brauner 	 * mount. We use the initial idmapping as an indicator of a mount
4348bd303368SChristian Brauner 	 * that is not idmapped. It can simply be passed into helpers that
4349bd303368SChristian Brauner 	 * are aware of idmapped mounts as a convenient shortcut. A user
4350bd303368SChristian Brauner 	 * can just create a dedicated identity mapping to achieve the same
4351bd303368SChristian Brauner 	 * result.
43529caccd41SChristian Brauner 	 */
43539caccd41SChristian Brauner 	mnt_userns = container_of(ns, struct user_namespace, ns);
4354bd303368SChristian Brauner 	if (initial_idmapping(mnt_userns)) {
43559caccd41SChristian Brauner 		err = -EPERM;
43569caccd41SChristian Brauner 		goto out_fput;
43579caccd41SChristian Brauner 	}
4358bf1ac16eSSeth Forshee 
4359bf1ac16eSSeth Forshee 	/* We're not controlling the target namespace. */
4360bf1ac16eSSeth Forshee 	if (!ns_capable(mnt_userns, CAP_SYS_ADMIN)) {
4361bf1ac16eSSeth Forshee 		err = -EPERM;
4362bf1ac16eSSeth Forshee 		goto out_fput;
4363bf1ac16eSSeth Forshee 	}
4364bf1ac16eSSeth Forshee 
43659caccd41SChristian Brauner 	kattr->mnt_userns = get_user_ns(mnt_userns);
43669caccd41SChristian Brauner 
43679caccd41SChristian Brauner out_fput:
43689caccd41SChristian Brauner 	fput(file);
43699caccd41SChristian Brauner 	return err;
43709caccd41SChristian Brauner }
43719caccd41SChristian Brauner 
43729caccd41SChristian Brauner static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
43732a186721SChristian Brauner 			     struct mount_kattr *kattr, unsigned int flags)
43742a186721SChristian Brauner {
43752a186721SChristian Brauner 	unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
43762a186721SChristian Brauner 
43772a186721SChristian Brauner 	if (flags & AT_NO_AUTOMOUNT)
43782a186721SChristian Brauner 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
43792a186721SChristian Brauner 	if (flags & AT_SYMLINK_NOFOLLOW)
43802a186721SChristian Brauner 		lookup_flags &= ~LOOKUP_FOLLOW;
43812a186721SChristian Brauner 	if (flags & AT_EMPTY_PATH)
43822a186721SChristian Brauner 		lookup_flags |= LOOKUP_EMPTY;
43832a186721SChristian Brauner 
43842a186721SChristian Brauner 	*kattr = (struct mount_kattr) {
43852a186721SChristian Brauner 		.lookup_flags	= lookup_flags,
43862a186721SChristian Brauner 		.recurse	= !!(flags & AT_RECURSIVE),
43872a186721SChristian Brauner 	};
43882a186721SChristian Brauner 
43892a186721SChristian Brauner 	if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
43902a186721SChristian Brauner 		return -EINVAL;
43912a186721SChristian Brauner 	if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
43922a186721SChristian Brauner 		return -EINVAL;
43932a186721SChristian Brauner 	kattr->propagation = attr->propagation;
43942a186721SChristian Brauner 
43952a186721SChristian Brauner 	if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
43962a186721SChristian Brauner 		return -EINVAL;
43972a186721SChristian Brauner 
43982a186721SChristian Brauner 	kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
43992a186721SChristian Brauner 	kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
44002a186721SChristian Brauner 
44012a186721SChristian Brauner 	/*
44022a186721SChristian Brauner 	 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
44032a186721SChristian Brauner 	 * users wanting to transition to a different atime setting cannot
44042a186721SChristian Brauner 	 * simply specify the atime setting in @attr_set, but must also
44052a186721SChristian Brauner 	 * specify MOUNT_ATTR__ATIME in the @attr_clr field.
44062a186721SChristian Brauner 	 * So ensure that MOUNT_ATTR__ATIME can't be partially set in
44072a186721SChristian Brauner 	 * @attr_clr and that @attr_set can't have any atime bits set if
44082a186721SChristian Brauner 	 * MOUNT_ATTR__ATIME isn't set in @attr_clr.
44092a186721SChristian Brauner 	 */
44102a186721SChristian Brauner 	if (attr->attr_clr & MOUNT_ATTR__ATIME) {
44112a186721SChristian Brauner 		if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
44122a186721SChristian Brauner 			return -EINVAL;
44132a186721SChristian Brauner 
44142a186721SChristian Brauner 		/*
44152a186721SChristian Brauner 		 * Clear all previous time settings as they are mutually
44162a186721SChristian Brauner 		 * exclusive.
44172a186721SChristian Brauner 		 */
44182a186721SChristian Brauner 		kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
44192a186721SChristian Brauner 		switch (attr->attr_set & MOUNT_ATTR__ATIME) {
44202a186721SChristian Brauner 		case MOUNT_ATTR_RELATIME:
44212a186721SChristian Brauner 			kattr->attr_set |= MNT_RELATIME;
44222a186721SChristian Brauner 			break;
44232a186721SChristian Brauner 		case MOUNT_ATTR_NOATIME:
44242a186721SChristian Brauner 			kattr->attr_set |= MNT_NOATIME;
44252a186721SChristian Brauner 			break;
44262a186721SChristian Brauner 		case MOUNT_ATTR_STRICTATIME:
44272a186721SChristian Brauner 			break;
44282a186721SChristian Brauner 		default:
44292a186721SChristian Brauner 			return -EINVAL;
44302a186721SChristian Brauner 		}
44312a186721SChristian Brauner 	} else {
44322a186721SChristian Brauner 		if (attr->attr_set & MOUNT_ATTR__ATIME)
44332a186721SChristian Brauner 			return -EINVAL;
44342a186721SChristian Brauner 	}
44352a186721SChristian Brauner 
44369caccd41SChristian Brauner 	return build_mount_idmapped(attr, usize, kattr, flags);
44379caccd41SChristian Brauner }
44389caccd41SChristian Brauner 
44399caccd41SChristian Brauner static void finish_mount_kattr(struct mount_kattr *kattr)
44409caccd41SChristian Brauner {
44419caccd41SChristian Brauner 	put_user_ns(kattr->mnt_userns);
44429caccd41SChristian Brauner 	kattr->mnt_userns = NULL;
4443256c8aedSChristian Brauner 
4444256c8aedSChristian Brauner 	if (kattr->mnt_idmap)
4445256c8aedSChristian Brauner 		mnt_idmap_put(kattr->mnt_idmap);
44462a186721SChristian Brauner }
44472a186721SChristian Brauner 
44482a186721SChristian Brauner SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
44492a186721SChristian Brauner 		unsigned int, flags, struct mount_attr __user *, uattr,
44502a186721SChristian Brauner 		size_t, usize)
44512a186721SChristian Brauner {
44522a186721SChristian Brauner 	int err;
44532a186721SChristian Brauner 	struct path target;
44542a186721SChristian Brauner 	struct mount_attr attr;
44552a186721SChristian Brauner 	struct mount_kattr kattr;
44562a186721SChristian Brauner 
44572a186721SChristian Brauner 	BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
44582a186721SChristian Brauner 
44592a186721SChristian Brauner 	if (flags & ~(AT_EMPTY_PATH |
44602a186721SChristian Brauner 		      AT_RECURSIVE |
44612a186721SChristian Brauner 		      AT_SYMLINK_NOFOLLOW |
44622a186721SChristian Brauner 		      AT_NO_AUTOMOUNT))
44632a186721SChristian Brauner 		return -EINVAL;
44642a186721SChristian Brauner 
44652a186721SChristian Brauner 	if (unlikely(usize > PAGE_SIZE))
44662a186721SChristian Brauner 		return -E2BIG;
44672a186721SChristian Brauner 	if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
44682a186721SChristian Brauner 		return -EINVAL;
44692a186721SChristian Brauner 
44702a186721SChristian Brauner 	if (!may_mount())
44712a186721SChristian Brauner 		return -EPERM;
44722a186721SChristian Brauner 
44732a186721SChristian Brauner 	err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
44742a186721SChristian Brauner 	if (err)
44752a186721SChristian Brauner 		return err;
44762a186721SChristian Brauner 
44772a186721SChristian Brauner 	/* Don't bother walking through the mounts if this is a nop. */
44782a186721SChristian Brauner 	if (attr.attr_set == 0 &&
44792a186721SChristian Brauner 	    attr.attr_clr == 0 &&
44802a186721SChristian Brauner 	    attr.propagation == 0)
44812a186721SChristian Brauner 		return 0;
44822a186721SChristian Brauner 
44839caccd41SChristian Brauner 	err = build_mount_kattr(&attr, usize, &kattr, flags);
44842a186721SChristian Brauner 	if (err)
44852a186721SChristian Brauner 		return err;
44862a186721SChristian Brauner 
44872a186721SChristian Brauner 	err = user_path_at(dfd, path, kattr.lookup_flags, &target);
4488012e3322SChristian Brauner 	if (!err) {
44892a186721SChristian Brauner 		err = do_mount_setattr(&target, &kattr);
44902a186721SChristian Brauner 		path_put(&target);
4491012e3322SChristian Brauner 	}
4492012e3322SChristian Brauner 	finish_mount_kattr(&kattr);
44932a186721SChristian Brauner 	return err;
44942a186721SChristian Brauner }
44952a186721SChristian Brauner 
44961da177e4SLinus Torvalds static void __init init_mount_tree(void)
44971da177e4SLinus Torvalds {
44981da177e4SLinus Torvalds 	struct vfsmount *mnt;
449974e83122SAl Viro 	struct mount *m;
45006b3286edSKirill Korotaev 	struct mnt_namespace *ns;
4501ac748a09SJan Blunck 	struct path root;
45021da177e4SLinus Torvalds 
4503fd3e007fSAl Viro 	mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
45041da177e4SLinus Torvalds 	if (IS_ERR(mnt))
45051da177e4SLinus Torvalds 		panic("Can't create rootfs");
4506b3e19d92SNick Piggin 
450774e83122SAl Viro 	ns = alloc_mnt_ns(&init_user_ns, false);
45083b22edc5STrond Myklebust 	if (IS_ERR(ns))
45091da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
451074e83122SAl Viro 	m = real_mount(mnt);
451174e83122SAl Viro 	m->mnt_ns = ns;
451274e83122SAl Viro 	ns->root = m;
451374e83122SAl Viro 	ns->mounts = 1;
451474e83122SAl Viro 	list_add(&m->mnt_list, &ns->list);
45156b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
45166b3286edSKirill Korotaev 	get_mnt_ns(ns);
45171da177e4SLinus Torvalds 
4518be08d6d2SAl Viro 	root.mnt = mnt;
4519be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
4520da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
4521ac748a09SJan Blunck 
4522ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
4523ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
45241da177e4SLinus Torvalds }
45251da177e4SLinus Torvalds 
452674bf17cfSDenis Cheng void __init mnt_init(void)
45271da177e4SLinus Torvalds {
452815a67dd8SRandy Dunlap 	int err;
45291da177e4SLinus Torvalds 
45307d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
453179f6540bSVasily Averin 			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
45321da177e4SLinus Torvalds 
45330818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
453438129a13SAl Viro 				sizeof(struct hlist_head),
45350818bf27SAl Viro 				mhash_entries, 19,
45363d375d78SPavel Tatashin 				HASH_ZERO,
45370818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
45380818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
45390818bf27SAl Viro 				sizeof(struct hlist_head),
45400818bf27SAl Viro 				mphash_entries, 19,
45413d375d78SPavel Tatashin 				HASH_ZERO,
45420818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
45431da177e4SLinus Torvalds 
454484d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
45451da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
45461da177e4SLinus Torvalds 
45474b93dc9bSTejun Heo 	kernfs_init();
45484b93dc9bSTejun Heo 
454915a67dd8SRandy Dunlap 	err = sysfs_init();
455015a67dd8SRandy Dunlap 	if (err)
455115a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
45528e24eea7SHarvey Harrison 			__func__, err);
455300d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
455400d26666SGreg Kroah-Hartman 	if (!fs_kobj)
45558e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
4556037f11b4SAl Viro 	shmem_init();
45571da177e4SLinus Torvalds 	init_rootfs();
45581da177e4SLinus Torvalds 	init_mount_tree();
45591da177e4SLinus Torvalds }
45601da177e4SLinus Torvalds 
4561616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
45621da177e4SLinus Torvalds {
45631a7b8969SKirill Tkhai 	if (!refcount_dec_and_test(&ns->ns.count))
4564616511d0STrond Myklebust 		return;
45657b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
4566771b1371SEric W. Biederman 	free_mnt_ns(ns);
45671da177e4SLinus Torvalds }
45689d412a43SAl Viro 
4569d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type)
45709d412a43SAl Viro {
4571423e0ab0STim Chen 	struct vfsmount *mnt;
4572d911b458SDavid Howells 	mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
4573423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
4574423e0ab0STim Chen 		/*
4575423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
4576423e0ab0STim Chen 		 * we unmount before file sys is unregistered
4577423e0ab0STim Chen 		*/
4578f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
4579423e0ab0STim Chen 	}
4580423e0ab0STim Chen 	return mnt;
45819d412a43SAl Viro }
4582d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount);
4583423e0ab0STim Chen 
4584423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
4585423e0ab0STim Chen {
4586423e0ab0STim Chen 	/* release long term mount so mount point can be released */
4587*da27f796SRik van Riel 	if (!IS_ERR(mnt)) {
4588*da27f796SRik van Riel 		mnt_make_shortterm(mnt);
458948a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
4590423e0ab0STim Chen 		mntput(mnt);
4591423e0ab0STim Chen 	}
4592423e0ab0STim Chen }
4593423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
459402125a82SAl Viro 
4595df820f8dSMiklos Szeredi void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
4596df820f8dSMiklos Szeredi {
4597df820f8dSMiklos Szeredi 	unsigned int i;
4598df820f8dSMiklos Szeredi 
4599df820f8dSMiklos Szeredi 	for (i = 0; i < num; i++)
4600*da27f796SRik van Riel 		mnt_make_shortterm(mnt[i]);
4601df820f8dSMiklos Szeredi 	synchronize_rcu_expedited();
4602df820f8dSMiklos Szeredi 	for (i = 0; i < num; i++)
4603df820f8dSMiklos Szeredi 		mntput(mnt[i]);
4604df820f8dSMiklos Szeredi }
4605df820f8dSMiklos Szeredi EXPORT_SYMBOL(kern_unmount_array);
4606df820f8dSMiklos Szeredi 
460702125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
460802125a82SAl Viro {
4609143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
461002125a82SAl Viro }
46118823c079SEric W. Biederman 
46123151527eSEric W. Biederman bool current_chrooted(void)
46133151527eSEric W. Biederman {
46143151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
46153151527eSEric W. Biederman 	struct path ns_root;
46163151527eSEric W. Biederman 	struct path fs_root;
46173151527eSEric W. Biederman 	bool chrooted;
46183151527eSEric W. Biederman 
46193151527eSEric W. Biederman 	/* Find the namespace root */
46203151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
46213151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
46223151527eSEric W. Biederman 	path_get(&ns_root);
46233151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
46243151527eSEric W. Biederman 		;
46253151527eSEric W. Biederman 
46263151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
46273151527eSEric W. Biederman 
46283151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
46293151527eSEric W. Biederman 
46303151527eSEric W. Biederman 	path_put(&fs_root);
46313151527eSEric W. Biederman 	path_put(&ns_root);
46323151527eSEric W. Biederman 
46333151527eSEric W. Biederman 	return chrooted;
46343151527eSEric W. Biederman }
46353151527eSEric W. Biederman 
4636132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns,
4637132e4608SDavid Howells 				const struct super_block *sb,
46388654df4eSEric W. Biederman 				int *new_mnt_flags)
463987a8ebd6SEric W. Biederman {
46408c6cf9ccSEric W. Biederman 	int new_flags = *new_mnt_flags;
464187a8ebd6SEric W. Biederman 	struct mount *mnt;
4642e51db735SEric W. Biederman 	bool visible = false;
464387a8ebd6SEric W. Biederman 
464444bb4385SAl Viro 	down_read(&namespace_sem);
46459f6c61f9SMiklos Szeredi 	lock_ns_list(ns);
464687a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
4647e51db735SEric W. Biederman 		struct mount *child;
464877b1a97dSEric W. Biederman 		int mnt_flags;
464977b1a97dSEric W. Biederman 
46509f6c61f9SMiklos Szeredi 		if (mnt_is_cursor(mnt))
46519f6c61f9SMiklos Szeredi 			continue;
46529f6c61f9SMiklos Szeredi 
4653132e4608SDavid Howells 		if (mnt->mnt.mnt_sb->s_type != sb->s_type)
4654e51db735SEric W. Biederman 			continue;
4655e51db735SEric W. Biederman 
46567e96c1b0SEric W. Biederman 		/* This mount is not fully visible if it's root directory
46577e96c1b0SEric W. Biederman 		 * is not the root directory of the filesystem.
46587e96c1b0SEric W. Biederman 		 */
46597e96c1b0SEric W. Biederman 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
46607e96c1b0SEric W. Biederman 			continue;
46617e96c1b0SEric W. Biederman 
4662a1935c17SEric W. Biederman 		/* A local view of the mount flags */
466377b1a97dSEric W. Biederman 		mnt_flags = mnt->mnt.mnt_flags;
466477b1a97dSEric W. Biederman 
4665695e9df0SEric W. Biederman 		/* Don't miss readonly hidden in the superblock flags */
4666bc98a42cSDavid Howells 		if (sb_rdonly(mnt->mnt.mnt_sb))
4667695e9df0SEric W. Biederman 			mnt_flags |= MNT_LOCK_READONLY;
4668695e9df0SEric W. Biederman 
46698c6cf9ccSEric W. Biederman 		/* Verify the mount flags are equal to or more permissive
46708c6cf9ccSEric W. Biederman 		 * than the proposed new mount.
46718c6cf9ccSEric W. Biederman 		 */
467277b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_READONLY) &&
46738c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_READONLY))
46748c6cf9ccSEric W. Biederman 			continue;
467577b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_ATIME) &&
467677b1a97dSEric W. Biederman 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
46778c6cf9ccSEric W. Biederman 			continue;
46788c6cf9ccSEric W. Biederman 
4679ceeb0e5dSEric W. Biederman 		/* This mount is not fully visible if there are any
4680ceeb0e5dSEric W. Biederman 		 * locked child mounts that cover anything except for
4681ceeb0e5dSEric W. Biederman 		 * empty directories.
4682e51db735SEric W. Biederman 		 */
4683e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
4684e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
4685ceeb0e5dSEric W. Biederman 			/* Only worry about locked mounts */
4686d71ed6c9SEric W. Biederman 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
4687ceeb0e5dSEric W. Biederman 				continue;
46887236c85eSEric W. Biederman 			/* Is the directory permanetly empty? */
46897236c85eSEric W. Biederman 			if (!is_empty_dir_inode(inode))
4690e51db735SEric W. Biederman 				goto next;
469187a8ebd6SEric W. Biederman 		}
46928c6cf9ccSEric W. Biederman 		/* Preserve the locked attributes */
469377b1a97dSEric W. Biederman 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
46948c6cf9ccSEric W. Biederman 					       MNT_LOCK_ATIME);
4695e51db735SEric W. Biederman 		visible = true;
4696e51db735SEric W. Biederman 		goto found;
4697e51db735SEric W. Biederman 	next:	;
469887a8ebd6SEric W. Biederman 	}
4699e51db735SEric W. Biederman found:
47009f6c61f9SMiklos Szeredi 	unlock_ns_list(ns);
470144bb4385SAl Viro 	up_read(&namespace_sem);
4702e51db735SEric W. Biederman 	return visible;
470387a8ebd6SEric W. Biederman }
470487a8ebd6SEric W. Biederman 
4705132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
47068654df4eSEric W. Biederman {
4707a1935c17SEric W. Biederman 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
47088654df4eSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
47098654df4eSEric W. Biederman 	unsigned long s_iflags;
47108654df4eSEric W. Biederman 
47118654df4eSEric W. Biederman 	if (ns->user_ns == &init_user_ns)
47128654df4eSEric W. Biederman 		return false;
47138654df4eSEric W. Biederman 
47148654df4eSEric W. Biederman 	/* Can this filesystem be too revealing? */
4715132e4608SDavid Howells 	s_iflags = sb->s_iflags;
47168654df4eSEric W. Biederman 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
47178654df4eSEric W. Biederman 		return false;
47188654df4eSEric W. Biederman 
4719a1935c17SEric W. Biederman 	if ((s_iflags & required_iflags) != required_iflags) {
4720a1935c17SEric W. Biederman 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
4721a1935c17SEric W. Biederman 			  required_iflags);
4722a1935c17SEric W. Biederman 		return true;
4723a1935c17SEric W. Biederman 	}
4724a1935c17SEric W. Biederman 
4725132e4608SDavid Howells 	return !mnt_already_visible(ns, sb, new_mnt_flags);
47268654df4eSEric W. Biederman }
47278654df4eSEric W. Biederman 
4728380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt)
4729380cf5baSAndy Lutomirski {
4730380cf5baSAndy Lutomirski 	/*
4731380cf5baSAndy Lutomirski 	 * Foreign mounts (accessed via fchdir or through /proc
4732380cf5baSAndy Lutomirski 	 * symlinks) are always treated as if they are nosuid.  This
4733380cf5baSAndy Lutomirski 	 * prevents namespaces from trusting potentially unsafe
4734380cf5baSAndy Lutomirski 	 * suid/sgid bits, file caps, or security labels that originate
4735380cf5baSAndy Lutomirski 	 * in other namespaces.
4736380cf5baSAndy Lutomirski 	 */
4737380cf5baSAndy Lutomirski 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
4738380cf5baSAndy Lutomirski 	       current_in_userns(mnt->mnt_sb->s_user_ns);
4739380cf5baSAndy Lutomirski }
4740380cf5baSAndy Lutomirski 
474164964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
47428823c079SEric W. Biederman {
474358be2825SAl Viro 	struct ns_common *ns = NULL;
47448823c079SEric W. Biederman 	struct nsproxy *nsproxy;
47458823c079SEric W. Biederman 
4746728dba3aSEric W. Biederman 	task_lock(task);
4747728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
47488823c079SEric W. Biederman 	if (nsproxy) {
474958be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
475058be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
47518823c079SEric W. Biederman 	}
4752728dba3aSEric W. Biederman 	task_unlock(task);
47538823c079SEric W. Biederman 
47548823c079SEric W. Biederman 	return ns;
47558823c079SEric W. Biederman }
47568823c079SEric W. Biederman 
475764964528SAl Viro static void mntns_put(struct ns_common *ns)
47588823c079SEric W. Biederman {
475958be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
47608823c079SEric W. Biederman }
47618823c079SEric W. Biederman 
4762f2a8d52eSChristian Brauner static int mntns_install(struct nsset *nsset, struct ns_common *ns)
47638823c079SEric W. Biederman {
4764f2a8d52eSChristian Brauner 	struct nsproxy *nsproxy = nsset->nsproxy;
4765f2a8d52eSChristian Brauner 	struct fs_struct *fs = nsset->fs;
47664f757f3cSAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
4767f2a8d52eSChristian Brauner 	struct user_namespace *user_ns = nsset->cred->user_ns;
47688823c079SEric W. Biederman 	struct path root;
47694f757f3cSAl Viro 	int err;
47708823c079SEric W. Biederman 
47710c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
4772f2a8d52eSChristian Brauner 	    !ns_capable(user_ns, CAP_SYS_CHROOT) ||
4773f2a8d52eSChristian Brauner 	    !ns_capable(user_ns, CAP_SYS_ADMIN))
4774ae11e0f1SZhao Hongjiang 		return -EPERM;
47758823c079SEric W. Biederman 
477674e83122SAl Viro 	if (is_anon_ns(mnt_ns))
477774e83122SAl Viro 		return -EINVAL;
477874e83122SAl Viro 
47798823c079SEric W. Biederman 	if (fs->users != 1)
47808823c079SEric W. Biederman 		return -EINVAL;
47818823c079SEric W. Biederman 
47828823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
47834f757f3cSAl Viro 	old_mnt_ns = nsproxy->mnt_ns;
47848823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
47858823c079SEric W. Biederman 
47868823c079SEric W. Biederman 	/* Find the root */
47874f757f3cSAl Viro 	err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
47884f757f3cSAl Viro 				"/", LOOKUP_DOWN, &root);
47894f757f3cSAl Viro 	if (err) {
47904f757f3cSAl Viro 		/* revert to old namespace */
47914f757f3cSAl Viro 		nsproxy->mnt_ns = old_mnt_ns;
47924f757f3cSAl Viro 		put_mnt_ns(mnt_ns);
47934f757f3cSAl Viro 		return err;
47944f757f3cSAl Viro 	}
47958823c079SEric W. Biederman 
47964068367cSAndrei Vagin 	put_mnt_ns(old_mnt_ns);
47974068367cSAndrei Vagin 
47988823c079SEric W. Biederman 	/* Update the pwd and root */
47998823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
48008823c079SEric W. Biederman 	set_fs_root(fs, &root);
48018823c079SEric W. Biederman 
48028823c079SEric W. Biederman 	path_put(&root);
48038823c079SEric W. Biederman 	return 0;
48048823c079SEric W. Biederman }
48058823c079SEric W. Biederman 
4806bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns)
4807bcac25a5SAndrey Vagin {
4808bcac25a5SAndrey Vagin 	return to_mnt_ns(ns)->user_ns;
4809bcac25a5SAndrey Vagin }
4810bcac25a5SAndrey Vagin 
48118823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
48128823c079SEric W. Biederman 	.name		= "mnt",
48138823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
48148823c079SEric W. Biederman 	.get		= mntns_get,
48158823c079SEric W. Biederman 	.put		= mntns_put,
48168823c079SEric W. Biederman 	.install	= mntns_install,
4817bcac25a5SAndrey Vagin 	.owner		= mntns_owner,
48188823c079SEric W. Biederman };
4819ab171b95SLuis Chamberlain 
4820ab171b95SLuis Chamberlain #ifdef CONFIG_SYSCTL
4821ab171b95SLuis Chamberlain static struct ctl_table fs_namespace_sysctls[] = {
4822ab171b95SLuis Chamberlain 	{
4823ab171b95SLuis Chamberlain 		.procname	= "mount-max",
4824ab171b95SLuis Chamberlain 		.data		= &sysctl_mount_max,
4825ab171b95SLuis Chamberlain 		.maxlen		= sizeof(unsigned int),
4826ab171b95SLuis Chamberlain 		.mode		= 0644,
4827ab171b95SLuis Chamberlain 		.proc_handler	= proc_dointvec_minmax,
4828ab171b95SLuis Chamberlain 		.extra1		= SYSCTL_ONE,
4829ab171b95SLuis Chamberlain 	},
4830ab171b95SLuis Chamberlain 	{ }
4831ab171b95SLuis Chamberlain };
4832ab171b95SLuis Chamberlain 
4833ab171b95SLuis Chamberlain static int __init init_fs_namespace_sysctls(void)
4834ab171b95SLuis Chamberlain {
4835ab171b95SLuis Chamberlain 	register_sysctl_init("fs", fs_namespace_sysctls);
4836ab171b95SLuis Chamberlain 	return 0;
4837ab171b95SLuis Chamberlain }
4838ab171b95SLuis Chamberlain fs_initcall(init_fs_namespace_sysctls);
4839ab171b95SLuis Chamberlain 
4840ab171b95SLuis Chamberlain #endif /* CONFIG_SYSCTL */
4841