xref: /openbmc/linux/fs/namespace.c (revision e06b933e)
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 
300818bf27SAl Viro static unsigned int m_hash_mask __read_mostly;
310818bf27SAl Viro static unsigned int m_hash_shift __read_mostly;
320818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly;
330818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly;
340818bf27SAl Viro 
350818bf27SAl Viro static __initdata unsigned long mhash_entries;
360818bf27SAl Viro static int __init set_mhash_entries(char *str)
370818bf27SAl Viro {
380818bf27SAl Viro 	if (!str)
390818bf27SAl Viro 		return 0;
400818bf27SAl Viro 	mhash_entries = simple_strtoul(str, &str, 0);
410818bf27SAl Viro 	return 1;
420818bf27SAl Viro }
430818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries);
440818bf27SAl Viro 
450818bf27SAl Viro static __initdata unsigned long mphash_entries;
460818bf27SAl Viro static int __init set_mphash_entries(char *str)
470818bf27SAl Viro {
480818bf27SAl Viro 	if (!str)
490818bf27SAl Viro 		return 0;
500818bf27SAl Viro 	mphash_entries = simple_strtoul(str, &str, 0);
510818bf27SAl Viro 	return 1;
520818bf27SAl Viro }
530818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries);
5413f14b4dSEric Dumazet 
55c7999c36SAl Viro static u64 event;
5673cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
57719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
5899b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock);
59f21f6220SAl Viro static int mnt_id_start = 0;
60f21f6220SAl Viro static int mnt_group_start = 1;
615addc5ddSAl Viro 
6238129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly;
630818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly;
64e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
6559aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem);
661da177e4SLinus Torvalds 
67f87fd4c2SMiklos Szeredi /* /sys/fs */
6800d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
6900d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
70f87fd4c2SMiklos Szeredi 
7199b7db7bSNick Piggin /*
7299b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
7399b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
7499b7db7bSNick Piggin  * up the tree.
7599b7db7bSNick Piggin  *
7699b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
7799b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
7899b7db7bSNick Piggin  */
7948a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
8099b7db7bSNick Piggin 
8138129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
841da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
850818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
860818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
870818bf27SAl Viro }
880818bf27SAl Viro 
890818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
900818bf27SAl Viro {
910818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
920818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
930818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
9699b7db7bSNick Piggin /*
9799b7db7bSNick Piggin  * allocation is serialized by namespace_sem, but we need the spinlock to
9899b7db7bSNick Piggin  * serialize with freeing.
9999b7db7bSNick Piggin  */
100b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
10173cd49ecSMiklos Szeredi {
10273cd49ecSMiklos Szeredi 	int res;
10373cd49ecSMiklos Szeredi 
10473cd49ecSMiklos Szeredi retry:
10573cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
10699b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
10715169fe7SAl Viro 	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
108f21f6220SAl Viro 	if (!res)
10915169fe7SAl Viro 		mnt_id_start = mnt->mnt_id + 1;
11099b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
11173cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
11273cd49ecSMiklos Szeredi 		goto retry;
11373cd49ecSMiklos Szeredi 
11473cd49ecSMiklos Szeredi 	return res;
11573cd49ecSMiklos Szeredi }
11673cd49ecSMiklos Szeredi 
117b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
11873cd49ecSMiklos Szeredi {
11915169fe7SAl Viro 	int id = mnt->mnt_id;
12099b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
121f21f6220SAl Viro 	ida_remove(&mnt_id_ida, id);
122f21f6220SAl Viro 	if (mnt_id_start > id)
123f21f6220SAl Viro 		mnt_id_start = id;
12499b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
12573cd49ecSMiklos Szeredi }
12673cd49ecSMiklos Szeredi 
127719f5d7fSMiklos Szeredi /*
128719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
129719f5d7fSMiklos Szeredi  *
130719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
131719f5d7fSMiklos Szeredi  */
1324b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
133719f5d7fSMiklos Szeredi {
134f21f6220SAl Viro 	int res;
135f21f6220SAl Viro 
136719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
137719f5d7fSMiklos Szeredi 		return -ENOMEM;
138719f5d7fSMiklos Szeredi 
139f21f6220SAl Viro 	res = ida_get_new_above(&mnt_group_ida,
140f21f6220SAl Viro 				mnt_group_start,
14115169fe7SAl Viro 				&mnt->mnt_group_id);
142f21f6220SAl Viro 	if (!res)
14315169fe7SAl Viro 		mnt_group_start = mnt->mnt_group_id + 1;
144f21f6220SAl Viro 
145f21f6220SAl Viro 	return res;
146719f5d7fSMiklos Szeredi }
147719f5d7fSMiklos Szeredi 
148719f5d7fSMiklos Szeredi /*
149719f5d7fSMiklos Szeredi  * Release a peer group ID
150719f5d7fSMiklos Szeredi  */
1514b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
152719f5d7fSMiklos Szeredi {
15315169fe7SAl Viro 	int id = mnt->mnt_group_id;
154f21f6220SAl Viro 	ida_remove(&mnt_group_ida, id);
155f21f6220SAl Viro 	if (mnt_group_start > id)
156f21f6220SAl Viro 		mnt_group_start = id;
15715169fe7SAl Viro 	mnt->mnt_group_id = 0;
158719f5d7fSMiklos Szeredi }
159719f5d7fSMiklos Szeredi 
160b3e19d92SNick Piggin /*
161b3e19d92SNick Piggin  * vfsmount lock must be held for read
162b3e19d92SNick Piggin  */
16383adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
164b3e19d92SNick Piggin {
165b3e19d92SNick Piggin #ifdef CONFIG_SMP
16668e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
167b3e19d92SNick Piggin #else
168b3e19d92SNick Piggin 	preempt_disable();
16968e8a9feSAl Viro 	mnt->mnt_count += n;
170b3e19d92SNick Piggin 	preempt_enable();
171b3e19d92SNick Piggin #endif
172b3e19d92SNick Piggin }
173b3e19d92SNick Piggin 
174b3e19d92SNick Piggin /*
175b3e19d92SNick Piggin  * vfsmount lock must be held for write
176b3e19d92SNick Piggin  */
17783adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt)
178b3e19d92SNick Piggin {
179b3e19d92SNick Piggin #ifdef CONFIG_SMP
180f03c6599SAl Viro 	unsigned int count = 0;
181b3e19d92SNick Piggin 	int cpu;
182b3e19d92SNick Piggin 
183b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
18468e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
185b3e19d92SNick Piggin 	}
186b3e19d92SNick Piggin 
187b3e19d92SNick Piggin 	return count;
188b3e19d92SNick Piggin #else
18968e8a9feSAl Viro 	return mnt->mnt_count;
190b3e19d92SNick Piggin #endif
191b3e19d92SNick Piggin }
192b3e19d92SNick Piggin 
19387b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p)
19487b95ce0SAl Viro {
19587b95ce0SAl Viro 	struct mount *m = container_of(p, struct mount, mnt_umount);
19687b95ce0SAl Viro 	dput(m->mnt_ex_mountpoint);
19787b95ce0SAl Viro 	pin_remove(p);
19887b95ce0SAl Viro 	mntput(&m->mnt);
19987b95ce0SAl Viro }
20087b95ce0SAl Viro 
201b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
2021da177e4SLinus Torvalds {
203c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
204c63181e6SAl Viro 	if (mnt) {
20573cd49ecSMiklos Szeredi 		int err;
20673cd49ecSMiklos Szeredi 
207c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
20888b38782SLi Zefan 		if (err)
20988b38782SLi Zefan 			goto out_free_cache;
21088b38782SLi Zefan 
21188b38782SLi Zefan 		if (name) {
212fcc139aeSAndrzej Hajda 			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
213c63181e6SAl Viro 			if (!mnt->mnt_devname)
21488b38782SLi Zefan 				goto out_free_id;
21573cd49ecSMiklos Szeredi 		}
21673cd49ecSMiklos Szeredi 
217b3e19d92SNick Piggin #ifdef CONFIG_SMP
218c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
219c63181e6SAl Viro 		if (!mnt->mnt_pcp)
220b3e19d92SNick Piggin 			goto out_free_devname;
221b3e19d92SNick Piggin 
222c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
223b3e19d92SNick Piggin #else
224c63181e6SAl Viro 		mnt->mnt_count = 1;
225c63181e6SAl Viro 		mnt->mnt_writers = 0;
226b3e19d92SNick Piggin #endif
227b3e19d92SNick Piggin 
22838129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
229c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
230c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
231c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
232c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
233c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
234c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
235c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2360a5eb7c8SEric W. Biederman 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
2372504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY
2382504c5d6SAndreas Gruenbacher 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
2392504c5d6SAndreas Gruenbacher #endif
24087b95ce0SAl Viro 		init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
2411da177e4SLinus Torvalds 	}
242c63181e6SAl Viro 	return mnt;
24388b38782SLi Zefan 
244d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
245d3ef3d73Snpiggin@suse.de out_free_devname:
246fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
247d3ef3d73Snpiggin@suse.de #endif
24888b38782SLi Zefan out_free_id:
249c63181e6SAl Viro 	mnt_free_id(mnt);
25088b38782SLi Zefan out_free_cache:
251c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
25288b38782SLi Zefan 	return NULL;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2558366025eSDave Hansen /*
2568366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2578366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2588366025eSDave Hansen  * We must keep track of when those operations start
2598366025eSDave Hansen  * (for permission checks) and when they end, so that
2608366025eSDave Hansen  * we can determine when writes are able to occur to
2618366025eSDave Hansen  * a filesystem.
2628366025eSDave Hansen  */
2633d733633SDave Hansen /*
2643d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2653d733633SDave Hansen  * @mnt: the mount to check for its write status
2663d733633SDave Hansen  *
2673d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2683d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2693d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2703d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2713d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2723d733633SDave Hansen  * r/w.
2733d733633SDave Hansen  */
2743d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2753d733633SDave Hansen {
2762e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2772e4b7fcdSDave Hansen 		return 1;
2782e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
2792e4b7fcdSDave Hansen 		return 1;
2802e4b7fcdSDave Hansen 	return 0;
2813d733633SDave Hansen }
2823d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2833d733633SDave Hansen 
28483adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2853d733633SDave Hansen {
286d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
28768e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
288d3ef3d73Snpiggin@suse.de #else
28968e8a9feSAl Viro 	mnt->mnt_writers++;
290d3ef3d73Snpiggin@suse.de #endif
2913d733633SDave Hansen }
2923d733633SDave Hansen 
29383adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2943d733633SDave Hansen {
295d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
29668e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
297d3ef3d73Snpiggin@suse.de #else
29868e8a9feSAl Viro 	mnt->mnt_writers--;
299d3ef3d73Snpiggin@suse.de #endif
300d3ef3d73Snpiggin@suse.de }
301d3ef3d73Snpiggin@suse.de 
30283adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
303d3ef3d73Snpiggin@suse.de {
304d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
305d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
3063d733633SDave Hansen 	int cpu;
3073d733633SDave Hansen 
3083d733633SDave Hansen 	for_each_possible_cpu(cpu) {
30968e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3103d733633SDave Hansen 	}
3113d733633SDave Hansen 
312d3ef3d73Snpiggin@suse.de 	return count;
313d3ef3d73Snpiggin@suse.de #else
314d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
315d3ef3d73Snpiggin@suse.de #endif
3163d733633SDave Hansen }
3173d733633SDave Hansen 
3184ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
3194ed5e82fSMiklos Szeredi {
3204ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
3214ed5e82fSMiklos Szeredi 		return 1;
3224ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
3234ed5e82fSMiklos Szeredi 	smp_rmb();
3244ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
3254ed5e82fSMiklos Szeredi }
3264ed5e82fSMiklos Szeredi 
3273d733633SDave Hansen /*
328eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
329eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
330eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
331eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
3323d733633SDave Hansen  */
3338366025eSDave Hansen /**
334eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
33583adc753SAl Viro  * @m: the mount on which to take a write
3368366025eSDave Hansen  *
337eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
338eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
339eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
340eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
341eb04c282SJan Kara  * called. This is effectively a refcount.
3428366025eSDave Hansen  */
343eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3448366025eSDave Hansen {
34583adc753SAl Viro 	struct mount *mnt = real_mount(m);
3463d733633SDave Hansen 	int ret = 0;
3473d733633SDave Hansen 
348d3ef3d73Snpiggin@suse.de 	preempt_disable();
349c6653a83SNick Piggin 	mnt_inc_writers(mnt);
350d3ef3d73Snpiggin@suse.de 	/*
351c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
352d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
353d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
354d3ef3d73Snpiggin@suse.de 	 */
355d3ef3d73Snpiggin@suse.de 	smp_mb();
3561e75529eSMiao Xie 	while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
357d3ef3d73Snpiggin@suse.de 		cpu_relax();
358d3ef3d73Snpiggin@suse.de 	/*
359d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
360d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
361d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
362d3ef3d73Snpiggin@suse.de 	 */
363d3ef3d73Snpiggin@suse.de 	smp_rmb();
3644ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
365c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3663d733633SDave Hansen 		ret = -EROFS;
3673d733633SDave Hansen 	}
368d3ef3d73Snpiggin@suse.de 	preempt_enable();
369eb04c282SJan Kara 
370eb04c282SJan Kara 	return ret;
371eb04c282SJan Kara }
372eb04c282SJan Kara 
373eb04c282SJan Kara /**
374eb04c282SJan Kara  * mnt_want_write - get write access to a mount
375eb04c282SJan Kara  * @m: the mount on which to take a write
376eb04c282SJan Kara  *
377eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
378eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
379eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
380eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
381eb04c282SJan Kara  */
382eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
383eb04c282SJan Kara {
384eb04c282SJan Kara 	int ret;
385eb04c282SJan Kara 
386eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
387eb04c282SJan Kara 	ret = __mnt_want_write(m);
388eb04c282SJan Kara 	if (ret)
389eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3903d733633SDave Hansen 	return ret;
3918366025eSDave Hansen }
3928366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3938366025eSDave Hansen 
3948366025eSDave Hansen /**
39596029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
39696029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
39796029c4eSnpiggin@suse.de  *
39896029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
39996029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
40096029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
40196029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
40296029c4eSnpiggin@suse.de  *
40396029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
40496029c4eSnpiggin@suse.de  * drop the reference.
40596029c4eSnpiggin@suse.de  */
40696029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
40796029c4eSnpiggin@suse.de {
40896029c4eSnpiggin@suse.de 	/* superblock may be r/o */
40996029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
41096029c4eSnpiggin@suse.de 		return -EROFS;
41196029c4eSnpiggin@suse.de 	preempt_disable();
41283adc753SAl Viro 	mnt_inc_writers(real_mount(mnt));
41396029c4eSnpiggin@suse.de 	preempt_enable();
41496029c4eSnpiggin@suse.de 	return 0;
41596029c4eSnpiggin@suse.de }
41696029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
41796029c4eSnpiggin@suse.de 
41896029c4eSnpiggin@suse.de /**
419eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
420eb04c282SJan Kara  * @file: the file who's mount on which to take a write
421eb04c282SJan Kara  *
422eb04c282SJan Kara  * This is like __mnt_want_write, but it takes a file and can
423eb04c282SJan Kara  * do some optimisations if the file is open for write already
424eb04c282SJan Kara  */
425eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
426eb04c282SJan Kara {
42783f936c7SAl Viro 	if (!(file->f_mode & FMODE_WRITER))
428eb04c282SJan Kara 		return __mnt_want_write(file->f_path.mnt);
429eb04c282SJan Kara 	else
430eb04c282SJan Kara 		return mnt_clone_write(file->f_path.mnt);
431eb04c282SJan Kara }
432eb04c282SJan Kara 
433eb04c282SJan Kara /**
43496029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
43596029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
43696029c4eSnpiggin@suse.de  *
43796029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
43896029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
43996029c4eSnpiggin@suse.de  */
44096029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
44196029c4eSnpiggin@suse.de {
442eb04c282SJan Kara 	int ret;
443eb04c282SJan Kara 
444eb04c282SJan Kara 	sb_start_write(file->f_path.mnt->mnt_sb);
445eb04c282SJan Kara 	ret = __mnt_want_write_file(file);
446eb04c282SJan Kara 	if (ret)
447eb04c282SJan Kara 		sb_end_write(file->f_path.mnt->mnt_sb);
448eb04c282SJan Kara 	return ret;
44996029c4eSnpiggin@suse.de }
45096029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
45196029c4eSnpiggin@suse.de 
45296029c4eSnpiggin@suse.de /**
453eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4548366025eSDave Hansen  * @mnt: the mount on which to give up write access
4558366025eSDave Hansen  *
4568366025eSDave Hansen  * Tells the low-level filesystem that we are done
4578366025eSDave Hansen  * performing writes to it.  Must be matched with
458eb04c282SJan Kara  * __mnt_want_write() call above.
4598366025eSDave Hansen  */
460eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4618366025eSDave Hansen {
462d3ef3d73Snpiggin@suse.de 	preempt_disable();
46383adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
464d3ef3d73Snpiggin@suse.de 	preempt_enable();
4658366025eSDave Hansen }
466eb04c282SJan Kara 
467eb04c282SJan Kara /**
468eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
469eb04c282SJan Kara  * @mnt: the mount on which to give up write access
470eb04c282SJan Kara  *
471eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
472eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
473eb04c282SJan Kara  * mnt_want_write() call above.
474eb04c282SJan Kara  */
475eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
476eb04c282SJan Kara {
477eb04c282SJan Kara 	__mnt_drop_write(mnt);
478eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
479eb04c282SJan Kara }
4808366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4818366025eSDave Hansen 
482eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
483eb04c282SJan Kara {
484eb04c282SJan Kara 	__mnt_drop_write(file->f_path.mnt);
485eb04c282SJan Kara }
486eb04c282SJan Kara 
4872a79f17eSAl Viro void mnt_drop_write_file(struct file *file)
4882a79f17eSAl Viro {
4892a79f17eSAl Viro 	mnt_drop_write(file->f_path.mnt);
4902a79f17eSAl Viro }
4912a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4922a79f17eSAl Viro 
49383adc753SAl Viro static int mnt_make_readonly(struct mount *mnt)
4948366025eSDave Hansen {
4953d733633SDave Hansen 	int ret = 0;
4963d733633SDave Hansen 
497719ea2fbSAl Viro 	lock_mount_hash();
49883adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
499d3ef3d73Snpiggin@suse.de 	/*
500d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
501d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
502d3ef3d73Snpiggin@suse.de 	 */
503d3ef3d73Snpiggin@suse.de 	smp_mb();
504d3ef3d73Snpiggin@suse.de 
505d3ef3d73Snpiggin@suse.de 	/*
506d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
507d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
508d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
509d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
510d3ef3d73Snpiggin@suse.de 	 *
511d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
512d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
513d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
514d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
515d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
516d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
517d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
518d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
519d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
520d3ef3d73Snpiggin@suse.de 	 */
521c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
522d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
523d3ef3d73Snpiggin@suse.de 	else
52483adc753SAl Viro 		mnt->mnt.mnt_flags |= MNT_READONLY;
525d3ef3d73Snpiggin@suse.de 	/*
526d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
527d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
528d3ef3d73Snpiggin@suse.de 	 */
529d3ef3d73Snpiggin@suse.de 	smp_wmb();
53083adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
531719ea2fbSAl Viro 	unlock_mount_hash();
5323d733633SDave Hansen 	return ret;
5333d733633SDave Hansen }
5348366025eSDave Hansen 
53583adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt)
5362e4b7fcdSDave Hansen {
537719ea2fbSAl Viro 	lock_mount_hash();
53883adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
539719ea2fbSAl Viro 	unlock_mount_hash();
5402e4b7fcdSDave Hansen }
5412e4b7fcdSDave Hansen 
5424ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5434ed5e82fSMiklos Szeredi {
5444ed5e82fSMiklos Szeredi 	struct mount *mnt;
5454ed5e82fSMiklos Szeredi 	int err = 0;
5464ed5e82fSMiklos Szeredi 
5478e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5488e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5498e8b8796SMiklos Szeredi 		return -EBUSY;
5508e8b8796SMiklos Szeredi 
551719ea2fbSAl Viro 	lock_mount_hash();
5524ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5534ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
5544ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
5554ed5e82fSMiklos Szeredi 			smp_mb();
5564ed5e82fSMiklos Szeredi 			if (mnt_get_writers(mnt) > 0) {
5574ed5e82fSMiklos Szeredi 				err = -EBUSY;
5584ed5e82fSMiklos Szeredi 				break;
5594ed5e82fSMiklos Szeredi 			}
5604ed5e82fSMiklos Szeredi 		}
5614ed5e82fSMiklos Szeredi 	}
5628e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5638e8b8796SMiklos Szeredi 		err = -EBUSY;
5648e8b8796SMiklos Szeredi 
5654ed5e82fSMiklos Szeredi 	if (!err) {
5664ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
5674ed5e82fSMiklos Szeredi 		smp_wmb();
5684ed5e82fSMiklos Szeredi 	}
5694ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5704ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
5714ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
5724ed5e82fSMiklos Szeredi 	}
573719ea2fbSAl Viro 	unlock_mount_hash();
5744ed5e82fSMiklos Szeredi 
5754ed5e82fSMiklos Szeredi 	return err;
5764ed5e82fSMiklos Szeredi }
5774ed5e82fSMiklos Szeredi 
578b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
5791da177e4SLinus Torvalds {
580fcc139aeSAndrzej Hajda 	kfree_const(mnt->mnt_devname);
581d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
58268e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
583d3ef3d73Snpiggin@suse.de #endif
584b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5878ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
5888ffcb32eSDavid Howells {
5898ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
5908ffcb32eSDavid Howells }
5918ffcb32eSDavid Howells 
59248a066e7SAl Viro /* call under rcu_read_lock */
593294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
59448a066e7SAl Viro {
59548a066e7SAl Viro 	struct mount *mnt;
59648a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
597294d71ffSAl Viro 		return 1;
59848a066e7SAl Viro 	if (bastard == NULL)
599294d71ffSAl Viro 		return 0;
60048a066e7SAl Viro 	mnt = real_mount(bastard);
60148a066e7SAl Viro 	mnt_add_count(mnt, 1);
60248a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
603294d71ffSAl Viro 		return 0;
60448a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
60548a066e7SAl Viro 		mnt_add_count(mnt, -1);
606294d71ffSAl Viro 		return 1;
60748a066e7SAl Viro 	}
608294d71ffSAl Viro 	return -1;
609294d71ffSAl Viro }
610294d71ffSAl Viro 
611294d71ffSAl Viro /* call under rcu_read_lock */
612294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
613294d71ffSAl Viro {
614294d71ffSAl Viro 	int res = __legitimize_mnt(bastard, seq);
615294d71ffSAl Viro 	if (likely(!res))
616294d71ffSAl Viro 		return true;
617294d71ffSAl Viro 	if (unlikely(res < 0)) {
61848a066e7SAl Viro 		rcu_read_unlock();
61948a066e7SAl Viro 		mntput(bastard);
62048a066e7SAl Viro 		rcu_read_lock();
621294d71ffSAl Viro 	}
62248a066e7SAl Viro 	return false;
62348a066e7SAl Viro }
62448a066e7SAl Viro 
6251da177e4SLinus Torvalds /*
626474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
62748a066e7SAl Viro  * call under rcu_read_lock()
6281da177e4SLinus Torvalds  */
629474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6301da177e4SLinus Torvalds {
63138129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
632474279dcSAl Viro 	struct mount *p;
6331da177e4SLinus Torvalds 
63438129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
635474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
636474279dcSAl Viro 			return p;
637474279dcSAl Viro 	return NULL;
6381da177e4SLinus Torvalds }
639474279dcSAl Viro 
640474279dcSAl Viro /*
641474279dcSAl Viro  * find the last mount at @dentry on vfsmount @mnt.
64248a066e7SAl Viro  * mount_lock must be held.
643474279dcSAl Viro  */
644474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
645474279dcSAl Viro {
646411a938bSEric W. Biederman 	struct mount *p, *res = NULL;
647411a938bSEric W. Biederman 	p = __lookup_mnt(mnt, dentry);
64838129a13SAl Viro 	if (!p)
64938129a13SAl Viro 		goto out;
650411a938bSEric W. Biederman 	if (!(p->mnt.mnt_flags & MNT_UMOUNT))
651411a938bSEric W. Biederman 		res = p;
65238129a13SAl Viro 	hlist_for_each_entry_continue(p, mnt_hash) {
6531d6a32acSAl Viro 		if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
6541d6a32acSAl Viro 			break;
655411a938bSEric W. Biederman 		if (!(p->mnt.mnt_flags & MNT_UMOUNT))
6561d6a32acSAl Viro 			res = p;
6571d6a32acSAl Viro 	}
65838129a13SAl Viro out:
6591d6a32acSAl Viro 	return res;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
662a05964f3SRam Pai /*
663f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
664f015f126SDavid Howells  *
665f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
666f015f126SDavid Howells  * following mounts:
667f015f126SDavid Howells  *
668f015f126SDavid Howells  * mount /dev/sda1 /mnt
669f015f126SDavid Howells  * mount /dev/sda2 /mnt
670f015f126SDavid Howells  * mount /dev/sda3 /mnt
671f015f126SDavid Howells  *
672f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
673f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
674f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
675f015f126SDavid Howells  *
676f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
677a05964f3SRam Pai  */
6781c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
679a05964f3SRam Pai {
680c7105365SAl Viro 	struct mount *child_mnt;
68148a066e7SAl Viro 	struct vfsmount *m;
68248a066e7SAl Viro 	unsigned seq;
68399b7db7bSNick Piggin 
68448a066e7SAl Viro 	rcu_read_lock();
68548a066e7SAl Viro 	do {
68648a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
687474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
68848a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
68948a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
69048a066e7SAl Viro 	rcu_read_unlock();
69148a066e7SAl Viro 	return m;
692a05964f3SRam Pai }
693a05964f3SRam Pai 
6947af1364fSEric W. Biederman /*
6957af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
6967af1364fSEric W. Biederman  *                         current mount namespace.
6977af1364fSEric W. Biederman  *
6987af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
6997af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
7007af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
7017af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
7027af1364fSEric W. Biederman  * is a mountpoint.
7037af1364fSEric W. Biederman  *
7047af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
7057af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
7067af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
7077af1364fSEric W. Biederman  * parent mount.
7087af1364fSEric W. Biederman  */
7097af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
7107af1364fSEric W. Biederman {
7117af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
7127af1364fSEric W. Biederman 	struct mount *mnt;
7137af1364fSEric W. Biederman 	bool is_covered = false;
7147af1364fSEric W. Biederman 
7157af1364fSEric W. Biederman 	if (!d_mountpoint(dentry))
7167af1364fSEric W. Biederman 		goto out;
7177af1364fSEric W. Biederman 
7187af1364fSEric W. Biederman 	down_read(&namespace_sem);
7197af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
7207af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
7217af1364fSEric W. Biederman 		if (is_covered)
7227af1364fSEric W. Biederman 			break;
7237af1364fSEric W. Biederman 	}
7247af1364fSEric W. Biederman 	up_read(&namespace_sem);
7257af1364fSEric W. Biederman out:
7267af1364fSEric W. Biederman 	return is_covered;
7277af1364fSEric W. Biederman }
7287af1364fSEric W. Biederman 
729e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
73084d17192SAl Viro {
7310818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
73284d17192SAl Viro 	struct mountpoint *mp;
73384d17192SAl Viro 
7340818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
73584d17192SAl Viro 		if (mp->m_dentry == dentry) {
73684d17192SAl Viro 			/* might be worth a WARN_ON() */
73784d17192SAl Viro 			if (d_unlinked(dentry))
73884d17192SAl Viro 				return ERR_PTR(-ENOENT);
73984d17192SAl Viro 			mp->m_count++;
74084d17192SAl Viro 			return mp;
74184d17192SAl Viro 		}
74284d17192SAl Viro 	}
743e2dfa935SEric W. Biederman 	return NULL;
744e2dfa935SEric W. Biederman }
745e2dfa935SEric W. Biederman 
746e2dfa935SEric W. Biederman static struct mountpoint *new_mountpoint(struct dentry *dentry)
747e2dfa935SEric W. Biederman {
748e2dfa935SEric W. Biederman 	struct hlist_head *chain = mp_hash(dentry);
749e2dfa935SEric W. Biederman 	struct mountpoint *mp;
750e2dfa935SEric W. Biederman 	int ret;
75184d17192SAl Viro 
75284d17192SAl Viro 	mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
75384d17192SAl Viro 	if (!mp)
75484d17192SAl Viro 		return ERR_PTR(-ENOMEM);
75584d17192SAl Viro 
756eed81007SMiklos Szeredi 	ret = d_set_mounted(dentry);
757eed81007SMiklos Szeredi 	if (ret) {
75884d17192SAl Viro 		kfree(mp);
759eed81007SMiklos Szeredi 		return ERR_PTR(ret);
76084d17192SAl Viro 	}
761eed81007SMiklos Szeredi 
76284d17192SAl Viro 	mp->m_dentry = dentry;
76384d17192SAl Viro 	mp->m_count = 1;
7640818bf27SAl Viro 	hlist_add_head(&mp->m_hash, chain);
7650a5eb7c8SEric W. Biederman 	INIT_HLIST_HEAD(&mp->m_list);
76684d17192SAl Viro 	return mp;
76784d17192SAl Viro }
76884d17192SAl Viro 
76984d17192SAl Viro static void put_mountpoint(struct mountpoint *mp)
77084d17192SAl Viro {
77184d17192SAl Viro 	if (!--mp->m_count) {
77284d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
7730a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
77484d17192SAl Viro 		spin_lock(&dentry->d_lock);
77584d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
77684d17192SAl Viro 		spin_unlock(&dentry->d_lock);
7770818bf27SAl Viro 		hlist_del(&mp->m_hash);
77884d17192SAl Viro 		kfree(mp);
77984d17192SAl Viro 	}
78084d17192SAl Viro }
78184d17192SAl Viro 
782143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
7831da177e4SLinus Torvalds {
7846b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
78799b7db7bSNick Piggin /*
78899b7db7bSNick Piggin  * vfsmount lock must be held for write
78999b7db7bSNick Piggin  */
7906b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
7915addc5ddSAl Viro {
7925addc5ddSAl Viro 	if (ns) {
7935addc5ddSAl Viro 		ns->event = ++event;
7945addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7955addc5ddSAl Viro 	}
7965addc5ddSAl Viro }
7975addc5ddSAl Viro 
79899b7db7bSNick Piggin /*
79999b7db7bSNick Piggin  * vfsmount lock must be held for write
80099b7db7bSNick Piggin  */
8016b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
8025addc5ddSAl Viro {
8035addc5ddSAl Viro 	if (ns && ns->event != event) {
8045addc5ddSAl Viro 		ns->event = event;
8055addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
8065addc5ddSAl Viro 	}
8075addc5ddSAl Viro }
8085addc5ddSAl Viro 
80999b7db7bSNick Piggin /*
81099b7db7bSNick Piggin  * vfsmount lock must be held for write
81199b7db7bSNick Piggin  */
8127bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt)
8131da177e4SLinus Torvalds {
8140714a533SAl Viro 	mnt->mnt_parent = mnt;
815a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8166b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
81738129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
8180a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
81984d17192SAl Viro 	put_mountpoint(mnt->mnt_mp);
82084d17192SAl Viro 	mnt->mnt_mp = NULL;
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
82399b7db7bSNick Piggin /*
82499b7db7bSNick Piggin  * vfsmount lock must be held for write
82599b7db7bSNick Piggin  */
8267bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path)
8277bdb11deSEric W. Biederman {
8287bdb11deSEric W. Biederman 	old_path->dentry = mnt->mnt_mountpoint;
8297bdb11deSEric W. Biederman 	old_path->mnt = &mnt->mnt_parent->mnt;
8307bdb11deSEric W. Biederman 	unhash_mnt(mnt);
8317bdb11deSEric W. Biederman }
8327bdb11deSEric W. Biederman 
8337bdb11deSEric W. Biederman /*
8347bdb11deSEric W. Biederman  * vfsmount lock must be held for write
8357bdb11deSEric W. Biederman  */
8366a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt)
8376a46c573SEric W. Biederman {
8386a46c573SEric W. Biederman 	/* old mountpoint will be dropped when we can do that */
8396a46c573SEric W. Biederman 	mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
8406a46c573SEric W. Biederman 	unhash_mnt(mnt);
8416a46c573SEric W. Biederman }
8426a46c573SEric W. Biederman 
8436a46c573SEric W. Biederman /*
8446a46c573SEric W. Biederman  * vfsmount lock must be held for write
8456a46c573SEric W. Biederman  */
84684d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
84784d17192SAl Viro 			struct mountpoint *mp,
84844d964d6SAl Viro 			struct mount *child_mnt)
849b90fa9aeSRam Pai {
85084d17192SAl Viro 	mp->m_count++;
8513a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
85284d17192SAl Viro 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
8533a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
85484d17192SAl Viro 	child_mnt->mnt_mp = mp;
8550a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
856b90fa9aeSRam Pai }
857b90fa9aeSRam Pai 
85899b7db7bSNick Piggin /*
85999b7db7bSNick Piggin  * vfsmount lock must be held for write
86099b7db7bSNick Piggin  */
86184d17192SAl Viro static void attach_mnt(struct mount *mnt,
86284d17192SAl Viro 			struct mount *parent,
86384d17192SAl Viro 			struct mountpoint *mp)
8641da177e4SLinus Torvalds {
86584d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
86638129a13SAl Viro 	hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
86784d17192SAl Viro 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
868b90fa9aeSRam Pai }
869b90fa9aeSRam Pai 
87012a5b529SAl Viro static void attach_shadowed(struct mount *mnt,
87112a5b529SAl Viro 			struct mount *parent,
87212a5b529SAl Viro 			struct mount *shadows)
87312a5b529SAl Viro {
87412a5b529SAl Viro 	if (shadows) {
875f6f99332SLinus Torvalds 		hlist_add_behind_rcu(&mnt->mnt_hash, &shadows->mnt_hash);
87612a5b529SAl Viro 		list_add(&mnt->mnt_child, &shadows->mnt_child);
87712a5b529SAl Viro 	} else {
87812a5b529SAl Viro 		hlist_add_head_rcu(&mnt->mnt_hash,
87912a5b529SAl Viro 				m_hash(&parent->mnt, mnt->mnt_mountpoint));
88012a5b529SAl Viro 		list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
88112a5b529SAl Viro 	}
88212a5b529SAl Viro }
88312a5b529SAl Viro 
884b90fa9aeSRam Pai /*
88599b7db7bSNick Piggin  * vfsmount lock must be held for write
886b90fa9aeSRam Pai  */
8871d6a32acSAl Viro static void commit_tree(struct mount *mnt, struct mount *shadows)
888b90fa9aeSRam Pai {
8890714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
89083adc753SAl Viro 	struct mount *m;
891b90fa9aeSRam Pai 	LIST_HEAD(head);
892143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
893b90fa9aeSRam Pai 
8940714a533SAl Viro 	BUG_ON(parent == mnt);
895b90fa9aeSRam Pai 
8961a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
897f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
898143c8c91SAl Viro 		m->mnt_ns = n;
899f03c6599SAl Viro 
900b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
901b90fa9aeSRam Pai 
90212a5b529SAl Viro 	attach_shadowed(mnt, parent, shadows);
9036b3286edSKirill Korotaev 	touch_mnt_namespace(n);
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
906909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
9071da177e4SLinus Torvalds {
9086b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
9096b41d536SAl Viro 	if (next == &p->mnt_mounts) {
9101da177e4SLinus Torvalds 		while (1) {
911909b0a88SAl Viro 			if (p == root)
9121da177e4SLinus Torvalds 				return NULL;
9136b41d536SAl Viro 			next = p->mnt_child.next;
9146b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
9151da177e4SLinus Torvalds 				break;
9160714a533SAl Viro 			p = p->mnt_parent;
9171da177e4SLinus Torvalds 		}
9181da177e4SLinus Torvalds 	}
9196b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds 
922315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
9239676f0c6SRam Pai {
9246b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
9256b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
9266b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
9276b41d536SAl Viro 		prev = p->mnt_mounts.prev;
9289676f0c6SRam Pai 	}
9299676f0c6SRam Pai 	return p;
9309676f0c6SRam Pai }
9319676f0c6SRam Pai 
9329d412a43SAl Viro struct vfsmount *
9339d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
9349d412a43SAl Viro {
935b105e270SAl Viro 	struct mount *mnt;
9369d412a43SAl Viro 	struct dentry *root;
9379d412a43SAl Viro 
9389d412a43SAl Viro 	if (!type)
9399d412a43SAl Viro 		return ERR_PTR(-ENODEV);
9409d412a43SAl Viro 
9419d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
9429d412a43SAl Viro 	if (!mnt)
9439d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
9449d412a43SAl Viro 
9459d412a43SAl Viro 	if (flags & MS_KERNMOUNT)
946b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
9479d412a43SAl Viro 
9489d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
9499d412a43SAl Viro 	if (IS_ERR(root)) {
9508ffcb32eSDavid Howells 		mnt_free_id(mnt);
9519d412a43SAl Viro 		free_vfsmnt(mnt);
9529d412a43SAl Viro 		return ERR_CAST(root);
9539d412a43SAl Viro 	}
9549d412a43SAl Viro 
955b105e270SAl Viro 	mnt->mnt.mnt_root = root;
956b105e270SAl Viro 	mnt->mnt.mnt_sb = root->d_sb;
957a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
9580714a533SAl Viro 	mnt->mnt_parent = mnt;
959719ea2fbSAl Viro 	lock_mount_hash();
96039f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
961719ea2fbSAl Viro 	unlock_mount_hash();
962b105e270SAl Viro 	return &mnt->mnt;
9639d412a43SAl Viro }
9649d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
9659d412a43SAl Viro 
96687129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
96736341f64SRam Pai 					int flag)
9681da177e4SLinus Torvalds {
96987129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
970be34d1a3SDavid Howells 	struct mount *mnt;
971be34d1a3SDavid Howells 	int err;
9721da177e4SLinus Torvalds 
973be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
974be34d1a3SDavid Howells 	if (!mnt)
975be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
976be34d1a3SDavid Howells 
9777a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
97815169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
979719f5d7fSMiklos Szeredi 	else
98015169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
981719f5d7fSMiklos Szeredi 
98215169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
983be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
984719f5d7fSMiklos Szeredi 		if (err)
985719f5d7fSMiklos Szeredi 			goto out_free;
986719f5d7fSMiklos Szeredi 	}
987719f5d7fSMiklos Szeredi 
988f2ebb3a9SAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
989132c94e3SEric W. Biederman 	/* Don't allow unprivileged users to change mount flags */
9909566d674SEric W. Biederman 	if (flag & CL_UNPRIVILEGED) {
9919566d674SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
9929566d674SEric W. Biederman 
9939566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_READONLY)
994132c94e3SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
995132c94e3SEric W. Biederman 
9969566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NODEV)
9979566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
9989566d674SEric W. Biederman 
9999566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NOSUID)
10009566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
10019566d674SEric W. Biederman 
10029566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NOEXEC)
10039566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
10049566d674SEric W. Biederman 	}
10059566d674SEric W. Biederman 
10065ff9d8a6SEric W. Biederman 	/* Don't allow unprivileged users to reveal what is under a mount */
1007381cacb1SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) &&
1008381cacb1SEric W. Biederman 	    (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
10095ff9d8a6SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCKED;
10105ff9d8a6SEric W. Biederman 
10111da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1012b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1013b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1014a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
10150714a533SAl Viro 	mnt->mnt_parent = mnt;
1016719ea2fbSAl Viro 	lock_mount_hash();
101739f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1018719ea2fbSAl Viro 	unlock_mount_hash();
1019b90fa9aeSRam Pai 
10207a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
10217a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
10226776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
102332301920SAl Viro 		mnt->mnt_master = old;
1024fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
10258aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1026fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
10276776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1028d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
10296776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1030d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
10315afe0022SRam Pai 	}
1032b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
10330f0afb1dSAl Viro 		set_mnt_shared(mnt);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
10361da177e4SLinus Torvalds 	 * as the original if that was on one */
103736341f64SRam Pai 	if (flag & CL_EXPIRE) {
10386776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
10396776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
10401da177e4SLinus Torvalds 	}
1041be34d1a3SDavid Howells 
1042cb338d06SAl Viro 	return mnt;
1043719f5d7fSMiklos Szeredi 
1044719f5d7fSMiklos Szeredi  out_free:
10458ffcb32eSDavid Howells 	mnt_free_id(mnt);
1046719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1047be34d1a3SDavid Howells 	return ERR_PTR(err);
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10509ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
10519ea459e1SAl Viro {
10529ea459e1SAl Viro 	/*
10539ea459e1SAl Viro 	 * This probably indicates that somebody messed
10549ea459e1SAl Viro 	 * up a mnt_want/drop_write() pair.  If this
10559ea459e1SAl Viro 	 * happens, the filesystem was probably unable
10569ea459e1SAl Viro 	 * to make r/w->r/o transitions.
10579ea459e1SAl Viro 	 */
10589ea459e1SAl Viro 	/*
10599ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
10609ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
10619ea459e1SAl Viro 	 */
10629ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
10639ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
10649ea459e1SAl Viro 		mnt_pin_kill(mnt);
10659ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
10669ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
10679ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
10689ea459e1SAl Viro 	mnt_free_id(mnt);
10699ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
10709ea459e1SAl Viro }
10719ea459e1SAl Viro 
10729ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
10739ea459e1SAl Viro {
10749ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
10759ea459e1SAl Viro }
10769ea459e1SAl Viro 
10779ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
10789ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
10799ea459e1SAl Viro {
10809ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
10819ea459e1SAl Viro 	struct llist_node *next;
10829ea459e1SAl Viro 
10839ea459e1SAl Viro 	for (; node; node = next) {
10849ea459e1SAl Viro 		next = llist_next(node);
10859ea459e1SAl Viro 		cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
10869ea459e1SAl Viro 	}
10879ea459e1SAl Viro }
10889ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
10899ea459e1SAl Viro 
1090900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
10917b7b1aceSAl Viro {
109248a066e7SAl Viro 	rcu_read_lock();
1093aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
109448a066e7SAl Viro 	if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
109548a066e7SAl Viro 		rcu_read_unlock();
109699b7db7bSNick Piggin 		return;
1097b3e19d92SNick Piggin 	}
1098719ea2fbSAl Viro 	lock_mount_hash();
1099b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
110048a066e7SAl Viro 		rcu_read_unlock();
1101719ea2fbSAl Viro 		unlock_mount_hash();
110299b7db7bSNick Piggin 		return;
110399b7db7bSNick Piggin 	}
110448a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
110548a066e7SAl Viro 		rcu_read_unlock();
110648a066e7SAl Viro 		unlock_mount_hash();
110748a066e7SAl Viro 		return;
110848a066e7SAl Viro 	}
110948a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
111048a066e7SAl Viro 	rcu_read_unlock();
1111962830dfSAndi Kleen 
111239f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1113ce07d891SEric W. Biederman 
1114ce07d891SEric W. Biederman 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1115ce07d891SEric W. Biederman 		struct mount *p, *tmp;
1116ce07d891SEric W. Biederman 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
1117ce07d891SEric W. Biederman 			umount_mnt(p);
1118ce07d891SEric W. Biederman 		}
1119ce07d891SEric W. Biederman 	}
1120719ea2fbSAl Viro 	unlock_mount_hash();
1121649a795aSAl Viro 
11229ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
11239ea459e1SAl Viro 		struct task_struct *task = current;
11249ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
11259ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
11269ea459e1SAl Viro 			if (!task_work_add(task, &mnt->mnt_rcu, true))
11279ea459e1SAl Viro 				return;
11289ea459e1SAl Viro 		}
11299ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
11309ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
11319ea459e1SAl Viro 		return;
11329ea459e1SAl Viro 	}
11339ea459e1SAl Viro 	cleanup_mnt(mnt);
1134b3e19d92SNick Piggin }
1135b3e19d92SNick Piggin 
1136b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1137b3e19d92SNick Piggin {
1138b3e19d92SNick Piggin 	if (mnt) {
1139863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1140b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1141863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1142863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1143863d684fSAl Viro 		mntput_no_expire(m);
1144b3e19d92SNick Piggin 	}
1145b3e19d92SNick Piggin }
1146b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1147b3e19d92SNick Piggin 
1148b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1149b3e19d92SNick Piggin {
1150b3e19d92SNick Piggin 	if (mnt)
115183adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1152b3e19d92SNick Piggin 	return mnt;
1153b3e19d92SNick Piggin }
1154b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1155b3e19d92SNick Piggin 
11563064c356SAl Viro struct vfsmount *mnt_clone_internal(struct path *path)
11577b7b1aceSAl Viro {
11583064c356SAl Viro 	struct mount *p;
11593064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
11603064c356SAl Viro 	if (IS_ERR(p))
11613064c356SAl Viro 		return ERR_CAST(p);
11623064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
11633064c356SAl Viro 	return &p->mnt;
11647b7b1aceSAl Viro }
11651da177e4SLinus Torvalds 
1166b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
1167b3b304a2SMiklos Szeredi {
1168b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
1169b3b304a2SMiklos Szeredi }
1170b3b304a2SMiklos Szeredi 
1171b3b304a2SMiklos Szeredi /*
1172b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
1173b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
1174b3b304a2SMiklos Szeredi  *
1175b3b304a2SMiklos Szeredi  * See also save_mount_options().
1176b3b304a2SMiklos Szeredi  */
117734c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root)
1178b3b304a2SMiklos Szeredi {
11792a32cebdSAl Viro 	const char *options;
11802a32cebdSAl Viro 
11812a32cebdSAl Viro 	rcu_read_lock();
118234c80b1dSAl Viro 	options = rcu_dereference(root->d_sb->s_options);
1183b3b304a2SMiklos Szeredi 
1184b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
1185b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
1186b3b304a2SMiklos Szeredi 		mangle(m, options);
1187b3b304a2SMiklos Szeredi 	}
11882a32cebdSAl Viro 	rcu_read_unlock();
1189b3b304a2SMiklos Szeredi 
1190b3b304a2SMiklos Szeredi 	return 0;
1191b3b304a2SMiklos Szeredi }
1192b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
1193b3b304a2SMiklos Szeredi 
1194b3b304a2SMiklos Szeredi /*
1195b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
1196b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
1197b3b304a2SMiklos Szeredi  *
1198b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
1199b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
1200b3b304a2SMiklos Szeredi  * remount fails.
1201b3b304a2SMiklos Szeredi  *
1202b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
1203b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
1204b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
1205b3b304a2SMiklos Szeredi  * any more.
1206b3b304a2SMiklos Szeredi  */
1207b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
1208b3b304a2SMiklos Szeredi {
12092a32cebdSAl Viro 	BUG_ON(sb->s_options);
12102a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1211b3b304a2SMiklos Szeredi }
1212b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
1213b3b304a2SMiklos Szeredi 
12142a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
12152a32cebdSAl Viro {
12162a32cebdSAl Viro 	char *old = sb->s_options;
12172a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
12182a32cebdSAl Viro 	if (old) {
12192a32cebdSAl Viro 		synchronize_rcu();
12202a32cebdSAl Viro 		kfree(old);
12212a32cebdSAl Viro 	}
12222a32cebdSAl Viro }
12232a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
12242a32cebdSAl Viro 
1225a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
12260226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
12271da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
12281da177e4SLinus Torvalds {
1229ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
12301da177e4SLinus Torvalds 
1231390c6843SRam Pai 	down_read(&namespace_sem);
1232c7999c36SAl Viro 	if (p->cached_event == p->ns->event) {
1233c7999c36SAl Viro 		void *v = p->cached_mount;
1234c7999c36SAl Viro 		if (*pos == p->cached_index)
1235c7999c36SAl Viro 			return v;
1236c7999c36SAl Viro 		if (*pos == p->cached_index + 1) {
1237c7999c36SAl Viro 			v = seq_list_next(v, &p->ns->list, &p->cached_index);
1238c7999c36SAl Viro 			return p->cached_mount = v;
1239c7999c36SAl Viro 		}
1240c7999c36SAl Viro 	}
1241c7999c36SAl Viro 
1242c7999c36SAl Viro 	p->cached_event = p->ns->event;
1243c7999c36SAl Viro 	p->cached_mount = seq_list_start(&p->ns->list, *pos);
1244c7999c36SAl Viro 	p->cached_index = *pos;
1245c7999c36SAl Viro 	return p->cached_mount;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
12491da177e4SLinus Torvalds {
1250ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
1251b0765fb8SPavel Emelianov 
1252c7999c36SAl Viro 	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1253c7999c36SAl Viro 	p->cached_index = *pos;
1254c7999c36SAl Viro 	return p->cached_mount;
12551da177e4SLinus Torvalds }
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
12581da177e4SLinus Torvalds {
1259390c6843SRam Pai 	up_read(&namespace_sem);
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
12620226f492SAl Viro static int m_show(struct seq_file *m, void *v)
12639f5596afSAl Viro {
1264ede1bf0dSYann Droneaud 	struct proc_mounts *p = m->private;
12651a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
12660226f492SAl Viro 	return p->show(m, &r->mnt);
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
1269a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
12701da177e4SLinus Torvalds 	.start	= m_start,
12711da177e4SLinus Torvalds 	.next	= m_next,
12721da177e4SLinus Torvalds 	.stop	= m_stop,
12730226f492SAl Viro 	.show	= m_show,
1274b4629fe2SChuck Lever };
1275a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1276b4629fe2SChuck Lever 
12771da177e4SLinus Torvalds /**
12781da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
12791da177e4SLinus Torvalds  * @mnt: root of mount tree
12801da177e4SLinus Torvalds  *
12811da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
12821da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
12831da177e4SLinus Torvalds  * busy.
12841da177e4SLinus Torvalds  */
1285909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
12861da177e4SLinus Torvalds {
1287909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
128836341f64SRam Pai 	int actual_refs = 0;
128936341f64SRam Pai 	int minimum_refs = 0;
1290315fc83eSAl Viro 	struct mount *p;
1291909b0a88SAl Viro 	BUG_ON(!m);
12921da177e4SLinus Torvalds 
1293b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1294719ea2fbSAl Viro 	lock_mount_hash();
1295909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
129683adc753SAl Viro 		actual_refs += mnt_get_count(p);
12971da177e4SLinus Torvalds 		minimum_refs += 2;
12981da177e4SLinus Torvalds 	}
1299719ea2fbSAl Viro 	unlock_mount_hash();
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
13021da177e4SLinus Torvalds 		return 0;
1303e3474a8eSIan Kent 
1304e3474a8eSIan Kent 	return 1;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds /**
13101da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
13111da177e4SLinus Torvalds  * @mnt: root of mount
13121da177e4SLinus Torvalds  *
13131da177e4SLinus Torvalds  * This is called to check if a mount point has any
13141da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
13151da177e4SLinus Torvalds  * mount has sub mounts this will return busy
13161da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
13171da177e4SLinus Torvalds  *
13181da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
13191da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
13201da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
13211da177e4SLinus Torvalds  */
13221da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
13231da177e4SLinus Torvalds {
1324e3474a8eSIan Kent 	int ret = 1;
13258ad08d8aSAl Viro 	down_read(&namespace_sem);
1326719ea2fbSAl Viro 	lock_mount_hash();
13271ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1328e3474a8eSIan Kent 		ret = 0;
1329719ea2fbSAl Viro 	unlock_mount_hash();
13308ad08d8aSAl Viro 	up_read(&namespace_sem);
1331a05964f3SRam Pai 	return ret;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
13351da177e4SLinus Torvalds 
133638129a13SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
1337e3197d83SAl Viro 
133897216be0SAl Viro static void namespace_unlock(void)
13391da177e4SLinus Torvalds {
1340a3b3c562SEric W. Biederman 	struct hlist_head head;
134197216be0SAl Viro 
1342a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
1343a3b3c562SEric W. Biederman 
134497216be0SAl Viro 	up_write(&namespace_sem);
1345a3b3c562SEric W. Biederman 
1346a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
134797216be0SAl Viro 		return;
134897216be0SAl Viro 
134948a066e7SAl Viro 	synchronize_rcu();
135048a066e7SAl Viro 
135187b95ce0SAl Viro 	group_pin_kill(&head);
135270fbcdf4SRam Pai }
135370fbcdf4SRam Pai 
135497216be0SAl Viro static inline void namespace_lock(void)
1355e3197d83SAl Viro {
135697216be0SAl Viro 	down_write(&namespace_sem);
1357e3197d83SAl Viro }
1358e3197d83SAl Viro 
1359e819f152SEric W. Biederman enum umount_tree_flags {
1360e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1361e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1362e0c9c0afSEric W. Biederman 	UMOUNT_CONNECTED = 4,
1363e819f152SEric W. Biederman };
1364f2d0a123SEric W. Biederman 
1365f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1366f2d0a123SEric W. Biederman {
1367f2d0a123SEric W. Biederman 	/* Leaving mounts connected is only valid for lazy umounts */
1368f2d0a123SEric W. Biederman 	if (how & UMOUNT_SYNC)
1369f2d0a123SEric W. Biederman 		return true;
1370f2d0a123SEric W. Biederman 
1371f2d0a123SEric W. Biederman 	/* A mount without a parent has nothing to be connected to */
1372f2d0a123SEric W. Biederman 	if (!mnt_has_parent(mnt))
1373f2d0a123SEric W. Biederman 		return true;
1374f2d0a123SEric W. Biederman 
1375f2d0a123SEric W. Biederman 	/* Because the reference counting rules change when mounts are
1376f2d0a123SEric W. Biederman 	 * unmounted and connected, umounted mounts may not be
1377f2d0a123SEric W. Biederman 	 * connected to mounted mounts.
1378f2d0a123SEric W. Biederman 	 */
1379f2d0a123SEric W. Biederman 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1380f2d0a123SEric W. Biederman 		return true;
1381f2d0a123SEric W. Biederman 
1382f2d0a123SEric W. Biederman 	/* Has it been requested that the mount remain connected? */
1383f2d0a123SEric W. Biederman 	if (how & UMOUNT_CONNECTED)
1384f2d0a123SEric W. Biederman 		return false;
1385f2d0a123SEric W. Biederman 
1386f2d0a123SEric W. Biederman 	/* Is the mount locked such that it needs to remain connected? */
1387f2d0a123SEric W. Biederman 	if (IS_MNT_LOCKED(mnt))
1388f2d0a123SEric W. Biederman 		return false;
1389f2d0a123SEric W. Biederman 
1390f2d0a123SEric W. Biederman 	/* By default disconnect the mount */
1391f2d0a123SEric W. Biederman 	return true;
1392f2d0a123SEric W. Biederman }
1393f2d0a123SEric W. Biederman 
139499b7db7bSNick Piggin /*
139548a066e7SAl Viro  * mount_lock must be held
139699b7db7bSNick Piggin  * namespace_sem must be held for write
139799b7db7bSNick Piggin  */
1398e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
139970fbcdf4SRam Pai {
1400c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1401315fc83eSAl Viro 	struct mount *p;
140270fbcdf4SRam Pai 
14035d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14045d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
14055d88457eSEric W. Biederman 
1406c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1407590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1408590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1409c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1410590ce4bcSEric W. Biederman 	}
1411c003b26fSEric W. Biederman 
1412411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1413c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1414c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
141538129a13SAl Viro 	}
141670fbcdf4SRam Pai 
1417c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1418e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
14197b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1420a05964f3SRam Pai 
1421c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1422ce07d891SEric W. Biederman 		bool disconnect;
1423c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
14246776db3dSAl Viro 		list_del_init(&p->mnt_expire);
14251a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1426143c8c91SAl Viro 		__touch_mnt_namespace(p->mnt_ns);
142763d37a84SAl Viro 		p->mnt_ns = NULL;
1428e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
142948a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
143087b95ce0SAl Viro 
1431f2d0a123SEric W. Biederman 		disconnect = disconnect_mount(p, how);
1432ce07d891SEric W. Biederman 
1433ce07d891SEric W. Biederman 		pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1434ce07d891SEric W. Biederman 				 disconnect ? &unmounted : NULL);
1435676da58dSAl Viro 		if (mnt_has_parent(p)) {
143681b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
1437ce07d891SEric W. Biederman 			if (!disconnect) {
1438ce07d891SEric W. Biederman 				/* Don't forget about p */
1439ce07d891SEric W. Biederman 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1440ce07d891SEric W. Biederman 			} else {
14416a46c573SEric W. Biederman 				umount_mnt(p);
14427c4b93d8SAl Viro 			}
1443ce07d891SEric W. Biederman 		}
14440f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
144538129a13SAl Viro 	}
14461da177e4SLinus Torvalds }
14471da177e4SLinus Torvalds 
1448b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1449c35038beSAl Viro 
14501ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
14511da177e4SLinus Torvalds {
14521ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
14531da177e4SLinus Torvalds 	int retval;
14541da177e4SLinus Torvalds 
14551ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
14561da177e4SLinus Torvalds 	if (retval)
14571da177e4SLinus Torvalds 		return retval;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	/*
14601da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
14611da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
14621da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
14631da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
14641da177e4SLinus Torvalds 	 */
14651da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
14661ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
14671da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
14681da177e4SLinus Torvalds 			return -EINVAL;
14691da177e4SLinus Torvalds 
1470b3e19d92SNick Piggin 		/*
1471b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1472b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1473b3e19d92SNick Piggin 		 */
1474719ea2fbSAl Viro 		lock_mount_hash();
147583adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1476719ea2fbSAl Viro 			unlock_mount_hash();
14771da177e4SLinus Torvalds 			return -EBUSY;
1478b3e19d92SNick Piggin 		}
1479719ea2fbSAl Viro 		unlock_mount_hash();
14801da177e4SLinus Torvalds 
1481863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
14821da177e4SLinus Torvalds 			return -EAGAIN;
14831da177e4SLinus Torvalds 	}
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	/*
14861da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
14871da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
14881da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
14891da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
14901da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
14911da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
14921da177e4SLinus Torvalds 	 * about for the moment.
14931da177e4SLinus Torvalds 	 */
14941da177e4SLinus Torvalds 
149542faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
149642faad99SAl Viro 		sb->s_op->umount_begin(sb);
149742faad99SAl Viro 	}
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	/*
15001da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
15011da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
15021da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
15031da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
15041da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
15051da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
15061da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
15071da177e4SLinus Torvalds 	 */
15081ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
15091da177e4SLinus Torvalds 		/*
15101da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
15111da177e4SLinus Torvalds 		 * we just try to remount it readonly.
15121da177e4SLinus Torvalds 		 */
1513a1480dccSAndy Lutomirski 		if (!capable(CAP_SYS_ADMIN))
1514a1480dccSAndy Lutomirski 			return -EPERM;
15151da177e4SLinus Torvalds 		down_write(&sb->s_umount);
15164aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
15171da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
15181da177e4SLinus Torvalds 		up_write(&sb->s_umount);
15191da177e4SLinus Torvalds 		return retval;
15201da177e4SLinus Torvalds 	}
15211da177e4SLinus Torvalds 
152297216be0SAl Viro 	namespace_lock();
1523719ea2fbSAl Viro 	lock_mount_hash();
15245addc5ddSAl Viro 	event++;
15251da177e4SLinus Torvalds 
152648a066e7SAl Viro 	if (flags & MNT_DETACH) {
152748a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1528e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
152948a066e7SAl Viro 		retval = 0;
153048a066e7SAl Viro 	} else {
1531b54b9be7SAl Viro 		shrink_submounts(mnt);
15321da177e4SLinus Torvalds 		retval = -EBUSY;
153348a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
15341a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1535e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
15361da177e4SLinus Torvalds 			retval = 0;
15371da177e4SLinus Torvalds 		}
153848a066e7SAl Viro 	}
1539719ea2fbSAl Viro 	unlock_mount_hash();
1540e3197d83SAl Viro 	namespace_unlock();
15411da177e4SLinus Torvalds 	return retval;
15421da177e4SLinus Torvalds }
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds /*
154580b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
154680b5dce8SEric W. Biederman  *
154780b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
154880b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
154980b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
155080b5dce8SEric W. Biederman  * leaking them.
155180b5dce8SEric W. Biederman  *
155280b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
155380b5dce8SEric W. Biederman  */
155480b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
155580b5dce8SEric W. Biederman {
155680b5dce8SEric W. Biederman 	struct mountpoint *mp;
155780b5dce8SEric W. Biederman 	struct mount *mnt;
155880b5dce8SEric W. Biederman 
155980b5dce8SEric W. Biederman 	namespace_lock();
156080b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
1561f53e5797SEric W. Biederman 	if (IS_ERR_OR_NULL(mp))
156280b5dce8SEric W. Biederman 		goto out_unlock;
156380b5dce8SEric W. Biederman 
156480b5dce8SEric W. Biederman 	lock_mount_hash();
1565e06b933eSAndrey Ulanov 	event++;
156680b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
156780b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1568ce07d891SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1569fe78fcc8SEric W. Biederman 			hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1570fe78fcc8SEric W. Biederman 			umount_mnt(mnt);
1571ce07d891SEric W. Biederman 		}
1572e0c9c0afSEric W. Biederman 		else umount_tree(mnt, UMOUNT_CONNECTED);
157380b5dce8SEric W. Biederman 	}
157480b5dce8SEric W. Biederman 	unlock_mount_hash();
157580b5dce8SEric W. Biederman 	put_mountpoint(mp);
157680b5dce8SEric W. Biederman out_unlock:
157780b5dce8SEric W. Biederman 	namespace_unlock();
157880b5dce8SEric W. Biederman }
157980b5dce8SEric W. Biederman 
158080b5dce8SEric W. Biederman /*
15819b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
15829b40bc90SAl Viro  */
15839b40bc90SAl Viro static inline bool may_mount(void)
15849b40bc90SAl Viro {
15859b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
15869b40bc90SAl Viro }
15879b40bc90SAl Viro 
15889e8925b6SJeff Layton static inline bool may_mandlock(void)
15899e8925b6SJeff Layton {
15909e8925b6SJeff Layton #ifndef	CONFIG_MANDATORY_FILE_LOCKING
15919e8925b6SJeff Layton 	return false;
15929e8925b6SJeff Layton #endif
159395ace754SEric W. Biederman 	return capable(CAP_SYS_ADMIN);
15949e8925b6SJeff Layton }
15959e8925b6SJeff Layton 
15969b40bc90SAl Viro /*
15971da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
15981da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
15991da177e4SLinus Torvalds  *
16001da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
16011da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
16021da177e4SLinus Torvalds  */
16031da177e4SLinus Torvalds 
1604bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
16051da177e4SLinus Torvalds {
16062d8f3038SAl Viro 	struct path path;
1607900148dcSAl Viro 	struct mount *mnt;
16081da177e4SLinus Torvalds 	int retval;
1609db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
16101da177e4SLinus Torvalds 
1611db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1612db1f05bbSMiklos Szeredi 		return -EINVAL;
1613db1f05bbSMiklos Szeredi 
16149b40bc90SAl Viro 	if (!may_mount())
16159b40bc90SAl Viro 		return -EPERM;
16169b40bc90SAl Viro 
1617db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1618db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1619db1f05bbSMiklos Szeredi 
1620197df04cSAl Viro 	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
16211da177e4SLinus Torvalds 	if (retval)
16221da177e4SLinus Torvalds 		goto out;
1623900148dcSAl Viro 	mnt = real_mount(path.mnt);
16241da177e4SLinus Torvalds 	retval = -EINVAL;
16252d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
16261da177e4SLinus Torvalds 		goto dput_and_out;
1627143c8c91SAl Viro 	if (!check_mnt(mnt))
16281da177e4SLinus Torvalds 		goto dput_and_out;
16295ff9d8a6SEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
16305ff9d8a6SEric W. Biederman 		goto dput_and_out;
1631b2f5d4dcSEric W. Biederman 	retval = -EPERM;
1632b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1633b2f5d4dcSEric W. Biederman 		goto dput_and_out;
16341da177e4SLinus Torvalds 
1635900148dcSAl Viro 	retval = do_umount(mnt, flags);
16361da177e4SLinus Torvalds dput_and_out:
1637429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
16382d8f3038SAl Viro 	dput(path.dentry);
1639900148dcSAl Viro 	mntput_no_expire(mnt);
16401da177e4SLinus Torvalds out:
16411da177e4SLinus Torvalds 	return retval;
16421da177e4SLinus Torvalds }
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds /*
16471da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
16481da177e4SLinus Torvalds  */
1649bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
16501da177e4SLinus Torvalds {
16511da177e4SLinus Torvalds 	return sys_umount(name, 0);
16521da177e4SLinus Torvalds }
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds #endif
16551da177e4SLinus Torvalds 
16564ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
16578823c079SEric W. Biederman {
16584ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1659e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1660e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
16614ce5d2b1SEric W. Biederman }
16624ce5d2b1SEric W. Biederman 
166358be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
166458be2825SAl Viro {
166558be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
166658be2825SAl Viro }
166758be2825SAl Viro 
16684ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
16694ce5d2b1SEric W. Biederman {
16704ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
16714ce5d2b1SEric W. Biederman 	 * mount namespace loop?
16724ce5d2b1SEric W. Biederman 	 */
16734ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
16744ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
16754ce5d2b1SEric W. Biederman 		return false;
16764ce5d2b1SEric W. Biederman 
1677f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
16788823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
16798823c079SEric W. Biederman }
16808823c079SEric W. Biederman 
168187129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
168236341f64SRam Pai 					int flag)
16831da177e4SLinus Torvalds {
168484d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
16851da177e4SLinus Torvalds 
16864ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
16874ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
16884ce5d2b1SEric W. Biederman 
16894ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1690be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
16919676f0c6SRam Pai 
169236341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1693be34d1a3SDavid Howells 	if (IS_ERR(q))
1694be34d1a3SDavid Howells 		return q;
1695be34d1a3SDavid Howells 
1696a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	p = mnt;
16996b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1700315fc83eSAl Viro 		struct mount *s;
17017ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
17021da177e4SLinus Torvalds 			continue;
17031da177e4SLinus Torvalds 
1704909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
170512a5b529SAl Viro 			struct mount *t = NULL;
17064ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
17074ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
17084ce5d2b1SEric W. Biederman 				s = skip_mnt_tree(s);
17094ce5d2b1SEric W. Biederman 				continue;
17104ce5d2b1SEric W. Biederman 			}
17114ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
17124ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
17139676f0c6SRam Pai 				s = skip_mnt_tree(s);
17149676f0c6SRam Pai 				continue;
17159676f0c6SRam Pai 			}
17160714a533SAl Viro 			while (p != s->mnt_parent) {
17170714a533SAl Viro 				p = p->mnt_parent;
17180714a533SAl Viro 				q = q->mnt_parent;
17191da177e4SLinus Torvalds 			}
172087129cc0SAl Viro 			p = s;
172184d17192SAl Viro 			parent = q;
172287129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1723be34d1a3SDavid Howells 			if (IS_ERR(q))
1724be34d1a3SDavid Howells 				goto out;
1725719ea2fbSAl Viro 			lock_mount_hash();
17261a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
172712a5b529SAl Viro 			mnt_set_mountpoint(parent, p->mnt_mp, q);
172812a5b529SAl Viro 			if (!list_empty(&parent->mnt_mounts)) {
172912a5b529SAl Viro 				t = list_last_entry(&parent->mnt_mounts,
173012a5b529SAl Viro 					struct mount, mnt_child);
173112a5b529SAl Viro 				if (t->mnt_mp != p->mnt_mp)
173212a5b529SAl Viro 					t = NULL;
173312a5b529SAl Viro 			}
173412a5b529SAl Viro 			attach_shadowed(q, parent, t);
1735719ea2fbSAl Viro 			unlock_mount_hash();
17361da177e4SLinus Torvalds 		}
17371da177e4SLinus Torvalds 	}
17381da177e4SLinus Torvalds 	return res;
1739be34d1a3SDavid Howells out:
17401da177e4SLinus Torvalds 	if (res) {
1741719ea2fbSAl Viro 		lock_mount_hash();
1742e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
1743719ea2fbSAl Viro 		unlock_mount_hash();
17441da177e4SLinus Torvalds 	}
1745be34d1a3SDavid Howells 	return q;
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds 
1748be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1749be34d1a3SDavid Howells 
1750589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
17518aec0809SAl Viro {
1752cb338d06SAl Viro 	struct mount *tree;
175397216be0SAl Viro 	namespace_lock();
1754cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
1755cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
1756cd4a4017SEric W. Biederman 	else
175787129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
175887129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
1759328e6d90SAl Viro 	namespace_unlock();
1760be34d1a3SDavid Howells 	if (IS_ERR(tree))
176152e220d3SDan Carpenter 		return ERR_CAST(tree);
1762be34d1a3SDavid Howells 	return &tree->mnt;
17638aec0809SAl Viro }
17648aec0809SAl Viro 
17658aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
17668aec0809SAl Viro {
176797216be0SAl Viro 	namespace_lock();
1768719ea2fbSAl Viro 	lock_mount_hash();
1769e819f152SEric W. Biederman 	umount_tree(real_mount(mnt), UMOUNT_SYNC);
1770719ea2fbSAl Viro 	unlock_mount_hash();
17713ab6abeeSAl Viro 	namespace_unlock();
17728aec0809SAl Viro }
17738aec0809SAl Viro 
1774c771d683SMiklos Szeredi /**
1775c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
1776c771d683SMiklos Szeredi  *
1777c771d683SMiklos Szeredi  * This creates a new vfsmount, which will be the clone of @path.  The new will
1778c771d683SMiklos Szeredi  * not be attached anywhere in the namespace and will be private (i.e. changes
1779c771d683SMiklos Szeredi  * to the originating mount won't be propagated into this).
1780c771d683SMiklos Szeredi  *
1781c771d683SMiklos Szeredi  * Release with mntput().
1782c771d683SMiklos Szeredi  */
1783c771d683SMiklos Szeredi struct vfsmount *clone_private_mount(struct path *path)
1784c771d683SMiklos Szeredi {
1785c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
1786c771d683SMiklos Szeredi 	struct mount *new_mnt;
1787c771d683SMiklos Szeredi 
1788c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
1789c771d683SMiklos Szeredi 		return ERR_PTR(-EINVAL);
1790c771d683SMiklos Szeredi 
1791c771d683SMiklos Szeredi 	down_read(&namespace_sem);
1792c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1793c771d683SMiklos Szeredi 	up_read(&namespace_sem);
1794c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
1795c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
1796c771d683SMiklos Szeredi 
1797c771d683SMiklos Szeredi 	return &new_mnt->mnt;
1798c771d683SMiklos Szeredi }
1799c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
1800c771d683SMiklos Szeredi 
18011f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
18021f707137SAl Viro 		   struct vfsmount *root)
18031f707137SAl Viro {
18041a4eeaf2SAl Viro 	struct mount *mnt;
18051f707137SAl Viro 	int res = f(root, arg);
18061f707137SAl Viro 	if (res)
18071f707137SAl Viro 		return res;
18081a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
18091a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
18101f707137SAl Viro 		if (res)
18111f707137SAl Viro 			return res;
18121f707137SAl Viro 	}
18131f707137SAl Viro 	return 0;
18141f707137SAl Viro }
18151f707137SAl Viro 
18164b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1817719f5d7fSMiklos Szeredi {
1818315fc83eSAl Viro 	struct mount *p;
1819719f5d7fSMiklos Szeredi 
1820909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1821fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
18224b8b21f4SAl Viro 			mnt_release_group_id(p);
1823719f5d7fSMiklos Szeredi 	}
1824719f5d7fSMiklos Szeredi }
1825719f5d7fSMiklos Szeredi 
18264b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1827719f5d7fSMiklos Szeredi {
1828315fc83eSAl Viro 	struct mount *p;
1829719f5d7fSMiklos Szeredi 
1830909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1831fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
18324b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1833719f5d7fSMiklos Szeredi 			if (err) {
18344b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1835719f5d7fSMiklos Szeredi 				return err;
1836719f5d7fSMiklos Szeredi 			}
1837719f5d7fSMiklos Szeredi 		}
1838719f5d7fSMiklos Szeredi 	}
1839719f5d7fSMiklos Szeredi 
1840719f5d7fSMiklos Szeredi 	return 0;
1841719f5d7fSMiklos Szeredi }
1842719f5d7fSMiklos Szeredi 
1843b90fa9aeSRam Pai /*
1844b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1845b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
184621444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
184721444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
184821444403SRam Pai  *  		   (done when source_mnt is moved)
1849b90fa9aeSRam Pai  *
1850b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1851b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
18529676f0c6SRam Pai  * ---------------------------------------------------------------------------
1853b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
18549676f0c6SRam Pai  * |**************************************************************************
18559676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
18569676f0c6SRam Pai  * | dest     |               |                |                |            |
18579676f0c6SRam Pai  * |   |      |               |                |                |            |
18589676f0c6SRam Pai  * |   v      |               |                |                |            |
18599676f0c6SRam Pai  * |**************************************************************************
18609676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
18615afe0022SRam Pai  * |          |               |                |                |            |
18629676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
18639676f0c6SRam Pai  * ***************************************************************************
1864b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1865b90fa9aeSRam Pai  * destination mount.
1866b90fa9aeSRam Pai  *
1867b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1868b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1869b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1870b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1871b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1872b90fa9aeSRam Pai  *       mount.
18735afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
18745afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
18755afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
18765afe0022SRam Pai  *       is marked as 'shared and slave'.
18775afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
18785afe0022SRam Pai  * 	 source mount.
18795afe0022SRam Pai  *
18809676f0c6SRam Pai  * ---------------------------------------------------------------------------
188121444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
18829676f0c6SRam Pai  * |**************************************************************************
18839676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
18849676f0c6SRam Pai  * | dest     |               |                |                |            |
18859676f0c6SRam Pai  * |   |      |               |                |                |            |
18869676f0c6SRam Pai  * |   v      |               |                |                |            |
18879676f0c6SRam Pai  * |**************************************************************************
18889676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
18895afe0022SRam Pai  * |          |               |                |                |            |
18909676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
18919676f0c6SRam Pai  * ***************************************************************************
18925afe0022SRam Pai  *
18935afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
18945afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
189521444403SRam Pai  * (+*)  the mount is moved to the destination.
18965afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
18975afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
18985afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
18995afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1900b90fa9aeSRam Pai  *
1901b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1902b90fa9aeSRam Pai  * applied to each mount in the tree.
1903b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1904b90fa9aeSRam Pai  * in allocations.
1905b90fa9aeSRam Pai  */
19060fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
190784d17192SAl Viro 			struct mount *dest_mnt,
190884d17192SAl Viro 			struct mountpoint *dest_mp,
190984d17192SAl Viro 			struct path *parent_path)
1910b90fa9aeSRam Pai {
191138129a13SAl Viro 	HLIST_HEAD(tree_list);
1912315fc83eSAl Viro 	struct mount *child, *p;
191338129a13SAl Viro 	struct hlist_node *n;
1914719f5d7fSMiklos Szeredi 	int err;
1915b90fa9aeSRam Pai 
1916fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
19170fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
1918719f5d7fSMiklos Szeredi 		if (err)
1919719f5d7fSMiklos Szeredi 			goto out;
192084d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1921f2ebb3a9SAl Viro 		lock_mount_hash();
1922719f5d7fSMiklos Szeredi 		if (err)
1923719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
1924909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
19250f0afb1dSAl Viro 			set_mnt_shared(p);
19260b1b901bSAl Viro 	} else {
19270b1b901bSAl Viro 		lock_mount_hash();
1928b90fa9aeSRam Pai 	}
19291a390689SAl Viro 	if (parent_path) {
19300fb54e50SAl Viro 		detach_mnt(source_mnt, parent_path);
193184d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
1932143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
193321444403SRam Pai 	} else {
193484d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
19351d6a32acSAl Viro 		commit_tree(source_mnt, NULL);
193621444403SRam Pai 	}
1937b90fa9aeSRam Pai 
193838129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
19391d6a32acSAl Viro 		struct mount *q;
194038129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
19411d6a32acSAl Viro 		q = __lookup_mnt_last(&child->mnt_parent->mnt,
19421d6a32acSAl Viro 				      child->mnt_mountpoint);
19431d6a32acSAl Viro 		commit_tree(child, q);
1944b90fa9aeSRam Pai 	}
1945719ea2fbSAl Viro 	unlock_mount_hash();
194699b7db7bSNick Piggin 
1947b90fa9aeSRam Pai 	return 0;
1948719f5d7fSMiklos Szeredi 
1949719f5d7fSMiklos Szeredi  out_cleanup_ids:
1950f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
1951f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
1952e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
1953f2ebb3a9SAl Viro 	}
1954f2ebb3a9SAl Viro 	unlock_mount_hash();
19550fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
1956719f5d7fSMiklos Szeredi  out:
1957719f5d7fSMiklos Szeredi 	return err;
1958b90fa9aeSRam Pai }
1959b90fa9aeSRam Pai 
196084d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
1961b12cea91SAl Viro {
1962b12cea91SAl Viro 	struct vfsmount *mnt;
196384d17192SAl Viro 	struct dentry *dentry = path->dentry;
1964b12cea91SAl Viro retry:
19655955102cSAl Viro 	inode_lock(dentry->d_inode);
196684d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
19675955102cSAl Viro 		inode_unlock(dentry->d_inode);
196884d17192SAl Viro 		return ERR_PTR(-ENOENT);
1969b12cea91SAl Viro 	}
197097216be0SAl Viro 	namespace_lock();
1971b12cea91SAl Viro 	mnt = lookup_mnt(path);
197284d17192SAl Viro 	if (likely(!mnt)) {
1973e2dfa935SEric W. Biederman 		struct mountpoint *mp = lookup_mountpoint(dentry);
1974e2dfa935SEric W. Biederman 		if (!mp)
1975e2dfa935SEric W. Biederman 			mp = new_mountpoint(dentry);
197684d17192SAl Viro 		if (IS_ERR(mp)) {
197797216be0SAl Viro 			namespace_unlock();
19785955102cSAl Viro 			inode_unlock(dentry->d_inode);
197984d17192SAl Viro 			return mp;
198084d17192SAl Viro 		}
198184d17192SAl Viro 		return mp;
198284d17192SAl Viro 	}
198397216be0SAl Viro 	namespace_unlock();
19845955102cSAl Viro 	inode_unlock(path->dentry->d_inode);
1985b12cea91SAl Viro 	path_put(path);
1986b12cea91SAl Viro 	path->mnt = mnt;
198784d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
1988b12cea91SAl Viro 	goto retry;
1989b12cea91SAl Viro }
1990b12cea91SAl Viro 
199184d17192SAl Viro static void unlock_mount(struct mountpoint *where)
1992b12cea91SAl Viro {
199384d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
199484d17192SAl Viro 	put_mountpoint(where);
1995328e6d90SAl Viro 	namespace_unlock();
19965955102cSAl Viro 	inode_unlock(dentry->d_inode);
1997b12cea91SAl Viro }
1998b12cea91SAl Viro 
199984d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
20001da177e4SLinus Torvalds {
200195bc5f25SAl Viro 	if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
20021da177e4SLinus Torvalds 		return -EINVAL;
20031da177e4SLinus Torvalds 
2004e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
2005e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
20061da177e4SLinus Torvalds 		return -ENOTDIR;
20071da177e4SLinus Torvalds 
200884d17192SAl Viro 	return attach_recursive_mnt(mnt, p, mp, NULL);
20091da177e4SLinus Torvalds }
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds /*
20127a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
20137a2e8a8fSValerie Aurora  */
20147a2e8a8fSValerie Aurora 
20157a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags)
20167a2e8a8fSValerie Aurora {
20177c6e984dSRoman Borisov 	int type = flags & ~(MS_REC | MS_SILENT);
20187a2e8a8fSValerie Aurora 
20197a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
20207a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
20217a2e8a8fSValerie Aurora 		return 0;
20227a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
20237a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
20247a2e8a8fSValerie Aurora 		return 0;
20257a2e8a8fSValerie Aurora 	return type;
20267a2e8a8fSValerie Aurora }
20277a2e8a8fSValerie Aurora 
20287a2e8a8fSValerie Aurora /*
202907b20889SRam Pai  * recursively change the type of the mountpoint.
203007b20889SRam Pai  */
20310a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
203207b20889SRam Pai {
2033315fc83eSAl Viro 	struct mount *m;
20344b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
203507b20889SRam Pai 	int recurse = flag & MS_REC;
20367a2e8a8fSValerie Aurora 	int type;
2037719f5d7fSMiklos Szeredi 	int err = 0;
203807b20889SRam Pai 
20392d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
204007b20889SRam Pai 		return -EINVAL;
204107b20889SRam Pai 
20427a2e8a8fSValerie Aurora 	type = flags_to_propagation_type(flag);
20437a2e8a8fSValerie Aurora 	if (!type)
20447a2e8a8fSValerie Aurora 		return -EINVAL;
20457a2e8a8fSValerie Aurora 
204697216be0SAl Viro 	namespace_lock();
2047719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
2048719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
2049719f5d7fSMiklos Szeredi 		if (err)
2050719f5d7fSMiklos Szeredi 			goto out_unlock;
2051719f5d7fSMiklos Szeredi 	}
2052719f5d7fSMiklos Szeredi 
2053719ea2fbSAl Viro 	lock_mount_hash();
2054909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
20550f0afb1dSAl Viro 		change_mnt_propagation(m, type);
2056719ea2fbSAl Viro 	unlock_mount_hash();
2057719f5d7fSMiklos Szeredi 
2058719f5d7fSMiklos Szeredi  out_unlock:
205997216be0SAl Viro 	namespace_unlock();
2060719f5d7fSMiklos Szeredi 	return err;
206107b20889SRam Pai }
206207b20889SRam Pai 
20635ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
20645ff9d8a6SEric W. Biederman {
20655ff9d8a6SEric W. Biederman 	struct mount *child;
20665ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
20675ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
20685ff9d8a6SEric W. Biederman 			continue;
20695ff9d8a6SEric W. Biederman 
20705ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
20715ff9d8a6SEric W. Biederman 			return true;
20725ff9d8a6SEric W. Biederman 	}
20735ff9d8a6SEric W. Biederman 	return false;
20745ff9d8a6SEric W. Biederman }
20755ff9d8a6SEric W. Biederman 
207607b20889SRam Pai /*
20771da177e4SLinus Torvalds  * do loopback mount.
20781da177e4SLinus Torvalds  */
2079808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
20802dafe1c4SEric Sandeen 				int recurse)
20811da177e4SLinus Torvalds {
20822d92ab3cSAl Viro 	struct path old_path;
208384d17192SAl Viro 	struct mount *mnt = NULL, *old, *parent;
208484d17192SAl Viro 	struct mountpoint *mp;
208557eccb83SAl Viro 	int err;
20861da177e4SLinus Torvalds 	if (!old_name || !*old_name)
20871da177e4SLinus Torvalds 		return -EINVAL;
2088815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
20891da177e4SLinus Torvalds 	if (err)
20901da177e4SLinus Torvalds 		return err;
20911da177e4SLinus Torvalds 
20928823c079SEric W. Biederman 	err = -EINVAL;
20934ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
20948823c079SEric W. Biederman 		goto out;
20958823c079SEric W. Biederman 
209684d17192SAl Viro 	mp = lock_mount(path);
209784d17192SAl Viro 	err = PTR_ERR(mp);
209884d17192SAl Viro 	if (IS_ERR(mp))
20999676f0c6SRam Pai 		goto out;
21009676f0c6SRam Pai 
210187129cc0SAl Viro 	old = real_mount(old_path.mnt);
210284d17192SAl Viro 	parent = real_mount(path->mnt);
210387129cc0SAl Viro 
2104b12cea91SAl Viro 	err = -EINVAL;
2105fc7be130SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2106b12cea91SAl Viro 		goto out2;
2107b12cea91SAl Viro 
2108e149ed2bSAl Viro 	if (!check_mnt(parent))
2109e149ed2bSAl Viro 		goto out2;
2110e149ed2bSAl Viro 
2111e149ed2bSAl Viro 	if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
2112b12cea91SAl Viro 		goto out2;
2113ccd48bc7SAl Viro 
21145ff9d8a6SEric W. Biederman 	if (!recurse && has_locked_children(old, old_path.dentry))
21155ff9d8a6SEric W. Biederman 		goto out2;
21165ff9d8a6SEric W. Biederman 
21171da177e4SLinus Torvalds 	if (recurse)
21184ce5d2b1SEric W. Biederman 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
21191da177e4SLinus Torvalds 	else
212087129cc0SAl Viro 		mnt = clone_mnt(old, old_path.dentry, 0);
21211da177e4SLinus Torvalds 
2122be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2123be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2124e9c5d8a5SAndrey Vagin 		goto out2;
2125be34d1a3SDavid Howells 	}
2126ccd48bc7SAl Viro 
21275ff9d8a6SEric W. Biederman 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
21285ff9d8a6SEric W. Biederman 
212984d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
21301da177e4SLinus Torvalds 	if (err) {
2131719ea2fbSAl Viro 		lock_mount_hash();
2132e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2133719ea2fbSAl Viro 		unlock_mount_hash();
21345b83d2c5SRam Pai 	}
2135b12cea91SAl Viro out2:
213684d17192SAl Viro 	unlock_mount(mp);
2137ccd48bc7SAl Viro out:
21382d92ab3cSAl Viro 	path_put(&old_path);
21391da177e4SLinus Torvalds 	return err;
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
21422e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
21432e4b7fcdSDave Hansen {
21442e4b7fcdSDave Hansen 	int error = 0;
21452e4b7fcdSDave Hansen 	int readonly_request = 0;
21462e4b7fcdSDave Hansen 
21472e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
21482e4b7fcdSDave Hansen 		readonly_request = 1;
21492e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
21502e4b7fcdSDave Hansen 		return 0;
21512e4b7fcdSDave Hansen 
21522e4b7fcdSDave Hansen 	if (readonly_request)
215383adc753SAl Viro 		error = mnt_make_readonly(real_mount(mnt));
21542e4b7fcdSDave Hansen 	else
215583adc753SAl Viro 		__mnt_unmake_readonly(real_mount(mnt));
21562e4b7fcdSDave Hansen 	return error;
21572e4b7fcdSDave Hansen }
21582e4b7fcdSDave Hansen 
21591da177e4SLinus Torvalds /*
21601da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
21611da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
21621da177e4SLinus Torvalds  * on it - tough luck.
21631da177e4SLinus Torvalds  */
21640a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
21651da177e4SLinus Torvalds 		      void *data)
21661da177e4SLinus Torvalds {
21671da177e4SLinus Torvalds 	int err;
21682d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2169143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
21701da177e4SLinus Torvalds 
2171143c8c91SAl Viro 	if (!check_mnt(mnt))
21721da177e4SLinus Torvalds 		return -EINVAL;
21731da177e4SLinus Torvalds 
21742d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
21751da177e4SLinus Torvalds 		return -EINVAL;
21761da177e4SLinus Torvalds 
217707b64558SEric W. Biederman 	/* Don't allow changing of locked mnt flags.
217807b64558SEric W. Biederman 	 *
217907b64558SEric W. Biederman 	 * No locks need to be held here while testing the various
218007b64558SEric W. Biederman 	 * MNT_LOCK flags because those flags can never be cleared
218107b64558SEric W. Biederman 	 * once they are set.
218207b64558SEric W. Biederman 	 */
218307b64558SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
218407b64558SEric W. Biederman 	    !(mnt_flags & MNT_READONLY)) {
218507b64558SEric W. Biederman 		return -EPERM;
218607b64558SEric W. Biederman 	}
21879566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
21889566d674SEric W. Biederman 	    !(mnt_flags & MNT_NODEV)) {
21893e186641SEric W. Biederman 		/* Was the nodev implicitly added in mount? */
21903e186641SEric W. Biederman 		if ((mnt->mnt_ns->user_ns != &init_user_ns) &&
21913e186641SEric W. Biederman 		    !(sb->s_type->fs_flags & FS_USERNS_DEV_MOUNT)) {
21923e186641SEric W. Biederman 			mnt_flags |= MNT_NODEV;
21933e186641SEric W. Biederman 		} else {
21949566d674SEric W. Biederman 			return -EPERM;
21959566d674SEric W. Biederman 		}
21963e186641SEric W. Biederman 	}
21979566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
21989566d674SEric W. Biederman 	    !(mnt_flags & MNT_NOSUID)) {
21999566d674SEric W. Biederman 		return -EPERM;
22009566d674SEric W. Biederman 	}
22019566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
22029566d674SEric W. Biederman 	    !(mnt_flags & MNT_NOEXEC)) {
22039566d674SEric W. Biederman 		return -EPERM;
22049566d674SEric W. Biederman 	}
22059566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
22069566d674SEric W. Biederman 	    ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
22079566d674SEric W. Biederman 		return -EPERM;
22089566d674SEric W. Biederman 	}
22099566d674SEric W. Biederman 
2210ff36fe2cSEric Paris 	err = security_sb_remount(sb, data);
2211ff36fe2cSEric Paris 	if (err)
2212ff36fe2cSEric Paris 		return err;
2213ff36fe2cSEric Paris 
22141da177e4SLinus Torvalds 	down_write(&sb->s_umount);
22152e4b7fcdSDave Hansen 	if (flags & MS_BIND)
22162d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
221757eccb83SAl Viro 	else if (!capable(CAP_SYS_ADMIN))
221857eccb83SAl Viro 		err = -EPERM;
22194aa98cf7SAl Viro 	else
22201da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
22217b43a79fSAl Viro 	if (!err) {
2222719ea2fbSAl Viro 		lock_mount_hash();
2223a6138db8SEric W. Biederman 		mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2224143c8c91SAl Viro 		mnt->mnt.mnt_flags = mnt_flags;
2225143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2226719ea2fbSAl Viro 		unlock_mount_hash();
22270e55a7ccSDan Williams 	}
22286339dab8SAl Viro 	up_write(&sb->s_umount);
22291da177e4SLinus Torvalds 	return err;
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
2232cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
22339676f0c6SRam Pai {
2234315fc83eSAl Viro 	struct mount *p;
2235909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2236fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
22379676f0c6SRam Pai 			return 1;
22389676f0c6SRam Pai 	}
22399676f0c6SRam Pai 	return 0;
22409676f0c6SRam Pai }
22419676f0c6SRam Pai 
2242808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name)
22431da177e4SLinus Torvalds {
22442d92ab3cSAl Viro 	struct path old_path, parent_path;
2245676da58dSAl Viro 	struct mount *p;
22460fb54e50SAl Viro 	struct mount *old;
224784d17192SAl Viro 	struct mountpoint *mp;
224857eccb83SAl Viro 	int err;
22491da177e4SLinus Torvalds 	if (!old_name || !*old_name)
22501da177e4SLinus Torvalds 		return -EINVAL;
22512d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
22521da177e4SLinus Torvalds 	if (err)
22531da177e4SLinus Torvalds 		return err;
22541da177e4SLinus Torvalds 
225584d17192SAl Viro 	mp = lock_mount(path);
225684d17192SAl Viro 	err = PTR_ERR(mp);
225784d17192SAl Viro 	if (IS_ERR(mp))
2258cc53ce53SDavid Howells 		goto out;
2259cc53ce53SDavid Howells 
2260143c8c91SAl Viro 	old = real_mount(old_path.mnt);
2261fc7be130SAl Viro 	p = real_mount(path->mnt);
2262143c8c91SAl Viro 
22631da177e4SLinus Torvalds 	err = -EINVAL;
2264fc7be130SAl Viro 	if (!check_mnt(p) || !check_mnt(old))
22651da177e4SLinus Torvalds 		goto out1;
22661da177e4SLinus Torvalds 
22675ff9d8a6SEric W. Biederman 	if (old->mnt.mnt_flags & MNT_LOCKED)
22685ff9d8a6SEric W. Biederman 		goto out1;
22695ff9d8a6SEric W. Biederman 
22701da177e4SLinus Torvalds 	err = -EINVAL;
22712d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
227221444403SRam Pai 		goto out1;
22731da177e4SLinus Torvalds 
2274676da58dSAl Viro 	if (!mnt_has_parent(old))
227521444403SRam Pai 		goto out1;
22761da177e4SLinus Torvalds 
2277e36cb0b8SDavid Howells 	if (d_is_dir(path->dentry) !=
2278e36cb0b8SDavid Howells 	      d_is_dir(old_path.dentry))
227921444403SRam Pai 		goto out1;
228021444403SRam Pai 	/*
228121444403SRam Pai 	 * Don't move a mount residing in a shared parent.
228221444403SRam Pai 	 */
2283fc7be130SAl Viro 	if (IS_MNT_SHARED(old->mnt_parent))
228421444403SRam Pai 		goto out1;
22859676f0c6SRam Pai 	/*
22869676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
22879676f0c6SRam Pai 	 * mount which is shared.
22889676f0c6SRam Pai 	 */
2289fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
22909676f0c6SRam Pai 		goto out1;
22911da177e4SLinus Torvalds 	err = -ELOOP;
2292fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2293676da58dSAl Viro 		if (p == old)
229421444403SRam Pai 			goto out1;
22951da177e4SLinus Torvalds 
229684d17192SAl Viro 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
22974ac91378SJan Blunck 	if (err)
229821444403SRam Pai 		goto out1;
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
23011da177e4SLinus Torvalds 	 * automatically */
23026776db3dSAl Viro 	list_del_init(&old->mnt_expire);
23031da177e4SLinus Torvalds out1:
230484d17192SAl Viro 	unlock_mount(mp);
23051da177e4SLinus Torvalds out:
23061da177e4SLinus Torvalds 	if (!err)
23071a390689SAl Viro 		path_put(&parent_path);
23082d92ab3cSAl Viro 	path_put(&old_path);
23091da177e4SLinus Torvalds 	return err;
23101da177e4SLinus Torvalds }
23111da177e4SLinus Torvalds 
23129d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
23139d412a43SAl Viro {
23149d412a43SAl Viro 	int err;
23159d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
23169d412a43SAl Viro 	if (subtype) {
23179d412a43SAl Viro 		subtype++;
23189d412a43SAl Viro 		err = -EINVAL;
23199d412a43SAl Viro 		if (!subtype[0])
23209d412a43SAl Viro 			goto err;
23219d412a43SAl Viro 	} else
23229d412a43SAl Viro 		subtype = "";
23239d412a43SAl Viro 
23249d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
23259d412a43SAl Viro 	err = -ENOMEM;
23269d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
23279d412a43SAl Viro 		goto err;
23289d412a43SAl Viro 	return mnt;
23299d412a43SAl Viro 
23309d412a43SAl Viro  err:
23319d412a43SAl Viro 	mntput(mnt);
23329d412a43SAl Viro 	return ERR_PTR(err);
23339d412a43SAl Viro }
23349d412a43SAl Viro 
23359d412a43SAl Viro /*
23369d412a43SAl Viro  * add a mount into a namespace's mount tree
23379d412a43SAl Viro  */
233895bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
23399d412a43SAl Viro {
234084d17192SAl Viro 	struct mountpoint *mp;
234184d17192SAl Viro 	struct mount *parent;
23429d412a43SAl Viro 	int err;
23439d412a43SAl Viro 
2344f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
23459d412a43SAl Viro 
234684d17192SAl Viro 	mp = lock_mount(path);
234784d17192SAl Viro 	if (IS_ERR(mp))
234884d17192SAl Viro 		return PTR_ERR(mp);
23499d412a43SAl Viro 
235084d17192SAl Viro 	parent = real_mount(path->mnt);
23519d412a43SAl Viro 	err = -EINVAL;
235284d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2353156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2354156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
23559d412a43SAl Viro 			goto unlock;
2356156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
235784d17192SAl Viro 		if (!parent->mnt_ns)
2358156cacb1SAl Viro 			goto unlock;
2359156cacb1SAl Viro 	}
23609d412a43SAl Viro 
23619d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
23629d412a43SAl Viro 	err = -EBUSY;
236395bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
23649d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
23659d412a43SAl Viro 		goto unlock;
23669d412a43SAl Viro 
23679d412a43SAl Viro 	err = -EINVAL;
2368e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
23699d412a43SAl Viro 		goto unlock;
23709d412a43SAl Viro 
237195bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
237284d17192SAl Viro 	err = graft_tree(newmnt, parent, mp);
23739d412a43SAl Viro 
23749d412a43SAl Viro unlock:
237584d17192SAl Viro 	unlock_mount(mp);
23769d412a43SAl Viro 	return err;
23779d412a43SAl Viro }
2378b1e75df4SAl Viro 
23798c6cf9ccSEric W. Biederman static bool fs_fully_visible(struct file_system_type *fs_type, int *new_mnt_flags);
23801b852bceSEric W. Biederman 
23811da177e4SLinus Torvalds /*
23821da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
23831da177e4SLinus Torvalds  * namespace's tree
23841da177e4SLinus Torvalds  */
23850c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags,
2386808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
23871da177e4SLinus Torvalds {
23880c55cfc4SEric W. Biederman 	struct file_system_type *type;
23899b40bc90SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
23901da177e4SLinus Torvalds 	struct vfsmount *mnt;
239115f9a3f3SAl Viro 	int err;
23921da177e4SLinus Torvalds 
23930c55cfc4SEric W. Biederman 	if (!fstype)
23941da177e4SLinus Torvalds 		return -EINVAL;
23951da177e4SLinus Torvalds 
23960c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
23970c55cfc4SEric W. Biederman 	if (!type)
23980c55cfc4SEric W. Biederman 		return -ENODEV;
23990c55cfc4SEric W. Biederman 
24000c55cfc4SEric W. Biederman 	if (user_ns != &init_user_ns) {
24010c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_MOUNT)) {
24020c55cfc4SEric W. Biederman 			put_filesystem(type);
24030c55cfc4SEric W. Biederman 			return -EPERM;
24040c55cfc4SEric W. Biederman 		}
24050c55cfc4SEric W. Biederman 		/* Only in special cases allow devices from mounts
24060c55cfc4SEric W. Biederman 		 * created outside the initial user namespace.
24070c55cfc4SEric W. Biederman 		 */
24080c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
24090c55cfc4SEric W. Biederman 			flags |= MS_NODEV;
24109566d674SEric W. Biederman 			mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV;
24110c55cfc4SEric W. Biederman 		}
24121b852bceSEric W. Biederman 		if (type->fs_flags & FS_USERNS_VISIBLE) {
24138c6cf9ccSEric W. Biederman 			if (!fs_fully_visible(type, &mnt_flags))
24141b852bceSEric W. Biederman 				return -EPERM;
24151b852bceSEric W. Biederman 		}
24160c55cfc4SEric W. Biederman 	}
24170c55cfc4SEric W. Biederman 
24180c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, flags, name, data);
24190c55cfc4SEric W. Biederman 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
24200c55cfc4SEric W. Biederman 	    !mnt->mnt_sb->s_subtype)
24210c55cfc4SEric W. Biederman 		mnt = fs_set_subtype(mnt, fstype);
24220c55cfc4SEric W. Biederman 
24230c55cfc4SEric W. Biederman 	put_filesystem(type);
24241da177e4SLinus Torvalds 	if (IS_ERR(mnt))
24251da177e4SLinus Torvalds 		return PTR_ERR(mnt);
24261da177e4SLinus Torvalds 
242795bc5f25SAl Viro 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
242815f9a3f3SAl Viro 	if (err)
242915f9a3f3SAl Viro 		mntput(mnt);
243015f9a3f3SAl Viro 	return err;
24311da177e4SLinus Torvalds }
24321da177e4SLinus Torvalds 
243319a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
243419a167afSAl Viro {
24356776db3dSAl Viro 	struct mount *mnt = real_mount(m);
243619a167afSAl Viro 	int err;
243719a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
243819a167afSAl Viro 	 * expired before we get a chance to add it
243919a167afSAl Viro 	 */
24406776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
244119a167afSAl Viro 
244219a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
244319a167afSAl Viro 	    m->mnt_root == path->dentry) {
2444b1e75df4SAl Viro 		err = -ELOOP;
2445b1e75df4SAl Viro 		goto fail;
244619a167afSAl Viro 	}
244719a167afSAl Viro 
244895bc5f25SAl Viro 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2449b1e75df4SAl Viro 	if (!err)
2450b1e75df4SAl Viro 		return 0;
2451b1e75df4SAl Viro fail:
2452b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
24536776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
245497216be0SAl Viro 		namespace_lock();
24556776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
245697216be0SAl Viro 		namespace_unlock();
245719a167afSAl Viro 	}
2458b1e75df4SAl Viro 	mntput(m);
2459b1e75df4SAl Viro 	mntput(m);
246019a167afSAl Viro 	return err;
246119a167afSAl Viro }
246219a167afSAl Viro 
2463ea5b778aSDavid Howells /**
2464ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2465ea5b778aSDavid Howells  * @mnt: The mount to list.
2466ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2467ea5b778aSDavid Howells  */
2468ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2469ea5b778aSDavid Howells {
247097216be0SAl Viro 	namespace_lock();
2471ea5b778aSDavid Howells 
24726776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2473ea5b778aSDavid Howells 
247497216be0SAl Viro 	namespace_unlock();
2475ea5b778aSDavid Howells }
2476ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2477ea5b778aSDavid Howells 
2478ea5b778aSDavid Howells /*
24791da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
24801da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
24811da177e4SLinus Torvalds  * here
24821da177e4SLinus Torvalds  */
24831da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
24841da177e4SLinus Torvalds {
2485761d5c38SAl Viro 	struct mount *mnt, *next;
24861da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	if (list_empty(mounts))
24891da177e4SLinus Torvalds 		return;
24901da177e4SLinus Torvalds 
249197216be0SAl Viro 	namespace_lock();
2492719ea2fbSAl Viro 	lock_mount_hash();
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
24951da177e4SLinus Torvalds 	 * following criteria:
24961da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
24971da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
24981da177e4SLinus Torvalds 	 *   cleared by mntput())
24991da177e4SLinus Torvalds 	 */
25006776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2501863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
25021ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
25031da177e4SLinus Torvalds 			continue;
25046776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
25051da177e4SLinus Torvalds 	}
2506bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
25076776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2508143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2509e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2510bcc5c7d2SAl Viro 	}
2511719ea2fbSAl Viro 	unlock_mount_hash();
25123ab6abeeSAl Viro 	namespace_unlock();
25131da177e4SLinus Torvalds }
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds /*
25185528f911STrond Myklebust  * Ripoff of 'select_parent()'
25195528f911STrond Myklebust  *
25205528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
25215528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
25225528f911STrond Myklebust  */
2523692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
25245528f911STrond Myklebust {
2525692afc31SAl Viro 	struct mount *this_parent = parent;
25265528f911STrond Myklebust 	struct list_head *next;
25275528f911STrond Myklebust 	int found = 0;
25285528f911STrond Myklebust 
25295528f911STrond Myklebust repeat:
25306b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
25315528f911STrond Myklebust resume:
25326b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
25335528f911STrond Myklebust 		struct list_head *tmp = next;
25346b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
25355528f911STrond Myklebust 
25365528f911STrond Myklebust 		next = tmp->next;
2537692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
25385528f911STrond Myklebust 			continue;
25395528f911STrond Myklebust 		/*
25405528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
25415528f911STrond Myklebust 		 */
25426b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
25435528f911STrond Myklebust 			this_parent = mnt;
25445528f911STrond Myklebust 			goto repeat;
25455528f911STrond Myklebust 		}
25465528f911STrond Myklebust 
25471ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
25486776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
25495528f911STrond Myklebust 			found++;
25505528f911STrond Myklebust 		}
25515528f911STrond Myklebust 	}
25525528f911STrond Myklebust 	/*
25535528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
25545528f911STrond Myklebust 	 */
25555528f911STrond Myklebust 	if (this_parent != parent) {
25566b41d536SAl Viro 		next = this_parent->mnt_child.next;
25570714a533SAl Viro 		this_parent = this_parent->mnt_parent;
25585528f911STrond Myklebust 		goto resume;
25595528f911STrond Myklebust 	}
25605528f911STrond Myklebust 	return found;
25615528f911STrond Myklebust }
25625528f911STrond Myklebust 
25635528f911STrond Myklebust /*
25645528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
25655528f911STrond Myklebust  * submounts of a specific parent mountpoint
256699b7db7bSNick Piggin  *
256748a066e7SAl Viro  * mount_lock must be held for write
25685528f911STrond Myklebust  */
2569b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
25705528f911STrond Myklebust {
25715528f911STrond Myklebust 	LIST_HEAD(graveyard);
2572761d5c38SAl Viro 	struct mount *m;
25735528f911STrond Myklebust 
25745528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2575c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2576bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2577761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
25786776db3dSAl Viro 						mnt_expire);
2579143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
2580e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2581bcc5c7d2SAl Viro 		}
2582bcc5c7d2SAl Viro 	}
25835528f911STrond Myklebust }
25845528f911STrond Myklebust 
25855528f911STrond Myklebust /*
25861da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
25871da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
25881da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
25891da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
25901da177e4SLinus Torvalds  */
2591b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2592b58fed8bSRam Pai 				 unsigned long n)
25931da177e4SLinus Torvalds {
25941da177e4SLinus Torvalds 	char *t = to;
25951da177e4SLinus Torvalds 	const char __user *f = from;
25961da177e4SLinus Torvalds 	char c;
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
25991da177e4SLinus Torvalds 		return n;
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 	while (n) {
26021da177e4SLinus Torvalds 		if (__get_user(c, f)) {
26031da177e4SLinus Torvalds 			memset(t, 0, n);
26041da177e4SLinus Torvalds 			break;
26051da177e4SLinus Torvalds 		}
26061da177e4SLinus Torvalds 		*t++ = c;
26071da177e4SLinus Torvalds 		f++;
26081da177e4SLinus Torvalds 		n--;
26091da177e4SLinus Torvalds 	}
26101da177e4SLinus Torvalds 	return n;
26111da177e4SLinus Torvalds }
26121da177e4SLinus Torvalds 
2613b40ef869SAl Viro void *copy_mount_options(const void __user * data)
26141da177e4SLinus Torvalds {
26151da177e4SLinus Torvalds 	int i;
26161da177e4SLinus Torvalds 	unsigned long size;
2617b40ef869SAl Viro 	char *copy;
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds 	if (!data)
2620b40ef869SAl Viro 		return NULL;
26211da177e4SLinus Torvalds 
2622b40ef869SAl Viro 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2623b40ef869SAl Viro 	if (!copy)
2624b40ef869SAl Viro 		return ERR_PTR(-ENOMEM);
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
26271da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
26281da177e4SLinus Torvalds 	 * the remainder of the page.
26291da177e4SLinus Torvalds 	 */
26301da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
26311da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
26321da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
26331da177e4SLinus Torvalds 		size = PAGE_SIZE;
26341da177e4SLinus Torvalds 
2635b40ef869SAl Viro 	i = size - exact_copy_from_user(copy, data, size);
26361da177e4SLinus Torvalds 	if (!i) {
2637b40ef869SAl Viro 		kfree(copy);
2638b40ef869SAl Viro 		return ERR_PTR(-EFAULT);
26391da177e4SLinus Torvalds 	}
26401da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
2641b40ef869SAl Viro 		memset(copy + i, 0, PAGE_SIZE - i);
2642b40ef869SAl Viro 	return copy;
26431da177e4SLinus Torvalds }
26441da177e4SLinus Torvalds 
2645b8850d1fSTim Gardner char *copy_mount_string(const void __user *data)
2646eca6f534SVegard Nossum {
2647b8850d1fSTim Gardner 	return data ? strndup_user(data, PAGE_SIZE) : NULL;
2648eca6f534SVegard Nossum }
2649eca6f534SVegard Nossum 
26501da177e4SLinus Torvalds /*
26511da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
26521da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
26531da177e4SLinus Torvalds  *
26541da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
26551da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
26561da177e4SLinus Torvalds  * information (or be NULL).
26571da177e4SLinus Torvalds  *
26581da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
26591da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
26601da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
26611da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
26621da177e4SLinus Torvalds  * and must be discarded.
26631da177e4SLinus Torvalds  */
26645e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name,
2665808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
26661da177e4SLinus Torvalds {
26672d92ab3cSAl Viro 	struct path path;
26681da177e4SLinus Torvalds 	int retval = 0;
26691da177e4SLinus Torvalds 	int mnt_flags = 0;
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	/* Discard magic */
26721da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
26731da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
26741da177e4SLinus Torvalds 
26751da177e4SLinus Torvalds 	/* Basic sanity checks */
26761da177e4SLinus Torvalds 	if (data_page)
26771da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
26781da177e4SLinus Torvalds 
2679a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
26805e6123f3SSeunghun Lee 	retval = user_path(dir_name, &path);
2681a27ab9f2STetsuo Handa 	if (retval)
2682a27ab9f2STetsuo Handa 		return retval;
2683a27ab9f2STetsuo Handa 
2684a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2685a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
26860d5cadb8SAl Viro 	if (!retval && !may_mount())
26870d5cadb8SAl Viro 		retval = -EPERM;
26889e8925b6SJeff Layton 	if (!retval && (flags & MS_MANDLOCK) && !may_mandlock())
26899e8925b6SJeff Layton 		retval = -EPERM;
2690a27ab9f2STetsuo Handa 	if (retval)
2691a27ab9f2STetsuo Handa 		goto dput_out;
2692a27ab9f2STetsuo Handa 
2693613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2694613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
26950a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
26960a1c01c9SMatthew Garrett 
26971da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
26981da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
26991da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
27001da177e4SLinus Torvalds 	if (flags & MS_NODEV)
27011da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
27021da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
27031da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2704fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2705fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2706fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2707fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2708d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2709d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
27102e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
27112e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2712fc33a7bbSChristoph Hellwig 
2713ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
2714ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
2715ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2716ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
2717ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
2718ffbc6f0eSEric W. Biederman 		mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2719ffbc6f0eSEric W. Biederman 	}
2720ffbc6f0eSEric W. Biederman 
27217a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2722d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2723d0adde57SMatthew Garrett 		   MS_STRICTATIME);
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
27262d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
27271da177e4SLinus Torvalds 				    data_page);
27281da177e4SLinus Torvalds 	else if (flags & MS_BIND)
27292d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
27309676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
27312d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
27321da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
27332d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
27341da177e4SLinus Torvalds 	else
27352d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
27361da177e4SLinus Torvalds 				      dev_name, data_page);
27371da177e4SLinus Torvalds dput_out:
27382d92ab3cSAl Viro 	path_put(&path);
27391da177e4SLinus Torvalds 	return retval;
27401da177e4SLinus Torvalds }
27411da177e4SLinus Torvalds 
2742771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
2743771b1371SEric W. Biederman {
27446344c433SAl Viro 	ns_free_inum(&ns->ns);
2745771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
2746771b1371SEric W. Biederman 	kfree(ns);
2747771b1371SEric W. Biederman }
2748771b1371SEric W. Biederman 
27498823c079SEric W. Biederman /*
27508823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
27518823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
27528823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
27538823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
27548823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
27558823c079SEric W. Biederman  */
27568823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
27578823c079SEric W. Biederman 
2758771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2759cf8d2c11STrond Myklebust {
2760cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
276198f842e6SEric W. Biederman 	int ret;
2762cf8d2c11STrond Myklebust 
2763cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2764cf8d2c11STrond Myklebust 	if (!new_ns)
2765cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
27666344c433SAl Viro 	ret = ns_alloc_inum(&new_ns->ns);
276798f842e6SEric W. Biederman 	if (ret) {
276898f842e6SEric W. Biederman 		kfree(new_ns);
276998f842e6SEric W. Biederman 		return ERR_PTR(ret);
277098f842e6SEric W. Biederman 	}
277133c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
27728823c079SEric W. Biederman 	new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2773cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2774cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2775cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2776cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2777cf8d2c11STrond Myklebust 	new_ns->event = 0;
2778771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
2779cf8d2c11STrond Myklebust 	return new_ns;
2780cf8d2c11STrond Myklebust }
2781cf8d2c11STrond Myklebust 
27829559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
27839559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
27841da177e4SLinus Torvalds {
27856b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
27867f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2787315fc83eSAl Viro 	struct mount *p, *q;
27889559f689SAl Viro 	struct mount *old;
2789cb338d06SAl Viro 	struct mount *new;
27907a472ef4SEric W. Biederman 	int copy_flags;
27911da177e4SLinus Torvalds 
27929559f689SAl Viro 	BUG_ON(!ns);
27939559f689SAl Viro 
27949559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
27959559f689SAl Viro 		get_mnt_ns(ns);
27969559f689SAl Viro 		return ns;
27979559f689SAl Viro 	}
27989559f689SAl Viro 
27999559f689SAl Viro 	old = ns->root;
28009559f689SAl Viro 
2801771b1371SEric W. Biederman 	new_ns = alloc_mnt_ns(user_ns);
2802cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2803cf8d2c11STrond Myklebust 		return new_ns;
28041da177e4SLinus Torvalds 
280597216be0SAl Viro 	namespace_lock();
28061da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
28074ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
28089559f689SAl Viro 	if (user_ns != ns->user_ns)
2809132c94e3SEric W. Biederman 		copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
28107a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2811be34d1a3SDavid Howells 	if (IS_ERR(new)) {
2812328e6d90SAl Viro 		namespace_unlock();
2813771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
2814be34d1a3SDavid Howells 		return ERR_CAST(new);
28151da177e4SLinus Torvalds 	}
2816be08d6d2SAl Viro 	new_ns->root = new;
28171a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 	/*
28201da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
28211da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
28221da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
28231da177e4SLinus Torvalds 	 */
2824909b0a88SAl Viro 	p = old;
2825cb338d06SAl Viro 	q = new;
28261da177e4SLinus Torvalds 	while (p) {
2827143c8c91SAl Viro 		q->mnt_ns = new_ns;
28289559f689SAl Viro 		if (new_fs) {
28299559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
28309559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
2831315fc83eSAl Viro 				rootmnt = &p->mnt;
28321da177e4SLinus Torvalds 			}
28339559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
28349559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
2835315fc83eSAl Viro 				pwdmnt = &p->mnt;
28361da177e4SLinus Torvalds 			}
28371da177e4SLinus Torvalds 		}
2838909b0a88SAl Viro 		p = next_mnt(p, old);
2839909b0a88SAl Viro 		q = next_mnt(q, new);
28404ce5d2b1SEric W. Biederman 		if (!q)
28414ce5d2b1SEric W. Biederman 			break;
28424ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
28434ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
28441da177e4SLinus Torvalds 	}
2845328e6d90SAl Viro 	namespace_unlock();
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds 	if (rootmnt)
2848f03c6599SAl Viro 		mntput(rootmnt);
28491da177e4SLinus Torvalds 	if (pwdmnt)
2850f03c6599SAl Viro 		mntput(pwdmnt);
28511da177e4SLinus Torvalds 
2852741a2951SJANAK DESAI 	return new_ns;
2853741a2951SJANAK DESAI }
2854741a2951SJANAK DESAI 
2855cf8d2c11STrond Myklebust /**
2856cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2857cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2858cf8d2c11STrond Myklebust  */
28591a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2860cf8d2c11STrond Myklebust {
2861771b1371SEric W. Biederman 	struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2862cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
28631a4eeaf2SAl Viro 		struct mount *mnt = real_mount(m);
28641a4eeaf2SAl Viro 		mnt->mnt_ns = new_ns;
2865be08d6d2SAl Viro 		new_ns->root = mnt;
2866b1983cd8SAl Viro 		list_add(&mnt->mnt_list, &new_ns->list);
2867c1334495SAl Viro 	} else {
28681a4eeaf2SAl Viro 		mntput(m);
2869cf8d2c11STrond Myklebust 	}
2870cf8d2c11STrond Myklebust 	return new_ns;
2871cf8d2c11STrond Myklebust }
2872cf8d2c11STrond Myklebust 
2873ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2874ea441d11SAl Viro {
2875ea441d11SAl Viro 	struct mnt_namespace *ns;
2876d31da0f0SAl Viro 	struct super_block *s;
2877ea441d11SAl Viro 	struct path path;
2878ea441d11SAl Viro 	int err;
2879ea441d11SAl Viro 
2880ea441d11SAl Viro 	ns = create_mnt_ns(mnt);
2881ea441d11SAl Viro 	if (IS_ERR(ns))
2882ea441d11SAl Viro 		return ERR_CAST(ns);
2883ea441d11SAl Viro 
2884ea441d11SAl Viro 	err = vfs_path_lookup(mnt->mnt_root, mnt,
2885ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2886ea441d11SAl Viro 
2887ea441d11SAl Viro 	put_mnt_ns(ns);
2888ea441d11SAl Viro 
2889ea441d11SAl Viro 	if (err)
2890ea441d11SAl Viro 		return ERR_PTR(err);
2891ea441d11SAl Viro 
2892ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
2893d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
2894d31da0f0SAl Viro 	atomic_inc(&s->s_active);
2895ea441d11SAl Viro 	mntput(path.mnt);
2896ea441d11SAl Viro 	/* lock the sucker */
2897d31da0f0SAl Viro 	down_write(&s->s_umount);
2898ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
2899ea441d11SAl Viro 	return path.dentry;
2900ea441d11SAl Viro }
2901ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
2902ea441d11SAl Viro 
2903bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2904bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
29051da177e4SLinus Torvalds {
2906eca6f534SVegard Nossum 	int ret;
2907eca6f534SVegard Nossum 	char *kernel_type;
2908eca6f534SVegard Nossum 	char *kernel_dev;
2909b40ef869SAl Viro 	void *options;
29101da177e4SLinus Torvalds 
2911b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
2912b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
2913b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
2914eca6f534SVegard Nossum 		goto out_type;
29151da177e4SLinus Torvalds 
2916b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
2917b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
2918b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
2919eca6f534SVegard Nossum 		goto out_dev;
29201da177e4SLinus Torvalds 
2921b40ef869SAl Viro 	options = copy_mount_options(data);
2922b40ef869SAl Viro 	ret = PTR_ERR(options);
2923b40ef869SAl Viro 	if (IS_ERR(options))
2924eca6f534SVegard Nossum 		goto out_data;
29251da177e4SLinus Torvalds 
2926b40ef869SAl Viro 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
2927eca6f534SVegard Nossum 
2928b40ef869SAl Viro 	kfree(options);
2929eca6f534SVegard Nossum out_data:
2930eca6f534SVegard Nossum 	kfree(kernel_dev);
2931eca6f534SVegard Nossum out_dev:
2932eca6f534SVegard Nossum 	kfree(kernel_type);
2933eca6f534SVegard Nossum out_type:
2934eca6f534SVegard Nossum 	return ret;
29351da177e4SLinus Torvalds }
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds /*
2938afac7cbaSAl Viro  * Return true if path is reachable from root
2939afac7cbaSAl Viro  *
294048a066e7SAl Viro  * namespace_sem or mount_lock is held
2941afac7cbaSAl Viro  */
2942643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2943afac7cbaSAl Viro 			 const struct path *root)
2944afac7cbaSAl Viro {
2945643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2946a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
29470714a533SAl Viro 		mnt = mnt->mnt_parent;
2948afac7cbaSAl Viro 	}
2949643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2950afac7cbaSAl Viro }
2951afac7cbaSAl Viro 
295225ab4c9bSYaowei Bai bool path_is_under(struct path *path1, struct path *path2)
2953afac7cbaSAl Viro {
295425ab4c9bSYaowei Bai 	bool res;
295548a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
2956643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
295748a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
2958afac7cbaSAl Viro 	return res;
2959afac7cbaSAl Viro }
2960afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
2961afac7cbaSAl Viro 
2962afac7cbaSAl Viro /*
29631da177e4SLinus Torvalds  * pivot_root Semantics:
29641da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
29651da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
29661da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
29671da177e4SLinus Torvalds  *
29681da177e4SLinus Torvalds  * Restrictions:
29691da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
29701da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
29711da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
29721da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
29731da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
29741da177e4SLinus Torvalds  *
29754a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
29764a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
29774a0d11faSNeil Brown  * in this situation.
29784a0d11faSNeil Brown  *
29791da177e4SLinus Torvalds  * Notes:
29801da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
29811da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
29821da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
29831da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
29841da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
29851da177e4SLinus Torvalds  *    first.
29861da177e4SLinus Torvalds  */
29873480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
29883480b257SHeiko Carstens 		const char __user *, put_old)
29891da177e4SLinus Torvalds {
29902d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
299184d17192SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt;
299284d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
29931da177e4SLinus Torvalds 	int error;
29941da177e4SLinus Torvalds 
29959b40bc90SAl Viro 	if (!may_mount())
29961da177e4SLinus Torvalds 		return -EPERM;
29971da177e4SLinus Torvalds 
29982d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
29991da177e4SLinus Torvalds 	if (error)
30001da177e4SLinus Torvalds 		goto out0;
30011da177e4SLinus Torvalds 
30022d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
30031da177e4SLinus Torvalds 	if (error)
30041da177e4SLinus Torvalds 		goto out1;
30051da177e4SLinus Torvalds 
30062d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
3007b12cea91SAl Viro 	if (error)
3008b12cea91SAl Viro 		goto out2;
30091da177e4SLinus Torvalds 
3010f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
301184d17192SAl Viro 	old_mp = lock_mount(&old);
301284d17192SAl Viro 	error = PTR_ERR(old_mp);
301384d17192SAl Viro 	if (IS_ERR(old_mp))
3014b12cea91SAl Viro 		goto out3;
3015b12cea91SAl Viro 
30161da177e4SLinus Torvalds 	error = -EINVAL;
3017419148daSAl Viro 	new_mnt = real_mount(new.mnt);
3018419148daSAl Viro 	root_mnt = real_mount(root.mnt);
301984d17192SAl Viro 	old_mnt = real_mount(old.mnt);
302084d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
3021fc7be130SAl Viro 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
3022fc7be130SAl Viro 		IS_MNT_SHARED(root_mnt->mnt_parent))
3023b12cea91SAl Viro 		goto out4;
3024143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
3025b12cea91SAl Viro 		goto out4;
30265ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
30275ff9d8a6SEric W. Biederman 		goto out4;
30281da177e4SLinus Torvalds 	error = -ENOENT;
3029f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
3030b12cea91SAl Viro 		goto out4;
30311da177e4SLinus Torvalds 	error = -EBUSY;
303284d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
3033b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
30341da177e4SLinus Torvalds 	error = -EINVAL;
30358c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
3036b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3037676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
3038b12cea91SAl Viro 		goto out4; /* not attached */
303984d17192SAl Viro 	root_mp = root_mnt->mnt_mp;
30402d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
3041b12cea91SAl Viro 		goto out4; /* not a mountpoint */
3042676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
3043b12cea91SAl Viro 		goto out4; /* not attached */
30444ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
304584d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
3046b12cea91SAl Viro 		goto out4;
30470d082601SEric W. Biederman 	/* make certain new is below the root */
30480d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
30490d082601SEric W. Biederman 		goto out4;
305084d17192SAl Viro 	root_mp->m_count++; /* pin it so it won't go away */
3051719ea2fbSAl Viro 	lock_mount_hash();
3052419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
3053419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
30545ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
30555ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
30565ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
30575ff9d8a6SEric W. Biederman 	}
30584ac91378SJan Blunck 	/* mount old root on put_old */
305984d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
30604ac91378SJan Blunck 	/* mount new_root on / */
306184d17192SAl Viro 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
30626b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
30634fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
30644fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
3065719ea2fbSAl Viro 	unlock_mount_hash();
30662d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
306784d17192SAl Viro 	put_mountpoint(root_mp);
30681da177e4SLinus Torvalds 	error = 0;
3069b12cea91SAl Viro out4:
307084d17192SAl Viro 	unlock_mount(old_mp);
3071b12cea91SAl Viro 	if (!error) {
30721a390689SAl Viro 		path_put(&root_parent);
30731a390689SAl Viro 		path_put(&parent_path);
3074b12cea91SAl Viro 	}
3075b12cea91SAl Viro out3:
30768c3ee42eSAl Viro 	path_put(&root);
3077b12cea91SAl Viro out2:
30782d8f3038SAl Viro 	path_put(&old);
30791da177e4SLinus Torvalds out1:
30802d8f3038SAl Viro 	path_put(&new);
30811da177e4SLinus Torvalds out0:
30821da177e4SLinus Torvalds 	return error;
30831da177e4SLinus Torvalds }
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds static void __init init_mount_tree(void)
30861da177e4SLinus Torvalds {
30871da177e4SLinus Torvalds 	struct vfsmount *mnt;
30886b3286edSKirill Korotaev 	struct mnt_namespace *ns;
3089ac748a09SJan Blunck 	struct path root;
30900c55cfc4SEric W. Biederman 	struct file_system_type *type;
30911da177e4SLinus Torvalds 
30920c55cfc4SEric W. Biederman 	type = get_fs_type("rootfs");
30930c55cfc4SEric W. Biederman 	if (!type)
30940c55cfc4SEric W. Biederman 		panic("Can't find rootfs type");
30950c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
30960c55cfc4SEric W. Biederman 	put_filesystem(type);
30971da177e4SLinus Torvalds 	if (IS_ERR(mnt))
30981da177e4SLinus Torvalds 		panic("Can't create rootfs");
3099b3e19d92SNick Piggin 
31003b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
31013b22edc5STrond Myklebust 	if (IS_ERR(ns))
31021da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
31031da177e4SLinus Torvalds 
31046b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
31056b3286edSKirill Korotaev 	get_mnt_ns(ns);
31061da177e4SLinus Torvalds 
3107be08d6d2SAl Viro 	root.mnt = mnt;
3108be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
3109da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
3110ac748a09SJan Blunck 
3111ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
3112ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
311574bf17cfSDenis Cheng void __init mnt_init(void)
31161da177e4SLinus Torvalds {
311713f14b4dSEric Dumazet 	unsigned u;
311815a67dd8SRandy Dunlap 	int err;
31191da177e4SLinus Torvalds 
31207d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
312120c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
31221da177e4SLinus Torvalds 
31230818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
312438129a13SAl Viro 				sizeof(struct hlist_head),
31250818bf27SAl Viro 				mhash_entries, 19,
31260818bf27SAl Viro 				0,
31270818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
31280818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
31290818bf27SAl Viro 				sizeof(struct hlist_head),
31300818bf27SAl Viro 				mphash_entries, 19,
31310818bf27SAl Viro 				0,
31320818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
31331da177e4SLinus Torvalds 
313484d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
31351da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
31361da177e4SLinus Torvalds 
31370818bf27SAl Viro 	for (u = 0; u <= m_hash_mask; u++)
313838129a13SAl Viro 		INIT_HLIST_HEAD(&mount_hashtable[u]);
31390818bf27SAl Viro 	for (u = 0; u <= mp_hash_mask; u++)
31400818bf27SAl Viro 		INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
31411da177e4SLinus Torvalds 
31424b93dc9bSTejun Heo 	kernfs_init();
31434b93dc9bSTejun Heo 
314415a67dd8SRandy Dunlap 	err = sysfs_init();
314515a67dd8SRandy Dunlap 	if (err)
314615a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
31478e24eea7SHarvey Harrison 			__func__, err);
314800d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
314900d26666SGreg Kroah-Hartman 	if (!fs_kobj)
31508e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
31511da177e4SLinus Torvalds 	init_rootfs();
31521da177e4SLinus Torvalds 	init_mount_tree();
31531da177e4SLinus Torvalds }
31541da177e4SLinus Torvalds 
3155616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
31561da177e4SLinus Torvalds {
3157d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
3158616511d0STrond Myklebust 		return;
31597b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
3160771b1371SEric W. Biederman 	free_mnt_ns(ns);
31611da177e4SLinus Torvalds }
31629d412a43SAl Viro 
31639d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
31649d412a43SAl Viro {
3165423e0ab0STim Chen 	struct vfsmount *mnt;
3166423e0ab0STim Chen 	mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
3167423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
3168423e0ab0STim Chen 		/*
3169423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
3170423e0ab0STim Chen 		 * we unmount before file sys is unregistered
3171423e0ab0STim Chen 		*/
3172f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
3173423e0ab0STim Chen 	}
3174423e0ab0STim Chen 	return mnt;
31759d412a43SAl Viro }
31769d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data);
3177423e0ab0STim Chen 
3178423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
3179423e0ab0STim Chen {
3180423e0ab0STim Chen 	/* release long term mount so mount point can be released */
3181423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
3182f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
318348a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
3184423e0ab0STim Chen 		mntput(mnt);
3185423e0ab0STim Chen 	}
3186423e0ab0STim Chen }
3187423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
318802125a82SAl Viro 
318902125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
319002125a82SAl Viro {
3191143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
319202125a82SAl Viro }
31938823c079SEric W. Biederman 
31943151527eSEric W. Biederman bool current_chrooted(void)
31953151527eSEric W. Biederman {
31963151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
31973151527eSEric W. Biederman 	struct path ns_root;
31983151527eSEric W. Biederman 	struct path fs_root;
31993151527eSEric W. Biederman 	bool chrooted;
32003151527eSEric W. Biederman 
32013151527eSEric W. Biederman 	/* Find the namespace root */
32023151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
32033151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
32043151527eSEric W. Biederman 	path_get(&ns_root);
32053151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
32063151527eSEric W. Biederman 		;
32073151527eSEric W. Biederman 
32083151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
32093151527eSEric W. Biederman 
32103151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
32113151527eSEric W. Biederman 
32123151527eSEric W. Biederman 	path_put(&fs_root);
32133151527eSEric W. Biederman 	path_put(&ns_root);
32143151527eSEric W. Biederman 
32153151527eSEric W. Biederman 	return chrooted;
32163151527eSEric W. Biederman }
32173151527eSEric W. Biederman 
32188c6cf9ccSEric W. Biederman static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags)
321987a8ebd6SEric W. Biederman {
322087a8ebd6SEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
32218c6cf9ccSEric W. Biederman 	int new_flags = *new_mnt_flags;
322287a8ebd6SEric W. Biederman 	struct mount *mnt;
3223e51db735SEric W. Biederman 	bool visible = false;
322487a8ebd6SEric W. Biederman 
3225e51db735SEric W. Biederman 	if (unlikely(!ns))
3226e51db735SEric W. Biederman 		return false;
3227e51db735SEric W. Biederman 
322844bb4385SAl Viro 	down_read(&namespace_sem);
322987a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
3230e51db735SEric W. Biederman 		struct mount *child;
323177b1a97dSEric W. Biederman 		int mnt_flags;
323277b1a97dSEric W. Biederman 
3233e51db735SEric W. Biederman 		if (mnt->mnt.mnt_sb->s_type != type)
3234e51db735SEric W. Biederman 			continue;
3235e51db735SEric W. Biederman 
32367e96c1b0SEric W. Biederman 		/* This mount is not fully visible if it's root directory
32377e96c1b0SEric W. Biederman 		 * is not the root directory of the filesystem.
32387e96c1b0SEric W. Biederman 		 */
32397e96c1b0SEric W. Biederman 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
32407e96c1b0SEric W. Biederman 			continue;
32417e96c1b0SEric W. Biederman 
324277b1a97dSEric W. Biederman 		/* Read the mount flags and filter out flags that
324377b1a97dSEric W. Biederman 		 * may safely be ignored.
324477b1a97dSEric W. Biederman 		 */
324577b1a97dSEric W. Biederman 		mnt_flags = mnt->mnt.mnt_flags;
324677b1a97dSEric W. Biederman 		if (mnt->mnt.mnt_sb->s_iflags & SB_I_NOEXEC)
324777b1a97dSEric W. Biederman 			mnt_flags &= ~(MNT_LOCK_NOSUID | MNT_LOCK_NOEXEC);
324877b1a97dSEric W. Biederman 
32498c6cf9ccSEric W. Biederman 		/* Verify the mount flags are equal to or more permissive
32508c6cf9ccSEric W. Biederman 		 * than the proposed new mount.
32518c6cf9ccSEric W. Biederman 		 */
325277b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_READONLY) &&
32538c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_READONLY))
32548c6cf9ccSEric W. Biederman 			continue;
325577b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_NODEV) &&
32568c6cf9ccSEric W. Biederman 		    !(new_flags & MNT_NODEV))
32578c6cf9ccSEric W. Biederman 			continue;
325877b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_NOSUID) &&
325977b1a97dSEric W. Biederman 		    !(new_flags & MNT_NOSUID))
326077b1a97dSEric W. Biederman 			continue;
326177b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_NOEXEC) &&
326277b1a97dSEric W. Biederman 		    !(new_flags & MNT_NOEXEC))
326377b1a97dSEric W. Biederman 			continue;
326477b1a97dSEric W. Biederman 		if ((mnt_flags & MNT_LOCK_ATIME) &&
326577b1a97dSEric W. Biederman 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
32668c6cf9ccSEric W. Biederman 			continue;
32678c6cf9ccSEric W. Biederman 
3268ceeb0e5dSEric W. Biederman 		/* This mount is not fully visible if there are any
3269ceeb0e5dSEric W. Biederman 		 * locked child mounts that cover anything except for
3270ceeb0e5dSEric W. Biederman 		 * empty directories.
3271e51db735SEric W. Biederman 		 */
3272e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3273e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
3274ceeb0e5dSEric W. Biederman 			/* Only worry about locked mounts */
327577b1a97dSEric W. Biederman 			if (!(mnt_flags & MNT_LOCKED))
3276ceeb0e5dSEric W. Biederman 				continue;
32777236c85eSEric W. Biederman 			/* Is the directory permanetly empty? */
32787236c85eSEric W. Biederman 			if (!is_empty_dir_inode(inode))
3279e51db735SEric W. Biederman 				goto next;
328087a8ebd6SEric W. Biederman 		}
32818c6cf9ccSEric W. Biederman 		/* Preserve the locked attributes */
328277b1a97dSEric W. Biederman 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
32838c6cf9ccSEric W. Biederman 					       MNT_LOCK_NODEV    | \
328477b1a97dSEric W. Biederman 					       MNT_LOCK_NOSUID   | \
328577b1a97dSEric W. Biederman 					       MNT_LOCK_NOEXEC   | \
32868c6cf9ccSEric W. Biederman 					       MNT_LOCK_ATIME);
3287e51db735SEric W. Biederman 		visible = true;
3288e51db735SEric W. Biederman 		goto found;
3289e51db735SEric W. Biederman 	next:	;
329087a8ebd6SEric W. Biederman 	}
3291e51db735SEric W. Biederman found:
329244bb4385SAl Viro 	up_read(&namespace_sem);
3293e51db735SEric W. Biederman 	return visible;
329487a8ebd6SEric W. Biederman }
329587a8ebd6SEric W. Biederman 
329664964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
32978823c079SEric W. Biederman {
329858be2825SAl Viro 	struct ns_common *ns = NULL;
32998823c079SEric W. Biederman 	struct nsproxy *nsproxy;
33008823c079SEric W. Biederman 
3301728dba3aSEric W. Biederman 	task_lock(task);
3302728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
33038823c079SEric W. Biederman 	if (nsproxy) {
330458be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
330558be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
33068823c079SEric W. Biederman 	}
3307728dba3aSEric W. Biederman 	task_unlock(task);
33088823c079SEric W. Biederman 
33098823c079SEric W. Biederman 	return ns;
33108823c079SEric W. Biederman }
33118823c079SEric W. Biederman 
331264964528SAl Viro static void mntns_put(struct ns_common *ns)
33138823c079SEric W. Biederman {
331458be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
33158823c079SEric W. Biederman }
33168823c079SEric W. Biederman 
331764964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
33188823c079SEric W. Biederman {
33198823c079SEric W. Biederman 	struct fs_struct *fs = current->fs;
332058be2825SAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
33218823c079SEric W. Biederman 	struct path root;
33228823c079SEric W. Biederman 
33230c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3324c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3325c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
3326ae11e0f1SZhao Hongjiang 		return -EPERM;
33278823c079SEric W. Biederman 
33288823c079SEric W. Biederman 	if (fs->users != 1)
33298823c079SEric W. Biederman 		return -EINVAL;
33308823c079SEric W. Biederman 
33318823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
33328823c079SEric W. Biederman 	put_mnt_ns(nsproxy->mnt_ns);
33338823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
33348823c079SEric W. Biederman 
33358823c079SEric W. Biederman 	/* Find the root */
33368823c079SEric W. Biederman 	root.mnt    = &mnt_ns->root->mnt;
33378823c079SEric W. Biederman 	root.dentry = mnt_ns->root->mnt.mnt_root;
33388823c079SEric W. Biederman 	path_get(&root);
33398823c079SEric W. Biederman 	while(d_mountpoint(root.dentry) && follow_down_one(&root))
33408823c079SEric W. Biederman 		;
33418823c079SEric W. Biederman 
33428823c079SEric W. Biederman 	/* Update the pwd and root */
33438823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
33448823c079SEric W. Biederman 	set_fs_root(fs, &root);
33458823c079SEric W. Biederman 
33468823c079SEric W. Biederman 	path_put(&root);
33478823c079SEric W. Biederman 	return 0;
33488823c079SEric W. Biederman }
33498823c079SEric W. Biederman 
33508823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
33518823c079SEric W. Biederman 	.name		= "mnt",
33528823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
33538823c079SEric W. Biederman 	.get		= mntns_get,
33548823c079SEric W. Biederman 	.put		= mntns_put,
33558823c079SEric W. Biederman 	.install	= mntns_install,
33568823c079SEric W. Biederman };
3357