xref: /openbmc/linux/fs/namespace.c (revision 7f2da1e7)
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>
301da177e4SLinus Torvalds #include <asm/uaccess.h>
311da177e4SLinus Torvalds #include <asm/unistd.h>
3207b20889SRam Pai #include "pnode.h"
33948730b0SAdrian Bunk #include "internal.h"
341da177e4SLinus Torvalds 
3513f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
3613f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT)
3713f14b4dSEric Dumazet 
381da177e4SLinus Torvalds /* spinlock for vfsmount related operations, inplace of dcache_lock */
391da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
401da177e4SLinus Torvalds 
415addc5ddSAl Viro static int event;
4273cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
43719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
445addc5ddSAl Viro 
45fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly;
46e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
47390c6843SRam Pai static struct rw_semaphore namespace_sem;
481da177e4SLinus Torvalds 
49f87fd4c2SMiklos Szeredi /* /sys/fs */
5000d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
5100d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
52f87fd4c2SMiklos Szeredi 
531da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
561da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
5713f14b4dSEric Dumazet 	tmp = tmp + (tmp >> HASH_SHIFT);
5813f14b4dSEric Dumazet 	return tmp & (HASH_SIZE - 1);
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
613d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
623d733633SDave Hansen 
6373cd49ecSMiklos Szeredi /* allocation is serialized by namespace_sem */
6473cd49ecSMiklos Szeredi static int mnt_alloc_id(struct vfsmount *mnt)
6573cd49ecSMiklos Szeredi {
6673cd49ecSMiklos Szeredi 	int res;
6773cd49ecSMiklos Szeredi 
6873cd49ecSMiklos Szeredi retry:
6973cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
7073cd49ecSMiklos Szeredi 	spin_lock(&vfsmount_lock);
7173cd49ecSMiklos Szeredi 	res = ida_get_new(&mnt_id_ida, &mnt->mnt_id);
7273cd49ecSMiklos Szeredi 	spin_unlock(&vfsmount_lock);
7373cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
7473cd49ecSMiklos Szeredi 		goto retry;
7573cd49ecSMiklos Szeredi 
7673cd49ecSMiklos Szeredi 	return res;
7773cd49ecSMiklos Szeredi }
7873cd49ecSMiklos Szeredi 
7973cd49ecSMiklos Szeredi static void mnt_free_id(struct vfsmount *mnt)
8073cd49ecSMiklos Szeredi {
8173cd49ecSMiklos Szeredi 	spin_lock(&vfsmount_lock);
8273cd49ecSMiklos Szeredi 	ida_remove(&mnt_id_ida, mnt->mnt_id);
8373cd49ecSMiklos Szeredi 	spin_unlock(&vfsmount_lock);
8473cd49ecSMiklos Szeredi }
8573cd49ecSMiklos Szeredi 
86719f5d7fSMiklos Szeredi /*
87719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
88719f5d7fSMiklos Szeredi  *
89719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
90719f5d7fSMiklos Szeredi  */
91719f5d7fSMiklos Szeredi static int mnt_alloc_group_id(struct vfsmount *mnt)
92719f5d7fSMiklos Szeredi {
93719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
94719f5d7fSMiklos Szeredi 		return -ENOMEM;
95719f5d7fSMiklos Szeredi 
96719f5d7fSMiklos Szeredi 	return ida_get_new_above(&mnt_group_ida, 1, &mnt->mnt_group_id);
97719f5d7fSMiklos Szeredi }
98719f5d7fSMiklos Szeredi 
99719f5d7fSMiklos Szeredi /*
100719f5d7fSMiklos Szeredi  * Release a peer group ID
101719f5d7fSMiklos Szeredi  */
102719f5d7fSMiklos Szeredi void mnt_release_group_id(struct vfsmount *mnt)
103719f5d7fSMiklos Szeredi {
104719f5d7fSMiklos Szeredi 	ida_remove(&mnt_group_ida, mnt->mnt_group_id);
105719f5d7fSMiklos Szeredi 	mnt->mnt_group_id = 0;
106719f5d7fSMiklos Szeredi }
107719f5d7fSMiklos Szeredi 
1081da177e4SLinus Torvalds struct vfsmount *alloc_vfsmnt(const char *name)
1091da177e4SLinus Torvalds {
110c3762229SRobert P. J. Day 	struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
1111da177e4SLinus Torvalds 	if (mnt) {
11273cd49ecSMiklos Szeredi 		int err;
11373cd49ecSMiklos Szeredi 
11473cd49ecSMiklos Szeredi 		err = mnt_alloc_id(mnt);
11573cd49ecSMiklos Szeredi 		if (err) {
11673cd49ecSMiklos Szeredi 			kmem_cache_free(mnt_cache, mnt);
11773cd49ecSMiklos Szeredi 			return NULL;
11873cd49ecSMiklos Szeredi 		}
11973cd49ecSMiklos Szeredi 
1201da177e4SLinus Torvalds 		atomic_set(&mnt->mnt_count, 1);
1211da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_hash);
1221da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_child);
1231da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_mounts);
1241da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_list);
12555e700b9SMiklos Szeredi 		INIT_LIST_HEAD(&mnt->mnt_expire);
12603e06e68SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_share);
127a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
128a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave);
1293d733633SDave Hansen 		atomic_set(&mnt->__mnt_writers, 0);
1301da177e4SLinus Torvalds 		if (name) {
1311da177e4SLinus Torvalds 			int size = strlen(name) + 1;
1321da177e4SLinus Torvalds 			char *newname = kmalloc(size, GFP_KERNEL);
1331da177e4SLinus Torvalds 			if (newname) {
1341da177e4SLinus Torvalds 				memcpy(newname, name, size);
1351da177e4SLinus Torvalds 				mnt->mnt_devname = newname;
1361da177e4SLinus Torvalds 			}
1371da177e4SLinus Torvalds 		}
1381da177e4SLinus Torvalds 	}
1391da177e4SLinus Torvalds 	return mnt;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1428366025eSDave Hansen /*
1438366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
1448366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
1458366025eSDave Hansen  * We must keep track of when those operations start
1468366025eSDave Hansen  * (for permission checks) and when they end, so that
1478366025eSDave Hansen  * we can determine when writes are able to occur to
1488366025eSDave Hansen  * a filesystem.
1498366025eSDave Hansen  */
1503d733633SDave Hansen /*
1513d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
1523d733633SDave Hansen  * @mnt: the mount to check for its write status
1533d733633SDave Hansen  *
1543d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
1553d733633SDave Hansen  * It does not guarantee that the filesystem will stay
1563d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
1573d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
1583d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
1593d733633SDave Hansen  * r/w.
1603d733633SDave Hansen  */
1613d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
1623d733633SDave Hansen {
1632e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
1642e4b7fcdSDave Hansen 		return 1;
1652e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
1662e4b7fcdSDave Hansen 		return 1;
1672e4b7fcdSDave Hansen 	return 0;
1683d733633SDave Hansen }
1693d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
1703d733633SDave Hansen 
1713d733633SDave Hansen struct mnt_writer {
1723d733633SDave Hansen 	/*
1733d733633SDave Hansen 	 * If holding multiple instances of this lock, they
1743d733633SDave Hansen 	 * must be ordered by cpu number.
1753d733633SDave Hansen 	 */
1763d733633SDave Hansen 	spinlock_t lock;
1773d733633SDave Hansen 	struct lock_class_key lock_class; /* compiles out with !lockdep */
1783d733633SDave Hansen 	unsigned long count;
1793d733633SDave Hansen 	struct vfsmount *mnt;
1803d733633SDave Hansen } ____cacheline_aligned_in_smp;
1813d733633SDave Hansen static DEFINE_PER_CPU(struct mnt_writer, mnt_writers);
1823d733633SDave Hansen 
1833d733633SDave Hansen static int __init init_mnt_writers(void)
1843d733633SDave Hansen {
1853d733633SDave Hansen 	int cpu;
1863d733633SDave Hansen 	for_each_possible_cpu(cpu) {
1873d733633SDave Hansen 		struct mnt_writer *writer = &per_cpu(mnt_writers, cpu);
1883d733633SDave Hansen 		spin_lock_init(&writer->lock);
1893d733633SDave Hansen 		lockdep_set_class(&writer->lock, &writer->lock_class);
1903d733633SDave Hansen 		writer->count = 0;
1913d733633SDave Hansen 	}
1923d733633SDave Hansen 	return 0;
1933d733633SDave Hansen }
1943d733633SDave Hansen fs_initcall(init_mnt_writers);
1953d733633SDave Hansen 
1963d733633SDave Hansen static void unlock_mnt_writers(void)
1973d733633SDave Hansen {
1983d733633SDave Hansen 	int cpu;
1993d733633SDave Hansen 	struct mnt_writer *cpu_writer;
2003d733633SDave Hansen 
2013d733633SDave Hansen 	for_each_possible_cpu(cpu) {
2023d733633SDave Hansen 		cpu_writer = &per_cpu(mnt_writers, cpu);
2033d733633SDave Hansen 		spin_unlock(&cpu_writer->lock);
2043d733633SDave Hansen 	}
2053d733633SDave Hansen }
2063d733633SDave Hansen 
2073d733633SDave Hansen static inline void __clear_mnt_count(struct mnt_writer *cpu_writer)
2083d733633SDave Hansen {
2093d733633SDave Hansen 	if (!cpu_writer->mnt)
2103d733633SDave Hansen 		return;
2113d733633SDave Hansen 	/*
2123d733633SDave Hansen 	 * This is in case anyone ever leaves an invalid,
2133d733633SDave Hansen 	 * old ->mnt and a count of 0.
2143d733633SDave Hansen 	 */
2153d733633SDave Hansen 	if (!cpu_writer->count)
2163d733633SDave Hansen 		return;
2173d733633SDave Hansen 	atomic_add(cpu_writer->count, &cpu_writer->mnt->__mnt_writers);
2183d733633SDave Hansen 	cpu_writer->count = 0;
2193d733633SDave Hansen }
2203d733633SDave Hansen  /*
2213d733633SDave Hansen  * must hold cpu_writer->lock
2223d733633SDave Hansen  */
2233d733633SDave Hansen static inline void use_cpu_writer_for_mount(struct mnt_writer *cpu_writer,
2243d733633SDave Hansen 					  struct vfsmount *mnt)
2253d733633SDave Hansen {
2263d733633SDave Hansen 	if (cpu_writer->mnt == mnt)
2273d733633SDave Hansen 		return;
2283d733633SDave Hansen 	__clear_mnt_count(cpu_writer);
2293d733633SDave Hansen 	cpu_writer->mnt = mnt;
2303d733633SDave Hansen }
2313d733633SDave Hansen 
2323d733633SDave Hansen /*
2333d733633SDave Hansen  * Most r/o checks on a fs are for operations that take
2343d733633SDave Hansen  * discrete amounts of time, like a write() or unlink().
2353d733633SDave Hansen  * We must keep track of when those operations start
2363d733633SDave Hansen  * (for permission checks) and when they end, so that
2373d733633SDave Hansen  * we can determine when writes are able to occur to
2383d733633SDave Hansen  * a filesystem.
2393d733633SDave Hansen  */
2408366025eSDave Hansen /**
2418366025eSDave Hansen  * mnt_want_write - get write access to a mount
2428366025eSDave Hansen  * @mnt: the mount on which to take a write
2438366025eSDave Hansen  *
2448366025eSDave Hansen  * This tells the low-level filesystem that a write is
2458366025eSDave Hansen  * about to be performed to it, and makes sure that
2468366025eSDave Hansen  * writes are allowed before returning success.  When
2478366025eSDave Hansen  * the write operation is finished, mnt_drop_write()
2488366025eSDave Hansen  * must be called.  This is effectively a refcount.
2498366025eSDave Hansen  */
2508366025eSDave Hansen int mnt_want_write(struct vfsmount *mnt)
2518366025eSDave Hansen {
2523d733633SDave Hansen 	int ret = 0;
2533d733633SDave Hansen 	struct mnt_writer *cpu_writer;
2543d733633SDave Hansen 
2553d733633SDave Hansen 	cpu_writer = &get_cpu_var(mnt_writers);
2563d733633SDave Hansen 	spin_lock(&cpu_writer->lock);
2573d733633SDave Hansen 	if (__mnt_is_readonly(mnt)) {
2583d733633SDave Hansen 		ret = -EROFS;
2593d733633SDave Hansen 		goto out;
2603d733633SDave Hansen 	}
2613d733633SDave Hansen 	use_cpu_writer_for_mount(cpu_writer, mnt);
2623d733633SDave Hansen 	cpu_writer->count++;
2633d733633SDave Hansen out:
2643d733633SDave Hansen 	spin_unlock(&cpu_writer->lock);
2653d733633SDave Hansen 	put_cpu_var(mnt_writers);
2663d733633SDave Hansen 	return ret;
2678366025eSDave Hansen }
2688366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
2698366025eSDave Hansen 
2703d733633SDave Hansen static void lock_mnt_writers(void)
2713d733633SDave Hansen {
2723d733633SDave Hansen 	int cpu;
2733d733633SDave Hansen 	struct mnt_writer *cpu_writer;
2743d733633SDave Hansen 
2753d733633SDave Hansen 	for_each_possible_cpu(cpu) {
2763d733633SDave Hansen 		cpu_writer = &per_cpu(mnt_writers, cpu);
2773d733633SDave Hansen 		spin_lock(&cpu_writer->lock);
2783d733633SDave Hansen 		__clear_mnt_count(cpu_writer);
2793d733633SDave Hansen 		cpu_writer->mnt = NULL;
2803d733633SDave Hansen 	}
2813d733633SDave Hansen }
2823d733633SDave Hansen 
2833d733633SDave Hansen /*
2843d733633SDave Hansen  * These per-cpu write counts are not guaranteed to have
2853d733633SDave Hansen  * matched increments and decrements on any given cpu.
2863d733633SDave Hansen  * A file open()ed for write on one cpu and close()d on
2873d733633SDave Hansen  * another cpu will imbalance this count.  Make sure it
2883d733633SDave Hansen  * does not get too far out of whack.
2893d733633SDave Hansen  */
2903d733633SDave Hansen static void handle_write_count_underflow(struct vfsmount *mnt)
2913d733633SDave Hansen {
2923d733633SDave Hansen 	if (atomic_read(&mnt->__mnt_writers) >=
2933d733633SDave Hansen 	    MNT_WRITER_UNDERFLOW_LIMIT)
2943d733633SDave Hansen 		return;
2953d733633SDave Hansen 	/*
2963d733633SDave Hansen 	 * It isn't necessary to hold all of the locks
2973d733633SDave Hansen 	 * at the same time, but doing it this way makes
2983d733633SDave Hansen 	 * us share a lot more code.
2993d733633SDave Hansen 	 */
3003d733633SDave Hansen 	lock_mnt_writers();
3013d733633SDave Hansen 	/*
3023d733633SDave Hansen 	 * vfsmount_lock is for mnt_flags.
3033d733633SDave Hansen 	 */
3043d733633SDave Hansen 	spin_lock(&vfsmount_lock);
3053d733633SDave Hansen 	/*
3063d733633SDave Hansen 	 * If coalescing the per-cpu writer counts did not
3073d733633SDave Hansen 	 * get us back to a positive writer count, we have
3083d733633SDave Hansen 	 * a bug.
3093d733633SDave Hansen 	 */
3103d733633SDave Hansen 	if ((atomic_read(&mnt->__mnt_writers) < 0) &&
3113d733633SDave Hansen 	    !(mnt->mnt_flags & MNT_IMBALANCED_WRITE_COUNT)) {
3125c752ad9SArjan van de Ven 		WARN(1, KERN_DEBUG "leak detected on mount(%p) writers "
3133d733633SDave Hansen 				"count: %d\n",
3143d733633SDave Hansen 			mnt, atomic_read(&mnt->__mnt_writers));
3153d733633SDave Hansen 		/* use the flag to keep the dmesg spam down */
3163d733633SDave Hansen 		mnt->mnt_flags |= MNT_IMBALANCED_WRITE_COUNT;
3173d733633SDave Hansen 	}
3183d733633SDave Hansen 	spin_unlock(&vfsmount_lock);
3193d733633SDave Hansen 	unlock_mnt_writers();
3203d733633SDave Hansen }
3213d733633SDave Hansen 
3228366025eSDave Hansen /**
3238366025eSDave Hansen  * mnt_drop_write - give up write access to a mount
3248366025eSDave Hansen  * @mnt: the mount on which to give up write access
3258366025eSDave Hansen  *
3268366025eSDave Hansen  * Tells the low-level filesystem that we are done
3278366025eSDave Hansen  * performing writes to it.  Must be matched with
3288366025eSDave Hansen  * mnt_want_write() call above.
3298366025eSDave Hansen  */
3308366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt)
3318366025eSDave Hansen {
3323d733633SDave Hansen 	int must_check_underflow = 0;
3333d733633SDave Hansen 	struct mnt_writer *cpu_writer;
3343d733633SDave Hansen 
3353d733633SDave Hansen 	cpu_writer = &get_cpu_var(mnt_writers);
3363d733633SDave Hansen 	spin_lock(&cpu_writer->lock);
3373d733633SDave Hansen 
3383d733633SDave Hansen 	use_cpu_writer_for_mount(cpu_writer, mnt);
3393d733633SDave Hansen 	if (cpu_writer->count > 0) {
3403d733633SDave Hansen 		cpu_writer->count--;
3413d733633SDave Hansen 	} else {
3423d733633SDave Hansen 		must_check_underflow = 1;
3433d733633SDave Hansen 		atomic_dec(&mnt->__mnt_writers);
3443d733633SDave Hansen 	}
3453d733633SDave Hansen 
3463d733633SDave Hansen 	spin_unlock(&cpu_writer->lock);
3473d733633SDave Hansen 	/*
3483d733633SDave Hansen 	 * Logically, we could call this each time,
3493d733633SDave Hansen 	 * but the __mnt_writers cacheline tends to
3503d733633SDave Hansen 	 * be cold, and makes this expensive.
3513d733633SDave Hansen 	 */
3523d733633SDave Hansen 	if (must_check_underflow)
3533d733633SDave Hansen 		handle_write_count_underflow(mnt);
3543d733633SDave Hansen 	/*
3553d733633SDave Hansen 	 * This could be done right after the spinlock
3563d733633SDave Hansen 	 * is taken because the spinlock keeps us on
3573d733633SDave Hansen 	 * the cpu, and disables preemption.  However,
3583d733633SDave Hansen 	 * putting it here bounds the amount that
3593d733633SDave Hansen 	 * __mnt_writers can underflow.  Without it,
3603d733633SDave Hansen 	 * we could theoretically wrap __mnt_writers.
3613d733633SDave Hansen 	 */
3623d733633SDave Hansen 	put_cpu_var(mnt_writers);
3638366025eSDave Hansen }
3648366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
3658366025eSDave Hansen 
3662e4b7fcdSDave Hansen static int mnt_make_readonly(struct vfsmount *mnt)
3678366025eSDave Hansen {
3683d733633SDave Hansen 	int ret = 0;
3693d733633SDave Hansen 
3703d733633SDave Hansen 	lock_mnt_writers();
3713d733633SDave Hansen 	/*
3723d733633SDave Hansen 	 * With all the locks held, this value is stable
3733d733633SDave Hansen 	 */
3743d733633SDave Hansen 	if (atomic_read(&mnt->__mnt_writers) > 0) {
3753d733633SDave Hansen 		ret = -EBUSY;
3763d733633SDave Hansen 		goto out;
3778366025eSDave Hansen 	}
3783d733633SDave Hansen 	/*
3792e4b7fcdSDave Hansen 	 * nobody can do a successful mnt_want_write() with all
3802e4b7fcdSDave Hansen 	 * of the counts in MNT_DENIED_WRITE and the locks held.
3813d733633SDave Hansen 	 */
3822e4b7fcdSDave Hansen 	spin_lock(&vfsmount_lock);
3832e4b7fcdSDave Hansen 	if (!ret)
3842e4b7fcdSDave Hansen 		mnt->mnt_flags |= MNT_READONLY;
3852e4b7fcdSDave Hansen 	spin_unlock(&vfsmount_lock);
3863d733633SDave Hansen out:
3873d733633SDave Hansen 	unlock_mnt_writers();
3883d733633SDave Hansen 	return ret;
3893d733633SDave Hansen }
3908366025eSDave Hansen 
3912e4b7fcdSDave Hansen static void __mnt_unmake_readonly(struct vfsmount *mnt)
3922e4b7fcdSDave Hansen {
3932e4b7fcdSDave Hansen 	spin_lock(&vfsmount_lock);
3942e4b7fcdSDave Hansen 	mnt->mnt_flags &= ~MNT_READONLY;
3952e4b7fcdSDave Hansen 	spin_unlock(&vfsmount_lock);
3962e4b7fcdSDave Hansen }
3972e4b7fcdSDave Hansen 
398454e2398SDavid Howells int simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
399454e2398SDavid Howells {
400454e2398SDavid Howells 	mnt->mnt_sb = sb;
401454e2398SDavid Howells 	mnt->mnt_root = dget(sb->s_root);
402454e2398SDavid Howells 	return 0;
403454e2398SDavid Howells }
404454e2398SDavid Howells 
405454e2398SDavid Howells EXPORT_SYMBOL(simple_set_mnt);
406454e2398SDavid Howells 
4071da177e4SLinus Torvalds void free_vfsmnt(struct vfsmount *mnt)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	kfree(mnt->mnt_devname);
41073cd49ecSMiklos Szeredi 	mnt_free_id(mnt);
4111da177e4SLinus Torvalds 	kmem_cache_free(mnt_cache, mnt);
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /*
415a05964f3SRam Pai  * find the first or last mount at @dentry on vfsmount @mnt depending on
416a05964f3SRam Pai  * @dir. If @dir is set return the first mount else return the last mount.
4171da177e4SLinus Torvalds  */
418a05964f3SRam Pai struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
419a05964f3SRam Pai 			      int dir)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
4221da177e4SLinus Torvalds 	struct list_head *tmp = head;
4231da177e4SLinus Torvalds 	struct vfsmount *p, *found = NULL;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	for (;;) {
426a05964f3SRam Pai 		tmp = dir ? tmp->next : tmp->prev;
4271da177e4SLinus Torvalds 		p = NULL;
4281da177e4SLinus Torvalds 		if (tmp == head)
4291da177e4SLinus Torvalds 			break;
4301da177e4SLinus Torvalds 		p = list_entry(tmp, struct vfsmount, mnt_hash);
4311da177e4SLinus Torvalds 		if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
432a05964f3SRam Pai 			found = p;
4331da177e4SLinus Torvalds 			break;
4341da177e4SLinus Torvalds 		}
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 	return found;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
439a05964f3SRam Pai /*
440a05964f3SRam Pai  * lookup_mnt increments the ref count before returning
441a05964f3SRam Pai  * the vfsmount struct.
442a05964f3SRam Pai  */
443a05964f3SRam Pai struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
444a05964f3SRam Pai {
445a05964f3SRam Pai 	struct vfsmount *child_mnt;
446a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
447a05964f3SRam Pai 	if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
448a05964f3SRam Pai 		mntget(child_mnt);
449a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
450a05964f3SRam Pai 	return child_mnt;
451a05964f3SRam Pai }
452a05964f3SRam Pai 
4531da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt)
4541da177e4SLinus Torvalds {
4556b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4586b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
4595addc5ddSAl Viro {
4605addc5ddSAl Viro 	if (ns) {
4615addc5ddSAl Viro 		ns->event = ++event;
4625addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
4635addc5ddSAl Viro 	}
4645addc5ddSAl Viro }
4655addc5ddSAl Viro 
4666b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
4675addc5ddSAl Viro {
4685addc5ddSAl Viro 	if (ns && ns->event != event) {
4695addc5ddSAl Viro 		ns->event = event;
4705addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
4715addc5ddSAl Viro 	}
4725addc5ddSAl Viro }
4735addc5ddSAl Viro 
4741a390689SAl Viro static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
4751da177e4SLinus Torvalds {
4761a390689SAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
4771a390689SAl Viro 	old_path->mnt = mnt->mnt_parent;
4781da177e4SLinus Torvalds 	mnt->mnt_parent = mnt;
4791da177e4SLinus Torvalds 	mnt->mnt_mountpoint = mnt->mnt_root;
4801da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_child);
4811da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_hash);
4821a390689SAl Viro 	old_path->dentry->d_mounted--;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
485b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
486b90fa9aeSRam Pai 			struct vfsmount *child_mnt)
487b90fa9aeSRam Pai {
488b90fa9aeSRam Pai 	child_mnt->mnt_parent = mntget(mnt);
489b90fa9aeSRam Pai 	child_mnt->mnt_mountpoint = dget(dentry);
490b90fa9aeSRam Pai 	dentry->d_mounted++;
491b90fa9aeSRam Pai }
492b90fa9aeSRam Pai 
4931a390689SAl Viro static void attach_mnt(struct vfsmount *mnt, struct path *path)
4941da177e4SLinus Torvalds {
4951a390689SAl Viro 	mnt_set_mountpoint(path->mnt, path->dentry, mnt);
496b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
4971a390689SAl Viro 			hash(path->mnt, path->dentry));
4981a390689SAl Viro 	list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
499b90fa9aeSRam Pai }
500b90fa9aeSRam Pai 
501b90fa9aeSRam Pai /*
502b90fa9aeSRam Pai  * the caller must hold vfsmount_lock
503b90fa9aeSRam Pai  */
504b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt)
505b90fa9aeSRam Pai {
506b90fa9aeSRam Pai 	struct vfsmount *parent = mnt->mnt_parent;
507b90fa9aeSRam Pai 	struct vfsmount *m;
508b90fa9aeSRam Pai 	LIST_HEAD(head);
5096b3286edSKirill Korotaev 	struct mnt_namespace *n = parent->mnt_ns;
510b90fa9aeSRam Pai 
511b90fa9aeSRam Pai 	BUG_ON(parent == mnt);
512b90fa9aeSRam Pai 
513b90fa9aeSRam Pai 	list_add_tail(&head, &mnt->mnt_list);
514b90fa9aeSRam Pai 	list_for_each_entry(m, &head, mnt_list)
5156b3286edSKirill Korotaev 		m->mnt_ns = n;
516b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
517b90fa9aeSRam Pai 
518b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
519b90fa9aeSRam Pai 				hash(parent, mnt->mnt_mountpoint));
520b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
5216b3286edSKirill Korotaev 	touch_mnt_namespace(n);
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct list_head *next = p->mnt_mounts.next;
5271da177e4SLinus Torvalds 	if (next == &p->mnt_mounts) {
5281da177e4SLinus Torvalds 		while (1) {
5291da177e4SLinus Torvalds 			if (p == root)
5301da177e4SLinus Torvalds 				return NULL;
5311da177e4SLinus Torvalds 			next = p->mnt_child.next;
5321da177e4SLinus Torvalds 			if (next != &p->mnt_parent->mnt_mounts)
5331da177e4SLinus Torvalds 				break;
5341da177e4SLinus Torvalds 			p = p->mnt_parent;
5351da177e4SLinus Torvalds 		}
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 	return list_entry(next, struct vfsmount, mnt_child);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5409676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
5419676f0c6SRam Pai {
5429676f0c6SRam Pai 	struct list_head *prev = p->mnt_mounts.prev;
5439676f0c6SRam Pai 	while (prev != &p->mnt_mounts) {
5449676f0c6SRam Pai 		p = list_entry(prev, struct vfsmount, mnt_child);
5459676f0c6SRam Pai 		prev = p->mnt_mounts.prev;
5469676f0c6SRam Pai 	}
5479676f0c6SRam Pai 	return p;
5489676f0c6SRam Pai }
5499676f0c6SRam Pai 
55036341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
55136341f64SRam Pai 					int flag)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds 	struct super_block *sb = old->mnt_sb;
5541da177e4SLinus Torvalds 	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (mnt) {
557719f5d7fSMiklos Szeredi 		if (flag & (CL_SLAVE | CL_PRIVATE))
558719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = 0; /* not a peer of original */
559719f5d7fSMiklos Szeredi 		else
560719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = old->mnt_group_id;
561719f5d7fSMiklos Szeredi 
562719f5d7fSMiklos Szeredi 		if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
563719f5d7fSMiklos Szeredi 			int err = mnt_alloc_group_id(mnt);
564719f5d7fSMiklos Szeredi 			if (err)
565719f5d7fSMiklos Szeredi 				goto out_free;
566719f5d7fSMiklos Szeredi 		}
567719f5d7fSMiklos Szeredi 
5681da177e4SLinus Torvalds 		mnt->mnt_flags = old->mnt_flags;
5691da177e4SLinus Torvalds 		atomic_inc(&sb->s_active);
5701da177e4SLinus Torvalds 		mnt->mnt_sb = sb;
5711da177e4SLinus Torvalds 		mnt->mnt_root = dget(root);
5721da177e4SLinus Torvalds 		mnt->mnt_mountpoint = mnt->mnt_root;
5731da177e4SLinus Torvalds 		mnt->mnt_parent = mnt;
574b90fa9aeSRam Pai 
5755afe0022SRam Pai 		if (flag & CL_SLAVE) {
5765afe0022SRam Pai 			list_add(&mnt->mnt_slave, &old->mnt_slave_list);
5775afe0022SRam Pai 			mnt->mnt_master = old;
5785afe0022SRam Pai 			CLEAR_MNT_SHARED(mnt);
5798aec0809SAl Viro 		} else if (!(flag & CL_PRIVATE)) {
580b90fa9aeSRam Pai 			if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
581b90fa9aeSRam Pai 				list_add(&mnt->mnt_share, &old->mnt_share);
5825afe0022SRam Pai 			if (IS_MNT_SLAVE(old))
5835afe0022SRam Pai 				list_add(&mnt->mnt_slave, &old->mnt_slave);
5845afe0022SRam Pai 			mnt->mnt_master = old->mnt_master;
5855afe0022SRam Pai 		}
586b90fa9aeSRam Pai 		if (flag & CL_MAKE_SHARED)
587b90fa9aeSRam Pai 			set_mnt_shared(mnt);
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 		/* stick the duplicate mount on the same expiry list
5901da177e4SLinus Torvalds 		 * as the original if that was on one */
59136341f64SRam Pai 		if (flag & CL_EXPIRE) {
59255e700b9SMiklos Szeredi 			if (!list_empty(&old->mnt_expire))
59355e700b9SMiklos Szeredi 				list_add(&mnt->mnt_expire, &old->mnt_expire);
5941da177e4SLinus Torvalds 		}
59536341f64SRam Pai 	}
5961da177e4SLinus Torvalds 	return mnt;
597719f5d7fSMiklos Szeredi 
598719f5d7fSMiklos Szeredi  out_free:
599719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
600719f5d7fSMiklos Szeredi 	return NULL;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6037b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt)
6041da177e4SLinus Torvalds {
6053d733633SDave Hansen 	int cpu;
6061da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
6073d733633SDave Hansen 	/*
6083d733633SDave Hansen 	 * We don't have to hold all of the locks at the
6093d733633SDave Hansen 	 * same time here because we know that we're the
6103d733633SDave Hansen 	 * last reference to mnt and that no new writers
6113d733633SDave Hansen 	 * can come in.
6123d733633SDave Hansen 	 */
6133d733633SDave Hansen 	for_each_possible_cpu(cpu) {
6143d733633SDave Hansen 		struct mnt_writer *cpu_writer = &per_cpu(mnt_writers, cpu);
6153d733633SDave Hansen 		if (cpu_writer->mnt != mnt)
6163d733633SDave Hansen 			continue;
6173d733633SDave Hansen 		spin_lock(&cpu_writer->lock);
6183d733633SDave Hansen 		atomic_add(cpu_writer->count, &mnt->__mnt_writers);
6193d733633SDave Hansen 		cpu_writer->count = 0;
6203d733633SDave Hansen 		/*
6213d733633SDave Hansen 		 * Might as well do this so that no one
6223d733633SDave Hansen 		 * ever sees the pointer and expects
6233d733633SDave Hansen 		 * it to be valid.
6243d733633SDave Hansen 		 */
6253d733633SDave Hansen 		cpu_writer->mnt = NULL;
6263d733633SDave Hansen 		spin_unlock(&cpu_writer->lock);
6273d733633SDave Hansen 	}
6283d733633SDave Hansen 	/*
6293d733633SDave Hansen 	 * This probably indicates that somebody messed
6303d733633SDave Hansen 	 * up a mnt_want/drop_write() pair.  If this
6313d733633SDave Hansen 	 * happens, the filesystem was probably unable
6323d733633SDave Hansen 	 * to make r/w->r/o transitions.
6333d733633SDave Hansen 	 */
6343d733633SDave Hansen 	WARN_ON(atomic_read(&mnt->__mnt_writers));
6351da177e4SLinus Torvalds 	dput(mnt->mnt_root);
6361da177e4SLinus Torvalds 	free_vfsmnt(mnt);
6371da177e4SLinus Torvalds 	deactivate_super(sb);
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6407b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt)
6417b7b1aceSAl Viro {
6427b7b1aceSAl Viro repeat:
6437b7b1aceSAl Viro 	if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
6447b7b1aceSAl Viro 		if (likely(!mnt->mnt_pinned)) {
6457b7b1aceSAl Viro 			spin_unlock(&vfsmount_lock);
6467b7b1aceSAl Viro 			__mntput(mnt);
6477b7b1aceSAl Viro 			return;
6487b7b1aceSAl Viro 		}
6497b7b1aceSAl Viro 		atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
6507b7b1aceSAl Viro 		mnt->mnt_pinned = 0;
6517b7b1aceSAl Viro 		spin_unlock(&vfsmount_lock);
6527b7b1aceSAl Viro 		acct_auto_close_mnt(mnt);
6537b7b1aceSAl Viro 		security_sb_umount_close(mnt);
6547b7b1aceSAl Viro 		goto repeat;
6557b7b1aceSAl Viro 	}
6567b7b1aceSAl Viro }
6577b7b1aceSAl Viro 
6587b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire);
6597b7b1aceSAl Viro 
6607b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
6617b7b1aceSAl Viro {
6627b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
6637b7b1aceSAl Viro 	mnt->mnt_pinned++;
6647b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
6657b7b1aceSAl Viro }
6667b7b1aceSAl Viro 
6677b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
6687b7b1aceSAl Viro 
6697b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt)
6707b7b1aceSAl Viro {
6717b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
6727b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
6737b7b1aceSAl Viro 		atomic_inc(&mnt->mnt_count);
6747b7b1aceSAl Viro 		mnt->mnt_pinned--;
6757b7b1aceSAl Viro 	}
6767b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
6777b7b1aceSAl Viro }
6787b7b1aceSAl Viro 
6797b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
6801da177e4SLinus Torvalds 
681b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
682b3b304a2SMiklos Szeredi {
683b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
684b3b304a2SMiklos Szeredi }
685b3b304a2SMiklos Szeredi 
686b3b304a2SMiklos Szeredi /*
687b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
688b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
689b3b304a2SMiklos Szeredi  *
690b3b304a2SMiklos Szeredi  * See also save_mount_options().
691b3b304a2SMiklos Szeredi  */
692b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
693b3b304a2SMiklos Szeredi {
694b3b304a2SMiklos Szeredi 	const char *options = mnt->mnt_sb->s_options;
695b3b304a2SMiklos Szeredi 
696b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
697b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
698b3b304a2SMiklos Szeredi 		mangle(m, options);
699b3b304a2SMiklos Szeredi 	}
700b3b304a2SMiklos Szeredi 
701b3b304a2SMiklos Szeredi 	return 0;
702b3b304a2SMiklos Szeredi }
703b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
704b3b304a2SMiklos Szeredi 
705b3b304a2SMiklos Szeredi /*
706b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
707b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
708b3b304a2SMiklos Szeredi  *
709b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
710b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
711b3b304a2SMiklos Szeredi  * remount fails.
712b3b304a2SMiklos Szeredi  *
713b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
714b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
715b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
716b3b304a2SMiklos Szeredi  * any more.
717b3b304a2SMiklos Szeredi  */
718b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
719b3b304a2SMiklos Szeredi {
720b3b304a2SMiklos Szeredi 	kfree(sb->s_options);
721b3b304a2SMiklos Szeredi 	sb->s_options = kstrdup(options, GFP_KERNEL);
722b3b304a2SMiklos Szeredi }
723b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
724b3b304a2SMiklos Szeredi 
725a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
7261da177e4SLinus Torvalds /* iterator */
7271da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
7281da177e4SLinus Torvalds {
729a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
7301da177e4SLinus Torvalds 
731390c6843SRam Pai 	down_read(&namespace_sem);
732a1a2c409SMiklos Szeredi 	return seq_list_start(&p->ns->list, *pos);
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
7361da177e4SLinus Torvalds {
737a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
738b0765fb8SPavel Emelianov 
739a1a2c409SMiklos Szeredi 	return seq_list_next(v, &p->ns->list, pos);
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
7431da177e4SLinus Torvalds {
744390c6843SRam Pai 	up_read(&namespace_sem);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7472d4d4864SRam Pai struct proc_fs_info {
7481da177e4SLinus Torvalds 	int flag;
7492d4d4864SRam Pai 	const char *str;
7502d4d4864SRam Pai };
7512d4d4864SRam Pai 
7522069f457SEric Paris static int show_sb_opts(struct seq_file *m, struct super_block *sb)
7532d4d4864SRam Pai {
7542d4d4864SRam Pai 	static const struct proc_fs_info fs_info[] = {
7551da177e4SLinus Torvalds 		{ MS_SYNCHRONOUS, ",sync" },
7561da177e4SLinus Torvalds 		{ MS_DIRSYNC, ",dirsync" },
7571da177e4SLinus Torvalds 		{ MS_MANDLOCK, ",mand" },
7581da177e4SLinus Torvalds 		{ 0, NULL }
7591da177e4SLinus Torvalds 	};
7602d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
7612d4d4864SRam Pai 
7622d4d4864SRam Pai 	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
7632d4d4864SRam Pai 		if (sb->s_flags & fs_infop->flag)
7642d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
7652d4d4864SRam Pai 	}
7662069f457SEric Paris 
7672069f457SEric Paris 	return security_sb_show_options(m, sb);
7682d4d4864SRam Pai }
7692d4d4864SRam Pai 
7702d4d4864SRam Pai static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
7712d4d4864SRam Pai {
7722d4d4864SRam Pai 	static const struct proc_fs_info mnt_info[] = {
7731da177e4SLinus Torvalds 		{ MNT_NOSUID, ",nosuid" },
7741da177e4SLinus Torvalds 		{ MNT_NODEV, ",nodev" },
7751da177e4SLinus Torvalds 		{ MNT_NOEXEC, ",noexec" },
776fc33a7bbSChristoph Hellwig 		{ MNT_NOATIME, ",noatime" },
777fc33a7bbSChristoph Hellwig 		{ MNT_NODIRATIME, ",nodiratime" },
77847ae32d6SValerie Henson 		{ MNT_RELATIME, ",relatime" },
7791da177e4SLinus Torvalds 		{ 0, NULL }
7801da177e4SLinus Torvalds 	};
7812d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
7822d4d4864SRam Pai 
7832d4d4864SRam Pai 	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
7842d4d4864SRam Pai 		if (mnt->mnt_flags & fs_infop->flag)
7852d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
7862d4d4864SRam Pai 	}
7872d4d4864SRam Pai }
7882d4d4864SRam Pai 
7892d4d4864SRam Pai static void show_type(struct seq_file *m, struct super_block *sb)
7902d4d4864SRam Pai {
7912d4d4864SRam Pai 	mangle(m, sb->s_type->name);
7922d4d4864SRam Pai 	if (sb->s_subtype && sb->s_subtype[0]) {
7932d4d4864SRam Pai 		seq_putc(m, '.');
7942d4d4864SRam Pai 		mangle(m, sb->s_subtype);
7952d4d4864SRam Pai 	}
7962d4d4864SRam Pai }
7972d4d4864SRam Pai 
7982d4d4864SRam Pai static int show_vfsmnt(struct seq_file *m, void *v)
7992d4d4864SRam Pai {
8002d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
8012d4d4864SRam Pai 	int err = 0;
802c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
8051da177e4SLinus Torvalds 	seq_putc(m, ' ');
806c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
8071da177e4SLinus Torvalds 	seq_putc(m, ' ');
8082d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
8092e4b7fcdSDave Hansen 	seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
8102069f457SEric Paris 	err = show_sb_opts(m, mnt->mnt_sb);
8112069f457SEric Paris 	if (err)
8122069f457SEric Paris 		goto out;
8132d4d4864SRam Pai 	show_mnt_opts(m, mnt);
8141da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_op->show_options)
8151da177e4SLinus Torvalds 		err = mnt->mnt_sb->s_op->show_options(m, mnt);
8161da177e4SLinus Torvalds 	seq_puts(m, " 0 0\n");
8172069f457SEric Paris out:
8181da177e4SLinus Torvalds 	return err;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
821a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
8221da177e4SLinus Torvalds 	.start	= m_start,
8231da177e4SLinus Torvalds 	.next	= m_next,
8241da177e4SLinus Torvalds 	.stop	= m_stop,
8251da177e4SLinus Torvalds 	.show	= show_vfsmnt
8261da177e4SLinus Torvalds };
8271da177e4SLinus Torvalds 
8282d4d4864SRam Pai static int show_mountinfo(struct seq_file *m, void *v)
8292d4d4864SRam Pai {
8302d4d4864SRam Pai 	struct proc_mounts *p = m->private;
8312d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
8322d4d4864SRam Pai 	struct super_block *sb = mnt->mnt_sb;
8332d4d4864SRam Pai 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
8342d4d4864SRam Pai 	struct path root = p->root;
8352d4d4864SRam Pai 	int err = 0;
8362d4d4864SRam Pai 
8372d4d4864SRam Pai 	seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
8382d4d4864SRam Pai 		   MAJOR(sb->s_dev), MINOR(sb->s_dev));
8392d4d4864SRam Pai 	seq_dentry(m, mnt->mnt_root, " \t\n\\");
8402d4d4864SRam Pai 	seq_putc(m, ' ');
8412d4d4864SRam Pai 	seq_path_root(m, &mnt_path, &root, " \t\n\\");
8422d4d4864SRam Pai 	if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
8432d4d4864SRam Pai 		/*
8442d4d4864SRam Pai 		 * Mountpoint is outside root, discard that one.  Ugly,
8452d4d4864SRam Pai 		 * but less so than trying to do that in iterator in a
8462d4d4864SRam Pai 		 * race-free way (due to renames).
8472d4d4864SRam Pai 		 */
8482d4d4864SRam Pai 		return SEQ_SKIP;
8492d4d4864SRam Pai 	}
8502d4d4864SRam Pai 	seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
8512d4d4864SRam Pai 	show_mnt_opts(m, mnt);
8522d4d4864SRam Pai 
8532d4d4864SRam Pai 	/* Tagged fields ("foo:X" or "bar") */
8542d4d4864SRam Pai 	if (IS_MNT_SHARED(mnt))
8552d4d4864SRam Pai 		seq_printf(m, " shared:%i", mnt->mnt_group_id);
85697e7e0f7SMiklos Szeredi 	if (IS_MNT_SLAVE(mnt)) {
85797e7e0f7SMiklos Szeredi 		int master = mnt->mnt_master->mnt_group_id;
85897e7e0f7SMiklos Szeredi 		int dom = get_dominating_id(mnt, &p->root);
85997e7e0f7SMiklos Szeredi 		seq_printf(m, " master:%i", master);
86097e7e0f7SMiklos Szeredi 		if (dom && dom != master)
86197e7e0f7SMiklos Szeredi 			seq_printf(m, " propagate_from:%i", dom);
86297e7e0f7SMiklos Szeredi 	}
8632d4d4864SRam Pai 	if (IS_MNT_UNBINDABLE(mnt))
8642d4d4864SRam Pai 		seq_puts(m, " unbindable");
8652d4d4864SRam Pai 
8662d4d4864SRam Pai 	/* Filesystem specific data */
8672d4d4864SRam Pai 	seq_puts(m, " - ");
8682d4d4864SRam Pai 	show_type(m, sb);
8692d4d4864SRam Pai 	seq_putc(m, ' ');
8702d4d4864SRam Pai 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
8712d4d4864SRam Pai 	seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
8722069f457SEric Paris 	err = show_sb_opts(m, sb);
8732069f457SEric Paris 	if (err)
8742069f457SEric Paris 		goto out;
8752d4d4864SRam Pai 	if (sb->s_op->show_options)
8762d4d4864SRam Pai 		err = sb->s_op->show_options(m, mnt);
8772d4d4864SRam Pai 	seq_putc(m, '\n');
8782069f457SEric Paris out:
8792d4d4864SRam Pai 	return err;
8802d4d4864SRam Pai }
8812d4d4864SRam Pai 
8822d4d4864SRam Pai const struct seq_operations mountinfo_op = {
8832d4d4864SRam Pai 	.start	= m_start,
8842d4d4864SRam Pai 	.next	= m_next,
8852d4d4864SRam Pai 	.stop	= m_stop,
8862d4d4864SRam Pai 	.show	= show_mountinfo,
8872d4d4864SRam Pai };
8882d4d4864SRam Pai 
889b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v)
890b4629fe2SChuck Lever {
891b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
892c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
893b4629fe2SChuck Lever 	int err = 0;
894b4629fe2SChuck Lever 
895b4629fe2SChuck Lever 	/* device */
896b4629fe2SChuck Lever 	if (mnt->mnt_devname) {
897b4629fe2SChuck Lever 		seq_puts(m, "device ");
898b4629fe2SChuck Lever 		mangle(m, mnt->mnt_devname);
899b4629fe2SChuck Lever 	} else
900b4629fe2SChuck Lever 		seq_puts(m, "no device");
901b4629fe2SChuck Lever 
902b4629fe2SChuck Lever 	/* mount point */
903b4629fe2SChuck Lever 	seq_puts(m, " mounted on ");
904c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
905b4629fe2SChuck Lever 	seq_putc(m, ' ');
906b4629fe2SChuck Lever 
907b4629fe2SChuck Lever 	/* file system type */
908b4629fe2SChuck Lever 	seq_puts(m, "with fstype ");
9092d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
910b4629fe2SChuck Lever 
911b4629fe2SChuck Lever 	/* optional statistics */
912b4629fe2SChuck Lever 	if (mnt->mnt_sb->s_op->show_stats) {
913b4629fe2SChuck Lever 		seq_putc(m, ' ');
914b4629fe2SChuck Lever 		err = mnt->mnt_sb->s_op->show_stats(m, mnt);
915b4629fe2SChuck Lever 	}
916b4629fe2SChuck Lever 
917b4629fe2SChuck Lever 	seq_putc(m, '\n');
918b4629fe2SChuck Lever 	return err;
919b4629fe2SChuck Lever }
920b4629fe2SChuck Lever 
921a1a2c409SMiklos Szeredi const struct seq_operations mountstats_op = {
922b4629fe2SChuck Lever 	.start	= m_start,
923b4629fe2SChuck Lever 	.next	= m_next,
924b4629fe2SChuck Lever 	.stop	= m_stop,
925b4629fe2SChuck Lever 	.show	= show_vfsstat,
926b4629fe2SChuck Lever };
927a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
928b4629fe2SChuck Lever 
9291da177e4SLinus Torvalds /**
9301da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
9311da177e4SLinus Torvalds  * @mnt: root of mount tree
9321da177e4SLinus Torvalds  *
9331da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
9341da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
9351da177e4SLinus Torvalds  * busy.
9361da177e4SLinus Torvalds  */
9371da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt)
9381da177e4SLinus Torvalds {
93936341f64SRam Pai 	int actual_refs = 0;
94036341f64SRam Pai 	int minimum_refs = 0;
94136341f64SRam Pai 	struct vfsmount *p;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
94436341f64SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
9451da177e4SLinus Torvalds 		actual_refs += atomic_read(&p->mnt_count);
9461da177e4SLinus Torvalds 		minimum_refs += 2;
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
9511da177e4SLinus Torvalds 		return 0;
952e3474a8eSIan Kent 
953e3474a8eSIan Kent 	return 1;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds /**
9591da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
9601da177e4SLinus Torvalds  * @mnt: root of mount
9611da177e4SLinus Torvalds  *
9621da177e4SLinus Torvalds  * This is called to check if a mount point has any
9631da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
9641da177e4SLinus Torvalds  * mount has sub mounts this will return busy
9651da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
9661da177e4SLinus Torvalds  *
9671da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
9681da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
9691da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
9701da177e4SLinus Torvalds  */
9711da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
9721da177e4SLinus Torvalds {
973e3474a8eSIan Kent 	int ret = 1;
974a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
975a05964f3SRam Pai 	if (propagate_mount_busy(mnt, 2))
976e3474a8eSIan Kent 		ret = 0;
977a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
978a05964f3SRam Pai 	return ret;
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
9821da177e4SLinus Torvalds 
983b90fa9aeSRam Pai void release_mounts(struct list_head *head)
9841da177e4SLinus Torvalds {
98570fbcdf4SRam Pai 	struct vfsmount *mnt;
98670fbcdf4SRam Pai 	while (!list_empty(head)) {
987b5e61818SPavel Emelianov 		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
98870fbcdf4SRam Pai 		list_del_init(&mnt->mnt_hash);
98970fbcdf4SRam Pai 		if (mnt->mnt_parent != mnt) {
99070fbcdf4SRam Pai 			struct dentry *dentry;
99170fbcdf4SRam Pai 			struct vfsmount *m;
99270fbcdf4SRam Pai 			spin_lock(&vfsmount_lock);
99370fbcdf4SRam Pai 			dentry = mnt->mnt_mountpoint;
99470fbcdf4SRam Pai 			m = mnt->mnt_parent;
99570fbcdf4SRam Pai 			mnt->mnt_mountpoint = mnt->mnt_root;
99670fbcdf4SRam Pai 			mnt->mnt_parent = mnt;
9977c4b93d8SAl Viro 			m->mnt_ghosts--;
9981da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
99970fbcdf4SRam Pai 			dput(dentry);
100070fbcdf4SRam Pai 			mntput(m);
10011da177e4SLinus Torvalds 		}
10021da177e4SLinus Torvalds 		mntput(mnt);
100370fbcdf4SRam Pai 	}
100470fbcdf4SRam Pai }
100570fbcdf4SRam Pai 
1006a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
100770fbcdf4SRam Pai {
100870fbcdf4SRam Pai 	struct vfsmount *p;
100970fbcdf4SRam Pai 
10101bfba4e8SAkinobu Mita 	for (p = mnt; p; p = next_mnt(p, mnt))
10111bfba4e8SAkinobu Mita 		list_move(&p->mnt_hash, kill);
101270fbcdf4SRam Pai 
1013a05964f3SRam Pai 	if (propagate)
1014a05964f3SRam Pai 		propagate_umount(kill);
1015a05964f3SRam Pai 
101670fbcdf4SRam Pai 	list_for_each_entry(p, kill, mnt_hash) {
101770fbcdf4SRam Pai 		list_del_init(&p->mnt_expire);
101870fbcdf4SRam Pai 		list_del_init(&p->mnt_list);
10196b3286edSKirill Korotaev 		__touch_mnt_namespace(p->mnt_ns);
10206b3286edSKirill Korotaev 		p->mnt_ns = NULL;
102170fbcdf4SRam Pai 		list_del_init(&p->mnt_child);
10227c4b93d8SAl Viro 		if (p->mnt_parent != p) {
10237c4b93d8SAl Viro 			p->mnt_parent->mnt_ghosts++;
1024f30ac319SAl Viro 			p->mnt_mountpoint->d_mounted--;
10257c4b93d8SAl Viro 		}
1026a05964f3SRam Pai 		change_mnt_propagation(p, MS_PRIVATE);
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds }
10291da177e4SLinus Torvalds 
1030c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
1031c35038beSAl Viro 
10321da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags)
10331da177e4SLinus Torvalds {
10341da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
10351da177e4SLinus Torvalds 	int retval;
103670fbcdf4SRam Pai 	LIST_HEAD(umount_list);
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	retval = security_sb_umount(mnt, flags);
10391da177e4SLinus Torvalds 	if (retval)
10401da177e4SLinus Torvalds 		return retval;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	/*
10431da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
10441da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
10451da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
10461da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
10471da177e4SLinus Torvalds 	 */
10481da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
10496ac08c39SJan Blunck 		if (mnt == current->fs->root.mnt ||
10501da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
10511da177e4SLinus Torvalds 			return -EINVAL;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 		if (atomic_read(&mnt->mnt_count) != 2)
10541da177e4SLinus Torvalds 			return -EBUSY;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1))
10571da177e4SLinus Torvalds 			return -EAGAIN;
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	/*
10611da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
10621da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
10631da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
10641da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
10651da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
10661da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
10671da177e4SLinus Torvalds 	 * about for the moment.
10681da177e4SLinus Torvalds 	 */
10691da177e4SLinus Torvalds 
107042faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
10711da177e4SLinus Torvalds 		lock_kernel();
107242faad99SAl Viro 		sb->s_op->umount_begin(sb);
10731da177e4SLinus Torvalds 		unlock_kernel();
107442faad99SAl Viro 	}
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	/*
10771da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
10781da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
10791da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
10801da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
10811da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
10821da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
10831da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
10841da177e4SLinus Torvalds 	 */
10856ac08c39SJan Blunck 	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
10861da177e4SLinus Torvalds 		/*
10871da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
10881da177e4SLinus Torvalds 		 * we just try to remount it readonly.
10891da177e4SLinus Torvalds 		 */
10901da177e4SLinus Torvalds 		down_write(&sb->s_umount);
10911da177e4SLinus Torvalds 		if (!(sb->s_flags & MS_RDONLY)) {
10921da177e4SLinus Torvalds 			lock_kernel();
10931da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
10941da177e4SLinus Torvalds 			unlock_kernel();
10951da177e4SLinus Torvalds 		}
10961da177e4SLinus Torvalds 		up_write(&sb->s_umount);
10971da177e4SLinus Torvalds 		return retval;
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds 
1100390c6843SRam Pai 	down_write(&namespace_sem);
11011da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
11025addc5ddSAl Viro 	event++;
11031da177e4SLinus Torvalds 
1104c35038beSAl Viro 	if (!(flags & MNT_DETACH))
1105c35038beSAl Viro 		shrink_submounts(mnt, &umount_list);
1106c35038beSAl Viro 
11071da177e4SLinus Torvalds 	retval = -EBUSY;
1108a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
11091da177e4SLinus Torvalds 		if (!list_empty(&mnt->mnt_list))
1110a05964f3SRam Pai 			umount_tree(mnt, 1, &umount_list);
11111da177e4SLinus Torvalds 		retval = 0;
11121da177e4SLinus Torvalds 	}
11131da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
11141da177e4SLinus Torvalds 	if (retval)
11151da177e4SLinus Torvalds 		security_sb_umount_busy(mnt);
1116390c6843SRam Pai 	up_write(&namespace_sem);
111770fbcdf4SRam Pai 	release_mounts(&umount_list);
11181da177e4SLinus Torvalds 	return retval;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds /*
11221da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
11231da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
11241da177e4SLinus Torvalds  *
11251da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
11261da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
11271da177e4SLinus Torvalds  */
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds asmlinkage long sys_umount(char __user * name, int flags)
11301da177e4SLinus Torvalds {
11311da177e4SLinus Torvalds 	struct nameidata nd;
11321da177e4SLinus Torvalds 	int retval;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
11351da177e4SLinus Torvalds 	if (retval)
11361da177e4SLinus Torvalds 		goto out;
11371da177e4SLinus Torvalds 	retval = -EINVAL;
11384ac91378SJan Blunck 	if (nd.path.dentry != nd.path.mnt->mnt_root)
11391da177e4SLinus Torvalds 		goto dput_and_out;
11404ac91378SJan Blunck 	if (!check_mnt(nd.path.mnt))
11411da177e4SLinus Torvalds 		goto dput_and_out;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	retval = -EPERM;
11441da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
11451da177e4SLinus Torvalds 		goto dput_and_out;
11461da177e4SLinus Torvalds 
11474ac91378SJan Blunck 	retval = do_umount(nd.path.mnt, flags);
11481da177e4SLinus Torvalds dput_and_out:
1149429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
11504ac91378SJan Blunck 	dput(nd.path.dentry);
11514ac91378SJan Blunck 	mntput_no_expire(nd.path.mnt);
11521da177e4SLinus Torvalds out:
11531da177e4SLinus Torvalds 	return retval;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds /*
11591da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
11601da177e4SLinus Torvalds  */
11611da177e4SLinus Torvalds asmlinkage long sys_oldumount(char __user * name)
11621da177e4SLinus Torvalds {
11631da177e4SLinus Torvalds 	return sys_umount(name, 0);
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds #endif
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds static int mount_is_safe(struct nameidata *nd)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	if (capable(CAP_SYS_ADMIN))
11711da177e4SLinus Torvalds 		return 0;
11721da177e4SLinus Torvalds 	return -EPERM;
11731da177e4SLinus Torvalds #ifdef notyet
11744ac91378SJan Blunck 	if (S_ISLNK(nd->path.dentry->d_inode->i_mode))
11751da177e4SLinus Torvalds 		return -EPERM;
11764ac91378SJan Blunck 	if (nd->path.dentry->d_inode->i_mode & S_ISVTX) {
11774ac91378SJan Blunck 		if (current->uid != nd->path.dentry->d_inode->i_uid)
11781da177e4SLinus Torvalds 			return -EPERM;
11791da177e4SLinus Torvalds 	}
1180e4543eddSChristoph Hellwig 	if (vfs_permission(nd, MAY_WRITE))
11811da177e4SLinus Torvalds 		return -EPERM;
11821da177e4SLinus Torvalds 	return 0;
11831da177e4SLinus Torvalds #endif
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
1186b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
118736341f64SRam Pai 					int flag)
11881da177e4SLinus Torvalds {
11891da177e4SLinus Torvalds 	struct vfsmount *res, *p, *q, *r, *s;
11901a390689SAl Viro 	struct path path;
11911da177e4SLinus Torvalds 
11929676f0c6SRam Pai 	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
11939676f0c6SRam Pai 		return NULL;
11949676f0c6SRam Pai 
119536341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
11961da177e4SLinus Torvalds 	if (!q)
11971da177e4SLinus Torvalds 		goto Enomem;
11981da177e4SLinus Torvalds 	q->mnt_mountpoint = mnt->mnt_mountpoint;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	p = mnt;
1201fdadd65fSDomen Puncer 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
12027ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
12031da177e4SLinus Torvalds 			continue;
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 		for (s = r; s; s = next_mnt(s, r)) {
12069676f0c6SRam Pai 			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
12079676f0c6SRam Pai 				s = skip_mnt_tree(s);
12089676f0c6SRam Pai 				continue;
12099676f0c6SRam Pai 			}
12101da177e4SLinus Torvalds 			while (p != s->mnt_parent) {
12111da177e4SLinus Torvalds 				p = p->mnt_parent;
12121da177e4SLinus Torvalds 				q = q->mnt_parent;
12131da177e4SLinus Torvalds 			}
12141da177e4SLinus Torvalds 			p = s;
12151a390689SAl Viro 			path.mnt = q;
12161a390689SAl Viro 			path.dentry = p->mnt_mountpoint;
121736341f64SRam Pai 			q = clone_mnt(p, p->mnt_root, flag);
12181da177e4SLinus Torvalds 			if (!q)
12191da177e4SLinus Torvalds 				goto Enomem;
12201da177e4SLinus Torvalds 			spin_lock(&vfsmount_lock);
12211da177e4SLinus Torvalds 			list_add_tail(&q->mnt_list, &res->mnt_list);
12221a390689SAl Viro 			attach_mnt(q, &path);
12231da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 	return res;
12271da177e4SLinus Torvalds Enomem:
12281da177e4SLinus Torvalds 	if (res) {
122970fbcdf4SRam Pai 		LIST_HEAD(umount_list);
12301da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
1231a05964f3SRam Pai 		umount_tree(res, 0, &umount_list);
12321da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
123370fbcdf4SRam Pai 		release_mounts(&umount_list);
12341da177e4SLinus Torvalds 	}
12351da177e4SLinus Torvalds 	return NULL;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds 
12388aec0809SAl Viro struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry)
12398aec0809SAl Viro {
12408aec0809SAl Viro 	struct vfsmount *tree;
12411a60a280SAl Viro 	down_write(&namespace_sem);
12428aec0809SAl Viro 	tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE);
12431a60a280SAl Viro 	up_write(&namespace_sem);
12448aec0809SAl Viro 	return tree;
12458aec0809SAl Viro }
12468aec0809SAl Viro 
12478aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
12488aec0809SAl Viro {
12498aec0809SAl Viro 	LIST_HEAD(umount_list);
12501a60a280SAl Viro 	down_write(&namespace_sem);
12518aec0809SAl Viro 	spin_lock(&vfsmount_lock);
12528aec0809SAl Viro 	umount_tree(mnt, 0, &umount_list);
12538aec0809SAl Viro 	spin_unlock(&vfsmount_lock);
12541a60a280SAl Viro 	up_write(&namespace_sem);
12558aec0809SAl Viro 	release_mounts(&umount_list);
12568aec0809SAl Viro }
12578aec0809SAl Viro 
1258719f5d7fSMiklos Szeredi static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
1259719f5d7fSMiklos Szeredi {
1260719f5d7fSMiklos Szeredi 	struct vfsmount *p;
1261719f5d7fSMiklos Szeredi 
1262719f5d7fSMiklos Szeredi 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1263719f5d7fSMiklos Szeredi 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
1264719f5d7fSMiklos Szeredi 			mnt_release_group_id(p);
1265719f5d7fSMiklos Szeredi 	}
1266719f5d7fSMiklos Szeredi }
1267719f5d7fSMiklos Szeredi 
1268719f5d7fSMiklos Szeredi static int invent_group_ids(struct vfsmount *mnt, bool recurse)
1269719f5d7fSMiklos Szeredi {
1270719f5d7fSMiklos Szeredi 	struct vfsmount *p;
1271719f5d7fSMiklos Szeredi 
1272719f5d7fSMiklos Szeredi 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1273719f5d7fSMiklos Szeredi 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1274719f5d7fSMiklos Szeredi 			int err = mnt_alloc_group_id(p);
1275719f5d7fSMiklos Szeredi 			if (err) {
1276719f5d7fSMiklos Szeredi 				cleanup_group_ids(mnt, p);
1277719f5d7fSMiklos Szeredi 				return err;
1278719f5d7fSMiklos Szeredi 			}
1279719f5d7fSMiklos Szeredi 		}
1280719f5d7fSMiklos Szeredi 	}
1281719f5d7fSMiklos Szeredi 
1282719f5d7fSMiklos Szeredi 	return 0;
1283719f5d7fSMiklos Szeredi }
1284719f5d7fSMiklos Szeredi 
1285b90fa9aeSRam Pai /*
1286b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1287b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
128821444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
128921444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
129021444403SRam Pai  *  		   (done when source_mnt is moved)
1291b90fa9aeSRam Pai  *
1292b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1293b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
12949676f0c6SRam Pai  * ---------------------------------------------------------------------------
1295b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
12969676f0c6SRam Pai  * |**************************************************************************
12979676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
12989676f0c6SRam Pai  * | dest     |               |                |                |            |
12999676f0c6SRam Pai  * |   |      |               |                |                |            |
13009676f0c6SRam Pai  * |   v      |               |                |                |            |
13019676f0c6SRam Pai  * |**************************************************************************
13029676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
13035afe0022SRam Pai  * |          |               |                |                |            |
13049676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
13059676f0c6SRam Pai  * ***************************************************************************
1306b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1307b90fa9aeSRam Pai  * destination mount.
1308b90fa9aeSRam Pai  *
1309b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1310b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1311b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1312b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1313b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1314b90fa9aeSRam Pai  *       mount.
13155afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
13165afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
13175afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
13185afe0022SRam Pai  *       is marked as 'shared and slave'.
13195afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
13205afe0022SRam Pai  * 	 source mount.
13215afe0022SRam Pai  *
13229676f0c6SRam Pai  * ---------------------------------------------------------------------------
132321444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
13249676f0c6SRam Pai  * |**************************************************************************
13259676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
13269676f0c6SRam Pai  * | dest     |               |                |                |            |
13279676f0c6SRam Pai  * |   |      |               |                |                |            |
13289676f0c6SRam Pai  * |   v      |               |                |                |            |
13299676f0c6SRam Pai  * |**************************************************************************
13309676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
13315afe0022SRam Pai  * |          |               |                |                |            |
13329676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
13339676f0c6SRam Pai  * ***************************************************************************
13345afe0022SRam Pai  *
13355afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
13365afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
133721444403SRam Pai  * (+*)  the mount is moved to the destination.
13385afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
13395afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
13405afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
13415afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1342b90fa9aeSRam Pai  *
1343b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1344b90fa9aeSRam Pai  * applied to each mount in the tree.
1345b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1346b90fa9aeSRam Pai  * in allocations.
1347b90fa9aeSRam Pai  */
1348b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt,
13491a390689SAl Viro 			struct path *path, struct path *parent_path)
1350b90fa9aeSRam Pai {
1351b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
13521a390689SAl Viro 	struct vfsmount *dest_mnt = path->mnt;
13531a390689SAl Viro 	struct dentry *dest_dentry = path->dentry;
1354b90fa9aeSRam Pai 	struct vfsmount *child, *p;
1355719f5d7fSMiklos Szeredi 	int err;
1356b90fa9aeSRam Pai 
1357719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt)) {
1358719f5d7fSMiklos Szeredi 		err = invent_group_ids(source_mnt, true);
1359719f5d7fSMiklos Szeredi 		if (err)
1360719f5d7fSMiklos Szeredi 			goto out;
1361719f5d7fSMiklos Szeredi 	}
1362719f5d7fSMiklos Szeredi 	err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1363719f5d7fSMiklos Szeredi 	if (err)
1364719f5d7fSMiklos Szeredi 		goto out_cleanup_ids;
1365b90fa9aeSRam Pai 
1366b90fa9aeSRam Pai 	if (IS_MNT_SHARED(dest_mnt)) {
1367b90fa9aeSRam Pai 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1368b90fa9aeSRam Pai 			set_mnt_shared(p);
1369b90fa9aeSRam Pai 	}
1370b90fa9aeSRam Pai 
1371b90fa9aeSRam Pai 	spin_lock(&vfsmount_lock);
13721a390689SAl Viro 	if (parent_path) {
13731a390689SAl Viro 		detach_mnt(source_mnt, parent_path);
13741a390689SAl Viro 		attach_mnt(source_mnt, path);
13756b3286edSKirill Korotaev 		touch_mnt_namespace(current->nsproxy->mnt_ns);
137621444403SRam Pai 	} else {
1377b90fa9aeSRam Pai 		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
1378b90fa9aeSRam Pai 		commit_tree(source_mnt);
137921444403SRam Pai 	}
1380b90fa9aeSRam Pai 
1381b90fa9aeSRam Pai 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1382b90fa9aeSRam Pai 		list_del_init(&child->mnt_hash);
1383b90fa9aeSRam Pai 		commit_tree(child);
1384b90fa9aeSRam Pai 	}
1385b90fa9aeSRam Pai 	spin_unlock(&vfsmount_lock);
1386b90fa9aeSRam Pai 	return 0;
1387719f5d7fSMiklos Szeredi 
1388719f5d7fSMiklos Szeredi  out_cleanup_ids:
1389719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt))
1390719f5d7fSMiklos Szeredi 		cleanup_group_ids(source_mnt, NULL);
1391719f5d7fSMiklos Szeredi  out:
1392719f5d7fSMiklos Szeredi 	return err;
1393b90fa9aeSRam Pai }
1394b90fa9aeSRam Pai 
13958c3ee42eSAl Viro static int graft_tree(struct vfsmount *mnt, struct path *path)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds 	int err;
13981da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_flags & MS_NOUSER)
13991da177e4SLinus Torvalds 		return -EINVAL;
14001da177e4SLinus Torvalds 
14018c3ee42eSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
14021da177e4SLinus Torvalds 	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
14031da177e4SLinus Torvalds 		return -ENOTDIR;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	err = -ENOENT;
14068c3ee42eSAl Viro 	mutex_lock(&path->dentry->d_inode->i_mutex);
14078c3ee42eSAl Viro 	if (IS_DEADDIR(path->dentry->d_inode))
14081da177e4SLinus Torvalds 		goto out_unlock;
14091da177e4SLinus Torvalds 
14108c3ee42eSAl Viro 	err = security_sb_check_sb(mnt, path);
14111da177e4SLinus Torvalds 	if (err)
14121da177e4SLinus Torvalds 		goto out_unlock;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	err = -ENOENT;
14158c3ee42eSAl Viro 	if (IS_ROOT(path->dentry) || !d_unhashed(path->dentry))
14168c3ee42eSAl Viro 		err = attach_recursive_mnt(mnt, path, NULL);
14171da177e4SLinus Torvalds out_unlock:
14188c3ee42eSAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
14191da177e4SLinus Torvalds 	if (!err)
14208c3ee42eSAl Viro 		security_sb_post_addmount(mnt, path);
14211da177e4SLinus Torvalds 	return err;
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds /*
142507b20889SRam Pai  * recursively change the type of the mountpoint.
14262dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
142707b20889SRam Pai  */
14282dafe1c4SEric Sandeen static noinline int do_change_type(struct nameidata *nd, int flag)
142907b20889SRam Pai {
14304ac91378SJan Blunck 	struct vfsmount *m, *mnt = nd->path.mnt;
143107b20889SRam Pai 	int recurse = flag & MS_REC;
143207b20889SRam Pai 	int type = flag & ~MS_REC;
1433719f5d7fSMiklos Szeredi 	int err = 0;
143407b20889SRam Pai 
1435ee6f9582SMiklos Szeredi 	if (!capable(CAP_SYS_ADMIN))
1436ee6f9582SMiklos Szeredi 		return -EPERM;
1437ee6f9582SMiklos Szeredi 
14384ac91378SJan Blunck 	if (nd->path.dentry != nd->path.mnt->mnt_root)
143907b20889SRam Pai 		return -EINVAL;
144007b20889SRam Pai 
144107b20889SRam Pai 	down_write(&namespace_sem);
1442719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1443719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1444719f5d7fSMiklos Szeredi 		if (err)
1445719f5d7fSMiklos Szeredi 			goto out_unlock;
1446719f5d7fSMiklos Szeredi 	}
1447719f5d7fSMiklos Szeredi 
144807b20889SRam Pai 	spin_lock(&vfsmount_lock);
144907b20889SRam Pai 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
145007b20889SRam Pai 		change_mnt_propagation(m, type);
145107b20889SRam Pai 	spin_unlock(&vfsmount_lock);
1452719f5d7fSMiklos Szeredi 
1453719f5d7fSMiklos Szeredi  out_unlock:
145407b20889SRam Pai 	up_write(&namespace_sem);
1455719f5d7fSMiklos Szeredi 	return err;
145607b20889SRam Pai }
145707b20889SRam Pai 
145807b20889SRam Pai /*
14591da177e4SLinus Torvalds  * do loopback mount.
14602dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
14611da177e4SLinus Torvalds  */
14622dafe1c4SEric Sandeen static noinline int do_loopback(struct nameidata *nd, char *old_name,
14632dafe1c4SEric Sandeen 				int recurse)
14641da177e4SLinus Torvalds {
14651da177e4SLinus Torvalds 	struct nameidata old_nd;
14661da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
14671da177e4SLinus Torvalds 	int err = mount_is_safe(nd);
14681da177e4SLinus Torvalds 	if (err)
14691da177e4SLinus Torvalds 		return err;
14701da177e4SLinus Torvalds 	if (!old_name || !*old_name)
14711da177e4SLinus Torvalds 		return -EINVAL;
14721da177e4SLinus Torvalds 	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
14731da177e4SLinus Torvalds 	if (err)
14741da177e4SLinus Torvalds 		return err;
14751da177e4SLinus Torvalds 
1476390c6843SRam Pai 	down_write(&namespace_sem);
14771da177e4SLinus Torvalds 	err = -EINVAL;
14784ac91378SJan Blunck 	if (IS_MNT_UNBINDABLE(old_nd.path.mnt))
14799676f0c6SRam Pai 		goto out;
14809676f0c6SRam Pai 
14814ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
1482ccd48bc7SAl Viro 		goto out;
1483ccd48bc7SAl Viro 
14841da177e4SLinus Torvalds 	err = -ENOMEM;
14851da177e4SLinus Torvalds 	if (recurse)
14864ac91378SJan Blunck 		mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0);
14871da177e4SLinus Torvalds 	else
14884ac91378SJan Blunck 		mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0);
14891da177e4SLinus Torvalds 
1490ccd48bc7SAl Viro 	if (!mnt)
1491ccd48bc7SAl Viro 		goto out;
1492ccd48bc7SAl Viro 
14938c3ee42eSAl Viro 	err = graft_tree(mnt, &nd->path);
14941da177e4SLinus Torvalds 	if (err) {
149570fbcdf4SRam Pai 		LIST_HEAD(umount_list);
14961da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
1497a05964f3SRam Pai 		umount_tree(mnt, 0, &umount_list);
14981da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
149970fbcdf4SRam Pai 		release_mounts(&umount_list);
15005b83d2c5SRam Pai 	}
15011da177e4SLinus Torvalds 
1502ccd48bc7SAl Viro out:
1503390c6843SRam Pai 	up_write(&namespace_sem);
15041d957f9bSJan Blunck 	path_put(&old_nd.path);
15051da177e4SLinus Torvalds 	return err;
15061da177e4SLinus Torvalds }
15071da177e4SLinus Torvalds 
15082e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
15092e4b7fcdSDave Hansen {
15102e4b7fcdSDave Hansen 	int error = 0;
15112e4b7fcdSDave Hansen 	int readonly_request = 0;
15122e4b7fcdSDave Hansen 
15132e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
15142e4b7fcdSDave Hansen 		readonly_request = 1;
15152e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
15162e4b7fcdSDave Hansen 		return 0;
15172e4b7fcdSDave Hansen 
15182e4b7fcdSDave Hansen 	if (readonly_request)
15192e4b7fcdSDave Hansen 		error = mnt_make_readonly(mnt);
15202e4b7fcdSDave Hansen 	else
15212e4b7fcdSDave Hansen 		__mnt_unmake_readonly(mnt);
15222e4b7fcdSDave Hansen 	return error;
15232e4b7fcdSDave Hansen }
15242e4b7fcdSDave Hansen 
15251da177e4SLinus Torvalds /*
15261da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
15271da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
15281da177e4SLinus Torvalds  * on it - tough luck.
15292dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
15301da177e4SLinus Torvalds  */
15312dafe1c4SEric Sandeen static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags,
15321da177e4SLinus Torvalds 		      void *data)
15331da177e4SLinus Torvalds {
15341da177e4SLinus Torvalds 	int err;
15354ac91378SJan Blunck 	struct super_block *sb = nd->path.mnt->mnt_sb;
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
15381da177e4SLinus Torvalds 		return -EPERM;
15391da177e4SLinus Torvalds 
15404ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt))
15411da177e4SLinus Torvalds 		return -EINVAL;
15421da177e4SLinus Torvalds 
15434ac91378SJan Blunck 	if (nd->path.dentry != nd->path.mnt->mnt_root)
15441da177e4SLinus Torvalds 		return -EINVAL;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	down_write(&sb->s_umount);
15472e4b7fcdSDave Hansen 	if (flags & MS_BIND)
15482e4b7fcdSDave Hansen 		err = change_mount_flags(nd->path.mnt, flags);
15492e4b7fcdSDave Hansen 	else
15501da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
15511da177e4SLinus Torvalds 	if (!err)
15524ac91378SJan Blunck 		nd->path.mnt->mnt_flags = mnt_flags;
15531da177e4SLinus Torvalds 	up_write(&sb->s_umount);
15541da177e4SLinus Torvalds 	if (!err)
15554ac91378SJan Blunck 		security_sb_post_remount(nd->path.mnt, flags, data);
15561da177e4SLinus Torvalds 	return err;
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds 
15599676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt)
15609676f0c6SRam Pai {
15619676f0c6SRam Pai 	struct vfsmount *p;
15629676f0c6SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
15639676f0c6SRam Pai 		if (IS_MNT_UNBINDABLE(p))
15649676f0c6SRam Pai 			return 1;
15659676f0c6SRam Pai 	}
15669676f0c6SRam Pai 	return 0;
15679676f0c6SRam Pai }
15689676f0c6SRam Pai 
15692dafe1c4SEric Sandeen /*
15702dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
15712dafe1c4SEric Sandeen  */
15722dafe1c4SEric Sandeen static noinline int do_move_mount(struct nameidata *nd, char *old_name)
15731da177e4SLinus Torvalds {
15741a390689SAl Viro 	struct nameidata old_nd;
15751a390689SAl Viro 	struct path parent_path;
15761da177e4SLinus Torvalds 	struct vfsmount *p;
15771da177e4SLinus Torvalds 	int err = 0;
15781da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
15791da177e4SLinus Torvalds 		return -EPERM;
15801da177e4SLinus Torvalds 	if (!old_name || !*old_name)
15811da177e4SLinus Torvalds 		return -EINVAL;
15821da177e4SLinus Torvalds 	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
15831da177e4SLinus Torvalds 	if (err)
15841da177e4SLinus Torvalds 		return err;
15851da177e4SLinus Torvalds 
1586390c6843SRam Pai 	down_write(&namespace_sem);
15874ac91378SJan Blunck 	while (d_mountpoint(nd->path.dentry) &&
15884ac91378SJan Blunck 	       follow_down(&nd->path.mnt, &nd->path.dentry))
15891da177e4SLinus Torvalds 		;
15901da177e4SLinus Torvalds 	err = -EINVAL;
15914ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
15921da177e4SLinus Torvalds 		goto out;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	err = -ENOENT;
15954ac91378SJan Blunck 	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
15964ac91378SJan Blunck 	if (IS_DEADDIR(nd->path.dentry->d_inode))
15971da177e4SLinus Torvalds 		goto out1;
15981da177e4SLinus Torvalds 
15994ac91378SJan Blunck 	if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry))
160021444403SRam Pai 		goto out1;
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	err = -EINVAL;
16034ac91378SJan Blunck 	if (old_nd.path.dentry != old_nd.path.mnt->mnt_root)
160421444403SRam Pai 		goto out1;
16051da177e4SLinus Torvalds 
16064ac91378SJan Blunck 	if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent)
160721444403SRam Pai 		goto out1;
16081da177e4SLinus Torvalds 
16094ac91378SJan Blunck 	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
16104ac91378SJan Blunck 	      S_ISDIR(old_nd.path.dentry->d_inode->i_mode))
161121444403SRam Pai 		goto out1;
161221444403SRam Pai 	/*
161321444403SRam Pai 	 * Don't move a mount residing in a shared parent.
161421444403SRam Pai 	 */
16154ac91378SJan Blunck 	if (old_nd.path.mnt->mnt_parent &&
16164ac91378SJan Blunck 	    IS_MNT_SHARED(old_nd.path.mnt->mnt_parent))
161721444403SRam Pai 		goto out1;
16189676f0c6SRam Pai 	/*
16199676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
16209676f0c6SRam Pai 	 * mount which is shared.
16219676f0c6SRam Pai 	 */
16224ac91378SJan Blunck 	if (IS_MNT_SHARED(nd->path.mnt) &&
16234ac91378SJan Blunck 	    tree_contains_unbindable(old_nd.path.mnt))
16249676f0c6SRam Pai 		goto out1;
16251da177e4SLinus Torvalds 	err = -ELOOP;
16264ac91378SJan Blunck 	for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent)
16274ac91378SJan Blunck 		if (p == old_nd.path.mnt)
162821444403SRam Pai 			goto out1;
16291da177e4SLinus Torvalds 
16301a390689SAl Viro 	err = attach_recursive_mnt(old_nd.path.mnt, &nd->path, &parent_path);
16314ac91378SJan Blunck 	if (err)
163221444403SRam Pai 		goto out1;
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
16351da177e4SLinus Torvalds 	 * automatically */
16364ac91378SJan Blunck 	list_del_init(&old_nd.path.mnt->mnt_expire);
16371da177e4SLinus Torvalds out1:
16384ac91378SJan Blunck 	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
16391da177e4SLinus Torvalds out:
1640390c6843SRam Pai 	up_write(&namespace_sem);
16411da177e4SLinus Torvalds 	if (!err)
16421a390689SAl Viro 		path_put(&parent_path);
16431d957f9bSJan Blunck 	path_put(&old_nd.path);
16441da177e4SLinus Torvalds 	return err;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds /*
16481da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
16491da177e4SLinus Torvalds  * namespace's tree
16502dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
16511da177e4SLinus Torvalds  */
16522dafe1c4SEric Sandeen static noinline int do_new_mount(struct nameidata *nd, char *type, int flags,
16531da177e4SLinus Torvalds 			int mnt_flags, char *name, void *data)
16541da177e4SLinus Torvalds {
16551da177e4SLinus Torvalds 	struct vfsmount *mnt;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	if (!type || !memchr(type, 0, PAGE_SIZE))
16581da177e4SLinus Torvalds 		return -EINVAL;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	/* we need capabilities... */
16611da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
16621da177e4SLinus Torvalds 		return -EPERM;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	mnt = do_kern_mount(type, flags, name, data);
16651da177e4SLinus Torvalds 	if (IS_ERR(mnt))
16661da177e4SLinus Torvalds 		return PTR_ERR(mnt);
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	return do_add_mount(mnt, nd, mnt_flags, NULL);
16691da177e4SLinus Torvalds }
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds /*
16721da177e4SLinus Torvalds  * add a mount into a namespace's mount tree
16731da177e4SLinus Torvalds  * - provide the option of adding the new mount to an expiration list
16741da177e4SLinus Torvalds  */
16751da177e4SLinus Torvalds int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
16761da177e4SLinus Torvalds 		 int mnt_flags, struct list_head *fslist)
16771da177e4SLinus Torvalds {
16781da177e4SLinus Torvalds 	int err;
16791da177e4SLinus Torvalds 
1680390c6843SRam Pai 	down_write(&namespace_sem);
16811da177e4SLinus Torvalds 	/* Something was mounted here while we slept */
16824ac91378SJan Blunck 	while (d_mountpoint(nd->path.dentry) &&
16834ac91378SJan Blunck 	       follow_down(&nd->path.mnt, &nd->path.dentry))
16841da177e4SLinus Torvalds 		;
16851da177e4SLinus Torvalds 	err = -EINVAL;
16864ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt))
16871da177e4SLinus Torvalds 		goto unlock;
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	/* Refuse the same filesystem on the same mount point */
16901da177e4SLinus Torvalds 	err = -EBUSY;
16914ac91378SJan Blunck 	if (nd->path.mnt->mnt_sb == newmnt->mnt_sb &&
16924ac91378SJan Blunck 	    nd->path.mnt->mnt_root == nd->path.dentry)
16931da177e4SLinus Torvalds 		goto unlock;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	err = -EINVAL;
16961da177e4SLinus Torvalds 	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
16971da177e4SLinus Torvalds 		goto unlock;
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	newmnt->mnt_flags = mnt_flags;
17008c3ee42eSAl Viro 	if ((err = graft_tree(newmnt, &nd->path)))
17015b83d2c5SRam Pai 		goto unlock;
17021da177e4SLinus Torvalds 
17036758f953SAl Viro 	if (fslist) /* add to the specified expiration list */
170455e700b9SMiklos Szeredi 		list_add_tail(&newmnt->mnt_expire, fslist);
17056758f953SAl Viro 
1706390c6843SRam Pai 	up_write(&namespace_sem);
17075b83d2c5SRam Pai 	return 0;
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds unlock:
1710390c6843SRam Pai 	up_write(&namespace_sem);
17111da177e4SLinus Torvalds 	mntput(newmnt);
17121da177e4SLinus Torvalds 	return err;
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount);
17161da177e4SLinus Torvalds 
17175528f911STrond Myklebust /*
17181da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
17191da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
17201da177e4SLinus Torvalds  * here
17211da177e4SLinus Torvalds  */
17221da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	struct vfsmount *mnt, *next;
17251da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
1726bcc5c7d2SAl Viro 	LIST_HEAD(umounts);
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 	if (list_empty(mounts))
17291da177e4SLinus Torvalds 		return;
17301da177e4SLinus Torvalds 
1731bcc5c7d2SAl Viro 	down_write(&namespace_sem);
17321da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
17351da177e4SLinus Torvalds 	 * following criteria:
17361da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
17371da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
17381da177e4SLinus Torvalds 	 *   cleared by mntput())
17391da177e4SLinus Torvalds 	 */
174055e700b9SMiklos Szeredi 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
17411da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1742bcc5c7d2SAl Viro 			propagate_mount_busy(mnt, 1))
17431da177e4SLinus Torvalds 			continue;
174455e700b9SMiklos Szeredi 		list_move(&mnt->mnt_expire, &graveyard);
17451da177e4SLinus Torvalds 	}
1746bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
1747bcc5c7d2SAl Viro 		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
1748bcc5c7d2SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1749bcc5c7d2SAl Viro 		umount_tree(mnt, 1, &umounts);
1750bcc5c7d2SAl Viro 	}
17511da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
1752bcc5c7d2SAl Viro 	up_write(&namespace_sem);
1753bcc5c7d2SAl Viro 
1754bcc5c7d2SAl Viro 	release_mounts(&umounts);
17551da177e4SLinus Torvalds }
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds /*
17605528f911STrond Myklebust  * Ripoff of 'select_parent()'
17615528f911STrond Myklebust  *
17625528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
17635528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
17645528f911STrond Myklebust  */
17655528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
17665528f911STrond Myklebust {
17675528f911STrond Myklebust 	struct vfsmount *this_parent = parent;
17685528f911STrond Myklebust 	struct list_head *next;
17695528f911STrond Myklebust 	int found = 0;
17705528f911STrond Myklebust 
17715528f911STrond Myklebust repeat:
17725528f911STrond Myklebust 	next = this_parent->mnt_mounts.next;
17735528f911STrond Myklebust resume:
17745528f911STrond Myklebust 	while (next != &this_parent->mnt_mounts) {
17755528f911STrond Myklebust 		struct list_head *tmp = next;
17765528f911STrond Myklebust 		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
17775528f911STrond Myklebust 
17785528f911STrond Myklebust 		next = tmp->next;
17795528f911STrond Myklebust 		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
17805528f911STrond Myklebust 			continue;
17815528f911STrond Myklebust 		/*
17825528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
17835528f911STrond Myklebust 		 */
17845528f911STrond Myklebust 		if (!list_empty(&mnt->mnt_mounts)) {
17855528f911STrond Myklebust 			this_parent = mnt;
17865528f911STrond Myklebust 			goto repeat;
17875528f911STrond Myklebust 		}
17885528f911STrond Myklebust 
17895528f911STrond Myklebust 		if (!propagate_mount_busy(mnt, 1)) {
17905528f911STrond Myklebust 			list_move_tail(&mnt->mnt_expire, graveyard);
17915528f911STrond Myklebust 			found++;
17925528f911STrond Myklebust 		}
17935528f911STrond Myklebust 	}
17945528f911STrond Myklebust 	/*
17955528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
17965528f911STrond Myklebust 	 */
17975528f911STrond Myklebust 	if (this_parent != parent) {
17985528f911STrond Myklebust 		next = this_parent->mnt_child.next;
17995528f911STrond Myklebust 		this_parent = this_parent->mnt_parent;
18005528f911STrond Myklebust 		goto resume;
18015528f911STrond Myklebust 	}
18025528f911STrond Myklebust 	return found;
18035528f911STrond Myklebust }
18045528f911STrond Myklebust 
18055528f911STrond Myklebust /*
18065528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
18075528f911STrond Myklebust  * submounts of a specific parent mountpoint
18085528f911STrond Myklebust  */
1809c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
18105528f911STrond Myklebust {
18115528f911STrond Myklebust 	LIST_HEAD(graveyard);
1812c35038beSAl Viro 	struct vfsmount *m;
18135528f911STrond Myklebust 
18145528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
1815c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
1816bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
1817c35038beSAl Viro 			m = list_first_entry(&graveyard, struct vfsmount,
1818bcc5c7d2SAl Viro 						mnt_expire);
1819bcc5c7d2SAl Viro 			touch_mnt_namespace(mnt->mnt_ns);
1820c35038beSAl Viro 			umount_tree(mnt, 1, umounts);
1821bcc5c7d2SAl Viro 		}
1822bcc5c7d2SAl Viro 	}
18235528f911STrond Myklebust }
18245528f911STrond Myklebust 
18255528f911STrond Myklebust /*
18261da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
18271da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
18281da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
18291da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
18301da177e4SLinus Torvalds  */
1831b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
1832b58fed8bSRam Pai 				 unsigned long n)
18331da177e4SLinus Torvalds {
18341da177e4SLinus Torvalds 	char *t = to;
18351da177e4SLinus Torvalds 	const char __user *f = from;
18361da177e4SLinus Torvalds 	char c;
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
18391da177e4SLinus Torvalds 		return n;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	while (n) {
18421da177e4SLinus Torvalds 		if (__get_user(c, f)) {
18431da177e4SLinus Torvalds 			memset(t, 0, n);
18441da177e4SLinus Torvalds 			break;
18451da177e4SLinus Torvalds 		}
18461da177e4SLinus Torvalds 		*t++ = c;
18471da177e4SLinus Torvalds 		f++;
18481da177e4SLinus Torvalds 		n--;
18491da177e4SLinus Torvalds 	}
18501da177e4SLinus Torvalds 	return n;
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
18541da177e4SLinus Torvalds {
18551da177e4SLinus Torvalds 	int i;
18561da177e4SLinus Torvalds 	unsigned long page;
18571da177e4SLinus Torvalds 	unsigned long size;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	*where = 0;
18601da177e4SLinus Torvalds 	if (!data)
18611da177e4SLinus Torvalds 		return 0;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
18641da177e4SLinus Torvalds 		return -ENOMEM;
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
18671da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
18681da177e4SLinus Torvalds 	 * the remainder of the page.
18691da177e4SLinus Torvalds 	 */
18701da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
18711da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
18721da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
18731da177e4SLinus Torvalds 		size = PAGE_SIZE;
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
18761da177e4SLinus Torvalds 	if (!i) {
18771da177e4SLinus Torvalds 		free_page(page);
18781da177e4SLinus Torvalds 		return -EFAULT;
18791da177e4SLinus Torvalds 	}
18801da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
18811da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
18821da177e4SLinus Torvalds 	*where = page;
18831da177e4SLinus Torvalds 	return 0;
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds /*
18871da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
18881da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
18891da177e4SLinus Torvalds  *
18901da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
18911da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
18921da177e4SLinus Torvalds  * information (or be NULL).
18931da177e4SLinus Torvalds  *
18941da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
18951da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
18961da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
18971da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
18981da177e4SLinus Torvalds  * and must be discarded.
18991da177e4SLinus Torvalds  */
19001da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page,
19011da177e4SLinus Torvalds 		  unsigned long flags, void *data_page)
19021da177e4SLinus Torvalds {
19031da177e4SLinus Torvalds 	struct nameidata nd;
19041da177e4SLinus Torvalds 	int retval = 0;
19051da177e4SLinus Torvalds 	int mnt_flags = 0;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	/* Discard magic */
19081da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
19091da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	/* Basic sanity checks */
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
19141da177e4SLinus Torvalds 		return -EINVAL;
19151da177e4SLinus Torvalds 	if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
19161da177e4SLinus Torvalds 		return -EINVAL;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	if (data_page)
19191da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
19221da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
19231da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
19241da177e4SLinus Torvalds 	if (flags & MS_NODEV)
19251da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
19261da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
19271da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
1928fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
1929fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
1930fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
1931fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
193247ae32d6SValerie Henson 	if (flags & MS_RELATIME)
193347ae32d6SValerie Henson 		mnt_flags |= MNT_RELATIME;
19342e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
19352e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
1936fc33a7bbSChristoph Hellwig 
1937fc33a7bbSChristoph Hellwig 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
19388bf9725cSPavel Emelyanov 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	/* ... and get the mountpoint */
19411da177e4SLinus Torvalds 	retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
19421da177e4SLinus Torvalds 	if (retval)
19431da177e4SLinus Torvalds 		return retval;
19441da177e4SLinus Torvalds 
1945b5266eb4SAl Viro 	retval = security_sb_mount(dev_name, &nd.path,
1946b5266eb4SAl Viro 				   type_page, flags, data_page);
19471da177e4SLinus Torvalds 	if (retval)
19481da177e4SLinus Torvalds 		goto dput_out;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
19511da177e4SLinus Torvalds 		retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
19521da177e4SLinus Torvalds 				    data_page);
19531da177e4SLinus Torvalds 	else if (flags & MS_BIND)
1954eee391a6SAndrew Morton 		retval = do_loopback(&nd, dev_name, flags & MS_REC);
19559676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
195607b20889SRam Pai 		retval = do_change_type(&nd, flags);
19571da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
19581da177e4SLinus Torvalds 		retval = do_move_mount(&nd, dev_name);
19591da177e4SLinus Torvalds 	else
19601da177e4SLinus Torvalds 		retval = do_new_mount(&nd, type_page, flags, mnt_flags,
19611da177e4SLinus Torvalds 				      dev_name, data_page);
19621da177e4SLinus Torvalds dput_out:
19631d957f9bSJan Blunck 	path_put(&nd.path);
19641da177e4SLinus Torvalds 	return retval;
19651da177e4SLinus Torvalds }
19661da177e4SLinus Torvalds 
1967741a2951SJANAK DESAI /*
1968741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
1969741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
1970741a2951SJANAK DESAI  */
1971e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
19726b3286edSKirill Korotaev 		struct fs_struct *fs)
19731da177e4SLinus Torvalds {
19746b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
19757f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
19761da177e4SLinus Torvalds 	struct vfsmount *p, *q;
19771da177e4SLinus Torvalds 
19786b3286edSKirill Korotaev 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
19791da177e4SLinus Torvalds 	if (!new_ns)
1980467e9f4bSCedric Le Goater 		return ERR_PTR(-ENOMEM);
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	atomic_set(&new_ns->count, 1);
19831da177e4SLinus Torvalds 	INIT_LIST_HEAD(&new_ns->list);
19845addc5ddSAl Viro 	init_waitqueue_head(&new_ns->poll);
19855addc5ddSAl Viro 	new_ns->event = 0;
19861da177e4SLinus Torvalds 
1987390c6843SRam Pai 	down_write(&namespace_sem);
19881da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
19896b3286edSKirill Korotaev 	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
19909676f0c6SRam Pai 					CL_COPY_ALL | CL_EXPIRE);
19911da177e4SLinus Torvalds 	if (!new_ns->root) {
1992390c6843SRam Pai 		up_write(&namespace_sem);
19931da177e4SLinus Torvalds 		kfree(new_ns);
1994467e9f4bSCedric Le Goater 		return ERR_PTR(-ENOMEM);;
19951da177e4SLinus Torvalds 	}
19961da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
19971da177e4SLinus Torvalds 	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
19981da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	/*
20011da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
20021da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
20031da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
20041da177e4SLinus Torvalds 	 */
20056b3286edSKirill Korotaev 	p = mnt_ns->root;
20061da177e4SLinus Torvalds 	q = new_ns->root;
20071da177e4SLinus Torvalds 	while (p) {
20086b3286edSKirill Korotaev 		q->mnt_ns = new_ns;
20091da177e4SLinus Torvalds 		if (fs) {
20106ac08c39SJan Blunck 			if (p == fs->root.mnt) {
20111da177e4SLinus Torvalds 				rootmnt = p;
20126ac08c39SJan Blunck 				fs->root.mnt = mntget(q);
20131da177e4SLinus Torvalds 			}
20146ac08c39SJan Blunck 			if (p == fs->pwd.mnt) {
20151da177e4SLinus Torvalds 				pwdmnt = p;
20166ac08c39SJan Blunck 				fs->pwd.mnt = mntget(q);
20171da177e4SLinus Torvalds 			}
20181da177e4SLinus Torvalds 		}
20196b3286edSKirill Korotaev 		p = next_mnt(p, mnt_ns->root);
20201da177e4SLinus Torvalds 		q = next_mnt(q, new_ns->root);
20211da177e4SLinus Torvalds 	}
2022390c6843SRam Pai 	up_write(&namespace_sem);
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	if (rootmnt)
20251da177e4SLinus Torvalds 		mntput(rootmnt);
20261da177e4SLinus Torvalds 	if (pwdmnt)
20271da177e4SLinus Torvalds 		mntput(pwdmnt);
20281da177e4SLinus Torvalds 
2029741a2951SJANAK DESAI 	return new_ns;
2030741a2951SJANAK DESAI }
2031741a2951SJANAK DESAI 
2032213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2033e3222c4eSBadari Pulavarty 		struct fs_struct *new_fs)
2034741a2951SJANAK DESAI {
20356b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
2036741a2951SJANAK DESAI 
2037e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
20386b3286edSKirill Korotaev 	get_mnt_ns(ns);
2039741a2951SJANAK DESAI 
2040741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
2041e3222c4eSBadari Pulavarty 		return ns;
2042741a2951SJANAK DESAI 
2043e3222c4eSBadari Pulavarty 	new_ns = dup_mnt_ns(ns, new_fs);
2044741a2951SJANAK DESAI 
20456b3286edSKirill Korotaev 	put_mnt_ns(ns);
2046e3222c4eSBadari Pulavarty 	return new_ns;
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
20501da177e4SLinus Torvalds 			  char __user * type, unsigned long flags,
20511da177e4SLinus Torvalds 			  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 	lock_kernel();
20771da177e4SLinus Torvalds 	retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
20781da177e4SLinus Torvalds 			  flags, (void *)data_page);
20791da177e4SLinus Torvalds 	unlock_kernel();
20801da177e4SLinus Torvalds 	free_page(data_page);
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds out3:
20831da177e4SLinus Torvalds 	free_page(dev_page);
20841da177e4SLinus Torvalds out2:
20851da177e4SLinus Torvalds 	putname(dir_page);
20861da177e4SLinus Torvalds out1:
20871da177e4SLinus Torvalds 	free_page(type_page);
20881da177e4SLinus Torvalds 	return retval;
20891da177e4SLinus Torvalds }
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds /*
20921da177e4SLinus Torvalds  * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
20931da177e4SLinus Torvalds  * It can block. Requires the big lock held.
20941da177e4SLinus Torvalds  */
2095ac748a09SJan Blunck void set_fs_root(struct fs_struct *fs, struct path *path)
20961da177e4SLinus Torvalds {
20976ac08c39SJan Blunck 	struct path old_root;
20986ac08c39SJan Blunck 
20991da177e4SLinus Torvalds 	write_lock(&fs->lock);
21001da177e4SLinus Torvalds 	old_root = fs->root;
2101ac748a09SJan Blunck 	fs->root = *path;
2102ac748a09SJan Blunck 	path_get(path);
21031da177e4SLinus Torvalds 	write_unlock(&fs->lock);
21046ac08c39SJan Blunck 	if (old_root.dentry)
21056ac08c39SJan Blunck 		path_put(&old_root);
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds /*
21091da177e4SLinus Torvalds  * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
21101da177e4SLinus Torvalds  * It can block. Requires the big lock held.
21111da177e4SLinus Torvalds  */
2112ac748a09SJan Blunck void set_fs_pwd(struct fs_struct *fs, struct path *path)
21131da177e4SLinus Torvalds {
21146ac08c39SJan Blunck 	struct path old_pwd;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	write_lock(&fs->lock);
21171da177e4SLinus Torvalds 	old_pwd = fs->pwd;
2118ac748a09SJan Blunck 	fs->pwd = *path;
2119ac748a09SJan Blunck 	path_get(path);
21201da177e4SLinus Torvalds 	write_unlock(&fs->lock);
21211da177e4SLinus Torvalds 
21226ac08c39SJan Blunck 	if (old_pwd.dentry)
21236ac08c39SJan Blunck 		path_put(&old_pwd);
21241da177e4SLinus Torvalds }
21251da177e4SLinus Torvalds 
21261a390689SAl Viro static void chroot_fs_refs(struct path *old_root, struct path *new_root)
21271da177e4SLinus Torvalds {
21281da177e4SLinus Torvalds 	struct task_struct *g, *p;
21291da177e4SLinus Torvalds 	struct fs_struct *fs;
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
21321da177e4SLinus Torvalds 	do_each_thread(g, p) {
21331da177e4SLinus Torvalds 		task_lock(p);
21341da177e4SLinus Torvalds 		fs = p->fs;
21351da177e4SLinus Torvalds 		if (fs) {
21361da177e4SLinus Torvalds 			atomic_inc(&fs->count);
21371da177e4SLinus Torvalds 			task_unlock(p);
21381a390689SAl Viro 			if (fs->root.dentry == old_root->dentry
21391a390689SAl Viro 			    && fs->root.mnt == old_root->mnt)
21401a390689SAl Viro 				set_fs_root(fs, new_root);
21411a390689SAl Viro 			if (fs->pwd.dentry == old_root->dentry
21421a390689SAl Viro 			    && fs->pwd.mnt == old_root->mnt)
21431a390689SAl Viro 				set_fs_pwd(fs, new_root);
21441da177e4SLinus Torvalds 			put_fs_struct(fs);
21451da177e4SLinus Torvalds 		} else
21461da177e4SLinus Torvalds 			task_unlock(p);
21471da177e4SLinus Torvalds 	} while_each_thread(g, p);
21481da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
21491da177e4SLinus Torvalds }
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds /*
21521da177e4SLinus Torvalds  * pivot_root Semantics:
21531da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
21541da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
21551da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
21561da177e4SLinus Torvalds  *
21571da177e4SLinus Torvalds  * Restrictions:
21581da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
21591da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
21601da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
21611da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
21621da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
21631da177e4SLinus Torvalds  *
21644a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
21654a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
21664a0d11faSNeil Brown  * in this situation.
21674a0d11faSNeil Brown  *
21681da177e4SLinus Torvalds  * Notes:
21691da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
21701da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
21711da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
21721da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
21731da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
21741da177e4SLinus Torvalds  *    first.
21751da177e4SLinus Torvalds  */
2176b58fed8bSRam Pai asmlinkage long sys_pivot_root(const char __user * new_root,
2177b58fed8bSRam Pai 			       const char __user * put_old)
21781da177e4SLinus Torvalds {
21791da177e4SLinus Torvalds 	struct vfsmount *tmp;
21808c3ee42eSAl Viro 	struct nameidata new_nd, old_nd;
21818c3ee42eSAl Viro 	struct path parent_path, root_parent, root;
21821da177e4SLinus Torvalds 	int error;
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
21851da177e4SLinus Torvalds 		return -EPERM;
21861da177e4SLinus Torvalds 
2187b58fed8bSRam Pai 	error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
2188b58fed8bSRam Pai 			    &new_nd);
21891da177e4SLinus Torvalds 	if (error)
21901da177e4SLinus Torvalds 		goto out0;
21911da177e4SLinus Torvalds 	error = -EINVAL;
21924ac91378SJan Blunck 	if (!check_mnt(new_nd.path.mnt))
21931da177e4SLinus Torvalds 		goto out1;
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 	error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
21961da177e4SLinus Torvalds 	if (error)
21971da177e4SLinus Torvalds 		goto out1;
21981da177e4SLinus Torvalds 
2199b5266eb4SAl Viro 	error = security_sb_pivotroot(&old_nd.path, &new_nd.path);
22001da177e4SLinus Torvalds 	if (error) {
22011d957f9bSJan Blunck 		path_put(&old_nd.path);
22021da177e4SLinus Torvalds 		goto out1;
22031da177e4SLinus Torvalds 	}
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds 	read_lock(&current->fs->lock);
22068c3ee42eSAl Viro 	root = current->fs->root;
22076ac08c39SJan Blunck 	path_get(&current->fs->root);
22081da177e4SLinus Torvalds 	read_unlock(&current->fs->lock);
2209390c6843SRam Pai 	down_write(&namespace_sem);
22104ac91378SJan Blunck 	mutex_lock(&old_nd.path.dentry->d_inode->i_mutex);
22111da177e4SLinus Torvalds 	error = -EINVAL;
22124ac91378SJan Blunck 	if (IS_MNT_SHARED(old_nd.path.mnt) ||
22134ac91378SJan Blunck 		IS_MNT_SHARED(new_nd.path.mnt->mnt_parent) ||
22148c3ee42eSAl Viro 		IS_MNT_SHARED(root.mnt->mnt_parent))
221521444403SRam Pai 		goto out2;
22168c3ee42eSAl Viro 	if (!check_mnt(root.mnt))
22171da177e4SLinus Torvalds 		goto out2;
22181da177e4SLinus Torvalds 	error = -ENOENT;
22194ac91378SJan Blunck 	if (IS_DEADDIR(new_nd.path.dentry->d_inode))
22201da177e4SLinus Torvalds 		goto out2;
22214ac91378SJan Blunck 	if (d_unhashed(new_nd.path.dentry) && !IS_ROOT(new_nd.path.dentry))
22221da177e4SLinus Torvalds 		goto out2;
22234ac91378SJan Blunck 	if (d_unhashed(old_nd.path.dentry) && !IS_ROOT(old_nd.path.dentry))
22241da177e4SLinus Torvalds 		goto out2;
22251da177e4SLinus Torvalds 	error = -EBUSY;
22268c3ee42eSAl Viro 	if (new_nd.path.mnt == root.mnt ||
22278c3ee42eSAl Viro 	    old_nd.path.mnt == root.mnt)
22281da177e4SLinus Torvalds 		goto out2; /* loop, on the same file system  */
22291da177e4SLinus Torvalds 	error = -EINVAL;
22308c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
22311da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
22328c3ee42eSAl Viro 	if (root.mnt->mnt_parent == root.mnt)
22330bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
22344ac91378SJan Blunck 	if (new_nd.path.mnt->mnt_root != new_nd.path.dentry)
22351da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
22364ac91378SJan Blunck 	if (new_nd.path.mnt->mnt_parent == new_nd.path.mnt)
22370bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
22384ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
22394ac91378SJan Blunck 	tmp = old_nd.path.mnt;
22401da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
22414ac91378SJan Blunck 	if (tmp != new_nd.path.mnt) {
22421da177e4SLinus Torvalds 		for (;;) {
22431da177e4SLinus Torvalds 			if (tmp->mnt_parent == tmp)
22441da177e4SLinus Torvalds 				goto out3; /* already mounted on put_old */
22454ac91378SJan Blunck 			if (tmp->mnt_parent == new_nd.path.mnt)
22461da177e4SLinus Torvalds 				break;
22471da177e4SLinus Torvalds 			tmp = tmp->mnt_parent;
22481da177e4SLinus Torvalds 		}
22494ac91378SJan Blunck 		if (!is_subdir(tmp->mnt_mountpoint, new_nd.path.dentry))
22501da177e4SLinus Torvalds 			goto out3;
22514ac91378SJan Blunck 	} else if (!is_subdir(old_nd.path.dentry, new_nd.path.dentry))
22521da177e4SLinus Torvalds 		goto out3;
22531a390689SAl Viro 	detach_mnt(new_nd.path.mnt, &parent_path);
22548c3ee42eSAl Viro 	detach_mnt(root.mnt, &root_parent);
22554ac91378SJan Blunck 	/* mount old root on put_old */
22568c3ee42eSAl Viro 	attach_mnt(root.mnt, &old_nd.path);
22574ac91378SJan Blunck 	/* mount new_root on / */
22584ac91378SJan Blunck 	attach_mnt(new_nd.path.mnt, &root_parent);
22596b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
22601da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
22618c3ee42eSAl Viro 	chroot_fs_refs(&root, &new_nd.path);
22628c3ee42eSAl Viro 	security_sb_post_pivotroot(&root, &new_nd.path);
22631da177e4SLinus Torvalds 	error = 0;
22641a390689SAl Viro 	path_put(&root_parent);
22651a390689SAl Viro 	path_put(&parent_path);
22661da177e4SLinus Torvalds out2:
22674ac91378SJan Blunck 	mutex_unlock(&old_nd.path.dentry->d_inode->i_mutex);
2268390c6843SRam Pai 	up_write(&namespace_sem);
22698c3ee42eSAl Viro 	path_put(&root);
22701d957f9bSJan Blunck 	path_put(&old_nd.path);
22711da177e4SLinus Torvalds out1:
22721d957f9bSJan Blunck 	path_put(&new_nd.path);
22731da177e4SLinus Torvalds out0:
22741da177e4SLinus Torvalds 	return error;
22751da177e4SLinus Torvalds out3:
22761da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
22771da177e4SLinus Torvalds 	goto out2;
22781da177e4SLinus Torvalds }
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds static void __init init_mount_tree(void)
22811da177e4SLinus Torvalds {
22821da177e4SLinus Torvalds 	struct vfsmount *mnt;
22836b3286edSKirill Korotaev 	struct mnt_namespace *ns;
2284ac748a09SJan Blunck 	struct path root;
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds 	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
22871da177e4SLinus Torvalds 	if (IS_ERR(mnt))
22881da177e4SLinus Torvalds 		panic("Can't create rootfs");
22896b3286edSKirill Korotaev 	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
22906b3286edSKirill Korotaev 	if (!ns)
22911da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
22926b3286edSKirill Korotaev 	atomic_set(&ns->count, 1);
22936b3286edSKirill Korotaev 	INIT_LIST_HEAD(&ns->list);
22946b3286edSKirill Korotaev 	init_waitqueue_head(&ns->poll);
22956b3286edSKirill Korotaev 	ns->event = 0;
22966b3286edSKirill Korotaev 	list_add(&mnt->mnt_list, &ns->list);
22976b3286edSKirill Korotaev 	ns->root = mnt;
22986b3286edSKirill Korotaev 	mnt->mnt_ns = ns;
22991da177e4SLinus Torvalds 
23006b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
23016b3286edSKirill Korotaev 	get_mnt_ns(ns);
23021da177e4SLinus Torvalds 
2303ac748a09SJan Blunck 	root.mnt = ns->root;
2304ac748a09SJan Blunck 	root.dentry = ns->root->mnt_root;
2305ac748a09SJan Blunck 
2306ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
2307ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
23081da177e4SLinus Torvalds }
23091da177e4SLinus Torvalds 
231074bf17cfSDenis Cheng void __init mnt_init(void)
23111da177e4SLinus Torvalds {
231213f14b4dSEric Dumazet 	unsigned u;
231315a67dd8SRandy Dunlap 	int err;
23141da177e4SLinus Torvalds 
2315390c6843SRam Pai 	init_rwsem(&namespace_sem);
2316390c6843SRam Pai 
23171da177e4SLinus Torvalds 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
231820c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
23191da177e4SLinus Torvalds 
2320b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	if (!mount_hashtable)
23231da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
23241da177e4SLinus Torvalds 
232513f14b4dSEric Dumazet 	printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
23261da177e4SLinus Torvalds 
232713f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
232813f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
23291da177e4SLinus Torvalds 
233015a67dd8SRandy Dunlap 	err = sysfs_init();
233115a67dd8SRandy Dunlap 	if (err)
233215a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
23338e24eea7SHarvey Harrison 			__func__, err);
233400d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
233500d26666SGreg Kroah-Hartman 	if (!fs_kobj)
23368e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
23371da177e4SLinus Torvalds 	init_rootfs();
23381da177e4SLinus Torvalds 	init_mount_tree();
23391da177e4SLinus Torvalds }
23401da177e4SLinus Torvalds 
23416b3286edSKirill Korotaev void __put_mnt_ns(struct mnt_namespace *ns)
23421da177e4SLinus Torvalds {
23436b3286edSKirill Korotaev 	struct vfsmount *root = ns->root;
234470fbcdf4SRam Pai 	LIST_HEAD(umount_list);
23456b3286edSKirill Korotaev 	ns->root = NULL;
23461ce88cf4SMiklos Szeredi 	spin_unlock(&vfsmount_lock);
2347390c6843SRam Pai 	down_write(&namespace_sem);
23481da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
2349a05964f3SRam Pai 	umount_tree(root, 0, &umount_list);
23501da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
2351390c6843SRam Pai 	up_write(&namespace_sem);
235270fbcdf4SRam Pai 	release_mounts(&umount_list);
23536b3286edSKirill Korotaev 	kfree(ns);
23541da177e4SLinus Torvalds }
2355