xref: /openbmc/linux/fs/namespace.c (revision b1e75df4)
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>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/sched.h>
1499b7db7bSNick Piggin #include <linux/spinlock.h>
1599b7db7bSNick Piggin #include <linux/percpu.h>
161da177e4SLinus Torvalds #include <linux/init.h>
1715a67dd8SRandy Dunlap #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/acct.h>
1916f7e0feSRandy Dunlap #include <linux/capability.h>
203d733633SDave Hansen #include <linux/cpumask.h>
211da177e4SLinus Torvalds #include <linux/module.h>
22f20a9eadSAndrew Morton #include <linux/sysfs.h>
231da177e4SLinus Torvalds #include <linux/seq_file.h>
246b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
251da177e4SLinus Torvalds #include <linux/namei.h>
26b43f3cbdSAlexey Dobriyan #include <linux/nsproxy.h>
271da177e4SLinus Torvalds #include <linux/security.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
2907f3f05cSDavid Howells #include <linux/ramfs.h>
3013f14b4dSEric Dumazet #include <linux/log2.h>
3173cd49ecSMiklos Szeredi #include <linux/idr.h>
325ad4e53bSAl Viro #include <linux/fs_struct.h>
332504c5d6SAndreas Gruenbacher #include <linux/fsnotify.h>
341da177e4SLinus Torvalds #include <asm/uaccess.h>
351da177e4SLinus Torvalds #include <asm/unistd.h>
3607b20889SRam Pai #include "pnode.h"
37948730b0SAdrian Bunk #include "internal.h"
381da177e4SLinus Torvalds 
3913f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
4013f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT)
4113f14b4dSEric Dumazet 
425addc5ddSAl Viro static int event;
4373cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
44719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
4599b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock);
46f21f6220SAl Viro static int mnt_id_start = 0;
47f21f6220SAl Viro static int mnt_group_start = 1;
485addc5ddSAl Viro 
49fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly;
50e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
51390c6843SRam Pai static struct rw_semaphore namespace_sem;
521da177e4SLinus Torvalds 
53f87fd4c2SMiklos Szeredi /* /sys/fs */
5400d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
5500d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
56f87fd4c2SMiklos Szeredi 
5799b7db7bSNick Piggin /*
5899b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
5999b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
6099b7db7bSNick Piggin  * up the tree.
6199b7db7bSNick Piggin  *
6299b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
6399b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
6499b7db7bSNick Piggin  */
6599b7db7bSNick Piggin DEFINE_BRLOCK(vfsmount_lock);
6699b7db7bSNick Piggin 
671da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
701da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
7113f14b4dSEric Dumazet 	tmp = tmp + (tmp >> HASH_SHIFT);
7213f14b4dSEric Dumazet 	return tmp & (HASH_SIZE - 1);
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
753d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
763d733633SDave Hansen 
7799b7db7bSNick Piggin /*
7899b7db7bSNick Piggin  * allocation is serialized by namespace_sem, but we need the spinlock to
7999b7db7bSNick Piggin  * serialize with freeing.
8099b7db7bSNick Piggin  */
8173cd49ecSMiklos Szeredi static int mnt_alloc_id(struct vfsmount *mnt)
8273cd49ecSMiklos Szeredi {
8373cd49ecSMiklos Szeredi 	int res;
8473cd49ecSMiklos Szeredi 
8573cd49ecSMiklos Szeredi retry:
8673cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
8799b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
88f21f6220SAl Viro 	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
89f21f6220SAl Viro 	if (!res)
90f21f6220SAl Viro 		mnt_id_start = mnt->mnt_id + 1;
9199b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
9273cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
9373cd49ecSMiklos Szeredi 		goto retry;
9473cd49ecSMiklos Szeredi 
9573cd49ecSMiklos Szeredi 	return res;
9673cd49ecSMiklos Szeredi }
9773cd49ecSMiklos Szeredi 
9873cd49ecSMiklos Szeredi static void mnt_free_id(struct vfsmount *mnt)
9973cd49ecSMiklos Szeredi {
100f21f6220SAl Viro 	int id = mnt->mnt_id;
10199b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
102f21f6220SAl Viro 	ida_remove(&mnt_id_ida, id);
103f21f6220SAl Viro 	if (mnt_id_start > id)
104f21f6220SAl Viro 		mnt_id_start = id;
10599b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
10673cd49ecSMiklos Szeredi }
10773cd49ecSMiklos Szeredi 
108719f5d7fSMiklos Szeredi /*
109719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
110719f5d7fSMiklos Szeredi  *
111719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
112719f5d7fSMiklos Szeredi  */
113719f5d7fSMiklos Szeredi static int mnt_alloc_group_id(struct vfsmount *mnt)
114719f5d7fSMiklos Szeredi {
115f21f6220SAl Viro 	int res;
116f21f6220SAl Viro 
117719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
118719f5d7fSMiklos Szeredi 		return -ENOMEM;
119719f5d7fSMiklos Szeredi 
120f21f6220SAl Viro 	res = ida_get_new_above(&mnt_group_ida,
121f21f6220SAl Viro 				mnt_group_start,
122f21f6220SAl Viro 				&mnt->mnt_group_id);
123f21f6220SAl Viro 	if (!res)
124f21f6220SAl Viro 		mnt_group_start = mnt->mnt_group_id + 1;
125f21f6220SAl Viro 
126f21f6220SAl Viro 	return res;
127719f5d7fSMiklos Szeredi }
128719f5d7fSMiklos Szeredi 
129719f5d7fSMiklos Szeredi /*
130719f5d7fSMiklos Szeredi  * Release a peer group ID
131719f5d7fSMiklos Szeredi  */
132719f5d7fSMiklos Szeredi void mnt_release_group_id(struct vfsmount *mnt)
133719f5d7fSMiklos Szeredi {
134f21f6220SAl Viro 	int id = mnt->mnt_group_id;
135f21f6220SAl Viro 	ida_remove(&mnt_group_ida, id);
136f21f6220SAl Viro 	if (mnt_group_start > id)
137f21f6220SAl Viro 		mnt_group_start = id;
138719f5d7fSMiklos Szeredi 	mnt->mnt_group_id = 0;
139719f5d7fSMiklos Szeredi }
140719f5d7fSMiklos Szeredi 
141b3e19d92SNick Piggin /*
142b3e19d92SNick Piggin  * vfsmount lock must be held for read
143b3e19d92SNick Piggin  */
144b3e19d92SNick Piggin static inline void mnt_add_count(struct vfsmount *mnt, int n)
145b3e19d92SNick Piggin {
146b3e19d92SNick Piggin #ifdef CONFIG_SMP
147b3e19d92SNick Piggin 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
148b3e19d92SNick Piggin #else
149b3e19d92SNick Piggin 	preempt_disable();
150b3e19d92SNick Piggin 	mnt->mnt_count += n;
151b3e19d92SNick Piggin 	preempt_enable();
152b3e19d92SNick Piggin #endif
153b3e19d92SNick Piggin }
154b3e19d92SNick Piggin 
155b3e19d92SNick Piggin static inline void mnt_set_count(struct vfsmount *mnt, int n)
156b3e19d92SNick Piggin {
157b3e19d92SNick Piggin #ifdef CONFIG_SMP
158b3e19d92SNick Piggin 	this_cpu_write(mnt->mnt_pcp->mnt_count, n);
159b3e19d92SNick Piggin #else
160b3e19d92SNick Piggin 	mnt->mnt_count = n;
161b3e19d92SNick Piggin #endif
162b3e19d92SNick Piggin }
163b3e19d92SNick Piggin 
164b3e19d92SNick Piggin /*
165b3e19d92SNick Piggin  * vfsmount lock must be held for read
166b3e19d92SNick Piggin  */
167b3e19d92SNick Piggin static inline void mnt_inc_count(struct vfsmount *mnt)
168b3e19d92SNick Piggin {
169b3e19d92SNick Piggin 	mnt_add_count(mnt, 1);
170b3e19d92SNick Piggin }
171b3e19d92SNick Piggin 
172b3e19d92SNick Piggin /*
173b3e19d92SNick Piggin  * vfsmount lock must be held for read
174b3e19d92SNick Piggin  */
175b3e19d92SNick Piggin static inline void mnt_dec_count(struct vfsmount *mnt)
176b3e19d92SNick Piggin {
177b3e19d92SNick Piggin 	mnt_add_count(mnt, -1);
178b3e19d92SNick Piggin }
179b3e19d92SNick Piggin 
180b3e19d92SNick Piggin /*
181b3e19d92SNick Piggin  * vfsmount lock must be held for write
182b3e19d92SNick Piggin  */
183b3e19d92SNick Piggin unsigned int mnt_get_count(struct vfsmount *mnt)
184b3e19d92SNick Piggin {
185b3e19d92SNick Piggin #ifdef CONFIG_SMP
186f03c6599SAl Viro 	unsigned int count = 0;
187b3e19d92SNick Piggin 	int cpu;
188b3e19d92SNick Piggin 
189b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
190b3e19d92SNick Piggin 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
191b3e19d92SNick Piggin 	}
192b3e19d92SNick Piggin 
193b3e19d92SNick Piggin 	return count;
194b3e19d92SNick Piggin #else
195b3e19d92SNick Piggin 	return mnt->mnt_count;
196b3e19d92SNick Piggin #endif
197b3e19d92SNick Piggin }
198b3e19d92SNick Piggin 
1991da177e4SLinus Torvalds struct vfsmount *alloc_vfsmnt(const char *name)
2001da177e4SLinus Torvalds {
201c3762229SRobert P. J. Day 	struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
2021da177e4SLinus Torvalds 	if (mnt) {
20373cd49ecSMiklos Szeredi 		int err;
20473cd49ecSMiklos Szeredi 
20573cd49ecSMiklos Szeredi 		err = mnt_alloc_id(mnt);
20688b38782SLi Zefan 		if (err)
20788b38782SLi Zefan 			goto out_free_cache;
20888b38782SLi Zefan 
20988b38782SLi Zefan 		if (name) {
21088b38782SLi Zefan 			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
21188b38782SLi Zefan 			if (!mnt->mnt_devname)
21288b38782SLi Zefan 				goto out_free_id;
21373cd49ecSMiklos Szeredi 		}
21473cd49ecSMiklos Szeredi 
215b3e19d92SNick Piggin #ifdef CONFIG_SMP
216b3e19d92SNick Piggin 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
217b3e19d92SNick Piggin 		if (!mnt->mnt_pcp)
218b3e19d92SNick Piggin 			goto out_free_devname;
219b3e19d92SNick Piggin 
220f03c6599SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
221b3e19d92SNick Piggin #else
222b3e19d92SNick Piggin 		mnt->mnt_count = 1;
223b3e19d92SNick Piggin 		mnt->mnt_writers = 0;
224b3e19d92SNick Piggin #endif
225b3e19d92SNick Piggin 
2261da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_hash);
2271da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_child);
2281da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_mounts);
2291da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_list);
23055e700b9SMiklos Szeredi 		INIT_LIST_HEAD(&mnt->mnt_expire);
23103e06e68SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_share);
232a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
233a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave);
2342504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY
2352504c5d6SAndreas Gruenbacher 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
2362504c5d6SAndreas Gruenbacher #endif
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 	return mnt;
23988b38782SLi Zefan 
240d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
241d3ef3d73Snpiggin@suse.de out_free_devname:
242d3ef3d73Snpiggin@suse.de 	kfree(mnt->mnt_devname);
243d3ef3d73Snpiggin@suse.de #endif
24488b38782SLi Zefan out_free_id:
24588b38782SLi Zefan 	mnt_free_id(mnt);
24688b38782SLi Zefan out_free_cache:
24788b38782SLi Zefan 	kmem_cache_free(mnt_cache, mnt);
24888b38782SLi Zefan 	return NULL;
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2518366025eSDave Hansen /*
2528366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2538366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2548366025eSDave Hansen  * We must keep track of when those operations start
2558366025eSDave Hansen  * (for permission checks) and when they end, so that
2568366025eSDave Hansen  * we can determine when writes are able to occur to
2578366025eSDave Hansen  * a filesystem.
2588366025eSDave Hansen  */
2593d733633SDave Hansen /*
2603d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2613d733633SDave Hansen  * @mnt: the mount to check for its write status
2623d733633SDave Hansen  *
2633d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2643d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2653d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2663d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2673d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2683d733633SDave Hansen  * r/w.
2693d733633SDave Hansen  */
2703d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2713d733633SDave Hansen {
2722e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2732e4b7fcdSDave Hansen 		return 1;
2742e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
2752e4b7fcdSDave Hansen 		return 1;
2762e4b7fcdSDave Hansen 	return 0;
2773d733633SDave Hansen }
2783d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2793d733633SDave Hansen 
280c6653a83SNick Piggin static inline void mnt_inc_writers(struct vfsmount *mnt)
2813d733633SDave Hansen {
282d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
283b3e19d92SNick Piggin 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
284d3ef3d73Snpiggin@suse.de #else
285d3ef3d73Snpiggin@suse.de 	mnt->mnt_writers++;
286d3ef3d73Snpiggin@suse.de #endif
2873d733633SDave Hansen }
2883d733633SDave Hansen 
289c6653a83SNick Piggin static inline void mnt_dec_writers(struct vfsmount *mnt)
2903d733633SDave Hansen {
291d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
292b3e19d92SNick Piggin 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
293d3ef3d73Snpiggin@suse.de #else
294d3ef3d73Snpiggin@suse.de 	mnt->mnt_writers--;
295d3ef3d73Snpiggin@suse.de #endif
296d3ef3d73Snpiggin@suse.de }
297d3ef3d73Snpiggin@suse.de 
298c6653a83SNick Piggin static unsigned int mnt_get_writers(struct vfsmount *mnt)
299d3ef3d73Snpiggin@suse.de {
300d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
301d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
3023d733633SDave Hansen 	int cpu;
3033d733633SDave Hansen 
3043d733633SDave Hansen 	for_each_possible_cpu(cpu) {
305b3e19d92SNick Piggin 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3063d733633SDave Hansen 	}
3073d733633SDave Hansen 
308d3ef3d73Snpiggin@suse.de 	return count;
309d3ef3d73Snpiggin@suse.de #else
310d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
311d3ef3d73Snpiggin@suse.de #endif
3123d733633SDave Hansen }
3133d733633SDave Hansen 
3143d733633SDave Hansen /*
3153d733633SDave Hansen  * Most r/o checks on a fs are for operations that take
3163d733633SDave Hansen  * discrete amounts of time, like a write() or unlink().
3173d733633SDave Hansen  * We must keep track of when those operations start
3183d733633SDave Hansen  * (for permission checks) and when they end, so that
3193d733633SDave Hansen  * we can determine when writes are able to occur to
3203d733633SDave Hansen  * a filesystem.
3213d733633SDave Hansen  */
3228366025eSDave Hansen /**
3238366025eSDave Hansen  * mnt_want_write - get write access to a mount
3248366025eSDave Hansen  * @mnt: the mount on which to take a write
3258366025eSDave Hansen  *
3268366025eSDave Hansen  * This tells the low-level filesystem that a write is
3278366025eSDave Hansen  * about to be performed to it, and makes sure that
3288366025eSDave Hansen  * writes are allowed before returning success.  When
3298366025eSDave Hansen  * the write operation is finished, mnt_drop_write()
3308366025eSDave Hansen  * must be called.  This is effectively a refcount.
3318366025eSDave Hansen  */
3328366025eSDave Hansen int mnt_want_write(struct vfsmount *mnt)
3338366025eSDave Hansen {
3343d733633SDave Hansen 	int ret = 0;
3353d733633SDave Hansen 
336d3ef3d73Snpiggin@suse.de 	preempt_disable();
337c6653a83SNick Piggin 	mnt_inc_writers(mnt);
338d3ef3d73Snpiggin@suse.de 	/*
339c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
340d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
341d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
342d3ef3d73Snpiggin@suse.de 	 */
343d3ef3d73Snpiggin@suse.de 	smp_mb();
344d3ef3d73Snpiggin@suse.de 	while (mnt->mnt_flags & MNT_WRITE_HOLD)
345d3ef3d73Snpiggin@suse.de 		cpu_relax();
346d3ef3d73Snpiggin@suse.de 	/*
347d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
348d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
349d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
350d3ef3d73Snpiggin@suse.de 	 */
351d3ef3d73Snpiggin@suse.de 	smp_rmb();
3523d733633SDave Hansen 	if (__mnt_is_readonly(mnt)) {
353c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3543d733633SDave Hansen 		ret = -EROFS;
3553d733633SDave Hansen 		goto out;
3563d733633SDave Hansen 	}
3573d733633SDave Hansen out:
358d3ef3d73Snpiggin@suse.de 	preempt_enable();
3593d733633SDave Hansen 	return ret;
3608366025eSDave Hansen }
3618366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3628366025eSDave Hansen 
3638366025eSDave Hansen /**
36496029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
36596029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
36696029c4eSnpiggin@suse.de  *
36796029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
36896029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
36996029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
37096029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
37196029c4eSnpiggin@suse.de  *
37296029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
37396029c4eSnpiggin@suse.de  * drop the reference.
37496029c4eSnpiggin@suse.de  */
37596029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
37696029c4eSnpiggin@suse.de {
37796029c4eSnpiggin@suse.de 	/* superblock may be r/o */
37896029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
37996029c4eSnpiggin@suse.de 		return -EROFS;
38096029c4eSnpiggin@suse.de 	preempt_disable();
381c6653a83SNick Piggin 	mnt_inc_writers(mnt);
38296029c4eSnpiggin@suse.de 	preempt_enable();
38396029c4eSnpiggin@suse.de 	return 0;
38496029c4eSnpiggin@suse.de }
38596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
38696029c4eSnpiggin@suse.de 
38796029c4eSnpiggin@suse.de /**
38896029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
38996029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
39096029c4eSnpiggin@suse.de  *
39196029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
39296029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
39396029c4eSnpiggin@suse.de  */
39496029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
39596029c4eSnpiggin@suse.de {
3962d8dd38aSOGAWA Hirofumi 	struct inode *inode = file->f_dentry->d_inode;
3972d8dd38aSOGAWA Hirofumi 	if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
39896029c4eSnpiggin@suse.de 		return mnt_want_write(file->f_path.mnt);
39996029c4eSnpiggin@suse.de 	else
40096029c4eSnpiggin@suse.de 		return mnt_clone_write(file->f_path.mnt);
40196029c4eSnpiggin@suse.de }
40296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
40396029c4eSnpiggin@suse.de 
40496029c4eSnpiggin@suse.de /**
4058366025eSDave Hansen  * mnt_drop_write - give up write access to a mount
4068366025eSDave Hansen  * @mnt: the mount on which to give up write access
4078366025eSDave Hansen  *
4088366025eSDave Hansen  * Tells the low-level filesystem that we are done
4098366025eSDave Hansen  * performing writes to it.  Must be matched with
4108366025eSDave Hansen  * mnt_want_write() call above.
4118366025eSDave Hansen  */
4128366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt)
4138366025eSDave Hansen {
414d3ef3d73Snpiggin@suse.de 	preempt_disable();
415c6653a83SNick Piggin 	mnt_dec_writers(mnt);
416d3ef3d73Snpiggin@suse.de 	preempt_enable();
4178366025eSDave Hansen }
4188366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
4198366025eSDave Hansen 
4202e4b7fcdSDave Hansen static int mnt_make_readonly(struct vfsmount *mnt)
4218366025eSDave Hansen {
4223d733633SDave Hansen 	int ret = 0;
4233d733633SDave Hansen 
42499b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
425d3ef3d73Snpiggin@suse.de 	mnt->mnt_flags |= MNT_WRITE_HOLD;
426d3ef3d73Snpiggin@suse.de 	/*
427d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
428d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
429d3ef3d73Snpiggin@suse.de 	 */
430d3ef3d73Snpiggin@suse.de 	smp_mb();
431d3ef3d73Snpiggin@suse.de 
432d3ef3d73Snpiggin@suse.de 	/*
433d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
434d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
435d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
436d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
437d3ef3d73Snpiggin@suse.de 	 *
438d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
439d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
440d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
441d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
442d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
443d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
444d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
445d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
446d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
447d3ef3d73Snpiggin@suse.de 	 */
448c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
449d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
450d3ef3d73Snpiggin@suse.de 	else
4512e4b7fcdSDave Hansen 		mnt->mnt_flags |= MNT_READONLY;
452d3ef3d73Snpiggin@suse.de 	/*
453d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
454d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
455d3ef3d73Snpiggin@suse.de 	 */
456d3ef3d73Snpiggin@suse.de 	smp_wmb();
457d3ef3d73Snpiggin@suse.de 	mnt->mnt_flags &= ~MNT_WRITE_HOLD;
45899b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
4593d733633SDave Hansen 	return ret;
4603d733633SDave Hansen }
4618366025eSDave Hansen 
4622e4b7fcdSDave Hansen static void __mnt_unmake_readonly(struct vfsmount *mnt)
4632e4b7fcdSDave Hansen {
46499b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
4652e4b7fcdSDave Hansen 	mnt->mnt_flags &= ~MNT_READONLY;
46699b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
4672e4b7fcdSDave Hansen }
4682e4b7fcdSDave Hansen 
469a3ec947cSSukadev Bhattiprolu void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
470454e2398SDavid Howells {
471454e2398SDavid Howells 	mnt->mnt_sb = sb;
472454e2398SDavid Howells 	mnt->mnt_root = dget(sb->s_root);
473454e2398SDavid Howells }
474454e2398SDavid Howells 
475454e2398SDavid Howells EXPORT_SYMBOL(simple_set_mnt);
476454e2398SDavid Howells 
4771da177e4SLinus Torvalds void free_vfsmnt(struct vfsmount *mnt)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	kfree(mnt->mnt_devname);
48073cd49ecSMiklos Szeredi 	mnt_free_id(mnt);
481d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
482b3e19d92SNick Piggin 	free_percpu(mnt->mnt_pcp);
483d3ef3d73Snpiggin@suse.de #endif
4841da177e4SLinus Torvalds 	kmem_cache_free(mnt_cache, mnt);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /*
488a05964f3SRam Pai  * find the first or last mount at @dentry on vfsmount @mnt depending on
489a05964f3SRam Pai  * @dir. If @dir is set return the first mount else return the last mount.
49099b7db7bSNick Piggin  * vfsmount_lock must be held for read or write.
4911da177e4SLinus Torvalds  */
492a05964f3SRam Pai struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
493a05964f3SRam Pai 			      int dir)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
4961da177e4SLinus Torvalds 	struct list_head *tmp = head;
4971da177e4SLinus Torvalds 	struct vfsmount *p, *found = NULL;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	for (;;) {
500a05964f3SRam Pai 		tmp = dir ? tmp->next : tmp->prev;
5011da177e4SLinus Torvalds 		p = NULL;
5021da177e4SLinus Torvalds 		if (tmp == head)
5031da177e4SLinus Torvalds 			break;
5041da177e4SLinus Torvalds 		p = list_entry(tmp, struct vfsmount, mnt_hash);
5051da177e4SLinus Torvalds 		if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
506a05964f3SRam Pai 			found = p;
5071da177e4SLinus Torvalds 			break;
5081da177e4SLinus Torvalds 		}
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 	return found;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
513a05964f3SRam Pai /*
514a05964f3SRam Pai  * lookup_mnt increments the ref count before returning
515a05964f3SRam Pai  * the vfsmount struct.
516a05964f3SRam Pai  */
5171c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
518a05964f3SRam Pai {
519a05964f3SRam Pai 	struct vfsmount *child_mnt;
52099b7db7bSNick Piggin 
52199b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
5221c755af4SAl Viro 	if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1)))
523a05964f3SRam Pai 		mntget(child_mnt);
52499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
525a05964f3SRam Pai 	return child_mnt;
526a05964f3SRam Pai }
527a05964f3SRam Pai 
5281da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt)
5291da177e4SLinus Torvalds {
5306b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
53399b7db7bSNick Piggin /*
53499b7db7bSNick Piggin  * vfsmount lock must be held for write
53599b7db7bSNick Piggin  */
5366b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
5375addc5ddSAl Viro {
5385addc5ddSAl Viro 	if (ns) {
5395addc5ddSAl Viro 		ns->event = ++event;
5405addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
5415addc5ddSAl Viro 	}
5425addc5ddSAl Viro }
5435addc5ddSAl Viro 
54499b7db7bSNick Piggin /*
54599b7db7bSNick Piggin  * vfsmount lock must be held for write
54699b7db7bSNick Piggin  */
5476b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
5485addc5ddSAl Viro {
5495addc5ddSAl Viro 	if (ns && ns->event != event) {
5505addc5ddSAl Viro 		ns->event = event;
5515addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
5525addc5ddSAl Viro 	}
5535addc5ddSAl Viro }
5545addc5ddSAl Viro 
55599b7db7bSNick Piggin /*
5565f57cbccSNick Piggin  * Clear dentry's mounted state if it has no remaining mounts.
5575f57cbccSNick Piggin  * vfsmount_lock must be held for write.
5585f57cbccSNick Piggin  */
5595f57cbccSNick Piggin static void dentry_reset_mounted(struct vfsmount *mnt, struct dentry *dentry)
5605f57cbccSNick Piggin {
5615f57cbccSNick Piggin 	unsigned u;
5625f57cbccSNick Piggin 
5635f57cbccSNick Piggin 	for (u = 0; u < HASH_SIZE; u++) {
5645f57cbccSNick Piggin 		struct vfsmount *p;
5655f57cbccSNick Piggin 
5665f57cbccSNick Piggin 		list_for_each_entry(p, &mount_hashtable[u], mnt_hash) {
5675f57cbccSNick Piggin 			if (p->mnt_mountpoint == dentry)
5685f57cbccSNick Piggin 				return;
5695f57cbccSNick Piggin 		}
5705f57cbccSNick Piggin 	}
5715f57cbccSNick Piggin 	spin_lock(&dentry->d_lock);
5725f57cbccSNick Piggin 	dentry->d_flags &= ~DCACHE_MOUNTED;
5735f57cbccSNick Piggin 	spin_unlock(&dentry->d_lock);
5745f57cbccSNick Piggin }
5755f57cbccSNick Piggin 
5765f57cbccSNick Piggin /*
57799b7db7bSNick Piggin  * vfsmount lock must be held for write
57899b7db7bSNick Piggin  */
5791a390689SAl Viro static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
5801da177e4SLinus Torvalds {
5811a390689SAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
5821a390689SAl Viro 	old_path->mnt = mnt->mnt_parent;
5831da177e4SLinus Torvalds 	mnt->mnt_parent = mnt;
5841da177e4SLinus Torvalds 	mnt->mnt_mountpoint = mnt->mnt_root;
5851da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_child);
5861da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_hash);
5875f57cbccSNick Piggin 	dentry_reset_mounted(old_path->mnt, old_path->dentry);
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
59099b7db7bSNick Piggin /*
59199b7db7bSNick Piggin  * vfsmount lock must be held for write
59299b7db7bSNick Piggin  */
593b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
594b90fa9aeSRam Pai 			struct vfsmount *child_mnt)
595b90fa9aeSRam Pai {
596b90fa9aeSRam Pai 	child_mnt->mnt_parent = mntget(mnt);
597b90fa9aeSRam Pai 	child_mnt->mnt_mountpoint = dget(dentry);
5985f57cbccSNick Piggin 	spin_lock(&dentry->d_lock);
5995f57cbccSNick Piggin 	dentry->d_flags |= DCACHE_MOUNTED;
6005f57cbccSNick Piggin 	spin_unlock(&dentry->d_lock);
601b90fa9aeSRam Pai }
602b90fa9aeSRam Pai 
60399b7db7bSNick Piggin /*
60499b7db7bSNick Piggin  * vfsmount lock must be held for write
60599b7db7bSNick Piggin  */
6061a390689SAl Viro static void attach_mnt(struct vfsmount *mnt, struct path *path)
6071da177e4SLinus Torvalds {
6081a390689SAl Viro 	mnt_set_mountpoint(path->mnt, path->dentry, mnt);
609b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
6101a390689SAl Viro 			hash(path->mnt, path->dentry));
6111a390689SAl Viro 	list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
612b90fa9aeSRam Pai }
613b90fa9aeSRam Pai 
6147e3d0eb0SAl Viro static inline void __mnt_make_longterm(struct vfsmount *mnt)
6157e3d0eb0SAl Viro {
6167e3d0eb0SAl Viro #ifdef CONFIG_SMP
6177e3d0eb0SAl Viro 	atomic_inc(&mnt->mnt_longterm);
6187e3d0eb0SAl Viro #endif
6197e3d0eb0SAl Viro }
6207e3d0eb0SAl Viro 
6217e3d0eb0SAl Viro /* needs vfsmount lock for write */
6227e3d0eb0SAl Viro static inline void __mnt_make_shortterm(struct vfsmount *mnt)
6237e3d0eb0SAl Viro {
6247e3d0eb0SAl Viro #ifdef CONFIG_SMP
6257e3d0eb0SAl Viro 	atomic_dec(&mnt->mnt_longterm);
6267e3d0eb0SAl Viro #endif
6277e3d0eb0SAl Viro }
6287e3d0eb0SAl Viro 
629b90fa9aeSRam Pai /*
63099b7db7bSNick Piggin  * vfsmount lock must be held for write
631b90fa9aeSRam Pai  */
632b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt)
633b90fa9aeSRam Pai {
634b90fa9aeSRam Pai 	struct vfsmount *parent = mnt->mnt_parent;
635b90fa9aeSRam Pai 	struct vfsmount *m;
636b90fa9aeSRam Pai 	LIST_HEAD(head);
6376b3286edSKirill Korotaev 	struct mnt_namespace *n = parent->mnt_ns;
638b90fa9aeSRam Pai 
639b90fa9aeSRam Pai 	BUG_ON(parent == mnt);
640b90fa9aeSRam Pai 
641b90fa9aeSRam Pai 	list_add_tail(&head, &mnt->mnt_list);
642f03c6599SAl Viro 	list_for_each_entry(m, &head, mnt_list) {
6436b3286edSKirill Korotaev 		m->mnt_ns = n;
6447e3d0eb0SAl Viro 		__mnt_make_longterm(m);
645f03c6599SAl Viro 	}
646f03c6599SAl Viro 
647b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
648b90fa9aeSRam Pai 
649b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
650b90fa9aeSRam Pai 				hash(parent, mnt->mnt_mountpoint));
651b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
6526b3286edSKirill Korotaev 	touch_mnt_namespace(n);
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	struct list_head *next = p->mnt_mounts.next;
6581da177e4SLinus Torvalds 	if (next == &p->mnt_mounts) {
6591da177e4SLinus Torvalds 		while (1) {
6601da177e4SLinus Torvalds 			if (p == root)
6611da177e4SLinus Torvalds 				return NULL;
6621da177e4SLinus Torvalds 			next = p->mnt_child.next;
6631da177e4SLinus Torvalds 			if (next != &p->mnt_parent->mnt_mounts)
6641da177e4SLinus Torvalds 				break;
6651da177e4SLinus Torvalds 			p = p->mnt_parent;
6661da177e4SLinus Torvalds 		}
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 	return list_entry(next, struct vfsmount, mnt_child);
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6719676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
6729676f0c6SRam Pai {
6739676f0c6SRam Pai 	struct list_head *prev = p->mnt_mounts.prev;
6749676f0c6SRam Pai 	while (prev != &p->mnt_mounts) {
6759676f0c6SRam Pai 		p = list_entry(prev, struct vfsmount, mnt_child);
6769676f0c6SRam Pai 		prev = p->mnt_mounts.prev;
6779676f0c6SRam Pai 	}
6789676f0c6SRam Pai 	return p;
6799676f0c6SRam Pai }
6809676f0c6SRam Pai 
68136341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
68236341f64SRam Pai 					int flag)
6831da177e4SLinus Torvalds {
6841da177e4SLinus Torvalds 	struct super_block *sb = old->mnt_sb;
6851da177e4SLinus Torvalds 	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	if (mnt) {
688719f5d7fSMiklos Szeredi 		if (flag & (CL_SLAVE | CL_PRIVATE))
689719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = 0; /* not a peer of original */
690719f5d7fSMiklos Szeredi 		else
691719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = old->mnt_group_id;
692719f5d7fSMiklos Szeredi 
693719f5d7fSMiklos Szeredi 		if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
694719f5d7fSMiklos Szeredi 			int err = mnt_alloc_group_id(mnt);
695719f5d7fSMiklos Szeredi 			if (err)
696719f5d7fSMiklos Szeredi 				goto out_free;
697719f5d7fSMiklos Szeredi 		}
698719f5d7fSMiklos Szeredi 
699be1a16a0SMiklos Szeredi 		mnt->mnt_flags = old->mnt_flags & ~MNT_WRITE_HOLD;
7001da177e4SLinus Torvalds 		atomic_inc(&sb->s_active);
7011da177e4SLinus Torvalds 		mnt->mnt_sb = sb;
7021da177e4SLinus Torvalds 		mnt->mnt_root = dget(root);
7031da177e4SLinus Torvalds 		mnt->mnt_mountpoint = mnt->mnt_root;
7041da177e4SLinus Torvalds 		mnt->mnt_parent = mnt;
705b90fa9aeSRam Pai 
7065afe0022SRam Pai 		if (flag & CL_SLAVE) {
7075afe0022SRam Pai 			list_add(&mnt->mnt_slave, &old->mnt_slave_list);
7085afe0022SRam Pai 			mnt->mnt_master = old;
7095afe0022SRam Pai 			CLEAR_MNT_SHARED(mnt);
7108aec0809SAl Viro 		} else if (!(flag & CL_PRIVATE)) {
711796a6b52SAl Viro 			if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
712b90fa9aeSRam Pai 				list_add(&mnt->mnt_share, &old->mnt_share);
7135afe0022SRam Pai 			if (IS_MNT_SLAVE(old))
7145afe0022SRam Pai 				list_add(&mnt->mnt_slave, &old->mnt_slave);
7155afe0022SRam Pai 			mnt->mnt_master = old->mnt_master;
7165afe0022SRam Pai 		}
717b90fa9aeSRam Pai 		if (flag & CL_MAKE_SHARED)
718b90fa9aeSRam Pai 			set_mnt_shared(mnt);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 		/* stick the duplicate mount on the same expiry list
7211da177e4SLinus Torvalds 		 * as the original if that was on one */
72236341f64SRam Pai 		if (flag & CL_EXPIRE) {
72355e700b9SMiklos Szeredi 			if (!list_empty(&old->mnt_expire))
72455e700b9SMiklos Szeredi 				list_add(&mnt->mnt_expire, &old->mnt_expire);
7251da177e4SLinus Torvalds 		}
72636341f64SRam Pai 	}
7271da177e4SLinus Torvalds 	return mnt;
728719f5d7fSMiklos Szeredi 
729719f5d7fSMiklos Szeredi  out_free:
730719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
731719f5d7fSMiklos Szeredi 	return NULL;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734b3e19d92SNick Piggin static inline void mntfree(struct vfsmount *mnt)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
737b3e19d92SNick Piggin 
7383d733633SDave Hansen 	/*
7393d733633SDave Hansen 	 * This probably indicates that somebody messed
7403d733633SDave Hansen 	 * up a mnt_want/drop_write() pair.  If this
7413d733633SDave Hansen 	 * happens, the filesystem was probably unable
7423d733633SDave Hansen 	 * to make r/w->r/o transitions.
7433d733633SDave Hansen 	 */
744d3ef3d73Snpiggin@suse.de 	/*
745b3e19d92SNick Piggin 	 * The locking used to deal with mnt_count decrement provides barriers,
746b3e19d92SNick Piggin 	 * so mnt_get_writers() below is safe.
747d3ef3d73Snpiggin@suse.de 	 */
748c6653a83SNick Piggin 	WARN_ON(mnt_get_writers(mnt));
749ca9c726eSAndreas Gruenbacher 	fsnotify_vfsmount_delete(mnt);
7501da177e4SLinus Torvalds 	dput(mnt->mnt_root);
7511da177e4SLinus Torvalds 	free_vfsmnt(mnt);
7521da177e4SLinus Torvalds 	deactivate_super(sb);
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
755f03c6599SAl Viro static void mntput_no_expire(struct vfsmount *mnt)
7567b7b1aceSAl Viro {
757b3e19d92SNick Piggin put_again:
758f03c6599SAl Viro #ifdef CONFIG_SMP
759b3e19d92SNick Piggin 	br_read_lock(vfsmount_lock);
760f03c6599SAl Viro 	if (likely(atomic_read(&mnt->mnt_longterm))) {
761b3e19d92SNick Piggin 		mnt_dec_count(mnt);
762b3e19d92SNick Piggin 		br_read_unlock(vfsmount_lock);
76399b7db7bSNick Piggin 		return;
764b3e19d92SNick Piggin 	}
765b3e19d92SNick Piggin 	br_read_unlock(vfsmount_lock);
766b3e19d92SNick Piggin 
76799b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
768b3e19d92SNick Piggin 	mnt_dec_count(mnt);
769b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
77099b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
77199b7db7bSNick Piggin 		return;
77299b7db7bSNick Piggin 	}
773b3e19d92SNick Piggin #else
774b3e19d92SNick Piggin 	mnt_dec_count(mnt);
775b3e19d92SNick Piggin 	if (likely(mnt_get_count(mnt)))
776b3e19d92SNick Piggin 		return;
777b3e19d92SNick Piggin 	br_write_lock(vfsmount_lock);
778f03c6599SAl Viro #endif
779b3e19d92SNick Piggin 	if (unlikely(mnt->mnt_pinned)) {
780b3e19d92SNick Piggin 		mnt_add_count(mnt, mnt->mnt_pinned + 1);
781b3e19d92SNick Piggin 		mnt->mnt_pinned = 0;
782b3e19d92SNick Piggin 		br_write_unlock(vfsmount_lock);
783b3e19d92SNick Piggin 		acct_auto_close_mnt(mnt);
784b3e19d92SNick Piggin 		goto put_again;
785b3e19d92SNick Piggin 	}
786b3e19d92SNick Piggin 	br_write_unlock(vfsmount_lock);
787b3e19d92SNick Piggin 	mntfree(mnt);
788b3e19d92SNick Piggin }
789b3e19d92SNick Piggin 
790b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
791b3e19d92SNick Piggin {
792b3e19d92SNick Piggin 	if (mnt) {
793b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
794b3e19d92SNick Piggin 		if (unlikely(mnt->mnt_expiry_mark))
795b3e19d92SNick Piggin 			mnt->mnt_expiry_mark = 0;
796f03c6599SAl Viro 		mntput_no_expire(mnt);
797b3e19d92SNick Piggin 	}
798b3e19d92SNick Piggin }
799b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
800b3e19d92SNick Piggin 
801b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
802b3e19d92SNick Piggin {
803b3e19d92SNick Piggin 	if (mnt)
804b3e19d92SNick Piggin 		mnt_inc_count(mnt);
805b3e19d92SNick Piggin 	return mnt;
806b3e19d92SNick Piggin }
807b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
808b3e19d92SNick Piggin 
8097b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
8107b7b1aceSAl Viro {
81199b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
8127b7b1aceSAl Viro 	mnt->mnt_pinned++;
81399b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
8147b7b1aceSAl Viro }
8157b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
8167b7b1aceSAl Viro 
8177b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt)
8187b7b1aceSAl Viro {
81999b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
8207b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
821b3e19d92SNick Piggin 		mnt_inc_count(mnt);
8227b7b1aceSAl Viro 		mnt->mnt_pinned--;
8237b7b1aceSAl Viro 	}
82499b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
8257b7b1aceSAl Viro }
8267b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
8271da177e4SLinus Torvalds 
828b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
829b3b304a2SMiklos Szeredi {
830b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
831b3b304a2SMiklos Szeredi }
832b3b304a2SMiklos Szeredi 
833b3b304a2SMiklos Szeredi /*
834b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
835b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
836b3b304a2SMiklos Szeredi  *
837b3b304a2SMiklos Szeredi  * See also save_mount_options().
838b3b304a2SMiklos Szeredi  */
839b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
840b3b304a2SMiklos Szeredi {
8412a32cebdSAl Viro 	const char *options;
8422a32cebdSAl Viro 
8432a32cebdSAl Viro 	rcu_read_lock();
8442a32cebdSAl Viro 	options = rcu_dereference(mnt->mnt_sb->s_options);
845b3b304a2SMiklos Szeredi 
846b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
847b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
848b3b304a2SMiklos Szeredi 		mangle(m, options);
849b3b304a2SMiklos Szeredi 	}
8502a32cebdSAl Viro 	rcu_read_unlock();
851b3b304a2SMiklos Szeredi 
852b3b304a2SMiklos Szeredi 	return 0;
853b3b304a2SMiklos Szeredi }
854b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
855b3b304a2SMiklos Szeredi 
856b3b304a2SMiklos Szeredi /*
857b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
858b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
859b3b304a2SMiklos Szeredi  *
860b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
861b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
862b3b304a2SMiklos Szeredi  * remount fails.
863b3b304a2SMiklos Szeredi  *
864b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
865b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
866b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
867b3b304a2SMiklos Szeredi  * any more.
868b3b304a2SMiklos Szeredi  */
869b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
870b3b304a2SMiklos Szeredi {
8712a32cebdSAl Viro 	BUG_ON(sb->s_options);
8722a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
873b3b304a2SMiklos Szeredi }
874b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
875b3b304a2SMiklos Szeredi 
8762a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
8772a32cebdSAl Viro {
8782a32cebdSAl Viro 	char *old = sb->s_options;
8792a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
8802a32cebdSAl Viro 	if (old) {
8812a32cebdSAl Viro 		synchronize_rcu();
8822a32cebdSAl Viro 		kfree(old);
8832a32cebdSAl Viro 	}
8842a32cebdSAl Viro }
8852a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
8862a32cebdSAl Viro 
887a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
8881da177e4SLinus Torvalds /* iterator */
8891da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
8901da177e4SLinus Torvalds {
891a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
8921da177e4SLinus Torvalds 
893390c6843SRam Pai 	down_read(&namespace_sem);
894a1a2c409SMiklos Szeredi 	return seq_list_start(&p->ns->list, *pos);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
8981da177e4SLinus Torvalds {
899a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
900b0765fb8SPavel Emelianov 
901a1a2c409SMiklos Szeredi 	return seq_list_next(v, &p->ns->list, pos);
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
9051da177e4SLinus Torvalds {
906390c6843SRam Pai 	up_read(&namespace_sem);
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9099f5596afSAl Viro int mnt_had_events(struct proc_mounts *p)
9109f5596afSAl Viro {
9119f5596afSAl Viro 	struct mnt_namespace *ns = p->ns;
9129f5596afSAl Viro 	int res = 0;
9139f5596afSAl Viro 
91499b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
9159f5596afSAl Viro 	if (p->event != ns->event) {
9169f5596afSAl Viro 		p->event = ns->event;
9179f5596afSAl Viro 		res = 1;
9189f5596afSAl Viro 	}
91999b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
9209f5596afSAl Viro 
9219f5596afSAl Viro 	return res;
9229f5596afSAl Viro }
9239f5596afSAl Viro 
9242d4d4864SRam Pai struct proc_fs_info {
9251da177e4SLinus Torvalds 	int flag;
9262d4d4864SRam Pai 	const char *str;
9272d4d4864SRam Pai };
9282d4d4864SRam Pai 
9292069f457SEric Paris static int show_sb_opts(struct seq_file *m, struct super_block *sb)
9302d4d4864SRam Pai {
9312d4d4864SRam Pai 	static const struct proc_fs_info fs_info[] = {
9321da177e4SLinus Torvalds 		{ MS_SYNCHRONOUS, ",sync" },
9331da177e4SLinus Torvalds 		{ MS_DIRSYNC, ",dirsync" },
9341da177e4SLinus Torvalds 		{ MS_MANDLOCK, ",mand" },
9351da177e4SLinus Torvalds 		{ 0, NULL }
9361da177e4SLinus Torvalds 	};
9372d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
9382d4d4864SRam Pai 
9392d4d4864SRam Pai 	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
9402d4d4864SRam Pai 		if (sb->s_flags & fs_infop->flag)
9412d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
9422d4d4864SRam Pai 	}
9432069f457SEric Paris 
9442069f457SEric Paris 	return security_sb_show_options(m, sb);
9452d4d4864SRam Pai }
9462d4d4864SRam Pai 
9472d4d4864SRam Pai static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
9482d4d4864SRam Pai {
9492d4d4864SRam Pai 	static const struct proc_fs_info mnt_info[] = {
9501da177e4SLinus Torvalds 		{ MNT_NOSUID, ",nosuid" },
9511da177e4SLinus Torvalds 		{ MNT_NODEV, ",nodev" },
9521da177e4SLinus Torvalds 		{ MNT_NOEXEC, ",noexec" },
953fc33a7bbSChristoph Hellwig 		{ MNT_NOATIME, ",noatime" },
954fc33a7bbSChristoph Hellwig 		{ MNT_NODIRATIME, ",nodiratime" },
95547ae32d6SValerie Henson 		{ MNT_RELATIME, ",relatime" },
9561da177e4SLinus Torvalds 		{ 0, NULL }
9571da177e4SLinus Torvalds 	};
9582d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
9592d4d4864SRam Pai 
9602d4d4864SRam Pai 	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
9612d4d4864SRam Pai 		if (mnt->mnt_flags & fs_infop->flag)
9622d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
9632d4d4864SRam Pai 	}
9642d4d4864SRam Pai }
9652d4d4864SRam Pai 
9662d4d4864SRam Pai static void show_type(struct seq_file *m, struct super_block *sb)
9672d4d4864SRam Pai {
9682d4d4864SRam Pai 	mangle(m, sb->s_type->name);
9692d4d4864SRam Pai 	if (sb->s_subtype && sb->s_subtype[0]) {
9702d4d4864SRam Pai 		seq_putc(m, '.');
9712d4d4864SRam Pai 		mangle(m, sb->s_subtype);
9722d4d4864SRam Pai 	}
9732d4d4864SRam Pai }
9742d4d4864SRam Pai 
9752d4d4864SRam Pai static int show_vfsmnt(struct seq_file *m, void *v)
9762d4d4864SRam Pai {
9772d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
9782d4d4864SRam Pai 	int err = 0;
979c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
9821da177e4SLinus Torvalds 	seq_putc(m, ' ');
983c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
9841da177e4SLinus Torvalds 	seq_putc(m, ' ');
9852d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
9862e4b7fcdSDave Hansen 	seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
9872069f457SEric Paris 	err = show_sb_opts(m, mnt->mnt_sb);
9882069f457SEric Paris 	if (err)
9892069f457SEric Paris 		goto out;
9902d4d4864SRam Pai 	show_mnt_opts(m, mnt);
9911da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_op->show_options)
9921da177e4SLinus Torvalds 		err = mnt->mnt_sb->s_op->show_options(m, mnt);
9931da177e4SLinus Torvalds 	seq_puts(m, " 0 0\n");
9942069f457SEric Paris out:
9951da177e4SLinus Torvalds 	return err;
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
998a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
9991da177e4SLinus Torvalds 	.start	= m_start,
10001da177e4SLinus Torvalds 	.next	= m_next,
10011da177e4SLinus Torvalds 	.stop	= m_stop,
10021da177e4SLinus Torvalds 	.show	= show_vfsmnt
10031da177e4SLinus Torvalds };
10041da177e4SLinus Torvalds 
10052d4d4864SRam Pai static int show_mountinfo(struct seq_file *m, void *v)
10062d4d4864SRam Pai {
10072d4d4864SRam Pai 	struct proc_mounts *p = m->private;
10082d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
10092d4d4864SRam Pai 	struct super_block *sb = mnt->mnt_sb;
10102d4d4864SRam Pai 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
10112d4d4864SRam Pai 	struct path root = p->root;
10122d4d4864SRam Pai 	int err = 0;
10132d4d4864SRam Pai 
10142d4d4864SRam Pai 	seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
10152d4d4864SRam Pai 		   MAJOR(sb->s_dev), MINOR(sb->s_dev));
10162d4d4864SRam Pai 	seq_dentry(m, mnt->mnt_root, " \t\n\\");
10172d4d4864SRam Pai 	seq_putc(m, ' ');
10182d4d4864SRam Pai 	seq_path_root(m, &mnt_path, &root, " \t\n\\");
10192d4d4864SRam Pai 	if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
10202d4d4864SRam Pai 		/*
10212d4d4864SRam Pai 		 * Mountpoint is outside root, discard that one.  Ugly,
10222d4d4864SRam Pai 		 * but less so than trying to do that in iterator in a
10232d4d4864SRam Pai 		 * race-free way (due to renames).
10242d4d4864SRam Pai 		 */
10252d4d4864SRam Pai 		return SEQ_SKIP;
10262d4d4864SRam Pai 	}
10272d4d4864SRam Pai 	seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
10282d4d4864SRam Pai 	show_mnt_opts(m, mnt);
10292d4d4864SRam Pai 
10302d4d4864SRam Pai 	/* Tagged fields ("foo:X" or "bar") */
10312d4d4864SRam Pai 	if (IS_MNT_SHARED(mnt))
10322d4d4864SRam Pai 		seq_printf(m, " shared:%i", mnt->mnt_group_id);
103397e7e0f7SMiklos Szeredi 	if (IS_MNT_SLAVE(mnt)) {
103497e7e0f7SMiklos Szeredi 		int master = mnt->mnt_master->mnt_group_id;
103597e7e0f7SMiklos Szeredi 		int dom = get_dominating_id(mnt, &p->root);
103697e7e0f7SMiklos Szeredi 		seq_printf(m, " master:%i", master);
103797e7e0f7SMiklos Szeredi 		if (dom && dom != master)
103897e7e0f7SMiklos Szeredi 			seq_printf(m, " propagate_from:%i", dom);
103997e7e0f7SMiklos Szeredi 	}
10402d4d4864SRam Pai 	if (IS_MNT_UNBINDABLE(mnt))
10412d4d4864SRam Pai 		seq_puts(m, " unbindable");
10422d4d4864SRam Pai 
10432d4d4864SRam Pai 	/* Filesystem specific data */
10442d4d4864SRam Pai 	seq_puts(m, " - ");
10452d4d4864SRam Pai 	show_type(m, sb);
10462d4d4864SRam Pai 	seq_putc(m, ' ');
10472d4d4864SRam Pai 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
10482d4d4864SRam Pai 	seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
10492069f457SEric Paris 	err = show_sb_opts(m, sb);
10502069f457SEric Paris 	if (err)
10512069f457SEric Paris 		goto out;
10522d4d4864SRam Pai 	if (sb->s_op->show_options)
10532d4d4864SRam Pai 		err = sb->s_op->show_options(m, mnt);
10542d4d4864SRam Pai 	seq_putc(m, '\n');
10552069f457SEric Paris out:
10562d4d4864SRam Pai 	return err;
10572d4d4864SRam Pai }
10582d4d4864SRam Pai 
10592d4d4864SRam Pai const struct seq_operations mountinfo_op = {
10602d4d4864SRam Pai 	.start	= m_start,
10612d4d4864SRam Pai 	.next	= m_next,
10622d4d4864SRam Pai 	.stop	= m_stop,
10632d4d4864SRam Pai 	.show	= show_mountinfo,
10642d4d4864SRam Pai };
10652d4d4864SRam Pai 
1066b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v)
1067b4629fe2SChuck Lever {
1068b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
1069c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
1070b4629fe2SChuck Lever 	int err = 0;
1071b4629fe2SChuck Lever 
1072b4629fe2SChuck Lever 	/* device */
1073b4629fe2SChuck Lever 	if (mnt->mnt_devname) {
1074b4629fe2SChuck Lever 		seq_puts(m, "device ");
1075b4629fe2SChuck Lever 		mangle(m, mnt->mnt_devname);
1076b4629fe2SChuck Lever 	} else
1077b4629fe2SChuck Lever 		seq_puts(m, "no device");
1078b4629fe2SChuck Lever 
1079b4629fe2SChuck Lever 	/* mount point */
1080b4629fe2SChuck Lever 	seq_puts(m, " mounted on ");
1081c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
1082b4629fe2SChuck Lever 	seq_putc(m, ' ');
1083b4629fe2SChuck Lever 
1084b4629fe2SChuck Lever 	/* file system type */
1085b4629fe2SChuck Lever 	seq_puts(m, "with fstype ");
10862d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
1087b4629fe2SChuck Lever 
1088b4629fe2SChuck Lever 	/* optional statistics */
1089b4629fe2SChuck Lever 	if (mnt->mnt_sb->s_op->show_stats) {
1090b4629fe2SChuck Lever 		seq_putc(m, ' ');
1091b4629fe2SChuck Lever 		err = mnt->mnt_sb->s_op->show_stats(m, mnt);
1092b4629fe2SChuck Lever 	}
1093b4629fe2SChuck Lever 
1094b4629fe2SChuck Lever 	seq_putc(m, '\n');
1095b4629fe2SChuck Lever 	return err;
1096b4629fe2SChuck Lever }
1097b4629fe2SChuck Lever 
1098a1a2c409SMiklos Szeredi const struct seq_operations mountstats_op = {
1099b4629fe2SChuck Lever 	.start	= m_start,
1100b4629fe2SChuck Lever 	.next	= m_next,
1101b4629fe2SChuck Lever 	.stop	= m_stop,
1102b4629fe2SChuck Lever 	.show	= show_vfsstat,
1103b4629fe2SChuck Lever };
1104a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1105b4629fe2SChuck Lever 
11061da177e4SLinus Torvalds /**
11071da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
11081da177e4SLinus Torvalds  * @mnt: root of mount tree
11091da177e4SLinus Torvalds  *
11101da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
11111da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
11121da177e4SLinus Torvalds  * busy.
11131da177e4SLinus Torvalds  */
11141da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt)
11151da177e4SLinus Torvalds {
111636341f64SRam Pai 	int actual_refs = 0;
111736341f64SRam Pai 	int minimum_refs = 0;
111836341f64SRam Pai 	struct vfsmount *p;
11191da177e4SLinus Torvalds 
1120b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1121b3e19d92SNick Piggin 	br_write_lock(vfsmount_lock);
112236341f64SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1123b3e19d92SNick Piggin 		actual_refs += mnt_get_count(p);
11241da177e4SLinus Torvalds 		minimum_refs += 2;
11251da177e4SLinus Torvalds 	}
1126b3e19d92SNick Piggin 	br_write_unlock(vfsmount_lock);
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
11291da177e4SLinus Torvalds 		return 0;
1130e3474a8eSIan Kent 
1131e3474a8eSIan Kent 	return 1;
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds /**
11371da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
11381da177e4SLinus Torvalds  * @mnt: root of mount
11391da177e4SLinus Torvalds  *
11401da177e4SLinus Torvalds  * This is called to check if a mount point has any
11411da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
11421da177e4SLinus Torvalds  * mount has sub mounts this will return busy
11431da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
11441da177e4SLinus Torvalds  *
11451da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
11461da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
11471da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
11481da177e4SLinus Torvalds  */
11491da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
11501da177e4SLinus Torvalds {
1151e3474a8eSIan Kent 	int ret = 1;
11528ad08d8aSAl Viro 	down_read(&namespace_sem);
1153b3e19d92SNick Piggin 	br_write_lock(vfsmount_lock);
1154a05964f3SRam Pai 	if (propagate_mount_busy(mnt, 2))
1155e3474a8eSIan Kent 		ret = 0;
1156b3e19d92SNick Piggin 	br_write_unlock(vfsmount_lock);
11578ad08d8aSAl Viro 	up_read(&namespace_sem);
1158a05964f3SRam Pai 	return ret;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
11621da177e4SLinus Torvalds 
1163b90fa9aeSRam Pai void release_mounts(struct list_head *head)
11641da177e4SLinus Torvalds {
116570fbcdf4SRam Pai 	struct vfsmount *mnt;
116670fbcdf4SRam Pai 	while (!list_empty(head)) {
1167b5e61818SPavel Emelianov 		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
116870fbcdf4SRam Pai 		list_del_init(&mnt->mnt_hash);
116970fbcdf4SRam Pai 		if (mnt->mnt_parent != mnt) {
117070fbcdf4SRam Pai 			struct dentry *dentry;
117170fbcdf4SRam Pai 			struct vfsmount *m;
117299b7db7bSNick Piggin 
117399b7db7bSNick Piggin 			br_write_lock(vfsmount_lock);
117470fbcdf4SRam Pai 			dentry = mnt->mnt_mountpoint;
117570fbcdf4SRam Pai 			m = mnt->mnt_parent;
117670fbcdf4SRam Pai 			mnt->mnt_mountpoint = mnt->mnt_root;
117770fbcdf4SRam Pai 			mnt->mnt_parent = mnt;
11787c4b93d8SAl Viro 			m->mnt_ghosts--;
117999b7db7bSNick Piggin 			br_write_unlock(vfsmount_lock);
118070fbcdf4SRam Pai 			dput(dentry);
118170fbcdf4SRam Pai 			mntput(m);
11821da177e4SLinus Torvalds 		}
1183f03c6599SAl Viro 		mntput(mnt);
118470fbcdf4SRam Pai 	}
118570fbcdf4SRam Pai }
118670fbcdf4SRam Pai 
118799b7db7bSNick Piggin /*
118899b7db7bSNick Piggin  * vfsmount lock must be held for write
118999b7db7bSNick Piggin  * namespace_sem must be held for write
119099b7db7bSNick Piggin  */
1191a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
119270fbcdf4SRam Pai {
11937b8a53fdSAl Viro 	LIST_HEAD(tmp_list);
119470fbcdf4SRam Pai 	struct vfsmount *p;
119570fbcdf4SRam Pai 
11961bfba4e8SAkinobu Mita 	for (p = mnt; p; p = next_mnt(p, mnt))
11977b8a53fdSAl Viro 		list_move(&p->mnt_hash, &tmp_list);
119870fbcdf4SRam Pai 
1199a05964f3SRam Pai 	if (propagate)
12007b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1201a05964f3SRam Pai 
12027b8a53fdSAl Viro 	list_for_each_entry(p, &tmp_list, mnt_hash) {
120370fbcdf4SRam Pai 		list_del_init(&p->mnt_expire);
120470fbcdf4SRam Pai 		list_del_init(&p->mnt_list);
12056b3286edSKirill Korotaev 		__touch_mnt_namespace(p->mnt_ns);
12066b3286edSKirill Korotaev 		p->mnt_ns = NULL;
12077e3d0eb0SAl Viro 		__mnt_make_shortterm(p);
120870fbcdf4SRam Pai 		list_del_init(&p->mnt_child);
12097c4b93d8SAl Viro 		if (p->mnt_parent != p) {
12107c4b93d8SAl Viro 			p->mnt_parent->mnt_ghosts++;
12115f57cbccSNick Piggin 			dentry_reset_mounted(p->mnt_parent, p->mnt_mountpoint);
12127c4b93d8SAl Viro 		}
1213a05964f3SRam Pai 		change_mnt_propagation(p, MS_PRIVATE);
12141da177e4SLinus Torvalds 	}
12157b8a53fdSAl Viro 	list_splice(&tmp_list, kill);
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
1218c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
1219c35038beSAl Viro 
12201da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags)
12211da177e4SLinus Torvalds {
12221da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
12231da177e4SLinus Torvalds 	int retval;
122470fbcdf4SRam Pai 	LIST_HEAD(umount_list);
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	retval = security_sb_umount(mnt, flags);
12271da177e4SLinus Torvalds 	if (retval)
12281da177e4SLinus Torvalds 		return retval;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	/*
12311da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
12321da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
12331da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
12341da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
12351da177e4SLinus Torvalds 	 */
12361da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
12376ac08c39SJan Blunck 		if (mnt == current->fs->root.mnt ||
12381da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
12391da177e4SLinus Torvalds 			return -EINVAL;
12401da177e4SLinus Torvalds 
1241b3e19d92SNick Piggin 		/*
1242b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1243b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1244b3e19d92SNick Piggin 		 */
1245b3e19d92SNick Piggin 		br_write_lock(vfsmount_lock);
1246b3e19d92SNick Piggin 		if (mnt_get_count(mnt) != 2) {
1247b3e19d92SNick Piggin 			br_write_lock(vfsmount_lock);
12481da177e4SLinus Torvalds 			return -EBUSY;
1249b3e19d92SNick Piggin 		}
1250b3e19d92SNick Piggin 		br_write_unlock(vfsmount_lock);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1))
12531da177e4SLinus Torvalds 			return -EAGAIN;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	/*
12571da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
12581da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
12591da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
12601da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
12611da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
12621da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
12631da177e4SLinus Torvalds 	 * about for the moment.
12641da177e4SLinus Torvalds 	 */
12651da177e4SLinus Torvalds 
126642faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
126742faad99SAl Viro 		sb->s_op->umount_begin(sb);
126842faad99SAl Viro 	}
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	/*
12711da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
12721da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
12731da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
12741da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
12751da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
12761da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
12771da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
12781da177e4SLinus Torvalds 	 */
12796ac08c39SJan Blunck 	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
12801da177e4SLinus Torvalds 		/*
12811da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
12821da177e4SLinus Torvalds 		 * we just try to remount it readonly.
12831da177e4SLinus Torvalds 		 */
12841da177e4SLinus Torvalds 		down_write(&sb->s_umount);
12854aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
12861da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
12871da177e4SLinus Torvalds 		up_write(&sb->s_umount);
12881da177e4SLinus Torvalds 		return retval;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
1291390c6843SRam Pai 	down_write(&namespace_sem);
129299b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
12935addc5ddSAl Viro 	event++;
12941da177e4SLinus Torvalds 
1295c35038beSAl Viro 	if (!(flags & MNT_DETACH))
1296c35038beSAl Viro 		shrink_submounts(mnt, &umount_list);
1297c35038beSAl Viro 
12981da177e4SLinus Torvalds 	retval = -EBUSY;
1299a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
13001da177e4SLinus Torvalds 		if (!list_empty(&mnt->mnt_list))
1301a05964f3SRam Pai 			umount_tree(mnt, 1, &umount_list);
13021da177e4SLinus Torvalds 		retval = 0;
13031da177e4SLinus Torvalds 	}
130499b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
1305390c6843SRam Pai 	up_write(&namespace_sem);
130670fbcdf4SRam Pai 	release_mounts(&umount_list);
13071da177e4SLinus Torvalds 	return retval;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds /*
13111da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
13121da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
13131da177e4SLinus Torvalds  *
13141da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
13151da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
13161da177e4SLinus Torvalds  */
13171da177e4SLinus Torvalds 
1318bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
13191da177e4SLinus Torvalds {
13202d8f3038SAl Viro 	struct path path;
13211da177e4SLinus Torvalds 	int retval;
1322db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
13231da177e4SLinus Torvalds 
1324db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1325db1f05bbSMiklos Szeredi 		return -EINVAL;
1326db1f05bbSMiklos Szeredi 
1327db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1328db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1329db1f05bbSMiklos Szeredi 
1330db1f05bbSMiklos Szeredi 	retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
13311da177e4SLinus Torvalds 	if (retval)
13321da177e4SLinus Torvalds 		goto out;
13331da177e4SLinus Torvalds 	retval = -EINVAL;
13342d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
13351da177e4SLinus Torvalds 		goto dput_and_out;
13362d8f3038SAl Viro 	if (!check_mnt(path.mnt))
13371da177e4SLinus Torvalds 		goto dput_and_out;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	retval = -EPERM;
13401da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
13411da177e4SLinus Torvalds 		goto dput_and_out;
13421da177e4SLinus Torvalds 
13432d8f3038SAl Viro 	retval = do_umount(path.mnt, flags);
13441da177e4SLinus Torvalds dput_and_out:
1345429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
13462d8f3038SAl Viro 	dput(path.dentry);
13472d8f3038SAl Viro 	mntput_no_expire(path.mnt);
13481da177e4SLinus Torvalds out:
13491da177e4SLinus Torvalds 	return retval;
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds /*
13551da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
13561da177e4SLinus Torvalds  */
1357bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
13581da177e4SLinus Torvalds {
13591da177e4SLinus Torvalds 	return sys_umount(name, 0);
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds #endif
13631da177e4SLinus Torvalds 
13642d92ab3cSAl Viro static int mount_is_safe(struct path *path)
13651da177e4SLinus Torvalds {
13661da177e4SLinus Torvalds 	if (capable(CAP_SYS_ADMIN))
13671da177e4SLinus Torvalds 		return 0;
13681da177e4SLinus Torvalds 	return -EPERM;
13691da177e4SLinus Torvalds #ifdef notyet
13702d92ab3cSAl Viro 	if (S_ISLNK(path->dentry->d_inode->i_mode))
13711da177e4SLinus Torvalds 		return -EPERM;
13722d92ab3cSAl Viro 	if (path->dentry->d_inode->i_mode & S_ISVTX) {
1373da9592edSDavid Howells 		if (current_uid() != path->dentry->d_inode->i_uid)
13741da177e4SLinus Torvalds 			return -EPERM;
13751da177e4SLinus Torvalds 	}
13762d92ab3cSAl Viro 	if (inode_permission(path->dentry->d_inode, MAY_WRITE))
13771da177e4SLinus Torvalds 		return -EPERM;
13781da177e4SLinus Torvalds 	return 0;
13791da177e4SLinus Torvalds #endif
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
1382b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
138336341f64SRam Pai 					int flag)
13841da177e4SLinus Torvalds {
13851da177e4SLinus Torvalds 	struct vfsmount *res, *p, *q, *r, *s;
13861a390689SAl Viro 	struct path path;
13871da177e4SLinus Torvalds 
13889676f0c6SRam Pai 	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
13899676f0c6SRam Pai 		return NULL;
13909676f0c6SRam Pai 
139136341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
13921da177e4SLinus Torvalds 	if (!q)
13931da177e4SLinus Torvalds 		goto Enomem;
13941da177e4SLinus Torvalds 	q->mnt_mountpoint = mnt->mnt_mountpoint;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	p = mnt;
1397fdadd65fSDomen Puncer 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
13987ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
13991da177e4SLinus Torvalds 			continue;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 		for (s = r; s; s = next_mnt(s, r)) {
14029676f0c6SRam Pai 			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
14039676f0c6SRam Pai 				s = skip_mnt_tree(s);
14049676f0c6SRam Pai 				continue;
14059676f0c6SRam Pai 			}
14061da177e4SLinus Torvalds 			while (p != s->mnt_parent) {
14071da177e4SLinus Torvalds 				p = p->mnt_parent;
14081da177e4SLinus Torvalds 				q = q->mnt_parent;
14091da177e4SLinus Torvalds 			}
14101da177e4SLinus Torvalds 			p = s;
14111a390689SAl Viro 			path.mnt = q;
14121a390689SAl Viro 			path.dentry = p->mnt_mountpoint;
141336341f64SRam Pai 			q = clone_mnt(p, p->mnt_root, flag);
14141da177e4SLinus Torvalds 			if (!q)
14151da177e4SLinus Torvalds 				goto Enomem;
141699b7db7bSNick Piggin 			br_write_lock(vfsmount_lock);
14171da177e4SLinus Torvalds 			list_add_tail(&q->mnt_list, &res->mnt_list);
14181a390689SAl Viro 			attach_mnt(q, &path);
141999b7db7bSNick Piggin 			br_write_unlock(vfsmount_lock);
14201da177e4SLinus Torvalds 		}
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 	return res;
14231da177e4SLinus Torvalds Enomem:
14241da177e4SLinus Torvalds 	if (res) {
142570fbcdf4SRam Pai 		LIST_HEAD(umount_list);
142699b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1427a05964f3SRam Pai 		umount_tree(res, 0, &umount_list);
142899b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
142970fbcdf4SRam Pai 		release_mounts(&umount_list);
14301da177e4SLinus Torvalds 	}
14311da177e4SLinus Torvalds 	return NULL;
14321da177e4SLinus Torvalds }
14331da177e4SLinus Torvalds 
1434589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
14358aec0809SAl Viro {
14368aec0809SAl Viro 	struct vfsmount *tree;
14371a60a280SAl Viro 	down_write(&namespace_sem);
1438589ff870SAl Viro 	tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
14391a60a280SAl Viro 	up_write(&namespace_sem);
14408aec0809SAl Viro 	return tree;
14418aec0809SAl Viro }
14428aec0809SAl Viro 
14438aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
14448aec0809SAl Viro {
14458aec0809SAl Viro 	LIST_HEAD(umount_list);
14461a60a280SAl Viro 	down_write(&namespace_sem);
144799b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
14488aec0809SAl Viro 	umount_tree(mnt, 0, &umount_list);
144999b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
14501a60a280SAl Viro 	up_write(&namespace_sem);
14518aec0809SAl Viro 	release_mounts(&umount_list);
14528aec0809SAl Viro }
14538aec0809SAl Viro 
14541f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
14551f707137SAl Viro 		   struct vfsmount *root)
14561f707137SAl Viro {
14571f707137SAl Viro 	struct vfsmount *mnt;
14581f707137SAl Viro 	int res = f(root, arg);
14591f707137SAl Viro 	if (res)
14601f707137SAl Viro 		return res;
14611f707137SAl Viro 	list_for_each_entry(mnt, &root->mnt_list, mnt_list) {
14621f707137SAl Viro 		res = f(mnt, arg);
14631f707137SAl Viro 		if (res)
14641f707137SAl Viro 			return res;
14651f707137SAl Viro 	}
14661f707137SAl Viro 	return 0;
14671f707137SAl Viro }
14681f707137SAl Viro 
1469719f5d7fSMiklos Szeredi static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
1470719f5d7fSMiklos Szeredi {
1471719f5d7fSMiklos Szeredi 	struct vfsmount *p;
1472719f5d7fSMiklos Szeredi 
1473719f5d7fSMiklos Szeredi 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1474719f5d7fSMiklos Szeredi 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
1475719f5d7fSMiklos Szeredi 			mnt_release_group_id(p);
1476719f5d7fSMiklos Szeredi 	}
1477719f5d7fSMiklos Szeredi }
1478719f5d7fSMiklos Szeredi 
1479719f5d7fSMiklos Szeredi static int invent_group_ids(struct vfsmount *mnt, bool recurse)
1480719f5d7fSMiklos Szeredi {
1481719f5d7fSMiklos Szeredi 	struct vfsmount *p;
1482719f5d7fSMiklos Szeredi 
1483719f5d7fSMiklos Szeredi 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1484719f5d7fSMiklos Szeredi 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1485719f5d7fSMiklos Szeredi 			int err = mnt_alloc_group_id(p);
1486719f5d7fSMiklos Szeredi 			if (err) {
1487719f5d7fSMiklos Szeredi 				cleanup_group_ids(mnt, p);
1488719f5d7fSMiklos Szeredi 				return err;
1489719f5d7fSMiklos Szeredi 			}
1490719f5d7fSMiklos Szeredi 		}
1491719f5d7fSMiklos Szeredi 	}
1492719f5d7fSMiklos Szeredi 
1493719f5d7fSMiklos Szeredi 	return 0;
1494719f5d7fSMiklos Szeredi }
1495719f5d7fSMiklos Szeredi 
1496b90fa9aeSRam Pai /*
1497b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1498b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
149921444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
150021444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
150121444403SRam Pai  *  		   (done when source_mnt is moved)
1502b90fa9aeSRam Pai  *
1503b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1504b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
15059676f0c6SRam Pai  * ---------------------------------------------------------------------------
1506b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
15079676f0c6SRam Pai  * |**************************************************************************
15089676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
15099676f0c6SRam Pai  * | dest     |               |                |                |            |
15109676f0c6SRam Pai  * |   |      |               |                |                |            |
15119676f0c6SRam Pai  * |   v      |               |                |                |            |
15129676f0c6SRam Pai  * |**************************************************************************
15139676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
15145afe0022SRam Pai  * |          |               |                |                |            |
15159676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
15169676f0c6SRam Pai  * ***************************************************************************
1517b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1518b90fa9aeSRam Pai  * destination mount.
1519b90fa9aeSRam Pai  *
1520b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1521b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1522b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1523b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1524b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1525b90fa9aeSRam Pai  *       mount.
15265afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
15275afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
15285afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
15295afe0022SRam Pai  *       is marked as 'shared and slave'.
15305afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
15315afe0022SRam Pai  * 	 source mount.
15325afe0022SRam Pai  *
15339676f0c6SRam Pai  * ---------------------------------------------------------------------------
153421444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
15359676f0c6SRam Pai  * |**************************************************************************
15369676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
15379676f0c6SRam Pai  * | dest     |               |                |                |            |
15389676f0c6SRam Pai  * |   |      |               |                |                |            |
15399676f0c6SRam Pai  * |   v      |               |                |                |            |
15409676f0c6SRam Pai  * |**************************************************************************
15419676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
15425afe0022SRam Pai  * |          |               |                |                |            |
15439676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
15449676f0c6SRam Pai  * ***************************************************************************
15455afe0022SRam Pai  *
15465afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
15475afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
154821444403SRam Pai  * (+*)  the mount is moved to the destination.
15495afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
15505afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
15515afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
15525afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1553b90fa9aeSRam Pai  *
1554b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1555b90fa9aeSRam Pai  * applied to each mount in the tree.
1556b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1557b90fa9aeSRam Pai  * in allocations.
1558b90fa9aeSRam Pai  */
1559b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt,
15601a390689SAl Viro 			struct path *path, struct path *parent_path)
1561b90fa9aeSRam Pai {
1562b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
15631a390689SAl Viro 	struct vfsmount *dest_mnt = path->mnt;
15641a390689SAl Viro 	struct dentry *dest_dentry = path->dentry;
1565b90fa9aeSRam Pai 	struct vfsmount *child, *p;
1566719f5d7fSMiklos Szeredi 	int err;
1567b90fa9aeSRam Pai 
1568719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt)) {
1569719f5d7fSMiklos Szeredi 		err = invent_group_ids(source_mnt, true);
1570719f5d7fSMiklos Szeredi 		if (err)
1571719f5d7fSMiklos Szeredi 			goto out;
1572719f5d7fSMiklos Szeredi 	}
1573719f5d7fSMiklos Szeredi 	err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1574719f5d7fSMiklos Szeredi 	if (err)
1575719f5d7fSMiklos Szeredi 		goto out_cleanup_ids;
1576b90fa9aeSRam Pai 
157799b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
1578df1a1ad2SAl Viro 
1579b90fa9aeSRam Pai 	if (IS_MNT_SHARED(dest_mnt)) {
1580b90fa9aeSRam Pai 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1581b90fa9aeSRam Pai 			set_mnt_shared(p);
1582b90fa9aeSRam Pai 	}
15831a390689SAl Viro 	if (parent_path) {
15841a390689SAl Viro 		detach_mnt(source_mnt, parent_path);
15851a390689SAl Viro 		attach_mnt(source_mnt, path);
1586e5d67f07SAl Viro 		touch_mnt_namespace(parent_path->mnt->mnt_ns);
158721444403SRam Pai 	} else {
1588b90fa9aeSRam Pai 		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
1589b90fa9aeSRam Pai 		commit_tree(source_mnt);
159021444403SRam Pai 	}
1591b90fa9aeSRam Pai 
1592b90fa9aeSRam Pai 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1593b90fa9aeSRam Pai 		list_del_init(&child->mnt_hash);
1594b90fa9aeSRam Pai 		commit_tree(child);
1595b90fa9aeSRam Pai 	}
159699b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
159799b7db7bSNick Piggin 
1598b90fa9aeSRam Pai 	return 0;
1599719f5d7fSMiklos Szeredi 
1600719f5d7fSMiklos Szeredi  out_cleanup_ids:
1601719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt))
1602719f5d7fSMiklos Szeredi 		cleanup_group_ids(source_mnt, NULL);
1603719f5d7fSMiklos Szeredi  out:
1604719f5d7fSMiklos Szeredi 	return err;
1605b90fa9aeSRam Pai }
1606b90fa9aeSRam Pai 
16078c3ee42eSAl Viro static int graft_tree(struct vfsmount *mnt, struct path *path)
16081da177e4SLinus Torvalds {
16091da177e4SLinus Torvalds 	int err;
16101da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_flags & MS_NOUSER)
16111da177e4SLinus Torvalds 		return -EINVAL;
16121da177e4SLinus Torvalds 
16138c3ee42eSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
16141da177e4SLinus Torvalds 	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
16151da177e4SLinus Torvalds 		return -ENOTDIR;
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	err = -ENOENT;
16188c3ee42eSAl Viro 	mutex_lock(&path->dentry->d_inode->i_mutex);
1619d83c49f3SAl Viro 	if (cant_mount(path->dentry))
16201da177e4SLinus Torvalds 		goto out_unlock;
16211da177e4SLinus Torvalds 
1622f3da392eSAlexey Dobriyan 	if (!d_unlinked(path->dentry))
16238c3ee42eSAl Viro 		err = attach_recursive_mnt(mnt, path, NULL);
16241da177e4SLinus Torvalds out_unlock:
16258c3ee42eSAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
16261da177e4SLinus Torvalds 	return err;
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds /*
16307a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
16317a2e8a8fSValerie Aurora  */
16327a2e8a8fSValerie Aurora 
16337a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags)
16347a2e8a8fSValerie Aurora {
16357a2e8a8fSValerie Aurora 	int type = flags & ~MS_REC;
16367a2e8a8fSValerie Aurora 
16377a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
16387a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
16397a2e8a8fSValerie Aurora 		return 0;
16407a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
16417a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
16427a2e8a8fSValerie Aurora 		return 0;
16437a2e8a8fSValerie Aurora 	return type;
16447a2e8a8fSValerie Aurora }
16457a2e8a8fSValerie Aurora 
16467a2e8a8fSValerie Aurora /*
164707b20889SRam Pai  * recursively change the type of the mountpoint.
164807b20889SRam Pai  */
16490a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
165007b20889SRam Pai {
16512d92ab3cSAl Viro 	struct vfsmount *m, *mnt = path->mnt;
165207b20889SRam Pai 	int recurse = flag & MS_REC;
16537a2e8a8fSValerie Aurora 	int type;
1654719f5d7fSMiklos Szeredi 	int err = 0;
165507b20889SRam Pai 
1656ee6f9582SMiklos Szeredi 	if (!capable(CAP_SYS_ADMIN))
1657ee6f9582SMiklos Szeredi 		return -EPERM;
1658ee6f9582SMiklos Szeredi 
16592d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
166007b20889SRam Pai 		return -EINVAL;
166107b20889SRam Pai 
16627a2e8a8fSValerie Aurora 	type = flags_to_propagation_type(flag);
16637a2e8a8fSValerie Aurora 	if (!type)
16647a2e8a8fSValerie Aurora 		return -EINVAL;
16657a2e8a8fSValerie Aurora 
166607b20889SRam Pai 	down_write(&namespace_sem);
1667719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1668719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1669719f5d7fSMiklos Szeredi 		if (err)
1670719f5d7fSMiklos Szeredi 			goto out_unlock;
1671719f5d7fSMiklos Szeredi 	}
1672719f5d7fSMiklos Szeredi 
167399b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
167407b20889SRam Pai 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
167507b20889SRam Pai 		change_mnt_propagation(m, type);
167699b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
1677719f5d7fSMiklos Szeredi 
1678719f5d7fSMiklos Szeredi  out_unlock:
167907b20889SRam Pai 	up_write(&namespace_sem);
1680719f5d7fSMiklos Szeredi 	return err;
168107b20889SRam Pai }
168207b20889SRam Pai 
168307b20889SRam Pai /*
16841da177e4SLinus Torvalds  * do loopback mount.
16851da177e4SLinus Torvalds  */
16860a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name,
16872dafe1c4SEric Sandeen 				int recurse)
16881da177e4SLinus Torvalds {
16892d92ab3cSAl Viro 	struct path old_path;
16901da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
16912d92ab3cSAl Viro 	int err = mount_is_safe(path);
16921da177e4SLinus Torvalds 	if (err)
16931da177e4SLinus Torvalds 		return err;
16941da177e4SLinus Torvalds 	if (!old_name || !*old_name)
16951da177e4SLinus Torvalds 		return -EINVAL;
16962d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
16971da177e4SLinus Torvalds 	if (err)
16981da177e4SLinus Torvalds 		return err;
16991da177e4SLinus Torvalds 
1700390c6843SRam Pai 	down_write(&namespace_sem);
17011da177e4SLinus Torvalds 	err = -EINVAL;
17022d92ab3cSAl Viro 	if (IS_MNT_UNBINDABLE(old_path.mnt))
17039676f0c6SRam Pai 		goto out;
17049676f0c6SRam Pai 
17052d92ab3cSAl Viro 	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
1706ccd48bc7SAl Viro 		goto out;
1707ccd48bc7SAl Viro 
17081da177e4SLinus Torvalds 	err = -ENOMEM;
17091da177e4SLinus Torvalds 	if (recurse)
17102d92ab3cSAl Viro 		mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
17111da177e4SLinus Torvalds 	else
17122d92ab3cSAl Viro 		mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
17131da177e4SLinus Torvalds 
1714ccd48bc7SAl Viro 	if (!mnt)
1715ccd48bc7SAl Viro 		goto out;
1716ccd48bc7SAl Viro 
17172d92ab3cSAl Viro 	err = graft_tree(mnt, path);
17181da177e4SLinus Torvalds 	if (err) {
171970fbcdf4SRam Pai 		LIST_HEAD(umount_list);
172099b7db7bSNick Piggin 
172199b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1722a05964f3SRam Pai 		umount_tree(mnt, 0, &umount_list);
172399b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
172470fbcdf4SRam Pai 		release_mounts(&umount_list);
17255b83d2c5SRam Pai 	}
17261da177e4SLinus Torvalds 
1727ccd48bc7SAl Viro out:
1728390c6843SRam Pai 	up_write(&namespace_sem);
17292d92ab3cSAl Viro 	path_put(&old_path);
17301da177e4SLinus Torvalds 	return err;
17311da177e4SLinus Torvalds }
17321da177e4SLinus Torvalds 
17332e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
17342e4b7fcdSDave Hansen {
17352e4b7fcdSDave Hansen 	int error = 0;
17362e4b7fcdSDave Hansen 	int readonly_request = 0;
17372e4b7fcdSDave Hansen 
17382e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
17392e4b7fcdSDave Hansen 		readonly_request = 1;
17402e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
17412e4b7fcdSDave Hansen 		return 0;
17422e4b7fcdSDave Hansen 
17432e4b7fcdSDave Hansen 	if (readonly_request)
17442e4b7fcdSDave Hansen 		error = mnt_make_readonly(mnt);
17452e4b7fcdSDave Hansen 	else
17462e4b7fcdSDave Hansen 		__mnt_unmake_readonly(mnt);
17472e4b7fcdSDave Hansen 	return error;
17482e4b7fcdSDave Hansen }
17492e4b7fcdSDave Hansen 
17501da177e4SLinus Torvalds /*
17511da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
17521da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
17531da177e4SLinus Torvalds  * on it - tough luck.
17541da177e4SLinus Torvalds  */
17550a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
17561da177e4SLinus Torvalds 		      void *data)
17571da177e4SLinus Torvalds {
17581da177e4SLinus Torvalds 	int err;
17592d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
17621da177e4SLinus Torvalds 		return -EPERM;
17631da177e4SLinus Torvalds 
17642d92ab3cSAl Viro 	if (!check_mnt(path->mnt))
17651da177e4SLinus Torvalds 		return -EINVAL;
17661da177e4SLinus Torvalds 
17672d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
17681da177e4SLinus Torvalds 		return -EINVAL;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	down_write(&sb->s_umount);
17712e4b7fcdSDave Hansen 	if (flags & MS_BIND)
17722d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
17734aa98cf7SAl Viro 	else
17741da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
17757b43a79fSAl Viro 	if (!err) {
177699b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1777495d6c9cSValerie Aurora 		mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK;
17782d92ab3cSAl Viro 		path->mnt->mnt_flags = mnt_flags;
177999b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
17807b43a79fSAl Viro 	}
17811da177e4SLinus Torvalds 	up_write(&sb->s_umount);
17820e55a7ccSDan Williams 	if (!err) {
178399b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
17840e55a7ccSDan Williams 		touch_mnt_namespace(path->mnt->mnt_ns);
178599b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
17860e55a7ccSDan Williams 	}
17871da177e4SLinus Torvalds 	return err;
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
17909676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt)
17919676f0c6SRam Pai {
17929676f0c6SRam Pai 	struct vfsmount *p;
17939676f0c6SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
17949676f0c6SRam Pai 		if (IS_MNT_UNBINDABLE(p))
17959676f0c6SRam Pai 			return 1;
17969676f0c6SRam Pai 	}
17979676f0c6SRam Pai 	return 0;
17989676f0c6SRam Pai }
17999676f0c6SRam Pai 
18000a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name)
18011da177e4SLinus Torvalds {
18022d92ab3cSAl Viro 	struct path old_path, parent_path;
18031da177e4SLinus Torvalds 	struct vfsmount *p;
18041da177e4SLinus Torvalds 	int err = 0;
18051da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
18061da177e4SLinus Torvalds 		return -EPERM;
18071da177e4SLinus Torvalds 	if (!old_name || !*old_name)
18081da177e4SLinus Torvalds 		return -EINVAL;
18092d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
18101da177e4SLinus Torvalds 	if (err)
18111da177e4SLinus Torvalds 		return err;
18121da177e4SLinus Torvalds 
1813390c6843SRam Pai 	down_write(&namespace_sem);
1814cc53ce53SDavid Howells 	err = follow_down(path, true);
1815cc53ce53SDavid Howells 	if (err < 0)
1816cc53ce53SDavid Howells 		goto out;
1817cc53ce53SDavid Howells 
18181da177e4SLinus Torvalds 	err = -EINVAL;
18192d92ab3cSAl Viro 	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
18201da177e4SLinus Torvalds 		goto out;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	err = -ENOENT;
18232d92ab3cSAl Viro 	mutex_lock(&path->dentry->d_inode->i_mutex);
1824d83c49f3SAl Viro 	if (cant_mount(path->dentry))
18251da177e4SLinus Torvalds 		goto out1;
18261da177e4SLinus Torvalds 
1827f3da392eSAlexey Dobriyan 	if (d_unlinked(path->dentry))
182821444403SRam Pai 		goto out1;
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	err = -EINVAL;
18312d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
183221444403SRam Pai 		goto out1;
18331da177e4SLinus Torvalds 
18342d92ab3cSAl Viro 	if (old_path.mnt == old_path.mnt->mnt_parent)
183521444403SRam Pai 		goto out1;
18361da177e4SLinus Torvalds 
18372d92ab3cSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
18382d92ab3cSAl Viro 	      S_ISDIR(old_path.dentry->d_inode->i_mode))
183921444403SRam Pai 		goto out1;
184021444403SRam Pai 	/*
184121444403SRam Pai 	 * Don't move a mount residing in a shared parent.
184221444403SRam Pai 	 */
18432d92ab3cSAl Viro 	if (old_path.mnt->mnt_parent &&
18442d92ab3cSAl Viro 	    IS_MNT_SHARED(old_path.mnt->mnt_parent))
184521444403SRam Pai 		goto out1;
18469676f0c6SRam Pai 	/*
18479676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
18489676f0c6SRam Pai 	 * mount which is shared.
18499676f0c6SRam Pai 	 */
18502d92ab3cSAl Viro 	if (IS_MNT_SHARED(path->mnt) &&
18512d92ab3cSAl Viro 	    tree_contains_unbindable(old_path.mnt))
18529676f0c6SRam Pai 		goto out1;
18531da177e4SLinus Torvalds 	err = -ELOOP;
18542d92ab3cSAl Viro 	for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent)
18552d92ab3cSAl Viro 		if (p == old_path.mnt)
185621444403SRam Pai 			goto out1;
18571da177e4SLinus Torvalds 
18582d92ab3cSAl Viro 	err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
18594ac91378SJan Blunck 	if (err)
186021444403SRam Pai 		goto out1;
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
18631da177e4SLinus Torvalds 	 * automatically */
18642d92ab3cSAl Viro 	list_del_init(&old_path.mnt->mnt_expire);
18651da177e4SLinus Torvalds out1:
18662d92ab3cSAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
18671da177e4SLinus Torvalds out:
1868390c6843SRam Pai 	up_write(&namespace_sem);
18691da177e4SLinus Torvalds 	if (!err)
18701a390689SAl Viro 		path_put(&parent_path);
18712d92ab3cSAl Viro 	path_put(&old_path);
18721da177e4SLinus Torvalds 	return err;
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
1875b1e75df4SAl Viro static int do_add_mount(struct vfsmount *, struct path *, int);
1876b1e75df4SAl Viro 
18771da177e4SLinus Torvalds /*
18781da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
18791da177e4SLinus Torvalds  * namespace's tree
18801da177e4SLinus Torvalds  */
18810a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags,
18821da177e4SLinus Torvalds 			int mnt_flags, char *name, void *data)
18831da177e4SLinus Torvalds {
18841da177e4SLinus Torvalds 	struct vfsmount *mnt;
188515f9a3f3SAl Viro 	int err;
18861da177e4SLinus Torvalds 
1887eca6f534SVegard Nossum 	if (!type)
18881da177e4SLinus Torvalds 		return -EINVAL;
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 	/* we need capabilities... */
18911da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
18921da177e4SLinus Torvalds 		return -EPERM;
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	mnt = do_kern_mount(type, flags, name, data);
18951da177e4SLinus Torvalds 	if (IS_ERR(mnt))
18961da177e4SLinus Torvalds 		return PTR_ERR(mnt);
18971da177e4SLinus Torvalds 
189815f9a3f3SAl Viro 	err = do_add_mount(mnt, path, mnt_flags);
189915f9a3f3SAl Viro 	if (err)
190015f9a3f3SAl Viro 		mntput(mnt);
190115f9a3f3SAl Viro 	return err;
19021da177e4SLinus Torvalds }
19031da177e4SLinus Torvalds 
190419a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
190519a167afSAl Viro {
190619a167afSAl Viro 	int err;
190719a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
190819a167afSAl Viro 	 * expired before we get a chance to add it
190919a167afSAl Viro 	 */
191019a167afSAl Viro 	BUG_ON(mnt_get_count(m) < 2);
191119a167afSAl Viro 
191219a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
191319a167afSAl Viro 	    m->mnt_root == path->dentry) {
1914b1e75df4SAl Viro 		err = -ELOOP;
1915b1e75df4SAl Viro 		goto fail;
191619a167afSAl Viro 	}
191719a167afSAl Viro 
191819a167afSAl Viro 	err = do_add_mount(m, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
1919b1e75df4SAl Viro 	if (!err)
1920b1e75df4SAl Viro 		return 0;
1921b1e75df4SAl Viro fail:
1922b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
1923b1e75df4SAl Viro 	if (!list_empty(&m->mnt_expire)) {
1924b1e75df4SAl Viro 		down_write(&namespace_sem);
1925b1e75df4SAl Viro 		br_write_lock(vfsmount_lock);
1926b1e75df4SAl Viro 		list_del_init(&m->mnt_expire);
1927b1e75df4SAl Viro 		br_write_unlock(vfsmount_lock);
1928b1e75df4SAl Viro 		up_write(&namespace_sem);
192919a167afSAl Viro 	}
1930b1e75df4SAl Viro 	mntput(m);
1931b1e75df4SAl Viro 	mntput(m);
193219a167afSAl Viro 	return err;
193319a167afSAl Viro }
193419a167afSAl Viro 
19351da177e4SLinus Torvalds /*
19361da177e4SLinus Torvalds  * add a mount into a namespace's mount tree
19371da177e4SLinus Torvalds  */
1938b1e75df4SAl Viro static int do_add_mount(struct vfsmount *newmnt, struct path *path, int mnt_flags)
19391da177e4SLinus Torvalds {
19401da177e4SLinus Torvalds 	int err;
19411da177e4SLinus Torvalds 
19428089352aSAl Viro 	mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
194327d55f1fSAl Viro 
1944390c6843SRam Pai 	down_write(&namespace_sem);
19451da177e4SLinus Torvalds 	/* Something was mounted here while we slept */
1946cc53ce53SDavid Howells 	err = follow_down(path, true);
1947cc53ce53SDavid Howells 	if (err < 0)
1948cc53ce53SDavid Howells 		goto unlock;
1949cc53ce53SDavid Howells 
19501da177e4SLinus Torvalds 	err = -EINVAL;
1951dd5cae6eSAl Viro 	if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
19521da177e4SLinus Torvalds 		goto unlock;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	/* Refuse the same filesystem on the same mount point */
19551da177e4SLinus Torvalds 	err = -EBUSY;
19568d66bf54SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt_sb &&
19578d66bf54SAl Viro 	    path->mnt->mnt_root == path->dentry)
19581da177e4SLinus Torvalds 		goto unlock;
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	err = -EINVAL;
19611da177e4SLinus Torvalds 	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
19621da177e4SLinus Torvalds 		goto unlock;
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	newmnt->mnt_flags = mnt_flags;
1965b1e75df4SAl Viro 	err = graft_tree(newmnt, path);
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds unlock:
1968390c6843SRam Pai 	up_write(&namespace_sem);
19691da177e4SLinus Torvalds 	return err;
19701da177e4SLinus Torvalds }
19711da177e4SLinus Torvalds 
1972ea5b778aSDavid Howells /**
1973ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
1974ea5b778aSDavid Howells  * @mnt: The mount to list.
1975ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
1976ea5b778aSDavid Howells  */
1977ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
1978ea5b778aSDavid Howells {
1979ea5b778aSDavid Howells 	down_write(&namespace_sem);
1980ea5b778aSDavid Howells 	br_write_lock(vfsmount_lock);
1981ea5b778aSDavid Howells 
1982ea5b778aSDavid Howells 	list_add_tail(&mnt->mnt_expire, expiry_list);
1983ea5b778aSDavid Howells 
1984ea5b778aSDavid Howells 	br_write_unlock(vfsmount_lock);
1985ea5b778aSDavid Howells 	up_write(&namespace_sem);
1986ea5b778aSDavid Howells }
1987ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
1988ea5b778aSDavid Howells 
1989ea5b778aSDavid Howells /*
19901da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
19911da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
19921da177e4SLinus Torvalds  * here
19931da177e4SLinus Torvalds  */
19941da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
19951da177e4SLinus Torvalds {
19961da177e4SLinus Torvalds 	struct vfsmount *mnt, *next;
19971da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
1998bcc5c7d2SAl Viro 	LIST_HEAD(umounts);
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	if (list_empty(mounts))
20011da177e4SLinus Torvalds 		return;
20021da177e4SLinus Torvalds 
2003bcc5c7d2SAl Viro 	down_write(&namespace_sem);
200499b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
20071da177e4SLinus Torvalds 	 * following criteria:
20081da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
20091da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
20101da177e4SLinus Torvalds 	 *   cleared by mntput())
20111da177e4SLinus Torvalds 	 */
201255e700b9SMiklos Szeredi 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
20131da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
2014bcc5c7d2SAl Viro 			propagate_mount_busy(mnt, 1))
20151da177e4SLinus Torvalds 			continue;
201655e700b9SMiklos Szeredi 		list_move(&mnt->mnt_expire, &graveyard);
20171da177e4SLinus Torvalds 	}
2018bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
2019bcc5c7d2SAl Viro 		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
2020bcc5c7d2SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2021bcc5c7d2SAl Viro 		umount_tree(mnt, 1, &umounts);
2022bcc5c7d2SAl Viro 	}
202399b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
2024bcc5c7d2SAl Viro 	up_write(&namespace_sem);
2025bcc5c7d2SAl Viro 
2026bcc5c7d2SAl Viro 	release_mounts(&umounts);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds /*
20325528f911STrond Myklebust  * Ripoff of 'select_parent()'
20335528f911STrond Myklebust  *
20345528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
20355528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
20365528f911STrond Myklebust  */
20375528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
20385528f911STrond Myklebust {
20395528f911STrond Myklebust 	struct vfsmount *this_parent = parent;
20405528f911STrond Myklebust 	struct list_head *next;
20415528f911STrond Myklebust 	int found = 0;
20425528f911STrond Myklebust 
20435528f911STrond Myklebust repeat:
20445528f911STrond Myklebust 	next = this_parent->mnt_mounts.next;
20455528f911STrond Myklebust resume:
20465528f911STrond Myklebust 	while (next != &this_parent->mnt_mounts) {
20475528f911STrond Myklebust 		struct list_head *tmp = next;
20485528f911STrond Myklebust 		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
20495528f911STrond Myklebust 
20505528f911STrond Myklebust 		next = tmp->next;
20515528f911STrond Myklebust 		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
20525528f911STrond Myklebust 			continue;
20535528f911STrond Myklebust 		/*
20545528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
20555528f911STrond Myklebust 		 */
20565528f911STrond Myklebust 		if (!list_empty(&mnt->mnt_mounts)) {
20575528f911STrond Myklebust 			this_parent = mnt;
20585528f911STrond Myklebust 			goto repeat;
20595528f911STrond Myklebust 		}
20605528f911STrond Myklebust 
20615528f911STrond Myklebust 		if (!propagate_mount_busy(mnt, 1)) {
20625528f911STrond Myklebust 			list_move_tail(&mnt->mnt_expire, graveyard);
20635528f911STrond Myklebust 			found++;
20645528f911STrond Myklebust 		}
20655528f911STrond Myklebust 	}
20665528f911STrond Myklebust 	/*
20675528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
20685528f911STrond Myklebust 	 */
20695528f911STrond Myklebust 	if (this_parent != parent) {
20705528f911STrond Myklebust 		next = this_parent->mnt_child.next;
20715528f911STrond Myklebust 		this_parent = this_parent->mnt_parent;
20725528f911STrond Myklebust 		goto resume;
20735528f911STrond Myklebust 	}
20745528f911STrond Myklebust 	return found;
20755528f911STrond Myklebust }
20765528f911STrond Myklebust 
20775528f911STrond Myklebust /*
20785528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
20795528f911STrond Myklebust  * submounts of a specific parent mountpoint
208099b7db7bSNick Piggin  *
208199b7db7bSNick Piggin  * vfsmount_lock must be held for write
20825528f911STrond Myklebust  */
2083c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
20845528f911STrond Myklebust {
20855528f911STrond Myklebust 	LIST_HEAD(graveyard);
2086c35038beSAl Viro 	struct vfsmount *m;
20875528f911STrond Myklebust 
20885528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2089c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2090bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2091c35038beSAl Viro 			m = list_first_entry(&graveyard, struct vfsmount,
2092bcc5c7d2SAl Viro 						mnt_expire);
2093afef80b3SEric W. Biederman 			touch_mnt_namespace(m->mnt_ns);
2094afef80b3SEric W. Biederman 			umount_tree(m, 1, umounts);
2095bcc5c7d2SAl Viro 		}
2096bcc5c7d2SAl Viro 	}
20975528f911STrond Myklebust }
20985528f911STrond Myklebust 
20995528f911STrond Myklebust /*
21001da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
21011da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
21021da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
21031da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
21041da177e4SLinus Torvalds  */
2105b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2106b58fed8bSRam Pai 				 unsigned long n)
21071da177e4SLinus Torvalds {
21081da177e4SLinus Torvalds 	char *t = to;
21091da177e4SLinus Torvalds 	const char __user *f = from;
21101da177e4SLinus Torvalds 	char c;
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
21131da177e4SLinus Torvalds 		return n;
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	while (n) {
21161da177e4SLinus Torvalds 		if (__get_user(c, f)) {
21171da177e4SLinus Torvalds 			memset(t, 0, n);
21181da177e4SLinus Torvalds 			break;
21191da177e4SLinus Torvalds 		}
21201da177e4SLinus Torvalds 		*t++ = c;
21211da177e4SLinus Torvalds 		f++;
21221da177e4SLinus Torvalds 		n--;
21231da177e4SLinus Torvalds 	}
21241da177e4SLinus Torvalds 	return n;
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
21281da177e4SLinus Torvalds {
21291da177e4SLinus Torvalds 	int i;
21301da177e4SLinus Torvalds 	unsigned long page;
21311da177e4SLinus Torvalds 	unsigned long size;
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	*where = 0;
21341da177e4SLinus Torvalds 	if (!data)
21351da177e4SLinus Torvalds 		return 0;
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
21381da177e4SLinus Torvalds 		return -ENOMEM;
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
21411da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
21421da177e4SLinus Torvalds 	 * the remainder of the page.
21431da177e4SLinus Torvalds 	 */
21441da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
21451da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
21461da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
21471da177e4SLinus Torvalds 		size = PAGE_SIZE;
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
21501da177e4SLinus Torvalds 	if (!i) {
21511da177e4SLinus Torvalds 		free_page(page);
21521da177e4SLinus Torvalds 		return -EFAULT;
21531da177e4SLinus Torvalds 	}
21541da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
21551da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
21561da177e4SLinus Torvalds 	*where = page;
21571da177e4SLinus Torvalds 	return 0;
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds 
2160eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where)
2161eca6f534SVegard Nossum {
2162eca6f534SVegard Nossum 	char *tmp;
2163eca6f534SVegard Nossum 
2164eca6f534SVegard Nossum 	if (!data) {
2165eca6f534SVegard Nossum 		*where = NULL;
2166eca6f534SVegard Nossum 		return 0;
2167eca6f534SVegard Nossum 	}
2168eca6f534SVegard Nossum 
2169eca6f534SVegard Nossum 	tmp = strndup_user(data, PAGE_SIZE);
2170eca6f534SVegard Nossum 	if (IS_ERR(tmp))
2171eca6f534SVegard Nossum 		return PTR_ERR(tmp);
2172eca6f534SVegard Nossum 
2173eca6f534SVegard Nossum 	*where = tmp;
2174eca6f534SVegard Nossum 	return 0;
2175eca6f534SVegard Nossum }
2176eca6f534SVegard Nossum 
21771da177e4SLinus Torvalds /*
21781da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
21791da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
21801da177e4SLinus Torvalds  *
21811da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
21821da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
21831da177e4SLinus Torvalds  * information (or be NULL).
21841da177e4SLinus Torvalds  *
21851da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
21861da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
21871da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
21881da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
21891da177e4SLinus Torvalds  * and must be discarded.
21901da177e4SLinus Torvalds  */
21911da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page,
21921da177e4SLinus Torvalds 		  unsigned long flags, void *data_page)
21931da177e4SLinus Torvalds {
21942d92ab3cSAl Viro 	struct path path;
21951da177e4SLinus Torvalds 	int retval = 0;
21961da177e4SLinus Torvalds 	int mnt_flags = 0;
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 	/* Discard magic */
21991da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
22001da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds 	/* Basic sanity checks */
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
22051da177e4SLinus Torvalds 		return -EINVAL;
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	if (data_page)
22081da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
22091da177e4SLinus Torvalds 
2210a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
2211a27ab9f2STetsuo Handa 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2212a27ab9f2STetsuo Handa 	if (retval)
2213a27ab9f2STetsuo Handa 		return retval;
2214a27ab9f2STetsuo Handa 
2215a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2216a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
2217a27ab9f2STetsuo Handa 	if (retval)
2218a27ab9f2STetsuo Handa 		goto dput_out;
2219a27ab9f2STetsuo Handa 
2220613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2221613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
22220a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
22230a1c01c9SMatthew Garrett 
22241da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
22251da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
22261da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
22271da177e4SLinus Torvalds 	if (flags & MS_NODEV)
22281da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
22291da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
22301da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2231fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2232fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2233fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2234fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2235d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2236d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
22372e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
22382e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2239fc33a7bbSChristoph Hellwig 
22407a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2241d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2242d0adde57SMatthew Garrett 		   MS_STRICTATIME);
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
22452d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
22461da177e4SLinus Torvalds 				    data_page);
22471da177e4SLinus Torvalds 	else if (flags & MS_BIND)
22482d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
22499676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
22502d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
22511da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
22522d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
22531da177e4SLinus Torvalds 	else
22542d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
22551da177e4SLinus Torvalds 				      dev_name, data_page);
22561da177e4SLinus Torvalds dput_out:
22572d92ab3cSAl Viro 	path_put(&path);
22581da177e4SLinus Torvalds 	return retval;
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds 
2261cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void)
2262cf8d2c11STrond Myklebust {
2263cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2264cf8d2c11STrond Myklebust 
2265cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2266cf8d2c11STrond Myklebust 	if (!new_ns)
2267cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
2268cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2269cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2270cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2271cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2272cf8d2c11STrond Myklebust 	new_ns->event = 0;
2273cf8d2c11STrond Myklebust 	return new_ns;
2274cf8d2c11STrond Myklebust }
2275cf8d2c11STrond Myklebust 
2276f03c6599SAl Viro void mnt_make_longterm(struct vfsmount *mnt)
2277f03c6599SAl Viro {
22787e3d0eb0SAl Viro 	__mnt_make_longterm(mnt);
2279f03c6599SAl Viro }
2280f03c6599SAl Viro 
2281f03c6599SAl Viro void mnt_make_shortterm(struct vfsmount *mnt)
2282f03c6599SAl Viro {
22837e3d0eb0SAl Viro #ifdef CONFIG_SMP
2284f03c6599SAl Viro 	if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
2285f03c6599SAl Viro 		return;
2286f03c6599SAl Viro 	br_write_lock(vfsmount_lock);
2287f03c6599SAl Viro 	atomic_dec(&mnt->mnt_longterm);
2288f03c6599SAl Viro 	br_write_unlock(vfsmount_lock);
22897e3d0eb0SAl Viro #endif
2290f03c6599SAl Viro }
2291f03c6599SAl Viro 
2292741a2951SJANAK DESAI /*
2293741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
2294741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
2295741a2951SJANAK DESAI  */
2296e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
22976b3286edSKirill Korotaev 		struct fs_struct *fs)
22981da177e4SLinus Torvalds {
22996b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
23007f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
23011da177e4SLinus Torvalds 	struct vfsmount *p, *q;
23021da177e4SLinus Torvalds 
2303cf8d2c11STrond Myklebust 	new_ns = alloc_mnt_ns();
2304cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2305cf8d2c11STrond Myklebust 		return new_ns;
23061da177e4SLinus Torvalds 
2307390c6843SRam Pai 	down_write(&namespace_sem);
23081da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
23096b3286edSKirill Korotaev 	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
23109676f0c6SRam Pai 					CL_COPY_ALL | CL_EXPIRE);
23111da177e4SLinus Torvalds 	if (!new_ns->root) {
2312390c6843SRam Pai 		up_write(&namespace_sem);
23131da177e4SLinus Torvalds 		kfree(new_ns);
23145cc4a034SJulia Lawall 		return ERR_PTR(-ENOMEM);
23151da177e4SLinus Torvalds 	}
231699b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
23171da177e4SLinus Torvalds 	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
231899b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	/*
23211da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
23221da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
23231da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
23241da177e4SLinus Torvalds 	 */
23256b3286edSKirill Korotaev 	p = mnt_ns->root;
23261da177e4SLinus Torvalds 	q = new_ns->root;
23271da177e4SLinus Torvalds 	while (p) {
23286b3286edSKirill Korotaev 		q->mnt_ns = new_ns;
23297e3d0eb0SAl Viro 		__mnt_make_longterm(q);
23301da177e4SLinus Torvalds 		if (fs) {
23316ac08c39SJan Blunck 			if (p == fs->root.mnt) {
2332f03c6599SAl Viro 				fs->root.mnt = mntget(q);
23337e3d0eb0SAl Viro 				__mnt_make_longterm(q);
2334f03c6599SAl Viro 				mnt_make_shortterm(p);
23351da177e4SLinus Torvalds 				rootmnt = p;
23361da177e4SLinus Torvalds 			}
23376ac08c39SJan Blunck 			if (p == fs->pwd.mnt) {
2338f03c6599SAl Viro 				fs->pwd.mnt = mntget(q);
23397e3d0eb0SAl Viro 				__mnt_make_longterm(q);
2340f03c6599SAl Viro 				mnt_make_shortterm(p);
23411da177e4SLinus Torvalds 				pwdmnt = p;
23421da177e4SLinus Torvalds 			}
23431da177e4SLinus Torvalds 		}
23446b3286edSKirill Korotaev 		p = next_mnt(p, mnt_ns->root);
23451da177e4SLinus Torvalds 		q = next_mnt(q, new_ns->root);
23461da177e4SLinus Torvalds 	}
2347390c6843SRam Pai 	up_write(&namespace_sem);
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	if (rootmnt)
2350f03c6599SAl Viro 		mntput(rootmnt);
23511da177e4SLinus Torvalds 	if (pwdmnt)
2352f03c6599SAl Viro 		mntput(pwdmnt);
23531da177e4SLinus Torvalds 
2354741a2951SJANAK DESAI 	return new_ns;
2355741a2951SJANAK DESAI }
2356741a2951SJANAK DESAI 
2357213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2358e3222c4eSBadari Pulavarty 		struct fs_struct *new_fs)
2359741a2951SJANAK DESAI {
23606b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
2361741a2951SJANAK DESAI 
2362e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
23636b3286edSKirill Korotaev 	get_mnt_ns(ns);
2364741a2951SJANAK DESAI 
2365741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
2366e3222c4eSBadari Pulavarty 		return ns;
2367741a2951SJANAK DESAI 
2368e3222c4eSBadari Pulavarty 	new_ns = dup_mnt_ns(ns, new_fs);
2369741a2951SJANAK DESAI 
23706b3286edSKirill Korotaev 	put_mnt_ns(ns);
2371e3222c4eSBadari Pulavarty 	return new_ns;
23721da177e4SLinus Torvalds }
23731da177e4SLinus Torvalds 
2374cf8d2c11STrond Myklebust /**
2375cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2376cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2377cf8d2c11STrond Myklebust  */
2378a2770d86SLinus Torvalds struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
2379cf8d2c11STrond Myklebust {
2380cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2381cf8d2c11STrond Myklebust 
2382cf8d2c11STrond Myklebust 	new_ns = alloc_mnt_ns();
2383cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
2384cf8d2c11STrond Myklebust 		mnt->mnt_ns = new_ns;
23857e3d0eb0SAl Viro 		__mnt_make_longterm(mnt);
2386cf8d2c11STrond Myklebust 		new_ns->root = mnt;
2387cf8d2c11STrond Myklebust 		list_add(&new_ns->list, &new_ns->root->mnt_list);
2388cf8d2c11STrond Myklebust 	}
2389cf8d2c11STrond Myklebust 	return new_ns;
2390cf8d2c11STrond Myklebust }
2391a2770d86SLinus Torvalds EXPORT_SYMBOL(create_mnt_ns);
2392cf8d2c11STrond Myklebust 
2393bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2394bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
23951da177e4SLinus Torvalds {
2396eca6f534SVegard Nossum 	int ret;
2397eca6f534SVegard Nossum 	char *kernel_type;
2398eca6f534SVegard Nossum 	char *kernel_dir;
2399eca6f534SVegard Nossum 	char *kernel_dev;
24001da177e4SLinus Torvalds 	unsigned long data_page;
24011da177e4SLinus Torvalds 
2402eca6f534SVegard Nossum 	ret = copy_mount_string(type, &kernel_type);
2403eca6f534SVegard Nossum 	if (ret < 0)
2404eca6f534SVegard Nossum 		goto out_type;
24051da177e4SLinus Torvalds 
2406eca6f534SVegard Nossum 	kernel_dir = getname(dir_name);
2407eca6f534SVegard Nossum 	if (IS_ERR(kernel_dir)) {
2408eca6f534SVegard Nossum 		ret = PTR_ERR(kernel_dir);
2409eca6f534SVegard Nossum 		goto out_dir;
2410eca6f534SVegard Nossum 	}
24111da177e4SLinus Torvalds 
2412eca6f534SVegard Nossum 	ret = copy_mount_string(dev_name, &kernel_dev);
2413eca6f534SVegard Nossum 	if (ret < 0)
2414eca6f534SVegard Nossum 		goto out_dev;
24151da177e4SLinus Torvalds 
2416eca6f534SVegard Nossum 	ret = copy_mount_options(data, &data_page);
2417eca6f534SVegard Nossum 	if (ret < 0)
2418eca6f534SVegard Nossum 		goto out_data;
24191da177e4SLinus Torvalds 
2420eca6f534SVegard Nossum 	ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
2421eca6f534SVegard Nossum 		(void *) data_page);
2422eca6f534SVegard Nossum 
24231da177e4SLinus Torvalds 	free_page(data_page);
2424eca6f534SVegard Nossum out_data:
2425eca6f534SVegard Nossum 	kfree(kernel_dev);
2426eca6f534SVegard Nossum out_dev:
2427eca6f534SVegard Nossum 	putname(kernel_dir);
2428eca6f534SVegard Nossum out_dir:
2429eca6f534SVegard Nossum 	kfree(kernel_type);
2430eca6f534SVegard Nossum out_type:
2431eca6f534SVegard Nossum 	return ret;
24321da177e4SLinus Torvalds }
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds /*
24351da177e4SLinus Torvalds  * pivot_root Semantics:
24361da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
24371da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
24381da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
24391da177e4SLinus Torvalds  *
24401da177e4SLinus Torvalds  * Restrictions:
24411da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
24421da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
24431da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
24441da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
24451da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
24461da177e4SLinus Torvalds  *
24474a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
24484a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
24494a0d11faSNeil Brown  * in this situation.
24504a0d11faSNeil Brown  *
24511da177e4SLinus Torvalds  * Notes:
24521da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
24531da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
24541da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
24551da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
24561da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
24571da177e4SLinus Torvalds  *    first.
24581da177e4SLinus Torvalds  */
24593480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
24603480b257SHeiko Carstens 		const char __user *, put_old)
24611da177e4SLinus Torvalds {
24621da177e4SLinus Torvalds 	struct vfsmount *tmp;
24632d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
24641da177e4SLinus Torvalds 	int error;
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
24671da177e4SLinus Torvalds 		return -EPERM;
24681da177e4SLinus Torvalds 
24692d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
24701da177e4SLinus Torvalds 	if (error)
24711da177e4SLinus Torvalds 		goto out0;
24721da177e4SLinus Torvalds 	error = -EINVAL;
24732d8f3038SAl Viro 	if (!check_mnt(new.mnt))
24741da177e4SLinus Torvalds 		goto out1;
24751da177e4SLinus Torvalds 
24762d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
24771da177e4SLinus Torvalds 	if (error)
24781da177e4SLinus Torvalds 		goto out1;
24791da177e4SLinus Torvalds 
24802d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
24811da177e4SLinus Torvalds 	if (error) {
24822d8f3038SAl Viro 		path_put(&old);
24831da177e4SLinus Torvalds 		goto out1;
24841da177e4SLinus Torvalds 	}
24851da177e4SLinus Torvalds 
2486f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
2487390c6843SRam Pai 	down_write(&namespace_sem);
24882d8f3038SAl Viro 	mutex_lock(&old.dentry->d_inode->i_mutex);
24891da177e4SLinus Torvalds 	error = -EINVAL;
24902d8f3038SAl Viro 	if (IS_MNT_SHARED(old.mnt) ||
24912d8f3038SAl Viro 		IS_MNT_SHARED(new.mnt->mnt_parent) ||
24928c3ee42eSAl Viro 		IS_MNT_SHARED(root.mnt->mnt_parent))
249321444403SRam Pai 		goto out2;
24948c3ee42eSAl Viro 	if (!check_mnt(root.mnt))
24951da177e4SLinus Torvalds 		goto out2;
24961da177e4SLinus Torvalds 	error = -ENOENT;
2497d83c49f3SAl Viro 	if (cant_mount(old.dentry))
24981da177e4SLinus Torvalds 		goto out2;
2499f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
25001da177e4SLinus Torvalds 		goto out2;
2501f3da392eSAlexey Dobriyan 	if (d_unlinked(old.dentry))
25021da177e4SLinus Torvalds 		goto out2;
25031da177e4SLinus Torvalds 	error = -EBUSY;
25042d8f3038SAl Viro 	if (new.mnt == root.mnt ||
25052d8f3038SAl Viro 	    old.mnt == root.mnt)
25061da177e4SLinus Torvalds 		goto out2; /* loop, on the same file system  */
25071da177e4SLinus Torvalds 	error = -EINVAL;
25088c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
25091da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
25108c3ee42eSAl Viro 	if (root.mnt->mnt_parent == root.mnt)
25110bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
25122d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
25131da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
25142d8f3038SAl Viro 	if (new.mnt->mnt_parent == new.mnt)
25150bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
25164ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
25172d8f3038SAl Viro 	tmp = old.mnt;
251899b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
25192d8f3038SAl Viro 	if (tmp != new.mnt) {
25201da177e4SLinus Torvalds 		for (;;) {
25211da177e4SLinus Torvalds 			if (tmp->mnt_parent == tmp)
25221da177e4SLinus Torvalds 				goto out3; /* already mounted on put_old */
25232d8f3038SAl Viro 			if (tmp->mnt_parent == new.mnt)
25241da177e4SLinus Torvalds 				break;
25251da177e4SLinus Torvalds 			tmp = tmp->mnt_parent;
25261da177e4SLinus Torvalds 		}
25272d8f3038SAl Viro 		if (!is_subdir(tmp->mnt_mountpoint, new.dentry))
25281da177e4SLinus Torvalds 			goto out3;
25292d8f3038SAl Viro 	} else if (!is_subdir(old.dentry, new.dentry))
25301da177e4SLinus Torvalds 		goto out3;
25312d8f3038SAl Viro 	detach_mnt(new.mnt, &parent_path);
25328c3ee42eSAl Viro 	detach_mnt(root.mnt, &root_parent);
25334ac91378SJan Blunck 	/* mount old root on put_old */
25342d8f3038SAl Viro 	attach_mnt(root.mnt, &old);
25354ac91378SJan Blunck 	/* mount new_root on / */
25362d8f3038SAl Viro 	attach_mnt(new.mnt, &root_parent);
25376b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
253899b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
25392d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
2540b3e19d92SNick Piggin 
25411da177e4SLinus Torvalds 	error = 0;
25421a390689SAl Viro 	path_put(&root_parent);
25431a390689SAl Viro 	path_put(&parent_path);
25441da177e4SLinus Torvalds out2:
25452d8f3038SAl Viro 	mutex_unlock(&old.dentry->d_inode->i_mutex);
2546390c6843SRam Pai 	up_write(&namespace_sem);
25478c3ee42eSAl Viro 	path_put(&root);
25482d8f3038SAl Viro 	path_put(&old);
25491da177e4SLinus Torvalds out1:
25502d8f3038SAl Viro 	path_put(&new);
25511da177e4SLinus Torvalds out0:
25521da177e4SLinus Torvalds 	return error;
25531da177e4SLinus Torvalds out3:
255499b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
25551da177e4SLinus Torvalds 	goto out2;
25561da177e4SLinus Torvalds }
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds static void __init init_mount_tree(void)
25591da177e4SLinus Torvalds {
25601da177e4SLinus Torvalds 	struct vfsmount *mnt;
25616b3286edSKirill Korotaev 	struct mnt_namespace *ns;
2562ac748a09SJan Blunck 	struct path root;
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
25651da177e4SLinus Torvalds 	if (IS_ERR(mnt))
25661da177e4SLinus Torvalds 		panic("Can't create rootfs");
2567b3e19d92SNick Piggin 
25683b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
25693b22edc5STrond Myklebust 	if (IS_ERR(ns))
25701da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
25711da177e4SLinus Torvalds 
25726b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
25736b3286edSKirill Korotaev 	get_mnt_ns(ns);
25741da177e4SLinus Torvalds 
2575ac748a09SJan Blunck 	root.mnt = ns->root;
2576ac748a09SJan Blunck 	root.dentry = ns->root->mnt_root;
2577ac748a09SJan Blunck 
2578ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
2579ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
25801da177e4SLinus Torvalds }
25811da177e4SLinus Torvalds 
258274bf17cfSDenis Cheng void __init mnt_init(void)
25831da177e4SLinus Torvalds {
258413f14b4dSEric Dumazet 	unsigned u;
258515a67dd8SRandy Dunlap 	int err;
25861da177e4SLinus Torvalds 
2587390c6843SRam Pai 	init_rwsem(&namespace_sem);
2588390c6843SRam Pai 
25891da177e4SLinus Torvalds 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
259020c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
25911da177e4SLinus Torvalds 
2592b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds 	if (!mount_hashtable)
25951da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
25961da177e4SLinus Torvalds 
259713f14b4dSEric Dumazet 	printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
25981da177e4SLinus Torvalds 
259913f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
260013f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
26011da177e4SLinus Torvalds 
260299b7db7bSNick Piggin 	br_lock_init(vfsmount_lock);
260399b7db7bSNick Piggin 
260415a67dd8SRandy Dunlap 	err = sysfs_init();
260515a67dd8SRandy Dunlap 	if (err)
260615a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
26078e24eea7SHarvey Harrison 			__func__, err);
260800d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
260900d26666SGreg Kroah-Hartman 	if (!fs_kobj)
26108e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
26111da177e4SLinus Torvalds 	init_rootfs();
26121da177e4SLinus Torvalds 	init_mount_tree();
26131da177e4SLinus Torvalds }
26141da177e4SLinus Torvalds 
2615616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
26161da177e4SLinus Torvalds {
261770fbcdf4SRam Pai 	LIST_HEAD(umount_list);
2618616511d0STrond Myklebust 
2619d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
2620616511d0STrond Myklebust 		return;
2621390c6843SRam Pai 	down_write(&namespace_sem);
262299b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
2623d498b25aSAl Viro 	umount_tree(ns->root, 0, &umount_list);
262499b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
2625390c6843SRam Pai 	up_write(&namespace_sem);
262670fbcdf4SRam Pai 	release_mounts(&umount_list);
26276b3286edSKirill Korotaev 	kfree(ns);
26281da177e4SLinus Torvalds }
2629cf8d2c11STrond Myklebust EXPORT_SYMBOL(put_mnt_ns);
2630