xref: /openbmc/linux/fs/namespace.c (revision 5235d448)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namespace.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright Al Viro 2000, 2001
51da177e4SLinus Torvalds  *	Released under GPL v2.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Based on code from fs/super.c, copyright Linus Torvalds and others.
81da177e4SLinus Torvalds  * Heavily rewritten.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/syscalls.h>
12d10577a8SAl Viro #include <linux/export.h>
1316f7e0feSRandy Dunlap #include <linux/capability.h>
146b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
15771b1371SEric W. Biederman #include <linux/user_namespace.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
171da177e4SLinus Torvalds #include <linux/security.h>
1873cd49ecSMiklos Szeredi #include <linux/idr.h>
1957f150a5SRob Landley #include <linux/init.h>		/* init_rootfs */
20d10577a8SAl Viro #include <linux/fs_struct.h>	/* get_fs_root et.al. */
21d10577a8SAl Viro #include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
22d10577a8SAl Viro #include <linux/uaccess.h>
230bb80f24SDavid Howells #include <linux/proc_ns.h>
2420b4fb48SLinus Torvalds #include <linux/magic.h>
250818bf27SAl Viro #include <linux/bootmem.h>
269ea459e1SAl Viro #include <linux/task_work.h>
2707b20889SRam Pai #include "pnode.h"
28948730b0SAdrian Bunk #include "internal.h"
291da177e4SLinus Torvalds 
30d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */
31d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000;
32d2921684SEric W. Biederman 
330818bf27SAl Viro static unsigned int m_hash_mask __read_mostly;
340818bf27SAl Viro static unsigned int m_hash_shift __read_mostly;
350818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly;
360818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly;
370818bf27SAl Viro 
380818bf27SAl Viro static __initdata unsigned long mhash_entries;
390818bf27SAl Viro static int __init set_mhash_entries(char *str)
400818bf27SAl Viro {
410818bf27SAl Viro 	if (!str)
420818bf27SAl Viro 		return 0;
430818bf27SAl Viro 	mhash_entries = simple_strtoul(str, &str, 0);
440818bf27SAl Viro 	return 1;
450818bf27SAl Viro }
460818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries);
470818bf27SAl Viro 
480818bf27SAl Viro static __initdata unsigned long mphash_entries;
490818bf27SAl Viro static int __init set_mphash_entries(char *str)
500818bf27SAl Viro {
510818bf27SAl Viro 	if (!str)
520818bf27SAl Viro 		return 0;
530818bf27SAl Viro 	mphash_entries = simple_strtoul(str, &str, 0);
540818bf27SAl Viro 	return 1;
550818bf27SAl Viro }
560818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries);
5713f14b4dSEric Dumazet 
58c7999c36SAl Viro static u64 event;
5973cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
60719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
6199b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock);
62f21f6220SAl Viro static int mnt_id_start = 0;
63f21f6220SAl Viro static int mnt_group_start = 1;
645addc5ddSAl Viro 
6538129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly;
660818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly;
67e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
6859aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem);
691da177e4SLinus Torvalds 
70f87fd4c2SMiklos Szeredi /* /sys/fs */
7100d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
7200d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
73f87fd4c2SMiklos Szeredi 
7499b7db7bSNick Piggin /*
7599b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
7699b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
7799b7db7bSNick Piggin  * up the tree.
7899b7db7bSNick Piggin  *
7999b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
8099b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
8199b7db7bSNick Piggin  */
8248a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
8399b7db7bSNick Piggin 
8438129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
871da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
880818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
890818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
900818bf27SAl Viro }
910818bf27SAl Viro 
920818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
930818bf27SAl Viro {
940818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
950818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
960818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
99b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
10073cd49ecSMiklos Szeredi {
10173cd49ecSMiklos Szeredi 	int res;
10273cd49ecSMiklos Szeredi 
10373cd49ecSMiklos Szeredi retry:
10473cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
10599b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
10615169fe7SAl Viro 	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
107f21f6220SAl Viro 	if (!res)
10815169fe7SAl Viro 		mnt_id_start = mnt->mnt_id + 1;
10999b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
11073cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
11173cd49ecSMiklos Szeredi 		goto retry;
11273cd49ecSMiklos Szeredi 
11373cd49ecSMiklos Szeredi 	return res;
11473cd49ecSMiklos Szeredi }
11573cd49ecSMiklos Szeredi 
116b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
11773cd49ecSMiklos Szeredi {
11815169fe7SAl Viro 	int id = mnt->mnt_id;
11999b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
120f21f6220SAl Viro 	ida_remove(&mnt_id_ida, id);
121f21f6220SAl Viro 	if (mnt_id_start > id)
122f21f6220SAl Viro 		mnt_id_start = id;
12399b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
12473cd49ecSMiklos Szeredi }
12573cd49ecSMiklos Szeredi 
126719f5d7fSMiklos Szeredi /*
127719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
128719f5d7fSMiklos Szeredi  *
129719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
130719f5d7fSMiklos Szeredi  */
1314b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
132719f5d7fSMiklos Szeredi {
133f21f6220SAl Viro 	int res;
134f21f6220SAl Viro 
135719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
136719f5d7fSMiklos Szeredi 		return -ENOMEM;
137719f5d7fSMiklos Szeredi 
138f21f6220SAl Viro 	res = ida_get_new_above(&mnt_group_ida,
139f21f6220SAl Viro 				mnt_group_start,
14015169fe7SAl Viro 				&mnt->mnt_group_id);
141f21f6220SAl Viro 	if (!res)
14215169fe7SAl Viro 		mnt_group_start = mnt->mnt_group_id + 1;
143f21f6220SAl Viro 
144f21f6220SAl Viro 	return res;
145719f5d7fSMiklos Szeredi }
146719f5d7fSMiklos Szeredi 
147719f5d7fSMiklos Szeredi /*
148719f5d7fSMiklos Szeredi  * Release a peer group ID
149719f5d7fSMiklos Szeredi  */
1504b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
151719f5d7fSMiklos Szeredi {
15215169fe7SAl Viro 	int id = mnt->mnt_group_id;
153f21f6220SAl Viro 	ida_remove(&mnt_group_ida, id);
154f21f6220SAl Viro 	if (mnt_group_start > id)
155f21f6220SAl Viro 		mnt_group_start = id;
15615169fe7SAl Viro 	mnt->mnt_group_id = 0;
157719f5d7fSMiklos Szeredi }
158719f5d7fSMiklos Szeredi 
159b3e19d92SNick Piggin /*
160b3e19d92SNick Piggin  * vfsmount lock must be held for read
161b3e19d92SNick Piggin  */
16283adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
163b3e19d92SNick Piggin {
164b3e19d92SNick Piggin #ifdef CONFIG_SMP
16568e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
166b3e19d92SNick Piggin #else
167b3e19d92SNick Piggin 	preempt_disable();
16868e8a9feSAl Viro 	mnt->mnt_count += n;
169b3e19d92SNick Piggin 	preempt_enable();
170b3e19d92SNick Piggin #endif
171b3e19d92SNick Piggin }
172b3e19d92SNick Piggin 
173b3e19d92SNick Piggin /*
174b3e19d92SNick Piggin  * vfsmount lock must be held for write
175b3e19d92SNick Piggin  */
17683adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt)
177b3e19d92SNick Piggin {
178b3e19d92SNick Piggin #ifdef CONFIG_SMP
179f03c6599SAl Viro 	unsigned int count = 0;
180b3e19d92SNick Piggin 	int cpu;
181b3e19d92SNick Piggin 
182b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
18368e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
184b3e19d92SNick Piggin 	}
185b3e19d92SNick Piggin 
186b3e19d92SNick Piggin 	return count;
187b3e19d92SNick Piggin #else
18868e8a9feSAl Viro 	return mnt->mnt_count;
189b3e19d92SNick Piggin #endif
190b3e19d92SNick Piggin }
191b3e19d92SNick Piggin 
19287b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p)
19387b95ce0SAl Viro {
19487b95ce0SAl Viro 	struct mount *m = container_of(p, struct mount, mnt_umount);
19587b95ce0SAl Viro 	dput(m->mnt_ex_mountpoint);
19687b95ce0SAl Viro 	pin_remove(p);
19787b95ce0SAl Viro 	mntput(&m->mnt);
19887b95ce0SAl Viro }
19987b95ce0SAl Viro 
200b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
2011da177e4SLinus Torvalds {
202c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
203c63181e6SAl Viro 	if (mnt) {
20473cd49ecSMiklos Szeredi 		int err;
20573cd49ecSMiklos Szeredi 
206c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
20788b38782SLi Zefan 		if (err)
20888b38782SLi Zefan 			goto out_free_cache;
20988b38782SLi Zefan 
21088b38782SLi Zefan 		if (name) {
211fcc139aeSAndrzej Hajda 			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
212c63181e6SAl Viro 			if (!mnt->mnt_devname)
21388b38782SLi Zefan 				goto out_free_id;
21473cd49ecSMiklos Szeredi 		}
21573cd49ecSMiklos Szeredi 
216b3e19d92SNick Piggin #ifdef CONFIG_SMP
217c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
218c63181e6SAl Viro 		if (!mnt->mnt_pcp)
219b3e19d92SNick Piggin 			goto out_free_devname;
220b3e19d92SNick Piggin 
221c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
222b3e19d92SNick Piggin #else
223c63181e6SAl Viro 		mnt->mnt_count = 1;
224c63181e6SAl Viro 		mnt->mnt_writers = 0;
225b3e19d92SNick Piggin #endif
226b3e19d92SNick Piggin 
22738129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
228c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
229c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
230c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
231c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
232c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
233c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
234c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2350a5eb7c8SEric W. Biederman 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
2362504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY
2372504c5d6SAndreas Gruenbacher 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
2382504c5d6SAndreas Gruenbacher #endif
23987b95ce0SAl Viro 		init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
2401da177e4SLinus Torvalds 	}
241c63181e6SAl Viro 	return mnt;
24288b38782SLi Zefan 
243d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
244d3ef3d73Snpiggin@suse.de out_free_devname:
245fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
246d3ef3d73Snpiggin@suse.de #endif
24788b38782SLi Zefan out_free_id:
248c63181e6SAl Viro 	mnt_free_id(mnt);
24988b38782SLi Zefan out_free_cache:
250c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
25188b38782SLi Zefan 	return NULL;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2548366025eSDave Hansen /*
2558366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2568366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2578366025eSDave Hansen  * We must keep track of when those operations start
2588366025eSDave Hansen  * (for permission checks) and when they end, so that
2598366025eSDave Hansen  * we can determine when writes are able to occur to
2608366025eSDave Hansen  * a filesystem.
2618366025eSDave Hansen  */
2623d733633SDave Hansen /*
2633d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2643d733633SDave Hansen  * @mnt: the mount to check for its write status
2653d733633SDave Hansen  *
2663d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2673d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2683d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2693d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2703d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2713d733633SDave Hansen  * r/w.
2723d733633SDave Hansen  */
2733d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2743d733633SDave Hansen {
2752e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2762e4b7fcdSDave Hansen 		return 1;
2772e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
2782e4b7fcdSDave Hansen 		return 1;
2792e4b7fcdSDave Hansen 	return 0;
2803d733633SDave Hansen }
2813d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2823d733633SDave Hansen 
28383adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2843d733633SDave Hansen {
285d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
28668e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
287d3ef3d73Snpiggin@suse.de #else
28868e8a9feSAl Viro 	mnt->mnt_writers++;
289d3ef3d73Snpiggin@suse.de #endif
2903d733633SDave Hansen }
2913d733633SDave Hansen 
29283adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2933d733633SDave Hansen {
294d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
29568e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
296d3ef3d73Snpiggin@suse.de #else
29768e8a9feSAl Viro 	mnt->mnt_writers--;
298d3ef3d73Snpiggin@suse.de #endif
299d3ef3d73Snpiggin@suse.de }
300d3ef3d73Snpiggin@suse.de 
30183adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
302d3ef3d73Snpiggin@suse.de {
303d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
304d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
3053d733633SDave Hansen 	int cpu;
3063d733633SDave Hansen 
3073d733633SDave Hansen 	for_each_possible_cpu(cpu) {
30868e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3093d733633SDave Hansen 	}
3103d733633SDave Hansen 
311d3ef3d73Snpiggin@suse.de 	return count;
312d3ef3d73Snpiggin@suse.de #else
313d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
314d3ef3d73Snpiggin@suse.de #endif
3153d733633SDave Hansen }
3163d733633SDave Hansen 
3174ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
3184ed5e82fSMiklos Szeredi {
3194ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
3204ed5e82fSMiklos Szeredi 		return 1;
3214ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
3224ed5e82fSMiklos Szeredi 	smp_rmb();
3234ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
3244ed5e82fSMiklos Szeredi }
3254ed5e82fSMiklos Szeredi 
3263d733633SDave Hansen /*
327eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
328eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
329eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
330eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
3313d733633SDave Hansen  */
3328366025eSDave Hansen /**
333eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
33483adc753SAl Viro  * @m: the mount on which to take a write
3358366025eSDave Hansen  *
336eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
337eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
338eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
339eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
340eb04c282SJan Kara  * called. This is effectively a refcount.
3418366025eSDave Hansen  */
342eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3438366025eSDave Hansen {
34483adc753SAl Viro 	struct mount *mnt = real_mount(m);
3453d733633SDave Hansen 	int ret = 0;
3463d733633SDave Hansen 
347d3ef3d73Snpiggin@suse.de 	preempt_disable();
348c6653a83SNick Piggin 	mnt_inc_writers(mnt);
349d3ef3d73Snpiggin@suse.de 	/*
350c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
351d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
352d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
353d3ef3d73Snpiggin@suse.de 	 */
354d3ef3d73Snpiggin@suse.de 	smp_mb();
3551e75529eSMiao Xie 	while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
356d3ef3d73Snpiggin@suse.de 		cpu_relax();
357d3ef3d73Snpiggin@suse.de 	/*
358d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
359d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
360d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
361d3ef3d73Snpiggin@suse.de 	 */
362d3ef3d73Snpiggin@suse.de 	smp_rmb();
3634ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
364c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3653d733633SDave Hansen 		ret = -EROFS;
3663d733633SDave Hansen 	}
367d3ef3d73Snpiggin@suse.de 	preempt_enable();
368eb04c282SJan Kara 
369eb04c282SJan Kara 	return ret;
370eb04c282SJan Kara }
371eb04c282SJan Kara 
372eb04c282SJan Kara /**
373eb04c282SJan Kara  * mnt_want_write - get write access to a mount
374eb04c282SJan Kara  * @m: the mount on which to take a write
375eb04c282SJan Kara  *
376eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
377eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
378eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
379eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
380eb04c282SJan Kara  */
381eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
382eb04c282SJan Kara {
383eb04c282SJan Kara 	int ret;
384eb04c282SJan Kara 
385eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
386eb04c282SJan Kara 	ret = __mnt_want_write(m);
387eb04c282SJan Kara 	if (ret)
388eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3893d733633SDave Hansen 	return ret;
3908366025eSDave Hansen }
3918366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3928366025eSDave Hansen 
3938366025eSDave Hansen /**
39496029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
39596029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
39696029c4eSnpiggin@suse.de  *
39796029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
39896029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
39996029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
40096029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
40196029c4eSnpiggin@suse.de  *
40296029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
40396029c4eSnpiggin@suse.de  * drop the reference.
40496029c4eSnpiggin@suse.de  */
40596029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
40696029c4eSnpiggin@suse.de {
40796029c4eSnpiggin@suse.de 	/* superblock may be r/o */
40896029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
40996029c4eSnpiggin@suse.de 		return -EROFS;
41096029c4eSnpiggin@suse.de 	preempt_disable();
41183adc753SAl Viro 	mnt_inc_writers(real_mount(mnt));
41296029c4eSnpiggin@suse.de 	preempt_enable();
41396029c4eSnpiggin@suse.de 	return 0;
41496029c4eSnpiggin@suse.de }
41596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
41696029c4eSnpiggin@suse.de 
41796029c4eSnpiggin@suse.de /**
418eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
419eb04c282SJan Kara  * @file: the file who's mount on which to take a write
420eb04c282SJan Kara  *
421eb04c282SJan Kara  * This is like __mnt_want_write, but it takes a file and can
422eb04c282SJan Kara  * do some optimisations if the file is open for write already
423eb04c282SJan Kara  */
424eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
425eb04c282SJan Kara {
42683f936c7SAl Viro 	if (!(file->f_mode & FMODE_WRITER))
427eb04c282SJan Kara 		return __mnt_want_write(file->f_path.mnt);
428eb04c282SJan Kara 	else
429eb04c282SJan Kara 		return mnt_clone_write(file->f_path.mnt);
430eb04c282SJan Kara }
431eb04c282SJan Kara 
432eb04c282SJan Kara /**
43396029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
43496029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
43596029c4eSnpiggin@suse.de  *
43696029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
43796029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
43896029c4eSnpiggin@suse.de  */
43996029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
44096029c4eSnpiggin@suse.de {
441eb04c282SJan Kara 	int ret;
442eb04c282SJan Kara 
443eb04c282SJan Kara 	sb_start_write(file->f_path.mnt->mnt_sb);
444eb04c282SJan Kara 	ret = __mnt_want_write_file(file);
445eb04c282SJan Kara 	if (ret)
446eb04c282SJan Kara 		sb_end_write(file->f_path.mnt->mnt_sb);
447eb04c282SJan Kara 	return ret;
44896029c4eSnpiggin@suse.de }
44996029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
45096029c4eSnpiggin@suse.de 
45196029c4eSnpiggin@suse.de /**
452eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4538366025eSDave Hansen  * @mnt: the mount on which to give up write access
4548366025eSDave Hansen  *
4558366025eSDave Hansen  * Tells the low-level filesystem that we are done
4568366025eSDave Hansen  * performing writes to it.  Must be matched with
457eb04c282SJan Kara  * __mnt_want_write() call above.
4588366025eSDave Hansen  */
459eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4608366025eSDave Hansen {
461d3ef3d73Snpiggin@suse.de 	preempt_disable();
46283adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
463d3ef3d73Snpiggin@suse.de 	preempt_enable();
4648366025eSDave Hansen }
465eb04c282SJan Kara 
466eb04c282SJan Kara /**
467eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
468eb04c282SJan Kara  * @mnt: the mount on which to give up write access
469eb04c282SJan Kara  *
470eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
471eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
472eb04c282SJan Kara  * mnt_want_write() call above.
473eb04c282SJan Kara  */
474eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
475eb04c282SJan Kara {
476eb04c282SJan Kara 	__mnt_drop_write(mnt);
477eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
478eb04c282SJan Kara }
4798366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4808366025eSDave Hansen 
481eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
482eb04c282SJan Kara {
483eb04c282SJan Kara 	__mnt_drop_write(file->f_path.mnt);
484eb04c282SJan Kara }
485eb04c282SJan Kara 
4862a79f17eSAl Viro void mnt_drop_write_file(struct file *file)
4872a79f17eSAl Viro {
4882a79f17eSAl Viro 	mnt_drop_write(file->f_path.mnt);
4892a79f17eSAl Viro }
4902a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4912a79f17eSAl Viro 
49283adc753SAl Viro static int mnt_make_readonly(struct mount *mnt)
4938366025eSDave Hansen {
4943d733633SDave Hansen 	int ret = 0;
4953d733633SDave Hansen 
496719ea2fbSAl Viro 	lock_mount_hash();
49783adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
498d3ef3d73Snpiggin@suse.de 	/*
499d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
500d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
501d3ef3d73Snpiggin@suse.de 	 */
502d3ef3d73Snpiggin@suse.de 	smp_mb();
503d3ef3d73Snpiggin@suse.de 
504d3ef3d73Snpiggin@suse.de 	/*
505d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
506d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
507d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
508d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
509d3ef3d73Snpiggin@suse.de 	 *
510d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
511d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
512d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
513d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
514d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
515d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
516d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
517d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
518d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
519d3ef3d73Snpiggin@suse.de 	 */
520c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
521d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
522d3ef3d73Snpiggin@suse.de 	else
52383adc753SAl Viro 		mnt->mnt.mnt_flags |= MNT_READONLY;
524d3ef3d73Snpiggin@suse.de 	/*
525d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
526d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
527d3ef3d73Snpiggin@suse.de 	 */
528d3ef3d73Snpiggin@suse.de 	smp_wmb();
52983adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
530719ea2fbSAl Viro 	unlock_mount_hash();
5313d733633SDave Hansen 	return ret;
5323d733633SDave Hansen }
5338366025eSDave Hansen 
53483adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt)
5352e4b7fcdSDave Hansen {
536719ea2fbSAl Viro 	lock_mount_hash();
53783adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
538719ea2fbSAl Viro 	unlock_mount_hash();
5392e4b7fcdSDave Hansen }
5402e4b7fcdSDave Hansen 
5414ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5424ed5e82fSMiklos Szeredi {
5434ed5e82fSMiklos Szeredi 	struct mount *mnt;
5444ed5e82fSMiklos Szeredi 	int err = 0;
5454ed5e82fSMiklos Szeredi 
5468e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5478e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5488e8b8796SMiklos Szeredi 		return -EBUSY;
5498e8b8796SMiklos Szeredi 
550719ea2fbSAl Viro 	lock_mount_hash();
5514ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5524ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
5534ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
5544ed5e82fSMiklos Szeredi 			smp_mb();
5554ed5e82fSMiklos Szeredi 			if (mnt_get_writers(mnt) > 0) {
5564ed5e82fSMiklos Szeredi 				err = -EBUSY;
5574ed5e82fSMiklos Szeredi 				break;
5584ed5e82fSMiklos Szeredi 			}
5594ed5e82fSMiklos Szeredi 		}
5604ed5e82fSMiklos Szeredi 	}
5618e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5628e8b8796SMiklos Szeredi 		err = -EBUSY;
5638e8b8796SMiklos Szeredi 
5644ed5e82fSMiklos Szeredi 	if (!err) {
5654ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
5664ed5e82fSMiklos Szeredi 		smp_wmb();
5674ed5e82fSMiklos Szeredi 	}
5684ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5694ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
5704ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
5714ed5e82fSMiklos Szeredi 	}
572719ea2fbSAl Viro 	unlock_mount_hash();
5734ed5e82fSMiklos Szeredi 
5744ed5e82fSMiklos Szeredi 	return err;
5754ed5e82fSMiklos Szeredi }
5764ed5e82fSMiklos Szeredi 
577b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
5781da177e4SLinus Torvalds {
579fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
580d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
58168e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
582d3ef3d73Snpiggin@suse.de #endif
583b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5868ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
5878ffcb32eSDavid Howells {
5888ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
5898ffcb32eSDavid Howells }
5908ffcb32eSDavid Howells 
59148a066e7SAl Viro /* call under rcu_read_lock */
592294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
59348a066e7SAl Viro {
59448a066e7SAl Viro 	struct mount *mnt;
59548a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
596294d71ffSAl Viro 		return 1;
59748a066e7SAl Viro 	if (bastard == NULL)
598294d71ffSAl Viro 		return 0;
59948a066e7SAl Viro 	mnt = real_mount(bastard);
60048a066e7SAl Viro 	mnt_add_count(mnt, 1);
60148a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
602294d71ffSAl Viro 		return 0;
60348a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
60448a066e7SAl Viro 		mnt_add_count(mnt, -1);
605294d71ffSAl Viro 		return 1;
60648a066e7SAl Viro 	}
607294d71ffSAl Viro 	return -1;
608294d71ffSAl Viro }
609294d71ffSAl Viro 
610294d71ffSAl Viro /* call under rcu_read_lock */
611294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
612294d71ffSAl Viro {
613294d71ffSAl Viro 	int res = __legitimize_mnt(bastard, seq);
614294d71ffSAl Viro 	if (likely(!res))
615294d71ffSAl Viro 		return true;
616294d71ffSAl Viro 	if (unlikely(res < 0)) {
61748a066e7SAl Viro 		rcu_read_unlock();
61848a066e7SAl Viro 		mntput(bastard);
61948a066e7SAl Viro 		rcu_read_lock();
620294d71ffSAl Viro 	}
62148a066e7SAl Viro 	return false;
62248a066e7SAl Viro }
62348a066e7SAl Viro 
6241da177e4SLinus Torvalds /*
625474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
62648a066e7SAl Viro  * call under rcu_read_lock()
6271da177e4SLinus Torvalds  */
628474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6291da177e4SLinus Torvalds {
63038129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
631474279dcSAl Viro 	struct mount *p;
6321da177e4SLinus Torvalds 
63338129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
634474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
635474279dcSAl Viro 			return p;
636474279dcSAl Viro 	return NULL;
6371da177e4SLinus Torvalds }
638474279dcSAl Viro 
639474279dcSAl Viro /*
640474279dcSAl Viro  * find the last mount at @dentry on vfsmount @mnt.
64148a066e7SAl Viro  * mount_lock must be held.
642474279dcSAl Viro  */
643474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
644474279dcSAl Viro {
645411a938bSEric W. Biederman 	struct mount *p, *res = NULL;
646411a938bSEric W. Biederman 	p = __lookup_mnt(mnt, dentry);
64738129a13SAl Viro 	if (!p)
64838129a13SAl Viro 		goto out;
649411a938bSEric W. Biederman 	if (!(p->mnt.mnt_flags & MNT_UMOUNT))
650411a938bSEric W. Biederman 		res = p;
65138129a13SAl Viro 	hlist_for_each_entry_continue(p, mnt_hash) {
6521d6a32acSAl Viro 		if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
6531d6a32acSAl Viro 			break;
654411a938bSEric W. Biederman 		if (!(p->mnt.mnt_flags & MNT_UMOUNT))
6551d6a32acSAl Viro 			res = p;
6561d6a32acSAl Viro 	}
65738129a13SAl Viro out:
6581d6a32acSAl Viro 	return res;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
661a05964f3SRam Pai /*
662f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
663f015f126SDavid Howells  *
664f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
665f015f126SDavid Howells  * following mounts:
666f015f126SDavid Howells  *
667f015f126SDavid Howells  * mount /dev/sda1 /mnt
668f015f126SDavid Howells  * mount /dev/sda2 /mnt
669f015f126SDavid Howells  * mount /dev/sda3 /mnt
670f015f126SDavid Howells  *
671f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
672f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
673f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
674f015f126SDavid Howells  *
675f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
676a05964f3SRam Pai  */
6771c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
678a05964f3SRam Pai {
679c7105365SAl Viro 	struct mount *child_mnt;
68048a066e7SAl Viro 	struct vfsmount *m;
68148a066e7SAl Viro 	unsigned seq;
68299b7db7bSNick Piggin 
68348a066e7SAl Viro 	rcu_read_lock();
68448a066e7SAl Viro 	do {
68548a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
686474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
68748a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
68848a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
68948a066e7SAl Viro 	rcu_read_unlock();
69048a066e7SAl Viro 	return m;
691a05964f3SRam Pai }
692a05964f3SRam Pai 
6937af1364fSEric W. Biederman /*
6947af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
6957af1364fSEric W. Biederman  *                         current mount namespace.
6967af1364fSEric W. Biederman  *
6977af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
6987af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
6997af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
7007af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
7017af1364fSEric W. Biederman  * is a mountpoint.
7027af1364fSEric W. Biederman  *
7037af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
7047af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
7057af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
7067af1364fSEric W. Biederman  * parent mount.
7077af1364fSEric W. Biederman  */
7087af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
7097af1364fSEric W. Biederman {
7107af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
7117af1364fSEric W. Biederman 	struct mount *mnt;
7127af1364fSEric W. Biederman 	bool is_covered = false;
7137af1364fSEric W. Biederman 
7147af1364fSEric W. Biederman 	if (!d_mountpoint(dentry))
7157af1364fSEric W. Biederman 		goto out;
7167af1364fSEric W. Biederman 
7177af1364fSEric W. Biederman 	down_read(&namespace_sem);
7187af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
7197af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
7207af1364fSEric W. Biederman 		if (is_covered)
7217af1364fSEric W. Biederman 			break;
7227af1364fSEric W. Biederman 	}
7237af1364fSEric W. Biederman 	up_read(&namespace_sem);
7247af1364fSEric W. Biederman out:
7257af1364fSEric W. Biederman 	return is_covered;
7267af1364fSEric W. Biederman }
7277af1364fSEric W. Biederman 
728e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
72984d17192SAl Viro {
7300818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
73184d17192SAl Viro 	struct mountpoint *mp;
73284d17192SAl Viro 
7330818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
73484d17192SAl Viro 		if (mp->m_dentry == dentry) {
73584d17192SAl Viro 			/* might be worth a WARN_ON() */
73684d17192SAl Viro 			if (d_unlinked(dentry))
73784d17192SAl Viro 				return ERR_PTR(-ENOENT);
73884d17192SAl Viro 			mp->m_count++;
73984d17192SAl Viro 			return mp;
74084d17192SAl Viro 		}
74184d17192SAl Viro 	}
742e2dfa935SEric W. Biederman 	return NULL;
743e2dfa935SEric W. Biederman }
744e2dfa935SEric W. Biederman 
745e2dfa935SEric W. Biederman static struct mountpoint *new_mountpoint(struct dentry *dentry)
746e2dfa935SEric W. Biederman {
747e2dfa935SEric W. Biederman 	struct hlist_head *chain = mp_hash(dentry);
748e2dfa935SEric W. Biederman 	struct mountpoint *mp;
749e2dfa935SEric W. Biederman 	int ret;
75084d17192SAl Viro 
75184d17192SAl Viro 	mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
75284d17192SAl Viro 	if (!mp)
75384d17192SAl Viro 		return ERR_PTR(-ENOMEM);
75484d17192SAl Viro 
755eed81007SMiklos Szeredi 	ret = d_set_mounted(dentry);
756eed81007SMiklos Szeredi 	if (ret) {
75784d17192SAl Viro 		kfree(mp);
758eed81007SMiklos Szeredi 		return ERR_PTR(ret);
75984d17192SAl Viro 	}
760eed81007SMiklos Szeredi 
76184d17192SAl Viro 	mp->m_dentry = dentry;
76284d17192SAl Viro 	mp->m_count = 1;
7630818bf27SAl Viro 	hlist_add_head(&mp->m_hash, chain);
7640a5eb7c8SEric W. Biederman 	INIT_HLIST_HEAD(&mp->m_list);
76584d17192SAl Viro 	return mp;
76684d17192SAl Viro }
76784d17192SAl Viro 
76884d17192SAl Viro static void put_mountpoint(struct mountpoint *mp)
76984d17192SAl Viro {
77084d17192SAl Viro 	if (!--mp->m_count) {
77184d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
7720a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
77384d17192SAl Viro 		spin_lock(&dentry->d_lock);
77484d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
77584d17192SAl Viro 		spin_unlock(&dentry->d_lock);
7760818bf27SAl Viro 		hlist_del(&mp->m_hash);
77784d17192SAl Viro 		kfree(mp);
77884d17192SAl Viro 	}
77984d17192SAl Viro }
78084d17192SAl Viro 
781143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
7821da177e4SLinus Torvalds {
7836b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
78699b7db7bSNick Piggin /*
78799b7db7bSNick Piggin  * vfsmount lock must be held for write
78899b7db7bSNick Piggin  */
7896b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
7905addc5ddSAl Viro {
7915addc5ddSAl Viro 	if (ns) {
7925addc5ddSAl Viro 		ns->event = ++event;
7935addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7945addc5ddSAl Viro 	}
7955addc5ddSAl Viro }
7965addc5ddSAl Viro 
79799b7db7bSNick Piggin /*
79899b7db7bSNick Piggin  * vfsmount lock must be held for write
79999b7db7bSNick Piggin  */
8006b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
8015addc5ddSAl Viro {
8025addc5ddSAl Viro 	if (ns && ns->event != event) {
8035addc5ddSAl Viro 		ns->event = event;
8045addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
8055addc5ddSAl Viro 	}
8065addc5ddSAl Viro }
8075addc5ddSAl Viro 
80899b7db7bSNick Piggin /*
80999b7db7bSNick Piggin  * vfsmount lock must be held for write
81099b7db7bSNick Piggin  */
8117bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt)
8121da177e4SLinus Torvalds {
8130714a533SAl Viro 	mnt->mnt_parent = mnt;
814a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8156b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
81638129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
8170a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
81884d17192SAl Viro 	put_mountpoint(mnt->mnt_mp);
81984d17192SAl Viro 	mnt->mnt_mp = NULL;
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
82299b7db7bSNick Piggin /*
82399b7db7bSNick Piggin  * vfsmount lock must be held for write
82499b7db7bSNick Piggin  */
8257bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path)
8267bdb11deSEric W. Biederman {
8277bdb11deSEric W. Biederman 	old_path->dentry = mnt->mnt_mountpoint;
8287bdb11deSEric W. Biederman 	old_path->mnt = &mnt->mnt_parent->mnt;
8297bdb11deSEric W. Biederman 	unhash_mnt(mnt);
8307bdb11deSEric W. Biederman }
8317bdb11deSEric W. Biederman 
8327bdb11deSEric W. Biederman /*
8337bdb11deSEric W. Biederman  * vfsmount lock must be held for write
8347bdb11deSEric W. Biederman  */
8356a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt)
8366a46c573SEric W. Biederman {
8376a46c573SEric W. Biederman 	/* old mountpoint will be dropped when we can do that */
8386a46c573SEric W. Biederman 	mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
8396a46c573SEric W. Biederman 	unhash_mnt(mnt);
8406a46c573SEric W. Biederman }
8416a46c573SEric W. Biederman 
8426a46c573SEric W. Biederman /*
8436a46c573SEric W. Biederman  * vfsmount lock must be held for write
8446a46c573SEric W. Biederman  */
84584d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
84684d17192SAl Viro 			struct mountpoint *mp,
84744d964d6SAl Viro 			struct mount *child_mnt)
848b90fa9aeSRam Pai {
84984d17192SAl Viro 	mp->m_count++;
8503a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
85184d17192SAl Viro 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
8523a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
85384d17192SAl Viro 	child_mnt->mnt_mp = mp;
8540a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
855b90fa9aeSRam Pai }
856b90fa9aeSRam Pai 
85799b7db7bSNick Piggin /*
85899b7db7bSNick Piggin  * vfsmount lock must be held for write
85999b7db7bSNick Piggin  */
86084d17192SAl Viro static void attach_mnt(struct mount *mnt,
86184d17192SAl Viro 			struct mount *parent,
86284d17192SAl Viro 			struct mountpoint *mp)
8631da177e4SLinus Torvalds {
86484d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
86538129a13SAl Viro 	hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
86684d17192SAl Viro 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
867b90fa9aeSRam Pai }
868b90fa9aeSRam Pai 
86912a5b529SAl Viro static void attach_shadowed(struct mount *mnt,
87012a5b529SAl Viro 			struct mount *parent,
87112a5b529SAl Viro 			struct mount *shadows)
87212a5b529SAl Viro {
87312a5b529SAl Viro 	if (shadows) {
874f6f99332SLinus Torvalds 		hlist_add_behind_rcu(&mnt->mnt_hash, &shadows->mnt_hash);
87512a5b529SAl Viro 		list_add(&mnt->mnt_child, &shadows->mnt_child);
87612a5b529SAl Viro 	} else {
87712a5b529SAl Viro 		hlist_add_head_rcu(&mnt->mnt_hash,
87812a5b529SAl Viro 				m_hash(&parent->mnt, mnt->mnt_mountpoint));
87912a5b529SAl Viro 		list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
88012a5b529SAl Viro 	}
88112a5b529SAl Viro }
88212a5b529SAl Viro 
883b90fa9aeSRam Pai /*
88499b7db7bSNick Piggin  * vfsmount lock must be held for write
885b90fa9aeSRam Pai  */
8861d6a32acSAl Viro static void commit_tree(struct mount *mnt, struct mount *shadows)
887b90fa9aeSRam Pai {
8880714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
88983adc753SAl Viro 	struct mount *m;
890b90fa9aeSRam Pai 	LIST_HEAD(head);
891143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
892b90fa9aeSRam Pai 
8930714a533SAl Viro 	BUG_ON(parent == mnt);
894b90fa9aeSRam Pai 
8951a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
896f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
897143c8c91SAl Viro 		m->mnt_ns = n;
898f03c6599SAl Viro 
899b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
900b90fa9aeSRam Pai 
901d2921684SEric W. Biederman 	n->mounts += n->pending_mounts;
902d2921684SEric W. Biederman 	n->pending_mounts = 0;
903d2921684SEric W. Biederman 
90412a5b529SAl Viro 	attach_shadowed(mnt, parent, shadows);
9056b3286edSKirill Korotaev 	touch_mnt_namespace(n);
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
908909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
9091da177e4SLinus Torvalds {
9106b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
9116b41d536SAl Viro 	if (next == &p->mnt_mounts) {
9121da177e4SLinus Torvalds 		while (1) {
913909b0a88SAl Viro 			if (p == root)
9141da177e4SLinus Torvalds 				return NULL;
9156b41d536SAl Viro 			next = p->mnt_child.next;
9166b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
9171da177e4SLinus Torvalds 				break;
9180714a533SAl Viro 			p = p->mnt_parent;
9191da177e4SLinus Torvalds 		}
9201da177e4SLinus Torvalds 	}
9216b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
924315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
9259676f0c6SRam Pai {
9266b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
9276b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
9286b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
9296b41d536SAl Viro 		prev = p->mnt_mounts.prev;
9309676f0c6SRam Pai 	}
9319676f0c6SRam Pai 	return p;
9329676f0c6SRam Pai }
9339676f0c6SRam Pai 
9349d412a43SAl Viro struct vfsmount *
9359d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
9369d412a43SAl Viro {
937b105e270SAl Viro 	struct mount *mnt;
9389d412a43SAl Viro 	struct dentry *root;
9399d412a43SAl Viro 
9409d412a43SAl Viro 	if (!type)
9419d412a43SAl Viro 		return ERR_PTR(-ENODEV);
9429d412a43SAl Viro 
9439d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
9449d412a43SAl Viro 	if (!mnt)
9459d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
9469d412a43SAl Viro 
9479d412a43SAl Viro 	if (flags & MS_KERNMOUNT)
948b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
9499d412a43SAl Viro 
9509d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
9519d412a43SAl Viro 	if (IS_ERR(root)) {
9528ffcb32eSDavid Howells 		mnt_free_id(mnt);
9539d412a43SAl Viro 		free_vfsmnt(mnt);
9549d412a43SAl Viro 		return ERR_CAST(root);
9559d412a43SAl Viro 	}
9569d412a43SAl Viro 
957b105e270SAl Viro 	mnt->mnt.mnt_root = root;
958b105e270SAl Viro 	mnt->mnt.mnt_sb = root->d_sb;
959a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
9600714a533SAl Viro 	mnt->mnt_parent = mnt;
961719ea2fbSAl Viro 	lock_mount_hash();
96239f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
963719ea2fbSAl Viro 	unlock_mount_hash();
964b105e270SAl Viro 	return &mnt->mnt;
9659d412a43SAl Viro }
9669d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
9679d412a43SAl Viro 
96887129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
96936341f64SRam Pai 					int flag)
9701da177e4SLinus Torvalds {
97187129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
972be34d1a3SDavid Howells 	struct mount *mnt;
973be34d1a3SDavid Howells 	int err;
9741da177e4SLinus Torvalds 
975be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
976be34d1a3SDavid Howells 	if (!mnt)
977be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
978be34d1a3SDavid Howells 
9797a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
98015169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
981719f5d7fSMiklos Szeredi 	else
98215169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
983719f5d7fSMiklos Szeredi 
98415169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
985be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
986719f5d7fSMiklos Szeredi 		if (err)
987719f5d7fSMiklos Szeredi 			goto out_free;
988719f5d7fSMiklos Szeredi 	}
989719f5d7fSMiklos Szeredi 
990f2ebb3a9SAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
991132c94e3SEric W. Biederman 	/* Don't allow unprivileged users to change mount flags */
9929566d674SEric W. Biederman 	if (flag & CL_UNPRIVILEGED) {
9939566d674SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
9949566d674SEric W. Biederman 
9959566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_READONLY)
996132c94e3SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
997132c94e3SEric W. Biederman 
9989566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NODEV)
9999566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
10009566d674SEric W. Biederman 
10019566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NOSUID)
10029566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
10039566d674SEric W. Biederman 
10049566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NOEXEC)
10059566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
10069566d674SEric W. Biederman 	}
10079566d674SEric W. Biederman 
10085ff9d8a6SEric W. Biederman 	/* Don't allow unprivileged users to reveal what is under a mount */
1009381cacb1SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) &&
1010381cacb1SEric W. Biederman 	    (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
10115ff9d8a6SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCKED;
10125ff9d8a6SEric W. Biederman 
10131da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1014b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1015b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1016a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
10170714a533SAl Viro 	mnt->mnt_parent = mnt;
1018719ea2fbSAl Viro 	lock_mount_hash();
101939f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1020719ea2fbSAl Viro 	unlock_mount_hash();
1021b90fa9aeSRam Pai 
10227a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
10237a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
10246776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
102532301920SAl Viro 		mnt->mnt_master = old;
1026fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
10278aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1028fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
10296776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1030d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
10316776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1032d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
10335235d448SAl Viro 	} else {
10345235d448SAl Viro 		CLEAR_MNT_SHARED(mnt);
10355afe0022SRam Pai 	}
1036b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
10370f0afb1dSAl Viro 		set_mnt_shared(mnt);
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
10401da177e4SLinus Torvalds 	 * as the original if that was on one */
104136341f64SRam Pai 	if (flag & CL_EXPIRE) {
10426776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
10436776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
10441da177e4SLinus Torvalds 	}
1045be34d1a3SDavid Howells 
1046cb338d06SAl Viro 	return mnt;
1047719f5d7fSMiklos Szeredi 
1048719f5d7fSMiklos Szeredi  out_free:
10498ffcb32eSDavid Howells 	mnt_free_id(mnt);
1050719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1051be34d1a3SDavid Howells 	return ERR_PTR(err);
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
10549ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
10559ea459e1SAl Viro {
10569ea459e1SAl Viro 	/*
10579ea459e1SAl Viro 	 * This probably indicates that somebody messed
10589ea459e1SAl Viro 	 * up a mnt_want/drop_write() pair.  If this
10599ea459e1SAl Viro 	 * happens, the filesystem was probably unable
10609ea459e1SAl Viro 	 * to make r/w->r/o transitions.
10619ea459e1SAl Viro 	 */
10629ea459e1SAl Viro 	/*
10639ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
10649ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
10659ea459e1SAl Viro 	 */
10669ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
10679ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
10689ea459e1SAl Viro 		mnt_pin_kill(mnt);
10699ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
10709ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
10719ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
10729ea459e1SAl Viro 	mnt_free_id(mnt);
10739ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
10749ea459e1SAl Viro }
10759ea459e1SAl Viro 
10769ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
10779ea459e1SAl Viro {
10789ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
10799ea459e1SAl Viro }
10809ea459e1SAl Viro 
10819ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
10829ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
10839ea459e1SAl Viro {
10849ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
10859ea459e1SAl Viro 	struct llist_node *next;
10869ea459e1SAl Viro 
10879ea459e1SAl Viro 	for (; node; node = next) {
10889ea459e1SAl Viro 		next = llist_next(node);
10899ea459e1SAl Viro 		cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
10909ea459e1SAl Viro 	}
10919ea459e1SAl Viro }
10929ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
10939ea459e1SAl Viro 
1094900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
10957b7b1aceSAl Viro {
109648a066e7SAl Viro 	rcu_read_lock();
1097aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
109848a066e7SAl Viro 	if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
109948a066e7SAl Viro 		rcu_read_unlock();
110099b7db7bSNick Piggin 		return;
1101b3e19d92SNick Piggin 	}
1102719ea2fbSAl Viro 	lock_mount_hash();
1103b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
110448a066e7SAl Viro 		rcu_read_unlock();
1105719ea2fbSAl Viro 		unlock_mount_hash();
110699b7db7bSNick Piggin 		return;
110799b7db7bSNick Piggin 	}
110848a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
110948a066e7SAl Viro 		rcu_read_unlock();
111048a066e7SAl Viro 		unlock_mount_hash();
111148a066e7SAl Viro 		return;
111248a066e7SAl Viro 	}
111348a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
111448a066e7SAl Viro 	rcu_read_unlock();
1115962830dfSAndi Kleen 
111639f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1117ce07d891SEric W. Biederman 
1118ce07d891SEric W. Biederman 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1119ce07d891SEric W. Biederman 		struct mount *p, *tmp;
1120ce07d891SEric W. Biederman 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
1121ce07d891SEric W. Biederman 			umount_mnt(p);
1122ce07d891SEric W. Biederman 		}
1123ce07d891SEric W. Biederman 	}
1124719ea2fbSAl Viro 	unlock_mount_hash();
1125649a795aSAl Viro 
11269ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
11279ea459e1SAl Viro 		struct task_struct *task = current;
11289ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
11299ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
11309ea459e1SAl Viro 			if (!task_work_add(task, &mnt->mnt_rcu, true))
11319ea459e1SAl Viro 				return;
11329ea459e1SAl Viro 		}
11339ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
11349ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
11359ea459e1SAl Viro 		return;
11369ea459e1SAl Viro 	}
11379ea459e1SAl Viro 	cleanup_mnt(mnt);
1138b3e19d92SNick Piggin }
1139b3e19d92SNick Piggin 
1140b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1141b3e19d92SNick Piggin {
1142b3e19d92SNick Piggin 	if (mnt) {
1143863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1144b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1145863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1146863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1147863d684fSAl Viro 		mntput_no_expire(m);
1148b3e19d92SNick Piggin 	}
1149b3e19d92SNick Piggin }
1150b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1151b3e19d92SNick Piggin 
1152b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1153b3e19d92SNick Piggin {
1154b3e19d92SNick Piggin 	if (mnt)
115583adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1156b3e19d92SNick Piggin 	return mnt;
1157b3e19d92SNick Piggin }
1158b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1159b3e19d92SNick Piggin 
11603064c356SAl Viro struct vfsmount *mnt_clone_internal(struct path *path)
11617b7b1aceSAl Viro {
11623064c356SAl Viro 	struct mount *p;
11633064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
11643064c356SAl Viro 	if (IS_ERR(p))
11653064c356SAl Viro 		return ERR_CAST(p);
11663064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
11673064c356SAl Viro 	return &p->mnt;
11687b7b1aceSAl Viro }
11691da177e4SLinus Torvalds 
1170b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
1171b3b304a2SMiklos Szeredi {
1172b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
1173b3b304a2SMiklos Szeredi }
1174b3b304a2SMiklos Szeredi 
1175b3b304a2SMiklos Szeredi /*
1176b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
1177b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
1178b3b304a2SMiklos Szeredi  *
1179b3b304a2SMiklos Szeredi  * See also save_mount_options().
1180b3b304a2SMiklos Szeredi  */
118134c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root)
1182b3b304a2SMiklos Szeredi {
11832a32cebdSAl Viro 	const char *options;
11842a32cebdSAl Viro 
11852a32cebdSAl Viro 	rcu_read_lock();
118634c80b1dSAl Viro 	options = rcu_dereference(root->d_sb->s_options);
1187b3b304a2SMiklos Szeredi 
1188b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
1189b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
1190b3b304a2SMiklos Szeredi 		mangle(m, options);
1191b3b304a2SMiklos Szeredi 	}
11922a32cebdSAl Viro 	rcu_read_unlock();
1193b3b304a2SMiklos Szeredi 
1194b3b304a2SMiklos Szeredi 	return 0;
1195b3b304a2SMiklos Szeredi }
1196b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
1197b3b304a2SMiklos Szeredi 
1198b3b304a2SMiklos Szeredi /*
1199b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
1200b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
1201b3b304a2SMiklos Szeredi  *
1202b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
1203b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
1204b3b304a2SMiklos Szeredi  * remount fails.
1205b3b304a2SMiklos Szeredi  *
1206b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
1207b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
1208b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
1209b3b304a2SMiklos Szeredi  * any more.
1210b3b304a2SMiklos Szeredi  */
1211b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
1212b3b304a2SMiklos Szeredi {
12132a32cebdSAl Viro 	BUG_ON(sb->s_options);
12142a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1215b3b304a2SMiklos Szeredi }
1216b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
1217b3b304a2SMiklos Szeredi 
12182a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
12192a32cebdSAl Viro {
12202a32cebdSAl Viro 	char *old = sb->s_options;
12212a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
12222a32cebdSAl Viro 	if (old) {
12232a32cebdSAl Viro 		synchronize_rcu();
12242a32cebdSAl Viro 		kfree(old);
12252a32cebdSAl Viro 	}
12262a32cebdSAl Viro }
12272a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
12282a32cebdSAl Viro 
1229a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
12300226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
12311da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
12321da177e4SLinus Torvalds {
1233ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
12341da177e4SLinus Torvalds 
1235390c6843SRam Pai 	down_read(&namespace_sem);
1236c7999c36SAl Viro 	if (p->cached_event == p->ns->event) {
1237c7999c36SAl Viro 		void *v = p->cached_mount;
1238c7999c36SAl Viro 		if (*pos == p->cached_index)
1239c7999c36SAl Viro 			return v;
1240c7999c36SAl Viro 		if (*pos == p->cached_index + 1) {
1241c7999c36SAl Viro 			v = seq_list_next(v, &p->ns->list, &p->cached_index);
1242c7999c36SAl Viro 			return p->cached_mount = v;
1243c7999c36SAl Viro 		}
1244c7999c36SAl Viro 	}
1245c7999c36SAl Viro 
1246c7999c36SAl Viro 	p->cached_event = p->ns->event;
1247c7999c36SAl Viro 	p->cached_mount = seq_list_start(&p->ns->list, *pos);
1248c7999c36SAl Viro 	p->cached_index = *pos;
1249c7999c36SAl Viro 	return p->cached_mount;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
12531da177e4SLinus Torvalds {
1254ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
1255b0765fb8SPavel Emelianov 
1256c7999c36SAl Viro 	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1257c7999c36SAl Viro 	p->cached_index = *pos;
1258c7999c36SAl Viro 	return p->cached_mount;
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
12621da177e4SLinus Torvalds {
1263390c6843SRam Pai 	up_read(&namespace_sem);
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds 
12660226f492SAl Viro static int m_show(struct seq_file *m, void *v)
12679f5596afSAl Viro {
1268ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
12691a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
12700226f492SAl Viro 	return p->show(m, &r->mnt);
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
1273a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
12741da177e4SLinus Torvalds 	.start	= m_start,
12751da177e4SLinus Torvalds 	.next	= m_next,
12761da177e4SLinus Torvalds 	.stop	= m_stop,
12770226f492SAl Viro 	.show	= m_show,
1278b4629fe2SChuck Lever };
1279a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1280b4629fe2SChuck Lever 
12811da177e4SLinus Torvalds /**
12821da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
12831da177e4SLinus Torvalds  * @mnt: root of mount tree
12841da177e4SLinus Torvalds  *
12851da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
12861da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
12871da177e4SLinus Torvalds  * busy.
12881da177e4SLinus Torvalds  */
1289909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
12901da177e4SLinus Torvalds {
1291909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
129236341f64SRam Pai 	int actual_refs = 0;
129336341f64SRam Pai 	int minimum_refs = 0;
1294315fc83eSAl Viro 	struct mount *p;
1295909b0a88SAl Viro 	BUG_ON(!m);
12961da177e4SLinus Torvalds 
1297b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1298719ea2fbSAl Viro 	lock_mount_hash();
1299909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
130083adc753SAl Viro 		actual_refs += mnt_get_count(p);
13011da177e4SLinus Torvalds 		minimum_refs += 2;
13021da177e4SLinus Torvalds 	}
1303719ea2fbSAl Viro 	unlock_mount_hash();
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
13061da177e4SLinus Torvalds 		return 0;
1307e3474a8eSIan Kent 
1308e3474a8eSIan Kent 	return 1;
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds /**
13141da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
13151da177e4SLinus Torvalds  * @mnt: root of mount
13161da177e4SLinus Torvalds  *
13171da177e4SLinus Torvalds  * This is called to check if a mount point has any
13181da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
13191da177e4SLinus Torvalds  * mount has sub mounts this will return busy
13201da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
13211da177e4SLinus Torvalds  *
13221da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
13231da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
13241da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
13251da177e4SLinus Torvalds  */
13261da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
13271da177e4SLinus Torvalds {
1328e3474a8eSIan Kent 	int ret = 1;
13298ad08d8aSAl Viro 	down_read(&namespace_sem);
1330719ea2fbSAl Viro 	lock_mount_hash();
13311ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1332e3474a8eSIan Kent 		ret = 0;
1333719ea2fbSAl Viro 	unlock_mount_hash();
13348ad08d8aSAl Viro 	up_read(&namespace_sem);
1335a05964f3SRam Pai 	return ret;
13361da177e4SLinus Torvalds }
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
13391da177e4SLinus Torvalds 
134038129a13SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
1341e3197d83SAl Viro 
134297216be0SAl Viro static void namespace_unlock(void)
13431da177e4SLinus Torvalds {
1344a3b3c562SEric W. Biederman 	struct hlist_head head;
134597216be0SAl Viro 
1346a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
1347a3b3c562SEric W. Biederman 
134897216be0SAl Viro 	up_write(&namespace_sem);
1349a3b3c562SEric W. Biederman 
1350a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
135197216be0SAl Viro 		return;
135297216be0SAl Viro 
135348a066e7SAl Viro 	synchronize_rcu();
135448a066e7SAl Viro 
135587b95ce0SAl Viro 	group_pin_kill(&head);
135670fbcdf4SRam Pai }
135770fbcdf4SRam Pai 
135897216be0SAl Viro static inline void namespace_lock(void)
1359e3197d83SAl Viro {
136097216be0SAl Viro 	down_write(&namespace_sem);
1361e3197d83SAl Viro }
1362e3197d83SAl Viro 
1363e819f152SEric W. Biederman enum umount_tree_flags {
1364e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1365e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1366e0c9c0afSEric W. Biederman 	UMOUNT_CONNECTED = 4,
1367e819f152SEric W. Biederman };
1368f2d0a123SEric W. Biederman 
1369f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1370f2d0a123SEric W. Biederman {
1371f2d0a123SEric W. Biederman 	/* Leaving mounts connected is only valid for lazy umounts */
1372f2d0a123SEric W. Biederman 	if (how & UMOUNT_SYNC)
1373f2d0a123SEric W. Biederman 		return true;
1374f2d0a123SEric W. Biederman 
1375f2d0a123SEric W. Biederman 	/* A mount without a parent has nothing to be connected to */
1376f2d0a123SEric W. Biederman 	if (!mnt_has_parent(mnt))
1377f2d0a123SEric W. Biederman 		return true;
1378f2d0a123SEric W. Biederman 
1379f2d0a123SEric W. Biederman 	/* Because the reference counting rules change when mounts are
1380f2d0a123SEric W. Biederman 	 * unmounted and connected, umounted mounts may not be
1381f2d0a123SEric W. Biederman 	 * connected to mounted mounts.
1382f2d0a123SEric W. Biederman 	 */
1383f2d0a123SEric W. Biederman 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1384f2d0a123SEric W. Biederman 		return true;
1385f2d0a123SEric W. Biederman 
1386f2d0a123SEric W. Biederman 	/* Has it been requested that the mount remain connected? */
1387f2d0a123SEric W. Biederman 	if (how & UMOUNT_CONNECTED)
1388f2d0a123SEric W. Biederman 		return false;
1389f2d0a123SEric W. Biederman 
1390f2d0a123SEric W. Biederman 	/* Is the mount locked such that it needs to remain connected? */
1391f2d0a123SEric W. Biederman 	if (IS_MNT_LOCKED(mnt))
1392f2d0a123SEric W. Biederman 		return false;
1393f2d0a123SEric W. Biederman 
1394f2d0a123SEric W. Biederman 	/* By default disconnect the mount */
1395f2d0a123SEric W. Biederman 	return true;
1396f2d0a123SEric W. Biederman }
1397f2d0a123SEric W. Biederman 
139899b7db7bSNick Piggin /*
139948a066e7SAl Viro  * mount_lock must be held
140099b7db7bSNick Piggin  * namespace_sem must be held for write
140199b7db7bSNick Piggin  */
1402e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
140370fbcdf4SRam Pai {
1404c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1405315fc83eSAl Viro 	struct mount *p;
140670fbcdf4SRam Pai 
14075d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14085d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
14095d88457eSEric W. Biederman 
1410c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1411590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1412590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1413c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1414590ce4bcSEric W. Biederman 	}
1415c003b26fSEric W. Biederman 
1416411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1417c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1418c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
141938129a13SAl Viro 	}
142070fbcdf4SRam Pai 
1421c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1422e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14237b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1424a05964f3SRam Pai 
1425c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1426d2921684SEric W. Biederman 		struct mnt_namespace *ns;
1427ce07d891SEric W. Biederman 		bool disconnect;
1428c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
14296776db3dSAl Viro 		list_del_init(&p->mnt_expire);
14301a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1431d2921684SEric W. Biederman 		ns = p->mnt_ns;
1432d2921684SEric W. Biederman 		if (ns) {
1433d2921684SEric W. Biederman 			ns->mounts--;
1434d2921684SEric W. Biederman 			__touch_mnt_namespace(ns);
1435d2921684SEric W. Biederman 		}
143663d37a84SAl Viro 		p->mnt_ns = NULL;
1437e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
143848a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
143987b95ce0SAl Viro 
1440f2d0a123SEric W. Biederman 		disconnect = disconnect_mount(p, how);
1441ce07d891SEric W. Biederman 
1442ce07d891SEric W. Biederman 		pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1443ce07d891SEric W. Biederman 				 disconnect ? &unmounted : NULL);
1444676da58dSAl Viro 		if (mnt_has_parent(p)) {
144581b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
1446ce07d891SEric W. Biederman 			if (!disconnect) {
1447ce07d891SEric W. Biederman 				/* Don't forget about p */
1448ce07d891SEric W. Biederman 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1449ce07d891SEric W. Biederman 			} else {
14506a46c573SEric W. Biederman 				umount_mnt(p);
14517c4b93d8SAl Viro 			}
1452ce07d891SEric W. Biederman 		}
14530f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
145438129a13SAl Viro 	}
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
1457b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1458c35038beSAl Viro 
14591ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
14601da177e4SLinus Torvalds {
14611ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
14621da177e4SLinus Torvalds 	int retval;
14631da177e4SLinus Torvalds 
14641ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
14651da177e4SLinus Torvalds 	if (retval)
14661da177e4SLinus Torvalds 		return retval;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	/*
14691da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
14701da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
14711da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
14721da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
14731da177e4SLinus Torvalds 	 */
14741da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
14751ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
14761da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
14771da177e4SLinus Torvalds 			return -EINVAL;
14781da177e4SLinus Torvalds 
1479b3e19d92SNick Piggin 		/*
1480b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1481b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1482b3e19d92SNick Piggin 		 */
1483719ea2fbSAl Viro 		lock_mount_hash();
148483adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1485719ea2fbSAl Viro 			unlock_mount_hash();
14861da177e4SLinus Torvalds 			return -EBUSY;
1487b3e19d92SNick Piggin 		}
1488719ea2fbSAl Viro 		unlock_mount_hash();
14891da177e4SLinus Torvalds 
1490863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
14911da177e4SLinus Torvalds 			return -EAGAIN;
14921da177e4SLinus Torvalds 	}
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	/*
14951da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
14961da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
14971da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
14981da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
14991da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
15001da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
15011da177e4SLinus Torvalds 	 * about for the moment.
15021da177e4SLinus Torvalds 	 */
15031da177e4SLinus Torvalds 
150442faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
150542faad99SAl Viro 		sb->s_op->umount_begin(sb);
150642faad99SAl Viro 	}
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	/*
15091da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
15101da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
15111da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
15121da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
15131da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
15141da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
15151da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
15161da177e4SLinus Torvalds 	 */
15171ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
15181da177e4SLinus Torvalds 		/*
15191da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
15201da177e4SLinus Torvalds 		 * we just try to remount it readonly.
15211da177e4SLinus Torvalds 		 */
1522a1480dccSAndy Lutomirski 		if (!capable(CAP_SYS_ADMIN))
1523a1480dccSAndy Lutomirski 			return -EPERM;
15241da177e4SLinus Torvalds 		down_write(&sb->s_umount);
15254aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
15261da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
15271da177e4SLinus Torvalds 		up_write(&sb->s_umount);
15281da177e4SLinus Torvalds 		return retval;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
153197216be0SAl Viro 	namespace_lock();
1532719ea2fbSAl Viro 	lock_mount_hash();
15335addc5ddSAl Viro 	event++;
15341da177e4SLinus Torvalds 
153548a066e7SAl Viro 	if (flags & MNT_DETACH) {
153648a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1537e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
153848a066e7SAl Viro 		retval = 0;
153948a066e7SAl Viro 	} else {
1540b54b9be7SAl Viro 		shrink_submounts(mnt);
15411da177e4SLinus Torvalds 		retval = -EBUSY;
154248a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
15431a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1544e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
15451da177e4SLinus Torvalds 			retval = 0;
15461da177e4SLinus Torvalds 		}
154748a066e7SAl Viro 	}
1548719ea2fbSAl Viro 	unlock_mount_hash();
1549e3197d83SAl Viro 	namespace_unlock();
15501da177e4SLinus Torvalds 	return retval;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds /*
155480b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
155580b5dce8SEric W. Biederman  *
155680b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
155780b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
155880b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
155980b5dce8SEric W. Biederman  * leaking them.
156080b5dce8SEric W. Biederman  *
156180b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
156280b5dce8SEric W. Biederman  */
156380b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
156480b5dce8SEric W. Biederman {
156580b5dce8SEric W. Biederman 	struct mountpoint *mp;
156680b5dce8SEric W. Biederman 	struct mount *mnt;
156780b5dce8SEric W. Biederman 
156880b5dce8SEric W. Biederman 	namespace_lock();
156980b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
1570f53e5797SEric W. Biederman 	if (IS_ERR_OR_NULL(mp))
157180b5dce8SEric W. Biederman 		goto out_unlock;
157280b5dce8SEric W. Biederman 
157380b5dce8SEric W. Biederman 	lock_mount_hash();
1574e06b933eSAndrey Ulanov 	event++;
157580b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
157680b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1577ce07d891SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1578fe78fcc8SEric W. Biederman 			hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1579fe78fcc8SEric W. Biederman 			umount_mnt(mnt);
1580ce07d891SEric W. Biederman 		}
1581e0c9c0afSEric W. Biederman 		else umount_tree(mnt, UMOUNT_CONNECTED);
158280b5dce8SEric W. Biederman 	}
158380b5dce8SEric W. Biederman 	unlock_mount_hash();
158480b5dce8SEric W. Biederman 	put_mountpoint(mp);
158580b5dce8SEric W. Biederman out_unlock:
158680b5dce8SEric W. Biederman 	namespace_unlock();
158780b5dce8SEric W. Biederman }
158880b5dce8SEric W. Biederman 
158980b5dce8SEric W. Biederman /*
15909b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
15919b40bc90SAl Viro  */
15929b40bc90SAl Viro static inline bool may_mount(void)
15939b40bc90SAl Viro {
15949b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
15959b40bc90SAl Viro }
15969b40bc90SAl Viro 
15979e8925b6SJeff Layton static inline bool may_mandlock(void)
15989e8925b6SJeff Layton {
15999e8925b6SJeff Layton #ifndef	CONFIG_MANDATORY_FILE_LOCKING
16009e8925b6SJeff Layton 	return false;
16019e8925b6SJeff Layton #endif
160295ace754SEric W. Biederman 	return capable(CAP_SYS_ADMIN);
16039e8925b6SJeff Layton }
16049e8925b6SJeff Layton 
16059b40bc90SAl Viro /*
16061da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
16071da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
16081da177e4SLinus Torvalds  *
16091da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
16101da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
16111da177e4SLinus Torvalds  */
16121da177e4SLinus Torvalds 
1613bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
16141da177e4SLinus Torvalds {
16152d8f3038SAl Viro 	struct path path;
1616900148dcSAl Viro 	struct mount *mnt;
16171da177e4SLinus Torvalds 	int retval;
1618db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
16191da177e4SLinus Torvalds 
1620db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1621db1f05bbSMiklos Szeredi 		return -EINVAL;
1622db1f05bbSMiklos Szeredi 
16239b40bc90SAl Viro 	if (!may_mount())
16249b40bc90SAl Viro 		return -EPERM;
16259b40bc90SAl Viro 
1626db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1627db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1628db1f05bbSMiklos Szeredi 
1629197df04cSAl Viro 	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
16301da177e4SLinus Torvalds 	if (retval)
16311da177e4SLinus Torvalds 		goto out;
1632900148dcSAl Viro 	mnt = real_mount(path.mnt);
16331da177e4SLinus Torvalds 	retval = -EINVAL;
16342d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
16351da177e4SLinus Torvalds 		goto dput_and_out;
1636143c8c91SAl Viro 	if (!check_mnt(mnt))
16371da177e4SLinus Torvalds 		goto dput_and_out;
16385ff9d8a6SEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
16395ff9d8a6SEric W. Biederman 		goto dput_and_out;
1640b2f5d4dcSEric W. Biederman 	retval = -EPERM;
1641b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1642b2f5d4dcSEric W. Biederman 		goto dput_and_out;
16431da177e4SLinus Torvalds 
1644900148dcSAl Viro 	retval = do_umount(mnt, flags);
16451da177e4SLinus Torvalds dput_and_out:
1646429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
16472d8f3038SAl Viro 	dput(path.dentry);
1648900148dcSAl Viro 	mntput_no_expire(mnt);
16491da177e4SLinus Torvalds out:
16501da177e4SLinus Torvalds 	return retval;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /*
16561da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
16571da177e4SLinus Torvalds  */
1658bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
16591da177e4SLinus Torvalds {
16601da177e4SLinus Torvalds 	return sys_umount(name, 0);
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds #endif
16641da177e4SLinus Torvalds 
16654ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
16668823c079SEric W. Biederman {
16674ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1668e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1669e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
16704ce5d2b1SEric W. Biederman }
16714ce5d2b1SEric W. Biederman 
167258be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
167358be2825SAl Viro {
167458be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
167558be2825SAl Viro }
167658be2825SAl Viro 
16774ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
16784ce5d2b1SEric W. Biederman {
16794ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
16804ce5d2b1SEric W. Biederman 	 * mount namespace loop?
16814ce5d2b1SEric W. Biederman 	 */
16824ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
16834ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
16844ce5d2b1SEric W. Biederman 		return false;
16854ce5d2b1SEric W. Biederman 
1686f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
16878823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
16888823c079SEric W. Biederman }
16898823c079SEric W. Biederman 
169087129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
169136341f64SRam Pai 					int flag)
16921da177e4SLinus Torvalds {
169384d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
16941da177e4SLinus Torvalds 
16954ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
16964ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
16974ce5d2b1SEric W. Biederman 
16984ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1699be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
17009676f0c6SRam Pai 
170136341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1702be34d1a3SDavid Howells 	if (IS_ERR(q))
1703be34d1a3SDavid Howells 		return q;
1704be34d1a3SDavid Howells 
1705a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	p = mnt;
17086b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1709315fc83eSAl Viro 		struct mount *s;
17107ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
17111da177e4SLinus Torvalds 			continue;
17121da177e4SLinus Torvalds 
1713909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
171412a5b529SAl Viro 			struct mount *t = NULL;
17154ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
17164ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
17174ce5d2b1SEric W. Biederman 				s = skip_mnt_tree(s);
17184ce5d2b1SEric W. Biederman 				continue;
17194ce5d2b1SEric W. Biederman 			}
17204ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
17214ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
17229676f0c6SRam Pai 				s = skip_mnt_tree(s);
17239676f0c6SRam Pai 				continue;
17249676f0c6SRam Pai 			}
17250714a533SAl Viro 			while (p != s->mnt_parent) {
17260714a533SAl Viro 				p = p->mnt_parent;
17270714a533SAl Viro 				q = q->mnt_parent;
17281da177e4SLinus Torvalds 			}
172987129cc0SAl Viro 			p = s;
173084d17192SAl Viro 			parent = q;
173187129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1732be34d1a3SDavid Howells 			if (IS_ERR(q))
1733be34d1a3SDavid Howells 				goto out;
1734719ea2fbSAl Viro 			lock_mount_hash();
17351a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
173612a5b529SAl Viro 			mnt_set_mountpoint(parent, p->mnt_mp, q);
173712a5b529SAl Viro 			if (!list_empty(&parent->mnt_mounts)) {
173812a5b529SAl Viro 				t = list_last_entry(&parent->mnt_mounts,
173912a5b529SAl Viro 					struct mount, mnt_child);
174012a5b529SAl Viro 				if (t->mnt_mp != p->mnt_mp)
174112a5b529SAl Viro 					t = NULL;
174212a5b529SAl Viro 			}
174312a5b529SAl Viro 			attach_shadowed(q, parent, t);
1744719ea2fbSAl Viro 			unlock_mount_hash();
17451da177e4SLinus Torvalds 		}
17461da177e4SLinus Torvalds 	}
17471da177e4SLinus Torvalds 	return res;
1748be34d1a3SDavid Howells out:
17491da177e4SLinus Torvalds 	if (res) {
1750719ea2fbSAl Viro 		lock_mount_hash();
1751e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
1752719ea2fbSAl Viro 		unlock_mount_hash();
17531da177e4SLinus Torvalds 	}
1754be34d1a3SDavid Howells 	return q;
17551da177e4SLinus Torvalds }
17561da177e4SLinus Torvalds 
1757be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1758be34d1a3SDavid Howells 
1759589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
17608aec0809SAl Viro {
1761cb338d06SAl Viro 	struct mount *tree;
176297216be0SAl Viro 	namespace_lock();
1763cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
1764cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
1765cd4a4017SEric W. Biederman 	else
176687129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
176787129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
1768328e6d90SAl Viro 	namespace_unlock();
1769be34d1a3SDavid Howells 	if (IS_ERR(tree))
177052e220d3SDan Carpenter 		return ERR_CAST(tree);
1771be34d1a3SDavid Howells 	return &tree->mnt;
17728aec0809SAl Viro }
17738aec0809SAl Viro 
17748aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
17758aec0809SAl Viro {
177697216be0SAl Viro 	namespace_lock();
1777719ea2fbSAl Viro 	lock_mount_hash();
1778e819f152SEric W. Biederman 	umount_tree(real_mount(mnt), UMOUNT_SYNC);
1779719ea2fbSAl Viro 	unlock_mount_hash();
17803ab6abeeSAl Viro 	namespace_unlock();
17818aec0809SAl Viro }
17828aec0809SAl Viro 
1783c771d683SMiklos Szeredi /**
1784c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
1785c771d683SMiklos Szeredi  *
1786c771d683SMiklos Szeredi  * This creates a new vfsmount, which will be the clone of @path.  The new will
1787c771d683SMiklos Szeredi  * not be attached anywhere in the namespace and will be private (i.e. changes
1788c771d683SMiklos Szeredi  * to the originating mount won't be propagated into this).
1789c771d683SMiklos Szeredi  *
1790c771d683SMiklos Szeredi  * Release with mntput().
1791c771d683SMiklos Szeredi  */
1792c771d683SMiklos Szeredi struct vfsmount *clone_private_mount(struct path *path)
1793c771d683SMiklos Szeredi {
1794c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
1795c771d683SMiklos Szeredi 	struct mount *new_mnt;
1796c771d683SMiklos Szeredi 
1797c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
1798c771d683SMiklos Szeredi 		return ERR_PTR(-EINVAL);
1799c771d683SMiklos Szeredi 
1800c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1801c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
1802c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
1803c771d683SMiklos Szeredi 
1804c771d683SMiklos Szeredi 	return &new_mnt->mnt;
1805c771d683SMiklos Szeredi }
1806c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
1807c771d683SMiklos Szeredi 
18081f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
18091f707137SAl Viro 		   struct vfsmount *root)
18101f707137SAl Viro {
18111a4eeaf2SAl Viro 	struct mount *mnt;
18121f707137SAl Viro 	int res = f(root, arg);
18131f707137SAl Viro 	if (res)
18141f707137SAl Viro 		return res;
18151a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
18161a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
18171f707137SAl Viro 		if (res)
18181f707137SAl Viro 			return res;
18191f707137SAl Viro 	}
18201f707137SAl Viro 	return 0;
18211f707137SAl Viro }
18221f707137SAl Viro 
18234b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1824719f5d7fSMiklos Szeredi {
1825315fc83eSAl Viro 	struct mount *p;
1826719f5d7fSMiklos Szeredi 
1827909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1828fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
18294b8b21f4SAl Viro 			mnt_release_group_id(p);
1830719f5d7fSMiklos Szeredi 	}
1831719f5d7fSMiklos Szeredi }
1832719f5d7fSMiklos Szeredi 
18334b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1834719f5d7fSMiklos Szeredi {
1835315fc83eSAl Viro 	struct mount *p;
1836719f5d7fSMiklos Szeredi 
1837909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1838fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
18394b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1840719f5d7fSMiklos Szeredi 			if (err) {
18414b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1842719f5d7fSMiklos Szeredi 				return err;
1843719f5d7fSMiklos Szeredi 			}
1844719f5d7fSMiklos Szeredi 		}
1845719f5d7fSMiklos Szeredi 	}
1846719f5d7fSMiklos Szeredi 
1847719f5d7fSMiklos Szeredi 	return 0;
1848719f5d7fSMiklos Szeredi }
1849719f5d7fSMiklos Szeredi 
1850d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1851d2921684SEric W. Biederman {
1852d2921684SEric W. Biederman 	unsigned int max = READ_ONCE(sysctl_mount_max);
1853d2921684SEric W. Biederman 	unsigned int mounts = 0, old, pending, sum;
1854d2921684SEric W. Biederman 	struct mount *p;
1855d2921684SEric W. Biederman 
1856d2921684SEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt))
1857d2921684SEric W. Biederman 		mounts++;
1858d2921684SEric W. Biederman 
1859d2921684SEric W. Biederman 	old = ns->mounts;
1860d2921684SEric W. Biederman 	pending = ns->pending_mounts;
1861d2921684SEric W. Biederman 	sum = old + pending;
1862d2921684SEric W. Biederman 	if ((old > sum) ||
1863d2921684SEric W. Biederman 	    (pending > sum) ||
1864d2921684SEric W. Biederman 	    (max < sum) ||
1865d2921684SEric W. Biederman 	    (mounts > (max - sum)))
1866d2921684SEric W. Biederman 		return -ENOSPC;
1867d2921684SEric W. Biederman 
1868d2921684SEric W. Biederman 	ns->pending_mounts = pending + mounts;
1869d2921684SEric W. Biederman 	return 0;
1870d2921684SEric W. Biederman }
1871d2921684SEric W. Biederman 
1872b90fa9aeSRam Pai /*
1873b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1874b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
187521444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
187621444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
187721444403SRam Pai  *  		   (done when source_mnt is moved)
1878b90fa9aeSRam Pai  *
1879b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1880b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
18819676f0c6SRam Pai  * ---------------------------------------------------------------------------
1882b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
18839676f0c6SRam Pai  * |**************************************************************************
18849676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
18859676f0c6SRam Pai  * | dest     |               |                |                |            |
18869676f0c6SRam Pai  * |   |      |               |                |                |            |
18879676f0c6SRam Pai  * |   v      |               |                |                |            |
18889676f0c6SRam Pai  * |**************************************************************************
18899676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
18905afe0022SRam Pai  * |          |               |                |                |            |
18919676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
18929676f0c6SRam Pai  * ***************************************************************************
1893b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1894b90fa9aeSRam Pai  * destination mount.
1895b90fa9aeSRam Pai  *
1896b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1897b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1898b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1899b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1900b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1901b90fa9aeSRam Pai  *       mount.
19025afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
19035afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
19045afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
19055afe0022SRam Pai  *       is marked as 'shared and slave'.
19065afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
19075afe0022SRam Pai  * 	 source mount.
19085afe0022SRam Pai  *
19099676f0c6SRam Pai  * ---------------------------------------------------------------------------
191021444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
19119676f0c6SRam Pai  * |**************************************************************************
19129676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
19139676f0c6SRam Pai  * | dest     |               |                |                |            |
19149676f0c6SRam Pai  * |   |      |               |                |                |            |
19159676f0c6SRam Pai  * |   v      |               |                |                |            |
19169676f0c6SRam Pai  * |**************************************************************************
19179676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
19185afe0022SRam Pai  * |          |               |                |                |            |
19199676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
19209676f0c6SRam Pai  * ***************************************************************************
19215afe0022SRam Pai  *
19225afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
19235afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
192421444403SRam Pai  * (+*)  the mount is moved to the destination.
19255afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
19265afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
19275afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
19285afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1929b90fa9aeSRam Pai  *
1930b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1931b90fa9aeSRam Pai  * applied to each mount in the tree.
1932b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1933b90fa9aeSRam Pai  * in allocations.
1934b90fa9aeSRam Pai  */
19350fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
193684d17192SAl Viro 			struct mount *dest_mnt,
193784d17192SAl Viro 			struct mountpoint *dest_mp,
193884d17192SAl Viro 			struct path *parent_path)
1939b90fa9aeSRam Pai {
194038129a13SAl Viro 	HLIST_HEAD(tree_list);
1941d2921684SEric W. Biederman 	struct mnt_namespace *ns = dest_mnt->mnt_ns;
1942315fc83eSAl Viro 	struct mount *child, *p;
194338129a13SAl Viro 	struct hlist_node *n;
1944719f5d7fSMiklos Szeredi 	int err;
1945b90fa9aeSRam Pai 
1946d2921684SEric W. Biederman 	/* Is there space to add these mounts to the mount namespace? */
1947d2921684SEric W. Biederman 	if (!parent_path) {
1948d2921684SEric W. Biederman 		err = count_mounts(ns, source_mnt);
1949d2921684SEric W. Biederman 		if (err)
1950d2921684SEric W. Biederman 			goto out;
1951d2921684SEric W. Biederman 	}
1952d2921684SEric W. Biederman 
1953fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
19540fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
1955719f5d7fSMiklos Szeredi 		if (err)
1956719f5d7fSMiklos Szeredi 			goto out;
195784d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1958f2ebb3a9SAl Viro 		lock_mount_hash();
1959719f5d7fSMiklos Szeredi 		if (err)
1960719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
1961909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
19620f0afb1dSAl Viro 			set_mnt_shared(p);
19630b1b901bSAl Viro 	} else {
19640b1b901bSAl Viro 		lock_mount_hash();
1965b90fa9aeSRam Pai 	}
19661a390689SAl Viro 	if (parent_path) {
19670fb54e50SAl Viro 		detach_mnt(source_mnt, parent_path);
196884d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
1969143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
197021444403SRam Pai 	} else {
197184d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
19721d6a32acSAl Viro 		commit_tree(source_mnt, NULL);
197321444403SRam Pai 	}
1974b90fa9aeSRam Pai 
197538129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
19761d6a32acSAl Viro 		struct mount *q;
197738129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
19781d6a32acSAl Viro 		q = __lookup_mnt_last(&child->mnt_parent->mnt,
19791d6a32acSAl Viro 				      child->mnt_mountpoint);
19801d6a32acSAl Viro 		commit_tree(child, q);
1981b90fa9aeSRam Pai 	}
1982719ea2fbSAl Viro 	unlock_mount_hash();
198399b7db7bSNick Piggin 
1984b90fa9aeSRam Pai 	return 0;
1985719f5d7fSMiklos Szeredi 
1986719f5d7fSMiklos Szeredi  out_cleanup_ids:
1987f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
1988f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
1989d2921684SEric W. Biederman 		child->mnt_parent->mnt_ns->pending_mounts = 0;
1990e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
1991f2ebb3a9SAl Viro 	}
1992f2ebb3a9SAl Viro 	unlock_mount_hash();
19930fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
1994719f5d7fSMiklos Szeredi  out:
1995d2921684SEric W. Biederman 	ns->pending_mounts = 0;
1996719f5d7fSMiklos Szeredi 	return err;
1997b90fa9aeSRam Pai }
1998b90fa9aeSRam Pai 
199984d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
2000b12cea91SAl Viro {
2001b12cea91SAl Viro 	struct vfsmount *mnt;
200284d17192SAl Viro 	struct dentry *dentry = path->dentry;
2003b12cea91SAl Viro retry:
20045955102cSAl Viro 	inode_lock(dentry->d_inode);
200584d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
20065955102cSAl Viro 		inode_unlock(dentry->d_inode);
200784d17192SAl Viro 		return ERR_PTR(-ENOENT);
2008b12cea91SAl Viro 	}
200997216be0SAl Viro 	namespace_lock();
2010b12cea91SAl Viro 	mnt = lookup_mnt(path);
201184d17192SAl Viro 	if (likely(!mnt)) {
2012e2dfa935SEric W. Biederman 		struct mountpoint *mp = lookup_mountpoint(dentry);
2013e2dfa935SEric W. Biederman 		if (!mp)
2014e2dfa935SEric W. Biederman 			mp = new_mountpoint(dentry);
201584d17192SAl Viro 		if (IS_ERR(mp)) {
201697216be0SAl Viro 			namespace_unlock();
20175955102cSAl Viro 			inode_unlock(dentry->d_inode);
201884d17192SAl Viro 			return mp;
201984d17192SAl Viro 		}
202084d17192SAl Viro 		return mp;
202184d17192SAl Viro 	}
202297216be0SAl Viro 	namespace_unlock();
20235955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
2024b12cea91SAl Viro 	path_put(path);
2025b12cea91SAl Viro 	path->mnt = mnt;
202684d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
2027b12cea91SAl Viro 	goto retry;
2028b12cea91SAl Viro }
2029b12cea91SAl Viro 
203084d17192SAl Viro static void unlock_mount(struct mountpoint *where)
2031b12cea91SAl Viro {
203284d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
203384d17192SAl Viro 	put_mountpoint(where);
2034328e6d90SAl Viro 	namespace_unlock();
20355955102cSAl Viro 	inode_unlock(dentry->d_inode);
2036b12cea91SAl Viro }
2037b12cea91SAl Viro 
203884d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
20391da177e4SLinus Torvalds {
204095bc5f25SAl Viro 	if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
20411da177e4SLinus Torvalds 		return -EINVAL;
20421da177e4SLinus Torvalds 
2043e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
2044e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
20451da177e4SLinus Torvalds 		return -ENOTDIR;
20461da177e4SLinus Torvalds 
204784d17192SAl Viro 	return attach_recursive_mnt(mnt, p, mp, NULL);
20481da177e4SLinus Torvalds }
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds /*
20517a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
20527a2e8a8fSValerie Aurora  */
20537a2e8a8fSValerie Aurora 
20547a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags)
20557a2e8a8fSValerie Aurora {
20567c6e984dSRoman Borisov 	int type = flags & ~(MS_REC | MS_SILENT);
20577a2e8a8fSValerie Aurora 
20587a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
20597a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
20607a2e8a8fSValerie Aurora 		return 0;
20617a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
20627a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
20637a2e8a8fSValerie Aurora 		return 0;
20647a2e8a8fSValerie Aurora 	return type;
20657a2e8a8fSValerie Aurora }
20667a2e8a8fSValerie Aurora 
20677a2e8a8fSValerie Aurora /*
206807b20889SRam Pai  * recursively change the type of the mountpoint.
206907b20889SRam Pai  */
20700a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
207107b20889SRam Pai {
2072315fc83eSAl Viro 	struct mount *m;
20734b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
207407b20889SRam Pai 	int recurse = flag & MS_REC;
20757a2e8a8fSValerie Aurora 	int type;
2076719f5d7fSMiklos Szeredi 	int err = 0;
207707b20889SRam Pai 
20782d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
207907b20889SRam Pai 		return -EINVAL;
208007b20889SRam Pai 
20817a2e8a8fSValerie Aurora 	type = flags_to_propagation_type(flag);
20827a2e8a8fSValerie Aurora 	if (!type)
20837a2e8a8fSValerie Aurora 		return -EINVAL;
20847a2e8a8fSValerie Aurora 
208597216be0SAl Viro 	namespace_lock();
2086719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
2087719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
2088719f5d7fSMiklos Szeredi 		if (err)
2089719f5d7fSMiklos Szeredi 			goto out_unlock;
2090719f5d7fSMiklos Szeredi 	}
2091719f5d7fSMiklos Szeredi 
2092719ea2fbSAl Viro 	lock_mount_hash();
2093909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
20940f0afb1dSAl Viro 		change_mnt_propagation(m, type);
2095719ea2fbSAl Viro 	unlock_mount_hash();
2096719f5d7fSMiklos Szeredi 
2097719f5d7fSMiklos Szeredi  out_unlock:
209897216be0SAl Viro 	namespace_unlock();
2099719f5d7fSMiklos Szeredi 	return err;
210007b20889SRam Pai }
210107b20889SRam Pai 
21025ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
21035ff9d8a6SEric W. Biederman {
21045ff9d8a6SEric W. Biederman 	struct mount *child;
21055ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
21065ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
21075ff9d8a6SEric W. Biederman 			continue;
21085ff9d8a6SEric W. Biederman 
21095ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
21105ff9d8a6SEric W. Biederman 			return true;
21115ff9d8a6SEric W. Biederman 	}
21125ff9d8a6SEric W. Biederman 	return false;
21135ff9d8a6SEric W. Biederman }
21145ff9d8a6SEric W. Biederman 
211507b20889SRam Pai /*
21161da177e4SLinus Torvalds  * do loopback mount.
21171da177e4SLinus Torvalds  */
2118808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
21192dafe1c4SEric Sandeen 				int recurse)
21201da177e4SLinus Torvalds {
21212d92ab3cSAl Viro 	struct path old_path;
212284d17192SAl Viro 	struct mount *mnt = NULL, *old, *parent;
212384d17192SAl Viro 	struct mountpoint *mp;
212457eccb83SAl Viro 	int err;
21251da177e4SLinus Torvalds 	if (!old_name || !*old_name)
21261da177e4SLinus Torvalds 		return -EINVAL;
2127815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
21281da177e4SLinus Torvalds 	if (err)
21291da177e4SLinus Torvalds 		return err;
21301da177e4SLinus Torvalds 
21318823c079SEric W. Biederman 	err = -EINVAL;
21324ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
21338823c079SEric W. Biederman 		goto out;
21348823c079SEric W. Biederman 
213584d17192SAl Viro 	mp = lock_mount(path);
213684d17192SAl Viro 	err = PTR_ERR(mp);
213784d17192SAl Viro 	if (IS_ERR(mp))
21389676f0c6SRam Pai 		goto out;
21399676f0c6SRam Pai 
214087129cc0SAl Viro 	old = real_mount(old_path.mnt);
214184d17192SAl Viro 	parent = real_mount(path->mnt);
214287129cc0SAl Viro 
2143b12cea91SAl Viro 	err = -EINVAL;
2144fc7be130SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2145b12cea91SAl Viro 		goto out2;
2146b12cea91SAl Viro 
2147e149ed2bSAl Viro 	if (!check_mnt(parent))
2148e149ed2bSAl Viro 		goto out2;
2149e149ed2bSAl Viro 
2150e149ed2bSAl Viro 	if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
2151b12cea91SAl Viro 		goto out2;
2152ccd48bc7SAl Viro 
21535ff9d8a6SEric W. Biederman 	if (!recurse && has_locked_children(old, old_path.dentry))
21545ff9d8a6SEric W. Biederman 		goto out2;
21555ff9d8a6SEric W. Biederman 
21561da177e4SLinus Torvalds 	if (recurse)
21574ce5d2b1SEric W. Biederman 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
21581da177e4SLinus Torvalds 	else
215987129cc0SAl Viro 		mnt = clone_mnt(old, old_path.dentry, 0);
21601da177e4SLinus Torvalds 
2161be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2162be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2163e9c5d8a5SAndrey Vagin 		goto out2;
2164be34d1a3SDavid Howells 	}
2165ccd48bc7SAl Viro 
21665ff9d8a6SEric W. Biederman 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
21675ff9d8a6SEric W. Biederman 
216884d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
21691da177e4SLinus Torvalds 	if (err) {
2170719ea2fbSAl Viro 		lock_mount_hash();
2171e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2172719ea2fbSAl Viro 		unlock_mount_hash();
21735b83d2c5SRam Pai 	}
2174b12cea91SAl Viro out2:
217584d17192SAl Viro 	unlock_mount(mp);
2176ccd48bc7SAl Viro out:
21772d92ab3cSAl Viro 	path_put(&old_path);
21781da177e4SLinus Torvalds 	return err;
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds 
21812e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
21822e4b7fcdSDave Hansen {
21832e4b7fcdSDave Hansen 	int error = 0;
21842e4b7fcdSDave Hansen 	int readonly_request = 0;
21852e4b7fcdSDave Hansen 
21862e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
21872e4b7fcdSDave Hansen 		readonly_request = 1;
21882e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
21892e4b7fcdSDave Hansen 		return 0;
21902e4b7fcdSDave Hansen 
21912e4b7fcdSDave Hansen 	if (readonly_request)
219283adc753SAl Viro 		error = mnt_make_readonly(real_mount(mnt));
21932e4b7fcdSDave Hansen 	else
219483adc753SAl Viro 		__mnt_unmake_readonly(real_mount(mnt));
21952e4b7fcdSDave Hansen 	return error;
21962e4b7fcdSDave Hansen }
21972e4b7fcdSDave Hansen 
21981da177e4SLinus Torvalds /*
21991da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
22001da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
22011da177e4SLinus Torvalds  * on it - tough luck.
22021da177e4SLinus Torvalds  */
22030a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
22041da177e4SLinus Torvalds 		      void *data)
22051da177e4SLinus Torvalds {
22061da177e4SLinus Torvalds 	int err;
22072d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2208143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
22091da177e4SLinus Torvalds 
2210143c8c91SAl Viro 	if (!check_mnt(mnt))
22111da177e4SLinus Torvalds 		return -EINVAL;
22121da177e4SLinus Torvalds 
22132d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
22141da177e4SLinus Torvalds 		return -EINVAL;
22151da177e4SLinus Torvalds 
221607b64558SEric W. Biederman 	/* Don't allow changing of locked mnt flags.
221707b64558SEric W. Biederman 	 *
221807b64558SEric W. Biederman 	 * No locks need to be held here while testing the various
221907b64558SEric W. Biederman 	 * MNT_LOCK flags because those flags can never be cleared
222007b64558SEric W. Biederman 	 * once they are set.
222107b64558SEric W. Biederman 	 */
222207b64558SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
222307b64558SEric W. Biederman 	    !(mnt_flags & MNT_READONLY)) {
222407b64558SEric W. Biederman 		return -EPERM;
222507b64558SEric W. Biederman 	}
22269566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
22279566d674SEric W. Biederman 	    !(mnt_flags & MNT_NODEV)) {
22289566d674SEric W. Biederman 		return -EPERM;
22299566d674SEric W. Biederman 	}
22309566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
22319566d674SEric W. Biederman 	    !(mnt_flags & MNT_NOSUID)) {
22329566d674SEric W. Biederman 		return -EPERM;
22339566d674SEric W. Biederman 	}
22349566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
22359566d674SEric W. Biederman 	    !(mnt_flags & MNT_NOEXEC)) {
22369566d674SEric W. Biederman 		return -EPERM;
22379566d674SEric W. Biederman 	}
22389566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
22399566d674SEric W. Biederman 	    ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
22409566d674SEric W. Biederman 		return -EPERM;
22419566d674SEric W. Biederman 	}
22429566d674SEric W. Biederman 
2243ff36fe2cSEric Paris 	err = security_sb_remount(sb, data);
2244ff36fe2cSEric Paris 	if (err)
2245ff36fe2cSEric Paris 		return err;
2246ff36fe2cSEric Paris 
22471da177e4SLinus Torvalds 	down_write(&sb->s_umount);
22482e4b7fcdSDave Hansen 	if (flags & MS_BIND)
22492d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
225057eccb83SAl Viro 	else if (!capable(CAP_SYS_ADMIN))
225157eccb83SAl Viro 		err = -EPERM;
22524aa98cf7SAl Viro 	else
22531da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
22547b43a79fSAl Viro 	if (!err) {
2255719ea2fbSAl Viro 		lock_mount_hash();
2256a6138db8SEric W. Biederman 		mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2257143c8c91SAl Viro 		mnt->mnt.mnt_flags = mnt_flags;
2258143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2259719ea2fbSAl Viro 		unlock_mount_hash();
22600e55a7ccSDan Williams 	}
22616339dab8SAl Viro 	up_write(&sb->s_umount);
22621da177e4SLinus Torvalds 	return err;
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
2265cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
22669676f0c6SRam Pai {
2267315fc83eSAl Viro 	struct mount *p;
2268909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2269fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
22709676f0c6SRam Pai 			return 1;
22719676f0c6SRam Pai 	}
22729676f0c6SRam Pai 	return 0;
22739676f0c6SRam Pai }
22749676f0c6SRam Pai 
2275808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name)
22761da177e4SLinus Torvalds {
22772d92ab3cSAl Viro 	struct path old_path, parent_path;
2278676da58dSAl Viro 	struct mount *p;
22790fb54e50SAl Viro 	struct mount *old;
228084d17192SAl Viro 	struct mountpoint *mp;
228157eccb83SAl Viro 	int err;
22821da177e4SLinus Torvalds 	if (!old_name || !*old_name)
22831da177e4SLinus Torvalds 		return -EINVAL;
22842d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
22851da177e4SLinus Torvalds 	if (err)
22861da177e4SLinus Torvalds 		return err;
22871da177e4SLinus Torvalds 
228884d17192SAl Viro 	mp = lock_mount(path);
228984d17192SAl Viro 	err = PTR_ERR(mp);
229084d17192SAl Viro 	if (IS_ERR(mp))
2291cc53ce53SDavid Howells 		goto out;
2292cc53ce53SDavid Howells 
2293143c8c91SAl Viro 	old = real_mount(old_path.mnt);
2294fc7be130SAl Viro 	p = real_mount(path->mnt);
2295143c8c91SAl Viro 
22961da177e4SLinus Torvalds 	err = -EINVAL;
2297fc7be130SAl Viro 	if (!check_mnt(p) || !check_mnt(old))
22981da177e4SLinus Torvalds 		goto out1;
22991da177e4SLinus Torvalds 
23005ff9d8a6SEric W. Biederman 	if (old->mnt.mnt_flags & MNT_LOCKED)
23015ff9d8a6SEric W. Biederman 		goto out1;
23025ff9d8a6SEric W. Biederman 
23031da177e4SLinus Torvalds 	err = -EINVAL;
23042d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
230521444403SRam Pai 		goto out1;
23061da177e4SLinus Torvalds 
2307676da58dSAl Viro 	if (!mnt_has_parent(old))
230821444403SRam Pai 		goto out1;
23091da177e4SLinus Torvalds 
2310e36cb0b8SDavid Howells 	if (d_is_dir(path->dentry) !=
2311e36cb0b8SDavid Howells 	      d_is_dir(old_path.dentry))
231221444403SRam Pai 		goto out1;
231321444403SRam Pai 	/*
231421444403SRam Pai 	 * Don't move a mount residing in a shared parent.
231521444403SRam Pai 	 */
2316fc7be130SAl Viro 	if (IS_MNT_SHARED(old->mnt_parent))
231721444403SRam Pai 		goto out1;
23189676f0c6SRam Pai 	/*
23199676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
23209676f0c6SRam Pai 	 * mount which is shared.
23219676f0c6SRam Pai 	 */
2322fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
23239676f0c6SRam Pai 		goto out1;
23241da177e4SLinus Torvalds 	err = -ELOOP;
2325fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2326676da58dSAl Viro 		if (p == old)
232721444403SRam Pai 			goto out1;
23281da177e4SLinus Torvalds 
232984d17192SAl Viro 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
23304ac91378SJan Blunck 	if (err)
233121444403SRam Pai 		goto out1;
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
23341da177e4SLinus Torvalds 	 * automatically */
23356776db3dSAl Viro 	list_del_init(&old->mnt_expire);
23361da177e4SLinus Torvalds out1:
233784d17192SAl Viro 	unlock_mount(mp);
23381da177e4SLinus Torvalds out:
23391da177e4SLinus Torvalds 	if (!err)
23401a390689SAl Viro 		path_put(&parent_path);
23412d92ab3cSAl Viro 	path_put(&old_path);
23421da177e4SLinus Torvalds 	return err;
23431da177e4SLinus Torvalds }
23441da177e4SLinus Torvalds 
23459d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
23469d412a43SAl Viro {
23479d412a43SAl Viro 	int err;
23489d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
23499d412a43SAl Viro 	if (subtype) {
23509d412a43SAl Viro 		subtype++;
23519d412a43SAl Viro 		err = -EINVAL;
23529d412a43SAl Viro 		if (!subtype[0])
23539d412a43SAl Viro 			goto err;
23549d412a43SAl Viro 	} else
23559d412a43SAl Viro 		subtype = "";
23569d412a43SAl Viro 
23579d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
23589d412a43SAl Viro 	err = -ENOMEM;
23599d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
23609d412a43SAl Viro 		goto err;
23619d412a43SAl Viro 	return mnt;
23629d412a43SAl Viro 
23639d412a43SAl Viro  err:
23649d412a43SAl Viro 	mntput(mnt);
23659d412a43SAl Viro 	return ERR_PTR(err);
23669d412a43SAl Viro }
23679d412a43SAl Viro 
23689d412a43SAl Viro /*
23699d412a43SAl Viro  * add a mount into a namespace's mount tree
23709d412a43SAl Viro  */
237195bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
23729d412a43SAl Viro {
237384d17192SAl Viro 	struct mountpoint *mp;
237484d17192SAl Viro 	struct mount *parent;
23759d412a43SAl Viro 	int err;
23769d412a43SAl Viro 
2377f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
23789d412a43SAl Viro 
237984d17192SAl Viro 	mp = lock_mount(path);
238084d17192SAl Viro 	if (IS_ERR(mp))
238184d17192SAl Viro 		return PTR_ERR(mp);
23829d412a43SAl Viro 
238384d17192SAl Viro 	parent = real_mount(path->mnt);
23849d412a43SAl Viro 	err = -EINVAL;
238584d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2386156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2387156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
23889d412a43SAl Viro 			goto unlock;
2389156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
239084d17192SAl Viro 		if (!parent->mnt_ns)
2391156cacb1SAl Viro 			goto unlock;
2392156cacb1SAl Viro 	}
23939d412a43SAl Viro 
23949d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
23959d412a43SAl Viro 	err = -EBUSY;
239695bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
23979d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
23989d412a43SAl Viro 		goto unlock;
23999d412a43SAl Viro 
24009d412a43SAl Viro 	err = -EINVAL;
2401e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
24029d412a43SAl Viro 		goto unlock;
24039d412a43SAl Viro 
240495bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
240584d17192SAl Viro 	err = graft_tree(newmnt, parent, mp);
24069d412a43SAl Viro 
24079d412a43SAl Viro unlock:
240884d17192SAl Viro 	unlock_mount(mp);
24099d412a43SAl Viro 	return err;
24109d412a43SAl Viro }
2411b1e75df4SAl Viro 
24128654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
24131b852bceSEric W. Biederman 
24141da177e4SLinus Torvalds /*
24151da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
24161da177e4SLinus Torvalds  * namespace's tree
24171da177e4SLinus Torvalds  */
24180c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags,
2419808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
24201da177e4SLinus Torvalds {
24210c55cfc4SEric W. Biederman 	struct file_system_type *type;
24221da177e4SLinus Torvalds 	struct vfsmount *mnt;
242315f9a3f3SAl Viro 	int err;
24241da177e4SLinus Torvalds 
24250c55cfc4SEric W. Biederman 	if (!fstype)
24261da177e4SLinus Torvalds 		return -EINVAL;
24271da177e4SLinus Torvalds 
24280c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
24290c55cfc4SEric W. Biederman 	if (!type)
24300c55cfc4SEric W. Biederman 		return -ENODEV;
24310c55cfc4SEric W. Biederman 
24320c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, flags, name, data);
24330c55cfc4SEric W. Biederman 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
24340c55cfc4SEric W. Biederman 	    !mnt->mnt_sb->s_subtype)
24350c55cfc4SEric W. Biederman 		mnt = fs_set_subtype(mnt, fstype);
24360c55cfc4SEric W. Biederman 
24370c55cfc4SEric W. Biederman 	put_filesystem(type);
24381da177e4SLinus Torvalds 	if (IS_ERR(mnt))
24391da177e4SLinus Torvalds 		return PTR_ERR(mnt);
24401da177e4SLinus Torvalds 
24418654df4eSEric W. Biederman 	if (mount_too_revealing(mnt, &mnt_flags)) {
24428654df4eSEric W. Biederman 		mntput(mnt);
24438654df4eSEric W. Biederman 		return -EPERM;
24448654df4eSEric W. Biederman 	}
24458654df4eSEric W. Biederman 
244695bc5f25SAl Viro 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
244715f9a3f3SAl Viro 	if (err)
244815f9a3f3SAl Viro 		mntput(mnt);
244915f9a3f3SAl Viro 	return err;
24501da177e4SLinus Torvalds }
24511da177e4SLinus Torvalds 
245219a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
245319a167afSAl Viro {
24546776db3dSAl Viro 	struct mount *mnt = real_mount(m);
245519a167afSAl Viro 	int err;
245619a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
245719a167afSAl Viro 	 * expired before we get a chance to add it
245819a167afSAl Viro 	 */
24596776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
246019a167afSAl Viro 
246119a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
246219a167afSAl Viro 	    m->mnt_root == path->dentry) {
2463b1e75df4SAl Viro 		err = -ELOOP;
2464b1e75df4SAl Viro 		goto fail;
246519a167afSAl Viro 	}
246619a167afSAl Viro 
246795bc5f25SAl Viro 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2468b1e75df4SAl Viro 	if (!err)
2469b1e75df4SAl Viro 		return 0;
2470b1e75df4SAl Viro fail:
2471b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
24726776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
247397216be0SAl Viro 		namespace_lock();
24746776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
247597216be0SAl Viro 		namespace_unlock();
247619a167afSAl Viro 	}
2477b1e75df4SAl Viro 	mntput(m);
2478b1e75df4SAl Viro 	mntput(m);
247919a167afSAl Viro 	return err;
248019a167afSAl Viro }
248119a167afSAl Viro 
2482ea5b778aSDavid Howells /**
2483ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2484ea5b778aSDavid Howells  * @mnt: The mount to list.
2485ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2486ea5b778aSDavid Howells  */
2487ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2488ea5b778aSDavid Howells {
248997216be0SAl Viro 	namespace_lock();
2490ea5b778aSDavid Howells 
24916776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2492ea5b778aSDavid Howells 
249397216be0SAl Viro 	namespace_unlock();
2494ea5b778aSDavid Howells }
2495ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2496ea5b778aSDavid Howells 
2497ea5b778aSDavid Howells /*
24981da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
24991da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
25001da177e4SLinus Torvalds  * here
25011da177e4SLinus Torvalds  */
25021da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
25031da177e4SLinus Torvalds {
2504761d5c38SAl Viro 	struct mount *mnt, *next;
25051da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 	if (list_empty(mounts))
25081da177e4SLinus Torvalds 		return;
25091da177e4SLinus Torvalds 
251097216be0SAl Viro 	namespace_lock();
2511719ea2fbSAl Viro 	lock_mount_hash();
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
25141da177e4SLinus Torvalds 	 * following criteria:
25151da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
25161da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
25171da177e4SLinus Torvalds 	 *   cleared by mntput())
25181da177e4SLinus Torvalds 	 */
25196776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2520863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
25211ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
25221da177e4SLinus Torvalds 			continue;
25236776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
25241da177e4SLinus Torvalds 	}
2525bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
25266776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2527143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2528e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2529bcc5c7d2SAl Viro 	}
2530719ea2fbSAl Viro 	unlock_mount_hash();
25313ab6abeeSAl Viro 	namespace_unlock();
25321da177e4SLinus Torvalds }
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds /*
25375528f911STrond Myklebust  * Ripoff of 'select_parent()'
25385528f911STrond Myklebust  *
25395528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
25405528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
25415528f911STrond Myklebust  */
2542692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
25435528f911STrond Myklebust {
2544692afc31SAl Viro 	struct mount *this_parent = parent;
25455528f911STrond Myklebust 	struct list_head *next;
25465528f911STrond Myklebust 	int found = 0;
25475528f911STrond Myklebust 
25485528f911STrond Myklebust repeat:
25496b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
25505528f911STrond Myklebust resume:
25516b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
25525528f911STrond Myklebust 		struct list_head *tmp = next;
25536b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
25545528f911STrond Myklebust 
25555528f911STrond Myklebust 		next = tmp->next;
2556692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
25575528f911STrond Myklebust 			continue;
25585528f911STrond Myklebust 		/*
25595528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
25605528f911STrond Myklebust 		 */
25616b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
25625528f911STrond Myklebust 			this_parent = mnt;
25635528f911STrond Myklebust 			goto repeat;
25645528f911STrond Myklebust 		}
25655528f911STrond Myklebust 
25661ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
25676776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
25685528f911STrond Myklebust 			found++;
25695528f911STrond Myklebust 		}
25705528f911STrond Myklebust 	}
25715528f911STrond Myklebust 	/*
25725528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
25735528f911STrond Myklebust 	 */
25745528f911STrond Myklebust 	if (this_parent != parent) {
25756b41d536SAl Viro 		next = this_parent->mnt_child.next;
25760714a533SAl Viro 		this_parent = this_parent->mnt_parent;
25775528f911STrond Myklebust 		goto resume;
25785528f911STrond Myklebust 	}
25795528f911STrond Myklebust 	return found;
25805528f911STrond Myklebust }
25815528f911STrond Myklebust 
25825528f911STrond Myklebust /*
25835528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
25845528f911STrond Myklebust  * submounts of a specific parent mountpoint
258599b7db7bSNick Piggin  *
258648a066e7SAl Viro  * mount_lock must be held for write
25875528f911STrond Myklebust  */
2588b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
25895528f911STrond Myklebust {
25905528f911STrond Myklebust 	LIST_HEAD(graveyard);
2591761d5c38SAl Viro 	struct mount *m;
25925528f911STrond Myklebust 
25935528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2594c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2595bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2596761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
25976776db3dSAl Viro 						mnt_expire);
2598143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
2599e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2600bcc5c7d2SAl Viro 		}
2601bcc5c7d2SAl Viro 	}
26025528f911STrond Myklebust }
26035528f911STrond Myklebust 
26045528f911STrond Myklebust /*
26051da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
26061da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
26071da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
26081da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
26091da177e4SLinus Torvalds  */
2610b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2611b58fed8bSRam Pai 				 unsigned long n)
26121da177e4SLinus Torvalds {
26131da177e4SLinus Torvalds 	char *t = to;
26141da177e4SLinus Torvalds 	const char __user *f = from;
26151da177e4SLinus Torvalds 	char c;
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
26181da177e4SLinus Torvalds 		return n;
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 	while (n) {
26211da177e4SLinus Torvalds 		if (__get_user(c, f)) {
26221da177e4SLinus Torvalds 			memset(t, 0, n);
26231da177e4SLinus Torvalds 			break;
26241da177e4SLinus Torvalds 		}
26251da177e4SLinus Torvalds 		*t++ = c;
26261da177e4SLinus Torvalds 		f++;
26271da177e4SLinus Torvalds 		n--;
26281da177e4SLinus Torvalds 	}
26291da177e4SLinus Torvalds 	return n;
26301da177e4SLinus Torvalds }
26311da177e4SLinus Torvalds 
2632b40ef869SAl Viro void *copy_mount_options(const void __user * data)
26331da177e4SLinus Torvalds {
26341da177e4SLinus Torvalds 	int i;
26351da177e4SLinus Torvalds 	unsigned long size;
2636b40ef869SAl Viro 	char *copy;
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds 	if (!data)
2639b40ef869SAl Viro 		return NULL;
26401da177e4SLinus Torvalds 
2641b40ef869SAl Viro 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2642b40ef869SAl Viro 	if (!copy)
2643b40ef869SAl Viro 		return ERR_PTR(-ENOMEM);
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
26461da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
26471da177e4SLinus Torvalds 	 * the remainder of the page.
26481da177e4SLinus Torvalds 	 */
26491da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
26501da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
26511da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
26521da177e4SLinus Torvalds 		size = PAGE_SIZE;
26531da177e4SLinus Torvalds 
2654b40ef869SAl Viro 	i = size - exact_copy_from_user(copy, data, size);
26551da177e4SLinus Torvalds 	if (!i) {
2656b40ef869SAl Viro 		kfree(copy);
2657b40ef869SAl Viro 		return ERR_PTR(-EFAULT);
26581da177e4SLinus Torvalds 	}
26591da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
2660b40ef869SAl Viro 		memset(copy + i, 0, PAGE_SIZE - i);
2661b40ef869SAl Viro 	return copy;
26621da177e4SLinus Torvalds }
26631da177e4SLinus Torvalds 
2664b8850d1fSTim Gardner char *copy_mount_string(const void __user *data)
2665eca6f534SVegard Nossum {
2666b8850d1fSTim Gardner 	return data ? strndup_user(data, PAGE_SIZE) : NULL;
2667eca6f534SVegard Nossum }
2668eca6f534SVegard Nossum 
26691da177e4SLinus Torvalds /*
26701da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
26711da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
26721da177e4SLinus Torvalds  *
26731da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
26741da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
26751da177e4SLinus Torvalds  * information (or be NULL).
26761da177e4SLinus Torvalds  *
26771da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
26781da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
26791da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
26801da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
26811da177e4SLinus Torvalds  * and must be discarded.
26821da177e4SLinus Torvalds  */
26835e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name,
2684808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
26851da177e4SLinus Torvalds {
26862d92ab3cSAl Viro 	struct path path;
26871da177e4SLinus Torvalds 	int retval = 0;
26881da177e4SLinus Torvalds 	int mnt_flags = 0;
26891da177e4SLinus Torvalds 
26901da177e4SLinus Torvalds 	/* Discard magic */
26911da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
26921da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	/* Basic sanity checks */
26951da177e4SLinus Torvalds 	if (data_page)
26961da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
26971da177e4SLinus Torvalds 
2698a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
26995e6123f3SSeunghun Lee 	retval = user_path(dir_name, &path);
2700a27ab9f2STetsuo Handa 	if (retval)
2701a27ab9f2STetsuo Handa 		return retval;
2702a27ab9f2STetsuo Handa 
2703a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2704a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
27050d5cadb8SAl Viro 	if (!retval && !may_mount())
27060d5cadb8SAl Viro 		retval = -EPERM;
27079e8925b6SJeff Layton 	if (!retval && (flags & MS_MANDLOCK) && !may_mandlock())
27089e8925b6SJeff Layton 		retval = -EPERM;
2709a27ab9f2STetsuo Handa 	if (retval)
2710a27ab9f2STetsuo Handa 		goto dput_out;
2711a27ab9f2STetsuo Handa 
2712613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2713613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
27140a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
27150a1c01c9SMatthew Garrett 
27161da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
27171da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
27181da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
27191da177e4SLinus Torvalds 	if (flags & MS_NODEV)
27201da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
27211da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
27221da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2723fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2724fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2725fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2726fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2727d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2728d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
27292e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
27302e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2731fc33a7bbSChristoph Hellwig 
2732ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
2733ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
2734ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2735ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
2736ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
2737ffbc6f0eSEric W. Biederman 		mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2738ffbc6f0eSEric W. Biederman 	}
2739ffbc6f0eSEric W. Biederman 
27407a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2741d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2742c568d683SMiklos Szeredi 		   MS_STRICTATIME | MS_NOREMOTELOCK);
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
27452d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
27461da177e4SLinus Torvalds 				    data_page);
27471da177e4SLinus Torvalds 	else if (flags & MS_BIND)
27482d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
27499676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
27502d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
27511da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
27522d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
27531da177e4SLinus Torvalds 	else
27542d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
27551da177e4SLinus Torvalds 				      dev_name, data_page);
27561da177e4SLinus Torvalds dput_out:
27572d92ab3cSAl Viro 	path_put(&path);
27581da177e4SLinus Torvalds 	return retval;
27591da177e4SLinus Torvalds }
27601da177e4SLinus Torvalds 
2761537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
2762537f7ccbSEric W. Biederman {
2763537f7ccbSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
2764537f7ccbSEric W. Biederman }
2765537f7ccbSEric W. Biederman 
2766537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts)
2767537f7ccbSEric W. Biederman {
2768537f7ccbSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
2769537f7ccbSEric W. Biederman }
2770537f7ccbSEric W. Biederman 
2771771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
2772771b1371SEric W. Biederman {
27736344c433SAl Viro 	ns_free_inum(&ns->ns);
2774537f7ccbSEric W. Biederman 	dec_mnt_namespaces(ns->ucounts);
2775771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
2776771b1371SEric W. Biederman 	kfree(ns);
2777771b1371SEric W. Biederman }
2778771b1371SEric W. Biederman 
27798823c079SEric W. Biederman /*
27808823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
27818823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
27828823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
27838823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
27848823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
27858823c079SEric W. Biederman  */
27868823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
27878823c079SEric W. Biederman 
2788771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2789cf8d2c11STrond Myklebust {
2790cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2791537f7ccbSEric W. Biederman 	struct ucounts *ucounts;
279298f842e6SEric W. Biederman 	int ret;
2793cf8d2c11STrond Myklebust 
2794537f7ccbSEric W. Biederman 	ucounts = inc_mnt_namespaces(user_ns);
2795537f7ccbSEric W. Biederman 	if (!ucounts)
2796df75e774SEric W. Biederman 		return ERR_PTR(-ENOSPC);
2797537f7ccbSEric W. Biederman 
2798cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2799537f7ccbSEric W. Biederman 	if (!new_ns) {
2800537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
2801cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
2802537f7ccbSEric W. Biederman 	}
28036344c433SAl Viro 	ret = ns_alloc_inum(&new_ns->ns);
280498f842e6SEric W. Biederman 	if (ret) {
280598f842e6SEric W. Biederman 		kfree(new_ns);
2806537f7ccbSEric W. Biederman 		dec_mnt_namespaces(ucounts);
280798f842e6SEric W. Biederman 		return ERR_PTR(ret);
280898f842e6SEric W. Biederman 	}
280933c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
28108823c079SEric W. Biederman 	new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2811cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2812cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2813cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2814cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2815cf8d2c11STrond Myklebust 	new_ns->event = 0;
2816771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
2817537f7ccbSEric W. Biederman 	new_ns->ucounts = ucounts;
2818d2921684SEric W. Biederman 	new_ns->mounts = 0;
2819d2921684SEric W. Biederman 	new_ns->pending_mounts = 0;
2820cf8d2c11STrond Myklebust 	return new_ns;
2821cf8d2c11STrond Myklebust }
2822cf8d2c11STrond Myklebust 
28230766f788SEmese Revfy __latent_entropy
28249559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
28259559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
28261da177e4SLinus Torvalds {
28276b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
28287f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2829315fc83eSAl Viro 	struct mount *p, *q;
28309559f689SAl Viro 	struct mount *old;
2831cb338d06SAl Viro 	struct mount *new;
28327a472ef4SEric W. Biederman 	int copy_flags;
28331da177e4SLinus Torvalds 
28349559f689SAl Viro 	BUG_ON(!ns);
28359559f689SAl Viro 
28369559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
28379559f689SAl Viro 		get_mnt_ns(ns);
28389559f689SAl Viro 		return ns;
28399559f689SAl Viro 	}
28409559f689SAl Viro 
28419559f689SAl Viro 	old = ns->root;
28429559f689SAl Viro 
2843771b1371SEric W. Biederman 	new_ns = alloc_mnt_ns(user_ns);
2844cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2845cf8d2c11STrond Myklebust 		return new_ns;
28461da177e4SLinus Torvalds 
284797216be0SAl Viro 	namespace_lock();
28481da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
28494ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
28509559f689SAl Viro 	if (user_ns != ns->user_ns)
2851132c94e3SEric W. Biederman 		copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
28527a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2853be34d1a3SDavid Howells 	if (IS_ERR(new)) {
2854328e6d90SAl Viro 		namespace_unlock();
2855771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
2856be34d1a3SDavid Howells 		return ERR_CAST(new);
28571da177e4SLinus Torvalds 	}
2858be08d6d2SAl Viro 	new_ns->root = new;
28591a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds 	/*
28621da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
28631da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
28641da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
28651da177e4SLinus Torvalds 	 */
2866909b0a88SAl Viro 	p = old;
2867cb338d06SAl Viro 	q = new;
28681da177e4SLinus Torvalds 	while (p) {
2869143c8c91SAl Viro 		q->mnt_ns = new_ns;
2870d2921684SEric W. Biederman 		new_ns->mounts++;
28719559f689SAl Viro 		if (new_fs) {
28729559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
28739559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
2874315fc83eSAl Viro 				rootmnt = &p->mnt;
28751da177e4SLinus Torvalds 			}
28769559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
28779559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
2878315fc83eSAl Viro 				pwdmnt = &p->mnt;
28791da177e4SLinus Torvalds 			}
28801da177e4SLinus Torvalds 		}
2881909b0a88SAl Viro 		p = next_mnt(p, old);
2882909b0a88SAl Viro 		q = next_mnt(q, new);
28834ce5d2b1SEric W. Biederman 		if (!q)
28844ce5d2b1SEric W. Biederman 			break;
28854ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
28864ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
28871da177e4SLinus Torvalds 	}
2888328e6d90SAl Viro 	namespace_unlock();
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds 	if (rootmnt)
2891f03c6599SAl Viro 		mntput(rootmnt);
28921da177e4SLinus Torvalds 	if (pwdmnt)
2893f03c6599SAl Viro 		mntput(pwdmnt);
28941da177e4SLinus Torvalds 
2895741a2951SJANAK DESAI 	return new_ns;
2896741a2951SJANAK DESAI }
2897741a2951SJANAK DESAI 
2898cf8d2c11STrond Myklebust /**
2899cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2900cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2901cf8d2c11STrond Myklebust  */
29021a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2903cf8d2c11STrond Myklebust {
2904771b1371SEric W. Biederman 	struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2905cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
29061a4eeaf2SAl Viro 		struct mount *mnt = real_mount(m);
29071a4eeaf2SAl Viro 		mnt->mnt_ns = new_ns;
2908be08d6d2SAl Viro 		new_ns->root = mnt;
2909d2921684SEric W. Biederman 		new_ns->mounts++;
2910b1983cd8SAl Viro 		list_add(&mnt->mnt_list, &new_ns->list);
2911c1334495SAl Viro 	} else {
29121a4eeaf2SAl Viro 		mntput(m);
2913cf8d2c11STrond Myklebust 	}
2914cf8d2c11STrond Myklebust 	return new_ns;
2915cf8d2c11STrond Myklebust }
2916cf8d2c11STrond Myklebust 
2917ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2918ea441d11SAl Viro {
2919ea441d11SAl Viro 	struct mnt_namespace *ns;
2920d31da0f0SAl Viro 	struct super_block *s;
2921ea441d11SAl Viro 	struct path path;
2922ea441d11SAl Viro 	int err;
2923ea441d11SAl Viro 
2924ea441d11SAl Viro 	ns = create_mnt_ns(mnt);
2925ea441d11SAl Viro 	if (IS_ERR(ns))
2926ea441d11SAl Viro 		return ERR_CAST(ns);
2927ea441d11SAl Viro 
2928ea441d11SAl Viro 	err = vfs_path_lookup(mnt->mnt_root, mnt,
2929ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2930ea441d11SAl Viro 
2931ea441d11SAl Viro 	put_mnt_ns(ns);
2932ea441d11SAl Viro 
2933ea441d11SAl Viro 	if (err)
2934ea441d11SAl Viro 		return ERR_PTR(err);
2935ea441d11SAl Viro 
2936ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
2937d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
2938d31da0f0SAl Viro 	atomic_inc(&s->s_active);
2939ea441d11SAl Viro 	mntput(path.mnt);
2940ea441d11SAl Viro 	/* lock the sucker */
2941d31da0f0SAl Viro 	down_write(&s->s_umount);
2942ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
2943ea441d11SAl Viro 	return path.dentry;
2944ea441d11SAl Viro }
2945ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
2946ea441d11SAl Viro 
2947bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2948bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
29491da177e4SLinus Torvalds {
2950eca6f534SVegard Nossum 	int ret;
2951eca6f534SVegard Nossum 	char *kernel_type;
2952eca6f534SVegard Nossum 	char *kernel_dev;
2953b40ef869SAl Viro 	void *options;
29541da177e4SLinus Torvalds 
2955b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
2956b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
2957b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
2958eca6f534SVegard Nossum 		goto out_type;
29591da177e4SLinus Torvalds 
2960b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
2961b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
2962b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
2963eca6f534SVegard Nossum 		goto out_dev;
29641da177e4SLinus Torvalds 
2965b40ef869SAl Viro 	options = copy_mount_options(data);
2966b40ef869SAl Viro 	ret = PTR_ERR(options);
2967b40ef869SAl Viro 	if (IS_ERR(options))
2968eca6f534SVegard Nossum 		goto out_data;
29691da177e4SLinus Torvalds 
2970b40ef869SAl Viro 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
2971eca6f534SVegard Nossum 
2972b40ef869SAl Viro 	kfree(options);
2973eca6f534SVegard Nossum out_data:
2974eca6f534SVegard Nossum 	kfree(kernel_dev);
2975eca6f534SVegard Nossum out_dev:
2976eca6f534SVegard Nossum 	kfree(kernel_type);
2977eca6f534SVegard Nossum out_type:
2978eca6f534SVegard Nossum 	return ret;
29791da177e4SLinus Torvalds }
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds /*
2982afac7cbaSAl Viro  * Return true if path is reachable from root
2983afac7cbaSAl Viro  *
298448a066e7SAl Viro  * namespace_sem or mount_lock is held
2985afac7cbaSAl Viro  */
2986643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2987afac7cbaSAl Viro 			 const struct path *root)
2988afac7cbaSAl Viro {
2989643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2990a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
29910714a533SAl Viro 		mnt = mnt->mnt_parent;
2992afac7cbaSAl Viro 	}
2993643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2994afac7cbaSAl Viro }
2995afac7cbaSAl Viro 
299625ab4c9bSYaowei Bai bool path_is_under(struct path *path1, struct path *path2)
2997afac7cbaSAl Viro {
299825ab4c9bSYaowei Bai 	bool res;
299948a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
3000643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
300148a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
3002afac7cbaSAl Viro 	return res;
3003afac7cbaSAl Viro }
3004afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
3005afac7cbaSAl Viro 
3006afac7cbaSAl Viro /*
30071da177e4SLinus Torvalds  * pivot_root Semantics:
30081da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
30091da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
30101da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
30111da177e4SLinus Torvalds  *
30121da177e4SLinus Torvalds  * Restrictions:
30131da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
30141da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
30151da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
30161da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
30171da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
30181da177e4SLinus Torvalds  *
30194a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
30204a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
30214a0d11faSNeil Brown  * in this situation.
30224a0d11faSNeil Brown  *
30231da177e4SLinus Torvalds  * Notes:
30241da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
30251da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
30261da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
30271da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
30281da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
30291da177e4SLinus Torvalds  *    first.
30301da177e4SLinus Torvalds  */
30313480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
30323480b257SHeiko Carstens 		const char __user *, put_old)
30331da177e4SLinus Torvalds {
30342d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
303584d17192SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt;
303684d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
30371da177e4SLinus Torvalds 	int error;
30381da177e4SLinus Torvalds 
30399b40bc90SAl Viro 	if (!may_mount())
30401da177e4SLinus Torvalds 		return -EPERM;
30411da177e4SLinus Torvalds 
30422d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
30431da177e4SLinus Torvalds 	if (error)
30441da177e4SLinus Torvalds 		goto out0;
30451da177e4SLinus Torvalds 
30462d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
30471da177e4SLinus Torvalds 	if (error)
30481da177e4SLinus Torvalds 		goto out1;
30491da177e4SLinus Torvalds 
30502d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
3051b12cea91SAl Viro 	if (error)
3052b12cea91SAl Viro 		goto out2;
30531da177e4SLinus Torvalds 
3054f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
305584d17192SAl Viro 	old_mp = lock_mount(&old);
305684d17192SAl Viro 	error = PTR_ERR(old_mp);
305784d17192SAl Viro 	if (IS_ERR(old_mp))
3058b12cea91SAl Viro 		goto out3;
3059b12cea91SAl Viro 
30601da177e4SLinus Torvalds 	error = -EINVAL;
3061419148daSAl Viro 	new_mnt = real_mount(new.mnt);
3062419148daSAl Viro 	root_mnt = real_mount(root.mnt);
306384d17192SAl Viro 	old_mnt = real_mount(old.mnt);
306484d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
3065fc7be130SAl Viro 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
3066fc7be130SAl Viro 		IS_MNT_SHARED(root_mnt->mnt_parent))
3067b12cea91SAl Viro 		goto out4;
3068143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
3069b12cea91SAl Viro 		goto out4;
30705ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
30715ff9d8a6SEric W. Biederman 		goto out4;
30721da177e4SLinus Torvalds 	error = -ENOENT;
3073f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
3074b12cea91SAl Viro 		goto out4;
30751da177e4SLinus Torvalds 	error = -EBUSY;
307684d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
3077b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
30781da177e4SLinus Torvalds 	error = -EINVAL;
30798c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
3080b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3081676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
3082b12cea91SAl Viro 		goto out4; /* not attached */
308384d17192SAl Viro 	root_mp = root_mnt->mnt_mp;
30842d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
3085b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3086676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
3087b12cea91SAl Viro 		goto out4; /* not attached */
30884ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
308984d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
3090b12cea91SAl Viro 		goto out4;
30910d082601SEric W. Biederman 	/* make certain new is below the root */
30920d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
30930d082601SEric W. Biederman 		goto out4;
309484d17192SAl Viro 	root_mp->m_count++; /* pin it so it won't go away */
3095719ea2fbSAl Viro 	lock_mount_hash();
3096419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
3097419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
30985ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
30995ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
31005ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
31015ff9d8a6SEric W. Biederman 	}
31024ac91378SJan Blunck 	/* mount old root on put_old */
310384d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
31044ac91378SJan Blunck 	/* mount new_root on / */
310584d17192SAl Viro 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
31066b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
31074fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
31084fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
3109719ea2fbSAl Viro 	unlock_mount_hash();
31102d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
311184d17192SAl Viro 	put_mountpoint(root_mp);
31121da177e4SLinus Torvalds 	error = 0;
3113b12cea91SAl Viro out4:
311484d17192SAl Viro 	unlock_mount(old_mp);
3115b12cea91SAl Viro 	if (!error) {
31161a390689SAl Viro 		path_put(&root_parent);
31171a390689SAl Viro 		path_put(&parent_path);
3118b12cea91SAl Viro 	}
3119b12cea91SAl Viro out3:
31208c3ee42eSAl Viro 	path_put(&root);
3121b12cea91SAl Viro out2:
31222d8f3038SAl Viro 	path_put(&old);
31231da177e4SLinus Torvalds out1:
31242d8f3038SAl Viro 	path_put(&new);
31251da177e4SLinus Torvalds out0:
31261da177e4SLinus Torvalds 	return error;
31271da177e4SLinus Torvalds }
31281da177e4SLinus Torvalds 
31291da177e4SLinus Torvalds static void __init init_mount_tree(void)
31301da177e4SLinus Torvalds {
31311da177e4SLinus Torvalds 	struct vfsmount *mnt;
31326b3286edSKirill Korotaev 	struct mnt_namespace *ns;
3133ac748a09SJan Blunck 	struct path root;
31340c55cfc4SEric W. Biederman 	struct file_system_type *type;
31351da177e4SLinus Torvalds 
31360c55cfc4SEric W. Biederman 	type = get_fs_type("rootfs");
31370c55cfc4SEric W. Biederman 	if (!type)
31380c55cfc4SEric W. Biederman 		panic("Can't find rootfs type");
31390c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
31400c55cfc4SEric W. Biederman 	put_filesystem(type);
31411da177e4SLinus Torvalds 	if (IS_ERR(mnt))
31421da177e4SLinus Torvalds 		panic("Can't create rootfs");
3143b3e19d92SNick Piggin 
31443b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
31453b22edc5STrond Myklebust 	if (IS_ERR(ns))
31461da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
31471da177e4SLinus Torvalds 
31486b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
31496b3286edSKirill Korotaev 	get_mnt_ns(ns);
31501da177e4SLinus Torvalds 
3151be08d6d2SAl Viro 	root.mnt = mnt;
3152be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
3153da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
3154ac748a09SJan Blunck 
3155ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
3156ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
31571da177e4SLinus Torvalds }
31581da177e4SLinus Torvalds 
315974bf17cfSDenis Cheng void __init mnt_init(void)
31601da177e4SLinus Torvalds {
316113f14b4dSEric Dumazet 	unsigned u;
316215a67dd8SRandy Dunlap 	int err;
31631da177e4SLinus Torvalds 
31647d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
316520c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
31661da177e4SLinus Torvalds 
31670818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
316838129a13SAl Viro 				sizeof(struct hlist_head),
31690818bf27SAl Viro 				mhash_entries, 19,
31700818bf27SAl Viro 				0,
31710818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
31720818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
31730818bf27SAl Viro 				sizeof(struct hlist_head),
31740818bf27SAl Viro 				mphash_entries, 19,
31750818bf27SAl Viro 				0,
31760818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
31771da177e4SLinus Torvalds 
317884d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
31791da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
31801da177e4SLinus Torvalds 
31810818bf27SAl Viro 	for (u = 0; u <= m_hash_mask; u++)
318238129a13SAl Viro 		INIT_HLIST_HEAD(&mount_hashtable[u]);
31830818bf27SAl Viro 	for (u = 0; u <= mp_hash_mask; u++)
31840818bf27SAl Viro 		INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
31851da177e4SLinus Torvalds 
31864b93dc9bSTejun Heo 	kernfs_init();
31874b93dc9bSTejun Heo 
318815a67dd8SRandy Dunlap 	err = sysfs_init();
318915a67dd8SRandy Dunlap 	if (err)
319015a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
31918e24eea7SHarvey Harrison 			__func__, err);
319200d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
319300d26666SGreg Kroah-Hartman 	if (!fs_kobj)
31948e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
31951da177e4SLinus Torvalds 	init_rootfs();
31961da177e4SLinus Torvalds 	init_mount_tree();
31971da177e4SLinus Torvalds }
31981da177e4SLinus Torvalds 
3199616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
32001da177e4SLinus Torvalds {
3201d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
3202616511d0STrond Myklebust 		return;
32037b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
3204771b1371SEric W. Biederman 	free_mnt_ns(ns);
32051da177e4SLinus Torvalds }
32069d412a43SAl Viro 
32079d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
32089d412a43SAl Viro {
3209423e0ab0STim Chen 	struct vfsmount *mnt;
3210423e0ab0STim Chen 	mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
3211423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
3212423e0ab0STim Chen 		/*
3213423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
3214423e0ab0STim Chen 		 * we unmount before file sys is unregistered
3215423e0ab0STim Chen 		*/
3216f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
3217423e0ab0STim Chen 	}
3218423e0ab0STim Chen 	return mnt;
32199d412a43SAl Viro }
32209d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data);
3221423e0ab0STim Chen 
3222423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
3223423e0ab0STim Chen {
3224423e0ab0STim Chen 	/* release long term mount so mount point can be released */
3225423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
3226f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
322748a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
3228423e0ab0STim Chen 		mntput(mnt);
3229423e0ab0STim Chen 	}
3230423e0ab0STim Chen }
3231423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
323202125a82SAl Viro 
323302125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
323402125a82SAl Viro {
3235143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
323602125a82SAl Viro }
32378823c079SEric W. Biederman 
32383151527eSEric W. Biederman bool current_chrooted(void)
32393151527eSEric W. Biederman {
32403151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
32413151527eSEric W. Biederman 	struct path ns_root;
32423151527eSEric W. Biederman 	struct path fs_root;
32433151527eSEric W. Biederman 	bool chrooted;
32443151527eSEric W. Biederman 
32453151527eSEric W. Biederman 	/* Find the namespace root */
32463151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
32473151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
32483151527eSEric W. Biederman 	path_get(&ns_root);
32493151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
32503151527eSEric W. Biederman 		;
32513151527eSEric W. Biederman 
32523151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
32533151527eSEric W. Biederman 
32543151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
32553151527eSEric W. Biederman 
32563151527eSEric W. Biederman 	path_put(&fs_root);
32573151527eSEric W. Biederman 	path_put(&ns_root);
32583151527eSEric W. Biederman 
32593151527eSEric W. Biederman 	return chrooted;
32603151527eSEric W. Biederman }
32613151527eSEric W. Biederman 
32628654df4eSEric W. Biederman static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
32638654df4eSEric W. Biederman 				int *new_mnt_flags)
326487a8ebd6SEric W. Biederman {
32658c6cf9ccSEric W. Biederman 	int new_flags = *new_mnt_flags;
326687a8ebd6SEric W. Biederman 	struct mount *mnt;
3267e51db735SEric W. Biederman 	bool visible = false;
326887a8ebd6SEric W. Biederman 
326944bb4385SAl Viro 	down_read(&namespace_sem);
327087a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
3271e51db735SEric W. Biederman 		struct mount *child;
327277b1a97dSEric W. Biederman 		int mnt_flags;
327377b1a97dSEric W. Biederman 
32748654df4eSEric W. Biederman 		if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
3275e51db735SEric W. Biederman 			continue;
3276e51db735SEric W. Biederman 
32777e96c1b0SEric W. Biederman 		/* This mount is not fully visible if it's root directory
32787e96c1b0SEric W. Biederman 		 * is not the root directory of the filesystem.
32797e96c1b0SEric W. Biederman 		 */
32807e96c1b0SEric W. Biederman 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
32817e96c1b0SEric W. Biederman 			continue;
32827e96c1b0SEric W. Biederman 
3283a1935c17SEric W. Biederman 		/* A local view of the mount flags */
328477b1a97dSEric W. Biederman 		mnt_flags = mnt->mnt.mnt_flags;
328577b1a97dSEric W. Biederman 
3286695e9df0SEric W. Biederman 		/* Don't miss readonly hidden in the superblock flags */
3287695e9df0SEric W. Biederman 		if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY)
3288695e9df0SEric W. Biederman 			mnt_flags |= MNT_LOCK_READONLY;
3289695e9df0SEric W. Biederman 
32908c6cf9ccSEric W. Biederman 		/* Verify the mount flags are equal to or more permissive
32918c6cf9ccSEric W. Biederman 		 * than the proposed new mount.
32928c6cf9ccSEric W. Biederman 		 */
329377b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_READONLY) &&
32948c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_READONLY))
32958c6cf9ccSEric W. Biederman 			continue;
329677b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_ATIME) &&
329777b1a97dSEric W. Biederman 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
32988c6cf9ccSEric W. Biederman 			continue;
32998c6cf9ccSEric W. Biederman 
3300ceeb0e5dSEric W. Biederman 		/* This mount is not fully visible if there are any
3301ceeb0e5dSEric W. Biederman 		 * locked child mounts that cover anything except for
3302ceeb0e5dSEric W. Biederman 		 * empty directories.
3303e51db735SEric W. Biederman 		 */
3304e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3305e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
3306ceeb0e5dSEric W. Biederman 			/* Only worry about locked mounts */
3307d71ed6c9SEric W. Biederman 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
3308ceeb0e5dSEric W. Biederman 				continue;
33097236c85eSEric W. Biederman 			/* Is the directory permanetly empty? */
33107236c85eSEric W. Biederman 			if (!is_empty_dir_inode(inode))
3311e51db735SEric W. Biederman 				goto next;
331287a8ebd6SEric W. Biederman 		}
33138c6cf9ccSEric W. Biederman 		/* Preserve the locked attributes */
331477b1a97dSEric W. Biederman 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
33158c6cf9ccSEric W. Biederman 					       MNT_LOCK_ATIME);
3316e51db735SEric W. Biederman 		visible = true;
3317e51db735SEric W. Biederman 		goto found;
3318e51db735SEric W. Biederman 	next:	;
331987a8ebd6SEric W. Biederman 	}
3320e51db735SEric W. Biederman found:
332144bb4385SAl Viro 	up_read(&namespace_sem);
3322e51db735SEric W. Biederman 	return visible;
332387a8ebd6SEric W. Biederman }
332487a8ebd6SEric W. Biederman 
33258654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
33268654df4eSEric W. Biederman {
3327a1935c17SEric W. Biederman 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
33288654df4eSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
33298654df4eSEric W. Biederman 	unsigned long s_iflags;
33308654df4eSEric W. Biederman 
33318654df4eSEric W. Biederman 	if (ns->user_ns == &init_user_ns)
33328654df4eSEric W. Biederman 		return false;
33338654df4eSEric W. Biederman 
33348654df4eSEric W. Biederman 	/* Can this filesystem be too revealing? */
33358654df4eSEric W. Biederman 	s_iflags = mnt->mnt_sb->s_iflags;
33368654df4eSEric W. Biederman 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
33378654df4eSEric W. Biederman 		return false;
33388654df4eSEric W. Biederman 
3339a1935c17SEric W. Biederman 	if ((s_iflags & required_iflags) != required_iflags) {
3340a1935c17SEric W. Biederman 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3341a1935c17SEric W. Biederman 			  required_iflags);
3342a1935c17SEric W. Biederman 		return true;
3343a1935c17SEric W. Biederman 	}
3344a1935c17SEric W. Biederman 
33458654df4eSEric W. Biederman 	return !mnt_already_visible(ns, mnt, new_mnt_flags);
33468654df4eSEric W. Biederman }
33478654df4eSEric W. Biederman 
3348380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt)
3349380cf5baSAndy Lutomirski {
3350380cf5baSAndy Lutomirski 	/*
3351380cf5baSAndy Lutomirski 	 * Foreign mounts (accessed via fchdir or through /proc
3352380cf5baSAndy Lutomirski 	 * symlinks) are always treated as if they are nosuid.  This
3353380cf5baSAndy Lutomirski 	 * prevents namespaces from trusting potentially unsafe
3354380cf5baSAndy Lutomirski 	 * suid/sgid bits, file caps, or security labels that originate
3355380cf5baSAndy Lutomirski 	 * in other namespaces.
3356380cf5baSAndy Lutomirski 	 */
3357380cf5baSAndy Lutomirski 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3358380cf5baSAndy Lutomirski 	       current_in_userns(mnt->mnt_sb->s_user_ns);
3359380cf5baSAndy Lutomirski }
3360380cf5baSAndy Lutomirski 
336164964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
33628823c079SEric W. Biederman {
336358be2825SAl Viro 	struct ns_common *ns = NULL;
33648823c079SEric W. Biederman 	struct nsproxy *nsproxy;
33658823c079SEric W. Biederman 
3366728dba3aSEric W. Biederman 	task_lock(task);
3367728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
33688823c079SEric W. Biederman 	if (nsproxy) {
336958be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
337058be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
33718823c079SEric W. Biederman 	}
3372728dba3aSEric W. Biederman 	task_unlock(task);
33738823c079SEric W. Biederman 
33748823c079SEric W. Biederman 	return ns;
33758823c079SEric W. Biederman }
33768823c079SEric W. Biederman 
337764964528SAl Viro static void mntns_put(struct ns_common *ns)
33788823c079SEric W. Biederman {
337958be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
33808823c079SEric W. Biederman }
33818823c079SEric W. Biederman 
338264964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
33838823c079SEric W. Biederman {
33848823c079SEric W. Biederman 	struct fs_struct *fs = current->fs;
338558be2825SAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
33868823c079SEric W. Biederman 	struct path root;
33878823c079SEric W. Biederman 
33880c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3389c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3390c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
3391ae11e0f1SZhao Hongjiang 		return -EPERM;
33928823c079SEric W. Biederman 
33938823c079SEric W. Biederman 	if (fs->users != 1)
33948823c079SEric W. Biederman 		return -EINVAL;
33958823c079SEric W. Biederman 
33968823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
33978823c079SEric W. Biederman 	put_mnt_ns(nsproxy->mnt_ns);
33988823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
33998823c079SEric W. Biederman 
34008823c079SEric W. Biederman 	/* Find the root */
34018823c079SEric W. Biederman 	root.mnt    = &mnt_ns->root->mnt;
34028823c079SEric W. Biederman 	root.dentry = mnt_ns->root->mnt.mnt_root;
34038823c079SEric W. Biederman 	path_get(&root);
34048823c079SEric W. Biederman 	while(d_mountpoint(root.dentry) && follow_down_one(&root))
34058823c079SEric W. Biederman 		;
34068823c079SEric W. Biederman 
34078823c079SEric W. Biederman 	/* Update the pwd and root */
34088823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
34098823c079SEric W. Biederman 	set_fs_root(fs, &root);
34108823c079SEric W. Biederman 
34118823c079SEric W. Biederman 	path_put(&root);
34128823c079SEric W. Biederman 	return 0;
34138823c079SEric W. Biederman }
34148823c079SEric W. Biederman 
3415bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns)
3416bcac25a5SAndrey Vagin {
3417bcac25a5SAndrey Vagin 	return to_mnt_ns(ns)->user_ns;
3418bcac25a5SAndrey Vagin }
3419bcac25a5SAndrey Vagin 
34208823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
34218823c079SEric W. Biederman 	.name		= "mnt",
34228823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
34238823c079SEric W. Biederman 	.get		= mntns_get,
34248823c079SEric W. Biederman 	.put		= mntns_put,
34258823c079SEric W. Biederman 	.install	= mntns_install,
3426bcac25a5SAndrey Vagin 	.owner		= mntns_owner,
34278823c079SEric W. Biederman };
3428