xref: /openbmc/linux/fs/namespace.c (revision 6a46c573)
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 */
59348a066e7SAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
59448a066e7SAl Viro {
59548a066e7SAl Viro 	struct mount *mnt;
59648a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
59748a066e7SAl Viro 		return false;
59848a066e7SAl Viro 	if (bastard == NULL)
59948a066e7SAl Viro 		return true;
60048a066e7SAl Viro 	mnt = real_mount(bastard);
60148a066e7SAl Viro 	mnt_add_count(mnt, 1);
60248a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
60348a066e7SAl Viro 		return true;
60448a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
60548a066e7SAl Viro 		mnt_add_count(mnt, -1);
60648a066e7SAl Viro 		return false;
60748a066e7SAl Viro 	}
60848a066e7SAl Viro 	rcu_read_unlock();
60948a066e7SAl Viro 	mntput(bastard);
61048a066e7SAl Viro 	rcu_read_lock();
61148a066e7SAl Viro 	return false;
61248a066e7SAl Viro }
61348a066e7SAl Viro 
6141da177e4SLinus Torvalds /*
615474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
61648a066e7SAl Viro  * call under rcu_read_lock()
6171da177e4SLinus Torvalds  */
618474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6191da177e4SLinus Torvalds {
62038129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
621474279dcSAl Viro 	struct mount *p;
6221da177e4SLinus Torvalds 
62338129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
624474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
625474279dcSAl Viro 			return p;
626474279dcSAl Viro 	return NULL;
6271da177e4SLinus Torvalds }
628474279dcSAl Viro 
629474279dcSAl Viro /*
630474279dcSAl Viro  * find the last mount at @dentry on vfsmount @mnt.
63148a066e7SAl Viro  * mount_lock must be held.
632474279dcSAl Viro  */
633474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
634474279dcSAl Viro {
635411a938bSEric W. Biederman 	struct mount *p, *res = NULL;
636411a938bSEric W. Biederman 	p = __lookup_mnt(mnt, dentry);
63738129a13SAl Viro 	if (!p)
63838129a13SAl Viro 		goto out;
639411a938bSEric W. Biederman 	if (!(p->mnt.mnt_flags & MNT_UMOUNT))
640411a938bSEric W. Biederman 		res = p;
64138129a13SAl Viro 	hlist_for_each_entry_continue(p, mnt_hash) {
6421d6a32acSAl Viro 		if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
6431d6a32acSAl Viro 			break;
644411a938bSEric W. Biederman 		if (!(p->mnt.mnt_flags & MNT_UMOUNT))
6451d6a32acSAl Viro 			res = p;
6461d6a32acSAl Viro 	}
64738129a13SAl Viro out:
6481d6a32acSAl Viro 	return res;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
651a05964f3SRam Pai /*
652f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
653f015f126SDavid Howells  *
654f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
655f015f126SDavid Howells  * following mounts:
656f015f126SDavid Howells  *
657f015f126SDavid Howells  * mount /dev/sda1 /mnt
658f015f126SDavid Howells  * mount /dev/sda2 /mnt
659f015f126SDavid Howells  * mount /dev/sda3 /mnt
660f015f126SDavid Howells  *
661f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
662f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
663f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
664f015f126SDavid Howells  *
665f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
666a05964f3SRam Pai  */
6671c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
668a05964f3SRam Pai {
669c7105365SAl Viro 	struct mount *child_mnt;
67048a066e7SAl Viro 	struct vfsmount *m;
67148a066e7SAl Viro 	unsigned seq;
67299b7db7bSNick Piggin 
67348a066e7SAl Viro 	rcu_read_lock();
67448a066e7SAl Viro 	do {
67548a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
676474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
67748a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
67848a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
67948a066e7SAl Viro 	rcu_read_unlock();
68048a066e7SAl Viro 	return m;
681a05964f3SRam Pai }
682a05964f3SRam Pai 
6837af1364fSEric W. Biederman /*
6847af1364fSEric W. Biederman  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
6857af1364fSEric W. Biederman  *                         current mount namespace.
6867af1364fSEric W. Biederman  *
6877af1364fSEric W. Biederman  * The common case is dentries are not mountpoints at all and that
6887af1364fSEric W. Biederman  * test is handled inline.  For the slow case when we are actually
6897af1364fSEric W. Biederman  * dealing with a mountpoint of some kind, walk through all of the
6907af1364fSEric W. Biederman  * mounts in the current mount namespace and test to see if the dentry
6917af1364fSEric W. Biederman  * is a mountpoint.
6927af1364fSEric W. Biederman  *
6937af1364fSEric W. Biederman  * The mount_hashtable is not usable in the context because we
6947af1364fSEric W. Biederman  * need to identify all mounts that may be in the current mount
6957af1364fSEric W. Biederman  * namespace not just a mount that happens to have some specified
6967af1364fSEric W. Biederman  * parent mount.
6977af1364fSEric W. Biederman  */
6987af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry)
6997af1364fSEric W. Biederman {
7007af1364fSEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
7017af1364fSEric W. Biederman 	struct mount *mnt;
7027af1364fSEric W. Biederman 	bool is_covered = false;
7037af1364fSEric W. Biederman 
7047af1364fSEric W. Biederman 	if (!d_mountpoint(dentry))
7057af1364fSEric W. Biederman 		goto out;
7067af1364fSEric W. Biederman 
7077af1364fSEric W. Biederman 	down_read(&namespace_sem);
7087af1364fSEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
7097af1364fSEric W. Biederman 		is_covered = (mnt->mnt_mountpoint == dentry);
7107af1364fSEric W. Biederman 		if (is_covered)
7117af1364fSEric W. Biederman 			break;
7127af1364fSEric W. Biederman 	}
7137af1364fSEric W. Biederman 	up_read(&namespace_sem);
7147af1364fSEric W. Biederman out:
7157af1364fSEric W. Biederman 	return is_covered;
7167af1364fSEric W. Biederman }
7177af1364fSEric W. Biederman 
718e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
71984d17192SAl Viro {
7200818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
72184d17192SAl Viro 	struct mountpoint *mp;
72284d17192SAl Viro 
7230818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
72484d17192SAl Viro 		if (mp->m_dentry == dentry) {
72584d17192SAl Viro 			/* might be worth a WARN_ON() */
72684d17192SAl Viro 			if (d_unlinked(dentry))
72784d17192SAl Viro 				return ERR_PTR(-ENOENT);
72884d17192SAl Viro 			mp->m_count++;
72984d17192SAl Viro 			return mp;
73084d17192SAl Viro 		}
73184d17192SAl Viro 	}
732e2dfa935SEric W. Biederman 	return NULL;
733e2dfa935SEric W. Biederman }
734e2dfa935SEric W. Biederman 
735e2dfa935SEric W. Biederman static struct mountpoint *new_mountpoint(struct dentry *dentry)
736e2dfa935SEric W. Biederman {
737e2dfa935SEric W. Biederman 	struct hlist_head *chain = mp_hash(dentry);
738e2dfa935SEric W. Biederman 	struct mountpoint *mp;
739e2dfa935SEric W. Biederman 	int ret;
74084d17192SAl Viro 
74184d17192SAl Viro 	mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
74284d17192SAl Viro 	if (!mp)
74384d17192SAl Viro 		return ERR_PTR(-ENOMEM);
74484d17192SAl Viro 
745eed81007SMiklos Szeredi 	ret = d_set_mounted(dentry);
746eed81007SMiklos Szeredi 	if (ret) {
74784d17192SAl Viro 		kfree(mp);
748eed81007SMiklos Szeredi 		return ERR_PTR(ret);
74984d17192SAl Viro 	}
750eed81007SMiklos Szeredi 
75184d17192SAl Viro 	mp->m_dentry = dentry;
75284d17192SAl Viro 	mp->m_count = 1;
7530818bf27SAl Viro 	hlist_add_head(&mp->m_hash, chain);
7540a5eb7c8SEric W. Biederman 	INIT_HLIST_HEAD(&mp->m_list);
75584d17192SAl Viro 	return mp;
75684d17192SAl Viro }
75784d17192SAl Viro 
75884d17192SAl Viro static void put_mountpoint(struct mountpoint *mp)
75984d17192SAl Viro {
76084d17192SAl Viro 	if (!--mp->m_count) {
76184d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
7620a5eb7c8SEric W. Biederman 		BUG_ON(!hlist_empty(&mp->m_list));
76384d17192SAl Viro 		spin_lock(&dentry->d_lock);
76484d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
76584d17192SAl Viro 		spin_unlock(&dentry->d_lock);
7660818bf27SAl Viro 		hlist_del(&mp->m_hash);
76784d17192SAl Viro 		kfree(mp);
76884d17192SAl Viro 	}
76984d17192SAl Viro }
77084d17192SAl Viro 
771143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
7721da177e4SLinus Torvalds {
7736b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
77699b7db7bSNick Piggin /*
77799b7db7bSNick Piggin  * vfsmount lock must be held for write
77899b7db7bSNick Piggin  */
7796b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
7805addc5ddSAl Viro {
7815addc5ddSAl Viro 	if (ns) {
7825addc5ddSAl Viro 		ns->event = ++event;
7835addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7845addc5ddSAl Viro 	}
7855addc5ddSAl Viro }
7865addc5ddSAl Viro 
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 && ns->event != event) {
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  */
8017bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt)
8021da177e4SLinus Torvalds {
8030714a533SAl Viro 	mnt->mnt_parent = mnt;
804a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8056b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
80638129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
8070a5eb7c8SEric W. Biederman 	hlist_del_init(&mnt->mnt_mp_list);
80884d17192SAl Viro 	put_mountpoint(mnt->mnt_mp);
80984d17192SAl Viro 	mnt->mnt_mp = NULL;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
81299b7db7bSNick Piggin /*
81399b7db7bSNick Piggin  * vfsmount lock must be held for write
81499b7db7bSNick Piggin  */
8157bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path)
8167bdb11deSEric W. Biederman {
8177bdb11deSEric W. Biederman 	old_path->dentry = mnt->mnt_mountpoint;
8187bdb11deSEric W. Biederman 	old_path->mnt = &mnt->mnt_parent->mnt;
8197bdb11deSEric W. Biederman 	unhash_mnt(mnt);
8207bdb11deSEric W. Biederman }
8217bdb11deSEric W. Biederman 
8227bdb11deSEric W. Biederman /*
8237bdb11deSEric W. Biederman  * vfsmount lock must be held for write
8247bdb11deSEric W. Biederman  */
8256a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt)
8266a46c573SEric W. Biederman {
8276a46c573SEric W. Biederman 	/* old mountpoint will be dropped when we can do that */
8286a46c573SEric W. Biederman 	mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
8296a46c573SEric W. Biederman 	unhash_mnt(mnt);
8306a46c573SEric W. Biederman }
8316a46c573SEric W. Biederman 
8326a46c573SEric W. Biederman /*
8336a46c573SEric W. Biederman  * vfsmount lock must be held for write
8346a46c573SEric W. Biederman  */
83584d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
83684d17192SAl Viro 			struct mountpoint *mp,
83744d964d6SAl Viro 			struct mount *child_mnt)
838b90fa9aeSRam Pai {
83984d17192SAl Viro 	mp->m_count++;
8403a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
84184d17192SAl Viro 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
8423a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
84384d17192SAl Viro 	child_mnt->mnt_mp = mp;
8440a5eb7c8SEric W. Biederman 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
845b90fa9aeSRam Pai }
846b90fa9aeSRam Pai 
84799b7db7bSNick Piggin /*
84899b7db7bSNick Piggin  * vfsmount lock must be held for write
84999b7db7bSNick Piggin  */
85084d17192SAl Viro static void attach_mnt(struct mount *mnt,
85184d17192SAl Viro 			struct mount *parent,
85284d17192SAl Viro 			struct mountpoint *mp)
8531da177e4SLinus Torvalds {
85484d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
85538129a13SAl Viro 	hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
85684d17192SAl Viro 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
857b90fa9aeSRam Pai }
858b90fa9aeSRam Pai 
85912a5b529SAl Viro static void attach_shadowed(struct mount *mnt,
86012a5b529SAl Viro 			struct mount *parent,
86112a5b529SAl Viro 			struct mount *shadows)
86212a5b529SAl Viro {
86312a5b529SAl Viro 	if (shadows) {
864f6f99332SLinus Torvalds 		hlist_add_behind_rcu(&mnt->mnt_hash, &shadows->mnt_hash);
86512a5b529SAl Viro 		list_add(&mnt->mnt_child, &shadows->mnt_child);
86612a5b529SAl Viro 	} else {
86712a5b529SAl Viro 		hlist_add_head_rcu(&mnt->mnt_hash,
86812a5b529SAl Viro 				m_hash(&parent->mnt, mnt->mnt_mountpoint));
86912a5b529SAl Viro 		list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
87012a5b529SAl Viro 	}
87112a5b529SAl Viro }
87212a5b529SAl Viro 
873b90fa9aeSRam Pai /*
87499b7db7bSNick Piggin  * vfsmount lock must be held for write
875b90fa9aeSRam Pai  */
8761d6a32acSAl Viro static void commit_tree(struct mount *mnt, struct mount *shadows)
877b90fa9aeSRam Pai {
8780714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
87983adc753SAl Viro 	struct mount *m;
880b90fa9aeSRam Pai 	LIST_HEAD(head);
881143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
882b90fa9aeSRam Pai 
8830714a533SAl Viro 	BUG_ON(parent == mnt);
884b90fa9aeSRam Pai 
8851a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
886f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
887143c8c91SAl Viro 		m->mnt_ns = n;
888f03c6599SAl Viro 
889b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
890b90fa9aeSRam Pai 
89112a5b529SAl Viro 	attach_shadowed(mnt, parent, shadows);
8926b3286edSKirill Korotaev 	touch_mnt_namespace(n);
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
895909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
8961da177e4SLinus Torvalds {
8976b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
8986b41d536SAl Viro 	if (next == &p->mnt_mounts) {
8991da177e4SLinus Torvalds 		while (1) {
900909b0a88SAl Viro 			if (p == root)
9011da177e4SLinus Torvalds 				return NULL;
9026b41d536SAl Viro 			next = p->mnt_child.next;
9036b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
9041da177e4SLinus Torvalds 				break;
9050714a533SAl Viro 			p = p->mnt_parent;
9061da177e4SLinus Torvalds 		}
9071da177e4SLinus Torvalds 	}
9086b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
911315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
9129676f0c6SRam Pai {
9136b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
9146b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
9156b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
9166b41d536SAl Viro 		prev = p->mnt_mounts.prev;
9179676f0c6SRam Pai 	}
9189676f0c6SRam Pai 	return p;
9199676f0c6SRam Pai }
9209676f0c6SRam Pai 
9219d412a43SAl Viro struct vfsmount *
9229d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
9239d412a43SAl Viro {
924b105e270SAl Viro 	struct mount *mnt;
9259d412a43SAl Viro 	struct dentry *root;
9269d412a43SAl Viro 
9279d412a43SAl Viro 	if (!type)
9289d412a43SAl Viro 		return ERR_PTR(-ENODEV);
9299d412a43SAl Viro 
9309d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
9319d412a43SAl Viro 	if (!mnt)
9329d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
9339d412a43SAl Viro 
9349d412a43SAl Viro 	if (flags & MS_KERNMOUNT)
935b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
9369d412a43SAl Viro 
9379d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
9389d412a43SAl Viro 	if (IS_ERR(root)) {
9398ffcb32eSDavid Howells 		mnt_free_id(mnt);
9409d412a43SAl Viro 		free_vfsmnt(mnt);
9419d412a43SAl Viro 		return ERR_CAST(root);
9429d412a43SAl Viro 	}
9439d412a43SAl Viro 
944b105e270SAl Viro 	mnt->mnt.mnt_root = root;
945b105e270SAl Viro 	mnt->mnt.mnt_sb = root->d_sb;
946a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
9470714a533SAl Viro 	mnt->mnt_parent = mnt;
948719ea2fbSAl Viro 	lock_mount_hash();
94939f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
950719ea2fbSAl Viro 	unlock_mount_hash();
951b105e270SAl Viro 	return &mnt->mnt;
9529d412a43SAl Viro }
9539d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
9549d412a43SAl Viro 
95587129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
95636341f64SRam Pai 					int flag)
9571da177e4SLinus Torvalds {
95887129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
959be34d1a3SDavid Howells 	struct mount *mnt;
960be34d1a3SDavid Howells 	int err;
9611da177e4SLinus Torvalds 
962be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
963be34d1a3SDavid Howells 	if (!mnt)
964be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
965be34d1a3SDavid Howells 
9667a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
96715169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
968719f5d7fSMiklos Szeredi 	else
96915169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
970719f5d7fSMiklos Szeredi 
97115169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
972be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
973719f5d7fSMiklos Szeredi 		if (err)
974719f5d7fSMiklos Szeredi 			goto out_free;
975719f5d7fSMiklos Szeredi 	}
976719f5d7fSMiklos Szeredi 
977f2ebb3a9SAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
978132c94e3SEric W. Biederman 	/* Don't allow unprivileged users to change mount flags */
9799566d674SEric W. Biederman 	if (flag & CL_UNPRIVILEGED) {
9809566d674SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
9819566d674SEric W. Biederman 
9829566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_READONLY)
983132c94e3SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
984132c94e3SEric W. Biederman 
9859566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NODEV)
9869566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
9879566d674SEric W. Biederman 
9889566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NOSUID)
9899566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
9909566d674SEric W. Biederman 
9919566d674SEric W. Biederman 		if (mnt->mnt.mnt_flags & MNT_NOEXEC)
9929566d674SEric W. Biederman 			mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
9939566d674SEric W. Biederman 	}
9949566d674SEric W. Biederman 
9955ff9d8a6SEric W. Biederman 	/* Don't allow unprivileged users to reveal what is under a mount */
996381cacb1SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) &&
997381cacb1SEric W. Biederman 	    (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
9985ff9d8a6SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCKED;
9995ff9d8a6SEric W. Biederman 
10001da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
1001b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
1002b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
1003a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
10040714a533SAl Viro 	mnt->mnt_parent = mnt;
1005719ea2fbSAl Viro 	lock_mount_hash();
100639f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1007719ea2fbSAl Viro 	unlock_mount_hash();
1008b90fa9aeSRam Pai 
10097a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
10107a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
10116776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
101232301920SAl Viro 		mnt->mnt_master = old;
1013fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
10148aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
1015fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
10166776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
1017d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
10186776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1019d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
10205afe0022SRam Pai 	}
1021b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
10220f0afb1dSAl Viro 		set_mnt_shared(mnt);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
10251da177e4SLinus Torvalds 	 * as the original if that was on one */
102636341f64SRam Pai 	if (flag & CL_EXPIRE) {
10276776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
10286776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
10291da177e4SLinus Torvalds 	}
1030be34d1a3SDavid Howells 
1031cb338d06SAl Viro 	return mnt;
1032719f5d7fSMiklos Szeredi 
1033719f5d7fSMiklos Szeredi  out_free:
10348ffcb32eSDavid Howells 	mnt_free_id(mnt);
1035719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
1036be34d1a3SDavid Howells 	return ERR_PTR(err);
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10399ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt)
10409ea459e1SAl Viro {
10419ea459e1SAl Viro 	/*
10429ea459e1SAl Viro 	 * This probably indicates that somebody messed
10439ea459e1SAl Viro 	 * up a mnt_want/drop_write() pair.  If this
10449ea459e1SAl Viro 	 * happens, the filesystem was probably unable
10459ea459e1SAl Viro 	 * to make r/w->r/o transitions.
10469ea459e1SAl Viro 	 */
10479ea459e1SAl Viro 	/*
10489ea459e1SAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
10499ea459e1SAl Viro 	 * so mnt_get_writers() below is safe.
10509ea459e1SAl Viro 	 */
10519ea459e1SAl Viro 	WARN_ON(mnt_get_writers(mnt));
10529ea459e1SAl Viro 	if (unlikely(mnt->mnt_pins.first))
10539ea459e1SAl Viro 		mnt_pin_kill(mnt);
10549ea459e1SAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
10559ea459e1SAl Viro 	dput(mnt->mnt.mnt_root);
10569ea459e1SAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
10579ea459e1SAl Viro 	mnt_free_id(mnt);
10589ea459e1SAl Viro 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
10599ea459e1SAl Viro }
10609ea459e1SAl Viro 
10619ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head)
10629ea459e1SAl Viro {
10639ea459e1SAl Viro 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
10649ea459e1SAl Viro }
10659ea459e1SAl Viro 
10669ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list);
10679ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused)
10689ea459e1SAl Viro {
10699ea459e1SAl Viro 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
10709ea459e1SAl Viro 	struct llist_node *next;
10719ea459e1SAl Viro 
10729ea459e1SAl Viro 	for (; node; node = next) {
10739ea459e1SAl Viro 		next = llist_next(node);
10749ea459e1SAl Viro 		cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
10759ea459e1SAl Viro 	}
10769ea459e1SAl Viro }
10779ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
10789ea459e1SAl Viro 
1079900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
10807b7b1aceSAl Viro {
108148a066e7SAl Viro 	rcu_read_lock();
1082aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
108348a066e7SAl Viro 	if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
108448a066e7SAl Viro 		rcu_read_unlock();
108599b7db7bSNick Piggin 		return;
1086b3e19d92SNick Piggin 	}
1087719ea2fbSAl Viro 	lock_mount_hash();
1088b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
108948a066e7SAl Viro 		rcu_read_unlock();
1090719ea2fbSAl Viro 		unlock_mount_hash();
109199b7db7bSNick Piggin 		return;
109299b7db7bSNick Piggin 	}
109348a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
109448a066e7SAl Viro 		rcu_read_unlock();
109548a066e7SAl Viro 		unlock_mount_hash();
109648a066e7SAl Viro 		return;
109748a066e7SAl Viro 	}
109848a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
109948a066e7SAl Viro 	rcu_read_unlock();
1100962830dfSAndi Kleen 
110139f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
1102719ea2fbSAl Viro 	unlock_mount_hash();
1103649a795aSAl Viro 
11049ea459e1SAl Viro 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
11059ea459e1SAl Viro 		struct task_struct *task = current;
11069ea459e1SAl Viro 		if (likely(!(task->flags & PF_KTHREAD))) {
11079ea459e1SAl Viro 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
11089ea459e1SAl Viro 			if (!task_work_add(task, &mnt->mnt_rcu, true))
11099ea459e1SAl Viro 				return;
11109ea459e1SAl Viro 		}
11119ea459e1SAl Viro 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
11129ea459e1SAl Viro 			schedule_delayed_work(&delayed_mntput_work, 1);
11139ea459e1SAl Viro 		return;
11149ea459e1SAl Viro 	}
11159ea459e1SAl Viro 	cleanup_mnt(mnt);
1116b3e19d92SNick Piggin }
1117b3e19d92SNick Piggin 
1118b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
1119b3e19d92SNick Piggin {
1120b3e19d92SNick Piggin 	if (mnt) {
1121863d684fSAl Viro 		struct mount *m = real_mount(mnt);
1122b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1123863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
1124863d684fSAl Viro 			m->mnt_expiry_mark = 0;
1125863d684fSAl Viro 		mntput_no_expire(m);
1126b3e19d92SNick Piggin 	}
1127b3e19d92SNick Piggin }
1128b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1129b3e19d92SNick Piggin 
1130b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1131b3e19d92SNick Piggin {
1132b3e19d92SNick Piggin 	if (mnt)
113383adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1134b3e19d92SNick Piggin 	return mnt;
1135b3e19d92SNick Piggin }
1136b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1137b3e19d92SNick Piggin 
11383064c356SAl Viro struct vfsmount *mnt_clone_internal(struct path *path)
11397b7b1aceSAl Viro {
11403064c356SAl Viro 	struct mount *p;
11413064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
11423064c356SAl Viro 	if (IS_ERR(p))
11433064c356SAl Viro 		return ERR_CAST(p);
11443064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
11453064c356SAl Viro 	return &p->mnt;
11467b7b1aceSAl Viro }
11471da177e4SLinus Torvalds 
1148b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
1149b3b304a2SMiklos Szeredi {
1150b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
1151b3b304a2SMiklos Szeredi }
1152b3b304a2SMiklos Szeredi 
1153b3b304a2SMiklos Szeredi /*
1154b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
1155b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
1156b3b304a2SMiklos Szeredi  *
1157b3b304a2SMiklos Szeredi  * See also save_mount_options().
1158b3b304a2SMiklos Szeredi  */
115934c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root)
1160b3b304a2SMiklos Szeredi {
11612a32cebdSAl Viro 	const char *options;
11622a32cebdSAl Viro 
11632a32cebdSAl Viro 	rcu_read_lock();
116434c80b1dSAl Viro 	options = rcu_dereference(root->d_sb->s_options);
1165b3b304a2SMiklos Szeredi 
1166b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
1167b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
1168b3b304a2SMiklos Szeredi 		mangle(m, options);
1169b3b304a2SMiklos Szeredi 	}
11702a32cebdSAl Viro 	rcu_read_unlock();
1171b3b304a2SMiklos Szeredi 
1172b3b304a2SMiklos Szeredi 	return 0;
1173b3b304a2SMiklos Szeredi }
1174b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
1175b3b304a2SMiklos Szeredi 
1176b3b304a2SMiklos Szeredi /*
1177b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
1178b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
1179b3b304a2SMiklos Szeredi  *
1180b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
1181b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
1182b3b304a2SMiklos Szeredi  * remount fails.
1183b3b304a2SMiklos Szeredi  *
1184b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
1185b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
1186b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
1187b3b304a2SMiklos Szeredi  * any more.
1188b3b304a2SMiklos Szeredi  */
1189b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
1190b3b304a2SMiklos Szeredi {
11912a32cebdSAl Viro 	BUG_ON(sb->s_options);
11922a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1193b3b304a2SMiklos Szeredi }
1194b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
1195b3b304a2SMiklos Szeredi 
11962a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
11972a32cebdSAl Viro {
11982a32cebdSAl Viro 	char *old = sb->s_options;
11992a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
12002a32cebdSAl Viro 	if (old) {
12012a32cebdSAl Viro 		synchronize_rcu();
12022a32cebdSAl Viro 		kfree(old);
12032a32cebdSAl Viro 	}
12042a32cebdSAl Viro }
12052a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
12062a32cebdSAl Viro 
1207a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
12080226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
12091da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
12101da177e4SLinus Torvalds {
12116ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
12121da177e4SLinus Torvalds 
1213390c6843SRam Pai 	down_read(&namespace_sem);
1214c7999c36SAl Viro 	if (p->cached_event == p->ns->event) {
1215c7999c36SAl Viro 		void *v = p->cached_mount;
1216c7999c36SAl Viro 		if (*pos == p->cached_index)
1217c7999c36SAl Viro 			return v;
1218c7999c36SAl Viro 		if (*pos == p->cached_index + 1) {
1219c7999c36SAl Viro 			v = seq_list_next(v, &p->ns->list, &p->cached_index);
1220c7999c36SAl Viro 			return p->cached_mount = v;
1221c7999c36SAl Viro 		}
1222c7999c36SAl Viro 	}
1223c7999c36SAl Viro 
1224c7999c36SAl Viro 	p->cached_event = p->ns->event;
1225c7999c36SAl Viro 	p->cached_mount = seq_list_start(&p->ns->list, *pos);
1226c7999c36SAl Viro 	p->cached_index = *pos;
1227c7999c36SAl Viro 	return p->cached_mount;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
12311da177e4SLinus Torvalds {
12326ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
1233b0765fb8SPavel Emelianov 
1234c7999c36SAl Viro 	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1235c7999c36SAl Viro 	p->cached_index = *pos;
1236c7999c36SAl Viro 	return p->cached_mount;
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
12401da177e4SLinus Torvalds {
1241390c6843SRam Pai 	up_read(&namespace_sem);
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12440226f492SAl Viro static int m_show(struct seq_file *m, void *v)
12459f5596afSAl Viro {
12466ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
12471a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
12480226f492SAl Viro 	return p->show(m, &r->mnt);
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
1251a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
12521da177e4SLinus Torvalds 	.start	= m_start,
12531da177e4SLinus Torvalds 	.next	= m_next,
12541da177e4SLinus Torvalds 	.stop	= m_stop,
12550226f492SAl Viro 	.show	= m_show,
1256b4629fe2SChuck Lever };
1257a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1258b4629fe2SChuck Lever 
12591da177e4SLinus Torvalds /**
12601da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
12611da177e4SLinus Torvalds  * @mnt: root of mount tree
12621da177e4SLinus Torvalds  *
12631da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
12641da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
12651da177e4SLinus Torvalds  * busy.
12661da177e4SLinus Torvalds  */
1267909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
12681da177e4SLinus Torvalds {
1269909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
127036341f64SRam Pai 	int actual_refs = 0;
127136341f64SRam Pai 	int minimum_refs = 0;
1272315fc83eSAl Viro 	struct mount *p;
1273909b0a88SAl Viro 	BUG_ON(!m);
12741da177e4SLinus Torvalds 
1275b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1276719ea2fbSAl Viro 	lock_mount_hash();
1277909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
127883adc753SAl Viro 		actual_refs += mnt_get_count(p);
12791da177e4SLinus Torvalds 		minimum_refs += 2;
12801da177e4SLinus Torvalds 	}
1281719ea2fbSAl Viro 	unlock_mount_hash();
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
12841da177e4SLinus Torvalds 		return 0;
1285e3474a8eSIan Kent 
1286e3474a8eSIan Kent 	return 1;
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds /**
12921da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
12931da177e4SLinus Torvalds  * @mnt: root of mount
12941da177e4SLinus Torvalds  *
12951da177e4SLinus Torvalds  * This is called to check if a mount point has any
12961da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
12971da177e4SLinus Torvalds  * mount has sub mounts this will return busy
12981da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
12991da177e4SLinus Torvalds  *
13001da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
13011da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
13021da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
13031da177e4SLinus Torvalds  */
13041da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
13051da177e4SLinus Torvalds {
1306e3474a8eSIan Kent 	int ret = 1;
13078ad08d8aSAl Viro 	down_read(&namespace_sem);
1308719ea2fbSAl Viro 	lock_mount_hash();
13091ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1310e3474a8eSIan Kent 		ret = 0;
1311719ea2fbSAl Viro 	unlock_mount_hash();
13128ad08d8aSAl Viro 	up_read(&namespace_sem);
1313a05964f3SRam Pai 	return ret;
13141da177e4SLinus Torvalds }
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
13171da177e4SLinus Torvalds 
131838129a13SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
1319e3197d83SAl Viro 
132097216be0SAl Viro static void namespace_unlock(void)
13211da177e4SLinus Torvalds {
1322a3b3c562SEric W. Biederman 	struct hlist_head head;
132397216be0SAl Viro 
1324a3b3c562SEric W. Biederman 	hlist_move_list(&unmounted, &head);
1325a3b3c562SEric W. Biederman 
132697216be0SAl Viro 	up_write(&namespace_sem);
1327a3b3c562SEric W. Biederman 
1328a3b3c562SEric W. Biederman 	if (likely(hlist_empty(&head)))
132997216be0SAl Viro 		return;
133097216be0SAl Viro 
133148a066e7SAl Viro 	synchronize_rcu();
133248a066e7SAl Viro 
133387b95ce0SAl Viro 	group_pin_kill(&head);
133470fbcdf4SRam Pai }
133570fbcdf4SRam Pai 
133697216be0SAl Viro static inline void namespace_lock(void)
1337e3197d83SAl Viro {
133897216be0SAl Viro 	down_write(&namespace_sem);
1339e3197d83SAl Viro }
1340e3197d83SAl Viro 
1341e819f152SEric W. Biederman enum umount_tree_flags {
1342e819f152SEric W. Biederman 	UMOUNT_SYNC = 1,
1343e819f152SEric W. Biederman 	UMOUNT_PROPAGATE = 2,
1344e819f152SEric W. Biederman };
134599b7db7bSNick Piggin /*
134648a066e7SAl Viro  * mount_lock must be held
134799b7db7bSNick Piggin  * namespace_sem must be held for write
134899b7db7bSNick Piggin  */
1349e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
135070fbcdf4SRam Pai {
1351c003b26fSEric W. Biederman 	LIST_HEAD(tmp_list);
1352315fc83eSAl Viro 	struct mount *p;
135370fbcdf4SRam Pai 
13545d88457eSEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
13555d88457eSEric W. Biederman 		propagate_mount_unlock(mnt);
13565d88457eSEric W. Biederman 
1357c003b26fSEric W. Biederman 	/* Gather the mounts to umount */
1358590ce4bcSEric W. Biederman 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1359590ce4bcSEric W. Biederman 		p->mnt.mnt_flags |= MNT_UMOUNT;
1360c003b26fSEric W. Biederman 		list_move(&p->mnt_list, &tmp_list);
1361590ce4bcSEric W. Biederman 	}
1362c003b26fSEric W. Biederman 
1363411a938bSEric W. Biederman 	/* Hide the mounts from mnt_mounts */
1364c003b26fSEric W. Biederman 	list_for_each_entry(p, &tmp_list, mnt_list) {
1365c003b26fSEric W. Biederman 		list_del_init(&p->mnt_child);
136638129a13SAl Viro 	}
136770fbcdf4SRam Pai 
1368c003b26fSEric W. Biederman 	/* Add propogated mounts to the tmp_list */
1369e819f152SEric W. Biederman 	if (how & UMOUNT_PROPAGATE)
13707b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1371a05964f3SRam Pai 
1372c003b26fSEric W. Biederman 	while (!list_empty(&tmp_list)) {
1373c003b26fSEric W. Biederman 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
13746776db3dSAl Viro 		list_del_init(&p->mnt_expire);
13751a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1376143c8c91SAl Viro 		__touch_mnt_namespace(p->mnt_ns);
137763d37a84SAl Viro 		p->mnt_ns = NULL;
1378e819f152SEric W. Biederman 		if (how & UMOUNT_SYNC)
137948a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
138087b95ce0SAl Viro 
138187b95ce0SAl Viro 		pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt, &unmounted);
1382676da58dSAl Viro 		if (mnt_has_parent(p)) {
138381b6b061SAl Viro 			mnt_add_count(p->mnt_parent, -1);
13846a46c573SEric W. Biederman 			umount_mnt(p);
13857c4b93d8SAl Viro 		}
13860f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
138738129a13SAl Viro 	}
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds 
1390b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1391c35038beSAl Viro 
13921ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
13931da177e4SLinus Torvalds {
13941ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
13951da177e4SLinus Torvalds 	int retval;
13961da177e4SLinus Torvalds 
13971ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
13981da177e4SLinus Torvalds 	if (retval)
13991da177e4SLinus Torvalds 		return retval;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	/*
14021da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
14031da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
14041da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
14051da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
14061da177e4SLinus Torvalds 	 */
14071da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
14081ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
14091da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
14101da177e4SLinus Torvalds 			return -EINVAL;
14111da177e4SLinus Torvalds 
1412b3e19d92SNick Piggin 		/*
1413b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1414b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1415b3e19d92SNick Piggin 		 */
1416719ea2fbSAl Viro 		lock_mount_hash();
141783adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1418719ea2fbSAl Viro 			unlock_mount_hash();
14191da177e4SLinus Torvalds 			return -EBUSY;
1420b3e19d92SNick Piggin 		}
1421719ea2fbSAl Viro 		unlock_mount_hash();
14221da177e4SLinus Torvalds 
1423863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
14241da177e4SLinus Torvalds 			return -EAGAIN;
14251da177e4SLinus Torvalds 	}
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 	/*
14281da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
14291da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
14301da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
14311da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
14321da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
14331da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
14341da177e4SLinus Torvalds 	 * about for the moment.
14351da177e4SLinus Torvalds 	 */
14361da177e4SLinus Torvalds 
143742faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
143842faad99SAl Viro 		sb->s_op->umount_begin(sb);
143942faad99SAl Viro 	}
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	/*
14421da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
14431da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
14441da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
14451da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
14461da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
14471da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
14481da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
14491da177e4SLinus Torvalds 	 */
14501ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
14511da177e4SLinus Torvalds 		/*
14521da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
14531da177e4SLinus Torvalds 		 * we just try to remount it readonly.
14541da177e4SLinus Torvalds 		 */
1455a1480dccSAndy Lutomirski 		if (!capable(CAP_SYS_ADMIN))
1456a1480dccSAndy Lutomirski 			return -EPERM;
14571da177e4SLinus Torvalds 		down_write(&sb->s_umount);
14584aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
14591da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
14601da177e4SLinus Torvalds 		up_write(&sb->s_umount);
14611da177e4SLinus Torvalds 		return retval;
14621da177e4SLinus Torvalds 	}
14631da177e4SLinus Torvalds 
146497216be0SAl Viro 	namespace_lock();
1465719ea2fbSAl Viro 	lock_mount_hash();
14665addc5ddSAl Viro 	event++;
14671da177e4SLinus Torvalds 
146848a066e7SAl Viro 	if (flags & MNT_DETACH) {
146948a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
1470e819f152SEric W. Biederman 			umount_tree(mnt, UMOUNT_PROPAGATE);
147148a066e7SAl Viro 		retval = 0;
147248a066e7SAl Viro 	} else {
1473b54b9be7SAl Viro 		shrink_submounts(mnt);
14741da177e4SLinus Torvalds 		retval = -EBUSY;
147548a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
14761a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1477e819f152SEric W. Biederman 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
14781da177e4SLinus Torvalds 			retval = 0;
14791da177e4SLinus Torvalds 		}
148048a066e7SAl Viro 	}
1481719ea2fbSAl Viro 	unlock_mount_hash();
1482e3197d83SAl Viro 	namespace_unlock();
14831da177e4SLinus Torvalds 	return retval;
14841da177e4SLinus Torvalds }
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds /*
148780b5dce8SEric W. Biederman  * __detach_mounts - lazily unmount all mounts on the specified dentry
148880b5dce8SEric W. Biederman  *
148980b5dce8SEric W. Biederman  * During unlink, rmdir, and d_drop it is possible to loose the path
149080b5dce8SEric W. Biederman  * to an existing mountpoint, and wind up leaking the mount.
149180b5dce8SEric W. Biederman  * detach_mounts allows lazily unmounting those mounts instead of
149280b5dce8SEric W. Biederman  * leaking them.
149380b5dce8SEric W. Biederman  *
149480b5dce8SEric W. Biederman  * The caller may hold dentry->d_inode->i_mutex.
149580b5dce8SEric W. Biederman  */
149680b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry)
149780b5dce8SEric W. Biederman {
149880b5dce8SEric W. Biederman 	struct mountpoint *mp;
149980b5dce8SEric W. Biederman 	struct mount *mnt;
150080b5dce8SEric W. Biederman 
150180b5dce8SEric W. Biederman 	namespace_lock();
150280b5dce8SEric W. Biederman 	mp = lookup_mountpoint(dentry);
150380b5dce8SEric W. Biederman 	if (!mp)
150480b5dce8SEric W. Biederman 		goto out_unlock;
150580b5dce8SEric W. Biederman 
150680b5dce8SEric W. Biederman 	lock_mount_hash();
150780b5dce8SEric W. Biederman 	while (!hlist_empty(&mp->m_list)) {
150880b5dce8SEric W. Biederman 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
15098318e667SEric W. Biederman 		umount_tree(mnt, 0);
151080b5dce8SEric W. Biederman 	}
151180b5dce8SEric W. Biederman 	unlock_mount_hash();
151280b5dce8SEric W. Biederman 	put_mountpoint(mp);
151380b5dce8SEric W. Biederman out_unlock:
151480b5dce8SEric W. Biederman 	namespace_unlock();
151580b5dce8SEric W. Biederman }
151680b5dce8SEric W. Biederman 
151780b5dce8SEric W. Biederman /*
15189b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
15199b40bc90SAl Viro  */
15209b40bc90SAl Viro static inline bool may_mount(void)
15219b40bc90SAl Viro {
15229b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
15239b40bc90SAl Viro }
15249b40bc90SAl Viro 
15259b40bc90SAl Viro /*
15261da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
15271da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
15281da177e4SLinus Torvalds  *
15291da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
15301da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
15311da177e4SLinus Torvalds  */
15321da177e4SLinus Torvalds 
1533bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
15341da177e4SLinus Torvalds {
15352d8f3038SAl Viro 	struct path path;
1536900148dcSAl Viro 	struct mount *mnt;
15371da177e4SLinus Torvalds 	int retval;
1538db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
15391da177e4SLinus Torvalds 
1540db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1541db1f05bbSMiklos Szeredi 		return -EINVAL;
1542db1f05bbSMiklos Szeredi 
15439b40bc90SAl Viro 	if (!may_mount())
15449b40bc90SAl Viro 		return -EPERM;
15459b40bc90SAl Viro 
1546db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1547db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1548db1f05bbSMiklos Szeredi 
1549197df04cSAl Viro 	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
15501da177e4SLinus Torvalds 	if (retval)
15511da177e4SLinus Torvalds 		goto out;
1552900148dcSAl Viro 	mnt = real_mount(path.mnt);
15531da177e4SLinus Torvalds 	retval = -EINVAL;
15542d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
15551da177e4SLinus Torvalds 		goto dput_and_out;
1556143c8c91SAl Viro 	if (!check_mnt(mnt))
15571da177e4SLinus Torvalds 		goto dput_and_out;
15585ff9d8a6SEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
15595ff9d8a6SEric W. Biederman 		goto dput_and_out;
1560b2f5d4dcSEric W. Biederman 	retval = -EPERM;
1561b2f5d4dcSEric W. Biederman 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1562b2f5d4dcSEric W. Biederman 		goto dput_and_out;
15631da177e4SLinus Torvalds 
1564900148dcSAl Viro 	retval = do_umount(mnt, flags);
15651da177e4SLinus Torvalds dput_and_out:
1566429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
15672d8f3038SAl Viro 	dput(path.dentry);
1568900148dcSAl Viro 	mntput_no_expire(mnt);
15691da177e4SLinus Torvalds out:
15701da177e4SLinus Torvalds 	return retval;
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds /*
15761da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
15771da177e4SLinus Torvalds  */
1578bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	return sys_umount(name, 0);
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds #endif
15841da177e4SLinus Torvalds 
15854ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
15868823c079SEric W. Biederman {
15874ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
1588e149ed2bSAl Viro 	return dentry->d_op == &ns_dentry_operations &&
1589e149ed2bSAl Viro 	       dentry->d_fsdata == &mntns_operations;
15904ce5d2b1SEric W. Biederman }
15914ce5d2b1SEric W. Biederman 
159258be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
159358be2825SAl Viro {
159458be2825SAl Viro 	return container_of(ns, struct mnt_namespace, ns);
159558be2825SAl Viro }
159658be2825SAl Viro 
15974ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
15984ce5d2b1SEric W. Biederman {
15994ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
16004ce5d2b1SEric W. Biederman 	 * mount namespace loop?
16014ce5d2b1SEric W. Biederman 	 */
16024ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
16034ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
16044ce5d2b1SEric W. Biederman 		return false;
16054ce5d2b1SEric W. Biederman 
1606f77c8014SAl Viro 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
16078823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
16088823c079SEric W. Biederman }
16098823c079SEric W. Biederman 
161087129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
161136341f64SRam Pai 					int flag)
16121da177e4SLinus Torvalds {
161384d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
16141da177e4SLinus Torvalds 
16154ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
16164ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
16174ce5d2b1SEric W. Biederman 
16184ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1619be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
16209676f0c6SRam Pai 
162136341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1622be34d1a3SDavid Howells 	if (IS_ERR(q))
1623be34d1a3SDavid Howells 		return q;
1624be34d1a3SDavid Howells 
1625a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	p = mnt;
16286b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1629315fc83eSAl Viro 		struct mount *s;
16307ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
16311da177e4SLinus Torvalds 			continue;
16321da177e4SLinus Torvalds 
1633909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
163412a5b529SAl Viro 			struct mount *t = NULL;
16354ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
16364ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
16374ce5d2b1SEric W. Biederman 				s = skip_mnt_tree(s);
16384ce5d2b1SEric W. Biederman 				continue;
16394ce5d2b1SEric W. Biederman 			}
16404ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
16414ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
16429676f0c6SRam Pai 				s = skip_mnt_tree(s);
16439676f0c6SRam Pai 				continue;
16449676f0c6SRam Pai 			}
16450714a533SAl Viro 			while (p != s->mnt_parent) {
16460714a533SAl Viro 				p = p->mnt_parent;
16470714a533SAl Viro 				q = q->mnt_parent;
16481da177e4SLinus Torvalds 			}
164987129cc0SAl Viro 			p = s;
165084d17192SAl Viro 			parent = q;
165187129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1652be34d1a3SDavid Howells 			if (IS_ERR(q))
1653be34d1a3SDavid Howells 				goto out;
1654719ea2fbSAl Viro 			lock_mount_hash();
16551a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
165612a5b529SAl Viro 			mnt_set_mountpoint(parent, p->mnt_mp, q);
165712a5b529SAl Viro 			if (!list_empty(&parent->mnt_mounts)) {
165812a5b529SAl Viro 				t = list_last_entry(&parent->mnt_mounts,
165912a5b529SAl Viro 					struct mount, mnt_child);
166012a5b529SAl Viro 				if (t->mnt_mp != p->mnt_mp)
166112a5b529SAl Viro 					t = NULL;
166212a5b529SAl Viro 			}
166312a5b529SAl Viro 			attach_shadowed(q, parent, t);
1664719ea2fbSAl Viro 			unlock_mount_hash();
16651da177e4SLinus Torvalds 		}
16661da177e4SLinus Torvalds 	}
16671da177e4SLinus Torvalds 	return res;
1668be34d1a3SDavid Howells out:
16691da177e4SLinus Torvalds 	if (res) {
1670719ea2fbSAl Viro 		lock_mount_hash();
1671e819f152SEric W. Biederman 		umount_tree(res, UMOUNT_SYNC);
1672719ea2fbSAl Viro 		unlock_mount_hash();
16731da177e4SLinus Torvalds 	}
1674be34d1a3SDavid Howells 	return q;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
1677be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1678be34d1a3SDavid Howells 
1679589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
16808aec0809SAl Viro {
1681cb338d06SAl Viro 	struct mount *tree;
168297216be0SAl Viro 	namespace_lock();
1683cd4a4017SEric W. Biederman 	if (!check_mnt(real_mount(path->mnt)))
1684cd4a4017SEric W. Biederman 		tree = ERR_PTR(-EINVAL);
1685cd4a4017SEric W. Biederman 	else
168687129cc0SAl Viro 		tree = copy_tree(real_mount(path->mnt), path->dentry,
168787129cc0SAl Viro 				 CL_COPY_ALL | CL_PRIVATE);
1688328e6d90SAl Viro 	namespace_unlock();
1689be34d1a3SDavid Howells 	if (IS_ERR(tree))
169052e220d3SDan Carpenter 		return ERR_CAST(tree);
1691be34d1a3SDavid Howells 	return &tree->mnt;
16928aec0809SAl Viro }
16938aec0809SAl Viro 
16948aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
16958aec0809SAl Viro {
169697216be0SAl Viro 	namespace_lock();
1697719ea2fbSAl Viro 	lock_mount_hash();
1698e819f152SEric W. Biederman 	umount_tree(real_mount(mnt), UMOUNT_SYNC);
1699719ea2fbSAl Viro 	unlock_mount_hash();
17003ab6abeeSAl Viro 	namespace_unlock();
17018aec0809SAl Viro }
17028aec0809SAl Viro 
1703c771d683SMiklos Szeredi /**
1704c771d683SMiklos Szeredi  * clone_private_mount - create a private clone of a path
1705c771d683SMiklos Szeredi  *
1706c771d683SMiklos Szeredi  * This creates a new vfsmount, which will be the clone of @path.  The new will
1707c771d683SMiklos Szeredi  * not be attached anywhere in the namespace and will be private (i.e. changes
1708c771d683SMiklos Szeredi  * to the originating mount won't be propagated into this).
1709c771d683SMiklos Szeredi  *
1710c771d683SMiklos Szeredi  * Release with mntput().
1711c771d683SMiklos Szeredi  */
1712c771d683SMiklos Szeredi struct vfsmount *clone_private_mount(struct path *path)
1713c771d683SMiklos Szeredi {
1714c771d683SMiklos Szeredi 	struct mount *old_mnt = real_mount(path->mnt);
1715c771d683SMiklos Szeredi 	struct mount *new_mnt;
1716c771d683SMiklos Szeredi 
1717c771d683SMiklos Szeredi 	if (IS_MNT_UNBINDABLE(old_mnt))
1718c771d683SMiklos Szeredi 		return ERR_PTR(-EINVAL);
1719c771d683SMiklos Szeredi 
1720c771d683SMiklos Szeredi 	down_read(&namespace_sem);
1721c771d683SMiklos Szeredi 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1722c771d683SMiklos Szeredi 	up_read(&namespace_sem);
1723c771d683SMiklos Szeredi 	if (IS_ERR(new_mnt))
1724c771d683SMiklos Szeredi 		return ERR_CAST(new_mnt);
1725c771d683SMiklos Szeredi 
1726c771d683SMiklos Szeredi 	return &new_mnt->mnt;
1727c771d683SMiklos Szeredi }
1728c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount);
1729c771d683SMiklos Szeredi 
17301f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
17311f707137SAl Viro 		   struct vfsmount *root)
17321f707137SAl Viro {
17331a4eeaf2SAl Viro 	struct mount *mnt;
17341f707137SAl Viro 	int res = f(root, arg);
17351f707137SAl Viro 	if (res)
17361f707137SAl Viro 		return res;
17371a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
17381a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
17391f707137SAl Viro 		if (res)
17401f707137SAl Viro 			return res;
17411f707137SAl Viro 	}
17421f707137SAl Viro 	return 0;
17431f707137SAl Viro }
17441f707137SAl Viro 
17454b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1746719f5d7fSMiklos Szeredi {
1747315fc83eSAl Viro 	struct mount *p;
1748719f5d7fSMiklos Szeredi 
1749909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1750fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
17514b8b21f4SAl Viro 			mnt_release_group_id(p);
1752719f5d7fSMiklos Szeredi 	}
1753719f5d7fSMiklos Szeredi }
1754719f5d7fSMiklos Szeredi 
17554b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1756719f5d7fSMiklos Szeredi {
1757315fc83eSAl Viro 	struct mount *p;
1758719f5d7fSMiklos Szeredi 
1759909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1760fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
17614b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1762719f5d7fSMiklos Szeredi 			if (err) {
17634b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1764719f5d7fSMiklos Szeredi 				return err;
1765719f5d7fSMiklos Szeredi 			}
1766719f5d7fSMiklos Szeredi 		}
1767719f5d7fSMiklos Szeredi 	}
1768719f5d7fSMiklos Szeredi 
1769719f5d7fSMiklos Szeredi 	return 0;
1770719f5d7fSMiklos Szeredi }
1771719f5d7fSMiklos Szeredi 
1772b90fa9aeSRam Pai /*
1773b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1774b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
177521444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
177621444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
177721444403SRam Pai  *  		   (done when source_mnt is moved)
1778b90fa9aeSRam Pai  *
1779b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1780b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
17819676f0c6SRam Pai  * ---------------------------------------------------------------------------
1782b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
17839676f0c6SRam Pai  * |**************************************************************************
17849676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
17859676f0c6SRam Pai  * | dest     |               |                |                |            |
17869676f0c6SRam Pai  * |   |      |               |                |                |            |
17879676f0c6SRam Pai  * |   v      |               |                |                |            |
17889676f0c6SRam Pai  * |**************************************************************************
17899676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
17905afe0022SRam Pai  * |          |               |                |                |            |
17919676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
17929676f0c6SRam Pai  * ***************************************************************************
1793b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1794b90fa9aeSRam Pai  * destination mount.
1795b90fa9aeSRam Pai  *
1796b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1797b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1798b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1799b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1800b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1801b90fa9aeSRam Pai  *       mount.
18025afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
18035afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
18045afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
18055afe0022SRam Pai  *       is marked as 'shared and slave'.
18065afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
18075afe0022SRam Pai  * 	 source mount.
18085afe0022SRam Pai  *
18099676f0c6SRam Pai  * ---------------------------------------------------------------------------
181021444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
18119676f0c6SRam Pai  * |**************************************************************************
18129676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
18139676f0c6SRam Pai  * | dest     |               |                |                |            |
18149676f0c6SRam Pai  * |   |      |               |                |                |            |
18159676f0c6SRam Pai  * |   v      |               |                |                |            |
18169676f0c6SRam Pai  * |**************************************************************************
18179676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
18185afe0022SRam Pai  * |          |               |                |                |            |
18199676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
18209676f0c6SRam Pai  * ***************************************************************************
18215afe0022SRam Pai  *
18225afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
18235afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
182421444403SRam Pai  * (+*)  the mount is moved to the destination.
18255afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
18265afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
18275afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
18285afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1829b90fa9aeSRam Pai  *
1830b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1831b90fa9aeSRam Pai  * applied to each mount in the tree.
1832b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1833b90fa9aeSRam Pai  * in allocations.
1834b90fa9aeSRam Pai  */
18350fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
183684d17192SAl Viro 			struct mount *dest_mnt,
183784d17192SAl Viro 			struct mountpoint *dest_mp,
183884d17192SAl Viro 			struct path *parent_path)
1839b90fa9aeSRam Pai {
184038129a13SAl Viro 	HLIST_HEAD(tree_list);
1841315fc83eSAl Viro 	struct mount *child, *p;
184238129a13SAl Viro 	struct hlist_node *n;
1843719f5d7fSMiklos Szeredi 	int err;
1844b90fa9aeSRam Pai 
1845fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
18460fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
1847719f5d7fSMiklos Szeredi 		if (err)
1848719f5d7fSMiklos Szeredi 			goto out;
184984d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1850f2ebb3a9SAl Viro 		lock_mount_hash();
1851719f5d7fSMiklos Szeredi 		if (err)
1852719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
1853909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
18540f0afb1dSAl Viro 			set_mnt_shared(p);
18550b1b901bSAl Viro 	} else {
18560b1b901bSAl Viro 		lock_mount_hash();
1857b90fa9aeSRam Pai 	}
18581a390689SAl Viro 	if (parent_path) {
18590fb54e50SAl Viro 		detach_mnt(source_mnt, parent_path);
186084d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
1861143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
186221444403SRam Pai 	} else {
186384d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
18641d6a32acSAl Viro 		commit_tree(source_mnt, NULL);
186521444403SRam Pai 	}
1866b90fa9aeSRam Pai 
186738129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
18681d6a32acSAl Viro 		struct mount *q;
186938129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
18701d6a32acSAl Viro 		q = __lookup_mnt_last(&child->mnt_parent->mnt,
18711d6a32acSAl Viro 				      child->mnt_mountpoint);
18721d6a32acSAl Viro 		commit_tree(child, q);
1873b90fa9aeSRam Pai 	}
1874719ea2fbSAl Viro 	unlock_mount_hash();
187599b7db7bSNick Piggin 
1876b90fa9aeSRam Pai 	return 0;
1877719f5d7fSMiklos Szeredi 
1878719f5d7fSMiklos Szeredi  out_cleanup_ids:
1879f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
1880f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
1881e819f152SEric W. Biederman 		umount_tree(child, UMOUNT_SYNC);
1882f2ebb3a9SAl Viro 	}
1883f2ebb3a9SAl Viro 	unlock_mount_hash();
18840fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
1885719f5d7fSMiklos Szeredi  out:
1886719f5d7fSMiklos Szeredi 	return err;
1887b90fa9aeSRam Pai }
1888b90fa9aeSRam Pai 
188984d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
1890b12cea91SAl Viro {
1891b12cea91SAl Viro 	struct vfsmount *mnt;
189284d17192SAl Viro 	struct dentry *dentry = path->dentry;
1893b12cea91SAl Viro retry:
189484d17192SAl Viro 	mutex_lock(&dentry->d_inode->i_mutex);
189584d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
189684d17192SAl Viro 		mutex_unlock(&dentry->d_inode->i_mutex);
189784d17192SAl Viro 		return ERR_PTR(-ENOENT);
1898b12cea91SAl Viro 	}
189997216be0SAl Viro 	namespace_lock();
1900b12cea91SAl Viro 	mnt = lookup_mnt(path);
190184d17192SAl Viro 	if (likely(!mnt)) {
1902e2dfa935SEric W. Biederman 		struct mountpoint *mp = lookup_mountpoint(dentry);
1903e2dfa935SEric W. Biederman 		if (!mp)
1904e2dfa935SEric W. Biederman 			mp = new_mountpoint(dentry);
190584d17192SAl Viro 		if (IS_ERR(mp)) {
190697216be0SAl Viro 			namespace_unlock();
190784d17192SAl Viro 			mutex_unlock(&dentry->d_inode->i_mutex);
190884d17192SAl Viro 			return mp;
190984d17192SAl Viro 		}
191084d17192SAl Viro 		return mp;
191184d17192SAl Viro 	}
191297216be0SAl Viro 	namespace_unlock();
1913b12cea91SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
1914b12cea91SAl Viro 	path_put(path);
1915b12cea91SAl Viro 	path->mnt = mnt;
191684d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
1917b12cea91SAl Viro 	goto retry;
1918b12cea91SAl Viro }
1919b12cea91SAl Viro 
192084d17192SAl Viro static void unlock_mount(struct mountpoint *where)
1921b12cea91SAl Viro {
192284d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
192384d17192SAl Viro 	put_mountpoint(where);
1924328e6d90SAl Viro 	namespace_unlock();
192584d17192SAl Viro 	mutex_unlock(&dentry->d_inode->i_mutex);
1926b12cea91SAl Viro }
1927b12cea91SAl Viro 
192884d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
19291da177e4SLinus Torvalds {
193095bc5f25SAl Viro 	if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
19311da177e4SLinus Torvalds 		return -EINVAL;
19321da177e4SLinus Torvalds 
1933e36cb0b8SDavid Howells 	if (d_is_dir(mp->m_dentry) !=
1934e36cb0b8SDavid Howells 	      d_is_dir(mnt->mnt.mnt_root))
19351da177e4SLinus Torvalds 		return -ENOTDIR;
19361da177e4SLinus Torvalds 
193784d17192SAl Viro 	return attach_recursive_mnt(mnt, p, mp, NULL);
19381da177e4SLinus Torvalds }
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds /*
19417a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
19427a2e8a8fSValerie Aurora  */
19437a2e8a8fSValerie Aurora 
19447a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags)
19457a2e8a8fSValerie Aurora {
19467c6e984dSRoman Borisov 	int type = flags & ~(MS_REC | MS_SILENT);
19477a2e8a8fSValerie Aurora 
19487a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
19497a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
19507a2e8a8fSValerie Aurora 		return 0;
19517a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
19527a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
19537a2e8a8fSValerie Aurora 		return 0;
19547a2e8a8fSValerie Aurora 	return type;
19557a2e8a8fSValerie Aurora }
19567a2e8a8fSValerie Aurora 
19577a2e8a8fSValerie Aurora /*
195807b20889SRam Pai  * recursively change the type of the mountpoint.
195907b20889SRam Pai  */
19600a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
196107b20889SRam Pai {
1962315fc83eSAl Viro 	struct mount *m;
19634b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
196407b20889SRam Pai 	int recurse = flag & MS_REC;
19657a2e8a8fSValerie Aurora 	int type;
1966719f5d7fSMiklos Szeredi 	int err = 0;
196707b20889SRam Pai 
19682d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
196907b20889SRam Pai 		return -EINVAL;
197007b20889SRam Pai 
19717a2e8a8fSValerie Aurora 	type = flags_to_propagation_type(flag);
19727a2e8a8fSValerie Aurora 	if (!type)
19737a2e8a8fSValerie Aurora 		return -EINVAL;
19747a2e8a8fSValerie Aurora 
197597216be0SAl Viro 	namespace_lock();
1976719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1977719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1978719f5d7fSMiklos Szeredi 		if (err)
1979719f5d7fSMiklos Szeredi 			goto out_unlock;
1980719f5d7fSMiklos Szeredi 	}
1981719f5d7fSMiklos Szeredi 
1982719ea2fbSAl Viro 	lock_mount_hash();
1983909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
19840f0afb1dSAl Viro 		change_mnt_propagation(m, type);
1985719ea2fbSAl Viro 	unlock_mount_hash();
1986719f5d7fSMiklos Szeredi 
1987719f5d7fSMiklos Szeredi  out_unlock:
198897216be0SAl Viro 	namespace_unlock();
1989719f5d7fSMiklos Szeredi 	return err;
199007b20889SRam Pai }
199107b20889SRam Pai 
19925ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
19935ff9d8a6SEric W. Biederman {
19945ff9d8a6SEric W. Biederman 	struct mount *child;
19955ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
19965ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
19975ff9d8a6SEric W. Biederman 			continue;
19985ff9d8a6SEric W. Biederman 
19995ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
20005ff9d8a6SEric W. Biederman 			return true;
20015ff9d8a6SEric W. Biederman 	}
20025ff9d8a6SEric W. Biederman 	return false;
20035ff9d8a6SEric W. Biederman }
20045ff9d8a6SEric W. Biederman 
200507b20889SRam Pai /*
20061da177e4SLinus Torvalds  * do loopback mount.
20071da177e4SLinus Torvalds  */
2008808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
20092dafe1c4SEric Sandeen 				int recurse)
20101da177e4SLinus Torvalds {
20112d92ab3cSAl Viro 	struct path old_path;
201284d17192SAl Viro 	struct mount *mnt = NULL, *old, *parent;
201384d17192SAl Viro 	struct mountpoint *mp;
201457eccb83SAl Viro 	int err;
20151da177e4SLinus Torvalds 	if (!old_name || !*old_name)
20161da177e4SLinus Torvalds 		return -EINVAL;
2017815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
20181da177e4SLinus Torvalds 	if (err)
20191da177e4SLinus Torvalds 		return err;
20201da177e4SLinus Torvalds 
20218823c079SEric W. Biederman 	err = -EINVAL;
20224ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
20238823c079SEric W. Biederman 		goto out;
20248823c079SEric W. Biederman 
202584d17192SAl Viro 	mp = lock_mount(path);
202684d17192SAl Viro 	err = PTR_ERR(mp);
202784d17192SAl Viro 	if (IS_ERR(mp))
20289676f0c6SRam Pai 		goto out;
20299676f0c6SRam Pai 
203087129cc0SAl Viro 	old = real_mount(old_path.mnt);
203184d17192SAl Viro 	parent = real_mount(path->mnt);
203287129cc0SAl Viro 
2033b12cea91SAl Viro 	err = -EINVAL;
2034fc7be130SAl Viro 	if (IS_MNT_UNBINDABLE(old))
2035b12cea91SAl Viro 		goto out2;
2036b12cea91SAl Viro 
2037e149ed2bSAl Viro 	if (!check_mnt(parent))
2038e149ed2bSAl Viro 		goto out2;
2039e149ed2bSAl Viro 
2040e149ed2bSAl Viro 	if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
2041b12cea91SAl Viro 		goto out2;
2042ccd48bc7SAl Viro 
20435ff9d8a6SEric W. Biederman 	if (!recurse && has_locked_children(old, old_path.dentry))
20445ff9d8a6SEric W. Biederman 		goto out2;
20455ff9d8a6SEric W. Biederman 
20461da177e4SLinus Torvalds 	if (recurse)
20474ce5d2b1SEric W. Biederman 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
20481da177e4SLinus Torvalds 	else
204987129cc0SAl Viro 		mnt = clone_mnt(old, old_path.dentry, 0);
20501da177e4SLinus Torvalds 
2051be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
2052be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
2053e9c5d8a5SAndrey Vagin 		goto out2;
2054be34d1a3SDavid Howells 	}
2055ccd48bc7SAl Viro 
20565ff9d8a6SEric W. Biederman 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
20575ff9d8a6SEric W. Biederman 
205884d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
20591da177e4SLinus Torvalds 	if (err) {
2060719ea2fbSAl Viro 		lock_mount_hash();
2061e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_SYNC);
2062719ea2fbSAl Viro 		unlock_mount_hash();
20635b83d2c5SRam Pai 	}
2064b12cea91SAl Viro out2:
206584d17192SAl Viro 	unlock_mount(mp);
2066ccd48bc7SAl Viro out:
20672d92ab3cSAl Viro 	path_put(&old_path);
20681da177e4SLinus Torvalds 	return err;
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
20712e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
20722e4b7fcdSDave Hansen {
20732e4b7fcdSDave Hansen 	int error = 0;
20742e4b7fcdSDave Hansen 	int readonly_request = 0;
20752e4b7fcdSDave Hansen 
20762e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
20772e4b7fcdSDave Hansen 		readonly_request = 1;
20782e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
20792e4b7fcdSDave Hansen 		return 0;
20802e4b7fcdSDave Hansen 
20812e4b7fcdSDave Hansen 	if (readonly_request)
208283adc753SAl Viro 		error = mnt_make_readonly(real_mount(mnt));
20832e4b7fcdSDave Hansen 	else
208483adc753SAl Viro 		__mnt_unmake_readonly(real_mount(mnt));
20852e4b7fcdSDave Hansen 	return error;
20862e4b7fcdSDave Hansen }
20872e4b7fcdSDave Hansen 
20881da177e4SLinus Torvalds /*
20891da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
20901da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
20911da177e4SLinus Torvalds  * on it - tough luck.
20921da177e4SLinus Torvalds  */
20930a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
20941da177e4SLinus Torvalds 		      void *data)
20951da177e4SLinus Torvalds {
20961da177e4SLinus Torvalds 	int err;
20972d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
2098143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
20991da177e4SLinus Torvalds 
2100143c8c91SAl Viro 	if (!check_mnt(mnt))
21011da177e4SLinus Torvalds 		return -EINVAL;
21021da177e4SLinus Torvalds 
21032d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
21041da177e4SLinus Torvalds 		return -EINVAL;
21051da177e4SLinus Torvalds 
210607b64558SEric W. Biederman 	/* Don't allow changing of locked mnt flags.
210707b64558SEric W. Biederman 	 *
210807b64558SEric W. Biederman 	 * No locks need to be held here while testing the various
210907b64558SEric W. Biederman 	 * MNT_LOCK flags because those flags can never be cleared
211007b64558SEric W. Biederman 	 * once they are set.
211107b64558SEric W. Biederman 	 */
211207b64558SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
211307b64558SEric W. Biederman 	    !(mnt_flags & MNT_READONLY)) {
211407b64558SEric W. Biederman 		return -EPERM;
211507b64558SEric W. Biederman 	}
21169566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
21179566d674SEric W. Biederman 	    !(mnt_flags & MNT_NODEV)) {
21183e186641SEric W. Biederman 		/* Was the nodev implicitly added in mount? */
21193e186641SEric W. Biederman 		if ((mnt->mnt_ns->user_ns != &init_user_ns) &&
21203e186641SEric W. Biederman 		    !(sb->s_type->fs_flags & FS_USERNS_DEV_MOUNT)) {
21213e186641SEric W. Biederman 			mnt_flags |= MNT_NODEV;
21223e186641SEric W. Biederman 		} else {
21239566d674SEric W. Biederman 			return -EPERM;
21249566d674SEric W. Biederman 		}
21253e186641SEric W. Biederman 	}
21269566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
21279566d674SEric W. Biederman 	    !(mnt_flags & MNT_NOSUID)) {
21289566d674SEric W. Biederman 		return -EPERM;
21299566d674SEric W. Biederman 	}
21309566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
21319566d674SEric W. Biederman 	    !(mnt_flags & MNT_NOEXEC)) {
21329566d674SEric W. Biederman 		return -EPERM;
21339566d674SEric W. Biederman 	}
21349566d674SEric W. Biederman 	if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
21359566d674SEric W. Biederman 	    ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
21369566d674SEric W. Biederman 		return -EPERM;
21379566d674SEric W. Biederman 	}
21389566d674SEric W. Biederman 
2139ff36fe2cSEric Paris 	err = security_sb_remount(sb, data);
2140ff36fe2cSEric Paris 	if (err)
2141ff36fe2cSEric Paris 		return err;
2142ff36fe2cSEric Paris 
21431da177e4SLinus Torvalds 	down_write(&sb->s_umount);
21442e4b7fcdSDave Hansen 	if (flags & MS_BIND)
21452d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
214657eccb83SAl Viro 	else if (!capable(CAP_SYS_ADMIN))
214757eccb83SAl Viro 		err = -EPERM;
21484aa98cf7SAl Viro 	else
21491da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
21507b43a79fSAl Viro 	if (!err) {
2151719ea2fbSAl Viro 		lock_mount_hash();
2152a6138db8SEric W. Biederman 		mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2153143c8c91SAl Viro 		mnt->mnt.mnt_flags = mnt_flags;
2154143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2155719ea2fbSAl Viro 		unlock_mount_hash();
21560e55a7ccSDan Williams 	}
21576339dab8SAl Viro 	up_write(&sb->s_umount);
21581da177e4SLinus Torvalds 	return err;
21591da177e4SLinus Torvalds }
21601da177e4SLinus Torvalds 
2161cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
21629676f0c6SRam Pai {
2163315fc83eSAl Viro 	struct mount *p;
2164909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2165fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
21669676f0c6SRam Pai 			return 1;
21679676f0c6SRam Pai 	}
21689676f0c6SRam Pai 	return 0;
21699676f0c6SRam Pai }
21709676f0c6SRam Pai 
2171808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name)
21721da177e4SLinus Torvalds {
21732d92ab3cSAl Viro 	struct path old_path, parent_path;
2174676da58dSAl Viro 	struct mount *p;
21750fb54e50SAl Viro 	struct mount *old;
217684d17192SAl Viro 	struct mountpoint *mp;
217757eccb83SAl Viro 	int err;
21781da177e4SLinus Torvalds 	if (!old_name || !*old_name)
21791da177e4SLinus Torvalds 		return -EINVAL;
21802d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
21811da177e4SLinus Torvalds 	if (err)
21821da177e4SLinus Torvalds 		return err;
21831da177e4SLinus Torvalds 
218484d17192SAl Viro 	mp = lock_mount(path);
218584d17192SAl Viro 	err = PTR_ERR(mp);
218684d17192SAl Viro 	if (IS_ERR(mp))
2187cc53ce53SDavid Howells 		goto out;
2188cc53ce53SDavid Howells 
2189143c8c91SAl Viro 	old = real_mount(old_path.mnt);
2190fc7be130SAl Viro 	p = real_mount(path->mnt);
2191143c8c91SAl Viro 
21921da177e4SLinus Torvalds 	err = -EINVAL;
2193fc7be130SAl Viro 	if (!check_mnt(p) || !check_mnt(old))
21941da177e4SLinus Torvalds 		goto out1;
21951da177e4SLinus Torvalds 
21965ff9d8a6SEric W. Biederman 	if (old->mnt.mnt_flags & MNT_LOCKED)
21975ff9d8a6SEric W. Biederman 		goto out1;
21985ff9d8a6SEric W. Biederman 
21991da177e4SLinus Torvalds 	err = -EINVAL;
22002d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
220121444403SRam Pai 		goto out1;
22021da177e4SLinus Torvalds 
2203676da58dSAl Viro 	if (!mnt_has_parent(old))
220421444403SRam Pai 		goto out1;
22051da177e4SLinus Torvalds 
2206e36cb0b8SDavid Howells 	if (d_is_dir(path->dentry) !=
2207e36cb0b8SDavid Howells 	      d_is_dir(old_path.dentry))
220821444403SRam Pai 		goto out1;
220921444403SRam Pai 	/*
221021444403SRam Pai 	 * Don't move a mount residing in a shared parent.
221121444403SRam Pai 	 */
2212fc7be130SAl Viro 	if (IS_MNT_SHARED(old->mnt_parent))
221321444403SRam Pai 		goto out1;
22149676f0c6SRam Pai 	/*
22159676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
22169676f0c6SRam Pai 	 * mount which is shared.
22179676f0c6SRam Pai 	 */
2218fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
22199676f0c6SRam Pai 		goto out1;
22201da177e4SLinus Torvalds 	err = -ELOOP;
2221fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2222676da58dSAl Viro 		if (p == old)
222321444403SRam Pai 			goto out1;
22241da177e4SLinus Torvalds 
222584d17192SAl Viro 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
22264ac91378SJan Blunck 	if (err)
222721444403SRam Pai 		goto out1;
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
22301da177e4SLinus Torvalds 	 * automatically */
22316776db3dSAl Viro 	list_del_init(&old->mnt_expire);
22321da177e4SLinus Torvalds out1:
223384d17192SAl Viro 	unlock_mount(mp);
22341da177e4SLinus Torvalds out:
22351da177e4SLinus Torvalds 	if (!err)
22361a390689SAl Viro 		path_put(&parent_path);
22372d92ab3cSAl Viro 	path_put(&old_path);
22381da177e4SLinus Torvalds 	return err;
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
22419d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
22429d412a43SAl Viro {
22439d412a43SAl Viro 	int err;
22449d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
22459d412a43SAl Viro 	if (subtype) {
22469d412a43SAl Viro 		subtype++;
22479d412a43SAl Viro 		err = -EINVAL;
22489d412a43SAl Viro 		if (!subtype[0])
22499d412a43SAl Viro 			goto err;
22509d412a43SAl Viro 	} else
22519d412a43SAl Viro 		subtype = "";
22529d412a43SAl Viro 
22539d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
22549d412a43SAl Viro 	err = -ENOMEM;
22559d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
22569d412a43SAl Viro 		goto err;
22579d412a43SAl Viro 	return mnt;
22589d412a43SAl Viro 
22599d412a43SAl Viro  err:
22609d412a43SAl Viro 	mntput(mnt);
22619d412a43SAl Viro 	return ERR_PTR(err);
22629d412a43SAl Viro }
22639d412a43SAl Viro 
22649d412a43SAl Viro /*
22659d412a43SAl Viro  * add a mount into a namespace's mount tree
22669d412a43SAl Viro  */
226795bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
22689d412a43SAl Viro {
226984d17192SAl Viro 	struct mountpoint *mp;
227084d17192SAl Viro 	struct mount *parent;
22719d412a43SAl Viro 	int err;
22729d412a43SAl Viro 
2273f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
22749d412a43SAl Viro 
227584d17192SAl Viro 	mp = lock_mount(path);
227684d17192SAl Viro 	if (IS_ERR(mp))
227784d17192SAl Viro 		return PTR_ERR(mp);
22789d412a43SAl Viro 
227984d17192SAl Viro 	parent = real_mount(path->mnt);
22809d412a43SAl Viro 	err = -EINVAL;
228184d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2282156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2283156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
22849d412a43SAl Viro 			goto unlock;
2285156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
228684d17192SAl Viro 		if (!parent->mnt_ns)
2287156cacb1SAl Viro 			goto unlock;
2288156cacb1SAl Viro 	}
22899d412a43SAl Viro 
22909d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
22919d412a43SAl Viro 	err = -EBUSY;
229295bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
22939d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
22949d412a43SAl Viro 		goto unlock;
22959d412a43SAl Viro 
22969d412a43SAl Viro 	err = -EINVAL;
2297e36cb0b8SDavid Howells 	if (d_is_symlink(newmnt->mnt.mnt_root))
22989d412a43SAl Viro 		goto unlock;
22999d412a43SAl Viro 
230095bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
230184d17192SAl Viro 	err = graft_tree(newmnt, parent, mp);
23029d412a43SAl Viro 
23039d412a43SAl Viro unlock:
230484d17192SAl Viro 	unlock_mount(mp);
23059d412a43SAl Viro 	return err;
23069d412a43SAl Viro }
2307b1e75df4SAl Viro 
23081da177e4SLinus Torvalds /*
23091da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
23101da177e4SLinus Torvalds  * namespace's tree
23111da177e4SLinus Torvalds  */
23120c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags,
2313808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
23141da177e4SLinus Torvalds {
23150c55cfc4SEric W. Biederman 	struct file_system_type *type;
23169b40bc90SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
23171da177e4SLinus Torvalds 	struct vfsmount *mnt;
231815f9a3f3SAl Viro 	int err;
23191da177e4SLinus Torvalds 
23200c55cfc4SEric W. Biederman 	if (!fstype)
23211da177e4SLinus Torvalds 		return -EINVAL;
23221da177e4SLinus Torvalds 
23230c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
23240c55cfc4SEric W. Biederman 	if (!type)
23250c55cfc4SEric W. Biederman 		return -ENODEV;
23260c55cfc4SEric W. Biederman 
23270c55cfc4SEric W. Biederman 	if (user_ns != &init_user_ns) {
23280c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_MOUNT)) {
23290c55cfc4SEric W. Biederman 			put_filesystem(type);
23300c55cfc4SEric W. Biederman 			return -EPERM;
23310c55cfc4SEric W. Biederman 		}
23320c55cfc4SEric W. Biederman 		/* Only in special cases allow devices from mounts
23330c55cfc4SEric W. Biederman 		 * created outside the initial user namespace.
23340c55cfc4SEric W. Biederman 		 */
23350c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
23360c55cfc4SEric W. Biederman 			flags |= MS_NODEV;
23379566d674SEric W. Biederman 			mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV;
23380c55cfc4SEric W. Biederman 		}
23390c55cfc4SEric W. Biederman 	}
23400c55cfc4SEric W. Biederman 
23410c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, flags, name, data);
23420c55cfc4SEric W. Biederman 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
23430c55cfc4SEric W. Biederman 	    !mnt->mnt_sb->s_subtype)
23440c55cfc4SEric W. Biederman 		mnt = fs_set_subtype(mnt, fstype);
23450c55cfc4SEric W. Biederman 
23460c55cfc4SEric W. Biederman 	put_filesystem(type);
23471da177e4SLinus Torvalds 	if (IS_ERR(mnt))
23481da177e4SLinus Torvalds 		return PTR_ERR(mnt);
23491da177e4SLinus Torvalds 
235095bc5f25SAl Viro 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
235115f9a3f3SAl Viro 	if (err)
235215f9a3f3SAl Viro 		mntput(mnt);
235315f9a3f3SAl Viro 	return err;
23541da177e4SLinus Torvalds }
23551da177e4SLinus Torvalds 
235619a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
235719a167afSAl Viro {
23586776db3dSAl Viro 	struct mount *mnt = real_mount(m);
235919a167afSAl Viro 	int err;
236019a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
236119a167afSAl Viro 	 * expired before we get a chance to add it
236219a167afSAl Viro 	 */
23636776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
236419a167afSAl Viro 
236519a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
236619a167afSAl Viro 	    m->mnt_root == path->dentry) {
2367b1e75df4SAl Viro 		err = -ELOOP;
2368b1e75df4SAl Viro 		goto fail;
236919a167afSAl Viro 	}
237019a167afSAl Viro 
237195bc5f25SAl Viro 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2372b1e75df4SAl Viro 	if (!err)
2373b1e75df4SAl Viro 		return 0;
2374b1e75df4SAl Viro fail:
2375b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
23766776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
237797216be0SAl Viro 		namespace_lock();
23786776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
237997216be0SAl Viro 		namespace_unlock();
238019a167afSAl Viro 	}
2381b1e75df4SAl Viro 	mntput(m);
2382b1e75df4SAl Viro 	mntput(m);
238319a167afSAl Viro 	return err;
238419a167afSAl Viro }
238519a167afSAl Viro 
2386ea5b778aSDavid Howells /**
2387ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2388ea5b778aSDavid Howells  * @mnt: The mount to list.
2389ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2390ea5b778aSDavid Howells  */
2391ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2392ea5b778aSDavid Howells {
239397216be0SAl Viro 	namespace_lock();
2394ea5b778aSDavid Howells 
23956776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2396ea5b778aSDavid Howells 
239797216be0SAl Viro 	namespace_unlock();
2398ea5b778aSDavid Howells }
2399ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2400ea5b778aSDavid Howells 
2401ea5b778aSDavid Howells /*
24021da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
24031da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
24041da177e4SLinus Torvalds  * here
24051da177e4SLinus Torvalds  */
24061da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
24071da177e4SLinus Torvalds {
2408761d5c38SAl Viro 	struct mount *mnt, *next;
24091da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 	if (list_empty(mounts))
24121da177e4SLinus Torvalds 		return;
24131da177e4SLinus Torvalds 
241497216be0SAl Viro 	namespace_lock();
2415719ea2fbSAl Viro 	lock_mount_hash();
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
24181da177e4SLinus Torvalds 	 * following criteria:
24191da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
24201da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
24211da177e4SLinus Torvalds 	 *   cleared by mntput())
24221da177e4SLinus Torvalds 	 */
24236776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2424863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
24251ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
24261da177e4SLinus Torvalds 			continue;
24276776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
24281da177e4SLinus Torvalds 	}
2429bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
24306776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2431143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2432e819f152SEric W. Biederman 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2433bcc5c7d2SAl Viro 	}
2434719ea2fbSAl Viro 	unlock_mount_hash();
24353ab6abeeSAl Viro 	namespace_unlock();
24361da177e4SLinus Torvalds }
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds /*
24415528f911STrond Myklebust  * Ripoff of 'select_parent()'
24425528f911STrond Myklebust  *
24435528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
24445528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
24455528f911STrond Myklebust  */
2446692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
24475528f911STrond Myklebust {
2448692afc31SAl Viro 	struct mount *this_parent = parent;
24495528f911STrond Myklebust 	struct list_head *next;
24505528f911STrond Myklebust 	int found = 0;
24515528f911STrond Myklebust 
24525528f911STrond Myklebust repeat:
24536b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
24545528f911STrond Myklebust resume:
24556b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
24565528f911STrond Myklebust 		struct list_head *tmp = next;
24576b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
24585528f911STrond Myklebust 
24595528f911STrond Myklebust 		next = tmp->next;
2460692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
24615528f911STrond Myklebust 			continue;
24625528f911STrond Myklebust 		/*
24635528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
24645528f911STrond Myklebust 		 */
24656b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
24665528f911STrond Myklebust 			this_parent = mnt;
24675528f911STrond Myklebust 			goto repeat;
24685528f911STrond Myklebust 		}
24695528f911STrond Myklebust 
24701ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
24716776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
24725528f911STrond Myklebust 			found++;
24735528f911STrond Myklebust 		}
24745528f911STrond Myklebust 	}
24755528f911STrond Myklebust 	/*
24765528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
24775528f911STrond Myklebust 	 */
24785528f911STrond Myklebust 	if (this_parent != parent) {
24796b41d536SAl Viro 		next = this_parent->mnt_child.next;
24800714a533SAl Viro 		this_parent = this_parent->mnt_parent;
24815528f911STrond Myklebust 		goto resume;
24825528f911STrond Myklebust 	}
24835528f911STrond Myklebust 	return found;
24845528f911STrond Myklebust }
24855528f911STrond Myklebust 
24865528f911STrond Myklebust /*
24875528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
24885528f911STrond Myklebust  * submounts of a specific parent mountpoint
248999b7db7bSNick Piggin  *
249048a066e7SAl Viro  * mount_lock must be held for write
24915528f911STrond Myklebust  */
2492b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
24935528f911STrond Myklebust {
24945528f911STrond Myklebust 	LIST_HEAD(graveyard);
2495761d5c38SAl Viro 	struct mount *m;
24965528f911STrond Myklebust 
24975528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2498c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2499bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2500761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
25016776db3dSAl Viro 						mnt_expire);
2502143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
2503e819f152SEric W. Biederman 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2504bcc5c7d2SAl Viro 		}
2505bcc5c7d2SAl Viro 	}
25065528f911STrond Myklebust }
25075528f911STrond Myklebust 
25085528f911STrond Myklebust /*
25091da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
25101da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
25111da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
25121da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
25131da177e4SLinus Torvalds  */
2514b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2515b58fed8bSRam Pai 				 unsigned long n)
25161da177e4SLinus Torvalds {
25171da177e4SLinus Torvalds 	char *t = to;
25181da177e4SLinus Torvalds 	const char __user *f = from;
25191da177e4SLinus Torvalds 	char c;
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
25221da177e4SLinus Torvalds 		return n;
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 	while (n) {
25251da177e4SLinus Torvalds 		if (__get_user(c, f)) {
25261da177e4SLinus Torvalds 			memset(t, 0, n);
25271da177e4SLinus Torvalds 			break;
25281da177e4SLinus Torvalds 		}
25291da177e4SLinus Torvalds 		*t++ = c;
25301da177e4SLinus Torvalds 		f++;
25311da177e4SLinus Torvalds 		n--;
25321da177e4SLinus Torvalds 	}
25331da177e4SLinus Torvalds 	return n;
25341da177e4SLinus Torvalds }
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
25371da177e4SLinus Torvalds {
25381da177e4SLinus Torvalds 	int i;
25391da177e4SLinus Torvalds 	unsigned long page;
25401da177e4SLinus Torvalds 	unsigned long size;
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds 	*where = 0;
25431da177e4SLinus Torvalds 	if (!data)
25441da177e4SLinus Torvalds 		return 0;
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
25471da177e4SLinus Torvalds 		return -ENOMEM;
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
25501da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
25511da177e4SLinus Torvalds 	 * the remainder of the page.
25521da177e4SLinus Torvalds 	 */
25531da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
25541da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
25551da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
25561da177e4SLinus Torvalds 		size = PAGE_SIZE;
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
25591da177e4SLinus Torvalds 	if (!i) {
25601da177e4SLinus Torvalds 		free_page(page);
25611da177e4SLinus Torvalds 		return -EFAULT;
25621da177e4SLinus Torvalds 	}
25631da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
25641da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
25651da177e4SLinus Torvalds 	*where = page;
25661da177e4SLinus Torvalds 	return 0;
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
2569b8850d1fSTim Gardner char *copy_mount_string(const void __user *data)
2570eca6f534SVegard Nossum {
2571b8850d1fSTim Gardner 	return data ? strndup_user(data, PAGE_SIZE) : NULL;
2572eca6f534SVegard Nossum }
2573eca6f534SVegard Nossum 
25741da177e4SLinus Torvalds /*
25751da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
25761da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
25771da177e4SLinus Torvalds  *
25781da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
25791da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
25801da177e4SLinus Torvalds  * information (or be NULL).
25811da177e4SLinus Torvalds  *
25821da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
25831da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
25841da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
25851da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
25861da177e4SLinus Torvalds  * and must be discarded.
25871da177e4SLinus Torvalds  */
25885e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name,
2589808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
25901da177e4SLinus Torvalds {
25912d92ab3cSAl Viro 	struct path path;
25921da177e4SLinus Torvalds 	int retval = 0;
25931da177e4SLinus Torvalds 	int mnt_flags = 0;
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds 	/* Discard magic */
25961da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
25971da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	/* Basic sanity checks */
26001da177e4SLinus Torvalds 	if (data_page)
26011da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
26021da177e4SLinus Torvalds 
2603a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
26045e6123f3SSeunghun Lee 	retval = user_path(dir_name, &path);
2605a27ab9f2STetsuo Handa 	if (retval)
2606a27ab9f2STetsuo Handa 		return retval;
2607a27ab9f2STetsuo Handa 
2608a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2609a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
26100d5cadb8SAl Viro 	if (!retval && !may_mount())
26110d5cadb8SAl Viro 		retval = -EPERM;
2612a27ab9f2STetsuo Handa 	if (retval)
2613a27ab9f2STetsuo Handa 		goto dput_out;
2614a27ab9f2STetsuo Handa 
2615613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2616613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
26170a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
26180a1c01c9SMatthew Garrett 
26191da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
26201da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
26211da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
26221da177e4SLinus Torvalds 	if (flags & MS_NODEV)
26231da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
26241da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
26251da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2626fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2627fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2628fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2629fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2630d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2631d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
26322e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
26332e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2634fc33a7bbSChristoph Hellwig 
2635ffbc6f0eSEric W. Biederman 	/* The default atime for remount is preservation */
2636ffbc6f0eSEric W. Biederman 	if ((flags & MS_REMOUNT) &&
2637ffbc6f0eSEric W. Biederman 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2638ffbc6f0eSEric W. Biederman 		       MS_STRICTATIME)) == 0)) {
2639ffbc6f0eSEric W. Biederman 		mnt_flags &= ~MNT_ATIME_MASK;
2640ffbc6f0eSEric W. Biederman 		mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2641ffbc6f0eSEric W. Biederman 	}
2642ffbc6f0eSEric W. Biederman 
26437a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2644d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2645d0adde57SMatthew Garrett 		   MS_STRICTATIME);
26461da177e4SLinus Torvalds 
26471da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
26482d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
26491da177e4SLinus Torvalds 				    data_page);
26501da177e4SLinus Torvalds 	else if (flags & MS_BIND)
26512d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
26529676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
26532d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
26541da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
26552d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
26561da177e4SLinus Torvalds 	else
26572d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
26581da177e4SLinus Torvalds 				      dev_name, data_page);
26591da177e4SLinus Torvalds dput_out:
26602d92ab3cSAl Viro 	path_put(&path);
26611da177e4SLinus Torvalds 	return retval;
26621da177e4SLinus Torvalds }
26631da177e4SLinus Torvalds 
2664771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
2665771b1371SEric W. Biederman {
26666344c433SAl Viro 	ns_free_inum(&ns->ns);
2667771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
2668771b1371SEric W. Biederman 	kfree(ns);
2669771b1371SEric W. Biederman }
2670771b1371SEric W. Biederman 
26718823c079SEric W. Biederman /*
26728823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
26738823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
26748823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
26758823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
26768823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
26778823c079SEric W. Biederman  */
26788823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
26798823c079SEric W. Biederman 
2680771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2681cf8d2c11STrond Myklebust {
2682cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
268398f842e6SEric W. Biederman 	int ret;
2684cf8d2c11STrond Myklebust 
2685cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2686cf8d2c11STrond Myklebust 	if (!new_ns)
2687cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
26886344c433SAl Viro 	ret = ns_alloc_inum(&new_ns->ns);
268998f842e6SEric W. Biederman 	if (ret) {
269098f842e6SEric W. Biederman 		kfree(new_ns);
269198f842e6SEric W. Biederman 		return ERR_PTR(ret);
269298f842e6SEric W. Biederman 	}
269333c42940SAl Viro 	new_ns->ns.ops = &mntns_operations;
26948823c079SEric W. Biederman 	new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2695cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2696cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2697cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2698cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2699cf8d2c11STrond Myklebust 	new_ns->event = 0;
2700771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
2701cf8d2c11STrond Myklebust 	return new_ns;
2702cf8d2c11STrond Myklebust }
2703cf8d2c11STrond Myklebust 
27049559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
27059559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
27061da177e4SLinus Torvalds {
27076b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
27087f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2709315fc83eSAl Viro 	struct mount *p, *q;
27109559f689SAl Viro 	struct mount *old;
2711cb338d06SAl Viro 	struct mount *new;
27127a472ef4SEric W. Biederman 	int copy_flags;
27131da177e4SLinus Torvalds 
27149559f689SAl Viro 	BUG_ON(!ns);
27159559f689SAl Viro 
27169559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
27179559f689SAl Viro 		get_mnt_ns(ns);
27189559f689SAl Viro 		return ns;
27199559f689SAl Viro 	}
27209559f689SAl Viro 
27219559f689SAl Viro 	old = ns->root;
27229559f689SAl Viro 
2723771b1371SEric W. Biederman 	new_ns = alloc_mnt_ns(user_ns);
2724cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2725cf8d2c11STrond Myklebust 		return new_ns;
27261da177e4SLinus Torvalds 
272797216be0SAl Viro 	namespace_lock();
27281da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
27294ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
27309559f689SAl Viro 	if (user_ns != ns->user_ns)
2731132c94e3SEric W. Biederman 		copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
27327a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2733be34d1a3SDavid Howells 	if (IS_ERR(new)) {
2734328e6d90SAl Viro 		namespace_unlock();
2735771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
2736be34d1a3SDavid Howells 		return ERR_CAST(new);
27371da177e4SLinus Torvalds 	}
2738be08d6d2SAl Viro 	new_ns->root = new;
27391a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds 	/*
27421da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
27431da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
27441da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
27451da177e4SLinus Torvalds 	 */
2746909b0a88SAl Viro 	p = old;
2747cb338d06SAl Viro 	q = new;
27481da177e4SLinus Torvalds 	while (p) {
2749143c8c91SAl Viro 		q->mnt_ns = new_ns;
27509559f689SAl Viro 		if (new_fs) {
27519559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
27529559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
2753315fc83eSAl Viro 				rootmnt = &p->mnt;
27541da177e4SLinus Torvalds 			}
27559559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
27569559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
2757315fc83eSAl Viro 				pwdmnt = &p->mnt;
27581da177e4SLinus Torvalds 			}
27591da177e4SLinus Torvalds 		}
2760909b0a88SAl Viro 		p = next_mnt(p, old);
2761909b0a88SAl Viro 		q = next_mnt(q, new);
27624ce5d2b1SEric W. Biederman 		if (!q)
27634ce5d2b1SEric W. Biederman 			break;
27644ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
27654ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
27661da177e4SLinus Torvalds 	}
2767328e6d90SAl Viro 	namespace_unlock();
27681da177e4SLinus Torvalds 
27691da177e4SLinus Torvalds 	if (rootmnt)
2770f03c6599SAl Viro 		mntput(rootmnt);
27711da177e4SLinus Torvalds 	if (pwdmnt)
2772f03c6599SAl Viro 		mntput(pwdmnt);
27731da177e4SLinus Torvalds 
2774741a2951SJANAK DESAI 	return new_ns;
2775741a2951SJANAK DESAI }
2776741a2951SJANAK DESAI 
2777cf8d2c11STrond Myklebust /**
2778cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2779cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2780cf8d2c11STrond Myklebust  */
27811a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2782cf8d2c11STrond Myklebust {
2783771b1371SEric W. Biederman 	struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2784cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
27851a4eeaf2SAl Viro 		struct mount *mnt = real_mount(m);
27861a4eeaf2SAl Viro 		mnt->mnt_ns = new_ns;
2787be08d6d2SAl Viro 		new_ns->root = mnt;
2788b1983cd8SAl Viro 		list_add(&mnt->mnt_list, &new_ns->list);
2789c1334495SAl Viro 	} else {
27901a4eeaf2SAl Viro 		mntput(m);
2791cf8d2c11STrond Myklebust 	}
2792cf8d2c11STrond Myklebust 	return new_ns;
2793cf8d2c11STrond Myklebust }
2794cf8d2c11STrond Myklebust 
2795ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2796ea441d11SAl Viro {
2797ea441d11SAl Viro 	struct mnt_namespace *ns;
2798d31da0f0SAl Viro 	struct super_block *s;
2799ea441d11SAl Viro 	struct path path;
2800ea441d11SAl Viro 	int err;
2801ea441d11SAl Viro 
2802ea441d11SAl Viro 	ns = create_mnt_ns(mnt);
2803ea441d11SAl Viro 	if (IS_ERR(ns))
2804ea441d11SAl Viro 		return ERR_CAST(ns);
2805ea441d11SAl Viro 
2806ea441d11SAl Viro 	err = vfs_path_lookup(mnt->mnt_root, mnt,
2807ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2808ea441d11SAl Viro 
2809ea441d11SAl Viro 	put_mnt_ns(ns);
2810ea441d11SAl Viro 
2811ea441d11SAl Viro 	if (err)
2812ea441d11SAl Viro 		return ERR_PTR(err);
2813ea441d11SAl Viro 
2814ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
2815d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
2816d31da0f0SAl Viro 	atomic_inc(&s->s_active);
2817ea441d11SAl Viro 	mntput(path.mnt);
2818ea441d11SAl Viro 	/* lock the sucker */
2819d31da0f0SAl Viro 	down_write(&s->s_umount);
2820ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
2821ea441d11SAl Viro 	return path.dentry;
2822ea441d11SAl Viro }
2823ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
2824ea441d11SAl Viro 
2825bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2826bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
28271da177e4SLinus Torvalds {
2828eca6f534SVegard Nossum 	int ret;
2829eca6f534SVegard Nossum 	char *kernel_type;
2830eca6f534SVegard Nossum 	char *kernel_dev;
28311da177e4SLinus Torvalds 	unsigned long data_page;
28321da177e4SLinus Torvalds 
2833b8850d1fSTim Gardner 	kernel_type = copy_mount_string(type);
2834b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_type);
2835b8850d1fSTim Gardner 	if (IS_ERR(kernel_type))
2836eca6f534SVegard Nossum 		goto out_type;
28371da177e4SLinus Torvalds 
2838b8850d1fSTim Gardner 	kernel_dev = copy_mount_string(dev_name);
2839b8850d1fSTim Gardner 	ret = PTR_ERR(kernel_dev);
2840b8850d1fSTim Gardner 	if (IS_ERR(kernel_dev))
2841eca6f534SVegard Nossum 		goto out_dev;
28421da177e4SLinus Torvalds 
2843eca6f534SVegard Nossum 	ret = copy_mount_options(data, &data_page);
2844eca6f534SVegard Nossum 	if (ret < 0)
2845eca6f534SVegard Nossum 		goto out_data;
28461da177e4SLinus Torvalds 
28475e6123f3SSeunghun Lee 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags,
2848eca6f534SVegard Nossum 		(void *) data_page);
2849eca6f534SVegard Nossum 
28501da177e4SLinus Torvalds 	free_page(data_page);
2851eca6f534SVegard Nossum out_data:
2852eca6f534SVegard Nossum 	kfree(kernel_dev);
2853eca6f534SVegard Nossum out_dev:
2854eca6f534SVegard Nossum 	kfree(kernel_type);
2855eca6f534SVegard Nossum out_type:
2856eca6f534SVegard Nossum 	return ret;
28571da177e4SLinus Torvalds }
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds /*
2860afac7cbaSAl Viro  * Return true if path is reachable from root
2861afac7cbaSAl Viro  *
286248a066e7SAl Viro  * namespace_sem or mount_lock is held
2863afac7cbaSAl Viro  */
2864643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2865afac7cbaSAl Viro 			 const struct path *root)
2866afac7cbaSAl Viro {
2867643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2868a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
28690714a533SAl Viro 		mnt = mnt->mnt_parent;
2870afac7cbaSAl Viro 	}
2871643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2872afac7cbaSAl Viro }
2873afac7cbaSAl Viro 
2874afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2)
2875afac7cbaSAl Viro {
2876afac7cbaSAl Viro 	int res;
287748a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
2878643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
287948a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
2880afac7cbaSAl Viro 	return res;
2881afac7cbaSAl Viro }
2882afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
2883afac7cbaSAl Viro 
2884afac7cbaSAl Viro /*
28851da177e4SLinus Torvalds  * pivot_root Semantics:
28861da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
28871da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
28881da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
28891da177e4SLinus Torvalds  *
28901da177e4SLinus Torvalds  * Restrictions:
28911da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
28921da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
28931da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
28941da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
28951da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
28961da177e4SLinus Torvalds  *
28974a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
28984a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
28994a0d11faSNeil Brown  * in this situation.
29004a0d11faSNeil Brown  *
29011da177e4SLinus Torvalds  * Notes:
29021da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
29031da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
29041da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
29051da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
29061da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
29071da177e4SLinus Torvalds  *    first.
29081da177e4SLinus Torvalds  */
29093480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
29103480b257SHeiko Carstens 		const char __user *, put_old)
29111da177e4SLinus Torvalds {
29122d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
291384d17192SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt;
291484d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
29151da177e4SLinus Torvalds 	int error;
29161da177e4SLinus Torvalds 
29179b40bc90SAl Viro 	if (!may_mount())
29181da177e4SLinus Torvalds 		return -EPERM;
29191da177e4SLinus Torvalds 
29202d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
29211da177e4SLinus Torvalds 	if (error)
29221da177e4SLinus Torvalds 		goto out0;
29231da177e4SLinus Torvalds 
29242d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
29251da177e4SLinus Torvalds 	if (error)
29261da177e4SLinus Torvalds 		goto out1;
29271da177e4SLinus Torvalds 
29282d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
2929b12cea91SAl Viro 	if (error)
2930b12cea91SAl Viro 		goto out2;
29311da177e4SLinus Torvalds 
2932f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
293384d17192SAl Viro 	old_mp = lock_mount(&old);
293484d17192SAl Viro 	error = PTR_ERR(old_mp);
293584d17192SAl Viro 	if (IS_ERR(old_mp))
2936b12cea91SAl Viro 		goto out3;
2937b12cea91SAl Viro 
29381da177e4SLinus Torvalds 	error = -EINVAL;
2939419148daSAl Viro 	new_mnt = real_mount(new.mnt);
2940419148daSAl Viro 	root_mnt = real_mount(root.mnt);
294184d17192SAl Viro 	old_mnt = real_mount(old.mnt);
294284d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
2943fc7be130SAl Viro 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
2944fc7be130SAl Viro 		IS_MNT_SHARED(root_mnt->mnt_parent))
2945b12cea91SAl Viro 		goto out4;
2946143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2947b12cea91SAl Viro 		goto out4;
29485ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
29495ff9d8a6SEric W. Biederman 		goto out4;
29501da177e4SLinus Torvalds 	error = -ENOENT;
2951f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
2952b12cea91SAl Viro 		goto out4;
29531da177e4SLinus Torvalds 	error = -EBUSY;
295484d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
2955b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
29561da177e4SLinus Torvalds 	error = -EINVAL;
29578c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
2958b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2959676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
2960b12cea91SAl Viro 		goto out4; /* not attached */
296184d17192SAl Viro 	root_mp = root_mnt->mnt_mp;
29622d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
2963b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2964676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
2965b12cea91SAl Viro 		goto out4; /* not attached */
29664ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
296784d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
2968b12cea91SAl Viro 		goto out4;
29690d082601SEric W. Biederman 	/* make certain new is below the root */
29700d082601SEric W. Biederman 	if (!is_path_reachable(new_mnt, new.dentry, &root))
29710d082601SEric W. Biederman 		goto out4;
297284d17192SAl Viro 	root_mp->m_count++; /* pin it so it won't go away */
2973719ea2fbSAl Viro 	lock_mount_hash();
2974419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
2975419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
29765ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
29775ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
29785ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
29795ff9d8a6SEric W. Biederman 	}
29804ac91378SJan Blunck 	/* mount old root on put_old */
298184d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
29824ac91378SJan Blunck 	/* mount new_root on / */
298384d17192SAl Viro 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
29846b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
29854fed655cSEric W. Biederman 	/* A moved mount should not expire automatically */
29864fed655cSEric W. Biederman 	list_del_init(&new_mnt->mnt_expire);
2987719ea2fbSAl Viro 	unlock_mount_hash();
29882d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
298984d17192SAl Viro 	put_mountpoint(root_mp);
29901da177e4SLinus Torvalds 	error = 0;
2991b12cea91SAl Viro out4:
299284d17192SAl Viro 	unlock_mount(old_mp);
2993b12cea91SAl Viro 	if (!error) {
29941a390689SAl Viro 		path_put(&root_parent);
29951a390689SAl Viro 		path_put(&parent_path);
2996b12cea91SAl Viro 	}
2997b12cea91SAl Viro out3:
29988c3ee42eSAl Viro 	path_put(&root);
2999b12cea91SAl Viro out2:
30002d8f3038SAl Viro 	path_put(&old);
30011da177e4SLinus Torvalds out1:
30022d8f3038SAl Viro 	path_put(&new);
30031da177e4SLinus Torvalds out0:
30041da177e4SLinus Torvalds 	return error;
30051da177e4SLinus Torvalds }
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds static void __init init_mount_tree(void)
30081da177e4SLinus Torvalds {
30091da177e4SLinus Torvalds 	struct vfsmount *mnt;
30106b3286edSKirill Korotaev 	struct mnt_namespace *ns;
3011ac748a09SJan Blunck 	struct path root;
30120c55cfc4SEric W. Biederman 	struct file_system_type *type;
30131da177e4SLinus Torvalds 
30140c55cfc4SEric W. Biederman 	type = get_fs_type("rootfs");
30150c55cfc4SEric W. Biederman 	if (!type)
30160c55cfc4SEric W. Biederman 		panic("Can't find rootfs type");
30170c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
30180c55cfc4SEric W. Biederman 	put_filesystem(type);
30191da177e4SLinus Torvalds 	if (IS_ERR(mnt))
30201da177e4SLinus Torvalds 		panic("Can't create rootfs");
3021b3e19d92SNick Piggin 
30223b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
30233b22edc5STrond Myklebust 	if (IS_ERR(ns))
30241da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
30251da177e4SLinus Torvalds 
30266b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
30276b3286edSKirill Korotaev 	get_mnt_ns(ns);
30281da177e4SLinus Torvalds 
3029be08d6d2SAl Viro 	root.mnt = mnt;
3030be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
3031da362b09SEric W. Biederman 	mnt->mnt_flags |= MNT_LOCKED;
3032ac748a09SJan Blunck 
3033ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
3034ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
30351da177e4SLinus Torvalds }
30361da177e4SLinus Torvalds 
303774bf17cfSDenis Cheng void __init mnt_init(void)
30381da177e4SLinus Torvalds {
303913f14b4dSEric Dumazet 	unsigned u;
304015a67dd8SRandy Dunlap 	int err;
30411da177e4SLinus Torvalds 
30427d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
304320c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
30441da177e4SLinus Torvalds 
30450818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
304638129a13SAl Viro 				sizeof(struct hlist_head),
30470818bf27SAl Viro 				mhash_entries, 19,
30480818bf27SAl Viro 				0,
30490818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
30500818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
30510818bf27SAl Viro 				sizeof(struct hlist_head),
30520818bf27SAl Viro 				mphash_entries, 19,
30530818bf27SAl Viro 				0,
30540818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
30551da177e4SLinus Torvalds 
305684d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
30571da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
30581da177e4SLinus Torvalds 
30590818bf27SAl Viro 	for (u = 0; u <= m_hash_mask; u++)
306038129a13SAl Viro 		INIT_HLIST_HEAD(&mount_hashtable[u]);
30610818bf27SAl Viro 	for (u = 0; u <= mp_hash_mask; u++)
30620818bf27SAl Viro 		INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
30631da177e4SLinus Torvalds 
30644b93dc9bSTejun Heo 	kernfs_init();
30654b93dc9bSTejun Heo 
306615a67dd8SRandy Dunlap 	err = sysfs_init();
306715a67dd8SRandy Dunlap 	if (err)
306815a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
30698e24eea7SHarvey Harrison 			__func__, err);
307000d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
307100d26666SGreg Kroah-Hartman 	if (!fs_kobj)
30728e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
30731da177e4SLinus Torvalds 	init_rootfs();
30741da177e4SLinus Torvalds 	init_mount_tree();
30751da177e4SLinus Torvalds }
30761da177e4SLinus Torvalds 
3077616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
30781da177e4SLinus Torvalds {
3079d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
3080616511d0STrond Myklebust 		return;
30817b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
3082771b1371SEric W. Biederman 	free_mnt_ns(ns);
30831da177e4SLinus Torvalds }
30849d412a43SAl Viro 
30859d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
30869d412a43SAl Viro {
3087423e0ab0STim Chen 	struct vfsmount *mnt;
3088423e0ab0STim Chen 	mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
3089423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
3090423e0ab0STim Chen 		/*
3091423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
3092423e0ab0STim Chen 		 * we unmount before file sys is unregistered
3093423e0ab0STim Chen 		*/
3094f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
3095423e0ab0STim Chen 	}
3096423e0ab0STim Chen 	return mnt;
30979d412a43SAl Viro }
30989d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data);
3099423e0ab0STim Chen 
3100423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
3101423e0ab0STim Chen {
3102423e0ab0STim Chen 	/* release long term mount so mount point can be released */
3103423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
3104f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
310548a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
3106423e0ab0STim Chen 		mntput(mnt);
3107423e0ab0STim Chen 	}
3108423e0ab0STim Chen }
3109423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
311002125a82SAl Viro 
311102125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
311202125a82SAl Viro {
3113143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
311402125a82SAl Viro }
31158823c079SEric W. Biederman 
31163151527eSEric W. Biederman bool current_chrooted(void)
31173151527eSEric W. Biederman {
31183151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
31193151527eSEric W. Biederman 	struct path ns_root;
31203151527eSEric W. Biederman 	struct path fs_root;
31213151527eSEric W. Biederman 	bool chrooted;
31223151527eSEric W. Biederman 
31233151527eSEric W. Biederman 	/* Find the namespace root */
31243151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
31253151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
31263151527eSEric W. Biederman 	path_get(&ns_root);
31273151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
31283151527eSEric W. Biederman 		;
31293151527eSEric W. Biederman 
31303151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
31313151527eSEric W. Biederman 
31323151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
31333151527eSEric W. Biederman 
31343151527eSEric W. Biederman 	path_put(&fs_root);
31353151527eSEric W. Biederman 	path_put(&ns_root);
31363151527eSEric W. Biederman 
31373151527eSEric W. Biederman 	return chrooted;
31383151527eSEric W. Biederman }
31393151527eSEric W. Biederman 
3140e51db735SEric W. Biederman bool fs_fully_visible(struct file_system_type *type)
314187a8ebd6SEric W. Biederman {
314287a8ebd6SEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
314387a8ebd6SEric W. Biederman 	struct mount *mnt;
3144e51db735SEric W. Biederman 	bool visible = false;
314587a8ebd6SEric W. Biederman 
3146e51db735SEric W. Biederman 	if (unlikely(!ns))
3147e51db735SEric W. Biederman 		return false;
3148e51db735SEric W. Biederman 
314944bb4385SAl Viro 	down_read(&namespace_sem);
315087a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
3151e51db735SEric W. Biederman 		struct mount *child;
3152e51db735SEric W. Biederman 		if (mnt->mnt.mnt_sb->s_type != type)
3153e51db735SEric W. Biederman 			continue;
3154e51db735SEric W. Biederman 
3155e51db735SEric W. Biederman 		/* This mount is not fully visible if there are any child mounts
3156e51db735SEric W. Biederman 		 * that cover anything except for empty directories.
3157e51db735SEric W. Biederman 		 */
3158e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3159e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
3160e51db735SEric W. Biederman 			if (!S_ISDIR(inode->i_mode))
3161e51db735SEric W. Biederman 				goto next;
316241301ae7SEric W. Biederman 			if (inode->i_nlink > 2)
3163e51db735SEric W. Biederman 				goto next;
316487a8ebd6SEric W. Biederman 		}
3165e51db735SEric W. Biederman 		visible = true;
3166e51db735SEric W. Biederman 		goto found;
3167e51db735SEric W. Biederman 	next:	;
316887a8ebd6SEric W. Biederman 	}
3169e51db735SEric W. Biederman found:
317044bb4385SAl Viro 	up_read(&namespace_sem);
3171e51db735SEric W. Biederman 	return visible;
317287a8ebd6SEric W. Biederman }
317387a8ebd6SEric W. Biederman 
317464964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task)
31758823c079SEric W. Biederman {
317658be2825SAl Viro 	struct ns_common *ns = NULL;
31778823c079SEric W. Biederman 	struct nsproxy *nsproxy;
31788823c079SEric W. Biederman 
3179728dba3aSEric W. Biederman 	task_lock(task);
3180728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
31818823c079SEric W. Biederman 	if (nsproxy) {
318258be2825SAl Viro 		ns = &nsproxy->mnt_ns->ns;
318358be2825SAl Viro 		get_mnt_ns(to_mnt_ns(ns));
31848823c079SEric W. Biederman 	}
3185728dba3aSEric W. Biederman 	task_unlock(task);
31868823c079SEric W. Biederman 
31878823c079SEric W. Biederman 	return ns;
31888823c079SEric W. Biederman }
31898823c079SEric W. Biederman 
319064964528SAl Viro static void mntns_put(struct ns_common *ns)
31918823c079SEric W. Biederman {
319258be2825SAl Viro 	put_mnt_ns(to_mnt_ns(ns));
31938823c079SEric W. Biederman }
31948823c079SEric W. Biederman 
319564964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
31968823c079SEric W. Biederman {
31978823c079SEric W. Biederman 	struct fs_struct *fs = current->fs;
319858be2825SAl Viro 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
31998823c079SEric W. Biederman 	struct path root;
32008823c079SEric W. Biederman 
32010c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3202c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3203c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
3204ae11e0f1SZhao Hongjiang 		return -EPERM;
32058823c079SEric W. Biederman 
32068823c079SEric W. Biederman 	if (fs->users != 1)
32078823c079SEric W. Biederman 		return -EINVAL;
32088823c079SEric W. Biederman 
32098823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
32108823c079SEric W. Biederman 	put_mnt_ns(nsproxy->mnt_ns);
32118823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
32128823c079SEric W. Biederman 
32138823c079SEric W. Biederman 	/* Find the root */
32148823c079SEric W. Biederman 	root.mnt    = &mnt_ns->root->mnt;
32158823c079SEric W. Biederman 	root.dentry = mnt_ns->root->mnt.mnt_root;
32168823c079SEric W. Biederman 	path_get(&root);
32178823c079SEric W. Biederman 	while(d_mountpoint(root.dentry) && follow_down_one(&root))
32188823c079SEric W. Biederman 		;
32198823c079SEric W. Biederman 
32208823c079SEric W. Biederman 	/* Update the pwd and root */
32218823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
32228823c079SEric W. Biederman 	set_fs_root(fs, &root);
32238823c079SEric W. Biederman 
32248823c079SEric W. Biederman 	path_put(&root);
32258823c079SEric W. Biederman 	return 0;
32268823c079SEric W. Biederman }
32278823c079SEric W. Biederman 
32288823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
32298823c079SEric W. Biederman 	.name		= "mnt",
32308823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
32318823c079SEric W. Biederman 	.get		= mntns_get,
32328823c079SEric W. Biederman 	.put		= mntns_put,
32338823c079SEric W. Biederman 	.install	= mntns_install,
32348823c079SEric W. Biederman };
3235