xref: /openbmc/linux/fs/namespace.c (revision 4b8b21f4)
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  */
1134b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *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,
1224b8b21f4SAl Viro 				&mnt->mnt.mnt_group_id);
123f21f6220SAl Viro 	if (!res)
1244b8b21f4SAl Viro 		mnt_group_start = mnt->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  */
1324b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt)
133719f5d7fSMiklos Szeredi {
1344b8b21f4SAl Viro 	int id = mnt->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;
1384b8b21f4SAl Viro 	mnt->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 /*
156b3e19d92SNick Piggin  * vfsmount lock must be held for write
157b3e19d92SNick Piggin  */
158b3e19d92SNick Piggin unsigned int mnt_get_count(struct vfsmount *mnt)
159b3e19d92SNick Piggin {
160b3e19d92SNick Piggin #ifdef CONFIG_SMP
161f03c6599SAl Viro 	unsigned int count = 0;
162b3e19d92SNick Piggin 	int cpu;
163b3e19d92SNick Piggin 
164b3e19d92SNick Piggin 	for_each_possible_cpu(cpu) {
165b3e19d92SNick Piggin 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
166b3e19d92SNick Piggin 	}
167b3e19d92SNick Piggin 
168b3e19d92SNick Piggin 	return count;
169b3e19d92SNick Piggin #else
170b3e19d92SNick Piggin 	return mnt->mnt_count;
171b3e19d92SNick Piggin #endif
172b3e19d92SNick Piggin }
173b3e19d92SNick Piggin 
1749d412a43SAl Viro static struct vfsmount *alloc_vfsmnt(const char *name)
1751da177e4SLinus Torvalds {
1767d6fec45SAl Viro 	struct mount *p = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
1777d6fec45SAl Viro 	if (p) {
1787d6fec45SAl Viro 		struct vfsmount *mnt = &p->mnt;
17973cd49ecSMiklos Szeredi 		int err;
18073cd49ecSMiklos Szeredi 
18173cd49ecSMiklos Szeredi 		err = mnt_alloc_id(mnt);
18288b38782SLi Zefan 		if (err)
18388b38782SLi Zefan 			goto out_free_cache;
18488b38782SLi Zefan 
18588b38782SLi Zefan 		if (name) {
18688b38782SLi Zefan 			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
18788b38782SLi Zefan 			if (!mnt->mnt_devname)
18888b38782SLi Zefan 				goto out_free_id;
18973cd49ecSMiklos Szeredi 		}
19073cd49ecSMiklos Szeredi 
191b3e19d92SNick Piggin #ifdef CONFIG_SMP
192b3e19d92SNick Piggin 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
193b3e19d92SNick Piggin 		if (!mnt->mnt_pcp)
194b3e19d92SNick Piggin 			goto out_free_devname;
195b3e19d92SNick Piggin 
196f03c6599SAl Viro 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
197b3e19d92SNick Piggin #else
198b3e19d92SNick Piggin 		mnt->mnt_count = 1;
199b3e19d92SNick Piggin 		mnt->mnt_writers = 0;
200b3e19d92SNick Piggin #endif
201b3e19d92SNick Piggin 
2021da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_hash);
2031da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_child);
2041da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_mounts);
2051da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_list);
20655e700b9SMiklos Szeredi 		INIT_LIST_HEAD(&mnt->mnt_expire);
20703e06e68SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_share);
208a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
209a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave);
2102504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY
2112504c5d6SAndreas Gruenbacher 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
2122504c5d6SAndreas Gruenbacher #endif
2131da177e4SLinus Torvalds 	}
2147d6fec45SAl Viro 	return &p->mnt;
21588b38782SLi Zefan 
216d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
217d3ef3d73Snpiggin@suse.de out_free_devname:
2187d6fec45SAl Viro 	kfree(p->mnt.mnt_devname);
219d3ef3d73Snpiggin@suse.de #endif
22088b38782SLi Zefan out_free_id:
2217d6fec45SAl Viro 	mnt_free_id(&p->mnt);
22288b38782SLi Zefan out_free_cache:
2237d6fec45SAl Viro 	kmem_cache_free(mnt_cache, p);
22488b38782SLi Zefan 	return NULL;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2278366025eSDave Hansen /*
2288366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
2298366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
2308366025eSDave Hansen  * We must keep track of when those operations start
2318366025eSDave Hansen  * (for permission checks) and when they end, so that
2328366025eSDave Hansen  * we can determine when writes are able to occur to
2338366025eSDave Hansen  * a filesystem.
2348366025eSDave Hansen  */
2353d733633SDave Hansen /*
2363d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2373d733633SDave Hansen  * @mnt: the mount to check for its write status
2383d733633SDave Hansen  *
2393d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2403d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2413d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2423d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2433d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2443d733633SDave Hansen  * r/w.
2453d733633SDave Hansen  */
2463d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2473d733633SDave Hansen {
2482e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2492e4b7fcdSDave Hansen 		return 1;
2502e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
2512e4b7fcdSDave Hansen 		return 1;
2522e4b7fcdSDave Hansen 	return 0;
2533d733633SDave Hansen }
2543d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2553d733633SDave Hansen 
256c6653a83SNick Piggin static inline void mnt_inc_writers(struct vfsmount *mnt)
2573d733633SDave Hansen {
258d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
259b3e19d92SNick Piggin 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
260d3ef3d73Snpiggin@suse.de #else
261d3ef3d73Snpiggin@suse.de 	mnt->mnt_writers++;
262d3ef3d73Snpiggin@suse.de #endif
2633d733633SDave Hansen }
2643d733633SDave Hansen 
265c6653a83SNick Piggin static inline void mnt_dec_writers(struct vfsmount *mnt)
2663d733633SDave Hansen {
267d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
268b3e19d92SNick Piggin 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
269d3ef3d73Snpiggin@suse.de #else
270d3ef3d73Snpiggin@suse.de 	mnt->mnt_writers--;
271d3ef3d73Snpiggin@suse.de #endif
272d3ef3d73Snpiggin@suse.de }
273d3ef3d73Snpiggin@suse.de 
274c6653a83SNick Piggin static unsigned int mnt_get_writers(struct vfsmount *mnt)
275d3ef3d73Snpiggin@suse.de {
276d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
277d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2783d733633SDave Hansen 	int cpu;
2793d733633SDave Hansen 
2803d733633SDave Hansen 	for_each_possible_cpu(cpu) {
281b3e19d92SNick Piggin 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
2823d733633SDave Hansen 	}
2833d733633SDave Hansen 
284d3ef3d73Snpiggin@suse.de 	return count;
285d3ef3d73Snpiggin@suse.de #else
286d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
287d3ef3d73Snpiggin@suse.de #endif
2883d733633SDave Hansen }
2893d733633SDave Hansen 
2903d733633SDave Hansen /*
2913d733633SDave Hansen  * Most r/o checks on a fs are for operations that take
2923d733633SDave Hansen  * discrete amounts of time, like a write() or unlink().
2933d733633SDave Hansen  * We must keep track of when those operations start
2943d733633SDave Hansen  * (for permission checks) and when they end, so that
2953d733633SDave Hansen  * we can determine when writes are able to occur to
2963d733633SDave Hansen  * a filesystem.
2973d733633SDave Hansen  */
2988366025eSDave Hansen /**
2998366025eSDave Hansen  * mnt_want_write - get write access to a mount
3008366025eSDave Hansen  * @mnt: the mount on which to take a write
3018366025eSDave Hansen  *
3028366025eSDave Hansen  * This tells the low-level filesystem that a write is
3038366025eSDave Hansen  * about to be performed to it, and makes sure that
3048366025eSDave Hansen  * writes are allowed before returning success.  When
3058366025eSDave Hansen  * the write operation is finished, mnt_drop_write()
3068366025eSDave Hansen  * must be called.  This is effectively a refcount.
3078366025eSDave Hansen  */
3088366025eSDave Hansen int mnt_want_write(struct vfsmount *mnt)
3098366025eSDave Hansen {
3103d733633SDave Hansen 	int ret = 0;
3113d733633SDave Hansen 
312d3ef3d73Snpiggin@suse.de 	preempt_disable();
313c6653a83SNick Piggin 	mnt_inc_writers(mnt);
314d3ef3d73Snpiggin@suse.de 	/*
315c6653a83SNick Piggin 	 * The store to mnt_inc_writers must be visible before we pass
316d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
317d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
318d3ef3d73Snpiggin@suse.de 	 */
319d3ef3d73Snpiggin@suse.de 	smp_mb();
320d3ef3d73Snpiggin@suse.de 	while (mnt->mnt_flags & MNT_WRITE_HOLD)
321d3ef3d73Snpiggin@suse.de 		cpu_relax();
322d3ef3d73Snpiggin@suse.de 	/*
323d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
324d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
325d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
326d3ef3d73Snpiggin@suse.de 	 */
327d3ef3d73Snpiggin@suse.de 	smp_rmb();
3283d733633SDave Hansen 	if (__mnt_is_readonly(mnt)) {
329c6653a83SNick Piggin 		mnt_dec_writers(mnt);
3303d733633SDave Hansen 		ret = -EROFS;
3313d733633SDave Hansen 		goto out;
3323d733633SDave Hansen 	}
3333d733633SDave Hansen out:
334d3ef3d73Snpiggin@suse.de 	preempt_enable();
3353d733633SDave Hansen 	return ret;
3368366025eSDave Hansen }
3378366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3388366025eSDave Hansen 
3398366025eSDave Hansen /**
34096029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
34196029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
34296029c4eSnpiggin@suse.de  *
34396029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
34496029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
34596029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
34696029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
34796029c4eSnpiggin@suse.de  *
34896029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
34996029c4eSnpiggin@suse.de  * drop the reference.
35096029c4eSnpiggin@suse.de  */
35196029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
35296029c4eSnpiggin@suse.de {
35396029c4eSnpiggin@suse.de 	/* superblock may be r/o */
35496029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
35596029c4eSnpiggin@suse.de 		return -EROFS;
35696029c4eSnpiggin@suse.de 	preempt_disable();
357c6653a83SNick Piggin 	mnt_inc_writers(mnt);
35896029c4eSnpiggin@suse.de 	preempt_enable();
35996029c4eSnpiggin@suse.de 	return 0;
36096029c4eSnpiggin@suse.de }
36196029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
36296029c4eSnpiggin@suse.de 
36396029c4eSnpiggin@suse.de /**
36496029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
36596029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
36696029c4eSnpiggin@suse.de  *
36796029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
36896029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
36996029c4eSnpiggin@suse.de  */
37096029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
37196029c4eSnpiggin@suse.de {
3722d8dd38aSOGAWA Hirofumi 	struct inode *inode = file->f_dentry->d_inode;
3732d8dd38aSOGAWA Hirofumi 	if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
37496029c4eSnpiggin@suse.de 		return mnt_want_write(file->f_path.mnt);
37596029c4eSnpiggin@suse.de 	else
37696029c4eSnpiggin@suse.de 		return mnt_clone_write(file->f_path.mnt);
37796029c4eSnpiggin@suse.de }
37896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
37996029c4eSnpiggin@suse.de 
38096029c4eSnpiggin@suse.de /**
3818366025eSDave Hansen  * mnt_drop_write - give up write access to a mount
3828366025eSDave Hansen  * @mnt: the mount on which to give up write access
3838366025eSDave Hansen  *
3848366025eSDave Hansen  * Tells the low-level filesystem that we are done
3858366025eSDave Hansen  * performing writes to it.  Must be matched with
3868366025eSDave Hansen  * mnt_want_write() call above.
3878366025eSDave Hansen  */
3888366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt)
3898366025eSDave Hansen {
390d3ef3d73Snpiggin@suse.de 	preempt_disable();
391c6653a83SNick Piggin 	mnt_dec_writers(mnt);
392d3ef3d73Snpiggin@suse.de 	preempt_enable();
3938366025eSDave Hansen }
3948366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
3958366025eSDave Hansen 
3962a79f17eSAl Viro void mnt_drop_write_file(struct file *file)
3972a79f17eSAl Viro {
3982a79f17eSAl Viro 	mnt_drop_write(file->f_path.mnt);
3992a79f17eSAl Viro }
4002a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file);
4012a79f17eSAl Viro 
4022e4b7fcdSDave Hansen static int mnt_make_readonly(struct vfsmount *mnt)
4038366025eSDave Hansen {
4043d733633SDave Hansen 	int ret = 0;
4053d733633SDave Hansen 
40699b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
407d3ef3d73Snpiggin@suse.de 	mnt->mnt_flags |= MNT_WRITE_HOLD;
408d3ef3d73Snpiggin@suse.de 	/*
409d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
410d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
411d3ef3d73Snpiggin@suse.de 	 */
412d3ef3d73Snpiggin@suse.de 	smp_mb();
413d3ef3d73Snpiggin@suse.de 
414d3ef3d73Snpiggin@suse.de 	/*
415d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
416d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
417d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
418d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
419d3ef3d73Snpiggin@suse.de 	 *
420d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
421d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
422d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
423d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
424d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
425d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
426d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
427d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
428d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
429d3ef3d73Snpiggin@suse.de 	 */
430c6653a83SNick Piggin 	if (mnt_get_writers(mnt) > 0)
431d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
432d3ef3d73Snpiggin@suse.de 	else
4332e4b7fcdSDave Hansen 		mnt->mnt_flags |= MNT_READONLY;
434d3ef3d73Snpiggin@suse.de 	/*
435d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
436d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
437d3ef3d73Snpiggin@suse.de 	 */
438d3ef3d73Snpiggin@suse.de 	smp_wmb();
439d3ef3d73Snpiggin@suse.de 	mnt->mnt_flags &= ~MNT_WRITE_HOLD;
44099b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
4413d733633SDave Hansen 	return ret;
4423d733633SDave Hansen }
4438366025eSDave Hansen 
4442e4b7fcdSDave Hansen static void __mnt_unmake_readonly(struct vfsmount *mnt)
4452e4b7fcdSDave Hansen {
44699b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
4472e4b7fcdSDave Hansen 	mnt->mnt_flags &= ~MNT_READONLY;
44899b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
4492e4b7fcdSDave Hansen }
4502e4b7fcdSDave Hansen 
4519d412a43SAl Viro static void free_vfsmnt(struct vfsmount *mnt)
4521da177e4SLinus Torvalds {
4537d6fec45SAl Viro 	struct mount *p = real_mount(mnt);
4541da177e4SLinus Torvalds 	kfree(mnt->mnt_devname);
45573cd49ecSMiklos Szeredi 	mnt_free_id(mnt);
456d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
457b3e19d92SNick Piggin 	free_percpu(mnt->mnt_pcp);
458d3ef3d73Snpiggin@suse.de #endif
4597d6fec45SAl Viro 	kmem_cache_free(mnt_cache, p);
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /*
463a05964f3SRam Pai  * find the first or last mount at @dentry on vfsmount @mnt depending on
464a05964f3SRam Pai  * @dir. If @dir is set return the first mount else return the last mount.
46599b7db7bSNick Piggin  * vfsmount_lock must be held for read or write.
4661da177e4SLinus Torvalds  */
467c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
468a05964f3SRam Pai 			      int dir)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
4711da177e4SLinus Torvalds 	struct list_head *tmp = head;
472c7105365SAl Viro 	struct mount *p, *found = NULL;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	for (;;) {
475a05964f3SRam Pai 		tmp = dir ? tmp->next : tmp->prev;
4761da177e4SLinus Torvalds 		p = NULL;
4771da177e4SLinus Torvalds 		if (tmp == head)
4781da177e4SLinus Torvalds 			break;
479c7105365SAl Viro 		p = list_entry(tmp, struct mount, mnt.mnt_hash);
480c7105365SAl Viro 		if (p->mnt.mnt_parent == mnt && p->mnt.mnt_mountpoint == dentry) {
481a05964f3SRam Pai 			found = p;
4821da177e4SLinus Torvalds 			break;
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 	return found;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
488a05964f3SRam Pai /*
489a05964f3SRam Pai  * lookup_mnt increments the ref count before returning
490a05964f3SRam Pai  * the vfsmount struct.
491a05964f3SRam Pai  */
4921c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
493a05964f3SRam Pai {
494c7105365SAl Viro 	struct mount *child_mnt;
49599b7db7bSNick Piggin 
49699b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
497c7105365SAl Viro 	child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
498c7105365SAl Viro 	if (child_mnt) {
499c7105365SAl Viro 		mnt_add_count(child_mnt, 1);
50099b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
501c7105365SAl Viro 		return &child_mnt->mnt;
502c7105365SAl Viro 	} else {
503c7105365SAl Viro 		br_read_unlock(vfsmount_lock);
504c7105365SAl Viro 		return NULL;
505c7105365SAl Viro 	}
506a05964f3SRam Pai }
507a05964f3SRam Pai 
5081da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt)
5091da177e4SLinus Torvalds {
5106b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
51399b7db7bSNick Piggin /*
51499b7db7bSNick Piggin  * vfsmount lock must be held for write
51599b7db7bSNick Piggin  */
5166b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
5175addc5ddSAl Viro {
5185addc5ddSAl Viro 	if (ns) {
5195addc5ddSAl Viro 		ns->event = ++event;
5205addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
5215addc5ddSAl Viro 	}
5225addc5ddSAl Viro }
5235addc5ddSAl Viro 
52499b7db7bSNick Piggin /*
52599b7db7bSNick Piggin  * vfsmount lock must be held for write
52699b7db7bSNick Piggin  */
5276b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
5285addc5ddSAl Viro {
5295addc5ddSAl Viro 	if (ns && ns->event != event) {
5305addc5ddSAl Viro 		ns->event = event;
5315addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
5325addc5ddSAl Viro 	}
5335addc5ddSAl Viro }
5345addc5ddSAl Viro 
53599b7db7bSNick Piggin /*
5365f57cbccSNick Piggin  * Clear dentry's mounted state if it has no remaining mounts.
5375f57cbccSNick Piggin  * vfsmount_lock must be held for write.
5385f57cbccSNick Piggin  */
539aa0a4cf0SAl Viro static void dentry_reset_mounted(struct dentry *dentry)
5405f57cbccSNick Piggin {
5415f57cbccSNick Piggin 	unsigned u;
5425f57cbccSNick Piggin 
5435f57cbccSNick Piggin 	for (u = 0; u < HASH_SIZE; u++) {
5445f57cbccSNick Piggin 		struct vfsmount *p;
5455f57cbccSNick Piggin 
5465f57cbccSNick Piggin 		list_for_each_entry(p, &mount_hashtable[u], mnt_hash) {
5475f57cbccSNick Piggin 			if (p->mnt_mountpoint == dentry)
5485f57cbccSNick Piggin 				return;
5495f57cbccSNick Piggin 		}
5505f57cbccSNick Piggin 	}
5515f57cbccSNick Piggin 	spin_lock(&dentry->d_lock);
5525f57cbccSNick Piggin 	dentry->d_flags &= ~DCACHE_MOUNTED;
5535f57cbccSNick Piggin 	spin_unlock(&dentry->d_lock);
5545f57cbccSNick Piggin }
5555f57cbccSNick Piggin 
5565f57cbccSNick Piggin /*
55799b7db7bSNick Piggin  * vfsmount lock must be held for write
55899b7db7bSNick Piggin  */
559419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path)
5601da177e4SLinus Torvalds {
561419148daSAl Viro 	old_path->dentry = mnt->mnt.mnt_mountpoint;
562419148daSAl Viro 	old_path->mnt = mnt->mnt.mnt_parent;
563419148daSAl Viro 	mnt->mnt.mnt_parent = &mnt->mnt;
564419148daSAl Viro 	mnt->mnt.mnt_mountpoint = mnt->mnt.mnt_root;
565419148daSAl Viro 	list_del_init(&mnt->mnt.mnt_child);
566419148daSAl Viro 	list_del_init(&mnt->mnt.mnt_hash);
567aa0a4cf0SAl Viro 	dentry_reset_mounted(old_path->dentry);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
57099b7db7bSNick Piggin /*
57199b7db7bSNick Piggin  * vfsmount lock must be held for write
57299b7db7bSNick Piggin  */
573b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
574b90fa9aeSRam Pai 			struct vfsmount *child_mnt)
575b90fa9aeSRam Pai {
576b90fa9aeSRam Pai 	child_mnt->mnt_parent = mntget(mnt);
577b90fa9aeSRam Pai 	child_mnt->mnt_mountpoint = dget(dentry);
5785f57cbccSNick Piggin 	spin_lock(&dentry->d_lock);
5795f57cbccSNick Piggin 	dentry->d_flags |= DCACHE_MOUNTED;
5805f57cbccSNick Piggin 	spin_unlock(&dentry->d_lock);
581b90fa9aeSRam Pai }
582b90fa9aeSRam Pai 
58399b7db7bSNick Piggin /*
58499b7db7bSNick Piggin  * vfsmount lock must be held for write
58599b7db7bSNick Piggin  */
586419148daSAl Viro static void attach_mnt(struct mount *mnt, struct path *path)
5871da177e4SLinus Torvalds {
588419148daSAl Viro 	mnt_set_mountpoint(path->mnt, path->dentry, &mnt->mnt);
589419148daSAl Viro 	list_add_tail(&mnt->mnt.mnt_hash, mount_hashtable +
5901a390689SAl Viro 			hash(path->mnt, path->dentry));
591419148daSAl Viro 	list_add_tail(&mnt->mnt.mnt_child, &path->mnt->mnt_mounts);
592b90fa9aeSRam Pai }
593b90fa9aeSRam Pai 
5947e3d0eb0SAl Viro static inline void __mnt_make_longterm(struct vfsmount *mnt)
5957e3d0eb0SAl Viro {
5967e3d0eb0SAl Viro #ifdef CONFIG_SMP
5977e3d0eb0SAl Viro 	atomic_inc(&mnt->mnt_longterm);
5987e3d0eb0SAl Viro #endif
5997e3d0eb0SAl Viro }
6007e3d0eb0SAl Viro 
6017e3d0eb0SAl Viro /* needs vfsmount lock for write */
6027e3d0eb0SAl Viro static inline void __mnt_make_shortterm(struct vfsmount *mnt)
6037e3d0eb0SAl Viro {
6047e3d0eb0SAl Viro #ifdef CONFIG_SMP
6057e3d0eb0SAl Viro 	atomic_dec(&mnt->mnt_longterm);
6067e3d0eb0SAl Viro #endif
6077e3d0eb0SAl Viro }
6087e3d0eb0SAl Viro 
609b90fa9aeSRam Pai /*
61099b7db7bSNick Piggin  * vfsmount lock must be held for write
611b90fa9aeSRam Pai  */
6124b2619a5SAl Viro static void commit_tree(struct mount *mnt)
613b90fa9aeSRam Pai {
6144b2619a5SAl Viro 	struct vfsmount *parent = mnt->mnt.mnt_parent;
615b90fa9aeSRam Pai 	struct vfsmount *m;
616b90fa9aeSRam Pai 	LIST_HEAD(head);
6176b3286edSKirill Korotaev 	struct mnt_namespace *n = parent->mnt_ns;
618b90fa9aeSRam Pai 
6194b2619a5SAl Viro 	BUG_ON(parent == &mnt->mnt);
620b90fa9aeSRam Pai 
6214b2619a5SAl Viro 	list_add_tail(&head, &mnt->mnt.mnt_list);
622f03c6599SAl Viro 	list_for_each_entry(m, &head, mnt_list) {
6236b3286edSKirill Korotaev 		m->mnt_ns = n;
6247e3d0eb0SAl Viro 		__mnt_make_longterm(m);
625f03c6599SAl Viro 	}
626f03c6599SAl Viro 
627b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
628b90fa9aeSRam Pai 
6294b2619a5SAl Viro 	list_add_tail(&mnt->mnt.mnt_hash, mount_hashtable +
6304b2619a5SAl Viro 				hash(parent, mnt->mnt.mnt_mountpoint));
6314b2619a5SAl Viro 	list_add_tail(&mnt->mnt.mnt_child, &parent->mnt_mounts);
6326b3286edSKirill Korotaev 	touch_mnt_namespace(n);
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
635315fc83eSAl Viro static struct mount *next_mnt(struct mount *p, struct vfsmount *root)
6361da177e4SLinus Torvalds {
637315fc83eSAl Viro 	struct list_head *next = p->mnt.mnt_mounts.next;
638315fc83eSAl Viro 	if (next == &p->mnt.mnt_mounts) {
6391da177e4SLinus Torvalds 		while (1) {
640315fc83eSAl Viro 			if (&p->mnt == root)
6411da177e4SLinus Torvalds 				return NULL;
642315fc83eSAl Viro 			next = p->mnt.mnt_child.next;
643315fc83eSAl Viro 			if (next != &p->mnt.mnt_parent->mnt_mounts)
6441da177e4SLinus Torvalds 				break;
645315fc83eSAl Viro 			p = real_mount(p->mnt.mnt_parent);
6461da177e4SLinus Torvalds 		}
6471da177e4SLinus Torvalds 	}
648315fc83eSAl Viro 	return list_entry(next, struct mount, mnt.mnt_child);
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
651315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p)
6529676f0c6SRam Pai {
653315fc83eSAl Viro 	struct list_head *prev = p->mnt.mnt_mounts.prev;
654315fc83eSAl Viro 	while (prev != &p->mnt.mnt_mounts) {
655315fc83eSAl Viro 		p = list_entry(prev, struct mount, mnt.mnt_child);
656315fc83eSAl Viro 		prev = p->mnt.mnt_mounts.prev;
6579676f0c6SRam Pai 	}
6589676f0c6SRam Pai 	return p;
6599676f0c6SRam Pai }
6609676f0c6SRam Pai 
6619d412a43SAl Viro struct vfsmount *
6629d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
6639d412a43SAl Viro {
6649d412a43SAl Viro 	struct vfsmount *mnt;
6659d412a43SAl Viro 	struct dentry *root;
6669d412a43SAl Viro 
6679d412a43SAl Viro 	if (!type)
6689d412a43SAl Viro 		return ERR_PTR(-ENODEV);
6699d412a43SAl Viro 
6709d412a43SAl Viro 	mnt = alloc_vfsmnt(name);
6719d412a43SAl Viro 	if (!mnt)
6729d412a43SAl Viro 		return ERR_PTR(-ENOMEM);
6739d412a43SAl Viro 
6749d412a43SAl Viro 	if (flags & MS_KERNMOUNT)
6759d412a43SAl Viro 		mnt->mnt_flags = MNT_INTERNAL;
6769d412a43SAl Viro 
6779d412a43SAl Viro 	root = mount_fs(type, flags, name, data);
6789d412a43SAl Viro 	if (IS_ERR(root)) {
6799d412a43SAl Viro 		free_vfsmnt(mnt);
6809d412a43SAl Viro 		return ERR_CAST(root);
6819d412a43SAl Viro 	}
6829d412a43SAl Viro 
6839d412a43SAl Viro 	mnt->mnt_root = root;
6849d412a43SAl Viro 	mnt->mnt_sb = root->d_sb;
6859d412a43SAl Viro 	mnt->mnt_mountpoint = mnt->mnt_root;
6869d412a43SAl Viro 	mnt->mnt_parent = mnt;
6879d412a43SAl Viro 	return mnt;
6889d412a43SAl Viro }
6899d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount);
6909d412a43SAl Viro 
69136341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
69236341f64SRam Pai 					int flag)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	struct super_block *sb = old->mnt_sb;
6951da177e4SLinus Torvalds 	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	if (mnt) {
698719f5d7fSMiklos Szeredi 		if (flag & (CL_SLAVE | CL_PRIVATE))
699719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = 0; /* not a peer of original */
700719f5d7fSMiklos Szeredi 		else
701719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = old->mnt_group_id;
702719f5d7fSMiklos Szeredi 
703719f5d7fSMiklos Szeredi 		if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
7044b8b21f4SAl Viro 			int err = mnt_alloc_group_id(real_mount(mnt));
705719f5d7fSMiklos Szeredi 			if (err)
706719f5d7fSMiklos Szeredi 				goto out_free;
707719f5d7fSMiklos Szeredi 		}
708719f5d7fSMiklos Szeredi 
709be1a16a0SMiklos Szeredi 		mnt->mnt_flags = old->mnt_flags & ~MNT_WRITE_HOLD;
7101da177e4SLinus Torvalds 		atomic_inc(&sb->s_active);
7111da177e4SLinus Torvalds 		mnt->mnt_sb = sb;
7121da177e4SLinus Torvalds 		mnt->mnt_root = dget(root);
7131da177e4SLinus Torvalds 		mnt->mnt_mountpoint = mnt->mnt_root;
7141da177e4SLinus Torvalds 		mnt->mnt_parent = mnt;
715b90fa9aeSRam Pai 
7165afe0022SRam Pai 		if (flag & CL_SLAVE) {
7175afe0022SRam Pai 			list_add(&mnt->mnt_slave, &old->mnt_slave_list);
7185afe0022SRam Pai 			mnt->mnt_master = old;
7195afe0022SRam Pai 			CLEAR_MNT_SHARED(mnt);
7208aec0809SAl Viro 		} else if (!(flag & CL_PRIVATE)) {
721796a6b52SAl Viro 			if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
722b90fa9aeSRam Pai 				list_add(&mnt->mnt_share, &old->mnt_share);
7235afe0022SRam Pai 			if (IS_MNT_SLAVE(old))
7245afe0022SRam Pai 				list_add(&mnt->mnt_slave, &old->mnt_slave);
7255afe0022SRam Pai 			mnt->mnt_master = old->mnt_master;
7265afe0022SRam Pai 		}
727b90fa9aeSRam Pai 		if (flag & CL_MAKE_SHARED)
728b90fa9aeSRam Pai 			set_mnt_shared(mnt);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 		/* stick the duplicate mount on the same expiry list
7311da177e4SLinus Torvalds 		 * as the original if that was on one */
73236341f64SRam Pai 		if (flag & CL_EXPIRE) {
73355e700b9SMiklos Szeredi 			if (!list_empty(&old->mnt_expire))
73455e700b9SMiklos Szeredi 				list_add(&mnt->mnt_expire, &old->mnt_expire);
7351da177e4SLinus Torvalds 		}
73636341f64SRam Pai 	}
7371da177e4SLinus Torvalds 	return mnt;
738719f5d7fSMiklos Szeredi 
739719f5d7fSMiklos Szeredi  out_free:
740719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
741719f5d7fSMiklos Szeredi 	return NULL;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744b3e19d92SNick Piggin static inline void mntfree(struct vfsmount *mnt)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
747b3e19d92SNick Piggin 
7483d733633SDave Hansen 	/*
7493d733633SDave Hansen 	 * This probably indicates that somebody messed
7503d733633SDave Hansen 	 * up a mnt_want/drop_write() pair.  If this
7513d733633SDave Hansen 	 * happens, the filesystem was probably unable
7523d733633SDave Hansen 	 * to make r/w->r/o transitions.
7533d733633SDave Hansen 	 */
754d3ef3d73Snpiggin@suse.de 	/*
755b3e19d92SNick Piggin 	 * The locking used to deal with mnt_count decrement provides barriers,
756b3e19d92SNick Piggin 	 * so mnt_get_writers() below is safe.
757d3ef3d73Snpiggin@suse.de 	 */
758c6653a83SNick Piggin 	WARN_ON(mnt_get_writers(mnt));
759ca9c726eSAndreas Gruenbacher 	fsnotify_vfsmount_delete(mnt);
7601da177e4SLinus Torvalds 	dput(mnt->mnt_root);
7611da177e4SLinus Torvalds 	free_vfsmnt(mnt);
7621da177e4SLinus Torvalds 	deactivate_super(sb);
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
765f03c6599SAl Viro static void mntput_no_expire(struct vfsmount *mnt)
7667b7b1aceSAl Viro {
767b3e19d92SNick Piggin put_again:
768f03c6599SAl Viro #ifdef CONFIG_SMP
769b3e19d92SNick Piggin 	br_read_lock(vfsmount_lock);
770f03c6599SAl Viro 	if (likely(atomic_read(&mnt->mnt_longterm))) {
771aa9c0e07SAl Viro 		mnt_add_count(mnt, -1);
772b3e19d92SNick Piggin 		br_read_unlock(vfsmount_lock);
77399b7db7bSNick Piggin 		return;
774b3e19d92SNick Piggin 	}
775b3e19d92SNick Piggin 	br_read_unlock(vfsmount_lock);
776b3e19d92SNick Piggin 
77799b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
778aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
779b3e19d92SNick Piggin 	if (mnt_get_count(mnt)) {
78099b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
78199b7db7bSNick Piggin 		return;
78299b7db7bSNick Piggin 	}
783b3e19d92SNick Piggin #else
784aa9c0e07SAl Viro 	mnt_add_count(mnt, -1);
785b3e19d92SNick Piggin 	if (likely(mnt_get_count(mnt)))
786b3e19d92SNick Piggin 		return;
787b3e19d92SNick Piggin 	br_write_lock(vfsmount_lock);
788f03c6599SAl Viro #endif
789b3e19d92SNick Piggin 	if (unlikely(mnt->mnt_pinned)) {
790b3e19d92SNick Piggin 		mnt_add_count(mnt, mnt->mnt_pinned + 1);
791b3e19d92SNick Piggin 		mnt->mnt_pinned = 0;
792b3e19d92SNick Piggin 		br_write_unlock(vfsmount_lock);
793b3e19d92SNick Piggin 		acct_auto_close_mnt(mnt);
794b3e19d92SNick Piggin 		goto put_again;
795b3e19d92SNick Piggin 	}
796b3e19d92SNick Piggin 	br_write_unlock(vfsmount_lock);
797b3e19d92SNick Piggin 	mntfree(mnt);
798b3e19d92SNick Piggin }
799b3e19d92SNick Piggin 
800b3e19d92SNick Piggin void mntput(struct vfsmount *mnt)
801b3e19d92SNick Piggin {
802b3e19d92SNick Piggin 	if (mnt) {
803b3e19d92SNick Piggin 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
804b3e19d92SNick Piggin 		if (unlikely(mnt->mnt_expiry_mark))
805b3e19d92SNick Piggin 			mnt->mnt_expiry_mark = 0;
806f03c6599SAl Viro 		mntput_no_expire(mnt);
807b3e19d92SNick Piggin 	}
808b3e19d92SNick Piggin }
809b3e19d92SNick Piggin EXPORT_SYMBOL(mntput);
810b3e19d92SNick Piggin 
811b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt)
812b3e19d92SNick Piggin {
813b3e19d92SNick Piggin 	if (mnt)
814aa9c0e07SAl Viro 		mnt_add_count(mnt, 1);
815b3e19d92SNick Piggin 	return mnt;
816b3e19d92SNick Piggin }
817b3e19d92SNick Piggin EXPORT_SYMBOL(mntget);
818b3e19d92SNick Piggin 
8197b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
8207b7b1aceSAl Viro {
82199b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
8227b7b1aceSAl Viro 	mnt->mnt_pinned++;
82399b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
8247b7b1aceSAl Viro }
8257b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
8267b7b1aceSAl Viro 
8277b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt)
8287b7b1aceSAl Viro {
82999b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
8307b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
831aa9c0e07SAl Viro 		mnt_add_count(mnt, 1);
8327b7b1aceSAl Viro 		mnt->mnt_pinned--;
8337b7b1aceSAl Viro 	}
83499b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
8357b7b1aceSAl Viro }
8367b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
8371da177e4SLinus Torvalds 
838b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
839b3b304a2SMiklos Szeredi {
840b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
841b3b304a2SMiklos Szeredi }
842b3b304a2SMiklos Szeredi 
843b3b304a2SMiklos Szeredi /*
844b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
845b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
846b3b304a2SMiklos Szeredi  *
847b3b304a2SMiklos Szeredi  * See also save_mount_options().
848b3b304a2SMiklos Szeredi  */
849b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
850b3b304a2SMiklos Szeredi {
8512a32cebdSAl Viro 	const char *options;
8522a32cebdSAl Viro 
8532a32cebdSAl Viro 	rcu_read_lock();
8542a32cebdSAl Viro 	options = rcu_dereference(mnt->mnt_sb->s_options);
855b3b304a2SMiklos Szeredi 
856b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
857b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
858b3b304a2SMiklos Szeredi 		mangle(m, options);
859b3b304a2SMiklos Szeredi 	}
8602a32cebdSAl Viro 	rcu_read_unlock();
861b3b304a2SMiklos Szeredi 
862b3b304a2SMiklos Szeredi 	return 0;
863b3b304a2SMiklos Szeredi }
864b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
865b3b304a2SMiklos Szeredi 
866b3b304a2SMiklos Szeredi /*
867b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
868b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
869b3b304a2SMiklos Szeredi  *
870b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
871b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
872b3b304a2SMiklos Szeredi  * remount fails.
873b3b304a2SMiklos Szeredi  *
874b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
875b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
876b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
877b3b304a2SMiklos Szeredi  * any more.
878b3b304a2SMiklos Szeredi  */
879b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
880b3b304a2SMiklos Szeredi {
8812a32cebdSAl Viro 	BUG_ON(sb->s_options);
8822a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
883b3b304a2SMiklos Szeredi }
884b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
885b3b304a2SMiklos Szeredi 
8862a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
8872a32cebdSAl Viro {
8882a32cebdSAl Viro 	char *old = sb->s_options;
8892a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
8902a32cebdSAl Viro 	if (old) {
8912a32cebdSAl Viro 		synchronize_rcu();
8922a32cebdSAl Viro 		kfree(old);
8932a32cebdSAl Viro 	}
8942a32cebdSAl Viro }
8952a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
8962a32cebdSAl Viro 
897a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
8981da177e4SLinus Torvalds /* iterator */
8991da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
9001da177e4SLinus Torvalds {
901a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
9021da177e4SLinus Torvalds 
903390c6843SRam Pai 	down_read(&namespace_sem);
904a1a2c409SMiklos Szeredi 	return seq_list_start(&p->ns->list, *pos);
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
9081da177e4SLinus Torvalds {
909a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
910b0765fb8SPavel Emelianov 
911a1a2c409SMiklos Szeredi 	return seq_list_next(v, &p->ns->list, pos);
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
9151da177e4SLinus Torvalds {
916390c6843SRam Pai 	up_read(&namespace_sem);
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
9199f5596afSAl Viro int mnt_had_events(struct proc_mounts *p)
9209f5596afSAl Viro {
9219f5596afSAl Viro 	struct mnt_namespace *ns = p->ns;
9229f5596afSAl Viro 	int res = 0;
9239f5596afSAl Viro 
92499b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
925f1514638SKay Sievers 	if (p->m.poll_event != ns->event) {
926f1514638SKay Sievers 		p->m.poll_event = ns->event;
9279f5596afSAl Viro 		res = 1;
9289f5596afSAl Viro 	}
92999b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
9309f5596afSAl Viro 
9319f5596afSAl Viro 	return res;
9329f5596afSAl Viro }
9339f5596afSAl Viro 
9342d4d4864SRam Pai struct proc_fs_info {
9351da177e4SLinus Torvalds 	int flag;
9362d4d4864SRam Pai 	const char *str;
9372d4d4864SRam Pai };
9382d4d4864SRam Pai 
9392069f457SEric Paris static int show_sb_opts(struct seq_file *m, struct super_block *sb)
9402d4d4864SRam Pai {
9412d4d4864SRam Pai 	static const struct proc_fs_info fs_info[] = {
9421da177e4SLinus Torvalds 		{ MS_SYNCHRONOUS, ",sync" },
9431da177e4SLinus Torvalds 		{ MS_DIRSYNC, ",dirsync" },
9441da177e4SLinus Torvalds 		{ MS_MANDLOCK, ",mand" },
9451da177e4SLinus Torvalds 		{ 0, NULL }
9461da177e4SLinus Torvalds 	};
9472d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
9482d4d4864SRam Pai 
9492d4d4864SRam Pai 	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
9502d4d4864SRam Pai 		if (sb->s_flags & fs_infop->flag)
9512d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
9522d4d4864SRam Pai 	}
9532069f457SEric Paris 
9542069f457SEric Paris 	return security_sb_show_options(m, sb);
9552d4d4864SRam Pai }
9562d4d4864SRam Pai 
9572d4d4864SRam Pai static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
9582d4d4864SRam Pai {
9592d4d4864SRam Pai 	static const struct proc_fs_info mnt_info[] = {
9601da177e4SLinus Torvalds 		{ MNT_NOSUID, ",nosuid" },
9611da177e4SLinus Torvalds 		{ MNT_NODEV, ",nodev" },
9621da177e4SLinus Torvalds 		{ MNT_NOEXEC, ",noexec" },
963fc33a7bbSChristoph Hellwig 		{ MNT_NOATIME, ",noatime" },
964fc33a7bbSChristoph Hellwig 		{ MNT_NODIRATIME, ",nodiratime" },
96547ae32d6SValerie Henson 		{ MNT_RELATIME, ",relatime" },
9661da177e4SLinus Torvalds 		{ 0, NULL }
9671da177e4SLinus Torvalds 	};
9682d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
9692d4d4864SRam Pai 
9702d4d4864SRam Pai 	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
9712d4d4864SRam Pai 		if (mnt->mnt_flags & fs_infop->flag)
9722d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
9732d4d4864SRam Pai 	}
9742d4d4864SRam Pai }
9752d4d4864SRam Pai 
9762d4d4864SRam Pai static void show_type(struct seq_file *m, struct super_block *sb)
9772d4d4864SRam Pai {
9782d4d4864SRam Pai 	mangle(m, sb->s_type->name);
9792d4d4864SRam Pai 	if (sb->s_subtype && sb->s_subtype[0]) {
9802d4d4864SRam Pai 		seq_putc(m, '.');
9812d4d4864SRam Pai 		mangle(m, sb->s_subtype);
9822d4d4864SRam Pai 	}
9832d4d4864SRam Pai }
9842d4d4864SRam Pai 
9852d4d4864SRam Pai static int show_vfsmnt(struct seq_file *m, void *v)
9862d4d4864SRam Pai {
9872d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
9882d4d4864SRam Pai 	int err = 0;
989c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
9901da177e4SLinus Torvalds 
991c7f404b4SAl Viro 	if (mnt->mnt_sb->s_op->show_devname) {
992c7f404b4SAl Viro 		err = mnt->mnt_sb->s_op->show_devname(m, mnt);
993c7f404b4SAl Viro 		if (err)
994c7f404b4SAl Viro 			goto out;
995c7f404b4SAl Viro 	} else {
9961da177e4SLinus Torvalds 		mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
997c7f404b4SAl Viro 	}
9981da177e4SLinus Torvalds 	seq_putc(m, ' ');
999c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
10001da177e4SLinus Torvalds 	seq_putc(m, ' ');
10012d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
10022e4b7fcdSDave Hansen 	seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
10032069f457SEric Paris 	err = show_sb_opts(m, mnt->mnt_sb);
10042069f457SEric Paris 	if (err)
10052069f457SEric Paris 		goto out;
10062d4d4864SRam Pai 	show_mnt_opts(m, mnt);
10071da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_op->show_options)
10081da177e4SLinus Torvalds 		err = mnt->mnt_sb->s_op->show_options(m, mnt);
10091da177e4SLinus Torvalds 	seq_puts(m, " 0 0\n");
10102069f457SEric Paris out:
10111da177e4SLinus Torvalds 	return err;
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
1014a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
10151da177e4SLinus Torvalds 	.start	= m_start,
10161da177e4SLinus Torvalds 	.next	= m_next,
10171da177e4SLinus Torvalds 	.stop	= m_stop,
10181da177e4SLinus Torvalds 	.show	= show_vfsmnt
10191da177e4SLinus Torvalds };
10201da177e4SLinus Torvalds 
10212d4d4864SRam Pai static int show_mountinfo(struct seq_file *m, void *v)
10222d4d4864SRam Pai {
10232d4d4864SRam Pai 	struct proc_mounts *p = m->private;
10242d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
10252d4d4864SRam Pai 	struct super_block *sb = mnt->mnt_sb;
10262d4d4864SRam Pai 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
10272d4d4864SRam Pai 	struct path root = p->root;
10282d4d4864SRam Pai 	int err = 0;
10292d4d4864SRam Pai 
10302d4d4864SRam Pai 	seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
10312d4d4864SRam Pai 		   MAJOR(sb->s_dev), MINOR(sb->s_dev));
1032c7f404b4SAl Viro 	if (sb->s_op->show_path)
1033c7f404b4SAl Viro 		err = sb->s_op->show_path(m, mnt);
1034c7f404b4SAl Viro 	else
10352d4d4864SRam Pai 		seq_dentry(m, mnt->mnt_root, " \t\n\\");
1036c7f404b4SAl Viro 	if (err)
1037c7f404b4SAl Viro 		goto out;
10382d4d4864SRam Pai 	seq_putc(m, ' ');
103902125a82SAl Viro 
104002125a82SAl Viro 	/* mountpoints outside of chroot jail will give SEQ_SKIP on this */
104102125a82SAl Viro 	err = seq_path_root(m, &mnt_path, &root, " \t\n\\");
104202125a82SAl Viro 	if (err)
104302125a82SAl Viro 		goto out;
104402125a82SAl Viro 
10452d4d4864SRam Pai 	seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
10462d4d4864SRam Pai 	show_mnt_opts(m, mnt);
10472d4d4864SRam Pai 
10482d4d4864SRam Pai 	/* Tagged fields ("foo:X" or "bar") */
10492d4d4864SRam Pai 	if (IS_MNT_SHARED(mnt))
10502d4d4864SRam Pai 		seq_printf(m, " shared:%i", mnt->mnt_group_id);
105197e7e0f7SMiklos Szeredi 	if (IS_MNT_SLAVE(mnt)) {
105297e7e0f7SMiklos Szeredi 		int master = mnt->mnt_master->mnt_group_id;
105397e7e0f7SMiklos Szeredi 		int dom = get_dominating_id(mnt, &p->root);
105497e7e0f7SMiklos Szeredi 		seq_printf(m, " master:%i", master);
105597e7e0f7SMiklos Szeredi 		if (dom && dom != master)
105697e7e0f7SMiklos Szeredi 			seq_printf(m, " propagate_from:%i", dom);
105797e7e0f7SMiklos Szeredi 	}
10582d4d4864SRam Pai 	if (IS_MNT_UNBINDABLE(mnt))
10592d4d4864SRam Pai 		seq_puts(m, " unbindable");
10602d4d4864SRam Pai 
10612d4d4864SRam Pai 	/* Filesystem specific data */
10622d4d4864SRam Pai 	seq_puts(m, " - ");
10632d4d4864SRam Pai 	show_type(m, sb);
10642d4d4864SRam Pai 	seq_putc(m, ' ');
1065c7f404b4SAl Viro 	if (sb->s_op->show_devname)
1066c7f404b4SAl Viro 		err = sb->s_op->show_devname(m, mnt);
1067c7f404b4SAl Viro 	else
10682d4d4864SRam Pai 		mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
1069c7f404b4SAl Viro 	if (err)
1070c7f404b4SAl Viro 		goto out;
10712d4d4864SRam Pai 	seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
10722069f457SEric Paris 	err = show_sb_opts(m, sb);
10732069f457SEric Paris 	if (err)
10742069f457SEric Paris 		goto out;
10752d4d4864SRam Pai 	if (sb->s_op->show_options)
10762d4d4864SRam Pai 		err = sb->s_op->show_options(m, mnt);
10772d4d4864SRam Pai 	seq_putc(m, '\n');
10782069f457SEric Paris out:
10792d4d4864SRam Pai 	return err;
10802d4d4864SRam Pai }
10812d4d4864SRam Pai 
10822d4d4864SRam Pai const struct seq_operations mountinfo_op = {
10832d4d4864SRam Pai 	.start	= m_start,
10842d4d4864SRam Pai 	.next	= m_next,
10852d4d4864SRam Pai 	.stop	= m_stop,
10862d4d4864SRam Pai 	.show	= show_mountinfo,
10872d4d4864SRam Pai };
10882d4d4864SRam Pai 
1089b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v)
1090b4629fe2SChuck Lever {
1091b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
1092c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
1093b4629fe2SChuck Lever 	int err = 0;
1094b4629fe2SChuck Lever 
1095b4629fe2SChuck Lever 	/* device */
1096c7f404b4SAl Viro 	if (mnt->mnt_sb->s_op->show_devname) {
1097a877ee03SBryan Schumaker 		seq_puts(m, "device ");
1098c7f404b4SAl Viro 		err = mnt->mnt_sb->s_op->show_devname(m, mnt);
1099c7f404b4SAl Viro 	} else {
1100b4629fe2SChuck Lever 		if (mnt->mnt_devname) {
1101b4629fe2SChuck Lever 			seq_puts(m, "device ");
1102b4629fe2SChuck Lever 			mangle(m, mnt->mnt_devname);
1103b4629fe2SChuck Lever 		} else
1104b4629fe2SChuck Lever 			seq_puts(m, "no device");
1105c7f404b4SAl Viro 	}
1106b4629fe2SChuck Lever 
1107b4629fe2SChuck Lever 	/* mount point */
1108b4629fe2SChuck Lever 	seq_puts(m, " mounted on ");
1109c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
1110b4629fe2SChuck Lever 	seq_putc(m, ' ');
1111b4629fe2SChuck Lever 
1112b4629fe2SChuck Lever 	/* file system type */
1113b4629fe2SChuck Lever 	seq_puts(m, "with fstype ");
11142d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
1115b4629fe2SChuck Lever 
1116b4629fe2SChuck Lever 	/* optional statistics */
1117b4629fe2SChuck Lever 	if (mnt->mnt_sb->s_op->show_stats) {
1118b4629fe2SChuck Lever 		seq_putc(m, ' ');
1119c7f404b4SAl Viro 		if (!err)
1120b4629fe2SChuck Lever 			err = mnt->mnt_sb->s_op->show_stats(m, mnt);
1121b4629fe2SChuck Lever 	}
1122b4629fe2SChuck Lever 
1123b4629fe2SChuck Lever 	seq_putc(m, '\n');
1124b4629fe2SChuck Lever 	return err;
1125b4629fe2SChuck Lever }
1126b4629fe2SChuck Lever 
1127a1a2c409SMiklos Szeredi const struct seq_operations mountstats_op = {
1128b4629fe2SChuck Lever 	.start	= m_start,
1129b4629fe2SChuck Lever 	.next	= m_next,
1130b4629fe2SChuck Lever 	.stop	= m_stop,
1131b4629fe2SChuck Lever 	.show	= show_vfsstat,
1132b4629fe2SChuck Lever };
1133a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
1134b4629fe2SChuck Lever 
11351da177e4SLinus Torvalds /**
11361da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
11371da177e4SLinus Torvalds  * @mnt: root of mount tree
11381da177e4SLinus Torvalds  *
11391da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
11401da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
11411da177e4SLinus Torvalds  * busy.
11421da177e4SLinus Torvalds  */
11431da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt)
11441da177e4SLinus Torvalds {
114536341f64SRam Pai 	int actual_refs = 0;
114636341f64SRam Pai 	int minimum_refs = 0;
1147315fc83eSAl Viro 	struct mount *p;
1148315fc83eSAl Viro 	BUG_ON(!mnt);
11491da177e4SLinus Torvalds 
1150b3e19d92SNick Piggin 	/* write lock needed for mnt_get_count */
1151b3e19d92SNick Piggin 	br_write_lock(vfsmount_lock);
1152315fc83eSAl Viro 	for (p = real_mount(mnt); p; p = next_mnt(p, mnt)) {
1153315fc83eSAl Viro 		actual_refs += mnt_get_count(&p->mnt);
11541da177e4SLinus Torvalds 		minimum_refs += 2;
11551da177e4SLinus Torvalds 	}
1156b3e19d92SNick Piggin 	br_write_unlock(vfsmount_lock);
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
11591da177e4SLinus Torvalds 		return 0;
1160e3474a8eSIan Kent 
1161e3474a8eSIan Kent 	return 1;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds /**
11671da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
11681da177e4SLinus Torvalds  * @mnt: root of mount
11691da177e4SLinus Torvalds  *
11701da177e4SLinus Torvalds  * This is called to check if a mount point has any
11711da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
11721da177e4SLinus Torvalds  * mount has sub mounts this will return busy
11731da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
11741da177e4SLinus Torvalds  *
11751da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
11761da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
11771da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
11781da177e4SLinus Torvalds  */
11791da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
11801da177e4SLinus Torvalds {
1181e3474a8eSIan Kent 	int ret = 1;
11828ad08d8aSAl Viro 	down_read(&namespace_sem);
1183b3e19d92SNick Piggin 	br_write_lock(vfsmount_lock);
1184a05964f3SRam Pai 	if (propagate_mount_busy(mnt, 2))
1185e3474a8eSIan Kent 		ret = 0;
1186b3e19d92SNick Piggin 	br_write_unlock(vfsmount_lock);
11878ad08d8aSAl Viro 	up_read(&namespace_sem);
1188a05964f3SRam Pai 	return ret;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
11921da177e4SLinus Torvalds 
1193b90fa9aeSRam Pai void release_mounts(struct list_head *head)
11941da177e4SLinus Torvalds {
119570fbcdf4SRam Pai 	struct vfsmount *mnt;
119670fbcdf4SRam Pai 	while (!list_empty(head)) {
1197b5e61818SPavel Emelianov 		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
119870fbcdf4SRam Pai 		list_del_init(&mnt->mnt_hash);
1199b2dba1afSAl Viro 		if (mnt_has_parent(mnt)) {
120070fbcdf4SRam Pai 			struct dentry *dentry;
120170fbcdf4SRam Pai 			struct vfsmount *m;
120299b7db7bSNick Piggin 
120399b7db7bSNick Piggin 			br_write_lock(vfsmount_lock);
120470fbcdf4SRam Pai 			dentry = mnt->mnt_mountpoint;
120570fbcdf4SRam Pai 			m = mnt->mnt_parent;
120670fbcdf4SRam Pai 			mnt->mnt_mountpoint = mnt->mnt_root;
120770fbcdf4SRam Pai 			mnt->mnt_parent = mnt;
12087c4b93d8SAl Viro 			m->mnt_ghosts--;
120999b7db7bSNick Piggin 			br_write_unlock(vfsmount_lock);
121070fbcdf4SRam Pai 			dput(dentry);
121170fbcdf4SRam Pai 			mntput(m);
12121da177e4SLinus Torvalds 		}
1213f03c6599SAl Viro 		mntput(mnt);
121470fbcdf4SRam Pai 	}
121570fbcdf4SRam Pai }
121670fbcdf4SRam Pai 
121799b7db7bSNick Piggin /*
121899b7db7bSNick Piggin  * vfsmount lock must be held for write
121999b7db7bSNick Piggin  * namespace_sem must be held for write
122099b7db7bSNick Piggin  */
1221a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
122270fbcdf4SRam Pai {
12237b8a53fdSAl Viro 	LIST_HEAD(tmp_list);
1224315fc83eSAl Viro 	struct mount *p;
122570fbcdf4SRam Pai 
1226315fc83eSAl Viro 	for (p = real_mount(mnt); p; p = next_mnt(p, mnt))
1227315fc83eSAl Viro 		list_move(&p->mnt.mnt_hash, &tmp_list);
122870fbcdf4SRam Pai 
1229a05964f3SRam Pai 	if (propagate)
12307b8a53fdSAl Viro 		propagate_umount(&tmp_list);
1231a05964f3SRam Pai 
1232315fc83eSAl Viro 	list_for_each_entry(p, &tmp_list, mnt.mnt_hash) {
1233315fc83eSAl Viro 		list_del_init(&p->mnt.mnt_expire);
1234315fc83eSAl Viro 		list_del_init(&p->mnt.mnt_list);
1235315fc83eSAl Viro 		__touch_mnt_namespace(p->mnt.mnt_ns);
1236315fc83eSAl Viro 		p->mnt.mnt_ns = NULL;
1237315fc83eSAl Viro 		__mnt_make_shortterm(&p->mnt);
1238315fc83eSAl Viro 		list_del_init(&p->mnt.mnt_child);
1239315fc83eSAl Viro 		if (mnt_has_parent(&p->mnt)) {
1240315fc83eSAl Viro 			p->mnt.mnt_parent->mnt_ghosts++;
1241315fc83eSAl Viro 			dentry_reset_mounted(p->mnt.mnt_mountpoint);
12427c4b93d8SAl Viro 		}
1243315fc83eSAl Viro 		change_mnt_propagation(&p->mnt, MS_PRIVATE);
12441da177e4SLinus Torvalds 	}
12457b8a53fdSAl Viro 	list_splice(&tmp_list, kill);
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
1248c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
1249c35038beSAl Viro 
12501da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags)
12511da177e4SLinus Torvalds {
12521da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
12531da177e4SLinus Torvalds 	int retval;
125470fbcdf4SRam Pai 	LIST_HEAD(umount_list);
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	retval = security_sb_umount(mnt, flags);
12571da177e4SLinus Torvalds 	if (retval)
12581da177e4SLinus Torvalds 		return retval;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	/*
12611da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
12621da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
12631da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
12641da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
12651da177e4SLinus Torvalds 	 */
12661da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
12676ac08c39SJan Blunck 		if (mnt == current->fs->root.mnt ||
12681da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
12691da177e4SLinus Torvalds 			return -EINVAL;
12701da177e4SLinus Torvalds 
1271b3e19d92SNick Piggin 		/*
1272b3e19d92SNick Piggin 		 * probably don't strictly need the lock here if we examined
1273b3e19d92SNick Piggin 		 * all race cases, but it's a slowpath.
1274b3e19d92SNick Piggin 		 */
1275b3e19d92SNick Piggin 		br_write_lock(vfsmount_lock);
1276b3e19d92SNick Piggin 		if (mnt_get_count(mnt) != 2) {
1277bf9faa2aSJ. R. Okajima 			br_write_unlock(vfsmount_lock);
12781da177e4SLinus Torvalds 			return -EBUSY;
1279b3e19d92SNick Piggin 		}
1280b3e19d92SNick Piggin 		br_write_unlock(vfsmount_lock);
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1))
12831da177e4SLinus Torvalds 			return -EAGAIN;
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	/*
12871da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
12881da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
12891da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
12901da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
12911da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
12921da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
12931da177e4SLinus Torvalds 	 * about for the moment.
12941da177e4SLinus Torvalds 	 */
12951da177e4SLinus Torvalds 
129642faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
129742faad99SAl Viro 		sb->s_op->umount_begin(sb);
129842faad99SAl Viro 	}
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	/*
13011da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
13021da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
13031da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
13041da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
13051da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
13061da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
13071da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
13081da177e4SLinus Torvalds 	 */
13096ac08c39SJan Blunck 	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
13101da177e4SLinus Torvalds 		/*
13111da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
13121da177e4SLinus Torvalds 		 * we just try to remount it readonly.
13131da177e4SLinus Torvalds 		 */
13141da177e4SLinus Torvalds 		down_write(&sb->s_umount);
13154aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
13161da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
13171da177e4SLinus Torvalds 		up_write(&sb->s_umount);
13181da177e4SLinus Torvalds 		return retval;
13191da177e4SLinus Torvalds 	}
13201da177e4SLinus Torvalds 
1321390c6843SRam Pai 	down_write(&namespace_sem);
132299b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
13235addc5ddSAl Viro 	event++;
13241da177e4SLinus Torvalds 
1325c35038beSAl Viro 	if (!(flags & MNT_DETACH))
1326c35038beSAl Viro 		shrink_submounts(mnt, &umount_list);
1327c35038beSAl Viro 
13281da177e4SLinus Torvalds 	retval = -EBUSY;
1329a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
13301da177e4SLinus Torvalds 		if (!list_empty(&mnt->mnt_list))
1331a05964f3SRam Pai 			umount_tree(mnt, 1, &umount_list);
13321da177e4SLinus Torvalds 		retval = 0;
13331da177e4SLinus Torvalds 	}
133499b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
1335390c6843SRam Pai 	up_write(&namespace_sem);
133670fbcdf4SRam Pai 	release_mounts(&umount_list);
13371da177e4SLinus Torvalds 	return retval;
13381da177e4SLinus Torvalds }
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds /*
13411da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
13421da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
13431da177e4SLinus Torvalds  *
13441da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
13451da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
13461da177e4SLinus Torvalds  */
13471da177e4SLinus Torvalds 
1348bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
13491da177e4SLinus Torvalds {
13502d8f3038SAl Viro 	struct path path;
13511da177e4SLinus Torvalds 	int retval;
1352db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
13531da177e4SLinus Torvalds 
1354db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1355db1f05bbSMiklos Szeredi 		return -EINVAL;
1356db1f05bbSMiklos Szeredi 
1357db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1358db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1359db1f05bbSMiklos Szeredi 
1360db1f05bbSMiklos Szeredi 	retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
13611da177e4SLinus Torvalds 	if (retval)
13621da177e4SLinus Torvalds 		goto out;
13631da177e4SLinus Torvalds 	retval = -EINVAL;
13642d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
13651da177e4SLinus Torvalds 		goto dput_and_out;
13662d8f3038SAl Viro 	if (!check_mnt(path.mnt))
13671da177e4SLinus Torvalds 		goto dput_and_out;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	retval = -EPERM;
13701da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
13711da177e4SLinus Torvalds 		goto dput_and_out;
13721da177e4SLinus Torvalds 
13732d8f3038SAl Viro 	retval = do_umount(path.mnt, flags);
13741da177e4SLinus Torvalds dput_and_out:
1375429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
13762d8f3038SAl Viro 	dput(path.dentry);
13772d8f3038SAl Viro 	mntput_no_expire(path.mnt);
13781da177e4SLinus Torvalds out:
13791da177e4SLinus Torvalds 	return retval;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds /*
13851da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
13861da177e4SLinus Torvalds  */
1387bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
13881da177e4SLinus Torvalds {
13891da177e4SLinus Torvalds 	return sys_umount(name, 0);
13901da177e4SLinus Torvalds }
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds #endif
13931da177e4SLinus Torvalds 
13942d92ab3cSAl Viro static int mount_is_safe(struct path *path)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	if (capable(CAP_SYS_ADMIN))
13971da177e4SLinus Torvalds 		return 0;
13981da177e4SLinus Torvalds 	return -EPERM;
13991da177e4SLinus Torvalds #ifdef notyet
14002d92ab3cSAl Viro 	if (S_ISLNK(path->dentry->d_inode->i_mode))
14011da177e4SLinus Torvalds 		return -EPERM;
14022d92ab3cSAl Viro 	if (path->dentry->d_inode->i_mode & S_ISVTX) {
1403da9592edSDavid Howells 		if (current_uid() != path->dentry->d_inode->i_uid)
14041da177e4SLinus Torvalds 			return -EPERM;
14051da177e4SLinus Torvalds 	}
14062d92ab3cSAl Viro 	if (inode_permission(path->dentry->d_inode, MAY_WRITE))
14071da177e4SLinus Torvalds 		return -EPERM;
14081da177e4SLinus Torvalds 	return 0;
14091da177e4SLinus Torvalds #endif
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds 
1412b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
141336341f64SRam Pai 					int flag)
14141da177e4SLinus Torvalds {
1415315fc83eSAl Viro 	struct vfsmount *res, *p, *q, *r;
14161a390689SAl Viro 	struct path path;
14171da177e4SLinus Torvalds 
14189676f0c6SRam Pai 	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
14199676f0c6SRam Pai 		return NULL;
14209676f0c6SRam Pai 
142136341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
14221da177e4SLinus Torvalds 	if (!q)
14231da177e4SLinus Torvalds 		goto Enomem;
14241da177e4SLinus Torvalds 	q->mnt_mountpoint = mnt->mnt_mountpoint;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	p = mnt;
1427fdadd65fSDomen Puncer 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1428315fc83eSAl Viro 		struct mount *s;
14297ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
14301da177e4SLinus Torvalds 			continue;
14311da177e4SLinus Torvalds 
1432315fc83eSAl Viro 		for (s = real_mount(r); s; s = next_mnt(s, r)) {
1433315fc83eSAl Viro 			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(&s->mnt)) {
14349676f0c6SRam Pai 				s = skip_mnt_tree(s);
14359676f0c6SRam Pai 				continue;
14369676f0c6SRam Pai 			}
1437315fc83eSAl Viro 			while (p != s->mnt.mnt_parent) {
14381da177e4SLinus Torvalds 				p = p->mnt_parent;
14391da177e4SLinus Torvalds 				q = q->mnt_parent;
14401da177e4SLinus Torvalds 			}
1441315fc83eSAl Viro 			p = &s->mnt;
14421a390689SAl Viro 			path.mnt = q;
14431a390689SAl Viro 			path.dentry = p->mnt_mountpoint;
144436341f64SRam Pai 			q = clone_mnt(p, p->mnt_root, flag);
14451da177e4SLinus Torvalds 			if (!q)
14461da177e4SLinus Torvalds 				goto Enomem;
144799b7db7bSNick Piggin 			br_write_lock(vfsmount_lock);
14481da177e4SLinus Torvalds 			list_add_tail(&q->mnt_list, &res->mnt_list);
1449419148daSAl Viro 			attach_mnt(real_mount(q), &path);
145099b7db7bSNick Piggin 			br_write_unlock(vfsmount_lock);
14511da177e4SLinus Torvalds 		}
14521da177e4SLinus Torvalds 	}
14531da177e4SLinus Torvalds 	return res;
14541da177e4SLinus Torvalds Enomem:
14551da177e4SLinus Torvalds 	if (res) {
145670fbcdf4SRam Pai 		LIST_HEAD(umount_list);
145799b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1458a05964f3SRam Pai 		umount_tree(res, 0, &umount_list);
145999b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
146070fbcdf4SRam Pai 		release_mounts(&umount_list);
14611da177e4SLinus Torvalds 	}
14621da177e4SLinus Torvalds 	return NULL;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
1465589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
14668aec0809SAl Viro {
14678aec0809SAl Viro 	struct vfsmount *tree;
14681a60a280SAl Viro 	down_write(&namespace_sem);
1469589ff870SAl Viro 	tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
14701a60a280SAl Viro 	up_write(&namespace_sem);
14718aec0809SAl Viro 	return tree;
14728aec0809SAl Viro }
14738aec0809SAl Viro 
14748aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
14758aec0809SAl Viro {
14768aec0809SAl Viro 	LIST_HEAD(umount_list);
14771a60a280SAl Viro 	down_write(&namespace_sem);
147899b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
14798aec0809SAl Viro 	umount_tree(mnt, 0, &umount_list);
148099b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
14811a60a280SAl Viro 	up_write(&namespace_sem);
14828aec0809SAl Viro 	release_mounts(&umount_list);
14838aec0809SAl Viro }
14848aec0809SAl Viro 
14851f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
14861f707137SAl Viro 		   struct vfsmount *root)
14871f707137SAl Viro {
14881f707137SAl Viro 	struct vfsmount *mnt;
14891f707137SAl Viro 	int res = f(root, arg);
14901f707137SAl Viro 	if (res)
14911f707137SAl Viro 		return res;
14921f707137SAl Viro 	list_for_each_entry(mnt, &root->mnt_list, mnt_list) {
14931f707137SAl Viro 		res = f(mnt, arg);
14941f707137SAl Viro 		if (res)
14951f707137SAl Viro 			return res;
14961f707137SAl Viro 	}
14971f707137SAl Viro 	return 0;
14981f707137SAl Viro }
14991f707137SAl Viro 
15004b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1501719f5d7fSMiklos Szeredi {
1502315fc83eSAl Viro 	struct mount *p;
1503719f5d7fSMiklos Szeredi 
15044b8b21f4SAl Viro 	for (p = mnt; p != end; p = next_mnt(p, &mnt->mnt)) {
1505315fc83eSAl Viro 		if (p->mnt.mnt_group_id && !IS_MNT_SHARED(&p->mnt))
15064b8b21f4SAl Viro 			mnt_release_group_id(p);
1507719f5d7fSMiklos Szeredi 	}
1508719f5d7fSMiklos Szeredi }
1509719f5d7fSMiklos Szeredi 
15104b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse)
1511719f5d7fSMiklos Szeredi {
1512315fc83eSAl Viro 	struct mount *p;
1513719f5d7fSMiklos Szeredi 
15144b8b21f4SAl Viro 	for (p = mnt; p; p = recurse ? next_mnt(p, &mnt->mnt) : NULL) {
1515315fc83eSAl Viro 		if (!p->mnt.mnt_group_id && !IS_MNT_SHARED(&p->mnt)) {
15164b8b21f4SAl Viro 			int err = mnt_alloc_group_id(p);
1517719f5d7fSMiklos Szeredi 			if (err) {
15184b8b21f4SAl Viro 				cleanup_group_ids(mnt, p);
1519719f5d7fSMiklos Szeredi 				return err;
1520719f5d7fSMiklos Szeredi 			}
1521719f5d7fSMiklos Szeredi 		}
1522719f5d7fSMiklos Szeredi 	}
1523719f5d7fSMiklos Szeredi 
1524719f5d7fSMiklos Szeredi 	return 0;
1525719f5d7fSMiklos Szeredi }
1526719f5d7fSMiklos Szeredi 
1527b90fa9aeSRam Pai /*
1528b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1529b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
153021444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
153121444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
153221444403SRam Pai  *  		   (done when source_mnt is moved)
1533b90fa9aeSRam Pai  *
1534b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1535b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
15369676f0c6SRam Pai  * ---------------------------------------------------------------------------
1537b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
15389676f0c6SRam Pai  * |**************************************************************************
15399676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
15409676f0c6SRam Pai  * | dest     |               |                |                |            |
15419676f0c6SRam Pai  * |   |      |               |                |                |            |
15429676f0c6SRam Pai  * |   v      |               |                |                |            |
15439676f0c6SRam Pai  * |**************************************************************************
15449676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
15455afe0022SRam Pai  * |          |               |                |                |            |
15469676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
15479676f0c6SRam Pai  * ***************************************************************************
1548b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1549b90fa9aeSRam Pai  * destination mount.
1550b90fa9aeSRam Pai  *
1551b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1552b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1553b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1554b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1555b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1556b90fa9aeSRam Pai  *       mount.
15575afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
15585afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
15595afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
15605afe0022SRam Pai  *       is marked as 'shared and slave'.
15615afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
15625afe0022SRam Pai  * 	 source mount.
15635afe0022SRam Pai  *
15649676f0c6SRam Pai  * ---------------------------------------------------------------------------
156521444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
15669676f0c6SRam Pai  * |**************************************************************************
15679676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
15689676f0c6SRam Pai  * | dest     |               |                |                |            |
15699676f0c6SRam Pai  * |   |      |               |                |                |            |
15709676f0c6SRam Pai  * |   v      |               |                |                |            |
15719676f0c6SRam Pai  * |**************************************************************************
15729676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
15735afe0022SRam Pai  * |          |               |                |                |            |
15749676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
15759676f0c6SRam Pai  * ***************************************************************************
15765afe0022SRam Pai  *
15775afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
15785afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
157921444403SRam Pai  * (+*)  the mount is moved to the destination.
15805afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
15815afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
15825afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
15835afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1584b90fa9aeSRam Pai  *
1585b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1586b90fa9aeSRam Pai  * applied to each mount in the tree.
1587b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1588b90fa9aeSRam Pai  * in allocations.
1589b90fa9aeSRam Pai  */
1590b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt,
15911a390689SAl Viro 			struct path *path, struct path *parent_path)
1592b90fa9aeSRam Pai {
1593b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
15941a390689SAl Viro 	struct vfsmount *dest_mnt = path->mnt;
15951a390689SAl Viro 	struct dentry *dest_dentry = path->dentry;
1596315fc83eSAl Viro 	struct mount *child, *p;
1597719f5d7fSMiklos Szeredi 	int err;
1598b90fa9aeSRam Pai 
1599719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt)) {
16004b8b21f4SAl Viro 		err = invent_group_ids(real_mount(source_mnt), true);
1601719f5d7fSMiklos Szeredi 		if (err)
1602719f5d7fSMiklos Szeredi 			goto out;
1603719f5d7fSMiklos Szeredi 	}
1604719f5d7fSMiklos Szeredi 	err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1605719f5d7fSMiklos Szeredi 	if (err)
1606719f5d7fSMiklos Szeredi 		goto out_cleanup_ids;
1607b90fa9aeSRam Pai 
160899b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
1609df1a1ad2SAl Viro 
1610b90fa9aeSRam Pai 	if (IS_MNT_SHARED(dest_mnt)) {
1611315fc83eSAl Viro 		for (p = real_mount(source_mnt); p; p = next_mnt(p, source_mnt))
1612315fc83eSAl Viro 			set_mnt_shared(&p->mnt);
1613b90fa9aeSRam Pai 	}
16141a390689SAl Viro 	if (parent_path) {
1615419148daSAl Viro 		detach_mnt(real_mount(source_mnt), parent_path);
1616419148daSAl Viro 		attach_mnt(real_mount(source_mnt), path);
1617e5d67f07SAl Viro 		touch_mnt_namespace(parent_path->mnt->mnt_ns);
161821444403SRam Pai 	} else {
1619b90fa9aeSRam Pai 		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
16204b2619a5SAl Viro 		commit_tree(real_mount(source_mnt));
162121444403SRam Pai 	}
1622b90fa9aeSRam Pai 
1623315fc83eSAl Viro 	list_for_each_entry_safe(child, p, &tree_list, mnt.mnt_hash) {
1624315fc83eSAl Viro 		list_del_init(&child->mnt.mnt_hash);
16254b2619a5SAl Viro 		commit_tree(child);
1626b90fa9aeSRam Pai 	}
162799b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
162899b7db7bSNick Piggin 
1629b90fa9aeSRam Pai 	return 0;
1630719f5d7fSMiklos Szeredi 
1631719f5d7fSMiklos Szeredi  out_cleanup_ids:
1632719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt))
16334b8b21f4SAl Viro 		cleanup_group_ids(real_mount(source_mnt), NULL);
1634719f5d7fSMiklos Szeredi  out:
1635719f5d7fSMiklos Szeredi 	return err;
1636b90fa9aeSRam Pai }
1637b90fa9aeSRam Pai 
1638b12cea91SAl Viro static int lock_mount(struct path *path)
1639b12cea91SAl Viro {
1640b12cea91SAl Viro 	struct vfsmount *mnt;
1641b12cea91SAl Viro retry:
1642b12cea91SAl Viro 	mutex_lock(&path->dentry->d_inode->i_mutex);
1643b12cea91SAl Viro 	if (unlikely(cant_mount(path->dentry))) {
1644b12cea91SAl Viro 		mutex_unlock(&path->dentry->d_inode->i_mutex);
1645b12cea91SAl Viro 		return -ENOENT;
1646b12cea91SAl Viro 	}
1647b12cea91SAl Viro 	down_write(&namespace_sem);
1648b12cea91SAl Viro 	mnt = lookup_mnt(path);
1649b12cea91SAl Viro 	if (likely(!mnt))
1650b12cea91SAl Viro 		return 0;
1651b12cea91SAl Viro 	up_write(&namespace_sem);
1652b12cea91SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
1653b12cea91SAl Viro 	path_put(path);
1654b12cea91SAl Viro 	path->mnt = mnt;
1655b12cea91SAl Viro 	path->dentry = dget(mnt->mnt_root);
1656b12cea91SAl Viro 	goto retry;
1657b12cea91SAl Viro }
1658b12cea91SAl Viro 
1659b12cea91SAl Viro static void unlock_mount(struct path *path)
1660b12cea91SAl Viro {
1661b12cea91SAl Viro 	up_write(&namespace_sem);
1662b12cea91SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
1663b12cea91SAl Viro }
1664b12cea91SAl Viro 
16658c3ee42eSAl Viro static int graft_tree(struct vfsmount *mnt, struct path *path)
16661da177e4SLinus Torvalds {
16671da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_flags & MS_NOUSER)
16681da177e4SLinus Torvalds 		return -EINVAL;
16691da177e4SLinus Torvalds 
16708c3ee42eSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
16711da177e4SLinus Torvalds 	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
16721da177e4SLinus Torvalds 		return -ENOTDIR;
16731da177e4SLinus Torvalds 
1674b12cea91SAl Viro 	if (d_unlinked(path->dentry))
1675b12cea91SAl Viro 		return -ENOENT;
16761da177e4SLinus Torvalds 
1677b12cea91SAl Viro 	return attach_recursive_mnt(mnt, path, NULL);
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds /*
16817a2e8a8fSValerie Aurora  * Sanity check the flags to change_mnt_propagation.
16827a2e8a8fSValerie Aurora  */
16837a2e8a8fSValerie Aurora 
16847a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags)
16857a2e8a8fSValerie Aurora {
16867c6e984dSRoman Borisov 	int type = flags & ~(MS_REC | MS_SILENT);
16877a2e8a8fSValerie Aurora 
16887a2e8a8fSValerie Aurora 	/* Fail if any non-propagation flags are set */
16897a2e8a8fSValerie Aurora 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
16907a2e8a8fSValerie Aurora 		return 0;
16917a2e8a8fSValerie Aurora 	/* Only one propagation flag should be set */
16927a2e8a8fSValerie Aurora 	if (!is_power_of_2(type))
16937a2e8a8fSValerie Aurora 		return 0;
16947a2e8a8fSValerie Aurora 	return type;
16957a2e8a8fSValerie Aurora }
16967a2e8a8fSValerie Aurora 
16977a2e8a8fSValerie Aurora /*
169807b20889SRam Pai  * recursively change the type of the mountpoint.
169907b20889SRam Pai  */
17000a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
170107b20889SRam Pai {
1702315fc83eSAl Viro 	struct mount *m;
17034b8b21f4SAl Viro 	struct mount *mnt = real_mount(path->mnt);
170407b20889SRam Pai 	int recurse = flag & MS_REC;
17057a2e8a8fSValerie Aurora 	int type;
1706719f5d7fSMiklos Szeredi 	int err = 0;
170707b20889SRam Pai 
1708ee6f9582SMiklos Szeredi 	if (!capable(CAP_SYS_ADMIN))
1709ee6f9582SMiklos Szeredi 		return -EPERM;
1710ee6f9582SMiklos Szeredi 
17112d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
171207b20889SRam Pai 		return -EINVAL;
171307b20889SRam Pai 
17147a2e8a8fSValerie Aurora 	type = flags_to_propagation_type(flag);
17157a2e8a8fSValerie Aurora 	if (!type)
17167a2e8a8fSValerie Aurora 		return -EINVAL;
17177a2e8a8fSValerie Aurora 
171807b20889SRam Pai 	down_write(&namespace_sem);
1719719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1720719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1721719f5d7fSMiklos Szeredi 		if (err)
1722719f5d7fSMiklos Szeredi 			goto out_unlock;
1723719f5d7fSMiklos Szeredi 	}
1724719f5d7fSMiklos Szeredi 
172599b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
17264b8b21f4SAl Viro 	for (m = mnt; m; m = (recurse ? next_mnt(m, &mnt->mnt) : NULL))
1727315fc83eSAl Viro 		change_mnt_propagation(&m->mnt, type);
172899b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
1729719f5d7fSMiklos Szeredi 
1730719f5d7fSMiklos Szeredi  out_unlock:
173107b20889SRam Pai 	up_write(&namespace_sem);
1732719f5d7fSMiklos Szeredi 	return err;
173307b20889SRam Pai }
173407b20889SRam Pai 
173507b20889SRam Pai /*
17361da177e4SLinus Torvalds  * do loopback mount.
17371da177e4SLinus Torvalds  */
17380a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name,
17392dafe1c4SEric Sandeen 				int recurse)
17401da177e4SLinus Torvalds {
1741b12cea91SAl Viro 	LIST_HEAD(umount_list);
17422d92ab3cSAl Viro 	struct path old_path;
17431da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
17442d92ab3cSAl Viro 	int err = mount_is_safe(path);
17451da177e4SLinus Torvalds 	if (err)
17461da177e4SLinus Torvalds 		return err;
17471da177e4SLinus Torvalds 	if (!old_name || !*old_name)
17481da177e4SLinus Torvalds 		return -EINVAL;
1749815d405cSTrond Myklebust 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
17501da177e4SLinus Torvalds 	if (err)
17511da177e4SLinus Torvalds 		return err;
17521da177e4SLinus Torvalds 
1753b12cea91SAl Viro 	err = lock_mount(path);
1754b12cea91SAl Viro 	if (err)
17559676f0c6SRam Pai 		goto out;
17569676f0c6SRam Pai 
1757b12cea91SAl Viro 	err = -EINVAL;
1758b12cea91SAl Viro 	if (IS_MNT_UNBINDABLE(old_path.mnt))
1759b12cea91SAl Viro 		goto out2;
1760b12cea91SAl Viro 
17612d92ab3cSAl Viro 	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
1762b12cea91SAl Viro 		goto out2;
1763ccd48bc7SAl Viro 
17641da177e4SLinus Torvalds 	err = -ENOMEM;
17651da177e4SLinus Torvalds 	if (recurse)
17662d92ab3cSAl Viro 		mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
17671da177e4SLinus Torvalds 	else
17682d92ab3cSAl Viro 		mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
17691da177e4SLinus Torvalds 
1770ccd48bc7SAl Viro 	if (!mnt)
1771b12cea91SAl Viro 		goto out2;
1772ccd48bc7SAl Viro 
17732d92ab3cSAl Viro 	err = graft_tree(mnt, path);
17741da177e4SLinus Torvalds 	if (err) {
177599b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1776a05964f3SRam Pai 		umount_tree(mnt, 0, &umount_list);
177799b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
17785b83d2c5SRam Pai 	}
1779b12cea91SAl Viro out2:
1780b12cea91SAl Viro 	unlock_mount(path);
1781b12cea91SAl Viro 	release_mounts(&umount_list);
1782ccd48bc7SAl Viro out:
17832d92ab3cSAl Viro 	path_put(&old_path);
17841da177e4SLinus Torvalds 	return err;
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
17872e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
17882e4b7fcdSDave Hansen {
17892e4b7fcdSDave Hansen 	int error = 0;
17902e4b7fcdSDave Hansen 	int readonly_request = 0;
17912e4b7fcdSDave Hansen 
17922e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
17932e4b7fcdSDave Hansen 		readonly_request = 1;
17942e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
17952e4b7fcdSDave Hansen 		return 0;
17962e4b7fcdSDave Hansen 
17972e4b7fcdSDave Hansen 	if (readonly_request)
17982e4b7fcdSDave Hansen 		error = mnt_make_readonly(mnt);
17992e4b7fcdSDave Hansen 	else
18002e4b7fcdSDave Hansen 		__mnt_unmake_readonly(mnt);
18012e4b7fcdSDave Hansen 	return error;
18022e4b7fcdSDave Hansen }
18032e4b7fcdSDave Hansen 
18041da177e4SLinus Torvalds /*
18051da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
18061da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
18071da177e4SLinus Torvalds  * on it - tough luck.
18081da177e4SLinus Torvalds  */
18090a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
18101da177e4SLinus Torvalds 		      void *data)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	int err;
18132d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
18161da177e4SLinus Torvalds 		return -EPERM;
18171da177e4SLinus Torvalds 
18182d92ab3cSAl Viro 	if (!check_mnt(path->mnt))
18191da177e4SLinus Torvalds 		return -EINVAL;
18201da177e4SLinus Torvalds 
18212d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
18221da177e4SLinus Torvalds 		return -EINVAL;
18231da177e4SLinus Torvalds 
1824ff36fe2cSEric Paris 	err = security_sb_remount(sb, data);
1825ff36fe2cSEric Paris 	if (err)
1826ff36fe2cSEric Paris 		return err;
1827ff36fe2cSEric Paris 
18281da177e4SLinus Torvalds 	down_write(&sb->s_umount);
18292e4b7fcdSDave Hansen 	if (flags & MS_BIND)
18302d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
18314aa98cf7SAl Viro 	else
18321da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
18337b43a79fSAl Viro 	if (!err) {
183499b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1835495d6c9cSValerie Aurora 		mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK;
18362d92ab3cSAl Viro 		path->mnt->mnt_flags = mnt_flags;
183799b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
18387b43a79fSAl Viro 	}
18391da177e4SLinus Torvalds 	up_write(&sb->s_umount);
18400e55a7ccSDan Williams 	if (!err) {
184199b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
18420e55a7ccSDan Williams 		touch_mnt_namespace(path->mnt->mnt_ns);
184399b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
18440e55a7ccSDan Williams 	}
18451da177e4SLinus Torvalds 	return err;
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
18489676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt)
18499676f0c6SRam Pai {
1850315fc83eSAl Viro 	struct mount *p;
1851315fc83eSAl Viro 	for (p = real_mount(mnt); p; p = next_mnt(p, mnt)) {
1852315fc83eSAl Viro 		if (IS_MNT_UNBINDABLE(&p->mnt))
18539676f0c6SRam Pai 			return 1;
18549676f0c6SRam Pai 	}
18559676f0c6SRam Pai 	return 0;
18569676f0c6SRam Pai }
18579676f0c6SRam Pai 
18580a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name)
18591da177e4SLinus Torvalds {
18602d92ab3cSAl Viro 	struct path old_path, parent_path;
18611da177e4SLinus Torvalds 	struct vfsmount *p;
18621da177e4SLinus Torvalds 	int err = 0;
18631da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
18641da177e4SLinus Torvalds 		return -EPERM;
18651da177e4SLinus Torvalds 	if (!old_name || !*old_name)
18661da177e4SLinus Torvalds 		return -EINVAL;
18672d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
18681da177e4SLinus Torvalds 	if (err)
18691da177e4SLinus Torvalds 		return err;
18701da177e4SLinus Torvalds 
1871b12cea91SAl Viro 	err = lock_mount(path);
1872cc53ce53SDavid Howells 	if (err < 0)
1873cc53ce53SDavid Howells 		goto out;
1874cc53ce53SDavid Howells 
18751da177e4SLinus Torvalds 	err = -EINVAL;
18762d92ab3cSAl Viro 	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
18771da177e4SLinus Torvalds 		goto out1;
18781da177e4SLinus Torvalds 
1879f3da392eSAlexey Dobriyan 	if (d_unlinked(path->dentry))
188021444403SRam Pai 		goto out1;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	err = -EINVAL;
18832d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
188421444403SRam Pai 		goto out1;
18851da177e4SLinus Torvalds 
1886b2dba1afSAl Viro 	if (!mnt_has_parent(old_path.mnt))
188721444403SRam Pai 		goto out1;
18881da177e4SLinus Torvalds 
18892d92ab3cSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
18902d92ab3cSAl Viro 	      S_ISDIR(old_path.dentry->d_inode->i_mode))
189121444403SRam Pai 		goto out1;
189221444403SRam Pai 	/*
189321444403SRam Pai 	 * Don't move a mount residing in a shared parent.
189421444403SRam Pai 	 */
1895afac7cbaSAl Viro 	if (IS_MNT_SHARED(old_path.mnt->mnt_parent))
189621444403SRam Pai 		goto out1;
18979676f0c6SRam Pai 	/*
18989676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
18999676f0c6SRam Pai 	 * mount which is shared.
19009676f0c6SRam Pai 	 */
19012d92ab3cSAl Viro 	if (IS_MNT_SHARED(path->mnt) &&
19022d92ab3cSAl Viro 	    tree_contains_unbindable(old_path.mnt))
19039676f0c6SRam Pai 		goto out1;
19041da177e4SLinus Torvalds 	err = -ELOOP;
1905b2dba1afSAl Viro 	for (p = path->mnt; mnt_has_parent(p); p = p->mnt_parent)
19062d92ab3cSAl Viro 		if (p == old_path.mnt)
190721444403SRam Pai 			goto out1;
19081da177e4SLinus Torvalds 
19092d92ab3cSAl Viro 	err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
19104ac91378SJan Blunck 	if (err)
191121444403SRam Pai 		goto out1;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
19141da177e4SLinus Torvalds 	 * automatically */
19152d92ab3cSAl Viro 	list_del_init(&old_path.mnt->mnt_expire);
19161da177e4SLinus Torvalds out1:
1917b12cea91SAl Viro 	unlock_mount(path);
19181da177e4SLinus Torvalds out:
19191da177e4SLinus Torvalds 	if (!err)
19201a390689SAl Viro 		path_put(&parent_path);
19212d92ab3cSAl Viro 	path_put(&old_path);
19221da177e4SLinus Torvalds 	return err;
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
19259d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
19269d412a43SAl Viro {
19279d412a43SAl Viro 	int err;
19289d412a43SAl Viro 	const char *subtype = strchr(fstype, '.');
19299d412a43SAl Viro 	if (subtype) {
19309d412a43SAl Viro 		subtype++;
19319d412a43SAl Viro 		err = -EINVAL;
19329d412a43SAl Viro 		if (!subtype[0])
19339d412a43SAl Viro 			goto err;
19349d412a43SAl Viro 	} else
19359d412a43SAl Viro 		subtype = "";
19369d412a43SAl Viro 
19379d412a43SAl Viro 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
19389d412a43SAl Viro 	err = -ENOMEM;
19399d412a43SAl Viro 	if (!mnt->mnt_sb->s_subtype)
19409d412a43SAl Viro 		goto err;
19419d412a43SAl Viro 	return mnt;
19429d412a43SAl Viro 
19439d412a43SAl Viro  err:
19449d412a43SAl Viro 	mntput(mnt);
19459d412a43SAl Viro 	return ERR_PTR(err);
19469d412a43SAl Viro }
19479d412a43SAl Viro 
194879e801a9SAl Viro static struct vfsmount *
19499d412a43SAl Viro do_kern_mount(const char *fstype, int flags, const char *name, void *data)
19509d412a43SAl Viro {
19519d412a43SAl Viro 	struct file_system_type *type = get_fs_type(fstype);
19529d412a43SAl Viro 	struct vfsmount *mnt;
19539d412a43SAl Viro 	if (!type)
19549d412a43SAl Viro 		return ERR_PTR(-ENODEV);
19559d412a43SAl Viro 	mnt = vfs_kern_mount(type, flags, name, data);
19569d412a43SAl Viro 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
19579d412a43SAl Viro 	    !mnt->mnt_sb->s_subtype)
19589d412a43SAl Viro 		mnt = fs_set_subtype(mnt, fstype);
19599d412a43SAl Viro 	put_filesystem(type);
19609d412a43SAl Viro 	return mnt;
19619d412a43SAl Viro }
19629d412a43SAl Viro 
19639d412a43SAl Viro /*
19649d412a43SAl Viro  * add a mount into a namespace's mount tree
19659d412a43SAl Viro  */
19669d412a43SAl Viro static int do_add_mount(struct vfsmount *newmnt, struct path *path, int mnt_flags)
19679d412a43SAl Viro {
19689d412a43SAl Viro 	int err;
19699d412a43SAl Viro 
19709d412a43SAl Viro 	mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
19719d412a43SAl Viro 
1972b12cea91SAl Viro 	err = lock_mount(path);
1973b12cea91SAl Viro 	if (err)
1974b12cea91SAl Viro 		return err;
19759d412a43SAl Viro 
19769d412a43SAl Viro 	err = -EINVAL;
19779d412a43SAl Viro 	if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
19789d412a43SAl Viro 		goto unlock;
19799d412a43SAl Viro 
19809d412a43SAl Viro 	/* Refuse the same filesystem on the same mount point */
19819d412a43SAl Viro 	err = -EBUSY;
19829d412a43SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt_sb &&
19839d412a43SAl Viro 	    path->mnt->mnt_root == path->dentry)
19849d412a43SAl Viro 		goto unlock;
19859d412a43SAl Viro 
19869d412a43SAl Viro 	err = -EINVAL;
19879d412a43SAl Viro 	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
19889d412a43SAl Viro 		goto unlock;
19899d412a43SAl Viro 
19909d412a43SAl Viro 	newmnt->mnt_flags = mnt_flags;
19919d412a43SAl Viro 	err = graft_tree(newmnt, path);
19929d412a43SAl Viro 
19939d412a43SAl Viro unlock:
1994b12cea91SAl Viro 	unlock_mount(path);
19959d412a43SAl Viro 	return err;
19969d412a43SAl Viro }
1997b1e75df4SAl Viro 
19981da177e4SLinus Torvalds /*
19991da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
20001da177e4SLinus Torvalds  * namespace's tree
20011da177e4SLinus Torvalds  */
20020a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags,
20031da177e4SLinus Torvalds 			int mnt_flags, char *name, void *data)
20041da177e4SLinus Torvalds {
20051da177e4SLinus Torvalds 	struct vfsmount *mnt;
200615f9a3f3SAl Viro 	int err;
20071da177e4SLinus Torvalds 
2008eca6f534SVegard Nossum 	if (!type)
20091da177e4SLinus Torvalds 		return -EINVAL;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	/* we need capabilities... */
20121da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
20131da177e4SLinus Torvalds 		return -EPERM;
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	mnt = do_kern_mount(type, flags, name, data);
20161da177e4SLinus Torvalds 	if (IS_ERR(mnt))
20171da177e4SLinus Torvalds 		return PTR_ERR(mnt);
20181da177e4SLinus Torvalds 
201915f9a3f3SAl Viro 	err = do_add_mount(mnt, path, mnt_flags);
202015f9a3f3SAl Viro 	if (err)
202115f9a3f3SAl Viro 		mntput(mnt);
202215f9a3f3SAl Viro 	return err;
20231da177e4SLinus Torvalds }
20241da177e4SLinus Torvalds 
202519a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path)
202619a167afSAl Viro {
202719a167afSAl Viro 	int err;
202819a167afSAl Viro 	/* The new mount record should have at least 2 refs to prevent it being
202919a167afSAl Viro 	 * expired before we get a chance to add it
203019a167afSAl Viro 	 */
203119a167afSAl Viro 	BUG_ON(mnt_get_count(m) < 2);
203219a167afSAl Viro 
203319a167afSAl Viro 	if (m->mnt_sb == path->mnt->mnt_sb &&
203419a167afSAl Viro 	    m->mnt_root == path->dentry) {
2035b1e75df4SAl Viro 		err = -ELOOP;
2036b1e75df4SAl Viro 		goto fail;
203719a167afSAl Viro 	}
203819a167afSAl Viro 
203919a167afSAl Viro 	err = do_add_mount(m, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2040b1e75df4SAl Viro 	if (!err)
2041b1e75df4SAl Viro 		return 0;
2042b1e75df4SAl Viro fail:
2043b1e75df4SAl Viro 	/* remove m from any expiration list it may be on */
2044b1e75df4SAl Viro 	if (!list_empty(&m->mnt_expire)) {
2045b1e75df4SAl Viro 		down_write(&namespace_sem);
2046b1e75df4SAl Viro 		br_write_lock(vfsmount_lock);
2047b1e75df4SAl Viro 		list_del_init(&m->mnt_expire);
2048b1e75df4SAl Viro 		br_write_unlock(vfsmount_lock);
2049b1e75df4SAl Viro 		up_write(&namespace_sem);
205019a167afSAl Viro 	}
2051b1e75df4SAl Viro 	mntput(m);
2052b1e75df4SAl Viro 	mntput(m);
205319a167afSAl Viro 	return err;
205419a167afSAl Viro }
205519a167afSAl Viro 
2056ea5b778aSDavid Howells /**
2057ea5b778aSDavid Howells  * mnt_set_expiry - Put a mount on an expiration list
2058ea5b778aSDavid Howells  * @mnt: The mount to list.
2059ea5b778aSDavid Howells  * @expiry_list: The list to add the mount to.
2060ea5b778aSDavid Howells  */
2061ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2062ea5b778aSDavid Howells {
2063ea5b778aSDavid Howells 	down_write(&namespace_sem);
2064ea5b778aSDavid Howells 	br_write_lock(vfsmount_lock);
2065ea5b778aSDavid Howells 
2066ea5b778aSDavid Howells 	list_add_tail(&mnt->mnt_expire, expiry_list);
2067ea5b778aSDavid Howells 
2068ea5b778aSDavid Howells 	br_write_unlock(vfsmount_lock);
2069ea5b778aSDavid Howells 	up_write(&namespace_sem);
2070ea5b778aSDavid Howells }
2071ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry);
2072ea5b778aSDavid Howells 
2073ea5b778aSDavid Howells /*
20741da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
20751da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
20761da177e4SLinus Torvalds  * here
20771da177e4SLinus Torvalds  */
20781da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
20791da177e4SLinus Torvalds {
20801da177e4SLinus Torvalds 	struct vfsmount *mnt, *next;
20811da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
2082bcc5c7d2SAl Viro 	LIST_HEAD(umounts);
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 	if (list_empty(mounts))
20851da177e4SLinus Torvalds 		return;
20861da177e4SLinus Torvalds 
2087bcc5c7d2SAl Viro 	down_write(&namespace_sem);
208899b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
20911da177e4SLinus Torvalds 	 * following criteria:
20921da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
20931da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
20941da177e4SLinus Torvalds 	 *   cleared by mntput())
20951da177e4SLinus Torvalds 	 */
209655e700b9SMiklos Szeredi 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
20971da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
2098bcc5c7d2SAl Viro 			propagate_mount_busy(mnt, 1))
20991da177e4SLinus Torvalds 			continue;
210055e700b9SMiklos Szeredi 		list_move(&mnt->mnt_expire, &graveyard);
21011da177e4SLinus Torvalds 	}
2102bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
2103bcc5c7d2SAl Viro 		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
2104bcc5c7d2SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
2105bcc5c7d2SAl Viro 		umount_tree(mnt, 1, &umounts);
2106bcc5c7d2SAl Viro 	}
210799b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
2108bcc5c7d2SAl Viro 	up_write(&namespace_sem);
2109bcc5c7d2SAl Viro 
2110bcc5c7d2SAl Viro 	release_mounts(&umounts);
21111da177e4SLinus Torvalds }
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds /*
21165528f911STrond Myklebust  * Ripoff of 'select_parent()'
21175528f911STrond Myklebust  *
21185528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
21195528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
21205528f911STrond Myklebust  */
21215528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
21225528f911STrond Myklebust {
21235528f911STrond Myklebust 	struct vfsmount *this_parent = parent;
21245528f911STrond Myklebust 	struct list_head *next;
21255528f911STrond Myklebust 	int found = 0;
21265528f911STrond Myklebust 
21275528f911STrond Myklebust repeat:
21285528f911STrond Myklebust 	next = this_parent->mnt_mounts.next;
21295528f911STrond Myklebust resume:
21305528f911STrond Myklebust 	while (next != &this_parent->mnt_mounts) {
21315528f911STrond Myklebust 		struct list_head *tmp = next;
21325528f911STrond Myklebust 		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
21335528f911STrond Myklebust 
21345528f911STrond Myklebust 		next = tmp->next;
21355528f911STrond Myklebust 		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
21365528f911STrond Myklebust 			continue;
21375528f911STrond Myklebust 		/*
21385528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
21395528f911STrond Myklebust 		 */
21405528f911STrond Myklebust 		if (!list_empty(&mnt->mnt_mounts)) {
21415528f911STrond Myklebust 			this_parent = mnt;
21425528f911STrond Myklebust 			goto repeat;
21435528f911STrond Myklebust 		}
21445528f911STrond Myklebust 
21455528f911STrond Myklebust 		if (!propagate_mount_busy(mnt, 1)) {
21465528f911STrond Myklebust 			list_move_tail(&mnt->mnt_expire, graveyard);
21475528f911STrond Myklebust 			found++;
21485528f911STrond Myklebust 		}
21495528f911STrond Myklebust 	}
21505528f911STrond Myklebust 	/*
21515528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
21525528f911STrond Myklebust 	 */
21535528f911STrond Myklebust 	if (this_parent != parent) {
21545528f911STrond Myklebust 		next = this_parent->mnt_child.next;
21555528f911STrond Myklebust 		this_parent = this_parent->mnt_parent;
21565528f911STrond Myklebust 		goto resume;
21575528f911STrond Myklebust 	}
21585528f911STrond Myklebust 	return found;
21595528f911STrond Myklebust }
21605528f911STrond Myklebust 
21615528f911STrond Myklebust /*
21625528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
21635528f911STrond Myklebust  * submounts of a specific parent mountpoint
216499b7db7bSNick Piggin  *
216599b7db7bSNick Piggin  * vfsmount_lock must be held for write
21665528f911STrond Myklebust  */
2167c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
21685528f911STrond Myklebust {
21695528f911STrond Myklebust 	LIST_HEAD(graveyard);
2170c35038beSAl Viro 	struct vfsmount *m;
21715528f911STrond Myklebust 
21725528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
2173c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
2174bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
2175c35038beSAl Viro 			m = list_first_entry(&graveyard, struct vfsmount,
2176bcc5c7d2SAl Viro 						mnt_expire);
2177afef80b3SEric W. Biederman 			touch_mnt_namespace(m->mnt_ns);
2178afef80b3SEric W. Biederman 			umount_tree(m, 1, umounts);
2179bcc5c7d2SAl Viro 		}
2180bcc5c7d2SAl Viro 	}
21815528f911STrond Myklebust }
21825528f911STrond Myklebust 
21835528f911STrond Myklebust /*
21841da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
21851da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
21861da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
21871da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
21881da177e4SLinus Torvalds  */
2189b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
2190b58fed8bSRam Pai 				 unsigned long n)
21911da177e4SLinus Torvalds {
21921da177e4SLinus Torvalds 	char *t = to;
21931da177e4SLinus Torvalds 	const char __user *f = from;
21941da177e4SLinus Torvalds 	char c;
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
21971da177e4SLinus Torvalds 		return n;
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 	while (n) {
22001da177e4SLinus Torvalds 		if (__get_user(c, f)) {
22011da177e4SLinus Torvalds 			memset(t, 0, n);
22021da177e4SLinus Torvalds 			break;
22031da177e4SLinus Torvalds 		}
22041da177e4SLinus Torvalds 		*t++ = c;
22051da177e4SLinus Torvalds 		f++;
22061da177e4SLinus Torvalds 		n--;
22071da177e4SLinus Torvalds 	}
22081da177e4SLinus Torvalds 	return n;
22091da177e4SLinus Torvalds }
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
22121da177e4SLinus Torvalds {
22131da177e4SLinus Torvalds 	int i;
22141da177e4SLinus Torvalds 	unsigned long page;
22151da177e4SLinus Torvalds 	unsigned long size;
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds 	*where = 0;
22181da177e4SLinus Torvalds 	if (!data)
22191da177e4SLinus Torvalds 		return 0;
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
22221da177e4SLinus Torvalds 		return -ENOMEM;
22231da177e4SLinus Torvalds 
22241da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
22251da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
22261da177e4SLinus Torvalds 	 * the remainder of the page.
22271da177e4SLinus Torvalds 	 */
22281da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
22291da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
22301da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
22311da177e4SLinus Torvalds 		size = PAGE_SIZE;
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
22341da177e4SLinus Torvalds 	if (!i) {
22351da177e4SLinus Torvalds 		free_page(page);
22361da177e4SLinus Torvalds 		return -EFAULT;
22371da177e4SLinus Torvalds 	}
22381da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
22391da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
22401da177e4SLinus Torvalds 	*where = page;
22411da177e4SLinus Torvalds 	return 0;
22421da177e4SLinus Torvalds }
22431da177e4SLinus Torvalds 
2244eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where)
2245eca6f534SVegard Nossum {
2246eca6f534SVegard Nossum 	char *tmp;
2247eca6f534SVegard Nossum 
2248eca6f534SVegard Nossum 	if (!data) {
2249eca6f534SVegard Nossum 		*where = NULL;
2250eca6f534SVegard Nossum 		return 0;
2251eca6f534SVegard Nossum 	}
2252eca6f534SVegard Nossum 
2253eca6f534SVegard Nossum 	tmp = strndup_user(data, PAGE_SIZE);
2254eca6f534SVegard Nossum 	if (IS_ERR(tmp))
2255eca6f534SVegard Nossum 		return PTR_ERR(tmp);
2256eca6f534SVegard Nossum 
2257eca6f534SVegard Nossum 	*where = tmp;
2258eca6f534SVegard Nossum 	return 0;
2259eca6f534SVegard Nossum }
2260eca6f534SVegard Nossum 
22611da177e4SLinus Torvalds /*
22621da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
22631da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
22641da177e4SLinus Torvalds  *
22651da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
22661da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
22671da177e4SLinus Torvalds  * information (or be NULL).
22681da177e4SLinus Torvalds  *
22691da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
22701da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
22711da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
22721da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
22731da177e4SLinus Torvalds  * and must be discarded.
22741da177e4SLinus Torvalds  */
22751da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page,
22761da177e4SLinus Torvalds 		  unsigned long flags, void *data_page)
22771da177e4SLinus Torvalds {
22782d92ab3cSAl Viro 	struct path path;
22791da177e4SLinus Torvalds 	int retval = 0;
22801da177e4SLinus Torvalds 	int mnt_flags = 0;
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 	/* Discard magic */
22831da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
22841da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	/* Basic sanity checks */
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
22891da177e4SLinus Torvalds 		return -EINVAL;
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds 	if (data_page)
22921da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
22931da177e4SLinus Torvalds 
2294a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
2295a27ab9f2STetsuo Handa 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2296a27ab9f2STetsuo Handa 	if (retval)
2297a27ab9f2STetsuo Handa 		return retval;
2298a27ab9f2STetsuo Handa 
2299a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2300a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
2301a27ab9f2STetsuo Handa 	if (retval)
2302a27ab9f2STetsuo Handa 		goto dput_out;
2303a27ab9f2STetsuo Handa 
2304613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2305613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
23060a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
23070a1c01c9SMatthew Garrett 
23081da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
23091da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
23101da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
23111da177e4SLinus Torvalds 	if (flags & MS_NODEV)
23121da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
23131da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
23141da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2315fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2316fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2317fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2318fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2319d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2320d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
23212e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
23222e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2323fc33a7bbSChristoph Hellwig 
23247a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2325d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2326d0adde57SMatthew Garrett 		   MS_STRICTATIME);
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
23292d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
23301da177e4SLinus Torvalds 				    data_page);
23311da177e4SLinus Torvalds 	else if (flags & MS_BIND)
23322d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
23339676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
23342d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
23351da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
23362d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
23371da177e4SLinus Torvalds 	else
23382d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
23391da177e4SLinus Torvalds 				      dev_name, data_page);
23401da177e4SLinus Torvalds dput_out:
23412d92ab3cSAl Viro 	path_put(&path);
23421da177e4SLinus Torvalds 	return retval;
23431da177e4SLinus Torvalds }
23441da177e4SLinus Torvalds 
2345cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void)
2346cf8d2c11STrond Myklebust {
2347cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2348cf8d2c11STrond Myklebust 
2349cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2350cf8d2c11STrond Myklebust 	if (!new_ns)
2351cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
2352cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2353cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2354cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2355cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2356cf8d2c11STrond Myklebust 	new_ns->event = 0;
2357cf8d2c11STrond Myklebust 	return new_ns;
2358cf8d2c11STrond Myklebust }
2359cf8d2c11STrond Myklebust 
2360f03c6599SAl Viro void mnt_make_longterm(struct vfsmount *mnt)
2361f03c6599SAl Viro {
23627e3d0eb0SAl Viro 	__mnt_make_longterm(mnt);
2363f03c6599SAl Viro }
2364f03c6599SAl Viro 
2365f03c6599SAl Viro void mnt_make_shortterm(struct vfsmount *mnt)
2366f03c6599SAl Viro {
23677e3d0eb0SAl Viro #ifdef CONFIG_SMP
2368f03c6599SAl Viro 	if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
2369f03c6599SAl Viro 		return;
2370f03c6599SAl Viro 	br_write_lock(vfsmount_lock);
2371f03c6599SAl Viro 	atomic_dec(&mnt->mnt_longterm);
2372f03c6599SAl Viro 	br_write_unlock(vfsmount_lock);
23737e3d0eb0SAl Viro #endif
2374f03c6599SAl Viro }
2375f03c6599SAl Viro 
2376741a2951SJANAK DESAI /*
2377741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
2378741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
2379741a2951SJANAK DESAI  */
2380e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
23816b3286edSKirill Korotaev 		struct fs_struct *fs)
23821da177e4SLinus Torvalds {
23836b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
23847f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2385315fc83eSAl Viro 	struct mount *p, *q;
23861da177e4SLinus Torvalds 
2387cf8d2c11STrond Myklebust 	new_ns = alloc_mnt_ns();
2388cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2389cf8d2c11STrond Myklebust 		return new_ns;
23901da177e4SLinus Torvalds 
2391390c6843SRam Pai 	down_write(&namespace_sem);
23921da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
23936b3286edSKirill Korotaev 	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
23949676f0c6SRam Pai 					CL_COPY_ALL | CL_EXPIRE);
23951da177e4SLinus Torvalds 	if (!new_ns->root) {
2396390c6843SRam Pai 		up_write(&namespace_sem);
23971da177e4SLinus Torvalds 		kfree(new_ns);
23985cc4a034SJulia Lawall 		return ERR_PTR(-ENOMEM);
23991da177e4SLinus Torvalds 	}
240099b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
24011da177e4SLinus Torvalds 	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
240299b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds 	/*
24051da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
24061da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
24071da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
24081da177e4SLinus Torvalds 	 */
2409315fc83eSAl Viro 	p = real_mount(mnt_ns->root);
2410315fc83eSAl Viro 	q = real_mount(new_ns->root);
24111da177e4SLinus Torvalds 	while (p) {
2412315fc83eSAl Viro 		q->mnt.mnt_ns = new_ns;
2413315fc83eSAl Viro 		__mnt_make_longterm(&q->mnt);
24141da177e4SLinus Torvalds 		if (fs) {
2415315fc83eSAl Viro 			if (&p->mnt == fs->root.mnt) {
2416315fc83eSAl Viro 				fs->root.mnt = mntget(&q->mnt);
2417315fc83eSAl Viro 				__mnt_make_longterm(&q->mnt);
2418315fc83eSAl Viro 				mnt_make_shortterm(&p->mnt);
2419315fc83eSAl Viro 				rootmnt = &p->mnt;
24201da177e4SLinus Torvalds 			}
2421315fc83eSAl Viro 			if (&p->mnt == fs->pwd.mnt) {
2422315fc83eSAl Viro 				fs->pwd.mnt = mntget(&q->mnt);
2423315fc83eSAl Viro 				__mnt_make_longterm(&q->mnt);
2424315fc83eSAl Viro 				mnt_make_shortterm(&p->mnt);
2425315fc83eSAl Viro 				pwdmnt = &p->mnt;
24261da177e4SLinus Torvalds 			}
24271da177e4SLinus Torvalds 		}
24286b3286edSKirill Korotaev 		p = next_mnt(p, mnt_ns->root);
24291da177e4SLinus Torvalds 		q = next_mnt(q, new_ns->root);
24301da177e4SLinus Torvalds 	}
2431390c6843SRam Pai 	up_write(&namespace_sem);
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	if (rootmnt)
2434f03c6599SAl Viro 		mntput(rootmnt);
24351da177e4SLinus Torvalds 	if (pwdmnt)
2436f03c6599SAl Viro 		mntput(pwdmnt);
24371da177e4SLinus Torvalds 
2438741a2951SJANAK DESAI 	return new_ns;
2439741a2951SJANAK DESAI }
2440741a2951SJANAK DESAI 
2441213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2442e3222c4eSBadari Pulavarty 		struct fs_struct *new_fs)
2443741a2951SJANAK DESAI {
24446b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
2445741a2951SJANAK DESAI 
2446e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
24476b3286edSKirill Korotaev 	get_mnt_ns(ns);
2448741a2951SJANAK DESAI 
2449741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
2450e3222c4eSBadari Pulavarty 		return ns;
2451741a2951SJANAK DESAI 
2452e3222c4eSBadari Pulavarty 	new_ns = dup_mnt_ns(ns, new_fs);
2453741a2951SJANAK DESAI 
24546b3286edSKirill Korotaev 	put_mnt_ns(ns);
2455e3222c4eSBadari Pulavarty 	return new_ns;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
2458cf8d2c11STrond Myklebust /**
2459cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2460cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2461cf8d2c11STrond Myklebust  */
24626c449c8dSAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
2463cf8d2c11STrond Myklebust {
2464cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2465cf8d2c11STrond Myklebust 
2466cf8d2c11STrond Myklebust 	new_ns = alloc_mnt_ns();
2467cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
2468cf8d2c11STrond Myklebust 		mnt->mnt_ns = new_ns;
24697e3d0eb0SAl Viro 		__mnt_make_longterm(mnt);
2470cf8d2c11STrond Myklebust 		new_ns->root = mnt;
2471cf8d2c11STrond Myklebust 		list_add(&new_ns->list, &new_ns->root->mnt_list);
2472c1334495SAl Viro 	} else {
2473c1334495SAl Viro 		mntput(mnt);
2474cf8d2c11STrond Myklebust 	}
2475cf8d2c11STrond Myklebust 	return new_ns;
2476cf8d2c11STrond Myklebust }
2477cf8d2c11STrond Myklebust 
2478ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2479ea441d11SAl Viro {
2480ea441d11SAl Viro 	struct mnt_namespace *ns;
2481d31da0f0SAl Viro 	struct super_block *s;
2482ea441d11SAl Viro 	struct path path;
2483ea441d11SAl Viro 	int err;
2484ea441d11SAl Viro 
2485ea441d11SAl Viro 	ns = create_mnt_ns(mnt);
2486ea441d11SAl Viro 	if (IS_ERR(ns))
2487ea441d11SAl Viro 		return ERR_CAST(ns);
2488ea441d11SAl Viro 
2489ea441d11SAl Viro 	err = vfs_path_lookup(mnt->mnt_root, mnt,
2490ea441d11SAl Viro 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2491ea441d11SAl Viro 
2492ea441d11SAl Viro 	put_mnt_ns(ns);
2493ea441d11SAl Viro 
2494ea441d11SAl Viro 	if (err)
2495ea441d11SAl Viro 		return ERR_PTR(err);
2496ea441d11SAl Viro 
2497ea441d11SAl Viro 	/* trade a vfsmount reference for active sb one */
2498d31da0f0SAl Viro 	s = path.mnt->mnt_sb;
2499d31da0f0SAl Viro 	atomic_inc(&s->s_active);
2500ea441d11SAl Viro 	mntput(path.mnt);
2501ea441d11SAl Viro 	/* lock the sucker */
2502d31da0f0SAl Viro 	down_write(&s->s_umount);
2503ea441d11SAl Viro 	/* ... and return the root of (sub)tree on it */
2504ea441d11SAl Viro 	return path.dentry;
2505ea441d11SAl Viro }
2506ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree);
2507ea441d11SAl Viro 
2508bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2509bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
25101da177e4SLinus Torvalds {
2511eca6f534SVegard Nossum 	int ret;
2512eca6f534SVegard Nossum 	char *kernel_type;
2513eca6f534SVegard Nossum 	char *kernel_dir;
2514eca6f534SVegard Nossum 	char *kernel_dev;
25151da177e4SLinus Torvalds 	unsigned long data_page;
25161da177e4SLinus Torvalds 
2517eca6f534SVegard Nossum 	ret = copy_mount_string(type, &kernel_type);
2518eca6f534SVegard Nossum 	if (ret < 0)
2519eca6f534SVegard Nossum 		goto out_type;
25201da177e4SLinus Torvalds 
2521eca6f534SVegard Nossum 	kernel_dir = getname(dir_name);
2522eca6f534SVegard Nossum 	if (IS_ERR(kernel_dir)) {
2523eca6f534SVegard Nossum 		ret = PTR_ERR(kernel_dir);
2524eca6f534SVegard Nossum 		goto out_dir;
2525eca6f534SVegard Nossum 	}
25261da177e4SLinus Torvalds 
2527eca6f534SVegard Nossum 	ret = copy_mount_string(dev_name, &kernel_dev);
2528eca6f534SVegard Nossum 	if (ret < 0)
2529eca6f534SVegard Nossum 		goto out_dev;
25301da177e4SLinus Torvalds 
2531eca6f534SVegard Nossum 	ret = copy_mount_options(data, &data_page);
2532eca6f534SVegard Nossum 	if (ret < 0)
2533eca6f534SVegard Nossum 		goto out_data;
25341da177e4SLinus Torvalds 
2535eca6f534SVegard Nossum 	ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
2536eca6f534SVegard Nossum 		(void *) data_page);
2537eca6f534SVegard Nossum 
25381da177e4SLinus Torvalds 	free_page(data_page);
2539eca6f534SVegard Nossum out_data:
2540eca6f534SVegard Nossum 	kfree(kernel_dev);
2541eca6f534SVegard Nossum out_dev:
2542eca6f534SVegard Nossum 	putname(kernel_dir);
2543eca6f534SVegard Nossum out_dir:
2544eca6f534SVegard Nossum 	kfree(kernel_type);
2545eca6f534SVegard Nossum out_type:
2546eca6f534SVegard Nossum 	return ret;
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds /*
2550afac7cbaSAl Viro  * Return true if path is reachable from root
2551afac7cbaSAl Viro  *
2552afac7cbaSAl Viro  * namespace_sem or vfsmount_lock is held
2553afac7cbaSAl Viro  */
2554afac7cbaSAl Viro bool is_path_reachable(struct vfsmount *mnt, struct dentry *dentry,
2555afac7cbaSAl Viro 			 const struct path *root)
2556afac7cbaSAl Viro {
2557afac7cbaSAl Viro 	while (mnt != root->mnt && mnt_has_parent(mnt)) {
2558afac7cbaSAl Viro 		dentry = mnt->mnt_mountpoint;
2559afac7cbaSAl Viro 		mnt = mnt->mnt_parent;
2560afac7cbaSAl Viro 	}
2561afac7cbaSAl Viro 	return mnt == root->mnt && is_subdir(dentry, root->dentry);
2562afac7cbaSAl Viro }
2563afac7cbaSAl Viro 
2564afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2)
2565afac7cbaSAl Viro {
2566afac7cbaSAl Viro 	int res;
2567afac7cbaSAl Viro 	br_read_lock(vfsmount_lock);
2568afac7cbaSAl Viro 	res = is_path_reachable(path1->mnt, path1->dentry, path2);
2569afac7cbaSAl Viro 	br_read_unlock(vfsmount_lock);
2570afac7cbaSAl Viro 	return res;
2571afac7cbaSAl Viro }
2572afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under);
2573afac7cbaSAl Viro 
2574afac7cbaSAl Viro /*
25751da177e4SLinus Torvalds  * pivot_root Semantics:
25761da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
25771da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
25781da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
25791da177e4SLinus Torvalds  *
25801da177e4SLinus Torvalds  * Restrictions:
25811da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
25821da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
25831da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
25841da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
25851da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
25861da177e4SLinus Torvalds  *
25874a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
25884a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
25894a0d11faSNeil Brown  * in this situation.
25904a0d11faSNeil Brown  *
25911da177e4SLinus Torvalds  * Notes:
25921da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
25931da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
25941da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
25951da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
25961da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
25971da177e4SLinus Torvalds  *    first.
25981da177e4SLinus Torvalds  */
25993480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
26003480b257SHeiko Carstens 		const char __user *, put_old)
26011da177e4SLinus Torvalds {
26022d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
2603419148daSAl Viro 	struct mount *new_mnt, *root_mnt;
26041da177e4SLinus Torvalds 	int error;
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
26071da177e4SLinus Torvalds 		return -EPERM;
26081da177e4SLinus Torvalds 
26092d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
26101da177e4SLinus Torvalds 	if (error)
26111da177e4SLinus Torvalds 		goto out0;
26121da177e4SLinus Torvalds 
26132d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
26141da177e4SLinus Torvalds 	if (error)
26151da177e4SLinus Torvalds 		goto out1;
26161da177e4SLinus Torvalds 
26172d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
2618b12cea91SAl Viro 	if (error)
2619b12cea91SAl Viro 		goto out2;
26201da177e4SLinus Torvalds 
2621f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
2622b12cea91SAl Viro 	error = lock_mount(&old);
2623b12cea91SAl Viro 	if (error)
2624b12cea91SAl Viro 		goto out3;
2625b12cea91SAl Viro 
26261da177e4SLinus Torvalds 	error = -EINVAL;
2627419148daSAl Viro 	new_mnt = real_mount(new.mnt);
2628419148daSAl Viro 	root_mnt = real_mount(root.mnt);
26292d8f3038SAl Viro 	if (IS_MNT_SHARED(old.mnt) ||
26302d8f3038SAl Viro 		IS_MNT_SHARED(new.mnt->mnt_parent) ||
26318c3ee42eSAl Viro 		IS_MNT_SHARED(root.mnt->mnt_parent))
2632b12cea91SAl Viro 		goto out4;
263327cb1572SAl Viro 	if (!check_mnt(root.mnt) || !check_mnt(new.mnt))
2634b12cea91SAl Viro 		goto out4;
26351da177e4SLinus Torvalds 	error = -ENOENT;
2636f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
2637b12cea91SAl Viro 		goto out4;
2638f3da392eSAlexey Dobriyan 	if (d_unlinked(old.dentry))
2639b12cea91SAl Viro 		goto out4;
26401da177e4SLinus Torvalds 	error = -EBUSY;
26412d8f3038SAl Viro 	if (new.mnt == root.mnt ||
26422d8f3038SAl Viro 	    old.mnt == root.mnt)
2643b12cea91SAl Viro 		goto out4; /* loop, on the same file system  */
26441da177e4SLinus Torvalds 	error = -EINVAL;
26458c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
2646b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2647b2dba1afSAl Viro 	if (!mnt_has_parent(root.mnt))
2648b12cea91SAl Viro 		goto out4; /* not attached */
26492d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
2650b12cea91SAl Viro 		goto out4; /* not a mountpoint */
2651b2dba1afSAl Viro 	if (!mnt_has_parent(new.mnt))
2652b12cea91SAl Viro 		goto out4; /* not attached */
26534ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
2654afac7cbaSAl Viro 	if (!is_path_reachable(old.mnt, old.dentry, &new))
2655b12cea91SAl Viro 		goto out4;
265627cb1572SAl Viro 	br_write_lock(vfsmount_lock);
2657419148daSAl Viro 	detach_mnt(new_mnt, &parent_path);
2658419148daSAl Viro 	detach_mnt(root_mnt, &root_parent);
26594ac91378SJan Blunck 	/* mount old root on put_old */
2660419148daSAl Viro 	attach_mnt(root_mnt, &old);
26614ac91378SJan Blunck 	/* mount new_root on / */
2662419148daSAl Viro 	attach_mnt(new_mnt, &root_parent);
26636b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
266499b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
26652d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
26661da177e4SLinus Torvalds 	error = 0;
2667b12cea91SAl Viro out4:
2668b12cea91SAl Viro 	unlock_mount(&old);
2669b12cea91SAl Viro 	if (!error) {
26701a390689SAl Viro 		path_put(&root_parent);
26711a390689SAl Viro 		path_put(&parent_path);
2672b12cea91SAl Viro 	}
2673b12cea91SAl Viro out3:
26748c3ee42eSAl Viro 	path_put(&root);
2675b12cea91SAl Viro out2:
26762d8f3038SAl Viro 	path_put(&old);
26771da177e4SLinus Torvalds out1:
26782d8f3038SAl Viro 	path_put(&new);
26791da177e4SLinus Torvalds out0:
26801da177e4SLinus Torvalds 	return error;
26811da177e4SLinus Torvalds }
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds static void __init init_mount_tree(void)
26841da177e4SLinus Torvalds {
26851da177e4SLinus Torvalds 	struct vfsmount *mnt;
26866b3286edSKirill Korotaev 	struct mnt_namespace *ns;
2687ac748a09SJan Blunck 	struct path root;
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds 	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
26901da177e4SLinus Torvalds 	if (IS_ERR(mnt))
26911da177e4SLinus Torvalds 		panic("Can't create rootfs");
2692b3e19d92SNick Piggin 
26933b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
26943b22edc5STrond Myklebust 	if (IS_ERR(ns))
26951da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
26961da177e4SLinus Torvalds 
26976b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
26986b3286edSKirill Korotaev 	get_mnt_ns(ns);
26991da177e4SLinus Torvalds 
2700ac748a09SJan Blunck 	root.mnt = ns->root;
2701ac748a09SJan Blunck 	root.dentry = ns->root->mnt_root;
2702ac748a09SJan Blunck 
2703ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
2704ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
270774bf17cfSDenis Cheng void __init mnt_init(void)
27081da177e4SLinus Torvalds {
270913f14b4dSEric Dumazet 	unsigned u;
271015a67dd8SRandy Dunlap 	int err;
27111da177e4SLinus Torvalds 
2712390c6843SRam Pai 	init_rwsem(&namespace_sem);
2713390c6843SRam Pai 
27147d6fec45SAl Viro 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
271520c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
27161da177e4SLinus Torvalds 
2717b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	if (!mount_hashtable)
27201da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
27211da177e4SLinus Torvalds 
272280cdc6daSMandeep Singh Baines 	printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
27231da177e4SLinus Torvalds 
272413f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
272513f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
27261da177e4SLinus Torvalds 
272799b7db7bSNick Piggin 	br_lock_init(vfsmount_lock);
272899b7db7bSNick Piggin 
272915a67dd8SRandy Dunlap 	err = sysfs_init();
273015a67dd8SRandy Dunlap 	if (err)
273115a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
27328e24eea7SHarvey Harrison 			__func__, err);
273300d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
273400d26666SGreg Kroah-Hartman 	if (!fs_kobj)
27358e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
27361da177e4SLinus Torvalds 	init_rootfs();
27371da177e4SLinus Torvalds 	init_mount_tree();
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds 
2740616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
27411da177e4SLinus Torvalds {
274270fbcdf4SRam Pai 	LIST_HEAD(umount_list);
2743616511d0STrond Myklebust 
2744d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
2745616511d0STrond Myklebust 		return;
2746390c6843SRam Pai 	down_write(&namespace_sem);
274799b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
2748d498b25aSAl Viro 	umount_tree(ns->root, 0, &umount_list);
274999b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
2750390c6843SRam Pai 	up_write(&namespace_sem);
275170fbcdf4SRam Pai 	release_mounts(&umount_list);
27526b3286edSKirill Korotaev 	kfree(ns);
27531da177e4SLinus Torvalds }
27549d412a43SAl Viro 
27559d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
27569d412a43SAl Viro {
2757423e0ab0STim Chen 	struct vfsmount *mnt;
2758423e0ab0STim Chen 	mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
2759423e0ab0STim Chen 	if (!IS_ERR(mnt)) {
2760423e0ab0STim Chen 		/*
2761423e0ab0STim Chen 		 * it is a longterm mount, don't release mnt until
2762423e0ab0STim Chen 		 * we unmount before file sys is unregistered
2763423e0ab0STim Chen 		*/
2764423e0ab0STim Chen 		mnt_make_longterm(mnt);
2765423e0ab0STim Chen 	}
2766423e0ab0STim Chen 	return mnt;
27679d412a43SAl Viro }
27689d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data);
2769423e0ab0STim Chen 
2770423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt)
2771423e0ab0STim Chen {
2772423e0ab0STim Chen 	/* release long term mount so mount point can be released */
2773423e0ab0STim Chen 	if (!IS_ERR_OR_NULL(mnt)) {
2774423e0ab0STim Chen 		mnt_make_shortterm(mnt);
2775423e0ab0STim Chen 		mntput(mnt);
2776423e0ab0STim Chen 	}
2777423e0ab0STim Chen }
2778423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount);
277902125a82SAl Viro 
278002125a82SAl Viro bool our_mnt(struct vfsmount *mnt)
278102125a82SAl Viro {
278202125a82SAl Viro 	return check_mnt(mnt);
278302125a82SAl Viro }
2784