xref: /openbmc/linux/fs/namespace.c (revision cf8d2c11)
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>
141da177e4SLinus Torvalds #include <linux/smp_lock.h>
151da177e4SLinus Torvalds #include <linux/init.h>
1615a67dd8SRandy Dunlap #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/acct.h>
1816f7e0feSRandy Dunlap #include <linux/capability.h>
193d733633SDave Hansen #include <linux/cpumask.h>
201da177e4SLinus Torvalds #include <linux/module.h>
21f20a9eadSAndrew Morton #include <linux/sysfs.h>
221da177e4SLinus Torvalds #include <linux/seq_file.h>
236b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
241da177e4SLinus Torvalds #include <linux/namei.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <linux/mount.h>
2707f3f05cSDavid Howells #include <linux/ramfs.h>
2813f14b4dSEric Dumazet #include <linux/log2.h>
2973cd49ecSMiklos Szeredi #include <linux/idr.h>
305ad4e53bSAl Viro #include <linux/fs_struct.h>
311da177e4SLinus Torvalds #include <asm/uaccess.h>
321da177e4SLinus Torvalds #include <asm/unistd.h>
3307b20889SRam Pai #include "pnode.h"
34948730b0SAdrian Bunk #include "internal.h"
351da177e4SLinus Torvalds 
3613f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
3713f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT)
3813f14b4dSEric Dumazet 
391da177e4SLinus Torvalds /* spinlock for vfsmount related operations, inplace of dcache_lock */
401da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
411da177e4SLinus Torvalds 
425addc5ddSAl Viro static int event;
4373cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
44719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
455addc5ddSAl Viro 
46fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly;
47e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
48390c6843SRam Pai static struct rw_semaphore namespace_sem;
491da177e4SLinus Torvalds 
50f87fd4c2SMiklos Szeredi /* /sys/fs */
5100d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
5200d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
53f87fd4c2SMiklos Szeredi 
541da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
571da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
5813f14b4dSEric Dumazet 	tmp = tmp + (tmp >> HASH_SHIFT);
5913f14b4dSEric Dumazet 	return tmp & (HASH_SIZE - 1);
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
623d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
633d733633SDave Hansen 
6473cd49ecSMiklos Szeredi /* allocation is serialized by namespace_sem */
6573cd49ecSMiklos Szeredi static int mnt_alloc_id(struct vfsmount *mnt)
6673cd49ecSMiklos Szeredi {
6773cd49ecSMiklos Szeredi 	int res;
6873cd49ecSMiklos Szeredi 
6973cd49ecSMiklos Szeredi retry:
7073cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
7173cd49ecSMiklos Szeredi 	spin_lock(&vfsmount_lock);
7273cd49ecSMiklos Szeredi 	res = ida_get_new(&mnt_id_ida, &mnt->mnt_id);
7373cd49ecSMiklos Szeredi 	spin_unlock(&vfsmount_lock);
7473cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
7573cd49ecSMiklos Szeredi 		goto retry;
7673cd49ecSMiklos Szeredi 
7773cd49ecSMiklos Szeredi 	return res;
7873cd49ecSMiklos Szeredi }
7973cd49ecSMiklos Szeredi 
8073cd49ecSMiklos Szeredi static void mnt_free_id(struct vfsmount *mnt)
8173cd49ecSMiklos Szeredi {
8273cd49ecSMiklos Szeredi 	spin_lock(&vfsmount_lock);
8373cd49ecSMiklos Szeredi 	ida_remove(&mnt_id_ida, mnt->mnt_id);
8473cd49ecSMiklos Szeredi 	spin_unlock(&vfsmount_lock);
8573cd49ecSMiklos Szeredi }
8673cd49ecSMiklos Szeredi 
87719f5d7fSMiklos Szeredi /*
88719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
89719f5d7fSMiklos Szeredi  *
90719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
91719f5d7fSMiklos Szeredi  */
92719f5d7fSMiklos Szeredi static int mnt_alloc_group_id(struct vfsmount *mnt)
93719f5d7fSMiklos Szeredi {
94719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
95719f5d7fSMiklos Szeredi 		return -ENOMEM;
96719f5d7fSMiklos Szeredi 
97719f5d7fSMiklos Szeredi 	return ida_get_new_above(&mnt_group_ida, 1, &mnt->mnt_group_id);
98719f5d7fSMiklos Szeredi }
99719f5d7fSMiklos Szeredi 
100719f5d7fSMiklos Szeredi /*
101719f5d7fSMiklos Szeredi  * Release a peer group ID
102719f5d7fSMiklos Szeredi  */
103719f5d7fSMiklos Szeredi void mnt_release_group_id(struct vfsmount *mnt)
104719f5d7fSMiklos Szeredi {
105719f5d7fSMiklos Szeredi 	ida_remove(&mnt_group_ida, mnt->mnt_group_id);
106719f5d7fSMiklos Szeredi 	mnt->mnt_group_id = 0;
107719f5d7fSMiklos Szeredi }
108719f5d7fSMiklos Szeredi 
1091da177e4SLinus Torvalds struct vfsmount *alloc_vfsmnt(const char *name)
1101da177e4SLinus Torvalds {
111c3762229SRobert P. J. Day 	struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
1121da177e4SLinus Torvalds 	if (mnt) {
11373cd49ecSMiklos Szeredi 		int err;
11473cd49ecSMiklos Szeredi 
11573cd49ecSMiklos Szeredi 		err = mnt_alloc_id(mnt);
11688b38782SLi Zefan 		if (err)
11788b38782SLi Zefan 			goto out_free_cache;
11888b38782SLi Zefan 
11988b38782SLi Zefan 		if (name) {
12088b38782SLi Zefan 			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
12188b38782SLi Zefan 			if (!mnt->mnt_devname)
12288b38782SLi Zefan 				goto out_free_id;
12373cd49ecSMiklos Szeredi 		}
12473cd49ecSMiklos Szeredi 
1251da177e4SLinus Torvalds 		atomic_set(&mnt->mnt_count, 1);
1261da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_hash);
1271da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_child);
1281da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_mounts);
1291da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_list);
13055e700b9SMiklos Szeredi 		INIT_LIST_HEAD(&mnt->mnt_expire);
13103e06e68SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_share);
132a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
133a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave);
134d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
135d3ef3d73Snpiggin@suse.de 		mnt->mnt_writers = alloc_percpu(int);
136d3ef3d73Snpiggin@suse.de 		if (!mnt->mnt_writers)
137d3ef3d73Snpiggin@suse.de 			goto out_free_devname;
138d3ef3d73Snpiggin@suse.de #else
139d3ef3d73Snpiggin@suse.de 		mnt->mnt_writers = 0;
140d3ef3d73Snpiggin@suse.de #endif
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds 	return mnt;
14388b38782SLi Zefan 
144d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
145d3ef3d73Snpiggin@suse.de out_free_devname:
146d3ef3d73Snpiggin@suse.de 	kfree(mnt->mnt_devname);
147d3ef3d73Snpiggin@suse.de #endif
14888b38782SLi Zefan out_free_id:
14988b38782SLi Zefan 	mnt_free_id(mnt);
15088b38782SLi Zefan out_free_cache:
15188b38782SLi Zefan 	kmem_cache_free(mnt_cache, mnt);
15288b38782SLi Zefan 	return NULL;
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1558366025eSDave Hansen /*
1568366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
1578366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
1588366025eSDave Hansen  * We must keep track of when those operations start
1598366025eSDave Hansen  * (for permission checks) and when they end, so that
1608366025eSDave Hansen  * we can determine when writes are able to occur to
1618366025eSDave Hansen  * a filesystem.
1628366025eSDave Hansen  */
1633d733633SDave Hansen /*
1643d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
1653d733633SDave Hansen  * @mnt: the mount to check for its write status
1663d733633SDave Hansen  *
1673d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
1683d733633SDave Hansen  * It does not guarantee that the filesystem will stay
1693d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
1703d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
1713d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
1723d733633SDave Hansen  * r/w.
1733d733633SDave Hansen  */
1743d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
1753d733633SDave Hansen {
1762e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
1772e4b7fcdSDave Hansen 		return 1;
1782e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
1792e4b7fcdSDave Hansen 		return 1;
1802e4b7fcdSDave Hansen 	return 0;
1813d733633SDave Hansen }
1823d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
1833d733633SDave Hansen 
184d3ef3d73Snpiggin@suse.de static inline void inc_mnt_writers(struct vfsmount *mnt)
1853d733633SDave Hansen {
186d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
187d3ef3d73Snpiggin@suse.de 	(*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))++;
188d3ef3d73Snpiggin@suse.de #else
189d3ef3d73Snpiggin@suse.de 	mnt->mnt_writers++;
190d3ef3d73Snpiggin@suse.de #endif
1913d733633SDave Hansen }
1923d733633SDave Hansen 
193d3ef3d73Snpiggin@suse.de static inline void dec_mnt_writers(struct vfsmount *mnt)
1943d733633SDave Hansen {
195d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
196d3ef3d73Snpiggin@suse.de 	(*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))--;
197d3ef3d73Snpiggin@suse.de #else
198d3ef3d73Snpiggin@suse.de 	mnt->mnt_writers--;
199d3ef3d73Snpiggin@suse.de #endif
200d3ef3d73Snpiggin@suse.de }
201d3ef3d73Snpiggin@suse.de 
202d3ef3d73Snpiggin@suse.de static unsigned int count_mnt_writers(struct vfsmount *mnt)
203d3ef3d73Snpiggin@suse.de {
204d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
205d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2063d733633SDave Hansen 	int cpu;
2073d733633SDave Hansen 
2083d733633SDave Hansen 	for_each_possible_cpu(cpu) {
209d3ef3d73Snpiggin@suse.de 		count += *per_cpu_ptr(mnt->mnt_writers, cpu);
2103d733633SDave Hansen 	}
2113d733633SDave Hansen 
212d3ef3d73Snpiggin@suse.de 	return count;
213d3ef3d73Snpiggin@suse.de #else
214d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
215d3ef3d73Snpiggin@suse.de #endif
2163d733633SDave Hansen }
2173d733633SDave Hansen 
2183d733633SDave Hansen /*
2193d733633SDave Hansen  * Most r/o checks on a fs are for operations that take
2203d733633SDave Hansen  * discrete amounts of time, like a write() or unlink().
2213d733633SDave Hansen  * We must keep track of when those operations start
2223d733633SDave Hansen  * (for permission checks) and when they end, so that
2233d733633SDave Hansen  * we can determine when writes are able to occur to
2243d733633SDave Hansen  * a filesystem.
2253d733633SDave Hansen  */
2268366025eSDave Hansen /**
2278366025eSDave Hansen  * mnt_want_write - get write access to a mount
2288366025eSDave Hansen  * @mnt: the mount on which to take a write
2298366025eSDave Hansen  *
2308366025eSDave Hansen  * This tells the low-level filesystem that a write is
2318366025eSDave Hansen  * about to be performed to it, and makes sure that
2328366025eSDave Hansen  * writes are allowed before returning success.  When
2338366025eSDave Hansen  * the write operation is finished, mnt_drop_write()
2348366025eSDave Hansen  * must be called.  This is effectively a refcount.
2358366025eSDave Hansen  */
2368366025eSDave Hansen int mnt_want_write(struct vfsmount *mnt)
2378366025eSDave Hansen {
2383d733633SDave Hansen 	int ret = 0;
2393d733633SDave Hansen 
240d3ef3d73Snpiggin@suse.de 	preempt_disable();
241d3ef3d73Snpiggin@suse.de 	inc_mnt_writers(mnt);
242d3ef3d73Snpiggin@suse.de 	/*
243d3ef3d73Snpiggin@suse.de 	 * The store to inc_mnt_writers must be visible before we pass
244d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
245d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
246d3ef3d73Snpiggin@suse.de 	 */
247d3ef3d73Snpiggin@suse.de 	smp_mb();
248d3ef3d73Snpiggin@suse.de 	while (mnt->mnt_flags & MNT_WRITE_HOLD)
249d3ef3d73Snpiggin@suse.de 		cpu_relax();
250d3ef3d73Snpiggin@suse.de 	/*
251d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
252d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
253d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
254d3ef3d73Snpiggin@suse.de 	 */
255d3ef3d73Snpiggin@suse.de 	smp_rmb();
2563d733633SDave Hansen 	if (__mnt_is_readonly(mnt)) {
257d3ef3d73Snpiggin@suse.de 		dec_mnt_writers(mnt);
2583d733633SDave Hansen 		ret = -EROFS;
2593d733633SDave Hansen 		goto out;
2603d733633SDave Hansen 	}
2613d733633SDave Hansen out:
262d3ef3d73Snpiggin@suse.de 	preempt_enable();
2633d733633SDave Hansen 	return ret;
2648366025eSDave Hansen }
2658366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
2668366025eSDave Hansen 
2678366025eSDave Hansen /**
26896029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
26996029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
27096029c4eSnpiggin@suse.de  *
27196029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
27296029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
27396029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
27496029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
27596029c4eSnpiggin@suse.de  *
27696029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
27796029c4eSnpiggin@suse.de  * drop the reference.
27896029c4eSnpiggin@suse.de  */
27996029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
28096029c4eSnpiggin@suse.de {
28196029c4eSnpiggin@suse.de 	/* superblock may be r/o */
28296029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
28396029c4eSnpiggin@suse.de 		return -EROFS;
28496029c4eSnpiggin@suse.de 	preempt_disable();
28596029c4eSnpiggin@suse.de 	inc_mnt_writers(mnt);
28696029c4eSnpiggin@suse.de 	preempt_enable();
28796029c4eSnpiggin@suse.de 	return 0;
28896029c4eSnpiggin@suse.de }
28996029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
29096029c4eSnpiggin@suse.de 
29196029c4eSnpiggin@suse.de /**
29296029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
29396029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
29496029c4eSnpiggin@suse.de  *
29596029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
29696029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
29796029c4eSnpiggin@suse.de  */
29896029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
29996029c4eSnpiggin@suse.de {
30096029c4eSnpiggin@suse.de 	if (!(file->f_mode & FMODE_WRITE))
30196029c4eSnpiggin@suse.de 		return mnt_want_write(file->f_path.mnt);
30296029c4eSnpiggin@suse.de 	else
30396029c4eSnpiggin@suse.de 		return mnt_clone_write(file->f_path.mnt);
30496029c4eSnpiggin@suse.de }
30596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
30696029c4eSnpiggin@suse.de 
30796029c4eSnpiggin@suse.de /**
3088366025eSDave Hansen  * mnt_drop_write - give up write access to a mount
3098366025eSDave Hansen  * @mnt: the mount on which to give up write access
3108366025eSDave Hansen  *
3118366025eSDave Hansen  * Tells the low-level filesystem that we are done
3128366025eSDave Hansen  * performing writes to it.  Must be matched with
3138366025eSDave Hansen  * mnt_want_write() call above.
3148366025eSDave Hansen  */
3158366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt)
3168366025eSDave Hansen {
317d3ef3d73Snpiggin@suse.de 	preempt_disable();
318d3ef3d73Snpiggin@suse.de 	dec_mnt_writers(mnt);
319d3ef3d73Snpiggin@suse.de 	preempt_enable();
3208366025eSDave Hansen }
3218366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
3228366025eSDave Hansen 
3232e4b7fcdSDave Hansen static int mnt_make_readonly(struct vfsmount *mnt)
3248366025eSDave Hansen {
3253d733633SDave Hansen 	int ret = 0;
3263d733633SDave Hansen 
3272e4b7fcdSDave Hansen 	spin_lock(&vfsmount_lock);
328d3ef3d73Snpiggin@suse.de 	mnt->mnt_flags |= MNT_WRITE_HOLD;
329d3ef3d73Snpiggin@suse.de 	/*
330d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
331d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
332d3ef3d73Snpiggin@suse.de 	 */
333d3ef3d73Snpiggin@suse.de 	smp_mb();
334d3ef3d73Snpiggin@suse.de 
335d3ef3d73Snpiggin@suse.de 	/*
336d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
337d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
338d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
339d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
340d3ef3d73Snpiggin@suse.de 	 *
341d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
342d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
343d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
344d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
345d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
346d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
347d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
348d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
349d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
350d3ef3d73Snpiggin@suse.de 	 */
351d3ef3d73Snpiggin@suse.de 	if (count_mnt_writers(mnt) > 0)
352d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
353d3ef3d73Snpiggin@suse.de 	else
3542e4b7fcdSDave Hansen 		mnt->mnt_flags |= MNT_READONLY;
355d3ef3d73Snpiggin@suse.de 	/*
356d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
357d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
358d3ef3d73Snpiggin@suse.de 	 */
359d3ef3d73Snpiggin@suse.de 	smp_wmb();
360d3ef3d73Snpiggin@suse.de 	mnt->mnt_flags &= ~MNT_WRITE_HOLD;
3612e4b7fcdSDave Hansen 	spin_unlock(&vfsmount_lock);
3623d733633SDave Hansen 	return ret;
3633d733633SDave Hansen }
3648366025eSDave Hansen 
3652e4b7fcdSDave Hansen static void __mnt_unmake_readonly(struct vfsmount *mnt)
3662e4b7fcdSDave Hansen {
3672e4b7fcdSDave Hansen 	spin_lock(&vfsmount_lock);
3682e4b7fcdSDave Hansen 	mnt->mnt_flags &= ~MNT_READONLY;
3692e4b7fcdSDave Hansen 	spin_unlock(&vfsmount_lock);
3702e4b7fcdSDave Hansen }
3712e4b7fcdSDave Hansen 
372a3ec947cSSukadev Bhattiprolu void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
373454e2398SDavid Howells {
374454e2398SDavid Howells 	mnt->mnt_sb = sb;
375454e2398SDavid Howells 	mnt->mnt_root = dget(sb->s_root);
376454e2398SDavid Howells }
377454e2398SDavid Howells 
378454e2398SDavid Howells EXPORT_SYMBOL(simple_set_mnt);
379454e2398SDavid Howells 
3801da177e4SLinus Torvalds void free_vfsmnt(struct vfsmount *mnt)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	kfree(mnt->mnt_devname);
38373cd49ecSMiklos Szeredi 	mnt_free_id(mnt);
384d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
385d3ef3d73Snpiggin@suse.de 	free_percpu(mnt->mnt_writers);
386d3ef3d73Snpiggin@suse.de #endif
3871da177e4SLinus Torvalds 	kmem_cache_free(mnt_cache, mnt);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*
391a05964f3SRam Pai  * find the first or last mount at @dentry on vfsmount @mnt depending on
392a05964f3SRam Pai  * @dir. If @dir is set return the first mount else return the last mount.
3931da177e4SLinus Torvalds  */
394a05964f3SRam Pai struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
395a05964f3SRam Pai 			      int dir)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
3981da177e4SLinus Torvalds 	struct list_head *tmp = head;
3991da177e4SLinus Torvalds 	struct vfsmount *p, *found = NULL;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	for (;;) {
402a05964f3SRam Pai 		tmp = dir ? tmp->next : tmp->prev;
4031da177e4SLinus Torvalds 		p = NULL;
4041da177e4SLinus Torvalds 		if (tmp == head)
4051da177e4SLinus Torvalds 			break;
4061da177e4SLinus Torvalds 		p = list_entry(tmp, struct vfsmount, mnt_hash);
4071da177e4SLinus Torvalds 		if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
408a05964f3SRam Pai 			found = p;
4091da177e4SLinus Torvalds 			break;
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 	return found;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
415a05964f3SRam Pai /*
416a05964f3SRam Pai  * lookup_mnt increments the ref count before returning
417a05964f3SRam Pai  * the vfsmount struct.
418a05964f3SRam Pai  */
4191c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
420a05964f3SRam Pai {
421a05964f3SRam Pai 	struct vfsmount *child_mnt;
422a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
4231c755af4SAl Viro 	if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1)))
424a05964f3SRam Pai 		mntget(child_mnt);
425a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
426a05964f3SRam Pai 	return child_mnt;
427a05964f3SRam Pai }
428a05964f3SRam Pai 
4291da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt)
4301da177e4SLinus Torvalds {
4316b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4346b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
4355addc5ddSAl Viro {
4365addc5ddSAl Viro 	if (ns) {
4375addc5ddSAl Viro 		ns->event = ++event;
4385addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
4395addc5ddSAl Viro 	}
4405addc5ddSAl Viro }
4415addc5ddSAl Viro 
4426b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
4435addc5ddSAl Viro {
4445addc5ddSAl Viro 	if (ns && ns->event != event) {
4455addc5ddSAl Viro 		ns->event = event;
4465addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
4475addc5ddSAl Viro 	}
4485addc5ddSAl Viro }
4495addc5ddSAl Viro 
4501a390689SAl Viro static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
4511da177e4SLinus Torvalds {
4521a390689SAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
4531a390689SAl Viro 	old_path->mnt = mnt->mnt_parent;
4541da177e4SLinus Torvalds 	mnt->mnt_parent = mnt;
4551da177e4SLinus Torvalds 	mnt->mnt_mountpoint = mnt->mnt_root;
4561da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_child);
4571da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_hash);
4581a390689SAl Viro 	old_path->dentry->d_mounted--;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
461b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
462b90fa9aeSRam Pai 			struct vfsmount *child_mnt)
463b90fa9aeSRam Pai {
464b90fa9aeSRam Pai 	child_mnt->mnt_parent = mntget(mnt);
465b90fa9aeSRam Pai 	child_mnt->mnt_mountpoint = dget(dentry);
466b90fa9aeSRam Pai 	dentry->d_mounted++;
467b90fa9aeSRam Pai }
468b90fa9aeSRam Pai 
4691a390689SAl Viro static void attach_mnt(struct vfsmount *mnt, struct path *path)
4701da177e4SLinus Torvalds {
4711a390689SAl Viro 	mnt_set_mountpoint(path->mnt, path->dentry, mnt);
472b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
4731a390689SAl Viro 			hash(path->mnt, path->dentry));
4741a390689SAl Viro 	list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
475b90fa9aeSRam Pai }
476b90fa9aeSRam Pai 
477b90fa9aeSRam Pai /*
478b90fa9aeSRam Pai  * the caller must hold vfsmount_lock
479b90fa9aeSRam Pai  */
480b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt)
481b90fa9aeSRam Pai {
482b90fa9aeSRam Pai 	struct vfsmount *parent = mnt->mnt_parent;
483b90fa9aeSRam Pai 	struct vfsmount *m;
484b90fa9aeSRam Pai 	LIST_HEAD(head);
4856b3286edSKirill Korotaev 	struct mnt_namespace *n = parent->mnt_ns;
486b90fa9aeSRam Pai 
487b90fa9aeSRam Pai 	BUG_ON(parent == mnt);
488b90fa9aeSRam Pai 
489b90fa9aeSRam Pai 	list_add_tail(&head, &mnt->mnt_list);
490b90fa9aeSRam Pai 	list_for_each_entry(m, &head, mnt_list)
4916b3286edSKirill Korotaev 		m->mnt_ns = n;
492b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
493b90fa9aeSRam Pai 
494b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
495b90fa9aeSRam Pai 				hash(parent, mnt->mnt_mountpoint));
496b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
4976b3286edSKirill Korotaev 	touch_mnt_namespace(n);
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	struct list_head *next = p->mnt_mounts.next;
5031da177e4SLinus Torvalds 	if (next == &p->mnt_mounts) {
5041da177e4SLinus Torvalds 		while (1) {
5051da177e4SLinus Torvalds 			if (p == root)
5061da177e4SLinus Torvalds 				return NULL;
5071da177e4SLinus Torvalds 			next = p->mnt_child.next;
5081da177e4SLinus Torvalds 			if (next != &p->mnt_parent->mnt_mounts)
5091da177e4SLinus Torvalds 				break;
5101da177e4SLinus Torvalds 			p = p->mnt_parent;
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 	return list_entry(next, struct vfsmount, mnt_child);
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5169676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
5179676f0c6SRam Pai {
5189676f0c6SRam Pai 	struct list_head *prev = p->mnt_mounts.prev;
5199676f0c6SRam Pai 	while (prev != &p->mnt_mounts) {
5209676f0c6SRam Pai 		p = list_entry(prev, struct vfsmount, mnt_child);
5219676f0c6SRam Pai 		prev = p->mnt_mounts.prev;
5229676f0c6SRam Pai 	}
5239676f0c6SRam Pai 	return p;
5249676f0c6SRam Pai }
5259676f0c6SRam Pai 
52636341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
52736341f64SRam Pai 					int flag)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct super_block *sb = old->mnt_sb;
5301da177e4SLinus Torvalds 	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	if (mnt) {
533719f5d7fSMiklos Szeredi 		if (flag & (CL_SLAVE | CL_PRIVATE))
534719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = 0; /* not a peer of original */
535719f5d7fSMiklos Szeredi 		else
536719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = old->mnt_group_id;
537719f5d7fSMiklos Szeredi 
538719f5d7fSMiklos Szeredi 		if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
539719f5d7fSMiklos Szeredi 			int err = mnt_alloc_group_id(mnt);
540719f5d7fSMiklos Szeredi 			if (err)
541719f5d7fSMiklos Szeredi 				goto out_free;
542719f5d7fSMiklos Szeredi 		}
543719f5d7fSMiklos Szeredi 
5441da177e4SLinus Torvalds 		mnt->mnt_flags = old->mnt_flags;
5451da177e4SLinus Torvalds 		atomic_inc(&sb->s_active);
5461da177e4SLinus Torvalds 		mnt->mnt_sb = sb;
5471da177e4SLinus Torvalds 		mnt->mnt_root = dget(root);
5481da177e4SLinus Torvalds 		mnt->mnt_mountpoint = mnt->mnt_root;
5491da177e4SLinus Torvalds 		mnt->mnt_parent = mnt;
550b90fa9aeSRam Pai 
5515afe0022SRam Pai 		if (flag & CL_SLAVE) {
5525afe0022SRam Pai 			list_add(&mnt->mnt_slave, &old->mnt_slave_list);
5535afe0022SRam Pai 			mnt->mnt_master = old;
5545afe0022SRam Pai 			CLEAR_MNT_SHARED(mnt);
5558aec0809SAl Viro 		} else if (!(flag & CL_PRIVATE)) {
556b90fa9aeSRam Pai 			if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
557b90fa9aeSRam Pai 				list_add(&mnt->mnt_share, &old->mnt_share);
5585afe0022SRam Pai 			if (IS_MNT_SLAVE(old))
5595afe0022SRam Pai 				list_add(&mnt->mnt_slave, &old->mnt_slave);
5605afe0022SRam Pai 			mnt->mnt_master = old->mnt_master;
5615afe0022SRam Pai 		}
562b90fa9aeSRam Pai 		if (flag & CL_MAKE_SHARED)
563b90fa9aeSRam Pai 			set_mnt_shared(mnt);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 		/* stick the duplicate mount on the same expiry list
5661da177e4SLinus Torvalds 		 * as the original if that was on one */
56736341f64SRam Pai 		if (flag & CL_EXPIRE) {
56855e700b9SMiklos Szeredi 			if (!list_empty(&old->mnt_expire))
56955e700b9SMiklos Szeredi 				list_add(&mnt->mnt_expire, &old->mnt_expire);
5701da177e4SLinus Torvalds 		}
57136341f64SRam Pai 	}
5721da177e4SLinus Torvalds 	return mnt;
573719f5d7fSMiklos Szeredi 
574719f5d7fSMiklos Szeredi  out_free:
575719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
576719f5d7fSMiklos Szeredi 	return NULL;
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5797b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
5823d733633SDave Hansen 	/*
5833d733633SDave Hansen 	 * This probably indicates that somebody messed
5843d733633SDave Hansen 	 * up a mnt_want/drop_write() pair.  If this
5853d733633SDave Hansen 	 * happens, the filesystem was probably unable
5863d733633SDave Hansen 	 * to make r/w->r/o transitions.
5873d733633SDave Hansen 	 */
588d3ef3d73Snpiggin@suse.de 	/*
589d3ef3d73Snpiggin@suse.de 	 * atomic_dec_and_lock() used to deal with ->mnt_count decrements
590d3ef3d73Snpiggin@suse.de 	 * provides barriers, so count_mnt_writers() below is safe.  AV
591d3ef3d73Snpiggin@suse.de 	 */
592d3ef3d73Snpiggin@suse.de 	WARN_ON(count_mnt_writers(mnt));
5931da177e4SLinus Torvalds 	dput(mnt->mnt_root);
5941da177e4SLinus Torvalds 	free_vfsmnt(mnt);
5951da177e4SLinus Torvalds 	deactivate_super(sb);
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
5987b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt)
5997b7b1aceSAl Viro {
6007b7b1aceSAl Viro repeat:
6017b7b1aceSAl Viro 	if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
6027b7b1aceSAl Viro 		if (likely(!mnt->mnt_pinned)) {
6037b7b1aceSAl Viro 			spin_unlock(&vfsmount_lock);
6047b7b1aceSAl Viro 			__mntput(mnt);
6057b7b1aceSAl Viro 			return;
6067b7b1aceSAl Viro 		}
6077b7b1aceSAl Viro 		atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
6087b7b1aceSAl Viro 		mnt->mnt_pinned = 0;
6097b7b1aceSAl Viro 		spin_unlock(&vfsmount_lock);
6107b7b1aceSAl Viro 		acct_auto_close_mnt(mnt);
6117b7b1aceSAl Viro 		security_sb_umount_close(mnt);
6127b7b1aceSAl Viro 		goto repeat;
6137b7b1aceSAl Viro 	}
6147b7b1aceSAl Viro }
6157b7b1aceSAl Viro 
6167b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire);
6177b7b1aceSAl Viro 
6187b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
6197b7b1aceSAl Viro {
6207b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
6217b7b1aceSAl Viro 	mnt->mnt_pinned++;
6227b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
6237b7b1aceSAl Viro }
6247b7b1aceSAl Viro 
6257b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
6267b7b1aceSAl Viro 
6277b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt)
6287b7b1aceSAl Viro {
6297b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
6307b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
6317b7b1aceSAl Viro 		atomic_inc(&mnt->mnt_count);
6327b7b1aceSAl Viro 		mnt->mnt_pinned--;
6337b7b1aceSAl Viro 	}
6347b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
6357b7b1aceSAl Viro }
6367b7b1aceSAl Viro 
6377b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
6381da177e4SLinus Torvalds 
639b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
640b3b304a2SMiklos Szeredi {
641b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
642b3b304a2SMiklos Szeredi }
643b3b304a2SMiklos Szeredi 
644b3b304a2SMiklos Szeredi /*
645b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
646b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
647b3b304a2SMiklos Szeredi  *
648b3b304a2SMiklos Szeredi  * See also save_mount_options().
649b3b304a2SMiklos Szeredi  */
650b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
651b3b304a2SMiklos Szeredi {
6522a32cebdSAl Viro 	const char *options;
6532a32cebdSAl Viro 
6542a32cebdSAl Viro 	rcu_read_lock();
6552a32cebdSAl Viro 	options = rcu_dereference(mnt->mnt_sb->s_options);
656b3b304a2SMiklos Szeredi 
657b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
658b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
659b3b304a2SMiklos Szeredi 		mangle(m, options);
660b3b304a2SMiklos Szeredi 	}
6612a32cebdSAl Viro 	rcu_read_unlock();
662b3b304a2SMiklos Szeredi 
663b3b304a2SMiklos Szeredi 	return 0;
664b3b304a2SMiklos Szeredi }
665b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
666b3b304a2SMiklos Szeredi 
667b3b304a2SMiklos Szeredi /*
668b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
669b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
670b3b304a2SMiklos Szeredi  *
671b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
672b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
673b3b304a2SMiklos Szeredi  * remount fails.
674b3b304a2SMiklos Szeredi  *
675b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
676b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
677b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
678b3b304a2SMiklos Szeredi  * any more.
679b3b304a2SMiklos Szeredi  */
680b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
681b3b304a2SMiklos Szeredi {
6822a32cebdSAl Viro 	BUG_ON(sb->s_options);
6832a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
684b3b304a2SMiklos Szeredi }
685b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
686b3b304a2SMiklos Szeredi 
6872a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
6882a32cebdSAl Viro {
6892a32cebdSAl Viro 	char *old = sb->s_options;
6902a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
6912a32cebdSAl Viro 	if (old) {
6922a32cebdSAl Viro 		synchronize_rcu();
6932a32cebdSAl Viro 		kfree(old);
6942a32cebdSAl Viro 	}
6952a32cebdSAl Viro }
6962a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
6972a32cebdSAl Viro 
698a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
6991da177e4SLinus Torvalds /* iterator */
7001da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
7011da177e4SLinus Torvalds {
702a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
7031da177e4SLinus Torvalds 
704390c6843SRam Pai 	down_read(&namespace_sem);
705a1a2c409SMiklos Szeredi 	return seq_list_start(&p->ns->list, *pos);
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
7091da177e4SLinus Torvalds {
710a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
711b0765fb8SPavel Emelianov 
712a1a2c409SMiklos Szeredi 	return seq_list_next(v, &p->ns->list, pos);
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
7161da177e4SLinus Torvalds {
717390c6843SRam Pai 	up_read(&namespace_sem);
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
7202d4d4864SRam Pai struct proc_fs_info {
7211da177e4SLinus Torvalds 	int flag;
7222d4d4864SRam Pai 	const char *str;
7232d4d4864SRam Pai };
7242d4d4864SRam Pai 
7252069f457SEric Paris static int show_sb_opts(struct seq_file *m, struct super_block *sb)
7262d4d4864SRam Pai {
7272d4d4864SRam Pai 	static const struct proc_fs_info fs_info[] = {
7281da177e4SLinus Torvalds 		{ MS_SYNCHRONOUS, ",sync" },
7291da177e4SLinus Torvalds 		{ MS_DIRSYNC, ",dirsync" },
7301da177e4SLinus Torvalds 		{ MS_MANDLOCK, ",mand" },
7311da177e4SLinus Torvalds 		{ 0, NULL }
7321da177e4SLinus Torvalds 	};
7332d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
7342d4d4864SRam Pai 
7352d4d4864SRam Pai 	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
7362d4d4864SRam Pai 		if (sb->s_flags & fs_infop->flag)
7372d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
7382d4d4864SRam Pai 	}
7392069f457SEric Paris 
7402069f457SEric Paris 	return security_sb_show_options(m, sb);
7412d4d4864SRam Pai }
7422d4d4864SRam Pai 
7432d4d4864SRam Pai static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
7442d4d4864SRam Pai {
7452d4d4864SRam Pai 	static const struct proc_fs_info mnt_info[] = {
7461da177e4SLinus Torvalds 		{ MNT_NOSUID, ",nosuid" },
7471da177e4SLinus Torvalds 		{ MNT_NODEV, ",nodev" },
7481da177e4SLinus Torvalds 		{ MNT_NOEXEC, ",noexec" },
749fc33a7bbSChristoph Hellwig 		{ MNT_NOATIME, ",noatime" },
750fc33a7bbSChristoph Hellwig 		{ MNT_NODIRATIME, ",nodiratime" },
75147ae32d6SValerie Henson 		{ MNT_RELATIME, ",relatime" },
752d0adde57SMatthew Garrett 		{ MNT_STRICTATIME, ",strictatime" },
7531da177e4SLinus Torvalds 		{ 0, NULL }
7541da177e4SLinus Torvalds 	};
7552d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
7562d4d4864SRam Pai 
7572d4d4864SRam Pai 	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
7582d4d4864SRam Pai 		if (mnt->mnt_flags & fs_infop->flag)
7592d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
7602d4d4864SRam Pai 	}
7612d4d4864SRam Pai }
7622d4d4864SRam Pai 
7632d4d4864SRam Pai static void show_type(struct seq_file *m, struct super_block *sb)
7642d4d4864SRam Pai {
7652d4d4864SRam Pai 	mangle(m, sb->s_type->name);
7662d4d4864SRam Pai 	if (sb->s_subtype && sb->s_subtype[0]) {
7672d4d4864SRam Pai 		seq_putc(m, '.');
7682d4d4864SRam Pai 		mangle(m, sb->s_subtype);
7692d4d4864SRam Pai 	}
7702d4d4864SRam Pai }
7712d4d4864SRam Pai 
7722d4d4864SRam Pai static int show_vfsmnt(struct seq_file *m, void *v)
7732d4d4864SRam Pai {
7742d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
7752d4d4864SRam Pai 	int err = 0;
776c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
7791da177e4SLinus Torvalds 	seq_putc(m, ' ');
780c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
7811da177e4SLinus Torvalds 	seq_putc(m, ' ');
7822d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
7832e4b7fcdSDave Hansen 	seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
7842069f457SEric Paris 	err = show_sb_opts(m, mnt->mnt_sb);
7852069f457SEric Paris 	if (err)
7862069f457SEric Paris 		goto out;
7872d4d4864SRam Pai 	show_mnt_opts(m, mnt);
7881da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_op->show_options)
7891da177e4SLinus Torvalds 		err = mnt->mnt_sb->s_op->show_options(m, mnt);
7901da177e4SLinus Torvalds 	seq_puts(m, " 0 0\n");
7912069f457SEric Paris out:
7921da177e4SLinus Torvalds 	return err;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
795a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
7961da177e4SLinus Torvalds 	.start	= m_start,
7971da177e4SLinus Torvalds 	.next	= m_next,
7981da177e4SLinus Torvalds 	.stop	= m_stop,
7991da177e4SLinus Torvalds 	.show	= show_vfsmnt
8001da177e4SLinus Torvalds };
8011da177e4SLinus Torvalds 
8022d4d4864SRam Pai static int show_mountinfo(struct seq_file *m, void *v)
8032d4d4864SRam Pai {
8042d4d4864SRam Pai 	struct proc_mounts *p = m->private;
8052d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
8062d4d4864SRam Pai 	struct super_block *sb = mnt->mnt_sb;
8072d4d4864SRam Pai 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
8082d4d4864SRam Pai 	struct path root = p->root;
8092d4d4864SRam Pai 	int err = 0;
8102d4d4864SRam Pai 
8112d4d4864SRam Pai 	seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
8122d4d4864SRam Pai 		   MAJOR(sb->s_dev), MINOR(sb->s_dev));
8132d4d4864SRam Pai 	seq_dentry(m, mnt->mnt_root, " \t\n\\");
8142d4d4864SRam Pai 	seq_putc(m, ' ');
8152d4d4864SRam Pai 	seq_path_root(m, &mnt_path, &root, " \t\n\\");
8162d4d4864SRam Pai 	if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
8172d4d4864SRam Pai 		/*
8182d4d4864SRam Pai 		 * Mountpoint is outside root, discard that one.  Ugly,
8192d4d4864SRam Pai 		 * but less so than trying to do that in iterator in a
8202d4d4864SRam Pai 		 * race-free way (due to renames).
8212d4d4864SRam Pai 		 */
8222d4d4864SRam Pai 		return SEQ_SKIP;
8232d4d4864SRam Pai 	}
8242d4d4864SRam Pai 	seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
8252d4d4864SRam Pai 	show_mnt_opts(m, mnt);
8262d4d4864SRam Pai 
8272d4d4864SRam Pai 	/* Tagged fields ("foo:X" or "bar") */
8282d4d4864SRam Pai 	if (IS_MNT_SHARED(mnt))
8292d4d4864SRam Pai 		seq_printf(m, " shared:%i", mnt->mnt_group_id);
83097e7e0f7SMiklos Szeredi 	if (IS_MNT_SLAVE(mnt)) {
83197e7e0f7SMiklos Szeredi 		int master = mnt->mnt_master->mnt_group_id;
83297e7e0f7SMiklos Szeredi 		int dom = get_dominating_id(mnt, &p->root);
83397e7e0f7SMiklos Szeredi 		seq_printf(m, " master:%i", master);
83497e7e0f7SMiklos Szeredi 		if (dom && dom != master)
83597e7e0f7SMiklos Szeredi 			seq_printf(m, " propagate_from:%i", dom);
83697e7e0f7SMiklos Szeredi 	}
8372d4d4864SRam Pai 	if (IS_MNT_UNBINDABLE(mnt))
8382d4d4864SRam Pai 		seq_puts(m, " unbindable");
8392d4d4864SRam Pai 
8402d4d4864SRam Pai 	/* Filesystem specific data */
8412d4d4864SRam Pai 	seq_puts(m, " - ");
8422d4d4864SRam Pai 	show_type(m, sb);
8432d4d4864SRam Pai 	seq_putc(m, ' ');
8442d4d4864SRam Pai 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
8452d4d4864SRam Pai 	seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
8462069f457SEric Paris 	err = show_sb_opts(m, sb);
8472069f457SEric Paris 	if (err)
8482069f457SEric Paris 		goto out;
8492d4d4864SRam Pai 	if (sb->s_op->show_options)
8502d4d4864SRam Pai 		err = sb->s_op->show_options(m, mnt);
8512d4d4864SRam Pai 	seq_putc(m, '\n');
8522069f457SEric Paris out:
8532d4d4864SRam Pai 	return err;
8542d4d4864SRam Pai }
8552d4d4864SRam Pai 
8562d4d4864SRam Pai const struct seq_operations mountinfo_op = {
8572d4d4864SRam Pai 	.start	= m_start,
8582d4d4864SRam Pai 	.next	= m_next,
8592d4d4864SRam Pai 	.stop	= m_stop,
8602d4d4864SRam Pai 	.show	= show_mountinfo,
8612d4d4864SRam Pai };
8622d4d4864SRam Pai 
863b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v)
864b4629fe2SChuck Lever {
865b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
866c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
867b4629fe2SChuck Lever 	int err = 0;
868b4629fe2SChuck Lever 
869b4629fe2SChuck Lever 	/* device */
870b4629fe2SChuck Lever 	if (mnt->mnt_devname) {
871b4629fe2SChuck Lever 		seq_puts(m, "device ");
872b4629fe2SChuck Lever 		mangle(m, mnt->mnt_devname);
873b4629fe2SChuck Lever 	} else
874b4629fe2SChuck Lever 		seq_puts(m, "no device");
875b4629fe2SChuck Lever 
876b4629fe2SChuck Lever 	/* mount point */
877b4629fe2SChuck Lever 	seq_puts(m, " mounted on ");
878c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
879b4629fe2SChuck Lever 	seq_putc(m, ' ');
880b4629fe2SChuck Lever 
881b4629fe2SChuck Lever 	/* file system type */
882b4629fe2SChuck Lever 	seq_puts(m, "with fstype ");
8832d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
884b4629fe2SChuck Lever 
885b4629fe2SChuck Lever 	/* optional statistics */
886b4629fe2SChuck Lever 	if (mnt->mnt_sb->s_op->show_stats) {
887b4629fe2SChuck Lever 		seq_putc(m, ' ');
888b4629fe2SChuck Lever 		err = mnt->mnt_sb->s_op->show_stats(m, mnt);
889b4629fe2SChuck Lever 	}
890b4629fe2SChuck Lever 
891b4629fe2SChuck Lever 	seq_putc(m, '\n');
892b4629fe2SChuck Lever 	return err;
893b4629fe2SChuck Lever }
894b4629fe2SChuck Lever 
895a1a2c409SMiklos Szeredi const struct seq_operations mountstats_op = {
896b4629fe2SChuck Lever 	.start	= m_start,
897b4629fe2SChuck Lever 	.next	= m_next,
898b4629fe2SChuck Lever 	.stop	= m_stop,
899b4629fe2SChuck Lever 	.show	= show_vfsstat,
900b4629fe2SChuck Lever };
901a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
902b4629fe2SChuck Lever 
9031da177e4SLinus Torvalds /**
9041da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
9051da177e4SLinus Torvalds  * @mnt: root of mount tree
9061da177e4SLinus Torvalds  *
9071da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
9081da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
9091da177e4SLinus Torvalds  * busy.
9101da177e4SLinus Torvalds  */
9111da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt)
9121da177e4SLinus Torvalds {
91336341f64SRam Pai 	int actual_refs = 0;
91436341f64SRam Pai 	int minimum_refs = 0;
91536341f64SRam Pai 	struct vfsmount *p;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
91836341f64SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
9191da177e4SLinus Torvalds 		actual_refs += atomic_read(&p->mnt_count);
9201da177e4SLinus Torvalds 		minimum_refs += 2;
9211da177e4SLinus Torvalds 	}
9221da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
9251da177e4SLinus Torvalds 		return 0;
926e3474a8eSIan Kent 
927e3474a8eSIan Kent 	return 1;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds /**
9331da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
9341da177e4SLinus Torvalds  * @mnt: root of mount
9351da177e4SLinus Torvalds  *
9361da177e4SLinus Torvalds  * This is called to check if a mount point has any
9371da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
9381da177e4SLinus Torvalds  * mount has sub mounts this will return busy
9391da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
9401da177e4SLinus Torvalds  *
9411da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
9421da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
9431da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
9441da177e4SLinus Torvalds  */
9451da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
9461da177e4SLinus Torvalds {
947e3474a8eSIan Kent 	int ret = 1;
948a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
949a05964f3SRam Pai 	if (propagate_mount_busy(mnt, 2))
950e3474a8eSIan Kent 		ret = 0;
951a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
952a05964f3SRam Pai 	return ret;
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
9561da177e4SLinus Torvalds 
957b90fa9aeSRam Pai void release_mounts(struct list_head *head)
9581da177e4SLinus Torvalds {
95970fbcdf4SRam Pai 	struct vfsmount *mnt;
96070fbcdf4SRam Pai 	while (!list_empty(head)) {
961b5e61818SPavel Emelianov 		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
96270fbcdf4SRam Pai 		list_del_init(&mnt->mnt_hash);
96370fbcdf4SRam Pai 		if (mnt->mnt_parent != mnt) {
96470fbcdf4SRam Pai 			struct dentry *dentry;
96570fbcdf4SRam Pai 			struct vfsmount *m;
96670fbcdf4SRam Pai 			spin_lock(&vfsmount_lock);
96770fbcdf4SRam Pai 			dentry = mnt->mnt_mountpoint;
96870fbcdf4SRam Pai 			m = mnt->mnt_parent;
96970fbcdf4SRam Pai 			mnt->mnt_mountpoint = mnt->mnt_root;
97070fbcdf4SRam Pai 			mnt->mnt_parent = mnt;
9717c4b93d8SAl Viro 			m->mnt_ghosts--;
9721da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
97370fbcdf4SRam Pai 			dput(dentry);
97470fbcdf4SRam Pai 			mntput(m);
9751da177e4SLinus Torvalds 		}
9761da177e4SLinus Torvalds 		mntput(mnt);
97770fbcdf4SRam Pai 	}
97870fbcdf4SRam Pai }
97970fbcdf4SRam Pai 
980a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
98170fbcdf4SRam Pai {
98270fbcdf4SRam Pai 	struct vfsmount *p;
98370fbcdf4SRam Pai 
9841bfba4e8SAkinobu Mita 	for (p = mnt; p; p = next_mnt(p, mnt))
9851bfba4e8SAkinobu Mita 		list_move(&p->mnt_hash, kill);
98670fbcdf4SRam Pai 
987a05964f3SRam Pai 	if (propagate)
988a05964f3SRam Pai 		propagate_umount(kill);
989a05964f3SRam Pai 
99070fbcdf4SRam Pai 	list_for_each_entry(p, kill, mnt_hash) {
99170fbcdf4SRam Pai 		list_del_init(&p->mnt_expire);
99270fbcdf4SRam Pai 		list_del_init(&p->mnt_list);
9936b3286edSKirill Korotaev 		__touch_mnt_namespace(p->mnt_ns);
9946b3286edSKirill Korotaev 		p->mnt_ns = NULL;
99570fbcdf4SRam Pai 		list_del_init(&p->mnt_child);
9967c4b93d8SAl Viro 		if (p->mnt_parent != p) {
9977c4b93d8SAl Viro 			p->mnt_parent->mnt_ghosts++;
998f30ac319SAl Viro 			p->mnt_mountpoint->d_mounted--;
9997c4b93d8SAl Viro 		}
1000a05964f3SRam Pai 		change_mnt_propagation(p, MS_PRIVATE);
10011da177e4SLinus Torvalds 	}
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
1004c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
1005c35038beSAl Viro 
10061da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
10091da177e4SLinus Torvalds 	int retval;
101070fbcdf4SRam Pai 	LIST_HEAD(umount_list);
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	retval = security_sb_umount(mnt, flags);
10131da177e4SLinus Torvalds 	if (retval)
10141da177e4SLinus Torvalds 		return retval;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	/*
10171da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
10181da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
10191da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
10201da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
10211da177e4SLinus Torvalds 	 */
10221da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
10236ac08c39SJan Blunck 		if (mnt == current->fs->root.mnt ||
10241da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
10251da177e4SLinus Torvalds 			return -EINVAL;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 		if (atomic_read(&mnt->mnt_count) != 2)
10281da177e4SLinus Torvalds 			return -EBUSY;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1))
10311da177e4SLinus Torvalds 			return -EAGAIN;
10321da177e4SLinus Torvalds 	}
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	/*
10351da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
10361da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
10371da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
10381da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
10391da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
10401da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
10411da177e4SLinus Torvalds 	 * about for the moment.
10421da177e4SLinus Torvalds 	 */
10431da177e4SLinus Torvalds 
104442faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
104542faad99SAl Viro 		sb->s_op->umount_begin(sb);
104642faad99SAl Viro 	}
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	/*
10491da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
10501da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
10511da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
10521da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
10531da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
10541da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
10551da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
10561da177e4SLinus Torvalds 	 */
10576ac08c39SJan Blunck 	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
10581da177e4SLinus Torvalds 		/*
10591da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
10601da177e4SLinus Torvalds 		 * we just try to remount it readonly.
10611da177e4SLinus Torvalds 		 */
10621da177e4SLinus Torvalds 		down_write(&sb->s_umount);
10634aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
10641da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
10651da177e4SLinus Torvalds 		up_write(&sb->s_umount);
10661da177e4SLinus Torvalds 		return retval;
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds 
1069390c6843SRam Pai 	down_write(&namespace_sem);
10701da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
10715addc5ddSAl Viro 	event++;
10721da177e4SLinus Torvalds 
1073c35038beSAl Viro 	if (!(flags & MNT_DETACH))
1074c35038beSAl Viro 		shrink_submounts(mnt, &umount_list);
1075c35038beSAl Viro 
10761da177e4SLinus Torvalds 	retval = -EBUSY;
1077a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
10781da177e4SLinus Torvalds 		if (!list_empty(&mnt->mnt_list))
1079a05964f3SRam Pai 			umount_tree(mnt, 1, &umount_list);
10801da177e4SLinus Torvalds 		retval = 0;
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
10831da177e4SLinus Torvalds 	if (retval)
10841da177e4SLinus Torvalds 		security_sb_umount_busy(mnt);
1085390c6843SRam Pai 	up_write(&namespace_sem);
108670fbcdf4SRam Pai 	release_mounts(&umount_list);
10871da177e4SLinus Torvalds 	return retval;
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /*
10911da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
10921da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
10931da177e4SLinus Torvalds  *
10941da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
10951da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
10961da177e4SLinus Torvalds  */
10971da177e4SLinus Torvalds 
1098bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
10991da177e4SLinus Torvalds {
11002d8f3038SAl Viro 	struct path path;
11011da177e4SLinus Torvalds 	int retval;
11021da177e4SLinus Torvalds 
11032d8f3038SAl Viro 	retval = user_path(name, &path);
11041da177e4SLinus Torvalds 	if (retval)
11051da177e4SLinus Torvalds 		goto out;
11061da177e4SLinus Torvalds 	retval = -EINVAL;
11072d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
11081da177e4SLinus Torvalds 		goto dput_and_out;
11092d8f3038SAl Viro 	if (!check_mnt(path.mnt))
11101da177e4SLinus Torvalds 		goto dput_and_out;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	retval = -EPERM;
11131da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
11141da177e4SLinus Torvalds 		goto dput_and_out;
11151da177e4SLinus Torvalds 
11162d8f3038SAl Viro 	retval = do_umount(path.mnt, flags);
11171da177e4SLinus Torvalds dput_and_out:
1118429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
11192d8f3038SAl Viro 	dput(path.dentry);
11202d8f3038SAl Viro 	mntput_no_expire(path.mnt);
11211da177e4SLinus Torvalds out:
11221da177e4SLinus Torvalds 	return retval;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds /*
11281da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
11291da177e4SLinus Torvalds  */
1130bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
11311da177e4SLinus Torvalds {
11321da177e4SLinus Torvalds 	return sys_umount(name, 0);
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds #endif
11361da177e4SLinus Torvalds 
11372d92ab3cSAl Viro static int mount_is_safe(struct path *path)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	if (capable(CAP_SYS_ADMIN))
11401da177e4SLinus Torvalds 		return 0;
11411da177e4SLinus Torvalds 	return -EPERM;
11421da177e4SLinus Torvalds #ifdef notyet
11432d92ab3cSAl Viro 	if (S_ISLNK(path->dentry->d_inode->i_mode))
11441da177e4SLinus Torvalds 		return -EPERM;
11452d92ab3cSAl Viro 	if (path->dentry->d_inode->i_mode & S_ISVTX) {
1146da9592edSDavid Howells 		if (current_uid() != path->dentry->d_inode->i_uid)
11471da177e4SLinus Torvalds 			return -EPERM;
11481da177e4SLinus Torvalds 	}
11492d92ab3cSAl Viro 	if (inode_permission(path->dentry->d_inode, MAY_WRITE))
11501da177e4SLinus Torvalds 		return -EPERM;
11511da177e4SLinus Torvalds 	return 0;
11521da177e4SLinus Torvalds #endif
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
1155b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
115636341f64SRam Pai 					int flag)
11571da177e4SLinus Torvalds {
11581da177e4SLinus Torvalds 	struct vfsmount *res, *p, *q, *r, *s;
11591a390689SAl Viro 	struct path path;
11601da177e4SLinus Torvalds 
11619676f0c6SRam Pai 	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
11629676f0c6SRam Pai 		return NULL;
11639676f0c6SRam Pai 
116436341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
11651da177e4SLinus Torvalds 	if (!q)
11661da177e4SLinus Torvalds 		goto Enomem;
11671da177e4SLinus Torvalds 	q->mnt_mountpoint = mnt->mnt_mountpoint;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	p = mnt;
1170fdadd65fSDomen Puncer 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
11717ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
11721da177e4SLinus Torvalds 			continue;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 		for (s = r; s; s = next_mnt(s, r)) {
11759676f0c6SRam Pai 			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
11769676f0c6SRam Pai 				s = skip_mnt_tree(s);
11779676f0c6SRam Pai 				continue;
11789676f0c6SRam Pai 			}
11791da177e4SLinus Torvalds 			while (p != s->mnt_parent) {
11801da177e4SLinus Torvalds 				p = p->mnt_parent;
11811da177e4SLinus Torvalds 				q = q->mnt_parent;
11821da177e4SLinus Torvalds 			}
11831da177e4SLinus Torvalds 			p = s;
11841a390689SAl Viro 			path.mnt = q;
11851a390689SAl Viro 			path.dentry = p->mnt_mountpoint;
118636341f64SRam Pai 			q = clone_mnt(p, p->mnt_root, flag);
11871da177e4SLinus Torvalds 			if (!q)
11881da177e4SLinus Torvalds 				goto Enomem;
11891da177e4SLinus Torvalds 			spin_lock(&vfsmount_lock);
11901da177e4SLinus Torvalds 			list_add_tail(&q->mnt_list, &res->mnt_list);
11911a390689SAl Viro 			attach_mnt(q, &path);
11921da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
11931da177e4SLinus Torvalds 		}
11941da177e4SLinus Torvalds 	}
11951da177e4SLinus Torvalds 	return res;
11961da177e4SLinus Torvalds Enomem:
11971da177e4SLinus Torvalds 	if (res) {
119870fbcdf4SRam Pai 		LIST_HEAD(umount_list);
11991da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
1200a05964f3SRam Pai 		umount_tree(res, 0, &umount_list);
12011da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
120270fbcdf4SRam Pai 		release_mounts(&umount_list);
12031da177e4SLinus Torvalds 	}
12041da177e4SLinus Torvalds 	return NULL;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
1207589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
12088aec0809SAl Viro {
12098aec0809SAl Viro 	struct vfsmount *tree;
12101a60a280SAl Viro 	down_write(&namespace_sem);
1211589ff870SAl Viro 	tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
12121a60a280SAl Viro 	up_write(&namespace_sem);
12138aec0809SAl Viro 	return tree;
12148aec0809SAl Viro }
12158aec0809SAl Viro 
12168aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
12178aec0809SAl Viro {
12188aec0809SAl Viro 	LIST_HEAD(umount_list);
12191a60a280SAl Viro 	down_write(&namespace_sem);
12208aec0809SAl Viro 	spin_lock(&vfsmount_lock);
12218aec0809SAl Viro 	umount_tree(mnt, 0, &umount_list);
12228aec0809SAl Viro 	spin_unlock(&vfsmount_lock);
12231a60a280SAl Viro 	up_write(&namespace_sem);
12248aec0809SAl Viro 	release_mounts(&umount_list);
12258aec0809SAl Viro }
12268aec0809SAl Viro 
1227719f5d7fSMiklos Szeredi static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
1228719f5d7fSMiklos Szeredi {
1229719f5d7fSMiklos Szeredi 	struct vfsmount *p;
1230719f5d7fSMiklos Szeredi 
1231719f5d7fSMiklos Szeredi 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1232719f5d7fSMiklos Szeredi 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
1233719f5d7fSMiklos Szeredi 			mnt_release_group_id(p);
1234719f5d7fSMiklos Szeredi 	}
1235719f5d7fSMiklos Szeredi }
1236719f5d7fSMiklos Szeredi 
1237719f5d7fSMiklos Szeredi static int invent_group_ids(struct vfsmount *mnt, bool recurse)
1238719f5d7fSMiklos Szeredi {
1239719f5d7fSMiklos Szeredi 	struct vfsmount *p;
1240719f5d7fSMiklos Szeredi 
1241719f5d7fSMiklos Szeredi 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1242719f5d7fSMiklos Szeredi 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1243719f5d7fSMiklos Szeredi 			int err = mnt_alloc_group_id(p);
1244719f5d7fSMiklos Szeredi 			if (err) {
1245719f5d7fSMiklos Szeredi 				cleanup_group_ids(mnt, p);
1246719f5d7fSMiklos Szeredi 				return err;
1247719f5d7fSMiklos Szeredi 			}
1248719f5d7fSMiklos Szeredi 		}
1249719f5d7fSMiklos Szeredi 	}
1250719f5d7fSMiklos Szeredi 
1251719f5d7fSMiklos Szeredi 	return 0;
1252719f5d7fSMiklos Szeredi }
1253719f5d7fSMiklos Szeredi 
1254b90fa9aeSRam Pai /*
1255b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1256b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
125721444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
125821444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
125921444403SRam Pai  *  		   (done when source_mnt is moved)
1260b90fa9aeSRam Pai  *
1261b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1262b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
12639676f0c6SRam Pai  * ---------------------------------------------------------------------------
1264b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
12659676f0c6SRam Pai  * |**************************************************************************
12669676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
12679676f0c6SRam Pai  * | dest     |               |                |                |            |
12689676f0c6SRam Pai  * |   |      |               |                |                |            |
12699676f0c6SRam Pai  * |   v      |               |                |                |            |
12709676f0c6SRam Pai  * |**************************************************************************
12719676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
12725afe0022SRam Pai  * |          |               |                |                |            |
12739676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
12749676f0c6SRam Pai  * ***************************************************************************
1275b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1276b90fa9aeSRam Pai  * destination mount.
1277b90fa9aeSRam Pai  *
1278b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1279b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1280b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1281b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1282b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1283b90fa9aeSRam Pai  *       mount.
12845afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
12855afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
12865afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
12875afe0022SRam Pai  *       is marked as 'shared and slave'.
12885afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
12895afe0022SRam Pai  * 	 source mount.
12905afe0022SRam Pai  *
12919676f0c6SRam Pai  * ---------------------------------------------------------------------------
129221444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
12939676f0c6SRam Pai  * |**************************************************************************
12949676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
12959676f0c6SRam Pai  * | dest     |               |                |                |            |
12969676f0c6SRam Pai  * |   |      |               |                |                |            |
12979676f0c6SRam Pai  * |   v      |               |                |                |            |
12989676f0c6SRam Pai  * |**************************************************************************
12999676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
13005afe0022SRam Pai  * |          |               |                |                |            |
13019676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
13029676f0c6SRam Pai  * ***************************************************************************
13035afe0022SRam Pai  *
13045afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
13055afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
130621444403SRam Pai  * (+*)  the mount is moved to the destination.
13075afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
13085afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
13095afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
13105afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1311b90fa9aeSRam Pai  *
1312b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1313b90fa9aeSRam Pai  * applied to each mount in the tree.
1314b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1315b90fa9aeSRam Pai  * in allocations.
1316b90fa9aeSRam Pai  */
1317b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt,
13181a390689SAl Viro 			struct path *path, struct path *parent_path)
1319b90fa9aeSRam Pai {
1320b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
13211a390689SAl Viro 	struct vfsmount *dest_mnt = path->mnt;
13221a390689SAl Viro 	struct dentry *dest_dentry = path->dentry;
1323b90fa9aeSRam Pai 	struct vfsmount *child, *p;
1324719f5d7fSMiklos Szeredi 	int err;
1325b90fa9aeSRam Pai 
1326719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt)) {
1327719f5d7fSMiklos Szeredi 		err = invent_group_ids(source_mnt, true);
1328719f5d7fSMiklos Szeredi 		if (err)
1329719f5d7fSMiklos Szeredi 			goto out;
1330719f5d7fSMiklos Szeredi 	}
1331719f5d7fSMiklos Szeredi 	err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1332719f5d7fSMiklos Szeredi 	if (err)
1333719f5d7fSMiklos Szeredi 		goto out_cleanup_ids;
1334b90fa9aeSRam Pai 
1335b90fa9aeSRam Pai 	if (IS_MNT_SHARED(dest_mnt)) {
1336b90fa9aeSRam Pai 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1337b90fa9aeSRam Pai 			set_mnt_shared(p);
1338b90fa9aeSRam Pai 	}
1339b90fa9aeSRam Pai 
1340b90fa9aeSRam Pai 	spin_lock(&vfsmount_lock);
13411a390689SAl Viro 	if (parent_path) {
13421a390689SAl Viro 		detach_mnt(source_mnt, parent_path);
13431a390689SAl Viro 		attach_mnt(source_mnt, path);
1344e5d67f07SAl Viro 		touch_mnt_namespace(parent_path->mnt->mnt_ns);
134521444403SRam Pai 	} else {
1346b90fa9aeSRam Pai 		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
1347b90fa9aeSRam Pai 		commit_tree(source_mnt);
134821444403SRam Pai 	}
1349b90fa9aeSRam Pai 
1350b90fa9aeSRam Pai 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1351b90fa9aeSRam Pai 		list_del_init(&child->mnt_hash);
1352b90fa9aeSRam Pai 		commit_tree(child);
1353b90fa9aeSRam Pai 	}
1354b90fa9aeSRam Pai 	spin_unlock(&vfsmount_lock);
1355b90fa9aeSRam Pai 	return 0;
1356719f5d7fSMiklos Szeredi 
1357719f5d7fSMiklos Szeredi  out_cleanup_ids:
1358719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt))
1359719f5d7fSMiklos Szeredi 		cleanup_group_ids(source_mnt, NULL);
1360719f5d7fSMiklos Szeredi  out:
1361719f5d7fSMiklos Szeredi 	return err;
1362b90fa9aeSRam Pai }
1363b90fa9aeSRam Pai 
13648c3ee42eSAl Viro static int graft_tree(struct vfsmount *mnt, struct path *path)
13651da177e4SLinus Torvalds {
13661da177e4SLinus Torvalds 	int err;
13671da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_flags & MS_NOUSER)
13681da177e4SLinus Torvalds 		return -EINVAL;
13691da177e4SLinus Torvalds 
13708c3ee42eSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
13711da177e4SLinus Torvalds 	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
13721da177e4SLinus Torvalds 		return -ENOTDIR;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	err = -ENOENT;
13758c3ee42eSAl Viro 	mutex_lock(&path->dentry->d_inode->i_mutex);
13768c3ee42eSAl Viro 	if (IS_DEADDIR(path->dentry->d_inode))
13771da177e4SLinus Torvalds 		goto out_unlock;
13781da177e4SLinus Torvalds 
13798c3ee42eSAl Viro 	err = security_sb_check_sb(mnt, path);
13801da177e4SLinus Torvalds 	if (err)
13811da177e4SLinus Torvalds 		goto out_unlock;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	err = -ENOENT;
1384f3da392eSAlexey Dobriyan 	if (!d_unlinked(path->dentry))
13858c3ee42eSAl Viro 		err = attach_recursive_mnt(mnt, path, NULL);
13861da177e4SLinus Torvalds out_unlock:
13878c3ee42eSAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
13881da177e4SLinus Torvalds 	if (!err)
13898c3ee42eSAl Viro 		security_sb_post_addmount(mnt, path);
13901da177e4SLinus Torvalds 	return err;
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds /*
139407b20889SRam Pai  * recursively change the type of the mountpoint.
139507b20889SRam Pai  */
13960a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
139707b20889SRam Pai {
13982d92ab3cSAl Viro 	struct vfsmount *m, *mnt = path->mnt;
139907b20889SRam Pai 	int recurse = flag & MS_REC;
140007b20889SRam Pai 	int type = flag & ~MS_REC;
1401719f5d7fSMiklos Szeredi 	int err = 0;
140207b20889SRam Pai 
1403ee6f9582SMiklos Szeredi 	if (!capable(CAP_SYS_ADMIN))
1404ee6f9582SMiklos Szeredi 		return -EPERM;
1405ee6f9582SMiklos Szeredi 
14062d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
140707b20889SRam Pai 		return -EINVAL;
140807b20889SRam Pai 
140907b20889SRam Pai 	down_write(&namespace_sem);
1410719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1411719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1412719f5d7fSMiklos Szeredi 		if (err)
1413719f5d7fSMiklos Szeredi 			goto out_unlock;
1414719f5d7fSMiklos Szeredi 	}
1415719f5d7fSMiklos Szeredi 
141607b20889SRam Pai 	spin_lock(&vfsmount_lock);
141707b20889SRam Pai 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
141807b20889SRam Pai 		change_mnt_propagation(m, type);
141907b20889SRam Pai 	spin_unlock(&vfsmount_lock);
1420719f5d7fSMiklos Szeredi 
1421719f5d7fSMiklos Szeredi  out_unlock:
142207b20889SRam Pai 	up_write(&namespace_sem);
1423719f5d7fSMiklos Szeredi 	return err;
142407b20889SRam Pai }
142507b20889SRam Pai 
142607b20889SRam Pai /*
14271da177e4SLinus Torvalds  * do loopback mount.
14281da177e4SLinus Torvalds  */
14290a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name,
14302dafe1c4SEric Sandeen 				int recurse)
14311da177e4SLinus Torvalds {
14322d92ab3cSAl Viro 	struct path old_path;
14331da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
14342d92ab3cSAl Viro 	int err = mount_is_safe(path);
14351da177e4SLinus Torvalds 	if (err)
14361da177e4SLinus Torvalds 		return err;
14371da177e4SLinus Torvalds 	if (!old_name || !*old_name)
14381da177e4SLinus Torvalds 		return -EINVAL;
14392d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
14401da177e4SLinus Torvalds 	if (err)
14411da177e4SLinus Torvalds 		return err;
14421da177e4SLinus Torvalds 
1443390c6843SRam Pai 	down_write(&namespace_sem);
14441da177e4SLinus Torvalds 	err = -EINVAL;
14452d92ab3cSAl Viro 	if (IS_MNT_UNBINDABLE(old_path.mnt))
14469676f0c6SRam Pai 		goto out;
14479676f0c6SRam Pai 
14482d92ab3cSAl Viro 	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
1449ccd48bc7SAl Viro 		goto out;
1450ccd48bc7SAl Viro 
14511da177e4SLinus Torvalds 	err = -ENOMEM;
14521da177e4SLinus Torvalds 	if (recurse)
14532d92ab3cSAl Viro 		mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
14541da177e4SLinus Torvalds 	else
14552d92ab3cSAl Viro 		mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
14561da177e4SLinus Torvalds 
1457ccd48bc7SAl Viro 	if (!mnt)
1458ccd48bc7SAl Viro 		goto out;
1459ccd48bc7SAl Viro 
14602d92ab3cSAl Viro 	err = graft_tree(mnt, path);
14611da177e4SLinus Torvalds 	if (err) {
146270fbcdf4SRam Pai 		LIST_HEAD(umount_list);
14631da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
1464a05964f3SRam Pai 		umount_tree(mnt, 0, &umount_list);
14651da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
146670fbcdf4SRam Pai 		release_mounts(&umount_list);
14675b83d2c5SRam Pai 	}
14681da177e4SLinus Torvalds 
1469ccd48bc7SAl Viro out:
1470390c6843SRam Pai 	up_write(&namespace_sem);
14712d92ab3cSAl Viro 	path_put(&old_path);
14721da177e4SLinus Torvalds 	return err;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
14752e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
14762e4b7fcdSDave Hansen {
14772e4b7fcdSDave Hansen 	int error = 0;
14782e4b7fcdSDave Hansen 	int readonly_request = 0;
14792e4b7fcdSDave Hansen 
14802e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
14812e4b7fcdSDave Hansen 		readonly_request = 1;
14822e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
14832e4b7fcdSDave Hansen 		return 0;
14842e4b7fcdSDave Hansen 
14852e4b7fcdSDave Hansen 	if (readonly_request)
14862e4b7fcdSDave Hansen 		error = mnt_make_readonly(mnt);
14872e4b7fcdSDave Hansen 	else
14882e4b7fcdSDave Hansen 		__mnt_unmake_readonly(mnt);
14892e4b7fcdSDave Hansen 	return error;
14902e4b7fcdSDave Hansen }
14912e4b7fcdSDave Hansen 
14921da177e4SLinus Torvalds /*
14931da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
14941da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
14951da177e4SLinus Torvalds  * on it - tough luck.
14961da177e4SLinus Torvalds  */
14970a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
14981da177e4SLinus Torvalds 		      void *data)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	int err;
15012d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
15041da177e4SLinus Torvalds 		return -EPERM;
15051da177e4SLinus Torvalds 
15062d92ab3cSAl Viro 	if (!check_mnt(path->mnt))
15071da177e4SLinus Torvalds 		return -EINVAL;
15081da177e4SLinus Torvalds 
15092d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
15101da177e4SLinus Torvalds 		return -EINVAL;
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	down_write(&sb->s_umount);
15132e4b7fcdSDave Hansen 	if (flags & MS_BIND)
15142d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
15154aa98cf7SAl Viro 	else
15161da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
15171da177e4SLinus Torvalds 	if (!err)
15182d92ab3cSAl Viro 		path->mnt->mnt_flags = mnt_flags;
15191da177e4SLinus Torvalds 	up_write(&sb->s_umount);
15200e55a7ccSDan Williams 	if (!err) {
15212d92ab3cSAl Viro 		security_sb_post_remount(path->mnt, flags, data);
15220e55a7ccSDan Williams 
15230e55a7ccSDan Williams 		spin_lock(&vfsmount_lock);
15240e55a7ccSDan Williams 		touch_mnt_namespace(path->mnt->mnt_ns);
15250e55a7ccSDan Williams 		spin_unlock(&vfsmount_lock);
15260e55a7ccSDan Williams 	}
15271da177e4SLinus Torvalds 	return err;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15309676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt)
15319676f0c6SRam Pai {
15329676f0c6SRam Pai 	struct vfsmount *p;
15339676f0c6SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
15349676f0c6SRam Pai 		if (IS_MNT_UNBINDABLE(p))
15359676f0c6SRam Pai 			return 1;
15369676f0c6SRam Pai 	}
15379676f0c6SRam Pai 	return 0;
15389676f0c6SRam Pai }
15399676f0c6SRam Pai 
15400a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name)
15411da177e4SLinus Torvalds {
15422d92ab3cSAl Viro 	struct path old_path, parent_path;
15431da177e4SLinus Torvalds 	struct vfsmount *p;
15441da177e4SLinus Torvalds 	int err = 0;
15451da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
15461da177e4SLinus Torvalds 		return -EPERM;
15471da177e4SLinus Torvalds 	if (!old_name || !*old_name)
15481da177e4SLinus Torvalds 		return -EINVAL;
15492d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
15501da177e4SLinus Torvalds 	if (err)
15511da177e4SLinus Torvalds 		return err;
15521da177e4SLinus Torvalds 
1553390c6843SRam Pai 	down_write(&namespace_sem);
15542d92ab3cSAl Viro 	while (d_mountpoint(path->dentry) &&
15559393bd07SAl Viro 	       follow_down(path))
15561da177e4SLinus Torvalds 		;
15571da177e4SLinus Torvalds 	err = -EINVAL;
15582d92ab3cSAl Viro 	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
15591da177e4SLinus Torvalds 		goto out;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	err = -ENOENT;
15622d92ab3cSAl Viro 	mutex_lock(&path->dentry->d_inode->i_mutex);
15632d92ab3cSAl Viro 	if (IS_DEADDIR(path->dentry->d_inode))
15641da177e4SLinus Torvalds 		goto out1;
15651da177e4SLinus Torvalds 
1566f3da392eSAlexey Dobriyan 	if (d_unlinked(path->dentry))
156721444403SRam Pai 		goto out1;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	err = -EINVAL;
15702d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
157121444403SRam Pai 		goto out1;
15721da177e4SLinus Torvalds 
15732d92ab3cSAl Viro 	if (old_path.mnt == old_path.mnt->mnt_parent)
157421444403SRam Pai 		goto out1;
15751da177e4SLinus Torvalds 
15762d92ab3cSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
15772d92ab3cSAl Viro 	      S_ISDIR(old_path.dentry->d_inode->i_mode))
157821444403SRam Pai 		goto out1;
157921444403SRam Pai 	/*
158021444403SRam Pai 	 * Don't move a mount residing in a shared parent.
158121444403SRam Pai 	 */
15822d92ab3cSAl Viro 	if (old_path.mnt->mnt_parent &&
15832d92ab3cSAl Viro 	    IS_MNT_SHARED(old_path.mnt->mnt_parent))
158421444403SRam Pai 		goto out1;
15859676f0c6SRam Pai 	/*
15869676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
15879676f0c6SRam Pai 	 * mount which is shared.
15889676f0c6SRam Pai 	 */
15892d92ab3cSAl Viro 	if (IS_MNT_SHARED(path->mnt) &&
15902d92ab3cSAl Viro 	    tree_contains_unbindable(old_path.mnt))
15919676f0c6SRam Pai 		goto out1;
15921da177e4SLinus Torvalds 	err = -ELOOP;
15932d92ab3cSAl Viro 	for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent)
15942d92ab3cSAl Viro 		if (p == old_path.mnt)
159521444403SRam Pai 			goto out1;
15961da177e4SLinus Torvalds 
15972d92ab3cSAl Viro 	err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
15984ac91378SJan Blunck 	if (err)
159921444403SRam Pai 		goto out1;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
16021da177e4SLinus Torvalds 	 * automatically */
16032d92ab3cSAl Viro 	list_del_init(&old_path.mnt->mnt_expire);
16041da177e4SLinus Torvalds out1:
16052d92ab3cSAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
16061da177e4SLinus Torvalds out:
1607390c6843SRam Pai 	up_write(&namespace_sem);
16081da177e4SLinus Torvalds 	if (!err)
16091a390689SAl Viro 		path_put(&parent_path);
16102d92ab3cSAl Viro 	path_put(&old_path);
16111da177e4SLinus Torvalds 	return err;
16121da177e4SLinus Torvalds }
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds /*
16151da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
16161da177e4SLinus Torvalds  * namespace's tree
16171da177e4SLinus Torvalds  */
16180a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags,
16191da177e4SLinus Torvalds 			int mnt_flags, char *name, void *data)
16201da177e4SLinus Torvalds {
16211da177e4SLinus Torvalds 	struct vfsmount *mnt;
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	if (!type || !memchr(type, 0, PAGE_SIZE))
16241da177e4SLinus Torvalds 		return -EINVAL;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	/* we need capabilities... */
16271da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
16281da177e4SLinus Torvalds 		return -EPERM;
16291da177e4SLinus Torvalds 
16307f78d4cdSAl Viro 	lock_kernel();
16311da177e4SLinus Torvalds 	mnt = do_kern_mount(type, flags, name, data);
16327f78d4cdSAl Viro 	unlock_kernel();
16331da177e4SLinus Torvalds 	if (IS_ERR(mnt))
16341da177e4SLinus Torvalds 		return PTR_ERR(mnt);
16351da177e4SLinus Torvalds 
16362d92ab3cSAl Viro 	return do_add_mount(mnt, path, mnt_flags, NULL);
16371da177e4SLinus Torvalds }
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds /*
16401da177e4SLinus Torvalds  * add a mount into a namespace's mount tree
16411da177e4SLinus Torvalds  * - provide the option of adding the new mount to an expiration list
16421da177e4SLinus Torvalds  */
16438d66bf54SAl Viro int do_add_mount(struct vfsmount *newmnt, struct path *path,
16441da177e4SLinus Torvalds 		 int mnt_flags, struct list_head *fslist)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	int err;
16471da177e4SLinus Torvalds 
1648390c6843SRam Pai 	down_write(&namespace_sem);
16491da177e4SLinus Torvalds 	/* Something was mounted here while we slept */
16508d66bf54SAl Viro 	while (d_mountpoint(path->dentry) &&
16519393bd07SAl Viro 	       follow_down(path))
16521da177e4SLinus Torvalds 		;
16531da177e4SLinus Torvalds 	err = -EINVAL;
1654dd5cae6eSAl Viro 	if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
16551da177e4SLinus Torvalds 		goto unlock;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	/* Refuse the same filesystem on the same mount point */
16581da177e4SLinus Torvalds 	err = -EBUSY;
16598d66bf54SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt_sb &&
16608d66bf54SAl Viro 	    path->mnt->mnt_root == path->dentry)
16611da177e4SLinus Torvalds 		goto unlock;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	err = -EINVAL;
16641da177e4SLinus Torvalds 	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
16651da177e4SLinus Torvalds 		goto unlock;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	newmnt->mnt_flags = mnt_flags;
16688d66bf54SAl Viro 	if ((err = graft_tree(newmnt, path)))
16695b83d2c5SRam Pai 		goto unlock;
16701da177e4SLinus Torvalds 
16716758f953SAl Viro 	if (fslist) /* add to the specified expiration list */
167255e700b9SMiklos Szeredi 		list_add_tail(&newmnt->mnt_expire, fslist);
16736758f953SAl Viro 
1674390c6843SRam Pai 	up_write(&namespace_sem);
16755b83d2c5SRam Pai 	return 0;
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds unlock:
1678390c6843SRam Pai 	up_write(&namespace_sem);
16791da177e4SLinus Torvalds 	mntput(newmnt);
16801da177e4SLinus Torvalds 	return err;
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount);
16841da177e4SLinus Torvalds 
16855528f911STrond Myklebust /*
16861da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
16871da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
16881da177e4SLinus Torvalds  * here
16891da177e4SLinus Torvalds  */
16901da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
16911da177e4SLinus Torvalds {
16921da177e4SLinus Torvalds 	struct vfsmount *mnt, *next;
16931da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
1694bcc5c7d2SAl Viro 	LIST_HEAD(umounts);
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	if (list_empty(mounts))
16971da177e4SLinus Torvalds 		return;
16981da177e4SLinus Torvalds 
1699bcc5c7d2SAl Viro 	down_write(&namespace_sem);
17001da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
17031da177e4SLinus Torvalds 	 * following criteria:
17041da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
17051da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
17061da177e4SLinus Torvalds 	 *   cleared by mntput())
17071da177e4SLinus Torvalds 	 */
170855e700b9SMiklos Szeredi 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
17091da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1710bcc5c7d2SAl Viro 			propagate_mount_busy(mnt, 1))
17111da177e4SLinus Torvalds 			continue;
171255e700b9SMiklos Szeredi 		list_move(&mnt->mnt_expire, &graveyard);
17131da177e4SLinus Torvalds 	}
1714bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
1715bcc5c7d2SAl Viro 		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
1716bcc5c7d2SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1717bcc5c7d2SAl Viro 		umount_tree(mnt, 1, &umounts);
1718bcc5c7d2SAl Viro 	}
17191da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
1720bcc5c7d2SAl Viro 	up_write(&namespace_sem);
1721bcc5c7d2SAl Viro 
1722bcc5c7d2SAl Viro 	release_mounts(&umounts);
17231da177e4SLinus Torvalds }
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds /*
17285528f911STrond Myklebust  * Ripoff of 'select_parent()'
17295528f911STrond Myklebust  *
17305528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
17315528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
17325528f911STrond Myklebust  */
17335528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
17345528f911STrond Myklebust {
17355528f911STrond Myklebust 	struct vfsmount *this_parent = parent;
17365528f911STrond Myklebust 	struct list_head *next;
17375528f911STrond Myklebust 	int found = 0;
17385528f911STrond Myklebust 
17395528f911STrond Myklebust repeat:
17405528f911STrond Myklebust 	next = this_parent->mnt_mounts.next;
17415528f911STrond Myklebust resume:
17425528f911STrond Myklebust 	while (next != &this_parent->mnt_mounts) {
17435528f911STrond Myklebust 		struct list_head *tmp = next;
17445528f911STrond Myklebust 		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
17455528f911STrond Myklebust 
17465528f911STrond Myklebust 		next = tmp->next;
17475528f911STrond Myklebust 		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
17485528f911STrond Myklebust 			continue;
17495528f911STrond Myklebust 		/*
17505528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
17515528f911STrond Myklebust 		 */
17525528f911STrond Myklebust 		if (!list_empty(&mnt->mnt_mounts)) {
17535528f911STrond Myklebust 			this_parent = mnt;
17545528f911STrond Myklebust 			goto repeat;
17555528f911STrond Myklebust 		}
17565528f911STrond Myklebust 
17575528f911STrond Myklebust 		if (!propagate_mount_busy(mnt, 1)) {
17585528f911STrond Myklebust 			list_move_tail(&mnt->mnt_expire, graveyard);
17595528f911STrond Myklebust 			found++;
17605528f911STrond Myklebust 		}
17615528f911STrond Myklebust 	}
17625528f911STrond Myklebust 	/*
17635528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
17645528f911STrond Myklebust 	 */
17655528f911STrond Myklebust 	if (this_parent != parent) {
17665528f911STrond Myklebust 		next = this_parent->mnt_child.next;
17675528f911STrond Myklebust 		this_parent = this_parent->mnt_parent;
17685528f911STrond Myklebust 		goto resume;
17695528f911STrond Myklebust 	}
17705528f911STrond Myklebust 	return found;
17715528f911STrond Myklebust }
17725528f911STrond Myklebust 
17735528f911STrond Myklebust /*
17745528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
17755528f911STrond Myklebust  * submounts of a specific parent mountpoint
17765528f911STrond Myklebust  */
1777c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
17785528f911STrond Myklebust {
17795528f911STrond Myklebust 	LIST_HEAD(graveyard);
1780c35038beSAl Viro 	struct vfsmount *m;
17815528f911STrond Myklebust 
17825528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
1783c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
1784bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
1785c35038beSAl Viro 			m = list_first_entry(&graveyard, struct vfsmount,
1786bcc5c7d2SAl Viro 						mnt_expire);
1787afef80b3SEric W. Biederman 			touch_mnt_namespace(m->mnt_ns);
1788afef80b3SEric W. Biederman 			umount_tree(m, 1, umounts);
1789bcc5c7d2SAl Viro 		}
1790bcc5c7d2SAl Viro 	}
17915528f911STrond Myklebust }
17925528f911STrond Myklebust 
17935528f911STrond Myklebust /*
17941da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
17951da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
17961da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
17971da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
17981da177e4SLinus Torvalds  */
1799b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
1800b58fed8bSRam Pai 				 unsigned long n)
18011da177e4SLinus Torvalds {
18021da177e4SLinus Torvalds 	char *t = to;
18031da177e4SLinus Torvalds 	const char __user *f = from;
18041da177e4SLinus Torvalds 	char c;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
18071da177e4SLinus Torvalds 		return n;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	while (n) {
18101da177e4SLinus Torvalds 		if (__get_user(c, f)) {
18111da177e4SLinus Torvalds 			memset(t, 0, n);
18121da177e4SLinus Torvalds 			break;
18131da177e4SLinus Torvalds 		}
18141da177e4SLinus Torvalds 		*t++ = c;
18151da177e4SLinus Torvalds 		f++;
18161da177e4SLinus Torvalds 		n--;
18171da177e4SLinus Torvalds 	}
18181da177e4SLinus Torvalds 	return n;
18191da177e4SLinus Torvalds }
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
18221da177e4SLinus Torvalds {
18231da177e4SLinus Torvalds 	int i;
18241da177e4SLinus Torvalds 	unsigned long page;
18251da177e4SLinus Torvalds 	unsigned long size;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	*where = 0;
18281da177e4SLinus Torvalds 	if (!data)
18291da177e4SLinus Torvalds 		return 0;
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
18321da177e4SLinus Torvalds 		return -ENOMEM;
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
18351da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
18361da177e4SLinus Torvalds 	 * the remainder of the page.
18371da177e4SLinus Torvalds 	 */
18381da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
18391da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
18401da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
18411da177e4SLinus Torvalds 		size = PAGE_SIZE;
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
18441da177e4SLinus Torvalds 	if (!i) {
18451da177e4SLinus Torvalds 		free_page(page);
18461da177e4SLinus Torvalds 		return -EFAULT;
18471da177e4SLinus Torvalds 	}
18481da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
18491da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
18501da177e4SLinus Torvalds 	*where = page;
18511da177e4SLinus Torvalds 	return 0;
18521da177e4SLinus Torvalds }
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds /*
18551da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
18561da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
18571da177e4SLinus Torvalds  *
18581da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
18591da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
18601da177e4SLinus Torvalds  * information (or be NULL).
18611da177e4SLinus Torvalds  *
18621da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
18631da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
18641da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
18651da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
18661da177e4SLinus Torvalds  * and must be discarded.
18671da177e4SLinus Torvalds  */
18681da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page,
18691da177e4SLinus Torvalds 		  unsigned long flags, void *data_page)
18701da177e4SLinus Torvalds {
18712d92ab3cSAl Viro 	struct path path;
18721da177e4SLinus Torvalds 	int retval = 0;
18731da177e4SLinus Torvalds 	int mnt_flags = 0;
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	/* Discard magic */
18761da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
18771da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	/* Basic sanity checks */
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
18821da177e4SLinus Torvalds 		return -EINVAL;
18831da177e4SLinus Torvalds 	if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
18841da177e4SLinus Torvalds 		return -EINVAL;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	if (data_page)
18871da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
18881da177e4SLinus Torvalds 
1889613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
1890613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
18910a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
18920a1c01c9SMatthew Garrett 
18931da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
18941da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
18951da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
18961da177e4SLinus Torvalds 	if (flags & MS_NODEV)
18971da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
18981da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
18991da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
1900fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
1901fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
1902fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
1903fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
1904d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
1905d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
19062e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
19072e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
1908fc33a7bbSChristoph Hellwig 
1909fc33a7bbSChristoph Hellwig 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
1910d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
1911d0adde57SMatthew Garrett 		   MS_STRICTATIME);
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	/* ... and get the mountpoint */
19142d92ab3cSAl Viro 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
19151da177e4SLinus Torvalds 	if (retval)
19161da177e4SLinus Torvalds 		return retval;
19171da177e4SLinus Torvalds 
19182d92ab3cSAl Viro 	retval = security_sb_mount(dev_name, &path,
1919b5266eb4SAl Viro 				   type_page, flags, data_page);
19201da177e4SLinus Torvalds 	if (retval)
19211da177e4SLinus Torvalds 		goto dput_out;
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
19242d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
19251da177e4SLinus Torvalds 				    data_page);
19261da177e4SLinus Torvalds 	else if (flags & MS_BIND)
19272d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
19289676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
19292d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
19301da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
19312d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
19321da177e4SLinus Torvalds 	else
19332d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
19341da177e4SLinus Torvalds 				      dev_name, data_page);
19351da177e4SLinus Torvalds dput_out:
19362d92ab3cSAl Viro 	path_put(&path);
19371da177e4SLinus Torvalds 	return retval;
19381da177e4SLinus Torvalds }
19391da177e4SLinus Torvalds 
1940cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void)
1941cf8d2c11STrond Myklebust {
1942cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
1943cf8d2c11STrond Myklebust 
1944cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
1945cf8d2c11STrond Myklebust 	if (!new_ns)
1946cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
1947cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
1948cf8d2c11STrond Myklebust 	new_ns->root = NULL;
1949cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
1950cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
1951cf8d2c11STrond Myklebust 	new_ns->event = 0;
1952cf8d2c11STrond Myklebust 	return new_ns;
1953cf8d2c11STrond Myklebust }
1954cf8d2c11STrond Myklebust 
1955741a2951SJANAK DESAI /*
1956741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
1957741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
1958741a2951SJANAK DESAI  */
1959e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
19606b3286edSKirill Korotaev 		struct fs_struct *fs)
19611da177e4SLinus Torvalds {
19626b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
19637f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
19641da177e4SLinus Torvalds 	struct vfsmount *p, *q;
19651da177e4SLinus Torvalds 
1966cf8d2c11STrond Myklebust 	new_ns = alloc_mnt_ns();
1967cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
1968cf8d2c11STrond Myklebust 		return new_ns;
19691da177e4SLinus Torvalds 
1970390c6843SRam Pai 	down_write(&namespace_sem);
19711da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
19726b3286edSKirill Korotaev 	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
19739676f0c6SRam Pai 					CL_COPY_ALL | CL_EXPIRE);
19741da177e4SLinus Torvalds 	if (!new_ns->root) {
1975390c6843SRam Pai 		up_write(&namespace_sem);
19761da177e4SLinus Torvalds 		kfree(new_ns);
19775cc4a034SJulia Lawall 		return ERR_PTR(-ENOMEM);
19781da177e4SLinus Torvalds 	}
19791da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
19801da177e4SLinus Torvalds 	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
19811da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 	/*
19841da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
19851da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
19861da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
19871da177e4SLinus Torvalds 	 */
19886b3286edSKirill Korotaev 	p = mnt_ns->root;
19891da177e4SLinus Torvalds 	q = new_ns->root;
19901da177e4SLinus Torvalds 	while (p) {
19916b3286edSKirill Korotaev 		q->mnt_ns = new_ns;
19921da177e4SLinus Torvalds 		if (fs) {
19936ac08c39SJan Blunck 			if (p == fs->root.mnt) {
19941da177e4SLinus Torvalds 				rootmnt = p;
19956ac08c39SJan Blunck 				fs->root.mnt = mntget(q);
19961da177e4SLinus Torvalds 			}
19976ac08c39SJan Blunck 			if (p == fs->pwd.mnt) {
19981da177e4SLinus Torvalds 				pwdmnt = p;
19996ac08c39SJan Blunck 				fs->pwd.mnt = mntget(q);
20001da177e4SLinus Torvalds 			}
20011da177e4SLinus Torvalds 		}
20026b3286edSKirill Korotaev 		p = next_mnt(p, mnt_ns->root);
20031da177e4SLinus Torvalds 		q = next_mnt(q, new_ns->root);
20041da177e4SLinus Torvalds 	}
2005390c6843SRam Pai 	up_write(&namespace_sem);
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	if (rootmnt)
20081da177e4SLinus Torvalds 		mntput(rootmnt);
20091da177e4SLinus Torvalds 	if (pwdmnt)
20101da177e4SLinus Torvalds 		mntput(pwdmnt);
20111da177e4SLinus Torvalds 
2012741a2951SJANAK DESAI 	return new_ns;
2013741a2951SJANAK DESAI }
2014741a2951SJANAK DESAI 
2015213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2016e3222c4eSBadari Pulavarty 		struct fs_struct *new_fs)
2017741a2951SJANAK DESAI {
20186b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
2019741a2951SJANAK DESAI 
2020e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
20216b3286edSKirill Korotaev 	get_mnt_ns(ns);
2022741a2951SJANAK DESAI 
2023741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
2024e3222c4eSBadari Pulavarty 		return ns;
2025741a2951SJANAK DESAI 
2026e3222c4eSBadari Pulavarty 	new_ns = dup_mnt_ns(ns, new_fs);
2027741a2951SJANAK DESAI 
20286b3286edSKirill Korotaev 	put_mnt_ns(ns);
2029e3222c4eSBadari Pulavarty 	return new_ns;
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
2032cf8d2c11STrond Myklebust /**
2033cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2034cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2035cf8d2c11STrond Myklebust  */
2036cf8d2c11STrond Myklebust struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
2037cf8d2c11STrond Myklebust {
2038cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2039cf8d2c11STrond Myklebust 
2040cf8d2c11STrond Myklebust 	new_ns = alloc_mnt_ns();
2041cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
2042cf8d2c11STrond Myklebust 		mnt->mnt_ns = new_ns;
2043cf8d2c11STrond Myklebust 		new_ns->root = mnt;
2044cf8d2c11STrond Myklebust 		list_add(&new_ns->list, &new_ns->root->mnt_list);
2045cf8d2c11STrond Myklebust 	}
2046cf8d2c11STrond Myklebust 	return new_ns;
2047cf8d2c11STrond Myklebust }
2048cf8d2c11STrond Myklebust EXPORT_SYMBOL(create_mnt_ns);
2049cf8d2c11STrond Myklebust 
2050bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2051bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	int retval;
20541da177e4SLinus Torvalds 	unsigned long data_page;
20551da177e4SLinus Torvalds 	unsigned long type_page;
20561da177e4SLinus Torvalds 	unsigned long dev_page;
20571da177e4SLinus Torvalds 	char *dir_page;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	retval = copy_mount_options(type, &type_page);
20601da177e4SLinus Torvalds 	if (retval < 0)
20611da177e4SLinus Torvalds 		return retval;
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	dir_page = getname(dir_name);
20641da177e4SLinus Torvalds 	retval = PTR_ERR(dir_page);
20651da177e4SLinus Torvalds 	if (IS_ERR(dir_page))
20661da177e4SLinus Torvalds 		goto out1;
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	retval = copy_mount_options(dev_name, &dev_page);
20691da177e4SLinus Torvalds 	if (retval < 0)
20701da177e4SLinus Torvalds 		goto out2;
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	retval = copy_mount_options(data, &data_page);
20731da177e4SLinus Torvalds 	if (retval < 0)
20741da177e4SLinus Torvalds 		goto out3;
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds 	retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
20771da177e4SLinus Torvalds 			  flags, (void *)data_page);
20781da177e4SLinus Torvalds 	free_page(data_page);
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds out3:
20811da177e4SLinus Torvalds 	free_page(dev_page);
20821da177e4SLinus Torvalds out2:
20831da177e4SLinus Torvalds 	putname(dir_page);
20841da177e4SLinus Torvalds out1:
20851da177e4SLinus Torvalds 	free_page(type_page);
20861da177e4SLinus Torvalds 	return retval;
20871da177e4SLinus Torvalds }
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds /*
20901da177e4SLinus Torvalds  * pivot_root Semantics:
20911da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
20921da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
20931da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
20941da177e4SLinus Torvalds  *
20951da177e4SLinus Torvalds  * Restrictions:
20961da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
20971da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
20981da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
20991da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
21001da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
21011da177e4SLinus Torvalds  *
21024a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
21034a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
21044a0d11faSNeil Brown  * in this situation.
21054a0d11faSNeil Brown  *
21061da177e4SLinus Torvalds  * Notes:
21071da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
21081da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
21091da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
21101da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
21111da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
21121da177e4SLinus Torvalds  *    first.
21131da177e4SLinus Torvalds  */
21143480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
21153480b257SHeiko Carstens 		const char __user *, put_old)
21161da177e4SLinus Torvalds {
21171da177e4SLinus Torvalds 	struct vfsmount *tmp;
21182d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
21191da177e4SLinus Torvalds 	int error;
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
21221da177e4SLinus Torvalds 		return -EPERM;
21231da177e4SLinus Torvalds 
21242d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
21251da177e4SLinus Torvalds 	if (error)
21261da177e4SLinus Torvalds 		goto out0;
21271da177e4SLinus Torvalds 	error = -EINVAL;
21282d8f3038SAl Viro 	if (!check_mnt(new.mnt))
21291da177e4SLinus Torvalds 		goto out1;
21301da177e4SLinus Torvalds 
21312d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
21321da177e4SLinus Torvalds 	if (error)
21331da177e4SLinus Torvalds 		goto out1;
21341da177e4SLinus Torvalds 
21352d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
21361da177e4SLinus Torvalds 	if (error) {
21372d8f3038SAl Viro 		path_put(&old);
21381da177e4SLinus Torvalds 		goto out1;
21391da177e4SLinus Torvalds 	}
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds 	read_lock(&current->fs->lock);
21428c3ee42eSAl Viro 	root = current->fs->root;
21436ac08c39SJan Blunck 	path_get(&current->fs->root);
21441da177e4SLinus Torvalds 	read_unlock(&current->fs->lock);
2145390c6843SRam Pai 	down_write(&namespace_sem);
21462d8f3038SAl Viro 	mutex_lock(&old.dentry->d_inode->i_mutex);
21471da177e4SLinus Torvalds 	error = -EINVAL;
21482d8f3038SAl Viro 	if (IS_MNT_SHARED(old.mnt) ||
21492d8f3038SAl Viro 		IS_MNT_SHARED(new.mnt->mnt_parent) ||
21508c3ee42eSAl Viro 		IS_MNT_SHARED(root.mnt->mnt_parent))
215121444403SRam Pai 		goto out2;
21528c3ee42eSAl Viro 	if (!check_mnt(root.mnt))
21531da177e4SLinus Torvalds 		goto out2;
21541da177e4SLinus Torvalds 	error = -ENOENT;
21552d8f3038SAl Viro 	if (IS_DEADDIR(new.dentry->d_inode))
21561da177e4SLinus Torvalds 		goto out2;
2157f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
21581da177e4SLinus Torvalds 		goto out2;
2159f3da392eSAlexey Dobriyan 	if (d_unlinked(old.dentry))
21601da177e4SLinus Torvalds 		goto out2;
21611da177e4SLinus Torvalds 	error = -EBUSY;
21622d8f3038SAl Viro 	if (new.mnt == root.mnt ||
21632d8f3038SAl Viro 	    old.mnt == root.mnt)
21641da177e4SLinus Torvalds 		goto out2; /* loop, on the same file system  */
21651da177e4SLinus Torvalds 	error = -EINVAL;
21668c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
21671da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
21688c3ee42eSAl Viro 	if (root.mnt->mnt_parent == root.mnt)
21690bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
21702d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
21711da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
21722d8f3038SAl Viro 	if (new.mnt->mnt_parent == new.mnt)
21730bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
21744ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
21752d8f3038SAl Viro 	tmp = old.mnt;
21761da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
21772d8f3038SAl Viro 	if (tmp != new.mnt) {
21781da177e4SLinus Torvalds 		for (;;) {
21791da177e4SLinus Torvalds 			if (tmp->mnt_parent == tmp)
21801da177e4SLinus Torvalds 				goto out3; /* already mounted on put_old */
21812d8f3038SAl Viro 			if (tmp->mnt_parent == new.mnt)
21821da177e4SLinus Torvalds 				break;
21831da177e4SLinus Torvalds 			tmp = tmp->mnt_parent;
21841da177e4SLinus Torvalds 		}
21852d8f3038SAl Viro 		if (!is_subdir(tmp->mnt_mountpoint, new.dentry))
21861da177e4SLinus Torvalds 			goto out3;
21872d8f3038SAl Viro 	} else if (!is_subdir(old.dentry, new.dentry))
21881da177e4SLinus Torvalds 		goto out3;
21892d8f3038SAl Viro 	detach_mnt(new.mnt, &parent_path);
21908c3ee42eSAl Viro 	detach_mnt(root.mnt, &root_parent);
21914ac91378SJan Blunck 	/* mount old root on put_old */
21922d8f3038SAl Viro 	attach_mnt(root.mnt, &old);
21934ac91378SJan Blunck 	/* mount new_root on / */
21942d8f3038SAl Viro 	attach_mnt(new.mnt, &root_parent);
21956b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
21961da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
21972d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
21982d8f3038SAl Viro 	security_sb_post_pivotroot(&root, &new);
21991da177e4SLinus Torvalds 	error = 0;
22001a390689SAl Viro 	path_put(&root_parent);
22011a390689SAl Viro 	path_put(&parent_path);
22021da177e4SLinus Torvalds out2:
22032d8f3038SAl Viro 	mutex_unlock(&old.dentry->d_inode->i_mutex);
2204390c6843SRam Pai 	up_write(&namespace_sem);
22058c3ee42eSAl Viro 	path_put(&root);
22062d8f3038SAl Viro 	path_put(&old);
22071da177e4SLinus Torvalds out1:
22082d8f3038SAl Viro 	path_put(&new);
22091da177e4SLinus Torvalds out0:
22101da177e4SLinus Torvalds 	return error;
22111da177e4SLinus Torvalds out3:
22121da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
22131da177e4SLinus Torvalds 	goto out2;
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds static void __init init_mount_tree(void)
22171da177e4SLinus Torvalds {
22181da177e4SLinus Torvalds 	struct vfsmount *mnt;
22196b3286edSKirill Korotaev 	struct mnt_namespace *ns;
2220ac748a09SJan Blunck 	struct path root;
22211da177e4SLinus Torvalds 
22221da177e4SLinus Torvalds 	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
22231da177e4SLinus Torvalds 	if (IS_ERR(mnt))
22241da177e4SLinus Torvalds 		panic("Can't create rootfs");
22256b3286edSKirill Korotaev 	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
22266b3286edSKirill Korotaev 	if (!ns)
22271da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
22286b3286edSKirill Korotaev 	atomic_set(&ns->count, 1);
22296b3286edSKirill Korotaev 	INIT_LIST_HEAD(&ns->list);
22306b3286edSKirill Korotaev 	init_waitqueue_head(&ns->poll);
22316b3286edSKirill Korotaev 	ns->event = 0;
22326b3286edSKirill Korotaev 	list_add(&mnt->mnt_list, &ns->list);
22336b3286edSKirill Korotaev 	ns->root = mnt;
22346b3286edSKirill Korotaev 	mnt->mnt_ns = ns;
22351da177e4SLinus Torvalds 
22366b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
22376b3286edSKirill Korotaev 	get_mnt_ns(ns);
22381da177e4SLinus Torvalds 
2239ac748a09SJan Blunck 	root.mnt = ns->root;
2240ac748a09SJan Blunck 	root.dentry = ns->root->mnt_root;
2241ac748a09SJan Blunck 
2242ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
2243ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
22441da177e4SLinus Torvalds }
22451da177e4SLinus Torvalds 
224674bf17cfSDenis Cheng void __init mnt_init(void)
22471da177e4SLinus Torvalds {
224813f14b4dSEric Dumazet 	unsigned u;
224915a67dd8SRandy Dunlap 	int err;
22501da177e4SLinus Torvalds 
2251390c6843SRam Pai 	init_rwsem(&namespace_sem);
2252390c6843SRam Pai 
22531da177e4SLinus Torvalds 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
225420c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
22551da177e4SLinus Torvalds 
2256b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds 	if (!mount_hashtable)
22591da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
22601da177e4SLinus Torvalds 
226113f14b4dSEric Dumazet 	printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
22621da177e4SLinus Torvalds 
226313f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
226413f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
22651da177e4SLinus Torvalds 
226615a67dd8SRandy Dunlap 	err = sysfs_init();
226715a67dd8SRandy Dunlap 	if (err)
226815a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
22698e24eea7SHarvey Harrison 			__func__, err);
227000d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
227100d26666SGreg Kroah-Hartman 	if (!fs_kobj)
22728e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
22731da177e4SLinus Torvalds 	init_rootfs();
22741da177e4SLinus Torvalds 	init_mount_tree();
22751da177e4SLinus Torvalds }
22761da177e4SLinus Torvalds 
2277616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
22781da177e4SLinus Torvalds {
2279616511d0STrond Myklebust 	struct vfsmount *root;
228070fbcdf4SRam Pai 	LIST_HEAD(umount_list);
2281616511d0STrond Myklebust 
2282616511d0STrond Myklebust 	if (!atomic_dec_and_lock(&ns->count, &vfsmount_lock))
2283616511d0STrond Myklebust 		return;
2284616511d0STrond Myklebust 	root = ns->root;
22856b3286edSKirill Korotaev 	ns->root = NULL;
22861ce88cf4SMiklos Szeredi 	spin_unlock(&vfsmount_lock);
2287390c6843SRam Pai 	down_write(&namespace_sem);
22881da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
2289a05964f3SRam Pai 	umount_tree(root, 0, &umount_list);
22901da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
2291390c6843SRam Pai 	up_write(&namespace_sem);
229270fbcdf4SRam Pai 	release_mounts(&umount_list);
22936b3286edSKirill Korotaev 	kfree(ns);
22941da177e4SLinus Torvalds }
2295cf8d2c11STrond Myklebust EXPORT_SYMBOL(put_mnt_ns);
2296