xref: /openbmc/linux/fs/namespace.c (revision 12a5b529)
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>
2607b20889SRam Pai #include "pnode.h"
27948730b0SAdrian Bunk #include "internal.h"
281da177e4SLinus Torvalds 
290818bf27SAl Viro static unsigned int m_hash_mask __read_mostly;
300818bf27SAl Viro static unsigned int m_hash_shift __read_mostly;
310818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly;
320818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly;
330818bf27SAl Viro 
340818bf27SAl Viro static __initdata unsigned long mhash_entries;
350818bf27SAl Viro static int __init set_mhash_entries(char *str)
360818bf27SAl Viro {
370818bf27SAl Viro 	if (!str)
380818bf27SAl Viro 		return 0;
390818bf27SAl Viro 	mhash_entries = simple_strtoul(str, &str, 0);
400818bf27SAl Viro 	return 1;
410818bf27SAl Viro }
420818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries);
430818bf27SAl Viro 
440818bf27SAl Viro static __initdata unsigned long mphash_entries;
450818bf27SAl Viro static int __init set_mphash_entries(char *str)
460818bf27SAl Viro {
470818bf27SAl Viro 	if (!str)
480818bf27SAl Viro 		return 0;
490818bf27SAl Viro 	mphash_entries = simple_strtoul(str, &str, 0);
500818bf27SAl Viro 	return 1;
510818bf27SAl Viro }
520818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries);
5313f14b4dSEric Dumazet 
54c7999c36SAl Viro static u64 event;
5573cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
56719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
5799b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock);
58f21f6220SAl Viro static int mnt_id_start = 0;
59f21f6220SAl Viro static int mnt_group_start = 1;
605addc5ddSAl Viro 
6138129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly;
620818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly;
63e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
6459aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem);
651da177e4SLinus Torvalds 
66f87fd4c2SMiklos Szeredi /* /sys/fs */
6700d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
6800d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
69f87fd4c2SMiklos Szeredi 
7099b7db7bSNick Piggin /*
7199b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
7299b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
7399b7db7bSNick Piggin  * up the tree.
7499b7db7bSNick Piggin  *
7599b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
7699b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
7799b7db7bSNick Piggin  */
7848a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
7999b7db7bSNick Piggin 
8038129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
831da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
840818bf27SAl Viro 	tmp = tmp + (tmp >> m_hash_shift);
850818bf27SAl Viro 	return &mount_hashtable[tmp & m_hash_mask];
860818bf27SAl Viro }
870818bf27SAl Viro 
880818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry)
890818bf27SAl Viro {
900818bf27SAl Viro 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
910818bf27SAl Viro 	tmp = tmp + (tmp >> mp_hash_shift);
920818bf27SAl Viro 	return &mountpoint_hashtable[tmp & mp_hash_mask];
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
9599b7db7bSNick Piggin /*
9699b7db7bSNick Piggin  * allocation is serialized by namespace_sem, but we need the spinlock to
9799b7db7bSNick Piggin  * serialize with freeing.
9899b7db7bSNick Piggin  */
99b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt)
10073cd49ecSMiklos Szeredi {
10173cd49ecSMiklos Szeredi 	int res;
10273cd49ecSMiklos Szeredi 
10373cd49ecSMiklos Szeredi retry:
10473cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
10599b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
10615169fe7SAl Viro 	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
107f21f6220SAl Viro 	if (!res)
10815169fe7SAl Viro 		mnt_id_start = mnt->mnt_id + 1;
10999b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
11073cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
11173cd49ecSMiklos Szeredi 		goto retry;
11273cd49ecSMiklos Szeredi 
11373cd49ecSMiklos Szeredi 	return res;
11473cd49ecSMiklos Szeredi }
11573cd49ecSMiklos Szeredi 
116b105e270SAl Viro static void mnt_free_id(struct mount *mnt)
11773cd49ecSMiklos Szeredi {
11815169fe7SAl Viro 	int id = mnt->mnt_id;
11999b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
120f21f6220SAl Viro 	ida_remove(&mnt_id_ida, id);
121f21f6220SAl Viro 	if (mnt_id_start > id)
122f21f6220SAl Viro 		mnt_id_start = id;
12399b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
12473cd49ecSMiklos Szeredi }
12573cd49ecSMiklos Szeredi 
126719f5d7fSMiklos Szeredi /*
127719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
128719f5d7fSMiklos Szeredi  *
129719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
130719f5d7fSMiklos Szeredi  */
1314b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt)
132719f5d7fSMiklos Szeredi {
133f21f6220SAl Viro 	int res;
134f21f6220SAl Viro 
135719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
136719f5d7fSMiklos Szeredi 		return -ENOMEM;
137719f5d7fSMiklos Szeredi 
138f21f6220SAl Viro 	res = ida_get_new_above(&mnt_group_ida,
139f21f6220SAl Viro 				mnt_group_start,
14015169fe7SAl Viro 				&mnt->mnt_group_id);
141f21f6220SAl Viro 	if (!res)
14215169fe7SAl Viro 		mnt_group_start = mnt->mnt_group_id + 1;
143f21f6220SAl Viro 
144f21f6220SAl Viro 	return res;
145719f5d7fSMiklos Szeredi }
146719f5d7fSMiklos Szeredi 
147719f5d7fSMiklos Szeredi /*
148719f5d7fSMiklos Szeredi  * Release a peer group ID
149719f5d7fSMiklos Szeredi  */
1504b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
151719f5d7fSMiklos Szeredi {
15215169fe7SAl Viro 	int id = mnt->mnt_group_id;
153f21f6220SAl Viro 	ida_remove(&mnt_group_ida, id);
154f21f6220SAl Viro 	if (mnt_group_start > id)
155f21f6220SAl Viro 		mnt_group_start = id;
15615169fe7SAl Viro 	mnt->mnt_group_id = 0;
157719f5d7fSMiklos Szeredi }
158719f5d7fSMiklos Szeredi 
159b3e19d92SNick Piggin /*
160b3e19d92SNick Piggin  * vfsmount lock must be held for read
161b3e19d92SNick Piggin  */
16283adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n)
163b3e19d92SNick Piggin {
164b3e19d92SNick Piggin #ifdef CONFIG_SMP
16568e8a9feSAl Viro 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
166b3e19d92SNick Piggin #else
167b3e19d92SNick Piggin 	preempt_disable();
16868e8a9feSAl Viro 	mnt->mnt_count += n;
169b3e19d92SNick Piggin 	preempt_enable();
170b3e19d92SNick Piggin #endif
171b3e19d92SNick Piggin }
172b3e19d92SNick Piggin 
173b3e19d92SNick Piggin /*
174b3e19d92SNick Piggin  * vfsmount lock must be held for write
175b3e19d92SNick Piggin  */
17683adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt)
177b3e19d92SNick Piggin {
178b3e19d92SNick Piggin #ifdef CONFIG_SMP
179f03c6599SAl Viro 	unsigned int count = 0;
180b3e19d92SNick Piggin 	int cpu;
181b3e19d92SNick Piggin 
182b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
18368e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
184b3e19d92SNick Piggin 	}
185b3e19d92SNick Piggin 
186b3e19d92SNick Piggin 	return count;
187b3e19d92SNick Piggin #else
18868e8a9feSAl Viro 	return mnt->mnt_count;
189b3e19d92SNick Piggin #endif
190b3e19d92SNick Piggin }
191b3e19d92SNick Piggin 
192b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name)
1931da177e4SLinus Torvalds {
194c63181e6SAl Viro 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
195c63181e6SAl Viro 	if (mnt) {
19673cd49ecSMiklos Szeredi 		int err;
19773cd49ecSMiklos Szeredi 
198c63181e6SAl Viro 		err = mnt_alloc_id(mnt);
19988b38782SLi Zefan 		if (err)
20088b38782SLi Zefan 			goto out_free_cache;
20188b38782SLi Zefan 
20288b38782SLi Zefan 		if (name) {
203c63181e6SAl Viro 			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
204c63181e6SAl Viro 			if (!mnt->mnt_devname)
20588b38782SLi Zefan 				goto out_free_id;
20673cd49ecSMiklos Szeredi 		}
20773cd49ecSMiklos Szeredi 
208b3e19d92SNick Piggin #ifdef CONFIG_SMP
209c63181e6SAl Viro 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
210c63181e6SAl Viro 		if (!mnt->mnt_pcp)
211b3e19d92SNick Piggin 			goto out_free_devname;
212b3e19d92SNick Piggin 
213c63181e6SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
214b3e19d92SNick Piggin #else
215c63181e6SAl Viro 		mnt->mnt_count = 1;
216c63181e6SAl Viro 		mnt->mnt_writers = 0;
217b3e19d92SNick Piggin #endif
218b3e19d92SNick Piggin 
21938129a13SAl Viro 		INIT_HLIST_NODE(&mnt->mnt_hash);
220c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_child);
221c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_mounts);
222c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_list);
223c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_expire);
224c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_share);
225c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
226c63181e6SAl Viro 		INIT_LIST_HEAD(&mnt->mnt_slave);
2272504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY
2282504c5d6SAndreas Gruenbacher 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
2292504c5d6SAndreas Gruenbacher #endif
2301da177e4SLinus Torvalds 	}
231c63181e6SAl Viro 	return mnt;
23288b38782SLi Zefan 
233d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
234d3ef3d73Snpiggin@suse.de out_free_devname:
235c63181e6SAl Viro 	kfree(mnt->mnt_devname);
236d3ef3d73Snpiggin@suse.de #endif
23788b38782SLi Zefan out_free_id:
238c63181e6SAl Viro 	mnt_free_id(mnt);
23988b38782SLi Zefan out_free_cache:
240c63181e6SAl Viro 	kmem_cache_free(mnt_cache, mnt);
24188b38782SLi Zefan 	return NULL;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2448366025eSDave Hansen /*
2458366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2468366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2478366025eSDave Hansen  * We must keep track of when those operations start
2488366025eSDave Hansen  * (for permission checks) and when they end, so that
2498366025eSDave Hansen  * we can determine when writes are able to occur to
2508366025eSDave Hansen  * a filesystem.
2518366025eSDave Hansen  */
2523d733633SDave Hansen /*
2533d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2543d733633SDave Hansen  * @mnt: the mount to check for its write status
2553d733633SDave Hansen  *
2563d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2573d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2583d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2593d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2603d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2613d733633SDave Hansen  * r/w.
2623d733633SDave Hansen  */
2633d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2643d733633SDave Hansen {
2652e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2662e4b7fcdSDave Hansen 		return 1;
2672e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
2682e4b7fcdSDave Hansen 		return 1;
2692e4b7fcdSDave Hansen 	return 0;
2703d733633SDave Hansen }
2713d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2723d733633SDave Hansen 
27383adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt)
2743d733633SDave Hansen {
275d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
27668e8a9feSAl Viro 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
277d3ef3d73Snpiggin@suse.de #else
27868e8a9feSAl Viro 	mnt->mnt_writers++;
279d3ef3d73Snpiggin@suse.de #endif
2803d733633SDave Hansen }
2813d733633SDave Hansen 
28283adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt)
2833d733633SDave Hansen {
284d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
28568e8a9feSAl Viro 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
286d3ef3d73Snpiggin@suse.de #else
28768e8a9feSAl Viro 	mnt->mnt_writers--;
288d3ef3d73Snpiggin@suse.de #endif
289d3ef3d73Snpiggin@suse.de }
290d3ef3d73Snpiggin@suse.de 
29183adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt)
292d3ef3d73Snpiggin@suse.de {
293d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
294d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2953d733633SDave Hansen 	int cpu;
2963d733633SDave Hansen 
2973d733633SDave Hansen 	for_each_possible_cpu(cpu) {
29868e8a9feSAl Viro 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
2993d733633SDave Hansen 	}
3003d733633SDave Hansen 
301d3ef3d73Snpiggin@suse.de 	return count;
302d3ef3d73Snpiggin@suse.de #else
303d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
304d3ef3d73Snpiggin@suse.de #endif
3053d733633SDave Hansen }
3063d733633SDave Hansen 
3074ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt)
3084ed5e82fSMiklos Szeredi {
3094ed5e82fSMiklos Szeredi 	if (mnt->mnt_sb->s_readonly_remount)
3104ed5e82fSMiklos Szeredi 		return 1;
3114ed5e82fSMiklos Szeredi 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
3124ed5e82fSMiklos Szeredi 	smp_rmb();
3134ed5e82fSMiklos Szeredi 	return __mnt_is_readonly(mnt);
3144ed5e82fSMiklos Szeredi }
3154ed5e82fSMiklos Szeredi 
3163d733633SDave Hansen /*
317eb04c282SJan Kara  * Most r/o & frozen checks on a fs are for operations that take discrete
318eb04c282SJan Kara  * amounts of time, like a write() or unlink().  We must keep track of when
319eb04c282SJan Kara  * those operations start (for permission checks) and when they end, so that we
320eb04c282SJan Kara  * can determine when writes are able to occur to a filesystem.
3213d733633SDave Hansen  */
3228366025eSDave Hansen /**
323eb04c282SJan Kara  * __mnt_want_write - get write access to a mount without freeze protection
32483adc753SAl Viro  * @m: the mount on which to take a write
3258366025eSDave Hansen  *
326eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
327eb04c282SJan Kara  * it, and makes sure that writes are allowed (mnt it read-write) before
328eb04c282SJan Kara  * returning success. This operation does not protect against filesystem being
329eb04c282SJan Kara  * frozen. When the write operation is finished, __mnt_drop_write() must be
330eb04c282SJan Kara  * called. This is effectively a refcount.
3318366025eSDave Hansen  */
332eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m)
3338366025eSDave Hansen {
33483adc753SAl Viro 	struct mount *mnt = real_mount(m);
3353d733633SDave Hansen 	int ret = 0;
3363d733633SDave Hansen 
337d3ef3d73Snpiggin@suse.de 	preempt_disable();
338c6653a83SNick Piggin 	mnt_inc_writers(mnt);
339d3ef3d73Snpiggin@suse.de 	/*
340c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
341d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
342d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
343d3ef3d73Snpiggin@suse.de 	 */
344d3ef3d73Snpiggin@suse.de 	smp_mb();
3451e75529eSMiao Xie 	while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
346d3ef3d73Snpiggin@suse.de 		cpu_relax();
347d3ef3d73Snpiggin@suse.de 	/*
348d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
349d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
350d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
351d3ef3d73Snpiggin@suse.de 	 */
352d3ef3d73Snpiggin@suse.de 	smp_rmb();
3534ed5e82fSMiklos Szeredi 	if (mnt_is_readonly(m)) {
354c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3553d733633SDave Hansen 		ret = -EROFS;
3563d733633SDave Hansen 	}
357d3ef3d73Snpiggin@suse.de 	preempt_enable();
358eb04c282SJan Kara 
359eb04c282SJan Kara 	return ret;
360eb04c282SJan Kara }
361eb04c282SJan Kara 
362eb04c282SJan Kara /**
363eb04c282SJan Kara  * mnt_want_write - get write access to a mount
364eb04c282SJan Kara  * @m: the mount on which to take a write
365eb04c282SJan Kara  *
366eb04c282SJan Kara  * This tells the low-level filesystem that a write is about to be performed to
367eb04c282SJan Kara  * it, and makes sure that writes are allowed (mount is read-write, filesystem
368eb04c282SJan Kara  * is not frozen) before returning success.  When the write operation is
369eb04c282SJan Kara  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
370eb04c282SJan Kara  */
371eb04c282SJan Kara int mnt_want_write(struct vfsmount *m)
372eb04c282SJan Kara {
373eb04c282SJan Kara 	int ret;
374eb04c282SJan Kara 
375eb04c282SJan Kara 	sb_start_write(m->mnt_sb);
376eb04c282SJan Kara 	ret = __mnt_want_write(m);
377eb04c282SJan Kara 	if (ret)
378eb04c282SJan Kara 		sb_end_write(m->mnt_sb);
3793d733633SDave Hansen 	return ret;
3808366025eSDave Hansen }
3818366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3828366025eSDave Hansen 
3838366025eSDave Hansen /**
38496029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
38596029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
38696029c4eSnpiggin@suse.de  *
38796029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
38896029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
38996029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
39096029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
39196029c4eSnpiggin@suse.de  *
39296029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
39396029c4eSnpiggin@suse.de  * drop the reference.
39496029c4eSnpiggin@suse.de  */
39596029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
39696029c4eSnpiggin@suse.de {
39796029c4eSnpiggin@suse.de 	/* superblock may be r/o */
39896029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
39996029c4eSnpiggin@suse.de 		return -EROFS;
40096029c4eSnpiggin@suse.de 	preempt_disable();
40183adc753SAl Viro 	mnt_inc_writers(real_mount(mnt));
40296029c4eSnpiggin@suse.de 	preempt_enable();
40396029c4eSnpiggin@suse.de 	return 0;
40496029c4eSnpiggin@suse.de }
40596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
40696029c4eSnpiggin@suse.de 
40796029c4eSnpiggin@suse.de /**
408eb04c282SJan Kara  * __mnt_want_write_file - get write access to a file's mount
409eb04c282SJan Kara  * @file: the file who's mount on which to take a write
410eb04c282SJan Kara  *
411eb04c282SJan Kara  * This is like __mnt_want_write, but it takes a file and can
412eb04c282SJan Kara  * do some optimisations if the file is open for write already
413eb04c282SJan Kara  */
414eb04c282SJan Kara int __mnt_want_write_file(struct file *file)
415eb04c282SJan Kara {
41683f936c7SAl Viro 	if (!(file->f_mode & FMODE_WRITER))
417eb04c282SJan Kara 		return __mnt_want_write(file->f_path.mnt);
418eb04c282SJan Kara 	else
419eb04c282SJan Kara 		return mnt_clone_write(file->f_path.mnt);
420eb04c282SJan Kara }
421eb04c282SJan Kara 
422eb04c282SJan Kara /**
42396029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
42496029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
42596029c4eSnpiggin@suse.de  *
42696029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
42796029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
42896029c4eSnpiggin@suse.de  */
42996029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
43096029c4eSnpiggin@suse.de {
431eb04c282SJan Kara 	int ret;
432eb04c282SJan Kara 
433eb04c282SJan Kara 	sb_start_write(file->f_path.mnt->mnt_sb);
434eb04c282SJan Kara 	ret = __mnt_want_write_file(file);
435eb04c282SJan Kara 	if (ret)
436eb04c282SJan Kara 		sb_end_write(file->f_path.mnt->mnt_sb);
437eb04c282SJan Kara 	return ret;
43896029c4eSnpiggin@suse.de }
43996029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
44096029c4eSnpiggin@suse.de 
44196029c4eSnpiggin@suse.de /**
442eb04c282SJan Kara  * __mnt_drop_write - give up write access to a mount
4438366025eSDave Hansen  * @mnt: the mount on which to give up write access
4448366025eSDave Hansen  *
4458366025eSDave Hansen  * Tells the low-level filesystem that we are done
4468366025eSDave Hansen  * performing writes to it.  Must be matched with
447eb04c282SJan Kara  * __mnt_want_write() call above.
4488366025eSDave Hansen  */
449eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt)
4508366025eSDave Hansen {
451d3ef3d73Snpiggin@suse.de 	preempt_disable();
45283adc753SAl Viro 	mnt_dec_writers(real_mount(mnt));
453d3ef3d73Snpiggin@suse.de 	preempt_enable();
4548366025eSDave Hansen }
455eb04c282SJan Kara 
456eb04c282SJan Kara /**
457eb04c282SJan Kara  * mnt_drop_write - give up write access to a mount
458eb04c282SJan Kara  * @mnt: the mount on which to give up write access
459eb04c282SJan Kara  *
460eb04c282SJan Kara  * Tells the low-level filesystem that we are done performing writes to it and
461eb04c282SJan Kara  * also allows filesystem to be frozen again.  Must be matched with
462eb04c282SJan Kara  * mnt_want_write() call above.
463eb04c282SJan Kara  */
464eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt)
465eb04c282SJan Kara {
466eb04c282SJan Kara 	__mnt_drop_write(mnt);
467eb04c282SJan Kara 	sb_end_write(mnt->mnt_sb);
468eb04c282SJan Kara }
4698366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4708366025eSDave Hansen 
471eb04c282SJan Kara void __mnt_drop_write_file(struct file *file)
472eb04c282SJan Kara {
473eb04c282SJan Kara 	__mnt_drop_write(file->f_path.mnt);
474eb04c282SJan Kara }
475eb04c282SJan Kara 
4762a79f17eSAl Viro void mnt_drop_write_file(struct file *file)
4772a79f17eSAl Viro {
4782a79f17eSAl Viro 	mnt_drop_write(file->f_path.mnt);
4792a79f17eSAl Viro }
4802a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4812a79f17eSAl Viro 
48283adc753SAl Viro static int mnt_make_readonly(struct mount *mnt)
4838366025eSDave Hansen {
4843d733633SDave Hansen 	int ret = 0;
4853d733633SDave Hansen 
486719ea2fbSAl Viro 	lock_mount_hash();
48783adc753SAl Viro 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
488d3ef3d73Snpiggin@suse.de 	/*
489d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
490d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
491d3ef3d73Snpiggin@suse.de 	 */
492d3ef3d73Snpiggin@suse.de 	smp_mb();
493d3ef3d73Snpiggin@suse.de 
494d3ef3d73Snpiggin@suse.de 	/*
495d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
496d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
497d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
498d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
499d3ef3d73Snpiggin@suse.de 	 *
500d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
501d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
502d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
503d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
504d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
505d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
506d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
507d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
508d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
509d3ef3d73Snpiggin@suse.de 	 */
510c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
511d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
512d3ef3d73Snpiggin@suse.de 	else
51383adc753SAl Viro 		mnt->mnt.mnt_flags |= MNT_READONLY;
514d3ef3d73Snpiggin@suse.de 	/*
515d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
516d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
517d3ef3d73Snpiggin@suse.de 	 */
518d3ef3d73Snpiggin@suse.de 	smp_wmb();
51983adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
520719ea2fbSAl Viro 	unlock_mount_hash();
5213d733633SDave Hansen 	return ret;
5223d733633SDave Hansen }
5238366025eSDave Hansen 
52483adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt)
5252e4b7fcdSDave Hansen {
526719ea2fbSAl Viro 	lock_mount_hash();
52783adc753SAl Viro 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
528719ea2fbSAl Viro 	unlock_mount_hash();
5292e4b7fcdSDave Hansen }
5302e4b7fcdSDave Hansen 
5314ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb)
5324ed5e82fSMiklos Szeredi {
5334ed5e82fSMiklos Szeredi 	struct mount *mnt;
5344ed5e82fSMiklos Szeredi 	int err = 0;
5354ed5e82fSMiklos Szeredi 
5368e8b8796SMiklos Szeredi 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
5378e8b8796SMiklos Szeredi 	if (atomic_long_read(&sb->s_remove_count))
5388e8b8796SMiklos Szeredi 		return -EBUSY;
5398e8b8796SMiklos Szeredi 
540719ea2fbSAl Viro 	lock_mount_hash();
5414ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5424ed5e82fSMiklos Szeredi 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
5434ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
5444ed5e82fSMiklos Szeredi 			smp_mb();
5454ed5e82fSMiklos Szeredi 			if (mnt_get_writers(mnt) > 0) {
5464ed5e82fSMiklos Szeredi 				err = -EBUSY;
5474ed5e82fSMiklos Szeredi 				break;
5484ed5e82fSMiklos Szeredi 			}
5494ed5e82fSMiklos Szeredi 		}
5504ed5e82fSMiklos Szeredi 	}
5518e8b8796SMiklos Szeredi 	if (!err && atomic_long_read(&sb->s_remove_count))
5528e8b8796SMiklos Szeredi 		err = -EBUSY;
5538e8b8796SMiklos Szeredi 
5544ed5e82fSMiklos Szeredi 	if (!err) {
5554ed5e82fSMiklos Szeredi 		sb->s_readonly_remount = 1;
5564ed5e82fSMiklos Szeredi 		smp_wmb();
5574ed5e82fSMiklos Szeredi 	}
5584ed5e82fSMiklos Szeredi 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
5594ed5e82fSMiklos Szeredi 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
5604ed5e82fSMiklos Szeredi 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
5614ed5e82fSMiklos Szeredi 	}
562719ea2fbSAl Viro 	unlock_mount_hash();
5634ed5e82fSMiklos Szeredi 
5644ed5e82fSMiklos Szeredi 	return err;
5654ed5e82fSMiklos Szeredi }
5664ed5e82fSMiklos Szeredi 
567b105e270SAl Viro static void free_vfsmnt(struct mount *mnt)
5681da177e4SLinus Torvalds {
56952ba1621SAl Viro 	kfree(mnt->mnt_devname);
570d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
57168e8a9feSAl Viro 	free_percpu(mnt->mnt_pcp);
572d3ef3d73Snpiggin@suse.de #endif
573b105e270SAl Viro 	kmem_cache_free(mnt_cache, mnt);
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
5768ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head)
5778ffcb32eSDavid Howells {
5788ffcb32eSDavid Howells 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
5798ffcb32eSDavid Howells }
5808ffcb32eSDavid Howells 
58148a066e7SAl Viro /* call under rcu_read_lock */
58248a066e7SAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
58348a066e7SAl Viro {
58448a066e7SAl Viro 	struct mount *mnt;
58548a066e7SAl Viro 	if (read_seqretry(&mount_lock, seq))
58648a066e7SAl Viro 		return false;
58748a066e7SAl Viro 	if (bastard == NULL)
58848a066e7SAl Viro 		return true;
58948a066e7SAl Viro 	mnt = real_mount(bastard);
59048a066e7SAl Viro 	mnt_add_count(mnt, 1);
59148a066e7SAl Viro 	if (likely(!read_seqretry(&mount_lock, seq)))
59248a066e7SAl Viro 		return true;
59348a066e7SAl Viro 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
59448a066e7SAl Viro 		mnt_add_count(mnt, -1);
59548a066e7SAl Viro 		return false;
59648a066e7SAl Viro 	}
59748a066e7SAl Viro 	rcu_read_unlock();
59848a066e7SAl Viro 	mntput(bastard);
59948a066e7SAl Viro 	rcu_read_lock();
60048a066e7SAl Viro 	return false;
60148a066e7SAl Viro }
60248a066e7SAl Viro 
6031da177e4SLinus Torvalds /*
604474279dcSAl Viro  * find the first mount at @dentry on vfsmount @mnt.
60548a066e7SAl Viro  * call under rcu_read_lock()
6061da177e4SLinus Torvalds  */
607474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
6081da177e4SLinus Torvalds {
60938129a13SAl Viro 	struct hlist_head *head = m_hash(mnt, dentry);
610474279dcSAl Viro 	struct mount *p;
6111da177e4SLinus Torvalds 
61238129a13SAl Viro 	hlist_for_each_entry_rcu(p, head, mnt_hash)
613474279dcSAl Viro 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
614474279dcSAl Viro 			return p;
615474279dcSAl Viro 	return NULL;
6161da177e4SLinus Torvalds }
617474279dcSAl Viro 
618474279dcSAl Viro /*
619474279dcSAl Viro  * find the last mount at @dentry on vfsmount @mnt.
62048a066e7SAl Viro  * mount_lock must be held.
621474279dcSAl Viro  */
622474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
623474279dcSAl Viro {
62438129a13SAl Viro 	struct mount *p, *res;
62538129a13SAl Viro 	res = p = __lookup_mnt(mnt, dentry);
62638129a13SAl Viro 	if (!p)
62738129a13SAl Viro 		goto out;
62838129a13SAl Viro 	hlist_for_each_entry_continue(p, mnt_hash) {
6291d6a32acSAl Viro 		if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
6301d6a32acSAl Viro 			break;
6311d6a32acSAl Viro 		res = p;
6321d6a32acSAl Viro 	}
63338129a13SAl Viro out:
6341d6a32acSAl Viro 	return res;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
637a05964f3SRam Pai /*
638f015f126SDavid Howells  * lookup_mnt - Return the first child mount mounted at path
639f015f126SDavid Howells  *
640f015f126SDavid Howells  * "First" means first mounted chronologically.  If you create the
641f015f126SDavid Howells  * following mounts:
642f015f126SDavid Howells  *
643f015f126SDavid Howells  * mount /dev/sda1 /mnt
644f015f126SDavid Howells  * mount /dev/sda2 /mnt
645f015f126SDavid Howells  * mount /dev/sda3 /mnt
646f015f126SDavid Howells  *
647f015f126SDavid Howells  * Then lookup_mnt() on the base /mnt dentry in the root mount will
648f015f126SDavid Howells  * return successively the root dentry and vfsmount of /dev/sda1, then
649f015f126SDavid Howells  * /dev/sda2, then /dev/sda3, then NULL.
650f015f126SDavid Howells  *
651f015f126SDavid Howells  * lookup_mnt takes a reference to the found vfsmount.
652a05964f3SRam Pai  */
6531c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
654a05964f3SRam Pai {
655c7105365SAl Viro 	struct mount *child_mnt;
65648a066e7SAl Viro 	struct vfsmount *m;
65748a066e7SAl Viro 	unsigned seq;
65899b7db7bSNick Piggin 
65948a066e7SAl Viro 	rcu_read_lock();
66048a066e7SAl Viro 	do {
66148a066e7SAl Viro 		seq = read_seqbegin(&mount_lock);
662474279dcSAl Viro 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
66348a066e7SAl Viro 		m = child_mnt ? &child_mnt->mnt : NULL;
66448a066e7SAl Viro 	} while (!legitimize_mnt(m, seq));
66548a066e7SAl Viro 	rcu_read_unlock();
66648a066e7SAl Viro 	return m;
667a05964f3SRam Pai }
668a05964f3SRam Pai 
66984d17192SAl Viro static struct mountpoint *new_mountpoint(struct dentry *dentry)
67084d17192SAl Viro {
6710818bf27SAl Viro 	struct hlist_head *chain = mp_hash(dentry);
67284d17192SAl Viro 	struct mountpoint *mp;
673eed81007SMiklos Szeredi 	int ret;
67484d17192SAl Viro 
6750818bf27SAl Viro 	hlist_for_each_entry(mp, chain, m_hash) {
67684d17192SAl Viro 		if (mp->m_dentry == dentry) {
67784d17192SAl Viro 			/* might be worth a WARN_ON() */
67884d17192SAl Viro 			if (d_unlinked(dentry))
67984d17192SAl Viro 				return ERR_PTR(-ENOENT);
68084d17192SAl Viro 			mp->m_count++;
68184d17192SAl Viro 			return mp;
68284d17192SAl Viro 		}
68384d17192SAl Viro 	}
68484d17192SAl Viro 
68584d17192SAl Viro 	mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
68684d17192SAl Viro 	if (!mp)
68784d17192SAl Viro 		return ERR_PTR(-ENOMEM);
68884d17192SAl Viro 
689eed81007SMiklos Szeredi 	ret = d_set_mounted(dentry);
690eed81007SMiklos Szeredi 	if (ret) {
69184d17192SAl Viro 		kfree(mp);
692eed81007SMiklos Szeredi 		return ERR_PTR(ret);
69384d17192SAl Viro 	}
694eed81007SMiklos Szeredi 
69584d17192SAl Viro 	mp->m_dentry = dentry;
69684d17192SAl Viro 	mp->m_count = 1;
6970818bf27SAl Viro 	hlist_add_head(&mp->m_hash, chain);
69884d17192SAl Viro 	return mp;
69984d17192SAl Viro }
70084d17192SAl Viro 
70184d17192SAl Viro static void put_mountpoint(struct mountpoint *mp)
70284d17192SAl Viro {
70384d17192SAl Viro 	if (!--mp->m_count) {
70484d17192SAl Viro 		struct dentry *dentry = mp->m_dentry;
70584d17192SAl Viro 		spin_lock(&dentry->d_lock);
70684d17192SAl Viro 		dentry->d_flags &= ~DCACHE_MOUNTED;
70784d17192SAl Viro 		spin_unlock(&dentry->d_lock);
7080818bf27SAl Viro 		hlist_del(&mp->m_hash);
70984d17192SAl Viro 		kfree(mp);
71084d17192SAl Viro 	}
71184d17192SAl Viro }
71284d17192SAl Viro 
713143c8c91SAl Viro static inline int check_mnt(struct mount *mnt)
7141da177e4SLinus Torvalds {
7156b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
71899b7db7bSNick Piggin /*
71999b7db7bSNick Piggin  * vfsmount lock must be held for write
72099b7db7bSNick Piggin  */
7216b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
7225addc5ddSAl Viro {
7235addc5ddSAl Viro 	if (ns) {
7245addc5ddSAl Viro 		ns->event = ++event;
7255addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7265addc5ddSAl Viro 	}
7275addc5ddSAl Viro }
7285addc5ddSAl Viro 
72999b7db7bSNick Piggin /*
73099b7db7bSNick Piggin  * vfsmount lock must be held for write
73199b7db7bSNick Piggin  */
7326b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
7335addc5ddSAl Viro {
7345addc5ddSAl Viro 	if (ns && ns->event != event) {
7355addc5ddSAl Viro 		ns->event = event;
7365addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
7375addc5ddSAl Viro 	}
7385addc5ddSAl Viro }
7395addc5ddSAl Viro 
74099b7db7bSNick Piggin /*
74199b7db7bSNick Piggin  * vfsmount lock must be held for write
74299b7db7bSNick Piggin  */
743419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path)
7441da177e4SLinus Torvalds {
745a73324daSAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
7460714a533SAl Viro 	old_path->mnt = &mnt->mnt_parent->mnt;
7470714a533SAl Viro 	mnt->mnt_parent = mnt;
748a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
7496b41d536SAl Viro 	list_del_init(&mnt->mnt_child);
75038129a13SAl Viro 	hlist_del_init_rcu(&mnt->mnt_hash);
75184d17192SAl Viro 	put_mountpoint(mnt->mnt_mp);
75284d17192SAl Viro 	mnt->mnt_mp = NULL;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
75599b7db7bSNick Piggin /*
75699b7db7bSNick Piggin  * vfsmount lock must be held for write
75799b7db7bSNick Piggin  */
75884d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt,
75984d17192SAl Viro 			struct mountpoint *mp,
76044d964d6SAl Viro 			struct mount *child_mnt)
761b90fa9aeSRam Pai {
76284d17192SAl Viro 	mp->m_count++;
7633a2393d7SAl Viro 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
76484d17192SAl Viro 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
7653a2393d7SAl Viro 	child_mnt->mnt_parent = mnt;
76684d17192SAl Viro 	child_mnt->mnt_mp = mp;
767b90fa9aeSRam Pai }
768b90fa9aeSRam Pai 
76999b7db7bSNick Piggin /*
77099b7db7bSNick Piggin  * vfsmount lock must be held for write
77199b7db7bSNick Piggin  */
77284d17192SAl Viro static void attach_mnt(struct mount *mnt,
77384d17192SAl Viro 			struct mount *parent,
77484d17192SAl Viro 			struct mountpoint *mp)
7751da177e4SLinus Torvalds {
77684d17192SAl Viro 	mnt_set_mountpoint(parent, mp, mnt);
77738129a13SAl Viro 	hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
77884d17192SAl Viro 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
779b90fa9aeSRam Pai }
780b90fa9aeSRam Pai 
78112a5b529SAl Viro static void attach_shadowed(struct mount *mnt,
78212a5b529SAl Viro 			struct mount *parent,
78312a5b529SAl Viro 			struct mount *shadows)
78412a5b529SAl Viro {
78512a5b529SAl Viro 	if (shadows) {
78612a5b529SAl Viro 		hlist_add_after_rcu(&shadows->mnt_hash, &mnt->mnt_hash);
78712a5b529SAl Viro 		list_add(&mnt->mnt_child, &shadows->mnt_child);
78812a5b529SAl Viro 	} else {
78912a5b529SAl Viro 		hlist_add_head_rcu(&mnt->mnt_hash,
79012a5b529SAl Viro 				m_hash(&parent->mnt, mnt->mnt_mountpoint));
79112a5b529SAl Viro 		list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
79212a5b529SAl Viro 	}
79312a5b529SAl Viro }
79412a5b529SAl Viro 
795b90fa9aeSRam Pai /*
79699b7db7bSNick Piggin  * vfsmount lock must be held for write
797b90fa9aeSRam Pai  */
7981d6a32acSAl Viro static void commit_tree(struct mount *mnt, struct mount *shadows)
799b90fa9aeSRam Pai {
8000714a533SAl Viro 	struct mount *parent = mnt->mnt_parent;
80183adc753SAl Viro 	struct mount *m;
802b90fa9aeSRam Pai 	LIST_HEAD(head);
803143c8c91SAl Viro 	struct mnt_namespace *n = parent->mnt_ns;
804b90fa9aeSRam Pai 
8050714a533SAl Viro 	BUG_ON(parent == mnt);
806b90fa9aeSRam Pai 
8071a4eeaf2SAl Viro 	list_add_tail(&head, &mnt->mnt_list);
808f7a99c5bSAl Viro 	list_for_each_entry(m, &head, mnt_list)
809143c8c91SAl Viro 		m->mnt_ns = n;
810f03c6599SAl Viro 
811b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
812b90fa9aeSRam Pai 
81312a5b529SAl Viro 	attach_shadowed(mnt, parent, shadows);
8146b3286edSKirill Korotaev 	touch_mnt_namespace(n);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
817909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root)
8181da177e4SLinus Torvalds {
8196b41d536SAl Viro 	struct list_head *next = p->mnt_mounts.next;
8206b41d536SAl Viro 	if (next == &p->mnt_mounts) {
8211da177e4SLinus Torvalds 		while (1) {
822909b0a88SAl Viro 			if (p == root)
8231da177e4SLinus Torvalds 				return NULL;
8246b41d536SAl Viro 			next = p->mnt_child.next;
8256b41d536SAl Viro 			if (next != &p->mnt_parent->mnt_mounts)
8261da177e4SLinus Torvalds 				break;
8270714a533SAl Viro 			p = p->mnt_parent;
8281da177e4SLinus Torvalds 		}
8291da177e4SLinus Torvalds 	}
8306b41d536SAl Viro 	return list_entry(next, struct mount, mnt_child);
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
833315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
8349676f0c6SRam Pai {
8356b41d536SAl Viro 	struct list_head *prev = p->mnt_mounts.prev;
8366b41d536SAl Viro 	while (prev != &p->mnt_mounts) {
8376b41d536SAl Viro 		p = list_entry(prev, struct mount, mnt_child);
8386b41d536SAl Viro 		prev = p->mnt_mounts.prev;
8399676f0c6SRam Pai 	}
8409676f0c6SRam Pai 	return p;
8419676f0c6SRam Pai }
8429676f0c6SRam Pai 
8439d412a43SAl Viro struct vfsmount *
8449d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
8459d412a43SAl Viro {
846b105e270SAl Viro 	struct mount *mnt;
8479d412a43SAl Viro 	struct dentry *root;
8489d412a43SAl Viro 
8499d412a43SAl Viro 	if (!type)
8509d412a43SAl Viro 		return ERR_PTR(-ENODEV);
8519d412a43SAl Viro 
8529d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
8539d412a43SAl Viro 	if (!mnt)
8549d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
8559d412a43SAl Viro 
8569d412a43SAl Viro 	if (flags & MS_KERNMOUNT)
857b105e270SAl Viro 		mnt->mnt.mnt_flags = MNT_INTERNAL;
8589d412a43SAl Viro 
8599d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
8609d412a43SAl Viro 	if (IS_ERR(root)) {
8618ffcb32eSDavid Howells 		mnt_free_id(mnt);
8629d412a43SAl Viro 		free_vfsmnt(mnt);
8639d412a43SAl Viro 		return ERR_CAST(root);
8649d412a43SAl Viro 	}
8659d412a43SAl Viro 
866b105e270SAl Viro 	mnt->mnt.mnt_root = root;
867b105e270SAl Viro 	mnt->mnt.mnt_sb = root->d_sb;
868a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8690714a533SAl Viro 	mnt->mnt_parent = mnt;
870719ea2fbSAl Viro 	lock_mount_hash();
87139f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
872719ea2fbSAl Viro 	unlock_mount_hash();
873b105e270SAl Viro 	return &mnt->mnt;
8749d412a43SAl Viro }
8759d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
8769d412a43SAl Viro 
87787129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root,
87836341f64SRam Pai 					int flag)
8791da177e4SLinus Torvalds {
88087129cc0SAl Viro 	struct super_block *sb = old->mnt.mnt_sb;
881be34d1a3SDavid Howells 	struct mount *mnt;
882be34d1a3SDavid Howells 	int err;
8831da177e4SLinus Torvalds 
884be34d1a3SDavid Howells 	mnt = alloc_vfsmnt(old->mnt_devname);
885be34d1a3SDavid Howells 	if (!mnt)
886be34d1a3SDavid Howells 		return ERR_PTR(-ENOMEM);
887be34d1a3SDavid Howells 
8887a472ef4SEric W. Biederman 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
88915169fe7SAl Viro 		mnt->mnt_group_id = 0; /* not a peer of original */
890719f5d7fSMiklos Szeredi 	else
89115169fe7SAl Viro 		mnt->mnt_group_id = old->mnt_group_id;
892719f5d7fSMiklos Szeredi 
89315169fe7SAl Viro 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
894be34d1a3SDavid Howells 		err = mnt_alloc_group_id(mnt);
895719f5d7fSMiklos Szeredi 		if (err)
896719f5d7fSMiklos Szeredi 			goto out_free;
897719f5d7fSMiklos Szeredi 	}
898719f5d7fSMiklos Szeredi 
899f2ebb3a9SAl Viro 	mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
900132c94e3SEric W. Biederman 	/* Don't allow unprivileged users to change mount flags */
901132c94e3SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY))
902132c94e3SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
903132c94e3SEric W. Biederman 
9045ff9d8a6SEric W. Biederman 	/* Don't allow unprivileged users to reveal what is under a mount */
9055ff9d8a6SEric W. Biederman 	if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire))
9065ff9d8a6SEric W. Biederman 		mnt->mnt.mnt_flags |= MNT_LOCKED;
9075ff9d8a6SEric W. Biederman 
9081da177e4SLinus Torvalds 	atomic_inc(&sb->s_active);
909b105e270SAl Viro 	mnt->mnt.mnt_sb = sb;
910b105e270SAl Viro 	mnt->mnt.mnt_root = dget(root);
911a73324daSAl Viro 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
9120714a533SAl Viro 	mnt->mnt_parent = mnt;
913719ea2fbSAl Viro 	lock_mount_hash();
91439f7c4dbSMiklos Szeredi 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
915719ea2fbSAl Viro 	unlock_mount_hash();
916b90fa9aeSRam Pai 
9177a472ef4SEric W. Biederman 	if ((flag & CL_SLAVE) ||
9187a472ef4SEric W. Biederman 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
9196776db3dSAl Viro 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
92032301920SAl Viro 		mnt->mnt_master = old;
921fc7be130SAl Viro 		CLEAR_MNT_SHARED(mnt);
9228aec0809SAl Viro 	} else if (!(flag & CL_PRIVATE)) {
923fc7be130SAl Viro 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
9246776db3dSAl Viro 			list_add(&mnt->mnt_share, &old->mnt_share);
925d10e8defSAl Viro 		if (IS_MNT_SLAVE(old))
9266776db3dSAl Viro 			list_add(&mnt->mnt_slave, &old->mnt_slave);
927d10e8defSAl Viro 		mnt->mnt_master = old->mnt_master;
9285afe0022SRam Pai 	}
929b90fa9aeSRam Pai 	if (flag & CL_MAKE_SHARED)
9300f0afb1dSAl Viro 		set_mnt_shared(mnt);
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	/* stick the duplicate mount on the same expiry list
9331da177e4SLinus Torvalds 	 * as the original if that was on one */
93436341f64SRam Pai 	if (flag & CL_EXPIRE) {
9356776db3dSAl Viro 		if (!list_empty(&old->mnt_expire))
9366776db3dSAl Viro 			list_add(&mnt->mnt_expire, &old->mnt_expire);
9371da177e4SLinus Torvalds 	}
938be34d1a3SDavid Howells 
939cb338d06SAl Viro 	return mnt;
940719f5d7fSMiklos Szeredi 
941719f5d7fSMiklos Szeredi  out_free:
9428ffcb32eSDavid Howells 	mnt_free_id(mnt);
943719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
944be34d1a3SDavid Howells 	return ERR_PTR(err);
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
947900148dcSAl Viro static void mntput_no_expire(struct mount *mnt)
9487b7b1aceSAl Viro {
94948a066e7SAl Viro 	rcu_read_lock();
950aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
95148a066e7SAl Viro 	if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
95248a066e7SAl Viro 		rcu_read_unlock();
95399b7db7bSNick Piggin 		return;
954b3e19d92SNick Piggin 	}
955719ea2fbSAl Viro 	lock_mount_hash();
956b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
95748a066e7SAl Viro 		rcu_read_unlock();
958719ea2fbSAl Viro 		unlock_mount_hash();
95999b7db7bSNick Piggin 		return;
96099b7db7bSNick Piggin 	}
96148a066e7SAl Viro 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
96248a066e7SAl Viro 		rcu_read_unlock();
96348a066e7SAl Viro 		unlock_mount_hash();
96448a066e7SAl Viro 		return;
96548a066e7SAl Viro 	}
96648a066e7SAl Viro 	mnt->mnt.mnt_flags |= MNT_DOOMED;
96748a066e7SAl Viro 	rcu_read_unlock();
968962830dfSAndi Kleen 
96939f7c4dbSMiklos Szeredi 	list_del(&mnt->mnt_instance);
970719ea2fbSAl Viro 	unlock_mount_hash();
971649a795aSAl Viro 
972649a795aSAl Viro 	/*
973649a795aSAl Viro 	 * This probably indicates that somebody messed
974649a795aSAl Viro 	 * up a mnt_want/drop_write() pair.  If this
975649a795aSAl Viro 	 * happens, the filesystem was probably unable
976649a795aSAl Viro 	 * to make r/w->r/o transitions.
977649a795aSAl Viro 	 */
978649a795aSAl Viro 	/*
979649a795aSAl Viro 	 * The locking used to deal with mnt_count decrement provides barriers,
980649a795aSAl Viro 	 * so mnt_get_writers() below is safe.
981649a795aSAl Viro 	 */
982649a795aSAl Viro 	WARN_ON(mnt_get_writers(mnt));
9833064c356SAl Viro 	if (unlikely(mnt->mnt_pins.first))
9843064c356SAl Viro 		mnt_pin_kill(mnt);
985649a795aSAl Viro 	fsnotify_vfsmount_delete(&mnt->mnt);
986649a795aSAl Viro 	dput(mnt->mnt.mnt_root);
987649a795aSAl Viro 	deactivate_super(mnt->mnt.mnt_sb);
98848a066e7SAl Viro 	mnt_free_id(mnt);
9898ffcb32eSDavid Howells 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
990b3e19d92SNick Piggin }
991b3e19d92SNick Piggin 
992b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
993b3e19d92SNick Piggin {
994b3e19d92SNick Piggin 	if (mnt) {
995863d684fSAl Viro 		struct mount *m = real_mount(mnt);
996b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
997863d684fSAl Viro 		if (unlikely(m->mnt_expiry_mark))
998863d684fSAl Viro 			m->mnt_expiry_mark = 0;
999863d684fSAl Viro 		mntput_no_expire(m);
1000b3e19d92SNick Piggin 	}
1001b3e19d92SNick Piggin }
1002b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
1003b3e19d92SNick Piggin 
1004b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
1005b3e19d92SNick Piggin {
1006b3e19d92SNick Piggin 	if (mnt)
100783adc753SAl Viro 		mnt_add_count(real_mount(mnt), 1);
1008b3e19d92SNick Piggin 	return mnt;
1009b3e19d92SNick Piggin }
1010b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
1011b3e19d92SNick Piggin 
10123064c356SAl Viro struct vfsmount *mnt_clone_internal(struct path *path)
10137b7b1aceSAl Viro {
10143064c356SAl Viro 	struct mount *p;
10153064c356SAl Viro 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
10163064c356SAl Viro 	if (IS_ERR(p))
10173064c356SAl Viro 		return ERR_CAST(p);
10183064c356SAl Viro 	p->mnt.mnt_flags |= MNT_INTERNAL;
10193064c356SAl Viro 	return &p->mnt;
10207b7b1aceSAl Viro }
10211da177e4SLinus Torvalds 
1022b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
1023b3b304a2SMiklos Szeredi {
1024b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
1025b3b304a2SMiklos Szeredi }
1026b3b304a2SMiklos Szeredi 
1027b3b304a2SMiklos Szeredi /*
1028b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
1029b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
1030b3b304a2SMiklos Szeredi  *
1031b3b304a2SMiklos Szeredi  * See also save_mount_options().
1032b3b304a2SMiklos Szeredi  */
103334c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root)
1034b3b304a2SMiklos Szeredi {
10352a32cebdSAl Viro 	const char *options;
10362a32cebdSAl Viro 
10372a32cebdSAl Viro 	rcu_read_lock();
103834c80b1dSAl Viro 	options = rcu_dereference(root->d_sb->s_options);
1039b3b304a2SMiklos Szeredi 
1040b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
1041b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
1042b3b304a2SMiklos Szeredi 		mangle(m, options);
1043b3b304a2SMiklos Szeredi 	}
10442a32cebdSAl Viro 	rcu_read_unlock();
1045b3b304a2SMiklos Szeredi 
1046b3b304a2SMiklos Szeredi 	return 0;
1047b3b304a2SMiklos Szeredi }
1048b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
1049b3b304a2SMiklos Szeredi 
1050b3b304a2SMiklos Szeredi /*
1051b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
1052b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
1053b3b304a2SMiklos Szeredi  *
1054b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
1055b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
1056b3b304a2SMiklos Szeredi  * remount fails.
1057b3b304a2SMiklos Szeredi  *
1058b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
1059b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
1060b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
1061b3b304a2SMiklos Szeredi  * any more.
1062b3b304a2SMiklos Szeredi  */
1063b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
1064b3b304a2SMiklos Szeredi {
10652a32cebdSAl Viro 	BUG_ON(sb->s_options);
10662a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1067b3b304a2SMiklos Szeredi }
1068b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
1069b3b304a2SMiklos Szeredi 
10702a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
10712a32cebdSAl Viro {
10722a32cebdSAl Viro 	char *old = sb->s_options;
10732a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
10742a32cebdSAl Viro 	if (old) {
10752a32cebdSAl Viro 		synchronize_rcu();
10762a32cebdSAl Viro 		kfree(old);
10772a32cebdSAl Viro 	}
10782a32cebdSAl Viro }
10792a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
10802a32cebdSAl Viro 
1081a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
10820226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */
10831da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
10841da177e4SLinus Torvalds {
10856ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
10861da177e4SLinus Torvalds 
1087390c6843SRam Pai 	down_read(&namespace_sem);
1088c7999c36SAl Viro 	if (p->cached_event == p->ns->event) {
1089c7999c36SAl Viro 		void *v = p->cached_mount;
1090c7999c36SAl Viro 		if (*pos == p->cached_index)
1091c7999c36SAl Viro 			return v;
1092c7999c36SAl Viro 		if (*pos == p->cached_index + 1) {
1093c7999c36SAl Viro 			v = seq_list_next(v, &p->ns->list, &p->cached_index);
1094c7999c36SAl Viro 			return p->cached_mount = v;
1095c7999c36SAl Viro 		}
1096c7999c36SAl Viro 	}
1097c7999c36SAl Viro 
1098c7999c36SAl Viro 	p->cached_event = p->ns->event;
1099c7999c36SAl Viro 	p->cached_mount = seq_list_start(&p->ns->list, *pos);
1100c7999c36SAl Viro 	p->cached_index = *pos;
1101c7999c36SAl Viro 	return p->cached_mount;
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
11051da177e4SLinus Torvalds {
11066ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
1107b0765fb8SPavel Emelianov 
1108c7999c36SAl Viro 	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1109c7999c36SAl Viro 	p->cached_index = *pos;
1110c7999c36SAl Viro 	return p->cached_mount;
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
11141da177e4SLinus Torvalds {
1115390c6843SRam Pai 	up_read(&namespace_sem);
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
11180226f492SAl Viro static int m_show(struct seq_file *m, void *v)
11199f5596afSAl Viro {
11206ce6e24eSAl Viro 	struct proc_mounts *p = proc_mounts(m);
11211a4eeaf2SAl Viro 	struct mount *r = list_entry(v, struct mount, mnt_list);
11220226f492SAl Viro 	return p->show(m, &r->mnt);
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
1125a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
11261da177e4SLinus Torvalds 	.start	= m_start,
11271da177e4SLinus Torvalds 	.next	= m_next,
11281da177e4SLinus Torvalds 	.stop	= m_stop,
11290226f492SAl Viro 	.show	= m_show,
1130b4629fe2SChuck Lever };
1131a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1132b4629fe2SChuck Lever 
11331da177e4SLinus Torvalds /**
11341da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
11351da177e4SLinus Torvalds  * @mnt: root of mount tree
11361da177e4SLinus Torvalds  *
11371da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
11381da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
11391da177e4SLinus Torvalds  * busy.
11401da177e4SLinus Torvalds  */
1141909b0a88SAl Viro int may_umount_tree(struct vfsmount *m)
11421da177e4SLinus Torvalds {
1143909b0a88SAl Viro 	struct mount *mnt = real_mount(m);
114436341f64SRam Pai 	int actual_refs = 0;
114536341f64SRam Pai 	int minimum_refs = 0;
1146315fc83eSAl Viro 	struct mount *p;
1147909b0a88SAl Viro 	BUG_ON(!m);
11481da177e4SLinus Torvalds 
1149b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1150719ea2fbSAl Viro 	lock_mount_hash();
1151909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
115283adc753SAl Viro 		actual_refs += mnt_get_count(p);
11531da177e4SLinus Torvalds 		minimum_refs += 2;
11541da177e4SLinus Torvalds 	}
1155719ea2fbSAl Viro 	unlock_mount_hash();
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
11581da177e4SLinus Torvalds 		return 0;
1159e3474a8eSIan Kent 
1160e3474a8eSIan Kent 	return 1;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds /**
11661da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
11671da177e4SLinus Torvalds  * @mnt: root of mount
11681da177e4SLinus Torvalds  *
11691da177e4SLinus Torvalds  * This is called to check if a mount point has any
11701da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
11711da177e4SLinus Torvalds  * mount has sub mounts this will return busy
11721da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
11731da177e4SLinus Torvalds  *
11741da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
11751da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
11761da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
11771da177e4SLinus Torvalds  */
11781da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
11791da177e4SLinus Torvalds {
1180e3474a8eSIan Kent 	int ret = 1;
11818ad08d8aSAl Viro 	down_read(&namespace_sem);
1182719ea2fbSAl Viro 	lock_mount_hash();
11831ab59738SAl Viro 	if (propagate_mount_busy(real_mount(mnt), 2))
1184e3474a8eSIan Kent 		ret = 0;
1185719ea2fbSAl Viro 	unlock_mount_hash();
11868ad08d8aSAl Viro 	up_read(&namespace_sem);
1187a05964f3SRam Pai 	return ret;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
11911da177e4SLinus Torvalds 
119238129a13SAl Viro static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
1193e3197d83SAl Viro 
119497216be0SAl Viro static void namespace_unlock(void)
11951da177e4SLinus Torvalds {
1196d5e50f74SAl Viro 	struct mount *mnt;
119738129a13SAl Viro 	struct hlist_head head = unmounted;
119897216be0SAl Viro 
119938129a13SAl Viro 	if (likely(hlist_empty(&head))) {
120097216be0SAl Viro 		up_write(&namespace_sem);
120197216be0SAl Viro 		return;
120297216be0SAl Viro 	}
120397216be0SAl Viro 
120438129a13SAl Viro 	head.first->pprev = &head.first;
120538129a13SAl Viro 	INIT_HLIST_HEAD(&unmounted);
120638129a13SAl Viro 
120797216be0SAl Viro 	up_write(&namespace_sem);
120897216be0SAl Viro 
120948a066e7SAl Viro 	synchronize_rcu();
121048a066e7SAl Viro 
121138129a13SAl Viro 	while (!hlist_empty(&head)) {
121238129a13SAl Viro 		mnt = hlist_entry(head.first, struct mount, mnt_hash);
121338129a13SAl Viro 		hlist_del_init(&mnt->mnt_hash);
1214aba809cfSAl Viro 		if (mnt->mnt_ex_mountpoint.mnt)
1215aba809cfSAl Viro 			path_put(&mnt->mnt_ex_mountpoint);
1216d5e50f74SAl Viro 		mntput(&mnt->mnt);
121770fbcdf4SRam Pai 	}
121870fbcdf4SRam Pai }
121970fbcdf4SRam Pai 
122097216be0SAl Viro static inline void namespace_lock(void)
1221e3197d83SAl Viro {
122297216be0SAl Viro 	down_write(&namespace_sem);
1223e3197d83SAl Viro }
1224e3197d83SAl Viro 
122599b7db7bSNick Piggin /*
122648a066e7SAl Viro  * mount_lock must be held
122799b7db7bSNick Piggin  * namespace_sem must be held for write
122848a066e7SAl Viro  * how = 0 => just this tree, don't propagate
122948a066e7SAl Viro  * how = 1 => propagate; we know that nobody else has reference to any victims
123048a066e7SAl Viro  * how = 2 => lazy umount
123199b7db7bSNick Piggin  */
123248a066e7SAl Viro void umount_tree(struct mount *mnt, int how)
123370fbcdf4SRam Pai {
123438129a13SAl Viro 	HLIST_HEAD(tmp_list);
1235315fc83eSAl Viro 	struct mount *p;
123638129a13SAl Viro 	struct mount *last = NULL;
123770fbcdf4SRam Pai 
123838129a13SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
123938129a13SAl Viro 		hlist_del_init_rcu(&p->mnt_hash);
124038129a13SAl Viro 		hlist_add_head(&p->mnt_hash, &tmp_list);
124138129a13SAl Viro 	}
124270fbcdf4SRam Pai 
124348a066e7SAl Viro 	if (how)
12447b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1245a05964f3SRam Pai 
124638129a13SAl Viro 	hlist_for_each_entry(p, &tmp_list, mnt_hash) {
12476776db3dSAl Viro 		list_del_init(&p->mnt_expire);
12481a4eeaf2SAl Viro 		list_del_init(&p->mnt_list);
1249143c8c91SAl Viro 		__touch_mnt_namespace(p->mnt_ns);
125063d37a84SAl Viro 		p->mnt_ns = NULL;
125148a066e7SAl Viro 		if (how < 2)
125248a066e7SAl Viro 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
12536b41d536SAl Viro 		list_del_init(&p->mnt_child);
1254676da58dSAl Viro 		if (mnt_has_parent(p)) {
125584d17192SAl Viro 			put_mountpoint(p->mnt_mp);
1256aba809cfSAl Viro 			/* move the reference to mountpoint into ->mnt_ex_mountpoint */
1257aba809cfSAl Viro 			p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint;
1258aba809cfSAl Viro 			p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt;
1259aba809cfSAl Viro 			p->mnt_mountpoint = p->mnt.mnt_root;
1260aba809cfSAl Viro 			p->mnt_parent = p;
126184d17192SAl Viro 			p->mnt_mp = NULL;
12627c4b93d8SAl Viro 		}
12630f0afb1dSAl Viro 		change_mnt_propagation(p, MS_PRIVATE);
126438129a13SAl Viro 		last = p;
12651da177e4SLinus Torvalds 	}
126638129a13SAl Viro 	if (last) {
126738129a13SAl Viro 		last->mnt_hash.next = unmounted.first;
126838129a13SAl Viro 		unmounted.first = tmp_list.first;
126938129a13SAl Viro 		unmounted.first->pprev = &unmounted.first;
127038129a13SAl Viro 	}
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
1273b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt);
1274c35038beSAl Viro 
12751ab59738SAl Viro static int do_umount(struct mount *mnt, int flags)
12761da177e4SLinus Torvalds {
12771ab59738SAl Viro 	struct super_block *sb = mnt->mnt.mnt_sb;
12781da177e4SLinus Torvalds 	int retval;
12791da177e4SLinus Torvalds 
12801ab59738SAl Viro 	retval = security_sb_umount(&mnt->mnt, flags);
12811da177e4SLinus Torvalds 	if (retval)
12821da177e4SLinus Torvalds 		return retval;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	/*
12851da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
12861da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
12871da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
12881da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
12891da177e4SLinus Torvalds 	 */
12901da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
12911ab59738SAl Viro 		if (&mnt->mnt == current->fs->root.mnt ||
12921da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
12931da177e4SLinus Torvalds 			return -EINVAL;
12941da177e4SLinus Torvalds 
1295b3e19d92SNick Piggin 		/*
1296b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1297b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1298b3e19d92SNick Piggin 		 */
1299719ea2fbSAl Viro 		lock_mount_hash();
130083adc753SAl Viro 		if (mnt_get_count(mnt) != 2) {
1301719ea2fbSAl Viro 			unlock_mount_hash();
13021da177e4SLinus Torvalds 			return -EBUSY;
1303b3e19d92SNick Piggin 		}
1304719ea2fbSAl Viro 		unlock_mount_hash();
13051da177e4SLinus Torvalds 
1306863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1))
13071da177e4SLinus Torvalds 			return -EAGAIN;
13081da177e4SLinus Torvalds 	}
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	/*
13111da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
13121da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
13131da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
13141da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
13151da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
13161da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
13171da177e4SLinus Torvalds 	 * about for the moment.
13181da177e4SLinus Torvalds 	 */
13191da177e4SLinus Torvalds 
132042faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
132142faad99SAl Viro 		sb->s_op->umount_begin(sb);
132242faad99SAl Viro 	}
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	/*
13251da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
13261da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
13271da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
13281da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
13291da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
13301da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
13311da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
13321da177e4SLinus Torvalds 	 */
13331ab59738SAl Viro 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
13341da177e4SLinus Torvalds 		/*
13351da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
13361da177e4SLinus Torvalds 		 * we just try to remount it readonly.
13371da177e4SLinus Torvalds 		 */
13381da177e4SLinus Torvalds 		down_write(&sb->s_umount);
13394aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
13401da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
13411da177e4SLinus Torvalds 		up_write(&sb->s_umount);
13421da177e4SLinus Torvalds 		return retval;
13431da177e4SLinus Torvalds 	}
13441da177e4SLinus Torvalds 
134597216be0SAl Viro 	namespace_lock();
1346719ea2fbSAl Viro 	lock_mount_hash();
13475addc5ddSAl Viro 	event++;
13481da177e4SLinus Torvalds 
134948a066e7SAl Viro 	if (flags & MNT_DETACH) {
135048a066e7SAl Viro 		if (!list_empty(&mnt->mnt_list))
135148a066e7SAl Viro 			umount_tree(mnt, 2);
135248a066e7SAl Viro 		retval = 0;
135348a066e7SAl Viro 	} else {
1354b54b9be7SAl Viro 		shrink_submounts(mnt);
13551da177e4SLinus Torvalds 		retval = -EBUSY;
135648a066e7SAl Viro 		if (!propagate_mount_busy(mnt, 2)) {
13571a4eeaf2SAl Viro 			if (!list_empty(&mnt->mnt_list))
1358328e6d90SAl Viro 				umount_tree(mnt, 1);
13591da177e4SLinus Torvalds 			retval = 0;
13601da177e4SLinus Torvalds 		}
136148a066e7SAl Viro 	}
1362719ea2fbSAl Viro 	unlock_mount_hash();
1363e3197d83SAl Viro 	namespace_unlock();
13641da177e4SLinus Torvalds 	return retval;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds /*
13689b40bc90SAl Viro  * Is the caller allowed to modify his namespace?
13699b40bc90SAl Viro  */
13709b40bc90SAl Viro static inline bool may_mount(void)
13719b40bc90SAl Viro {
13729b40bc90SAl Viro 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
13739b40bc90SAl Viro }
13749b40bc90SAl Viro 
13759b40bc90SAl Viro /*
13761da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
13771da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
13781da177e4SLinus Torvalds  *
13791da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
13801da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
13811da177e4SLinus Torvalds  */
13821da177e4SLinus Torvalds 
1383bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
13841da177e4SLinus Torvalds {
13852d8f3038SAl Viro 	struct path path;
1386900148dcSAl Viro 	struct mount *mnt;
13871da177e4SLinus Torvalds 	int retval;
1388db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
13891da177e4SLinus Torvalds 
1390db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1391db1f05bbSMiklos Szeredi 		return -EINVAL;
1392db1f05bbSMiklos Szeredi 
13939b40bc90SAl Viro 	if (!may_mount())
13949b40bc90SAl Viro 		return -EPERM;
13959b40bc90SAl Viro 
1396db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1397db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1398db1f05bbSMiklos Szeredi 
1399197df04cSAl Viro 	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
14001da177e4SLinus Torvalds 	if (retval)
14011da177e4SLinus Torvalds 		goto out;
1402900148dcSAl Viro 	mnt = real_mount(path.mnt);
14031da177e4SLinus Torvalds 	retval = -EINVAL;
14042d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
14051da177e4SLinus Torvalds 		goto dput_and_out;
1406143c8c91SAl Viro 	if (!check_mnt(mnt))
14071da177e4SLinus Torvalds 		goto dput_and_out;
14085ff9d8a6SEric W. Biederman 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
14095ff9d8a6SEric W. Biederman 		goto dput_and_out;
14101da177e4SLinus Torvalds 
1411900148dcSAl Viro 	retval = do_umount(mnt, flags);
14121da177e4SLinus Torvalds dput_and_out:
1413429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
14142d8f3038SAl Viro 	dput(path.dentry);
1415900148dcSAl Viro 	mntput_no_expire(mnt);
14161da177e4SLinus Torvalds out:
14171da177e4SLinus Torvalds 	return retval;
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds /*
14231da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
14241da177e4SLinus Torvalds  */
1425bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
14261da177e4SLinus Torvalds {
14271da177e4SLinus Torvalds 	return sys_umount(name, 0);
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds #endif
14311da177e4SLinus Torvalds 
14324ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry)
14338823c079SEric W. Biederman {
14344ce5d2b1SEric W. Biederman 	/* Is this a proxy for a mount namespace? */
14354ce5d2b1SEric W. Biederman 	struct inode *inode = dentry->d_inode;
14360bb80f24SDavid Howells 	struct proc_ns *ei;
14378823c079SEric W. Biederman 
14388823c079SEric W. Biederman 	if (!proc_ns_inode(inode))
14398823c079SEric W. Biederman 		return false;
14408823c079SEric W. Biederman 
14410bb80f24SDavid Howells 	ei = get_proc_ns(inode);
14428823c079SEric W. Biederman 	if (ei->ns_ops != &mntns_operations)
14438823c079SEric W. Biederman 		return false;
14448823c079SEric W. Biederman 
14454ce5d2b1SEric W. Biederman 	return true;
14464ce5d2b1SEric W. Biederman }
14474ce5d2b1SEric W. Biederman 
14484ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry)
14494ce5d2b1SEric W. Biederman {
14504ce5d2b1SEric W. Biederman 	/* Could bind mounting the mount namespace inode cause a
14514ce5d2b1SEric W. Biederman 	 * mount namespace loop?
14524ce5d2b1SEric W. Biederman 	 */
14534ce5d2b1SEric W. Biederman 	struct mnt_namespace *mnt_ns;
14544ce5d2b1SEric W. Biederman 	if (!is_mnt_ns_file(dentry))
14554ce5d2b1SEric W. Biederman 		return false;
14564ce5d2b1SEric W. Biederman 
14574ce5d2b1SEric W. Biederman 	mnt_ns = get_proc_ns(dentry->d_inode)->ns;
14588823c079SEric W. Biederman 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
14598823c079SEric W. Biederman }
14608823c079SEric W. Biederman 
146187129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
146236341f64SRam Pai 					int flag)
14631da177e4SLinus Torvalds {
146484d17192SAl Viro 	struct mount *res, *p, *q, *r, *parent;
14651da177e4SLinus Torvalds 
14664ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
14674ce5d2b1SEric W. Biederman 		return ERR_PTR(-EINVAL);
14684ce5d2b1SEric W. Biederman 
14694ce5d2b1SEric W. Biederman 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1470be34d1a3SDavid Howells 		return ERR_PTR(-EINVAL);
14719676f0c6SRam Pai 
147236341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
1473be34d1a3SDavid Howells 	if (IS_ERR(q))
1474be34d1a3SDavid Howells 		return q;
1475be34d1a3SDavid Howells 
14765ff9d8a6SEric W. Biederman 	q->mnt.mnt_flags &= ~MNT_LOCKED;
1477a73324daSAl Viro 	q->mnt_mountpoint = mnt->mnt_mountpoint;
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	p = mnt;
14806b41d536SAl Viro 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1481315fc83eSAl Viro 		struct mount *s;
14827ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
14831da177e4SLinus Torvalds 			continue;
14841da177e4SLinus Torvalds 
1485909b0a88SAl Viro 		for (s = r; s; s = next_mnt(s, r)) {
148612a5b529SAl Viro 			struct mount *t = NULL;
14874ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_UNBINDABLE) &&
14884ce5d2b1SEric W. Biederman 			    IS_MNT_UNBINDABLE(s)) {
14894ce5d2b1SEric W. Biederman 				s = skip_mnt_tree(s);
14904ce5d2b1SEric W. Biederman 				continue;
14914ce5d2b1SEric W. Biederman 			}
14924ce5d2b1SEric W. Biederman 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
14934ce5d2b1SEric W. Biederman 			    is_mnt_ns_file(s->mnt.mnt_root)) {
14949676f0c6SRam Pai 				s = skip_mnt_tree(s);
14959676f0c6SRam Pai 				continue;
14969676f0c6SRam Pai 			}
14970714a533SAl Viro 			while (p != s->mnt_parent) {
14980714a533SAl Viro 				p = p->mnt_parent;
14990714a533SAl Viro 				q = q->mnt_parent;
15001da177e4SLinus Torvalds 			}
150187129cc0SAl Viro 			p = s;
150284d17192SAl Viro 			parent = q;
150387129cc0SAl Viro 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1504be34d1a3SDavid Howells 			if (IS_ERR(q))
1505be34d1a3SDavid Howells 				goto out;
1506719ea2fbSAl Viro 			lock_mount_hash();
15071a4eeaf2SAl Viro 			list_add_tail(&q->mnt_list, &res->mnt_list);
150812a5b529SAl Viro 			mnt_set_mountpoint(parent, p->mnt_mp, q);
150912a5b529SAl Viro 			if (!list_empty(&parent->mnt_mounts)) {
151012a5b529SAl Viro 				t = list_last_entry(&parent->mnt_mounts,
151112a5b529SAl Viro 					struct mount, mnt_child);
151212a5b529SAl Viro 				if (t->mnt_mp != p->mnt_mp)
151312a5b529SAl Viro 					t = NULL;
151412a5b529SAl Viro 			}
151512a5b529SAl Viro 			attach_shadowed(q, parent, t);
1516719ea2fbSAl Viro 			unlock_mount_hash();
15171da177e4SLinus Torvalds 		}
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 	return res;
1520be34d1a3SDavid Howells out:
15211da177e4SLinus Torvalds 	if (res) {
1522719ea2fbSAl Viro 		lock_mount_hash();
1523328e6d90SAl Viro 		umount_tree(res, 0);
1524719ea2fbSAl Viro 		unlock_mount_hash();
15251da177e4SLinus Torvalds 	}
1526be34d1a3SDavid Howells 	return q;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
1529be34d1a3SDavid Howells /* Caller should check returned pointer for errors */
1530be34d1a3SDavid Howells 
1531589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
15328aec0809SAl Viro {
1533cb338d06SAl Viro 	struct mount *tree;
153497216be0SAl Viro 	namespace_lock();
153587129cc0SAl Viro 	tree = copy_tree(real_mount(path->mnt), path->dentry,
153687129cc0SAl Viro 			 CL_COPY_ALL | CL_PRIVATE);
1537328e6d90SAl Viro 	namespace_unlock();
1538be34d1a3SDavid Howells 	if (IS_ERR(tree))
153952e220d3SDan Carpenter 		return ERR_CAST(tree);
1540be34d1a3SDavid Howells 	return &tree->mnt;
15418aec0809SAl Viro }
15428aec0809SAl Viro 
15438aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
15448aec0809SAl Viro {
154597216be0SAl Viro 	namespace_lock();
1546719ea2fbSAl Viro 	lock_mount_hash();
1547328e6d90SAl Viro 	umount_tree(real_mount(mnt), 0);
1548719ea2fbSAl Viro 	unlock_mount_hash();
15493ab6abeeSAl Viro 	namespace_unlock();
15508aec0809SAl Viro }
15518aec0809SAl Viro 
15521f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
15531f707137SAl Viro 		   struct vfsmount *root)
15541f707137SAl Viro {
15551a4eeaf2SAl Viro 	struct mount *mnt;
15561f707137SAl Viro 	int res = f(root, arg);
15571f707137SAl Viro 	if (res)
15581f707137SAl Viro 		return res;
15591a4eeaf2SAl Viro 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
15601a4eeaf2SAl Viro 		res = f(&mnt->mnt, arg);
15611f707137SAl Viro 		if (res)
15621f707137SAl Viro 			return res;
15631f707137SAl Viro 	}
15641f707137SAl Viro 	return 0;
15651f707137SAl Viro }
15661f707137SAl Viro 
15674b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1568719f5d7fSMiklos Szeredi {
1569315fc83eSAl Viro 	struct mount *p;
1570719f5d7fSMiklos Szeredi 
1571909b0a88SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1572fc7be130SAl Viro 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
15734b8b21f4SAl Viro 			mnt_release_group_id(p);
1574719f5d7fSMiklos Szeredi 	}
1575719f5d7fSMiklos Szeredi }
1576719f5d7fSMiklos Szeredi 
15774b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1578719f5d7fSMiklos Szeredi {
1579315fc83eSAl Viro 	struct mount *p;
1580719f5d7fSMiklos Szeredi 
1581909b0a88SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1582fc7be130SAl Viro 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
15834b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1584719f5d7fSMiklos Szeredi 			if (err) {
15854b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1586719f5d7fSMiklos Szeredi 				return err;
1587719f5d7fSMiklos Szeredi 			}
1588719f5d7fSMiklos Szeredi 		}
1589719f5d7fSMiklos Szeredi 	}
1590719f5d7fSMiklos Szeredi 
1591719f5d7fSMiklos Szeredi 	return 0;
1592719f5d7fSMiklos Szeredi }
1593719f5d7fSMiklos Szeredi 
1594b90fa9aeSRam Pai /*
1595b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1596b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
159721444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
159821444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
159921444403SRam Pai  *  		   (done when source_mnt is moved)
1600b90fa9aeSRam Pai  *
1601b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1602b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
16039676f0c6SRam Pai  * ---------------------------------------------------------------------------
1604b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
16059676f0c6SRam Pai  * |**************************************************************************
16069676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
16079676f0c6SRam Pai  * | dest     |               |                |                |            |
16089676f0c6SRam Pai  * |   |      |               |                |                |            |
16099676f0c6SRam Pai  * |   v      |               |                |                |            |
16109676f0c6SRam Pai  * |**************************************************************************
16119676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
16125afe0022SRam Pai  * |          |               |                |                |            |
16139676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
16149676f0c6SRam Pai  * ***************************************************************************
1615b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1616b90fa9aeSRam Pai  * destination mount.
1617b90fa9aeSRam Pai  *
1618b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1619b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1620b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1621b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1622b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1623b90fa9aeSRam Pai  *       mount.
16245afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
16255afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
16265afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
16275afe0022SRam Pai  *       is marked as 'shared and slave'.
16285afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
16295afe0022SRam Pai  * 	 source mount.
16305afe0022SRam Pai  *
16319676f0c6SRam Pai  * ---------------------------------------------------------------------------
163221444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
16339676f0c6SRam Pai  * |**************************************************************************
16349676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
16359676f0c6SRam Pai  * | dest     |               |                |                |            |
16369676f0c6SRam Pai  * |   |      |               |                |                |            |
16379676f0c6SRam Pai  * |   v      |               |                |                |            |
16389676f0c6SRam Pai  * |**************************************************************************
16399676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
16405afe0022SRam Pai  * |          |               |                |                |            |
16419676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
16429676f0c6SRam Pai  * ***************************************************************************
16435afe0022SRam Pai  *
16445afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
16455afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
164621444403SRam Pai  * (+*)  the mount is moved to the destination.
16475afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
16485afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
16495afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
16505afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1651b90fa9aeSRam Pai  *
1652b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1653b90fa9aeSRam Pai  * applied to each mount in the tree.
1654b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1655b90fa9aeSRam Pai  * in allocations.
1656b90fa9aeSRam Pai  */
16570fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt,
165884d17192SAl Viro 			struct mount *dest_mnt,
165984d17192SAl Viro 			struct mountpoint *dest_mp,
166084d17192SAl Viro 			struct path *parent_path)
1661b90fa9aeSRam Pai {
166238129a13SAl Viro 	HLIST_HEAD(tree_list);
1663315fc83eSAl Viro 	struct mount *child, *p;
166438129a13SAl Viro 	struct hlist_node *n;
1665719f5d7fSMiklos Szeredi 	int err;
1666b90fa9aeSRam Pai 
1667fc7be130SAl Viro 	if (IS_MNT_SHARED(dest_mnt)) {
16680fb54e50SAl Viro 		err = invent_group_ids(source_mnt, true);
1669719f5d7fSMiklos Szeredi 		if (err)
1670719f5d7fSMiklos Szeredi 			goto out;
167184d17192SAl Viro 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1672f2ebb3a9SAl Viro 		lock_mount_hash();
1673719f5d7fSMiklos Szeredi 		if (err)
1674719f5d7fSMiklos Szeredi 			goto out_cleanup_ids;
1675909b0a88SAl Viro 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
16760f0afb1dSAl Viro 			set_mnt_shared(p);
16770b1b901bSAl Viro 	} else {
16780b1b901bSAl Viro 		lock_mount_hash();
1679b90fa9aeSRam Pai 	}
16801a390689SAl Viro 	if (parent_path) {
16810fb54e50SAl Viro 		detach_mnt(source_mnt, parent_path);
168284d17192SAl Viro 		attach_mnt(source_mnt, dest_mnt, dest_mp);
1683143c8c91SAl Viro 		touch_mnt_namespace(source_mnt->mnt_ns);
168421444403SRam Pai 	} else {
168584d17192SAl Viro 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
16861d6a32acSAl Viro 		commit_tree(source_mnt, NULL);
168721444403SRam Pai 	}
1688b90fa9aeSRam Pai 
168938129a13SAl Viro 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
16901d6a32acSAl Viro 		struct mount *q;
169138129a13SAl Viro 		hlist_del_init(&child->mnt_hash);
16921d6a32acSAl Viro 		q = __lookup_mnt_last(&child->mnt_parent->mnt,
16931d6a32acSAl Viro 				      child->mnt_mountpoint);
16941d6a32acSAl Viro 		commit_tree(child, q);
1695b90fa9aeSRam Pai 	}
1696719ea2fbSAl Viro 	unlock_mount_hash();
169799b7db7bSNick Piggin 
1698b90fa9aeSRam Pai 	return 0;
1699719f5d7fSMiklos Szeredi 
1700719f5d7fSMiklos Szeredi  out_cleanup_ids:
1701f2ebb3a9SAl Viro 	while (!hlist_empty(&tree_list)) {
1702f2ebb3a9SAl Viro 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
1703f2ebb3a9SAl Viro 		umount_tree(child, 0);
1704f2ebb3a9SAl Viro 	}
1705f2ebb3a9SAl Viro 	unlock_mount_hash();
17060fb54e50SAl Viro 	cleanup_group_ids(source_mnt, NULL);
1707719f5d7fSMiklos Szeredi  out:
1708719f5d7fSMiklos Szeredi 	return err;
1709b90fa9aeSRam Pai }
1710b90fa9aeSRam Pai 
171184d17192SAl Viro static struct mountpoint *lock_mount(struct path *path)
1712b12cea91SAl Viro {
1713b12cea91SAl Viro 	struct vfsmount *mnt;
171484d17192SAl Viro 	struct dentry *dentry = path->dentry;
1715b12cea91SAl Viro retry:
171684d17192SAl Viro 	mutex_lock(&dentry->d_inode->i_mutex);
171784d17192SAl Viro 	if (unlikely(cant_mount(dentry))) {
171884d17192SAl Viro 		mutex_unlock(&dentry->d_inode->i_mutex);
171984d17192SAl Viro 		return ERR_PTR(-ENOENT);
1720b12cea91SAl Viro 	}
172197216be0SAl Viro 	namespace_lock();
1722b12cea91SAl Viro 	mnt = lookup_mnt(path);
172384d17192SAl Viro 	if (likely(!mnt)) {
172484d17192SAl Viro 		struct mountpoint *mp = new_mountpoint(dentry);
172584d17192SAl Viro 		if (IS_ERR(mp)) {
172697216be0SAl Viro 			namespace_unlock();
172784d17192SAl Viro 			mutex_unlock(&dentry->d_inode->i_mutex);
172884d17192SAl Viro 			return mp;
172984d17192SAl Viro 		}
173084d17192SAl Viro 		return mp;
173184d17192SAl Viro 	}
173297216be0SAl Viro 	namespace_unlock();
1733b12cea91SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
1734b12cea91SAl Viro 	path_put(path);
1735b12cea91SAl Viro 	path->mnt = mnt;
173684d17192SAl Viro 	dentry = path->dentry = dget(mnt->mnt_root);
1737b12cea91SAl Viro 	goto retry;
1738b12cea91SAl Viro }
1739b12cea91SAl Viro 
174084d17192SAl Viro static void unlock_mount(struct mountpoint *where)
1741b12cea91SAl Viro {
174284d17192SAl Viro 	struct dentry *dentry = where->m_dentry;
174384d17192SAl Viro 	put_mountpoint(where);
1744328e6d90SAl Viro 	namespace_unlock();
174584d17192SAl Viro 	mutex_unlock(&dentry->d_inode->i_mutex);
1746b12cea91SAl Viro }
1747b12cea91SAl Viro 
174884d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
17491da177e4SLinus Torvalds {
175095bc5f25SAl Viro 	if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
17511da177e4SLinus Torvalds 		return -EINVAL;
17521da177e4SLinus Torvalds 
175384d17192SAl Viro 	if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
175495bc5f25SAl Viro 	      S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
17551da177e4SLinus Torvalds 		return -ENOTDIR;
17561da177e4SLinus Torvalds 
175784d17192SAl Viro 	return attach_recursive_mnt(mnt, p, mp, NULL);
17581da177e4SLinus Torvalds }
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds /*
17617a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
17627a2e8a8fSValerie Aurora  */
17637a2e8a8fSValerie Aurora 
17647a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags)
17657a2e8a8fSValerie Aurora {
17667c6e984dSRoman Borisov 	int type = flags & ~(MS_REC | MS_SILENT);
17677a2e8a8fSValerie Aurora 
17687a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
17697a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
17707a2e8a8fSValerie Aurora 		return 0;
17717a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
17727a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
17737a2e8a8fSValerie Aurora 		return 0;
17747a2e8a8fSValerie Aurora 	return type;
17757a2e8a8fSValerie Aurora }
17767a2e8a8fSValerie Aurora 
17777a2e8a8fSValerie Aurora /*
177807b20889SRam Pai  * recursively change the type of the mountpoint.
177907b20889SRam Pai  */
17800a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
178107b20889SRam Pai {
1782315fc83eSAl Viro 	struct mount *m;
17834b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
178407b20889SRam Pai 	int recurse = flag & MS_REC;
17857a2e8a8fSValerie Aurora 	int type;
1786719f5d7fSMiklos Szeredi 	int err = 0;
178707b20889SRam Pai 
17882d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
178907b20889SRam Pai 		return -EINVAL;
179007b20889SRam Pai 
17917a2e8a8fSValerie Aurora 	type = flags_to_propagation_type(flag);
17927a2e8a8fSValerie Aurora 	if (!type)
17937a2e8a8fSValerie Aurora 		return -EINVAL;
17947a2e8a8fSValerie Aurora 
179597216be0SAl Viro 	namespace_lock();
1796719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1797719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1798719f5d7fSMiklos Szeredi 		if (err)
1799719f5d7fSMiklos Szeredi 			goto out_unlock;
1800719f5d7fSMiklos Szeredi 	}
1801719f5d7fSMiklos Szeredi 
1802719ea2fbSAl Viro 	lock_mount_hash();
1803909b0a88SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
18040f0afb1dSAl Viro 		change_mnt_propagation(m, type);
1805719ea2fbSAl Viro 	unlock_mount_hash();
1806719f5d7fSMiklos Szeredi 
1807719f5d7fSMiklos Szeredi  out_unlock:
180897216be0SAl Viro 	namespace_unlock();
1809719f5d7fSMiklos Szeredi 	return err;
181007b20889SRam Pai }
181107b20889SRam Pai 
18125ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
18135ff9d8a6SEric W. Biederman {
18145ff9d8a6SEric W. Biederman 	struct mount *child;
18155ff9d8a6SEric W. Biederman 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
18165ff9d8a6SEric W. Biederman 		if (!is_subdir(child->mnt_mountpoint, dentry))
18175ff9d8a6SEric W. Biederman 			continue;
18185ff9d8a6SEric W. Biederman 
18195ff9d8a6SEric W. Biederman 		if (child->mnt.mnt_flags & MNT_LOCKED)
18205ff9d8a6SEric W. Biederman 			return true;
18215ff9d8a6SEric W. Biederman 	}
18225ff9d8a6SEric W. Biederman 	return false;
18235ff9d8a6SEric W. Biederman }
18245ff9d8a6SEric W. Biederman 
182507b20889SRam Pai /*
18261da177e4SLinus Torvalds  * do loopback mount.
18271da177e4SLinus Torvalds  */
1828808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name,
18292dafe1c4SEric Sandeen 				int recurse)
18301da177e4SLinus Torvalds {
18312d92ab3cSAl Viro 	struct path old_path;
183284d17192SAl Viro 	struct mount *mnt = NULL, *old, *parent;
183384d17192SAl Viro 	struct mountpoint *mp;
183457eccb83SAl Viro 	int err;
18351da177e4SLinus Torvalds 	if (!old_name || !*old_name)
18361da177e4SLinus Torvalds 		return -EINVAL;
1837815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
18381da177e4SLinus Torvalds 	if (err)
18391da177e4SLinus Torvalds 		return err;
18401da177e4SLinus Torvalds 
18418823c079SEric W. Biederman 	err = -EINVAL;
18424ce5d2b1SEric W. Biederman 	if (mnt_ns_loop(old_path.dentry))
18438823c079SEric W. Biederman 		goto out;
18448823c079SEric W. Biederman 
184584d17192SAl Viro 	mp = lock_mount(path);
184684d17192SAl Viro 	err = PTR_ERR(mp);
184784d17192SAl Viro 	if (IS_ERR(mp))
18489676f0c6SRam Pai 		goto out;
18499676f0c6SRam Pai 
185087129cc0SAl Viro 	old = real_mount(old_path.mnt);
185184d17192SAl Viro 	parent = real_mount(path->mnt);
185287129cc0SAl Viro 
1853b12cea91SAl Viro 	err = -EINVAL;
1854fc7be130SAl Viro 	if (IS_MNT_UNBINDABLE(old))
1855b12cea91SAl Viro 		goto out2;
1856b12cea91SAl Viro 
185784d17192SAl Viro 	if (!check_mnt(parent) || !check_mnt(old))
1858b12cea91SAl Viro 		goto out2;
1859ccd48bc7SAl Viro 
18605ff9d8a6SEric W. Biederman 	if (!recurse && has_locked_children(old, old_path.dentry))
18615ff9d8a6SEric W. Biederman 		goto out2;
18625ff9d8a6SEric W. Biederman 
18631da177e4SLinus Torvalds 	if (recurse)
18644ce5d2b1SEric W. Biederman 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
18651da177e4SLinus Torvalds 	else
186687129cc0SAl Viro 		mnt = clone_mnt(old, old_path.dentry, 0);
18671da177e4SLinus Torvalds 
1868be34d1a3SDavid Howells 	if (IS_ERR(mnt)) {
1869be34d1a3SDavid Howells 		err = PTR_ERR(mnt);
1870e9c5d8a5SAndrey Vagin 		goto out2;
1871be34d1a3SDavid Howells 	}
1872ccd48bc7SAl Viro 
18735ff9d8a6SEric W. Biederman 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
18745ff9d8a6SEric W. Biederman 
187584d17192SAl Viro 	err = graft_tree(mnt, parent, mp);
18761da177e4SLinus Torvalds 	if (err) {
1877719ea2fbSAl Viro 		lock_mount_hash();
1878328e6d90SAl Viro 		umount_tree(mnt, 0);
1879719ea2fbSAl Viro 		unlock_mount_hash();
18805b83d2c5SRam Pai 	}
1881b12cea91SAl Viro out2:
188284d17192SAl Viro 	unlock_mount(mp);
1883ccd48bc7SAl Viro out:
18842d92ab3cSAl Viro 	path_put(&old_path);
18851da177e4SLinus Torvalds 	return err;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
18882e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
18892e4b7fcdSDave Hansen {
18902e4b7fcdSDave Hansen 	int error = 0;
18912e4b7fcdSDave Hansen 	int readonly_request = 0;
18922e4b7fcdSDave Hansen 
18932e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
18942e4b7fcdSDave Hansen 		readonly_request = 1;
18952e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
18962e4b7fcdSDave Hansen 		return 0;
18972e4b7fcdSDave Hansen 
189890563b19SEric W. Biederman 	if (mnt->mnt_flags & MNT_LOCK_READONLY)
189990563b19SEric W. Biederman 		return -EPERM;
190090563b19SEric W. Biederman 
19012e4b7fcdSDave Hansen 	if (readonly_request)
190283adc753SAl Viro 		error = mnt_make_readonly(real_mount(mnt));
19032e4b7fcdSDave Hansen 	else
190483adc753SAl Viro 		__mnt_unmake_readonly(real_mount(mnt));
19052e4b7fcdSDave Hansen 	return error;
19062e4b7fcdSDave Hansen }
19072e4b7fcdSDave Hansen 
19081da177e4SLinus Torvalds /*
19091da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
19101da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
19111da177e4SLinus Torvalds  * on it - tough luck.
19121da177e4SLinus Torvalds  */
19130a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
19141da177e4SLinus Torvalds 		      void *data)
19151da177e4SLinus Torvalds {
19161da177e4SLinus Torvalds 	int err;
19172d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
1918143c8c91SAl Viro 	struct mount *mnt = real_mount(path->mnt);
19191da177e4SLinus Torvalds 
1920143c8c91SAl Viro 	if (!check_mnt(mnt))
19211da177e4SLinus Torvalds 		return -EINVAL;
19221da177e4SLinus Torvalds 
19232d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
19241da177e4SLinus Torvalds 		return -EINVAL;
19251da177e4SLinus Torvalds 
1926ff36fe2cSEric Paris 	err = security_sb_remount(sb, data);
1927ff36fe2cSEric Paris 	if (err)
1928ff36fe2cSEric Paris 		return err;
1929ff36fe2cSEric Paris 
19301da177e4SLinus Torvalds 	down_write(&sb->s_umount);
19312e4b7fcdSDave Hansen 	if (flags & MS_BIND)
19322d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
193357eccb83SAl Viro 	else if (!capable(CAP_SYS_ADMIN))
193457eccb83SAl Viro 		err = -EPERM;
19354aa98cf7SAl Viro 	else
19361da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
19377b43a79fSAl Viro 	if (!err) {
1938719ea2fbSAl Viro 		lock_mount_hash();
1939143c8c91SAl Viro 		mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
1940143c8c91SAl Viro 		mnt->mnt.mnt_flags = mnt_flags;
1941143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1942719ea2fbSAl Viro 		unlock_mount_hash();
19430e55a7ccSDan Williams 	}
19446339dab8SAl Viro 	up_write(&sb->s_umount);
19451da177e4SLinus Torvalds 	return err;
19461da177e4SLinus Torvalds }
19471da177e4SLinus Torvalds 
1948cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt)
19499676f0c6SRam Pai {
1950315fc83eSAl Viro 	struct mount *p;
1951909b0a88SAl Viro 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1952fc7be130SAl Viro 		if (IS_MNT_UNBINDABLE(p))
19539676f0c6SRam Pai 			return 1;
19549676f0c6SRam Pai 	}
19559676f0c6SRam Pai 	return 0;
19569676f0c6SRam Pai }
19579676f0c6SRam Pai 
1958808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name)
19591da177e4SLinus Torvalds {
19602d92ab3cSAl Viro 	struct path old_path, parent_path;
1961676da58dSAl Viro 	struct mount *p;
19620fb54e50SAl Viro 	struct mount *old;
196384d17192SAl Viro 	struct mountpoint *mp;
196457eccb83SAl Viro 	int err;
19651da177e4SLinus Torvalds 	if (!old_name || !*old_name)
19661da177e4SLinus Torvalds 		return -EINVAL;
19672d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
19681da177e4SLinus Torvalds 	if (err)
19691da177e4SLinus Torvalds 		return err;
19701da177e4SLinus Torvalds 
197184d17192SAl Viro 	mp = lock_mount(path);
197284d17192SAl Viro 	err = PTR_ERR(mp);
197384d17192SAl Viro 	if (IS_ERR(mp))
1974cc53ce53SDavid Howells 		goto out;
1975cc53ce53SDavid Howells 
1976143c8c91SAl Viro 	old = real_mount(old_path.mnt);
1977fc7be130SAl Viro 	p = real_mount(path->mnt);
1978143c8c91SAl Viro 
19791da177e4SLinus Torvalds 	err = -EINVAL;
1980fc7be130SAl Viro 	if (!check_mnt(p) || !check_mnt(old))
19811da177e4SLinus Torvalds 		goto out1;
19821da177e4SLinus Torvalds 
19835ff9d8a6SEric W. Biederman 	if (old->mnt.mnt_flags & MNT_LOCKED)
19845ff9d8a6SEric W. Biederman 		goto out1;
19855ff9d8a6SEric W. Biederman 
19861da177e4SLinus Torvalds 	err = -EINVAL;
19872d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
198821444403SRam Pai 		goto out1;
19891da177e4SLinus Torvalds 
1990676da58dSAl Viro 	if (!mnt_has_parent(old))
199121444403SRam Pai 		goto out1;
19921da177e4SLinus Torvalds 
19932d92ab3cSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
19942d92ab3cSAl Viro 	      S_ISDIR(old_path.dentry->d_inode->i_mode))
199521444403SRam Pai 		goto out1;
199621444403SRam Pai 	/*
199721444403SRam Pai 	 * Don't move a mount residing in a shared parent.
199821444403SRam Pai 	 */
1999fc7be130SAl Viro 	if (IS_MNT_SHARED(old->mnt_parent))
200021444403SRam Pai 		goto out1;
20019676f0c6SRam Pai 	/*
20029676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
20039676f0c6SRam Pai 	 * mount which is shared.
20049676f0c6SRam Pai 	 */
2005fc7be130SAl Viro 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
20069676f0c6SRam Pai 		goto out1;
20071da177e4SLinus Torvalds 	err = -ELOOP;
2008fc7be130SAl Viro 	for (; mnt_has_parent(p); p = p->mnt_parent)
2009676da58dSAl Viro 		if (p == old)
201021444403SRam Pai 			goto out1;
20111da177e4SLinus Torvalds 
201284d17192SAl Viro 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
20134ac91378SJan Blunck 	if (err)
201421444403SRam Pai 		goto out1;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
20171da177e4SLinus Torvalds 	 * automatically */
20186776db3dSAl Viro 	list_del_init(&old->mnt_expire);
20191da177e4SLinus Torvalds out1:
202084d17192SAl Viro 	unlock_mount(mp);
20211da177e4SLinus Torvalds out:
20221da177e4SLinus Torvalds 	if (!err)
20231a390689SAl Viro 		path_put(&parent_path);
20242d92ab3cSAl Viro 	path_put(&old_path);
20251da177e4SLinus Torvalds 	return err;
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds 
20289d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
20299d412a43SAl Viro {
20309d412a43SAl Viro 	int err;
20319d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
20329d412a43SAl Viro 	if (subtype) {
20339d412a43SAl Viro 		subtype++;
20349d412a43SAl Viro 		err = -EINVAL;
20359d412a43SAl Viro 		if (!subtype[0])
20369d412a43SAl Viro 			goto err;
20379d412a43SAl Viro 	} else
20389d412a43SAl Viro 		subtype = "";
20399d412a43SAl Viro 
20409d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
20419d412a43SAl Viro 	err = -ENOMEM;
20429d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
20439d412a43SAl Viro 		goto err;
20449d412a43SAl Viro 	return mnt;
20459d412a43SAl Viro 
20469d412a43SAl Viro  err:
20479d412a43SAl Viro 	mntput(mnt);
20489d412a43SAl Viro 	return ERR_PTR(err);
20499d412a43SAl Viro }
20509d412a43SAl Viro 
20519d412a43SAl Viro /*
20529d412a43SAl Viro  * add a mount into a namespace's mount tree
20539d412a43SAl Viro  */
205495bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
20559d412a43SAl Viro {
205684d17192SAl Viro 	struct mountpoint *mp;
205784d17192SAl Viro 	struct mount *parent;
20589d412a43SAl Viro 	int err;
20599d412a43SAl Viro 
2060f2ebb3a9SAl Viro 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
20619d412a43SAl Viro 
206284d17192SAl Viro 	mp = lock_mount(path);
206384d17192SAl Viro 	if (IS_ERR(mp))
206484d17192SAl Viro 		return PTR_ERR(mp);
20659d412a43SAl Viro 
206684d17192SAl Viro 	parent = real_mount(path->mnt);
20679d412a43SAl Viro 	err = -EINVAL;
206884d17192SAl Viro 	if (unlikely(!check_mnt(parent))) {
2069156cacb1SAl Viro 		/* that's acceptable only for automounts done in private ns */
2070156cacb1SAl Viro 		if (!(mnt_flags & MNT_SHRINKABLE))
20719d412a43SAl Viro 			goto unlock;
2072156cacb1SAl Viro 		/* ... and for those we'd better have mountpoint still alive */
207384d17192SAl Viro 		if (!parent->mnt_ns)
2074156cacb1SAl Viro 			goto unlock;
2075156cacb1SAl Viro 	}
20769d412a43SAl Viro 
20779d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
20789d412a43SAl Viro 	err = -EBUSY;
207995bc5f25SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
20809d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
20819d412a43SAl Viro 		goto unlock;
20829d412a43SAl Viro 
20839d412a43SAl Viro 	err = -EINVAL;
208495bc5f25SAl Viro 	if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
20859d412a43SAl Viro 		goto unlock;
20869d412a43SAl Viro 
208795bc5f25SAl Viro 	newmnt->mnt.mnt_flags = mnt_flags;
208884d17192SAl Viro 	err = graft_tree(newmnt, parent, mp);
20899d412a43SAl Viro 
20909d412a43SAl Viro unlock:
209184d17192SAl Viro 	unlock_mount(mp);
20929d412a43SAl Viro 	return err;
20939d412a43SAl Viro }
2094b1e75df4SAl Viro 
20951da177e4SLinus Torvalds /*
20961da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
20971da177e4SLinus Torvalds  * namespace's tree
20981da177e4SLinus Torvalds  */
20990c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags,
2100808d4e3cSAl Viro 			int mnt_flags, const char *name, void *data)
21011da177e4SLinus Torvalds {
21020c55cfc4SEric W. Biederman 	struct file_system_type *type;
21039b40bc90SAl Viro 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
21041da177e4SLinus Torvalds 	struct vfsmount *mnt;
210515f9a3f3SAl Viro 	int err;
21061da177e4SLinus Torvalds 
21070c55cfc4SEric W. Biederman 	if (!fstype)
21081da177e4SLinus Torvalds 		return -EINVAL;
21091da177e4SLinus Torvalds 
21100c55cfc4SEric W. Biederman 	type = get_fs_type(fstype);
21110c55cfc4SEric W. Biederman 	if (!type)
21120c55cfc4SEric W. Biederman 		return -ENODEV;
21130c55cfc4SEric W. Biederman 
21140c55cfc4SEric W. Biederman 	if (user_ns != &init_user_ns) {
21150c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_MOUNT)) {
21160c55cfc4SEric W. Biederman 			put_filesystem(type);
21170c55cfc4SEric W. Biederman 			return -EPERM;
21180c55cfc4SEric W. Biederman 		}
21190c55cfc4SEric W. Biederman 		/* Only in special cases allow devices from mounts
21200c55cfc4SEric W. Biederman 		 * created outside the initial user namespace.
21210c55cfc4SEric W. Biederman 		 */
21220c55cfc4SEric W. Biederman 		if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
21230c55cfc4SEric W. Biederman 			flags |= MS_NODEV;
21240c55cfc4SEric W. Biederman 			mnt_flags |= MNT_NODEV;
21250c55cfc4SEric W. Biederman 		}
21260c55cfc4SEric W. Biederman 	}
21270c55cfc4SEric W. Biederman 
21280c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, flags, name, data);
21290c55cfc4SEric W. Biederman 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
21300c55cfc4SEric W. Biederman 	    !mnt->mnt_sb->s_subtype)
21310c55cfc4SEric W. Biederman 		mnt = fs_set_subtype(mnt, fstype);
21320c55cfc4SEric W. Biederman 
21330c55cfc4SEric W. Biederman 	put_filesystem(type);
21341da177e4SLinus Torvalds 	if (IS_ERR(mnt))
21351da177e4SLinus Torvalds 		return PTR_ERR(mnt);
21361da177e4SLinus Torvalds 
213795bc5f25SAl Viro 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
213815f9a3f3SAl Viro 	if (err)
213915f9a3f3SAl Viro 		mntput(mnt);
214015f9a3f3SAl Viro 	return err;
21411da177e4SLinus Torvalds }
21421da177e4SLinus Torvalds 
214319a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
214419a167afSAl Viro {
21456776db3dSAl Viro 	struct mount *mnt = real_mount(m);
214619a167afSAl Viro 	int err;
214719a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
214819a167afSAl Viro 	 * expired before we get a chance to add it
214919a167afSAl Viro 	 */
21506776db3dSAl Viro 	BUG_ON(mnt_get_count(mnt) < 2);
215119a167afSAl Viro 
215219a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
215319a167afSAl Viro 	    m->mnt_root == path->dentry) {
2154b1e75df4SAl Viro 		err = -ELOOP;
2155b1e75df4SAl Viro 		goto fail;
215619a167afSAl Viro 	}
215719a167afSAl Viro 
215895bc5f25SAl Viro 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2159b1e75df4SAl Viro 	if (!err)
2160b1e75df4SAl Viro 		return 0;
2161b1e75df4SAl Viro fail:
2162b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
21636776db3dSAl Viro 	if (!list_empty(&mnt->mnt_expire)) {
216497216be0SAl Viro 		namespace_lock();
21656776db3dSAl Viro 		list_del_init(&mnt->mnt_expire);
216697216be0SAl Viro 		namespace_unlock();
216719a167afSAl Viro 	}
2168b1e75df4SAl Viro 	mntput(m);
2169b1e75df4SAl Viro 	mntput(m);
217019a167afSAl Viro 	return err;
217119a167afSAl Viro }
217219a167afSAl Viro 
2173ea5b778aSDavid Howells /**
2174ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2175ea5b778aSDavid Howells  * @mnt: The mount to list.
2176ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2177ea5b778aSDavid Howells  */
2178ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2179ea5b778aSDavid Howells {
218097216be0SAl Viro 	namespace_lock();
2181ea5b778aSDavid Howells 
21826776db3dSAl Viro 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2183ea5b778aSDavid Howells 
218497216be0SAl Viro 	namespace_unlock();
2185ea5b778aSDavid Howells }
2186ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2187ea5b778aSDavid Howells 
2188ea5b778aSDavid Howells /*
21891da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
21901da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
21911da177e4SLinus Torvalds  * here
21921da177e4SLinus Torvalds  */
21931da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
21941da177e4SLinus Torvalds {
2195761d5c38SAl Viro 	struct mount *mnt, *next;
21961da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 	if (list_empty(mounts))
21991da177e4SLinus Torvalds 		return;
22001da177e4SLinus Torvalds 
220197216be0SAl Viro 	namespace_lock();
2202719ea2fbSAl Viro 	lock_mount_hash();
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
22051da177e4SLinus Torvalds 	 * following criteria:
22061da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
22071da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
22081da177e4SLinus Torvalds 	 *   cleared by mntput())
22091da177e4SLinus Torvalds 	 */
22106776db3dSAl Viro 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2211863d684fSAl Viro 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
22121ab59738SAl Viro 			propagate_mount_busy(mnt, 1))
22131da177e4SLinus Torvalds 			continue;
22146776db3dSAl Viro 		list_move(&mnt->mnt_expire, &graveyard);
22151da177e4SLinus Torvalds 	}
2216bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
22176776db3dSAl Viro 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2218143c8c91SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2219328e6d90SAl Viro 		umount_tree(mnt, 1);
2220bcc5c7d2SAl Viro 	}
2221719ea2fbSAl Viro 	unlock_mount_hash();
22223ab6abeeSAl Viro 	namespace_unlock();
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds /*
22285528f911STrond Myklebust  * Ripoff of 'select_parent()'
22295528f911STrond Myklebust  *
22305528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
22315528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
22325528f911STrond Myklebust  */
2233692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard)
22345528f911STrond Myklebust {
2235692afc31SAl Viro 	struct mount *this_parent = parent;
22365528f911STrond Myklebust 	struct list_head *next;
22375528f911STrond Myklebust 	int found = 0;
22385528f911STrond Myklebust 
22395528f911STrond Myklebust repeat:
22406b41d536SAl Viro 	next = this_parent->mnt_mounts.next;
22415528f911STrond Myklebust resume:
22426b41d536SAl Viro 	while (next != &this_parent->mnt_mounts) {
22435528f911STrond Myklebust 		struct list_head *tmp = next;
22446b41d536SAl Viro 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
22455528f911STrond Myklebust 
22465528f911STrond Myklebust 		next = tmp->next;
2247692afc31SAl Viro 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
22485528f911STrond Myklebust 			continue;
22495528f911STrond Myklebust 		/*
22505528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
22515528f911STrond Myklebust 		 */
22526b41d536SAl Viro 		if (!list_empty(&mnt->mnt_mounts)) {
22535528f911STrond Myklebust 			this_parent = mnt;
22545528f911STrond Myklebust 			goto repeat;
22555528f911STrond Myklebust 		}
22565528f911STrond Myklebust 
22571ab59738SAl Viro 		if (!propagate_mount_busy(mnt, 1)) {
22586776db3dSAl Viro 			list_move_tail(&mnt->mnt_expire, graveyard);
22595528f911STrond Myklebust 			found++;
22605528f911STrond Myklebust 		}
22615528f911STrond Myklebust 	}
22625528f911STrond Myklebust 	/*
22635528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
22645528f911STrond Myklebust 	 */
22655528f911STrond Myklebust 	if (this_parent != parent) {
22666b41d536SAl Viro 		next = this_parent->mnt_child.next;
22670714a533SAl Viro 		this_parent = this_parent->mnt_parent;
22685528f911STrond Myklebust 		goto resume;
22695528f911STrond Myklebust 	}
22705528f911STrond Myklebust 	return found;
22715528f911STrond Myklebust }
22725528f911STrond Myklebust 
22735528f911STrond Myklebust /*
22745528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
22755528f911STrond Myklebust  * submounts of a specific parent mountpoint
227699b7db7bSNick Piggin  *
227748a066e7SAl Viro  * mount_lock must be held for write
22785528f911STrond Myklebust  */
2279b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt)
22805528f911STrond Myklebust {
22815528f911STrond Myklebust 	LIST_HEAD(graveyard);
2282761d5c38SAl Viro 	struct mount *m;
22835528f911STrond Myklebust 
22845528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2285c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2286bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2287761d5c38SAl Viro 			m = list_first_entry(&graveyard, struct mount,
22886776db3dSAl Viro 						mnt_expire);
2289143c8c91SAl Viro 			touch_mnt_namespace(m->mnt_ns);
2290328e6d90SAl Viro 			umount_tree(m, 1);
2291bcc5c7d2SAl Viro 		}
2292bcc5c7d2SAl Viro 	}
22935528f911STrond Myklebust }
22945528f911STrond Myklebust 
22955528f911STrond Myklebust /*
22961da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
22971da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
22981da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
22991da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
23001da177e4SLinus Torvalds  */
2301b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2302b58fed8bSRam Pai 				 unsigned long n)
23031da177e4SLinus Torvalds {
23041da177e4SLinus Torvalds 	char *t = to;
23051da177e4SLinus Torvalds 	const char __user *f = from;
23061da177e4SLinus Torvalds 	char c;
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
23091da177e4SLinus Torvalds 		return n;
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds 	while (n) {
23121da177e4SLinus Torvalds 		if (__get_user(c, f)) {
23131da177e4SLinus Torvalds 			memset(t, 0, n);
23141da177e4SLinus Torvalds 			break;
23151da177e4SLinus Torvalds 		}
23161da177e4SLinus Torvalds 		*t++ = c;
23171da177e4SLinus Torvalds 		f++;
23181da177e4SLinus Torvalds 		n--;
23191da177e4SLinus Torvalds 	}
23201da177e4SLinus Torvalds 	return n;
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
23241da177e4SLinus Torvalds {
23251da177e4SLinus Torvalds 	int i;
23261da177e4SLinus Torvalds 	unsigned long page;
23271da177e4SLinus Torvalds 	unsigned long size;
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 	*where = 0;
23301da177e4SLinus Torvalds 	if (!data)
23311da177e4SLinus Torvalds 		return 0;
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
23341da177e4SLinus Torvalds 		return -ENOMEM;
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
23371da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
23381da177e4SLinus Torvalds 	 * the remainder of the page.
23391da177e4SLinus Torvalds 	 */
23401da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
23411da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
23421da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
23431da177e4SLinus Torvalds 		size = PAGE_SIZE;
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
23461da177e4SLinus Torvalds 	if (!i) {
23471da177e4SLinus Torvalds 		free_page(page);
23481da177e4SLinus Torvalds 		return -EFAULT;
23491da177e4SLinus Torvalds 	}
23501da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
23511da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
23521da177e4SLinus Torvalds 	*where = page;
23531da177e4SLinus Torvalds 	return 0;
23541da177e4SLinus Torvalds }
23551da177e4SLinus Torvalds 
2356eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where)
2357eca6f534SVegard Nossum {
2358eca6f534SVegard Nossum 	char *tmp;
2359eca6f534SVegard Nossum 
2360eca6f534SVegard Nossum 	if (!data) {
2361eca6f534SVegard Nossum 		*where = NULL;
2362eca6f534SVegard Nossum 		return 0;
2363eca6f534SVegard Nossum 	}
2364eca6f534SVegard Nossum 
2365eca6f534SVegard Nossum 	tmp = strndup_user(data, PAGE_SIZE);
2366eca6f534SVegard Nossum 	if (IS_ERR(tmp))
2367eca6f534SVegard Nossum 		return PTR_ERR(tmp);
2368eca6f534SVegard Nossum 
2369eca6f534SVegard Nossum 	*where = tmp;
2370eca6f534SVegard Nossum 	return 0;
2371eca6f534SVegard Nossum }
2372eca6f534SVegard Nossum 
23731da177e4SLinus Torvalds /*
23741da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
23751da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
23761da177e4SLinus Torvalds  *
23771da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
23781da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
23791da177e4SLinus Torvalds  * information (or be NULL).
23801da177e4SLinus Torvalds  *
23811da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
23821da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
23831da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
23841da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
23851da177e4SLinus Torvalds  * and must be discarded.
23861da177e4SLinus Torvalds  */
2387808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name,
2388808d4e3cSAl Viro 		const char *type_page, unsigned long flags, void *data_page)
23891da177e4SLinus Torvalds {
23902d92ab3cSAl Viro 	struct path path;
23911da177e4SLinus Torvalds 	int retval = 0;
23921da177e4SLinus Torvalds 	int mnt_flags = 0;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 	/* Discard magic */
23951da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
23961da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	/* Basic sanity checks */
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
24011da177e4SLinus Torvalds 		return -EINVAL;
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds 	if (data_page)
24041da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
24051da177e4SLinus Torvalds 
2406a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
2407a27ab9f2STetsuo Handa 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2408a27ab9f2STetsuo Handa 	if (retval)
2409a27ab9f2STetsuo Handa 		return retval;
2410a27ab9f2STetsuo Handa 
2411a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2412a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
24130d5cadb8SAl Viro 	if (!retval && !may_mount())
24140d5cadb8SAl Viro 		retval = -EPERM;
2415a27ab9f2STetsuo Handa 	if (retval)
2416a27ab9f2STetsuo Handa 		goto dput_out;
2417a27ab9f2STetsuo Handa 
2418613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2419613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
24200a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
24210a1c01c9SMatthew Garrett 
24221da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
24231da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
24241da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
24251da177e4SLinus Torvalds 	if (flags & MS_NODEV)
24261da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
24271da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
24281da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2429fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2430fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2431fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2432fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2433d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2434d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
24352e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
24362e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2437fc33a7bbSChristoph Hellwig 
24387a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2439d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2440d0adde57SMatthew Garrett 		   MS_STRICTATIME);
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
24432d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
24441da177e4SLinus Torvalds 				    data_page);
24451da177e4SLinus Torvalds 	else if (flags & MS_BIND)
24462d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
24479676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
24482d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
24491da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
24502d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
24511da177e4SLinus Torvalds 	else
24522d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
24531da177e4SLinus Torvalds 				      dev_name, data_page);
24541da177e4SLinus Torvalds dput_out:
24552d92ab3cSAl Viro 	path_put(&path);
24561da177e4SLinus Torvalds 	return retval;
24571da177e4SLinus Torvalds }
24581da177e4SLinus Torvalds 
2459771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns)
2460771b1371SEric W. Biederman {
246198f842e6SEric W. Biederman 	proc_free_inum(ns->proc_inum);
2462771b1371SEric W. Biederman 	put_user_ns(ns->user_ns);
2463771b1371SEric W. Biederman 	kfree(ns);
2464771b1371SEric W. Biederman }
2465771b1371SEric W. Biederman 
24668823c079SEric W. Biederman /*
24678823c079SEric W. Biederman  * Assign a sequence number so we can detect when we attempt to bind
24688823c079SEric W. Biederman  * mount a reference to an older mount namespace into the current
24698823c079SEric W. Biederman  * mount namespace, preventing reference counting loops.  A 64bit
24708823c079SEric W. Biederman  * number incrementing at 10Ghz will take 12,427 years to wrap which
24718823c079SEric W. Biederman  * is effectively never, so we can ignore the possibility.
24728823c079SEric W. Biederman  */
24738823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
24748823c079SEric W. Biederman 
2475771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2476cf8d2c11STrond Myklebust {
2477cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
247898f842e6SEric W. Biederman 	int ret;
2479cf8d2c11STrond Myklebust 
2480cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2481cf8d2c11STrond Myklebust 	if (!new_ns)
2482cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
248398f842e6SEric W. Biederman 	ret = proc_alloc_inum(&new_ns->proc_inum);
248498f842e6SEric W. Biederman 	if (ret) {
248598f842e6SEric W. Biederman 		kfree(new_ns);
248698f842e6SEric W. Biederman 		return ERR_PTR(ret);
248798f842e6SEric W. Biederman 	}
24888823c079SEric W. Biederman 	new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2489cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2490cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2491cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2492cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2493cf8d2c11STrond Myklebust 	new_ns->event = 0;
2494771b1371SEric W. Biederman 	new_ns->user_ns = get_user_ns(user_ns);
2495cf8d2c11STrond Myklebust 	return new_ns;
2496cf8d2c11STrond Myklebust }
2497cf8d2c11STrond Myklebust 
24989559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
24999559f689SAl Viro 		struct user_namespace *user_ns, struct fs_struct *new_fs)
25001da177e4SLinus Torvalds {
25016b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
25027f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2503315fc83eSAl Viro 	struct mount *p, *q;
25049559f689SAl Viro 	struct mount *old;
2505cb338d06SAl Viro 	struct mount *new;
25067a472ef4SEric W. Biederman 	int copy_flags;
25071da177e4SLinus Torvalds 
25089559f689SAl Viro 	BUG_ON(!ns);
25099559f689SAl Viro 
25109559f689SAl Viro 	if (likely(!(flags & CLONE_NEWNS))) {
25119559f689SAl Viro 		get_mnt_ns(ns);
25129559f689SAl Viro 		return ns;
25139559f689SAl Viro 	}
25149559f689SAl Viro 
25159559f689SAl Viro 	old = ns->root;
25169559f689SAl Viro 
2517771b1371SEric W. Biederman 	new_ns = alloc_mnt_ns(user_ns);
2518cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2519cf8d2c11STrond Myklebust 		return new_ns;
25201da177e4SLinus Torvalds 
252197216be0SAl Viro 	namespace_lock();
25221da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
25234ce5d2b1SEric W. Biederman 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
25249559f689SAl Viro 	if (user_ns != ns->user_ns)
2525132c94e3SEric W. Biederman 		copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
25267a472ef4SEric W. Biederman 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2527be34d1a3SDavid Howells 	if (IS_ERR(new)) {
2528328e6d90SAl Viro 		namespace_unlock();
2529771b1371SEric W. Biederman 		free_mnt_ns(new_ns);
2530be34d1a3SDavid Howells 		return ERR_CAST(new);
25311da177e4SLinus Torvalds 	}
2532be08d6d2SAl Viro 	new_ns->root = new;
25331a4eeaf2SAl Viro 	list_add_tail(&new_ns->list, &new->mnt_list);
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	/*
25361da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
25371da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
25381da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
25391da177e4SLinus Torvalds 	 */
2540909b0a88SAl Viro 	p = old;
2541cb338d06SAl Viro 	q = new;
25421da177e4SLinus Torvalds 	while (p) {
2543143c8c91SAl Viro 		q->mnt_ns = new_ns;
25449559f689SAl Viro 		if (new_fs) {
25459559f689SAl Viro 			if (&p->mnt == new_fs->root.mnt) {
25469559f689SAl Viro 				new_fs->root.mnt = mntget(&q->mnt);
2547315fc83eSAl Viro 				rootmnt = &p->mnt;
25481da177e4SLinus Torvalds 			}
25499559f689SAl Viro 			if (&p->mnt == new_fs->pwd.mnt) {
25509559f689SAl Viro 				new_fs->pwd.mnt = mntget(&q->mnt);
2551315fc83eSAl Viro 				pwdmnt = &p->mnt;
25521da177e4SLinus Torvalds 			}
25531da177e4SLinus Torvalds 		}
2554909b0a88SAl Viro 		p = next_mnt(p, old);
2555909b0a88SAl Viro 		q = next_mnt(q, new);
25564ce5d2b1SEric W. Biederman 		if (!q)
25574ce5d2b1SEric W. Biederman 			break;
25584ce5d2b1SEric W. Biederman 		while (p->mnt.mnt_root != q->mnt.mnt_root)
25594ce5d2b1SEric W. Biederman 			p = next_mnt(p, old);
25601da177e4SLinus Torvalds 	}
2561328e6d90SAl Viro 	namespace_unlock();
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 	if (rootmnt)
2564f03c6599SAl Viro 		mntput(rootmnt);
25651da177e4SLinus Torvalds 	if (pwdmnt)
2566f03c6599SAl Viro 		mntput(pwdmnt);
25671da177e4SLinus Torvalds 
2568741a2951SJANAK DESAI 	return new_ns;
2569741a2951SJANAK DESAI }
2570741a2951SJANAK DESAI 
2571cf8d2c11STrond Myklebust /**
2572cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2573cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2574cf8d2c11STrond Myklebust  */
25751a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2576cf8d2c11STrond Myklebust {
2577771b1371SEric W. Biederman 	struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2578cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
25791a4eeaf2SAl Viro 		struct mount *mnt = real_mount(m);
25801a4eeaf2SAl Viro 		mnt->mnt_ns = new_ns;
2581be08d6d2SAl Viro 		new_ns->root = mnt;
2582b1983cd8SAl Viro 		list_add(&mnt->mnt_list, &new_ns->list);
2583c1334495SAl Viro 	} else {
25841a4eeaf2SAl Viro 		mntput(m);
2585cf8d2c11STrond Myklebust 	}
2586cf8d2c11STrond Myklebust 	return new_ns;
2587cf8d2c11STrond Myklebust }
2588cf8d2c11STrond Myklebust 
2589ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2590ea441d11SAl Viro {
2591ea441d11SAl Viro 	struct mnt_namespace *ns;
2592d31da0f0SAl Viro 	struct super_block *s;
2593ea441d11SAl Viro 	struct path path;
2594ea441d11SAl Viro 	int err;
2595ea441d11SAl Viro 
2596ea441d11SAl Viro 	ns = create_mnt_ns(mnt);
2597ea441d11SAl Viro 	if (IS_ERR(ns))
2598ea441d11SAl Viro 		return ERR_CAST(ns);
2599ea441d11SAl Viro 
2600ea441d11SAl Viro 	err = vfs_path_lookup(mnt->mnt_root, mnt,
2601ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2602ea441d11SAl Viro 
2603ea441d11SAl Viro 	put_mnt_ns(ns);
2604ea441d11SAl Viro 
2605ea441d11SAl Viro 	if (err)
2606ea441d11SAl Viro 		return ERR_PTR(err);
2607ea441d11SAl Viro 
2608ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
2609d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
2610d31da0f0SAl Viro 	atomic_inc(&s->s_active);
2611ea441d11SAl Viro 	mntput(path.mnt);
2612ea441d11SAl Viro 	/* lock the sucker */
2613d31da0f0SAl Viro 	down_write(&s->s_umount);
2614ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
2615ea441d11SAl Viro 	return path.dentry;
2616ea441d11SAl Viro }
2617ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
2618ea441d11SAl Viro 
2619bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2620bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
26211da177e4SLinus Torvalds {
2622eca6f534SVegard Nossum 	int ret;
2623eca6f534SVegard Nossum 	char *kernel_type;
262491a27b2aSJeff Layton 	struct filename *kernel_dir;
2625eca6f534SVegard Nossum 	char *kernel_dev;
26261da177e4SLinus Torvalds 	unsigned long data_page;
26271da177e4SLinus Torvalds 
2628eca6f534SVegard Nossum 	ret = copy_mount_string(type, &kernel_type);
2629eca6f534SVegard Nossum 	if (ret < 0)
2630eca6f534SVegard Nossum 		goto out_type;
26311da177e4SLinus Torvalds 
2632eca6f534SVegard Nossum 	kernel_dir = getname(dir_name);
2633eca6f534SVegard Nossum 	if (IS_ERR(kernel_dir)) {
2634eca6f534SVegard Nossum 		ret = PTR_ERR(kernel_dir);
2635eca6f534SVegard Nossum 		goto out_dir;
2636eca6f534SVegard Nossum 	}
26371da177e4SLinus Torvalds 
2638eca6f534SVegard Nossum 	ret = copy_mount_string(dev_name, &kernel_dev);
2639eca6f534SVegard Nossum 	if (ret < 0)
2640eca6f534SVegard Nossum 		goto out_dev;
26411da177e4SLinus Torvalds 
2642eca6f534SVegard Nossum 	ret = copy_mount_options(data, &data_page);
2643eca6f534SVegard Nossum 	if (ret < 0)
2644eca6f534SVegard Nossum 		goto out_data;
26451da177e4SLinus Torvalds 
264691a27b2aSJeff Layton 	ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags,
2647eca6f534SVegard Nossum 		(void *) data_page);
2648eca6f534SVegard Nossum 
26491da177e4SLinus Torvalds 	free_page(data_page);
2650eca6f534SVegard Nossum out_data:
2651eca6f534SVegard Nossum 	kfree(kernel_dev);
2652eca6f534SVegard Nossum out_dev:
2653eca6f534SVegard Nossum 	putname(kernel_dir);
2654eca6f534SVegard Nossum out_dir:
2655eca6f534SVegard Nossum 	kfree(kernel_type);
2656eca6f534SVegard Nossum out_type:
2657eca6f534SVegard Nossum 	return ret;
26581da177e4SLinus Torvalds }
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds /*
2661afac7cbaSAl Viro  * Return true if path is reachable from root
2662afac7cbaSAl Viro  *
266348a066e7SAl Viro  * namespace_sem or mount_lock is held
2664afac7cbaSAl Viro  */
2665643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2666afac7cbaSAl Viro 			 const struct path *root)
2667afac7cbaSAl Viro {
2668643822b4SAl Viro 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2669a73324daSAl Viro 		dentry = mnt->mnt_mountpoint;
26700714a533SAl Viro 		mnt = mnt->mnt_parent;
2671afac7cbaSAl Viro 	}
2672643822b4SAl Viro 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2673afac7cbaSAl Viro }
2674afac7cbaSAl Viro 
2675afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2)
2676afac7cbaSAl Viro {
2677afac7cbaSAl Viro 	int res;
267848a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
2679643822b4SAl Viro 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
268048a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
2681afac7cbaSAl Viro 	return res;
2682afac7cbaSAl Viro }
2683afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
2684afac7cbaSAl Viro 
2685afac7cbaSAl Viro /*
26861da177e4SLinus Torvalds  * pivot_root Semantics:
26871da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
26881da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
26891da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
26901da177e4SLinus Torvalds  *
26911da177e4SLinus Torvalds  * Restrictions:
26921da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
26931da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
26941da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
26951da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
26961da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
26971da177e4SLinus Torvalds  *
26984a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
26994a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
27004a0d11faSNeil Brown  * in this situation.
27014a0d11faSNeil Brown  *
27021da177e4SLinus Torvalds  * Notes:
27031da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
27041da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
27051da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
27061da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
27071da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
27081da177e4SLinus Torvalds  *    first.
27091da177e4SLinus Torvalds  */
27103480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
27113480b257SHeiko Carstens 		const char __user *, put_old)
27121da177e4SLinus Torvalds {
27132d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
271484d17192SAl Viro 	struct mount *new_mnt, *root_mnt, *old_mnt;
271584d17192SAl Viro 	struct mountpoint *old_mp, *root_mp;
27161da177e4SLinus Torvalds 	int error;
27171da177e4SLinus Torvalds 
27189b40bc90SAl Viro 	if (!may_mount())
27191da177e4SLinus Torvalds 		return -EPERM;
27201da177e4SLinus Torvalds 
27212d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
27221da177e4SLinus Torvalds 	if (error)
27231da177e4SLinus Torvalds 		goto out0;
27241da177e4SLinus Torvalds 
27252d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
27261da177e4SLinus Torvalds 	if (error)
27271da177e4SLinus Torvalds 		goto out1;
27281da177e4SLinus Torvalds 
27292d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
2730b12cea91SAl Viro 	if (error)
2731b12cea91SAl Viro 		goto out2;
27321da177e4SLinus Torvalds 
2733f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
273484d17192SAl Viro 	old_mp = lock_mount(&old);
273584d17192SAl Viro 	error = PTR_ERR(old_mp);
273684d17192SAl Viro 	if (IS_ERR(old_mp))
2737b12cea91SAl Viro 		goto out3;
2738b12cea91SAl Viro 
27391da177e4SLinus Torvalds 	error = -EINVAL;
2740419148daSAl Viro 	new_mnt = real_mount(new.mnt);
2741419148daSAl Viro 	root_mnt = real_mount(root.mnt);
274284d17192SAl Viro 	old_mnt = real_mount(old.mnt);
274384d17192SAl Viro 	if (IS_MNT_SHARED(old_mnt) ||
2744fc7be130SAl Viro 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
2745fc7be130SAl Viro 		IS_MNT_SHARED(root_mnt->mnt_parent))
2746b12cea91SAl Viro 		goto out4;
2747143c8c91SAl Viro 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2748b12cea91SAl Viro 		goto out4;
27495ff9d8a6SEric W. Biederman 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
27505ff9d8a6SEric W. Biederman 		goto out4;
27511da177e4SLinus Torvalds 	error = -ENOENT;
2752f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
2753b12cea91SAl Viro 		goto out4;
27541da177e4SLinus Torvalds 	error = -EBUSY;
275584d17192SAl Viro 	if (new_mnt == root_mnt || old_mnt == root_mnt)
2756b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
27571da177e4SLinus Torvalds 	error = -EINVAL;
27588c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
2759b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2760676da58dSAl Viro 	if (!mnt_has_parent(root_mnt))
2761b12cea91SAl Viro 		goto out4; /* not attached */
276284d17192SAl Viro 	root_mp = root_mnt->mnt_mp;
27632d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
2764b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2765676da58dSAl Viro 	if (!mnt_has_parent(new_mnt))
2766b12cea91SAl Viro 		goto out4; /* not attached */
27674ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
276884d17192SAl Viro 	if (!is_path_reachable(old_mnt, old.dentry, &new))
2769b12cea91SAl Viro 		goto out4;
277084d17192SAl Viro 	root_mp->m_count++; /* pin it so it won't go away */
2771719ea2fbSAl Viro 	lock_mount_hash();
2772419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
2773419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
27745ff9d8a6SEric W. Biederman 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
27755ff9d8a6SEric W. Biederman 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
27765ff9d8a6SEric W. Biederman 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
27775ff9d8a6SEric W. Biederman 	}
27784ac91378SJan Blunck 	/* mount old root on put_old */
277984d17192SAl Viro 	attach_mnt(root_mnt, old_mnt, old_mp);
27804ac91378SJan Blunck 	/* mount new_root on / */
278184d17192SAl Viro 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
27826b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
2783719ea2fbSAl Viro 	unlock_mount_hash();
27842d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
278584d17192SAl Viro 	put_mountpoint(root_mp);
27861da177e4SLinus Torvalds 	error = 0;
2787b12cea91SAl Viro out4:
278884d17192SAl Viro 	unlock_mount(old_mp);
2789b12cea91SAl Viro 	if (!error) {
27901a390689SAl Viro 		path_put(&root_parent);
27911a390689SAl Viro 		path_put(&parent_path);
2792b12cea91SAl Viro 	}
2793b12cea91SAl Viro out3:
27948c3ee42eSAl Viro 	path_put(&root);
2795b12cea91SAl Viro out2:
27962d8f3038SAl Viro 	path_put(&old);
27971da177e4SLinus Torvalds out1:
27982d8f3038SAl Viro 	path_put(&new);
27991da177e4SLinus Torvalds out0:
28001da177e4SLinus Torvalds 	return error;
28011da177e4SLinus Torvalds }
28021da177e4SLinus Torvalds 
28031da177e4SLinus Torvalds static void __init init_mount_tree(void)
28041da177e4SLinus Torvalds {
28051da177e4SLinus Torvalds 	struct vfsmount *mnt;
28066b3286edSKirill Korotaev 	struct mnt_namespace *ns;
2807ac748a09SJan Blunck 	struct path root;
28080c55cfc4SEric W. Biederman 	struct file_system_type *type;
28091da177e4SLinus Torvalds 
28100c55cfc4SEric W. Biederman 	type = get_fs_type("rootfs");
28110c55cfc4SEric W. Biederman 	if (!type)
28120c55cfc4SEric W. Biederman 		panic("Can't find rootfs type");
28130c55cfc4SEric W. Biederman 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
28140c55cfc4SEric W. Biederman 	put_filesystem(type);
28151da177e4SLinus Torvalds 	if (IS_ERR(mnt))
28161da177e4SLinus Torvalds 		panic("Can't create rootfs");
2817b3e19d92SNick Piggin 
28183b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
28193b22edc5STrond Myklebust 	if (IS_ERR(ns))
28201da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
28211da177e4SLinus Torvalds 
28226b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
28236b3286edSKirill Korotaev 	get_mnt_ns(ns);
28241da177e4SLinus Torvalds 
2825be08d6d2SAl Viro 	root.mnt = mnt;
2826be08d6d2SAl Viro 	root.dentry = mnt->mnt_root;
2827ac748a09SJan Blunck 
2828ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
2829ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
28301da177e4SLinus Torvalds }
28311da177e4SLinus Torvalds 
283274bf17cfSDenis Cheng void __init mnt_init(void)
28331da177e4SLinus Torvalds {
283413f14b4dSEric Dumazet 	unsigned u;
283515a67dd8SRandy Dunlap 	int err;
28361da177e4SLinus Torvalds 
28377d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
283820c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
28391da177e4SLinus Torvalds 
28400818bf27SAl Viro 	mount_hashtable = alloc_large_system_hash("Mount-cache",
284138129a13SAl Viro 				sizeof(struct hlist_head),
28420818bf27SAl Viro 				mhash_entries, 19,
28430818bf27SAl Viro 				0,
28440818bf27SAl Viro 				&m_hash_shift, &m_hash_mask, 0, 0);
28450818bf27SAl Viro 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
28460818bf27SAl Viro 				sizeof(struct hlist_head),
28470818bf27SAl Viro 				mphash_entries, 19,
28480818bf27SAl Viro 				0,
28490818bf27SAl Viro 				&mp_hash_shift, &mp_hash_mask, 0, 0);
28501da177e4SLinus Torvalds 
285184d17192SAl Viro 	if (!mount_hashtable || !mountpoint_hashtable)
28521da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
28531da177e4SLinus Torvalds 
28540818bf27SAl Viro 	for (u = 0; u <= m_hash_mask; u++)
285538129a13SAl Viro 		INIT_HLIST_HEAD(&mount_hashtable[u]);
28560818bf27SAl Viro 	for (u = 0; u <= mp_hash_mask; u++)
28570818bf27SAl Viro 		INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
28581da177e4SLinus Torvalds 
28594b93dc9bSTejun Heo 	kernfs_init();
28604b93dc9bSTejun Heo 
286115a67dd8SRandy Dunlap 	err = sysfs_init();
286215a67dd8SRandy Dunlap 	if (err)
286315a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
28648e24eea7SHarvey Harrison 			__func__, err);
286500d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
286600d26666SGreg Kroah-Hartman 	if (!fs_kobj)
28678e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
28681da177e4SLinus Torvalds 	init_rootfs();
28691da177e4SLinus Torvalds 	init_mount_tree();
28701da177e4SLinus Torvalds }
28711da177e4SLinus Torvalds 
2872616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
28731da177e4SLinus Torvalds {
2874d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
2875616511d0STrond Myklebust 		return;
28767b00ed6fSAl Viro 	drop_collected_mounts(&ns->root->mnt);
2877771b1371SEric W. Biederman 	free_mnt_ns(ns);
28781da177e4SLinus Torvalds }
28799d412a43SAl Viro 
28809d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
28819d412a43SAl Viro {
2882423e0ab0STim Chen 	struct vfsmount *mnt;
2883423e0ab0STim Chen 	mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
2884423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
2885423e0ab0STim Chen 		/*
2886423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
2887423e0ab0STim Chen 		 * we unmount before file sys is unregistered
2888423e0ab0STim Chen 		*/
2889f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
2890423e0ab0STim Chen 	}
2891423e0ab0STim Chen 	return mnt;
28929d412a43SAl Viro }
28939d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data);
2894423e0ab0STim Chen 
2895423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
2896423e0ab0STim Chen {
2897423e0ab0STim Chen 	/* release long term mount so mount point can be released */
2898423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
2899f7a99c5bSAl Viro 		real_mount(mnt)->mnt_ns = NULL;
290048a066e7SAl Viro 		synchronize_rcu();	/* yecchhh... */
2901423e0ab0STim Chen 		mntput(mnt);
2902423e0ab0STim Chen 	}
2903423e0ab0STim Chen }
2904423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
290502125a82SAl Viro 
290602125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
290702125a82SAl Viro {
2908143c8c91SAl Viro 	return check_mnt(real_mount(mnt));
290902125a82SAl Viro }
29108823c079SEric W. Biederman 
29113151527eSEric W. Biederman bool current_chrooted(void)
29123151527eSEric W. Biederman {
29133151527eSEric W. Biederman 	/* Does the current process have a non-standard root */
29143151527eSEric W. Biederman 	struct path ns_root;
29153151527eSEric W. Biederman 	struct path fs_root;
29163151527eSEric W. Biederman 	bool chrooted;
29173151527eSEric W. Biederman 
29183151527eSEric W. Biederman 	/* Find the namespace root */
29193151527eSEric W. Biederman 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
29203151527eSEric W. Biederman 	ns_root.dentry = ns_root.mnt->mnt_root;
29213151527eSEric W. Biederman 	path_get(&ns_root);
29223151527eSEric W. Biederman 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
29233151527eSEric W. Biederman 		;
29243151527eSEric W. Biederman 
29253151527eSEric W. Biederman 	get_fs_root(current->fs, &fs_root);
29263151527eSEric W. Biederman 
29273151527eSEric W. Biederman 	chrooted = !path_equal(&fs_root, &ns_root);
29283151527eSEric W. Biederman 
29293151527eSEric W. Biederman 	path_put(&fs_root);
29303151527eSEric W. Biederman 	path_put(&ns_root);
29313151527eSEric W. Biederman 
29323151527eSEric W. Biederman 	return chrooted;
29333151527eSEric W. Biederman }
29343151527eSEric W. Biederman 
2935e51db735SEric W. Biederman bool fs_fully_visible(struct file_system_type *type)
293687a8ebd6SEric W. Biederman {
293787a8ebd6SEric W. Biederman 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
293887a8ebd6SEric W. Biederman 	struct mount *mnt;
2939e51db735SEric W. Biederman 	bool visible = false;
294087a8ebd6SEric W. Biederman 
2941e51db735SEric W. Biederman 	if (unlikely(!ns))
2942e51db735SEric W. Biederman 		return false;
2943e51db735SEric W. Biederman 
294444bb4385SAl Viro 	down_read(&namespace_sem);
294587a8ebd6SEric W. Biederman 	list_for_each_entry(mnt, &ns->list, mnt_list) {
2946e51db735SEric W. Biederman 		struct mount *child;
2947e51db735SEric W. Biederman 		if (mnt->mnt.mnt_sb->s_type != type)
2948e51db735SEric W. Biederman 			continue;
2949e51db735SEric W. Biederman 
2950e51db735SEric W. Biederman 		/* This mount is not fully visible if there are any child mounts
2951e51db735SEric W. Biederman 		 * that cover anything except for empty directories.
2952e51db735SEric W. Biederman 		 */
2953e51db735SEric W. Biederman 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2954e51db735SEric W. Biederman 			struct inode *inode = child->mnt_mountpoint->d_inode;
2955e51db735SEric W. Biederman 			if (!S_ISDIR(inode->i_mode))
2956e51db735SEric W. Biederman 				goto next;
295741301ae7SEric W. Biederman 			if (inode->i_nlink > 2)
2958e51db735SEric W. Biederman 				goto next;
295987a8ebd6SEric W. Biederman 		}
2960e51db735SEric W. Biederman 		visible = true;
2961e51db735SEric W. Biederman 		goto found;
2962e51db735SEric W. Biederman 	next:	;
296387a8ebd6SEric W. Biederman 	}
2964e51db735SEric W. Biederman found:
296544bb4385SAl Viro 	up_read(&namespace_sem);
2966e51db735SEric W. Biederman 	return visible;
296787a8ebd6SEric W. Biederman }
296887a8ebd6SEric W. Biederman 
29698823c079SEric W. Biederman static void *mntns_get(struct task_struct *task)
29708823c079SEric W. Biederman {
29718823c079SEric W. Biederman 	struct mnt_namespace *ns = NULL;
29728823c079SEric W. Biederman 	struct nsproxy *nsproxy;
29738823c079SEric W. Biederman 
29748823c079SEric W. Biederman 	rcu_read_lock();
29758823c079SEric W. Biederman 	nsproxy = task_nsproxy(task);
29768823c079SEric W. Biederman 	if (nsproxy) {
29778823c079SEric W. Biederman 		ns = nsproxy->mnt_ns;
29788823c079SEric W. Biederman 		get_mnt_ns(ns);
29798823c079SEric W. Biederman 	}
29808823c079SEric W. Biederman 	rcu_read_unlock();
29818823c079SEric W. Biederman 
29828823c079SEric W. Biederman 	return ns;
29838823c079SEric W. Biederman }
29848823c079SEric W. Biederman 
29858823c079SEric W. Biederman static void mntns_put(void *ns)
29868823c079SEric W. Biederman {
29878823c079SEric W. Biederman 	put_mnt_ns(ns);
29888823c079SEric W. Biederman }
29898823c079SEric W. Biederman 
29908823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns)
29918823c079SEric W. Biederman {
29928823c079SEric W. Biederman 	struct fs_struct *fs = current->fs;
29938823c079SEric W. Biederman 	struct mnt_namespace *mnt_ns = ns;
29948823c079SEric W. Biederman 	struct path root;
29958823c079SEric W. Biederman 
29960c55cfc4SEric W. Biederman 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
2997c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
2998c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
2999ae11e0f1SZhao Hongjiang 		return -EPERM;
30008823c079SEric W. Biederman 
30018823c079SEric W. Biederman 	if (fs->users != 1)
30028823c079SEric W. Biederman 		return -EINVAL;
30038823c079SEric W. Biederman 
30048823c079SEric W. Biederman 	get_mnt_ns(mnt_ns);
30058823c079SEric W. Biederman 	put_mnt_ns(nsproxy->mnt_ns);
30068823c079SEric W. Biederman 	nsproxy->mnt_ns = mnt_ns;
30078823c079SEric W. Biederman 
30088823c079SEric W. Biederman 	/* Find the root */
30098823c079SEric W. Biederman 	root.mnt    = &mnt_ns->root->mnt;
30108823c079SEric W. Biederman 	root.dentry = mnt_ns->root->mnt.mnt_root;
30118823c079SEric W. Biederman 	path_get(&root);
30128823c079SEric W. Biederman 	while(d_mountpoint(root.dentry) && follow_down_one(&root))
30138823c079SEric W. Biederman 		;
30148823c079SEric W. Biederman 
30158823c079SEric W. Biederman 	/* Update the pwd and root */
30168823c079SEric W. Biederman 	set_fs_pwd(fs, &root);
30178823c079SEric W. Biederman 	set_fs_root(fs, &root);
30188823c079SEric W. Biederman 
30198823c079SEric W. Biederman 	path_put(&root);
30208823c079SEric W. Biederman 	return 0;
30218823c079SEric W. Biederman }
30228823c079SEric W. Biederman 
302398f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns)
302498f842e6SEric W. Biederman {
302598f842e6SEric W. Biederman 	struct mnt_namespace *mnt_ns = ns;
302698f842e6SEric W. Biederman 	return mnt_ns->proc_inum;
302798f842e6SEric W. Biederman }
302898f842e6SEric W. Biederman 
30298823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = {
30308823c079SEric W. Biederman 	.name		= "mnt",
30318823c079SEric W. Biederman 	.type		= CLONE_NEWNS,
30328823c079SEric W. Biederman 	.get		= mntns_get,
30338823c079SEric W. Biederman 	.put		= mntns_put,
30348823c079SEric W. Biederman 	.install	= mntns_install,
303598f842e6SEric W. Biederman 	.inum		= mntns_inum,
30368823c079SEric W. Biederman };
3037