xref: /openbmc/linux/fs/namespace.c (revision 99b7db7b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namespace.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright Al Viro 2000, 2001
51da177e4SLinus Torvalds  *	Released under GPL v2.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Based on code from fs/super.c, copyright Linus Torvalds and others.
81da177e4SLinus Torvalds  * Heavily rewritten.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/syscalls.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/sched.h>
1499b7db7bSNick Piggin #include <linux/spinlock.h>
1599b7db7bSNick Piggin #include <linux/percpu.h>
161da177e4SLinus Torvalds #include <linux/smp_lock.h>
171da177e4SLinus Torvalds #include <linux/init.h>
1815a67dd8SRandy Dunlap #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/acct.h>
2016f7e0feSRandy Dunlap #include <linux/capability.h>
213d733633SDave Hansen #include <linux/cpumask.h>
221da177e4SLinus Torvalds #include <linux/module.h>
23f20a9eadSAndrew Morton #include <linux/sysfs.h>
241da177e4SLinus Torvalds #include <linux/seq_file.h>
256b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
261da177e4SLinus Torvalds #include <linux/namei.h>
27b43f3cbdSAlexey Dobriyan #include <linux/nsproxy.h>
281da177e4SLinus Torvalds #include <linux/security.h>
291da177e4SLinus Torvalds #include <linux/mount.h>
3007f3f05cSDavid Howells #include <linux/ramfs.h>
3113f14b4dSEric Dumazet #include <linux/log2.h>
3273cd49ecSMiklos Szeredi #include <linux/idr.h>
335ad4e53bSAl Viro #include <linux/fs_struct.h>
342504c5d6SAndreas Gruenbacher #include <linux/fsnotify.h>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds #include <asm/unistd.h>
3707b20889SRam Pai #include "pnode.h"
38948730b0SAdrian Bunk #include "internal.h"
391da177e4SLinus Torvalds 
4013f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
4113f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT)
4213f14b4dSEric Dumazet 
435addc5ddSAl Viro static int event;
4473cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida);
45719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida);
4699b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock);
47f21f6220SAl Viro static int mnt_id_start = 0;
48f21f6220SAl Viro static int mnt_group_start = 1;
495addc5ddSAl Viro 
50fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly;
51e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
52390c6843SRam Pai static struct rw_semaphore namespace_sem;
531da177e4SLinus Torvalds 
54f87fd4c2SMiklos Szeredi /* /sys/fs */
5500d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
5600d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
57f87fd4c2SMiklos Szeredi 
5899b7db7bSNick Piggin /*
5999b7db7bSNick Piggin  * vfsmount lock may be taken for read to prevent changes to the
6099b7db7bSNick Piggin  * vfsmount hash, ie. during mountpoint lookups or walking back
6199b7db7bSNick Piggin  * up the tree.
6299b7db7bSNick Piggin  *
6399b7db7bSNick Piggin  * It should be taken for write in all cases where the vfsmount
6499b7db7bSNick Piggin  * tree or hash is modified or when a vfsmount structure is modified.
6599b7db7bSNick Piggin  */
6699b7db7bSNick Piggin DEFINE_BRLOCK(vfsmount_lock);
6799b7db7bSNick Piggin 
681da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
711da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
7213f14b4dSEric Dumazet 	tmp = tmp + (tmp >> HASH_SHIFT);
7313f14b4dSEric Dumazet 	return tmp & (HASH_SIZE - 1);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
763d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
773d733633SDave Hansen 
7899b7db7bSNick Piggin /*
7999b7db7bSNick Piggin  * allocation is serialized by namespace_sem, but we need the spinlock to
8099b7db7bSNick Piggin  * serialize with freeing.
8199b7db7bSNick Piggin  */
8273cd49ecSMiklos Szeredi static int mnt_alloc_id(struct vfsmount *mnt)
8373cd49ecSMiklos Szeredi {
8473cd49ecSMiklos Szeredi 	int res;
8573cd49ecSMiklos Szeredi 
8673cd49ecSMiklos Szeredi retry:
8773cd49ecSMiklos Szeredi 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
8899b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
89f21f6220SAl Viro 	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
90f21f6220SAl Viro 	if (!res)
91f21f6220SAl Viro 		mnt_id_start = mnt->mnt_id + 1;
9299b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
9373cd49ecSMiklos Szeredi 	if (res == -EAGAIN)
9473cd49ecSMiklos Szeredi 		goto retry;
9573cd49ecSMiklos Szeredi 
9673cd49ecSMiklos Szeredi 	return res;
9773cd49ecSMiklos Szeredi }
9873cd49ecSMiklos Szeredi 
9973cd49ecSMiklos Szeredi static void mnt_free_id(struct vfsmount *mnt)
10073cd49ecSMiklos Szeredi {
101f21f6220SAl Viro 	int id = mnt->mnt_id;
10299b7db7bSNick Piggin 	spin_lock(&mnt_id_lock);
103f21f6220SAl Viro 	ida_remove(&mnt_id_ida, id);
104f21f6220SAl Viro 	if (mnt_id_start > id)
105f21f6220SAl Viro 		mnt_id_start = id;
10699b7db7bSNick Piggin 	spin_unlock(&mnt_id_lock);
10773cd49ecSMiklos Szeredi }
10873cd49ecSMiklos Szeredi 
109719f5d7fSMiklos Szeredi /*
110719f5d7fSMiklos Szeredi  * Allocate a new peer group ID
111719f5d7fSMiklos Szeredi  *
112719f5d7fSMiklos Szeredi  * mnt_group_ida is protected by namespace_sem
113719f5d7fSMiklos Szeredi  */
114719f5d7fSMiklos Szeredi static int mnt_alloc_group_id(struct vfsmount *mnt)
115719f5d7fSMiklos Szeredi {
116f21f6220SAl Viro 	int res;
117f21f6220SAl Viro 
118719f5d7fSMiklos Szeredi 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
119719f5d7fSMiklos Szeredi 		return -ENOMEM;
120719f5d7fSMiklos Szeredi 
121f21f6220SAl Viro 	res = ida_get_new_above(&mnt_group_ida,
122f21f6220SAl Viro 				mnt_group_start,
123f21f6220SAl Viro 				&mnt->mnt_group_id);
124f21f6220SAl Viro 	if (!res)
125f21f6220SAl Viro 		mnt_group_start = mnt->mnt_group_id + 1;
126f21f6220SAl Viro 
127f21f6220SAl Viro 	return res;
128719f5d7fSMiklos Szeredi }
129719f5d7fSMiklos Szeredi 
130719f5d7fSMiklos Szeredi /*
131719f5d7fSMiklos Szeredi  * Release a peer group ID
132719f5d7fSMiklos Szeredi  */
133719f5d7fSMiklos Szeredi void mnt_release_group_id(struct vfsmount *mnt)
134719f5d7fSMiklos Szeredi {
135f21f6220SAl Viro 	int id = mnt->mnt_group_id;
136f21f6220SAl Viro 	ida_remove(&mnt_group_ida, id);
137f21f6220SAl Viro 	if (mnt_group_start > id)
138f21f6220SAl Viro 		mnt_group_start = id;
139719f5d7fSMiklos Szeredi 	mnt->mnt_group_id = 0;
140719f5d7fSMiklos Szeredi }
141719f5d7fSMiklos Szeredi 
1421da177e4SLinus Torvalds struct vfsmount *alloc_vfsmnt(const char *name)
1431da177e4SLinus Torvalds {
144c3762229SRobert P. J. Day 	struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
1451da177e4SLinus Torvalds 	if (mnt) {
14673cd49ecSMiklos Szeredi 		int err;
14773cd49ecSMiklos Szeredi 
14873cd49ecSMiklos Szeredi 		err = mnt_alloc_id(mnt);
14988b38782SLi Zefan 		if (err)
15088b38782SLi Zefan 			goto out_free_cache;
15188b38782SLi Zefan 
15288b38782SLi Zefan 		if (name) {
15388b38782SLi Zefan 			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
15488b38782SLi Zefan 			if (!mnt->mnt_devname)
15588b38782SLi Zefan 				goto out_free_id;
15673cd49ecSMiklos Szeredi 		}
15773cd49ecSMiklos Szeredi 
1581da177e4SLinus Torvalds 		atomic_set(&mnt->mnt_count, 1);
1591da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_hash);
1601da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_child);
1611da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_mounts);
1621da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_list);
16355e700b9SMiklos Szeredi 		INIT_LIST_HEAD(&mnt->mnt_expire);
16403e06e68SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_share);
165a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
166a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave);
1672504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY
1682504c5d6SAndreas Gruenbacher 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
1692504c5d6SAndreas Gruenbacher #endif
170d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
171d3ef3d73Snpiggin@suse.de 		mnt->mnt_writers = alloc_percpu(int);
172d3ef3d73Snpiggin@suse.de 		if (!mnt->mnt_writers)
173d3ef3d73Snpiggin@suse.de 			goto out_free_devname;
174d3ef3d73Snpiggin@suse.de #else
175d3ef3d73Snpiggin@suse.de 		mnt->mnt_writers = 0;
176d3ef3d73Snpiggin@suse.de #endif
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 	return mnt;
17988b38782SLi Zefan 
180d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
181d3ef3d73Snpiggin@suse.de out_free_devname:
182d3ef3d73Snpiggin@suse.de 	kfree(mnt->mnt_devname);
183d3ef3d73Snpiggin@suse.de #endif
18488b38782SLi Zefan out_free_id:
18588b38782SLi Zefan 	mnt_free_id(mnt);
18688b38782SLi Zefan out_free_cache:
18788b38782SLi Zefan 	kmem_cache_free(mnt_cache, mnt);
18888b38782SLi Zefan 	return NULL;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1918366025eSDave Hansen /*
1928366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
1938366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
1948366025eSDave Hansen  * We must keep track of when those operations start
1958366025eSDave Hansen  * (for permission checks) and when they end, so that
1968366025eSDave Hansen  * we can determine when writes are able to occur to
1978366025eSDave Hansen  * a filesystem.
1988366025eSDave Hansen  */
1993d733633SDave Hansen /*
2003d733633SDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
2013d733633SDave Hansen  * @mnt: the mount to check for its write status
2023d733633SDave Hansen  *
2033d733633SDave Hansen  * This shouldn't be used directly ouside of the VFS.
2043d733633SDave Hansen  * It does not guarantee that the filesystem will stay
2053d733633SDave Hansen  * r/w, just that it is right *now*.  This can not and
2063d733633SDave Hansen  * should not be used in place of IS_RDONLY(inode).
2073d733633SDave Hansen  * mnt_want/drop_write() will _keep_ the filesystem
2083d733633SDave Hansen  * r/w.
2093d733633SDave Hansen  */
2103d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
2113d733633SDave Hansen {
2122e4b7fcdSDave Hansen 	if (mnt->mnt_flags & MNT_READONLY)
2132e4b7fcdSDave Hansen 		return 1;
2142e4b7fcdSDave Hansen 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
2152e4b7fcdSDave Hansen 		return 1;
2162e4b7fcdSDave Hansen 	return 0;
2173d733633SDave Hansen }
2183d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
2193d733633SDave Hansen 
220d3ef3d73Snpiggin@suse.de static inline void inc_mnt_writers(struct vfsmount *mnt)
2213d733633SDave Hansen {
222d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
223d3ef3d73Snpiggin@suse.de 	(*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))++;
224d3ef3d73Snpiggin@suse.de #else
225d3ef3d73Snpiggin@suse.de 	mnt->mnt_writers++;
226d3ef3d73Snpiggin@suse.de #endif
2273d733633SDave Hansen }
2283d733633SDave Hansen 
229d3ef3d73Snpiggin@suse.de static inline void dec_mnt_writers(struct vfsmount *mnt)
2303d733633SDave Hansen {
231d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
232d3ef3d73Snpiggin@suse.de 	(*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))--;
233d3ef3d73Snpiggin@suse.de #else
234d3ef3d73Snpiggin@suse.de 	mnt->mnt_writers--;
235d3ef3d73Snpiggin@suse.de #endif
236d3ef3d73Snpiggin@suse.de }
237d3ef3d73Snpiggin@suse.de 
238d3ef3d73Snpiggin@suse.de static unsigned int count_mnt_writers(struct vfsmount *mnt)
239d3ef3d73Snpiggin@suse.de {
240d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
241d3ef3d73Snpiggin@suse.de 	unsigned int count = 0;
2423d733633SDave Hansen 	int cpu;
2433d733633SDave Hansen 
2443d733633SDave Hansen 	for_each_possible_cpu(cpu) {
245d3ef3d73Snpiggin@suse.de 		count += *per_cpu_ptr(mnt->mnt_writers, cpu);
2463d733633SDave Hansen 	}
2473d733633SDave Hansen 
248d3ef3d73Snpiggin@suse.de 	return count;
249d3ef3d73Snpiggin@suse.de #else
250d3ef3d73Snpiggin@suse.de 	return mnt->mnt_writers;
251d3ef3d73Snpiggin@suse.de #endif
2523d733633SDave Hansen }
2533d733633SDave Hansen 
2543d733633SDave Hansen /*
2553d733633SDave Hansen  * Most r/o checks on a fs are for operations that take
2563d733633SDave Hansen  * discrete amounts of time, like a write() or unlink().
2573d733633SDave Hansen  * We must keep track of when those operations start
2583d733633SDave Hansen  * (for permission checks) and when they end, so that
2593d733633SDave Hansen  * we can determine when writes are able to occur to
2603d733633SDave Hansen  * a filesystem.
2613d733633SDave Hansen  */
2628366025eSDave Hansen /**
2638366025eSDave Hansen  * mnt_want_write - get write access to a mount
2648366025eSDave Hansen  * @mnt: the mount on which to take a write
2658366025eSDave Hansen  *
2668366025eSDave Hansen  * This tells the low-level filesystem that a write is
2678366025eSDave Hansen  * about to be performed to it, and makes sure that
2688366025eSDave Hansen  * writes are allowed before returning success.  When
2698366025eSDave Hansen  * the write operation is finished, mnt_drop_write()
2708366025eSDave Hansen  * must be called.  This is effectively a refcount.
2718366025eSDave Hansen  */
2728366025eSDave Hansen int mnt_want_write(struct vfsmount *mnt)
2738366025eSDave Hansen {
2743d733633SDave Hansen 	int ret = 0;
2753d733633SDave Hansen 
276d3ef3d73Snpiggin@suse.de 	preempt_disable();
277d3ef3d73Snpiggin@suse.de 	inc_mnt_writers(mnt);
278d3ef3d73Snpiggin@suse.de 	/*
279d3ef3d73Snpiggin@suse.de 	 * The store to inc_mnt_writers must be visible before we pass
280d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
281d3ef3d73Snpiggin@suse.de 	 * incremented count after it has set MNT_WRITE_HOLD.
282d3ef3d73Snpiggin@suse.de 	 */
283d3ef3d73Snpiggin@suse.de 	smp_mb();
284d3ef3d73Snpiggin@suse.de 	while (mnt->mnt_flags & MNT_WRITE_HOLD)
285d3ef3d73Snpiggin@suse.de 		cpu_relax();
286d3ef3d73Snpiggin@suse.de 	/*
287d3ef3d73Snpiggin@suse.de 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
288d3ef3d73Snpiggin@suse.de 	 * be set to match its requirements. So we must not load that until
289d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD is cleared.
290d3ef3d73Snpiggin@suse.de 	 */
291d3ef3d73Snpiggin@suse.de 	smp_rmb();
2923d733633SDave Hansen 	if (__mnt_is_readonly(mnt)) {
293d3ef3d73Snpiggin@suse.de 		dec_mnt_writers(mnt);
2943d733633SDave Hansen 		ret = -EROFS;
2953d733633SDave Hansen 		goto out;
2963d733633SDave Hansen 	}
2973d733633SDave Hansen out:
298d3ef3d73Snpiggin@suse.de 	preempt_enable();
2993d733633SDave Hansen 	return ret;
3008366025eSDave Hansen }
3018366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
3028366025eSDave Hansen 
3038366025eSDave Hansen /**
30496029c4eSnpiggin@suse.de  * mnt_clone_write - get write access to a mount
30596029c4eSnpiggin@suse.de  * @mnt: the mount on which to take a write
30696029c4eSnpiggin@suse.de  *
30796029c4eSnpiggin@suse.de  * This is effectively like mnt_want_write, except
30896029c4eSnpiggin@suse.de  * it must only be used to take an extra write reference
30996029c4eSnpiggin@suse.de  * on a mountpoint that we already know has a write reference
31096029c4eSnpiggin@suse.de  * on it. This allows some optimisation.
31196029c4eSnpiggin@suse.de  *
31296029c4eSnpiggin@suse.de  * After finished, mnt_drop_write must be called as usual to
31396029c4eSnpiggin@suse.de  * drop the reference.
31496029c4eSnpiggin@suse.de  */
31596029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt)
31696029c4eSnpiggin@suse.de {
31796029c4eSnpiggin@suse.de 	/* superblock may be r/o */
31896029c4eSnpiggin@suse.de 	if (__mnt_is_readonly(mnt))
31996029c4eSnpiggin@suse.de 		return -EROFS;
32096029c4eSnpiggin@suse.de 	preempt_disable();
32196029c4eSnpiggin@suse.de 	inc_mnt_writers(mnt);
32296029c4eSnpiggin@suse.de 	preempt_enable();
32396029c4eSnpiggin@suse.de 	return 0;
32496029c4eSnpiggin@suse.de }
32596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write);
32696029c4eSnpiggin@suse.de 
32796029c4eSnpiggin@suse.de /**
32896029c4eSnpiggin@suse.de  * mnt_want_write_file - get write access to a file's mount
32996029c4eSnpiggin@suse.de  * @file: the file who's mount on which to take a write
33096029c4eSnpiggin@suse.de  *
33196029c4eSnpiggin@suse.de  * This is like mnt_want_write, but it takes a file and can
33296029c4eSnpiggin@suse.de  * do some optimisations if the file is open for write already
33396029c4eSnpiggin@suse.de  */
33496029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file)
33596029c4eSnpiggin@suse.de {
3362d8dd38aSOGAWA Hirofumi 	struct inode *inode = file->f_dentry->d_inode;
3372d8dd38aSOGAWA Hirofumi 	if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
33896029c4eSnpiggin@suse.de 		return mnt_want_write(file->f_path.mnt);
33996029c4eSnpiggin@suse.de 	else
34096029c4eSnpiggin@suse.de 		return mnt_clone_write(file->f_path.mnt);
34196029c4eSnpiggin@suse.de }
34296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file);
34396029c4eSnpiggin@suse.de 
34496029c4eSnpiggin@suse.de /**
3458366025eSDave Hansen  * mnt_drop_write - give up write access to a mount
3468366025eSDave Hansen  * @mnt: the mount on which to give up write access
3478366025eSDave Hansen  *
3488366025eSDave Hansen  * Tells the low-level filesystem that we are done
3498366025eSDave Hansen  * performing writes to it.  Must be matched with
3508366025eSDave Hansen  * mnt_want_write() call above.
3518366025eSDave Hansen  */
3528366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt)
3538366025eSDave Hansen {
354d3ef3d73Snpiggin@suse.de 	preempt_disable();
355d3ef3d73Snpiggin@suse.de 	dec_mnt_writers(mnt);
356d3ef3d73Snpiggin@suse.de 	preempt_enable();
3578366025eSDave Hansen }
3588366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
3598366025eSDave Hansen 
3602e4b7fcdSDave Hansen static int mnt_make_readonly(struct vfsmount *mnt)
3618366025eSDave Hansen {
3623d733633SDave Hansen 	int ret = 0;
3633d733633SDave Hansen 
36499b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
365d3ef3d73Snpiggin@suse.de 	mnt->mnt_flags |= MNT_WRITE_HOLD;
366d3ef3d73Snpiggin@suse.de 	/*
367d3ef3d73Snpiggin@suse.de 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
368d3ef3d73Snpiggin@suse.de 	 * should be visible before we do.
369d3ef3d73Snpiggin@suse.de 	 */
370d3ef3d73Snpiggin@suse.de 	smp_mb();
371d3ef3d73Snpiggin@suse.de 
372d3ef3d73Snpiggin@suse.de 	/*
373d3ef3d73Snpiggin@suse.de 	 * With writers on hold, if this value is zero, then there are
374d3ef3d73Snpiggin@suse.de 	 * definitely no active writers (although held writers may subsequently
375d3ef3d73Snpiggin@suse.de 	 * increment the count, they'll have to wait, and decrement it after
376d3ef3d73Snpiggin@suse.de 	 * seeing MNT_READONLY).
377d3ef3d73Snpiggin@suse.de 	 *
378d3ef3d73Snpiggin@suse.de 	 * It is OK to have counter incremented on one CPU and decremented on
379d3ef3d73Snpiggin@suse.de 	 * another: the sum will add up correctly. The danger would be when we
380d3ef3d73Snpiggin@suse.de 	 * sum up each counter, if we read a counter before it is incremented,
381d3ef3d73Snpiggin@suse.de 	 * but then read another CPU's count which it has been subsequently
382d3ef3d73Snpiggin@suse.de 	 * decremented from -- we would see more decrements than we should.
383d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD protects against this scenario, because
384d3ef3d73Snpiggin@suse.de 	 * mnt_want_write first increments count, then smp_mb, then spins on
385d3ef3d73Snpiggin@suse.de 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
386d3ef3d73Snpiggin@suse.de 	 * we're counting up here.
387d3ef3d73Snpiggin@suse.de 	 */
388d3ef3d73Snpiggin@suse.de 	if (count_mnt_writers(mnt) > 0)
389d3ef3d73Snpiggin@suse.de 		ret = -EBUSY;
390d3ef3d73Snpiggin@suse.de 	else
3912e4b7fcdSDave Hansen 		mnt->mnt_flags |= MNT_READONLY;
392d3ef3d73Snpiggin@suse.de 	/*
393d3ef3d73Snpiggin@suse.de 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
394d3ef3d73Snpiggin@suse.de 	 * that become unheld will see MNT_READONLY.
395d3ef3d73Snpiggin@suse.de 	 */
396d3ef3d73Snpiggin@suse.de 	smp_wmb();
397d3ef3d73Snpiggin@suse.de 	mnt->mnt_flags &= ~MNT_WRITE_HOLD;
39899b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
3993d733633SDave Hansen 	return ret;
4003d733633SDave Hansen }
4018366025eSDave Hansen 
4022e4b7fcdSDave Hansen static void __mnt_unmake_readonly(struct vfsmount *mnt)
4032e4b7fcdSDave Hansen {
40499b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
4052e4b7fcdSDave Hansen 	mnt->mnt_flags &= ~MNT_READONLY;
40699b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
4072e4b7fcdSDave Hansen }
4082e4b7fcdSDave Hansen 
409a3ec947cSSukadev Bhattiprolu void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
410454e2398SDavid Howells {
411454e2398SDavid Howells 	mnt->mnt_sb = sb;
412454e2398SDavid Howells 	mnt->mnt_root = dget(sb->s_root);
413454e2398SDavid Howells }
414454e2398SDavid Howells 
415454e2398SDavid Howells EXPORT_SYMBOL(simple_set_mnt);
416454e2398SDavid Howells 
4171da177e4SLinus Torvalds void free_vfsmnt(struct vfsmount *mnt)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	kfree(mnt->mnt_devname);
42073cd49ecSMiklos Szeredi 	mnt_free_id(mnt);
421d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP
422d3ef3d73Snpiggin@suse.de 	free_percpu(mnt->mnt_writers);
423d3ef3d73Snpiggin@suse.de #endif
4241da177e4SLinus Torvalds 	kmem_cache_free(mnt_cache, mnt);
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds /*
428a05964f3SRam Pai  * find the first or last mount at @dentry on vfsmount @mnt depending on
429a05964f3SRam Pai  * @dir. If @dir is set return the first mount else return the last mount.
43099b7db7bSNick Piggin  * vfsmount_lock must be held for read or write.
4311da177e4SLinus Torvalds  */
432a05964f3SRam Pai struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
433a05964f3SRam Pai 			      int dir)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
4361da177e4SLinus Torvalds 	struct list_head *tmp = head;
4371da177e4SLinus Torvalds 	struct vfsmount *p, *found = NULL;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	for (;;) {
440a05964f3SRam Pai 		tmp = dir ? tmp->next : tmp->prev;
4411da177e4SLinus Torvalds 		p = NULL;
4421da177e4SLinus Torvalds 		if (tmp == head)
4431da177e4SLinus Torvalds 			break;
4441da177e4SLinus Torvalds 		p = list_entry(tmp, struct vfsmount, mnt_hash);
4451da177e4SLinus Torvalds 		if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
446a05964f3SRam Pai 			found = p;
4471da177e4SLinus Torvalds 			break;
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 	return found;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
453a05964f3SRam Pai /*
454a05964f3SRam Pai  * lookup_mnt increments the ref count before returning
455a05964f3SRam Pai  * the vfsmount struct.
456a05964f3SRam Pai  */
4571c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path)
458a05964f3SRam Pai {
459a05964f3SRam Pai 	struct vfsmount *child_mnt;
46099b7db7bSNick Piggin 
46199b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
4621c755af4SAl Viro 	if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1)))
463a05964f3SRam Pai 		mntget(child_mnt);
46499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
465a05964f3SRam Pai 	return child_mnt;
466a05964f3SRam Pai }
467a05964f3SRam Pai 
4681da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt)
4691da177e4SLinus Torvalds {
4706b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
47399b7db7bSNick Piggin /*
47499b7db7bSNick Piggin  * vfsmount lock must be held for write
47599b7db7bSNick Piggin  */
4766b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
4775addc5ddSAl Viro {
4785addc5ddSAl Viro 	if (ns) {
4795addc5ddSAl Viro 		ns->event = ++event;
4805addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
4815addc5ddSAl Viro 	}
4825addc5ddSAl Viro }
4835addc5ddSAl Viro 
48499b7db7bSNick Piggin /*
48599b7db7bSNick Piggin  * vfsmount lock must be held for write
48699b7db7bSNick Piggin  */
4876b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
4885addc5ddSAl Viro {
4895addc5ddSAl Viro 	if (ns && ns->event != event) {
4905addc5ddSAl Viro 		ns->event = event;
4915addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
4925addc5ddSAl Viro 	}
4935addc5ddSAl Viro }
4945addc5ddSAl Viro 
49599b7db7bSNick Piggin /*
49699b7db7bSNick Piggin  * vfsmount lock must be held for write
49799b7db7bSNick Piggin  */
4981a390689SAl Viro static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
4991da177e4SLinus Torvalds {
5001a390689SAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
5011a390689SAl Viro 	old_path->mnt = mnt->mnt_parent;
5021da177e4SLinus Torvalds 	mnt->mnt_parent = mnt;
5031da177e4SLinus Torvalds 	mnt->mnt_mountpoint = mnt->mnt_root;
5041da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_child);
5051da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_hash);
5061a390689SAl Viro 	old_path->dentry->d_mounted--;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
50999b7db7bSNick Piggin /*
51099b7db7bSNick Piggin  * vfsmount lock must be held for write
51199b7db7bSNick Piggin  */
512b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
513b90fa9aeSRam Pai 			struct vfsmount *child_mnt)
514b90fa9aeSRam Pai {
515b90fa9aeSRam Pai 	child_mnt->mnt_parent = mntget(mnt);
516b90fa9aeSRam Pai 	child_mnt->mnt_mountpoint = dget(dentry);
517b90fa9aeSRam Pai 	dentry->d_mounted++;
518b90fa9aeSRam Pai }
519b90fa9aeSRam Pai 
52099b7db7bSNick Piggin /*
52199b7db7bSNick Piggin  * vfsmount lock must be held for write
52299b7db7bSNick Piggin  */
5231a390689SAl Viro static void attach_mnt(struct vfsmount *mnt, struct path *path)
5241da177e4SLinus Torvalds {
5251a390689SAl Viro 	mnt_set_mountpoint(path->mnt, path->dentry, mnt);
526b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
5271a390689SAl Viro 			hash(path->mnt, path->dentry));
5281a390689SAl Viro 	list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
529b90fa9aeSRam Pai }
530b90fa9aeSRam Pai 
531b90fa9aeSRam Pai /*
53299b7db7bSNick Piggin  * vfsmount lock must be held for write
533b90fa9aeSRam Pai  */
534b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt)
535b90fa9aeSRam Pai {
536b90fa9aeSRam Pai 	struct vfsmount *parent = mnt->mnt_parent;
537b90fa9aeSRam Pai 	struct vfsmount *m;
538b90fa9aeSRam Pai 	LIST_HEAD(head);
5396b3286edSKirill Korotaev 	struct mnt_namespace *n = parent->mnt_ns;
540b90fa9aeSRam Pai 
541b90fa9aeSRam Pai 	BUG_ON(parent == mnt);
542b90fa9aeSRam Pai 
543b90fa9aeSRam Pai 	list_add_tail(&head, &mnt->mnt_list);
544b90fa9aeSRam Pai 	list_for_each_entry(m, &head, mnt_list)
5456b3286edSKirill Korotaev 		m->mnt_ns = n;
546b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
547b90fa9aeSRam Pai 
548b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
549b90fa9aeSRam Pai 				hash(parent, mnt->mnt_mountpoint));
550b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
5516b3286edSKirill Korotaev 	touch_mnt_namespace(n);
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	struct list_head *next = p->mnt_mounts.next;
5571da177e4SLinus Torvalds 	if (next == &p->mnt_mounts) {
5581da177e4SLinus Torvalds 		while (1) {
5591da177e4SLinus Torvalds 			if (p == root)
5601da177e4SLinus Torvalds 				return NULL;
5611da177e4SLinus Torvalds 			next = p->mnt_child.next;
5621da177e4SLinus Torvalds 			if (next != &p->mnt_parent->mnt_mounts)
5631da177e4SLinus Torvalds 				break;
5641da177e4SLinus Torvalds 			p = p->mnt_parent;
5651da177e4SLinus Torvalds 		}
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 	return list_entry(next, struct vfsmount, mnt_child);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5709676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
5719676f0c6SRam Pai {
5729676f0c6SRam Pai 	struct list_head *prev = p->mnt_mounts.prev;
5739676f0c6SRam Pai 	while (prev != &p->mnt_mounts) {
5749676f0c6SRam Pai 		p = list_entry(prev, struct vfsmount, mnt_child);
5759676f0c6SRam Pai 		prev = p->mnt_mounts.prev;
5769676f0c6SRam Pai 	}
5779676f0c6SRam Pai 	return p;
5789676f0c6SRam Pai }
5799676f0c6SRam Pai 
58036341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
58136341f64SRam Pai 					int flag)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	struct super_block *sb = old->mnt_sb;
5841da177e4SLinus Torvalds 	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	if (mnt) {
587719f5d7fSMiklos Szeredi 		if (flag & (CL_SLAVE | CL_PRIVATE))
588719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = 0; /* not a peer of original */
589719f5d7fSMiklos Szeredi 		else
590719f5d7fSMiklos Szeredi 			mnt->mnt_group_id = old->mnt_group_id;
591719f5d7fSMiklos Szeredi 
592719f5d7fSMiklos Szeredi 		if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
593719f5d7fSMiklos Szeredi 			int err = mnt_alloc_group_id(mnt);
594719f5d7fSMiklos Szeredi 			if (err)
595719f5d7fSMiklos Szeredi 				goto out_free;
596719f5d7fSMiklos Szeredi 		}
597719f5d7fSMiklos Szeredi 
5981da177e4SLinus Torvalds 		mnt->mnt_flags = old->mnt_flags;
5991da177e4SLinus Torvalds 		atomic_inc(&sb->s_active);
6001da177e4SLinus Torvalds 		mnt->mnt_sb = sb;
6011da177e4SLinus Torvalds 		mnt->mnt_root = dget(root);
6021da177e4SLinus Torvalds 		mnt->mnt_mountpoint = mnt->mnt_root;
6031da177e4SLinus Torvalds 		mnt->mnt_parent = mnt;
604b90fa9aeSRam Pai 
6055afe0022SRam Pai 		if (flag & CL_SLAVE) {
6065afe0022SRam Pai 			list_add(&mnt->mnt_slave, &old->mnt_slave_list);
6075afe0022SRam Pai 			mnt->mnt_master = old;
6085afe0022SRam Pai 			CLEAR_MNT_SHARED(mnt);
6098aec0809SAl Viro 		} else if (!(flag & CL_PRIVATE)) {
610796a6b52SAl Viro 			if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
611b90fa9aeSRam Pai 				list_add(&mnt->mnt_share, &old->mnt_share);
6125afe0022SRam Pai 			if (IS_MNT_SLAVE(old))
6135afe0022SRam Pai 				list_add(&mnt->mnt_slave, &old->mnt_slave);
6145afe0022SRam Pai 			mnt->mnt_master = old->mnt_master;
6155afe0022SRam Pai 		}
616b90fa9aeSRam Pai 		if (flag & CL_MAKE_SHARED)
617b90fa9aeSRam Pai 			set_mnt_shared(mnt);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		/* stick the duplicate mount on the same expiry list
6201da177e4SLinus Torvalds 		 * as the original if that was on one */
62136341f64SRam Pai 		if (flag & CL_EXPIRE) {
62255e700b9SMiklos Szeredi 			if (!list_empty(&old->mnt_expire))
62355e700b9SMiklos Szeredi 				list_add(&mnt->mnt_expire, &old->mnt_expire);
6241da177e4SLinus Torvalds 		}
62536341f64SRam Pai 	}
6261da177e4SLinus Torvalds 	return mnt;
627719f5d7fSMiklos Szeredi 
628719f5d7fSMiklos Szeredi  out_free:
629719f5d7fSMiklos Szeredi 	free_vfsmnt(mnt);
630719f5d7fSMiklos Szeredi 	return NULL;
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6337b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
6363d733633SDave Hansen 	/*
6373d733633SDave Hansen 	 * This probably indicates that somebody messed
6383d733633SDave Hansen 	 * up a mnt_want/drop_write() pair.  If this
6393d733633SDave Hansen 	 * happens, the filesystem was probably unable
6403d733633SDave Hansen 	 * to make r/w->r/o transitions.
6413d733633SDave Hansen 	 */
642d3ef3d73Snpiggin@suse.de 	/*
643d3ef3d73Snpiggin@suse.de 	 * atomic_dec_and_lock() used to deal with ->mnt_count decrements
644d3ef3d73Snpiggin@suse.de 	 * provides barriers, so count_mnt_writers() below is safe.  AV
645d3ef3d73Snpiggin@suse.de 	 */
646d3ef3d73Snpiggin@suse.de 	WARN_ON(count_mnt_writers(mnt));
647ca9c726eSAndreas Gruenbacher 	fsnotify_vfsmount_delete(mnt);
6481da177e4SLinus Torvalds 	dput(mnt->mnt_root);
6491da177e4SLinus Torvalds 	free_vfsmnt(mnt);
6501da177e4SLinus Torvalds 	deactivate_super(sb);
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6537b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt)
6547b7b1aceSAl Viro {
6557b7b1aceSAl Viro repeat:
65699b7db7bSNick Piggin 	if (atomic_add_unless(&mnt->mnt_count, -1, 1))
65799b7db7bSNick Piggin 		return;
65899b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
65999b7db7bSNick Piggin 	if (!atomic_dec_and_test(&mnt->mnt_count)) {
66099b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
66199b7db7bSNick Piggin 		return;
66299b7db7bSNick Piggin 	}
6637b7b1aceSAl Viro 	if (likely(!mnt->mnt_pinned)) {
66499b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
6657b7b1aceSAl Viro 		__mntput(mnt);
6667b7b1aceSAl Viro 		return;
6677b7b1aceSAl Viro 	}
6687b7b1aceSAl Viro 	atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
6697b7b1aceSAl Viro 	mnt->mnt_pinned = 0;
67099b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
6717b7b1aceSAl Viro 	acct_auto_close_mnt(mnt);
6727b7b1aceSAl Viro 	goto repeat;
6737b7b1aceSAl Viro }
6747b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire);
6757b7b1aceSAl Viro 
6767b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
6777b7b1aceSAl Viro {
67899b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
6797b7b1aceSAl Viro 	mnt->mnt_pinned++;
68099b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
6817b7b1aceSAl Viro }
6827b7b1aceSAl Viro 
6837b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
6847b7b1aceSAl Viro 
6857b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt)
6867b7b1aceSAl Viro {
68799b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
6887b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
6897b7b1aceSAl Viro 		atomic_inc(&mnt->mnt_count);
6907b7b1aceSAl Viro 		mnt->mnt_pinned--;
6917b7b1aceSAl Viro 	}
69299b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
6937b7b1aceSAl Viro }
6947b7b1aceSAl Viro 
6957b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
6961da177e4SLinus Torvalds 
697b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
698b3b304a2SMiklos Szeredi {
699b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
700b3b304a2SMiklos Szeredi }
701b3b304a2SMiklos Szeredi 
702b3b304a2SMiklos Szeredi /*
703b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
704b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
705b3b304a2SMiklos Szeredi  *
706b3b304a2SMiklos Szeredi  * See also save_mount_options().
707b3b304a2SMiklos Szeredi  */
708b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
709b3b304a2SMiklos Szeredi {
7102a32cebdSAl Viro 	const char *options;
7112a32cebdSAl Viro 
7122a32cebdSAl Viro 	rcu_read_lock();
7132a32cebdSAl Viro 	options = rcu_dereference(mnt->mnt_sb->s_options);
714b3b304a2SMiklos Szeredi 
715b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
716b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
717b3b304a2SMiklos Szeredi 		mangle(m, options);
718b3b304a2SMiklos Szeredi 	}
7192a32cebdSAl Viro 	rcu_read_unlock();
720b3b304a2SMiklos Szeredi 
721b3b304a2SMiklos Szeredi 	return 0;
722b3b304a2SMiklos Szeredi }
723b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
724b3b304a2SMiklos Szeredi 
725b3b304a2SMiklos Szeredi /*
726b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
727b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
728b3b304a2SMiklos Szeredi  *
729b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
730b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
731b3b304a2SMiklos Szeredi  * remount fails.
732b3b304a2SMiklos Szeredi  *
733b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
734b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
735b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
736b3b304a2SMiklos Szeredi  * any more.
737b3b304a2SMiklos Szeredi  */
738b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
739b3b304a2SMiklos Szeredi {
7402a32cebdSAl Viro 	BUG_ON(sb->s_options);
7412a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
742b3b304a2SMiklos Szeredi }
743b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
744b3b304a2SMiklos Szeredi 
7452a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options)
7462a32cebdSAl Viro {
7472a32cebdSAl Viro 	char *old = sb->s_options;
7482a32cebdSAl Viro 	rcu_assign_pointer(sb->s_options, options);
7492a32cebdSAl Viro 	if (old) {
7502a32cebdSAl Viro 		synchronize_rcu();
7512a32cebdSAl Viro 		kfree(old);
7522a32cebdSAl Viro 	}
7532a32cebdSAl Viro }
7542a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options);
7552a32cebdSAl Viro 
756a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS
7571da177e4SLinus Torvalds /* iterator */
7581da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
7591da177e4SLinus Torvalds {
760a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
7611da177e4SLinus Torvalds 
762390c6843SRam Pai 	down_read(&namespace_sem);
763a1a2c409SMiklos Szeredi 	return seq_list_start(&p->ns->list, *pos);
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
7671da177e4SLinus Torvalds {
768a1a2c409SMiklos Szeredi 	struct proc_mounts *p = m->private;
769b0765fb8SPavel Emelianov 
770a1a2c409SMiklos Szeredi 	return seq_list_next(v, &p->ns->list, pos);
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
7741da177e4SLinus Torvalds {
775390c6843SRam Pai 	up_read(&namespace_sem);
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7789f5596afSAl Viro int mnt_had_events(struct proc_mounts *p)
7799f5596afSAl Viro {
7809f5596afSAl Viro 	struct mnt_namespace *ns = p->ns;
7819f5596afSAl Viro 	int res = 0;
7829f5596afSAl Viro 
78399b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
7849f5596afSAl Viro 	if (p->event != ns->event) {
7859f5596afSAl Viro 		p->event = ns->event;
7869f5596afSAl Viro 		res = 1;
7879f5596afSAl Viro 	}
78899b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
7899f5596afSAl Viro 
7909f5596afSAl Viro 	return res;
7919f5596afSAl Viro }
7929f5596afSAl Viro 
7932d4d4864SRam Pai struct proc_fs_info {
7941da177e4SLinus Torvalds 	int flag;
7952d4d4864SRam Pai 	const char *str;
7962d4d4864SRam Pai };
7972d4d4864SRam Pai 
7982069f457SEric Paris static int show_sb_opts(struct seq_file *m, struct super_block *sb)
7992d4d4864SRam Pai {
8002d4d4864SRam Pai 	static const struct proc_fs_info fs_info[] = {
8011da177e4SLinus Torvalds 		{ MS_SYNCHRONOUS, ",sync" },
8021da177e4SLinus Torvalds 		{ MS_DIRSYNC, ",dirsync" },
8031da177e4SLinus Torvalds 		{ MS_MANDLOCK, ",mand" },
8041da177e4SLinus Torvalds 		{ 0, NULL }
8051da177e4SLinus Torvalds 	};
8062d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
8072d4d4864SRam Pai 
8082d4d4864SRam Pai 	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
8092d4d4864SRam Pai 		if (sb->s_flags & fs_infop->flag)
8102d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
8112d4d4864SRam Pai 	}
8122069f457SEric Paris 
8132069f457SEric Paris 	return security_sb_show_options(m, sb);
8142d4d4864SRam Pai }
8152d4d4864SRam Pai 
8162d4d4864SRam Pai static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
8172d4d4864SRam Pai {
8182d4d4864SRam Pai 	static const struct proc_fs_info mnt_info[] = {
8191da177e4SLinus Torvalds 		{ MNT_NOSUID, ",nosuid" },
8201da177e4SLinus Torvalds 		{ MNT_NODEV, ",nodev" },
8211da177e4SLinus Torvalds 		{ MNT_NOEXEC, ",noexec" },
822fc33a7bbSChristoph Hellwig 		{ MNT_NOATIME, ",noatime" },
823fc33a7bbSChristoph Hellwig 		{ MNT_NODIRATIME, ",nodiratime" },
82447ae32d6SValerie Henson 		{ MNT_RELATIME, ",relatime" },
8251da177e4SLinus Torvalds 		{ 0, NULL }
8261da177e4SLinus Torvalds 	};
8272d4d4864SRam Pai 	const struct proc_fs_info *fs_infop;
8282d4d4864SRam Pai 
8292d4d4864SRam Pai 	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
8302d4d4864SRam Pai 		if (mnt->mnt_flags & fs_infop->flag)
8312d4d4864SRam Pai 			seq_puts(m, fs_infop->str);
8322d4d4864SRam Pai 	}
8332d4d4864SRam Pai }
8342d4d4864SRam Pai 
8352d4d4864SRam Pai static void show_type(struct seq_file *m, struct super_block *sb)
8362d4d4864SRam Pai {
8372d4d4864SRam Pai 	mangle(m, sb->s_type->name);
8382d4d4864SRam Pai 	if (sb->s_subtype && sb->s_subtype[0]) {
8392d4d4864SRam Pai 		seq_putc(m, '.');
8402d4d4864SRam Pai 		mangle(m, sb->s_subtype);
8412d4d4864SRam Pai 	}
8422d4d4864SRam Pai }
8432d4d4864SRam Pai 
8442d4d4864SRam Pai static int show_vfsmnt(struct seq_file *m, void *v)
8452d4d4864SRam Pai {
8462d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
8472d4d4864SRam Pai 	int err = 0;
848c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
8511da177e4SLinus Torvalds 	seq_putc(m, ' ');
852c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
8531da177e4SLinus Torvalds 	seq_putc(m, ' ');
8542d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
8552e4b7fcdSDave Hansen 	seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
8562069f457SEric Paris 	err = show_sb_opts(m, mnt->mnt_sb);
8572069f457SEric Paris 	if (err)
8582069f457SEric Paris 		goto out;
8592d4d4864SRam Pai 	show_mnt_opts(m, mnt);
8601da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_op->show_options)
8611da177e4SLinus Torvalds 		err = mnt->mnt_sb->s_op->show_options(m, mnt);
8621da177e4SLinus Torvalds 	seq_puts(m, " 0 0\n");
8632069f457SEric Paris out:
8641da177e4SLinus Torvalds 	return err;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
867a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = {
8681da177e4SLinus Torvalds 	.start	= m_start,
8691da177e4SLinus Torvalds 	.next	= m_next,
8701da177e4SLinus Torvalds 	.stop	= m_stop,
8711da177e4SLinus Torvalds 	.show	= show_vfsmnt
8721da177e4SLinus Torvalds };
8731da177e4SLinus Torvalds 
8742d4d4864SRam Pai static int show_mountinfo(struct seq_file *m, void *v)
8752d4d4864SRam Pai {
8762d4d4864SRam Pai 	struct proc_mounts *p = m->private;
8772d4d4864SRam Pai 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
8782d4d4864SRam Pai 	struct super_block *sb = mnt->mnt_sb;
8792d4d4864SRam Pai 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
8802d4d4864SRam Pai 	struct path root = p->root;
8812d4d4864SRam Pai 	int err = 0;
8822d4d4864SRam Pai 
8832d4d4864SRam Pai 	seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id,
8842d4d4864SRam Pai 		   MAJOR(sb->s_dev), MINOR(sb->s_dev));
8852d4d4864SRam Pai 	seq_dentry(m, mnt->mnt_root, " \t\n\\");
8862d4d4864SRam Pai 	seq_putc(m, ' ');
8872d4d4864SRam Pai 	seq_path_root(m, &mnt_path, &root, " \t\n\\");
8882d4d4864SRam Pai 	if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) {
8892d4d4864SRam Pai 		/*
8902d4d4864SRam Pai 		 * Mountpoint is outside root, discard that one.  Ugly,
8912d4d4864SRam Pai 		 * but less so than trying to do that in iterator in a
8922d4d4864SRam Pai 		 * race-free way (due to renames).
8932d4d4864SRam Pai 		 */
8942d4d4864SRam Pai 		return SEQ_SKIP;
8952d4d4864SRam Pai 	}
8962d4d4864SRam Pai 	seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
8972d4d4864SRam Pai 	show_mnt_opts(m, mnt);
8982d4d4864SRam Pai 
8992d4d4864SRam Pai 	/* Tagged fields ("foo:X" or "bar") */
9002d4d4864SRam Pai 	if (IS_MNT_SHARED(mnt))
9012d4d4864SRam Pai 		seq_printf(m, " shared:%i", mnt->mnt_group_id);
90297e7e0f7SMiklos Szeredi 	if (IS_MNT_SLAVE(mnt)) {
90397e7e0f7SMiklos Szeredi 		int master = mnt->mnt_master->mnt_group_id;
90497e7e0f7SMiklos Szeredi 		int dom = get_dominating_id(mnt, &p->root);
90597e7e0f7SMiklos Szeredi 		seq_printf(m, " master:%i", master);
90697e7e0f7SMiklos Szeredi 		if (dom && dom != master)
90797e7e0f7SMiklos Szeredi 			seq_printf(m, " propagate_from:%i", dom);
90897e7e0f7SMiklos Szeredi 	}
9092d4d4864SRam Pai 	if (IS_MNT_UNBINDABLE(mnt))
9102d4d4864SRam Pai 		seq_puts(m, " unbindable");
9112d4d4864SRam Pai 
9122d4d4864SRam Pai 	/* Filesystem specific data */
9132d4d4864SRam Pai 	seq_puts(m, " - ");
9142d4d4864SRam Pai 	show_type(m, sb);
9152d4d4864SRam Pai 	seq_putc(m, ' ');
9162d4d4864SRam Pai 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
9172d4d4864SRam Pai 	seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
9182069f457SEric Paris 	err = show_sb_opts(m, sb);
9192069f457SEric Paris 	if (err)
9202069f457SEric Paris 		goto out;
9212d4d4864SRam Pai 	if (sb->s_op->show_options)
9222d4d4864SRam Pai 		err = sb->s_op->show_options(m, mnt);
9232d4d4864SRam Pai 	seq_putc(m, '\n');
9242069f457SEric Paris out:
9252d4d4864SRam Pai 	return err;
9262d4d4864SRam Pai }
9272d4d4864SRam Pai 
9282d4d4864SRam Pai const struct seq_operations mountinfo_op = {
9292d4d4864SRam Pai 	.start	= m_start,
9302d4d4864SRam Pai 	.next	= m_next,
9312d4d4864SRam Pai 	.stop	= m_stop,
9322d4d4864SRam Pai 	.show	= show_mountinfo,
9332d4d4864SRam Pai };
9342d4d4864SRam Pai 
935b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v)
936b4629fe2SChuck Lever {
937b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
938c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
939b4629fe2SChuck Lever 	int err = 0;
940b4629fe2SChuck Lever 
941b4629fe2SChuck Lever 	/* device */
942b4629fe2SChuck Lever 	if (mnt->mnt_devname) {
943b4629fe2SChuck Lever 		seq_puts(m, "device ");
944b4629fe2SChuck Lever 		mangle(m, mnt->mnt_devname);
945b4629fe2SChuck Lever 	} else
946b4629fe2SChuck Lever 		seq_puts(m, "no device");
947b4629fe2SChuck Lever 
948b4629fe2SChuck Lever 	/* mount point */
949b4629fe2SChuck Lever 	seq_puts(m, " mounted on ");
950c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
951b4629fe2SChuck Lever 	seq_putc(m, ' ');
952b4629fe2SChuck Lever 
953b4629fe2SChuck Lever 	/* file system type */
954b4629fe2SChuck Lever 	seq_puts(m, "with fstype ");
9552d4d4864SRam Pai 	show_type(m, mnt->mnt_sb);
956b4629fe2SChuck Lever 
957b4629fe2SChuck Lever 	/* optional statistics */
958b4629fe2SChuck Lever 	if (mnt->mnt_sb->s_op->show_stats) {
959b4629fe2SChuck Lever 		seq_putc(m, ' ');
960b4629fe2SChuck Lever 		err = mnt->mnt_sb->s_op->show_stats(m, mnt);
961b4629fe2SChuck Lever 	}
962b4629fe2SChuck Lever 
963b4629fe2SChuck Lever 	seq_putc(m, '\n');
964b4629fe2SChuck Lever 	return err;
965b4629fe2SChuck Lever }
966b4629fe2SChuck Lever 
967a1a2c409SMiklos Szeredi const struct seq_operations mountstats_op = {
968b4629fe2SChuck Lever 	.start	= m_start,
969b4629fe2SChuck Lever 	.next	= m_next,
970b4629fe2SChuck Lever 	.stop	= m_stop,
971b4629fe2SChuck Lever 	.show	= show_vfsstat,
972b4629fe2SChuck Lever };
973a1a2c409SMiklos Szeredi #endif  /* CONFIG_PROC_FS */
974b4629fe2SChuck Lever 
9751da177e4SLinus Torvalds /**
9761da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
9771da177e4SLinus Torvalds  * @mnt: root of mount tree
9781da177e4SLinus Torvalds  *
9791da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
9801da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
9811da177e4SLinus Torvalds  * busy.
9821da177e4SLinus Torvalds  */
9831da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt)
9841da177e4SLinus Torvalds {
98536341f64SRam Pai 	int actual_refs = 0;
98636341f64SRam Pai 	int minimum_refs = 0;
98736341f64SRam Pai 	struct vfsmount *p;
9881da177e4SLinus Torvalds 
98999b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
99036341f64SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
9911da177e4SLinus Torvalds 		actual_refs += atomic_read(&p->mnt_count);
9921da177e4SLinus Torvalds 		minimum_refs += 2;
9931da177e4SLinus Torvalds 	}
99499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
9971da177e4SLinus Torvalds 		return 0;
998e3474a8eSIan Kent 
999e3474a8eSIan Kent 	return 1;
10001da177e4SLinus Torvalds }
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds /**
10051da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
10061da177e4SLinus Torvalds  * @mnt: root of mount
10071da177e4SLinus Torvalds  *
10081da177e4SLinus Torvalds  * This is called to check if a mount point has any
10091da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
10101da177e4SLinus Torvalds  * mount has sub mounts this will return busy
10111da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
10121da177e4SLinus Torvalds  *
10131da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
10141da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
10151da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
10161da177e4SLinus Torvalds  */
10171da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
10181da177e4SLinus Torvalds {
1019e3474a8eSIan Kent 	int ret = 1;
10208ad08d8aSAl Viro 	down_read(&namespace_sem);
102199b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
1022a05964f3SRam Pai 	if (propagate_mount_busy(mnt, 2))
1023e3474a8eSIan Kent 		ret = 0;
102499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
10258ad08d8aSAl Viro 	up_read(&namespace_sem);
1026a05964f3SRam Pai 	return ret;
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
10301da177e4SLinus Torvalds 
1031b90fa9aeSRam Pai void release_mounts(struct list_head *head)
10321da177e4SLinus Torvalds {
103370fbcdf4SRam Pai 	struct vfsmount *mnt;
103470fbcdf4SRam Pai 	while (!list_empty(head)) {
1035b5e61818SPavel Emelianov 		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
103670fbcdf4SRam Pai 		list_del_init(&mnt->mnt_hash);
103770fbcdf4SRam Pai 		if (mnt->mnt_parent != mnt) {
103870fbcdf4SRam Pai 			struct dentry *dentry;
103970fbcdf4SRam Pai 			struct vfsmount *m;
104099b7db7bSNick Piggin 
104199b7db7bSNick Piggin 			br_write_lock(vfsmount_lock);
104270fbcdf4SRam Pai 			dentry = mnt->mnt_mountpoint;
104370fbcdf4SRam Pai 			m = mnt->mnt_parent;
104470fbcdf4SRam Pai 			mnt->mnt_mountpoint = mnt->mnt_root;
104570fbcdf4SRam Pai 			mnt->mnt_parent = mnt;
10467c4b93d8SAl Viro 			m->mnt_ghosts--;
104799b7db7bSNick Piggin 			br_write_unlock(vfsmount_lock);
104870fbcdf4SRam Pai 			dput(dentry);
104970fbcdf4SRam Pai 			mntput(m);
10501da177e4SLinus Torvalds 		}
10511da177e4SLinus Torvalds 		mntput(mnt);
105270fbcdf4SRam Pai 	}
105370fbcdf4SRam Pai }
105470fbcdf4SRam Pai 
105599b7db7bSNick Piggin /*
105699b7db7bSNick Piggin  * vfsmount lock must be held for write
105799b7db7bSNick Piggin  * namespace_sem must be held for write
105899b7db7bSNick Piggin  */
1059a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
106070fbcdf4SRam Pai {
106170fbcdf4SRam Pai 	struct vfsmount *p;
106270fbcdf4SRam Pai 
10631bfba4e8SAkinobu Mita 	for (p = mnt; p; p = next_mnt(p, mnt))
10641bfba4e8SAkinobu Mita 		list_move(&p->mnt_hash, kill);
106570fbcdf4SRam Pai 
1066a05964f3SRam Pai 	if (propagate)
1067a05964f3SRam Pai 		propagate_umount(kill);
1068a05964f3SRam Pai 
106970fbcdf4SRam Pai 	list_for_each_entry(p, kill, mnt_hash) {
107070fbcdf4SRam Pai 		list_del_init(&p->mnt_expire);
107170fbcdf4SRam Pai 		list_del_init(&p->mnt_list);
10726b3286edSKirill Korotaev 		__touch_mnt_namespace(p->mnt_ns);
10736b3286edSKirill Korotaev 		p->mnt_ns = NULL;
107470fbcdf4SRam Pai 		list_del_init(&p->mnt_child);
10757c4b93d8SAl Viro 		if (p->mnt_parent != p) {
10767c4b93d8SAl Viro 			p->mnt_parent->mnt_ghosts++;
1077f30ac319SAl Viro 			p->mnt_mountpoint->d_mounted--;
10787c4b93d8SAl Viro 		}
1079a05964f3SRam Pai 		change_mnt_propagation(p, MS_PRIVATE);
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds }
10821da177e4SLinus Torvalds 
1083c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
1084c35038beSAl Viro 
10851da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
10881da177e4SLinus Torvalds 	int retval;
108970fbcdf4SRam Pai 	LIST_HEAD(umount_list);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	retval = security_sb_umount(mnt, flags);
10921da177e4SLinus Torvalds 	if (retval)
10931da177e4SLinus Torvalds 		return retval;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	/*
10961da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
10971da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
10981da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
10991da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
11001da177e4SLinus Torvalds 	 */
11011da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
11026ac08c39SJan Blunck 		if (mnt == current->fs->root.mnt ||
11031da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
11041da177e4SLinus Torvalds 			return -EINVAL;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 		if (atomic_read(&mnt->mnt_count) != 2)
11071da177e4SLinus Torvalds 			return -EBUSY;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1))
11101da177e4SLinus Torvalds 			return -EAGAIN;
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	/*
11141da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
11151da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
11161da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
11171da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
11181da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
11191da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
11201da177e4SLinus Torvalds 	 * about for the moment.
11211da177e4SLinus Torvalds 	 */
11221da177e4SLinus Torvalds 
112342faad99SAl Viro 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
112442faad99SAl Viro 		sb->s_op->umount_begin(sb);
112542faad99SAl Viro 	}
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	/*
11281da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
11291da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
11301da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
11311da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
11321da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
11331da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
11341da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
11351da177e4SLinus Torvalds 	 */
11366ac08c39SJan Blunck 	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
11371da177e4SLinus Torvalds 		/*
11381da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
11391da177e4SLinus Torvalds 		 * we just try to remount it readonly.
11401da177e4SLinus Torvalds 		 */
11411da177e4SLinus Torvalds 		down_write(&sb->s_umount);
11424aa98cf7SAl Viro 		if (!(sb->s_flags & MS_RDONLY))
11431da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
11441da177e4SLinus Torvalds 		up_write(&sb->s_umount);
11451da177e4SLinus Torvalds 		return retval;
11461da177e4SLinus Torvalds 	}
11471da177e4SLinus Torvalds 
1148390c6843SRam Pai 	down_write(&namespace_sem);
114999b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
11505addc5ddSAl Viro 	event++;
11511da177e4SLinus Torvalds 
1152c35038beSAl Viro 	if (!(flags & MNT_DETACH))
1153c35038beSAl Viro 		shrink_submounts(mnt, &umount_list);
1154c35038beSAl Viro 
11551da177e4SLinus Torvalds 	retval = -EBUSY;
1156a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
11571da177e4SLinus Torvalds 		if (!list_empty(&mnt->mnt_list))
1158a05964f3SRam Pai 			umount_tree(mnt, 1, &umount_list);
11591da177e4SLinus Torvalds 		retval = 0;
11601da177e4SLinus Torvalds 	}
116199b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
1162390c6843SRam Pai 	up_write(&namespace_sem);
116370fbcdf4SRam Pai 	release_mounts(&umount_list);
11641da177e4SLinus Torvalds 	return retval;
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds /*
11681da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
11691da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
11701da177e4SLinus Torvalds  *
11711da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
11721da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
11731da177e4SLinus Torvalds  */
11741da177e4SLinus Torvalds 
1175bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
11761da177e4SLinus Torvalds {
11772d8f3038SAl Viro 	struct path path;
11781da177e4SLinus Torvalds 	int retval;
1179db1f05bbSMiklos Szeredi 	int lookup_flags = 0;
11801da177e4SLinus Torvalds 
1181db1f05bbSMiklos Szeredi 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1182db1f05bbSMiklos Szeredi 		return -EINVAL;
1183db1f05bbSMiklos Szeredi 
1184db1f05bbSMiklos Szeredi 	if (!(flags & UMOUNT_NOFOLLOW))
1185db1f05bbSMiklos Szeredi 		lookup_flags |= LOOKUP_FOLLOW;
1186db1f05bbSMiklos Szeredi 
1187db1f05bbSMiklos Szeredi 	retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
11881da177e4SLinus Torvalds 	if (retval)
11891da177e4SLinus Torvalds 		goto out;
11901da177e4SLinus Torvalds 	retval = -EINVAL;
11912d8f3038SAl Viro 	if (path.dentry != path.mnt->mnt_root)
11921da177e4SLinus Torvalds 		goto dput_and_out;
11932d8f3038SAl Viro 	if (!check_mnt(path.mnt))
11941da177e4SLinus Torvalds 		goto dput_and_out;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	retval = -EPERM;
11971da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
11981da177e4SLinus Torvalds 		goto dput_and_out;
11991da177e4SLinus Torvalds 
12002d8f3038SAl Viro 	retval = do_umount(path.mnt, flags);
12011da177e4SLinus Torvalds dput_and_out:
1202429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
12032d8f3038SAl Viro 	dput(path.dentry);
12042d8f3038SAl Viro 	mntput_no_expire(path.mnt);
12051da177e4SLinus Torvalds out:
12061da177e4SLinus Torvalds 	return retval;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds /*
12121da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
12131da177e4SLinus Torvalds  */
1214bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name)
12151da177e4SLinus Torvalds {
12161da177e4SLinus Torvalds 	return sys_umount(name, 0);
12171da177e4SLinus Torvalds }
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds #endif
12201da177e4SLinus Torvalds 
12212d92ab3cSAl Viro static int mount_is_safe(struct path *path)
12221da177e4SLinus Torvalds {
12231da177e4SLinus Torvalds 	if (capable(CAP_SYS_ADMIN))
12241da177e4SLinus Torvalds 		return 0;
12251da177e4SLinus Torvalds 	return -EPERM;
12261da177e4SLinus Torvalds #ifdef notyet
12272d92ab3cSAl Viro 	if (S_ISLNK(path->dentry->d_inode->i_mode))
12281da177e4SLinus Torvalds 		return -EPERM;
12292d92ab3cSAl Viro 	if (path->dentry->d_inode->i_mode & S_ISVTX) {
1230da9592edSDavid Howells 		if (current_uid() != path->dentry->d_inode->i_uid)
12311da177e4SLinus Torvalds 			return -EPERM;
12321da177e4SLinus Torvalds 	}
12332d92ab3cSAl Viro 	if (inode_permission(path->dentry->d_inode, MAY_WRITE))
12341da177e4SLinus Torvalds 		return -EPERM;
12351da177e4SLinus Torvalds 	return 0;
12361da177e4SLinus Torvalds #endif
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
1239b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
124036341f64SRam Pai 					int flag)
12411da177e4SLinus Torvalds {
12421da177e4SLinus Torvalds 	struct vfsmount *res, *p, *q, *r, *s;
12431a390689SAl Viro 	struct path path;
12441da177e4SLinus Torvalds 
12459676f0c6SRam Pai 	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
12469676f0c6SRam Pai 		return NULL;
12479676f0c6SRam Pai 
124836341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
12491da177e4SLinus Torvalds 	if (!q)
12501da177e4SLinus Torvalds 		goto Enomem;
12511da177e4SLinus Torvalds 	q->mnt_mountpoint = mnt->mnt_mountpoint;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	p = mnt;
1254fdadd65fSDomen Puncer 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
12557ec02ef1SJan Blunck 		if (!is_subdir(r->mnt_mountpoint, dentry))
12561da177e4SLinus Torvalds 			continue;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 		for (s = r; s; s = next_mnt(s, r)) {
12599676f0c6SRam Pai 			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
12609676f0c6SRam Pai 				s = skip_mnt_tree(s);
12619676f0c6SRam Pai 				continue;
12629676f0c6SRam Pai 			}
12631da177e4SLinus Torvalds 			while (p != s->mnt_parent) {
12641da177e4SLinus Torvalds 				p = p->mnt_parent;
12651da177e4SLinus Torvalds 				q = q->mnt_parent;
12661da177e4SLinus Torvalds 			}
12671da177e4SLinus Torvalds 			p = s;
12681a390689SAl Viro 			path.mnt = q;
12691a390689SAl Viro 			path.dentry = p->mnt_mountpoint;
127036341f64SRam Pai 			q = clone_mnt(p, p->mnt_root, flag);
12711da177e4SLinus Torvalds 			if (!q)
12721da177e4SLinus Torvalds 				goto Enomem;
127399b7db7bSNick Piggin 			br_write_lock(vfsmount_lock);
12741da177e4SLinus Torvalds 			list_add_tail(&q->mnt_list, &res->mnt_list);
12751a390689SAl Viro 			attach_mnt(q, &path);
127699b7db7bSNick Piggin 			br_write_unlock(vfsmount_lock);
12771da177e4SLinus Torvalds 		}
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds 	return res;
12801da177e4SLinus Torvalds Enomem:
12811da177e4SLinus Torvalds 	if (res) {
128270fbcdf4SRam Pai 		LIST_HEAD(umount_list);
128399b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1284a05964f3SRam Pai 		umount_tree(res, 0, &umount_list);
128599b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
128670fbcdf4SRam Pai 		release_mounts(&umount_list);
12871da177e4SLinus Torvalds 	}
12881da177e4SLinus Torvalds 	return NULL;
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
1291589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path)
12928aec0809SAl Viro {
12938aec0809SAl Viro 	struct vfsmount *tree;
12941a60a280SAl Viro 	down_write(&namespace_sem);
1295589ff870SAl Viro 	tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE);
12961a60a280SAl Viro 	up_write(&namespace_sem);
12978aec0809SAl Viro 	return tree;
12988aec0809SAl Viro }
12998aec0809SAl Viro 
13008aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
13018aec0809SAl Viro {
13028aec0809SAl Viro 	LIST_HEAD(umount_list);
13031a60a280SAl Viro 	down_write(&namespace_sem);
130499b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
13058aec0809SAl Viro 	umount_tree(mnt, 0, &umount_list);
130699b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
13071a60a280SAl Viro 	up_write(&namespace_sem);
13088aec0809SAl Viro 	release_mounts(&umount_list);
13098aec0809SAl Viro }
13108aec0809SAl Viro 
13111f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
13121f707137SAl Viro 		   struct vfsmount *root)
13131f707137SAl Viro {
13141f707137SAl Viro 	struct vfsmount *mnt;
13151f707137SAl Viro 	int res = f(root, arg);
13161f707137SAl Viro 	if (res)
13171f707137SAl Viro 		return res;
13181f707137SAl Viro 	list_for_each_entry(mnt, &root->mnt_list, mnt_list) {
13191f707137SAl Viro 		res = f(mnt, arg);
13201f707137SAl Viro 		if (res)
13211f707137SAl Viro 			return res;
13221f707137SAl Viro 	}
13231f707137SAl Viro 	return 0;
13241f707137SAl Viro }
13251f707137SAl Viro 
1326719f5d7fSMiklos Szeredi static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
1327719f5d7fSMiklos Szeredi {
1328719f5d7fSMiklos Szeredi 	struct vfsmount *p;
1329719f5d7fSMiklos Szeredi 
1330719f5d7fSMiklos Szeredi 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1331719f5d7fSMiklos Szeredi 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
1332719f5d7fSMiklos Szeredi 			mnt_release_group_id(p);
1333719f5d7fSMiklos Szeredi 	}
1334719f5d7fSMiklos Szeredi }
1335719f5d7fSMiklos Szeredi 
1336719f5d7fSMiklos Szeredi static int invent_group_ids(struct vfsmount *mnt, bool recurse)
1337719f5d7fSMiklos Szeredi {
1338719f5d7fSMiklos Szeredi 	struct vfsmount *p;
1339719f5d7fSMiklos Szeredi 
1340719f5d7fSMiklos Szeredi 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1341719f5d7fSMiklos Szeredi 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1342719f5d7fSMiklos Szeredi 			int err = mnt_alloc_group_id(p);
1343719f5d7fSMiklos Szeredi 			if (err) {
1344719f5d7fSMiklos Szeredi 				cleanup_group_ids(mnt, p);
1345719f5d7fSMiklos Szeredi 				return err;
1346719f5d7fSMiklos Szeredi 			}
1347719f5d7fSMiklos Szeredi 		}
1348719f5d7fSMiklos Szeredi 	}
1349719f5d7fSMiklos Szeredi 
1350719f5d7fSMiklos Szeredi 	return 0;
1351719f5d7fSMiklos Szeredi }
1352719f5d7fSMiklos Szeredi 
1353b90fa9aeSRam Pai /*
1354b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
1355b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
135621444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
135721444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
135821444403SRam Pai  *  		   (done when source_mnt is moved)
1359b90fa9aeSRam Pai  *
1360b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
1361b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
13629676f0c6SRam Pai  * ---------------------------------------------------------------------------
1363b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
13649676f0c6SRam Pai  * |**************************************************************************
13659676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
13669676f0c6SRam Pai  * | dest     |               |                |                |            |
13679676f0c6SRam Pai  * |   |      |               |                |                |            |
13689676f0c6SRam Pai  * |   v      |               |                |                |            |
13699676f0c6SRam Pai  * |**************************************************************************
13709676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
13715afe0022SRam Pai  * |          |               |                |                |            |
13729676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
13739676f0c6SRam Pai  * ***************************************************************************
1374b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
1375b90fa9aeSRam Pai  * destination mount.
1376b90fa9aeSRam Pai  *
1377b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
1378b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
1379b90fa9aeSRam Pai  * 	 the peer group of the source mount.
1380b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
1381b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
1382b90fa9aeSRam Pai  *       mount.
13835afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
13845afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
13855afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
13865afe0022SRam Pai  *       is marked as 'shared and slave'.
13875afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
13885afe0022SRam Pai  * 	 source mount.
13895afe0022SRam Pai  *
13909676f0c6SRam Pai  * ---------------------------------------------------------------------------
139121444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
13929676f0c6SRam Pai  * |**************************************************************************
13939676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
13949676f0c6SRam Pai  * | dest     |               |                |                |            |
13959676f0c6SRam Pai  * |   |      |               |                |                |            |
13969676f0c6SRam Pai  * |   v      |               |                |                |            |
13979676f0c6SRam Pai  * |**************************************************************************
13989676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
13995afe0022SRam Pai  * |          |               |                |                |            |
14009676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
14019676f0c6SRam Pai  * ***************************************************************************
14025afe0022SRam Pai  *
14035afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
14045afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
140521444403SRam Pai  * (+*)  the mount is moved to the destination.
14065afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
14075afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
14085afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
14095afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
1410b90fa9aeSRam Pai  *
1411b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
1412b90fa9aeSRam Pai  * applied to each mount in the tree.
1413b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
1414b90fa9aeSRam Pai  * in allocations.
1415b90fa9aeSRam Pai  */
1416b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt,
14171a390689SAl Viro 			struct path *path, struct path *parent_path)
1418b90fa9aeSRam Pai {
1419b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
14201a390689SAl Viro 	struct vfsmount *dest_mnt = path->mnt;
14211a390689SAl Viro 	struct dentry *dest_dentry = path->dentry;
1422b90fa9aeSRam Pai 	struct vfsmount *child, *p;
1423719f5d7fSMiklos Szeredi 	int err;
1424b90fa9aeSRam Pai 
1425719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt)) {
1426719f5d7fSMiklos Szeredi 		err = invent_group_ids(source_mnt, true);
1427719f5d7fSMiklos Szeredi 		if (err)
1428719f5d7fSMiklos Szeredi 			goto out;
1429719f5d7fSMiklos Szeredi 	}
1430719f5d7fSMiklos Szeredi 	err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1431719f5d7fSMiklos Szeredi 	if (err)
1432719f5d7fSMiklos Szeredi 		goto out_cleanup_ids;
1433b90fa9aeSRam Pai 
143499b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
1435df1a1ad2SAl Viro 
1436b90fa9aeSRam Pai 	if (IS_MNT_SHARED(dest_mnt)) {
1437b90fa9aeSRam Pai 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1438b90fa9aeSRam Pai 			set_mnt_shared(p);
1439b90fa9aeSRam Pai 	}
14401a390689SAl Viro 	if (parent_path) {
14411a390689SAl Viro 		detach_mnt(source_mnt, parent_path);
14421a390689SAl Viro 		attach_mnt(source_mnt, path);
1443e5d67f07SAl Viro 		touch_mnt_namespace(parent_path->mnt->mnt_ns);
144421444403SRam Pai 	} else {
1445b90fa9aeSRam Pai 		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
1446b90fa9aeSRam Pai 		commit_tree(source_mnt);
144721444403SRam Pai 	}
1448b90fa9aeSRam Pai 
1449b90fa9aeSRam Pai 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1450b90fa9aeSRam Pai 		list_del_init(&child->mnt_hash);
1451b90fa9aeSRam Pai 		commit_tree(child);
1452b90fa9aeSRam Pai 	}
145399b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
145499b7db7bSNick Piggin 
1455b90fa9aeSRam Pai 	return 0;
1456719f5d7fSMiklos Szeredi 
1457719f5d7fSMiklos Szeredi  out_cleanup_ids:
1458719f5d7fSMiklos Szeredi 	if (IS_MNT_SHARED(dest_mnt))
1459719f5d7fSMiklos Szeredi 		cleanup_group_ids(source_mnt, NULL);
1460719f5d7fSMiklos Szeredi  out:
1461719f5d7fSMiklos Szeredi 	return err;
1462b90fa9aeSRam Pai }
1463b90fa9aeSRam Pai 
14648c3ee42eSAl Viro static int graft_tree(struct vfsmount *mnt, struct path *path)
14651da177e4SLinus Torvalds {
14661da177e4SLinus Torvalds 	int err;
14671da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_flags & MS_NOUSER)
14681da177e4SLinus Torvalds 		return -EINVAL;
14691da177e4SLinus Torvalds 
14708c3ee42eSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
14711da177e4SLinus Torvalds 	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
14721da177e4SLinus Torvalds 		return -ENOTDIR;
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	err = -ENOENT;
14758c3ee42eSAl Viro 	mutex_lock(&path->dentry->d_inode->i_mutex);
1476d83c49f3SAl Viro 	if (cant_mount(path->dentry))
14771da177e4SLinus Torvalds 		goto out_unlock;
14781da177e4SLinus Torvalds 
1479f3da392eSAlexey Dobriyan 	if (!d_unlinked(path->dentry))
14808c3ee42eSAl Viro 		err = attach_recursive_mnt(mnt, path, NULL);
14811da177e4SLinus Torvalds out_unlock:
14828c3ee42eSAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
14831da177e4SLinus Torvalds 	return err;
14841da177e4SLinus Torvalds }
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds /*
148707b20889SRam Pai  * recursively change the type of the mountpoint.
148807b20889SRam Pai  */
14890a0d8a46SAl Viro static int do_change_type(struct path *path, int flag)
149007b20889SRam Pai {
14912d92ab3cSAl Viro 	struct vfsmount *m, *mnt = path->mnt;
149207b20889SRam Pai 	int recurse = flag & MS_REC;
149307b20889SRam Pai 	int type = flag & ~MS_REC;
1494719f5d7fSMiklos Szeredi 	int err = 0;
149507b20889SRam Pai 
1496ee6f9582SMiklos Szeredi 	if (!capable(CAP_SYS_ADMIN))
1497ee6f9582SMiklos Szeredi 		return -EPERM;
1498ee6f9582SMiklos Szeredi 
14992d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
150007b20889SRam Pai 		return -EINVAL;
150107b20889SRam Pai 
150207b20889SRam Pai 	down_write(&namespace_sem);
1503719f5d7fSMiklos Szeredi 	if (type == MS_SHARED) {
1504719f5d7fSMiklos Szeredi 		err = invent_group_ids(mnt, recurse);
1505719f5d7fSMiklos Szeredi 		if (err)
1506719f5d7fSMiklos Szeredi 			goto out_unlock;
1507719f5d7fSMiklos Szeredi 	}
1508719f5d7fSMiklos Szeredi 
150999b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
151007b20889SRam Pai 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
151107b20889SRam Pai 		change_mnt_propagation(m, type);
151299b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
1513719f5d7fSMiklos Szeredi 
1514719f5d7fSMiklos Szeredi  out_unlock:
151507b20889SRam Pai 	up_write(&namespace_sem);
1516719f5d7fSMiklos Szeredi 	return err;
151707b20889SRam Pai }
151807b20889SRam Pai 
151907b20889SRam Pai /*
15201da177e4SLinus Torvalds  * do loopback mount.
15211da177e4SLinus Torvalds  */
15220a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name,
15232dafe1c4SEric Sandeen 				int recurse)
15241da177e4SLinus Torvalds {
15252d92ab3cSAl Viro 	struct path old_path;
15261da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
15272d92ab3cSAl Viro 	int err = mount_is_safe(path);
15281da177e4SLinus Torvalds 	if (err)
15291da177e4SLinus Torvalds 		return err;
15301da177e4SLinus Torvalds 	if (!old_name || !*old_name)
15311da177e4SLinus Torvalds 		return -EINVAL;
15322d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
15331da177e4SLinus Torvalds 	if (err)
15341da177e4SLinus Torvalds 		return err;
15351da177e4SLinus Torvalds 
1536390c6843SRam Pai 	down_write(&namespace_sem);
15371da177e4SLinus Torvalds 	err = -EINVAL;
15382d92ab3cSAl Viro 	if (IS_MNT_UNBINDABLE(old_path.mnt))
15399676f0c6SRam Pai 		goto out;
15409676f0c6SRam Pai 
15412d92ab3cSAl Viro 	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
1542ccd48bc7SAl Viro 		goto out;
1543ccd48bc7SAl Viro 
15441da177e4SLinus Torvalds 	err = -ENOMEM;
15451da177e4SLinus Torvalds 	if (recurse)
15462d92ab3cSAl Viro 		mnt = copy_tree(old_path.mnt, old_path.dentry, 0);
15471da177e4SLinus Torvalds 	else
15482d92ab3cSAl Viro 		mnt = clone_mnt(old_path.mnt, old_path.dentry, 0);
15491da177e4SLinus Torvalds 
1550ccd48bc7SAl Viro 	if (!mnt)
1551ccd48bc7SAl Viro 		goto out;
1552ccd48bc7SAl Viro 
15532d92ab3cSAl Viro 	err = graft_tree(mnt, path);
15541da177e4SLinus Torvalds 	if (err) {
155570fbcdf4SRam Pai 		LIST_HEAD(umount_list);
155699b7db7bSNick Piggin 
155799b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1558a05964f3SRam Pai 		umount_tree(mnt, 0, &umount_list);
155999b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
156070fbcdf4SRam Pai 		release_mounts(&umount_list);
15615b83d2c5SRam Pai 	}
15621da177e4SLinus Torvalds 
1563ccd48bc7SAl Viro out:
1564390c6843SRam Pai 	up_write(&namespace_sem);
15652d92ab3cSAl Viro 	path_put(&old_path);
15661da177e4SLinus Torvalds 	return err;
15671da177e4SLinus Torvalds }
15681da177e4SLinus Torvalds 
15692e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
15702e4b7fcdSDave Hansen {
15712e4b7fcdSDave Hansen 	int error = 0;
15722e4b7fcdSDave Hansen 	int readonly_request = 0;
15732e4b7fcdSDave Hansen 
15742e4b7fcdSDave Hansen 	if (ms_flags & MS_RDONLY)
15752e4b7fcdSDave Hansen 		readonly_request = 1;
15762e4b7fcdSDave Hansen 	if (readonly_request == __mnt_is_readonly(mnt))
15772e4b7fcdSDave Hansen 		return 0;
15782e4b7fcdSDave Hansen 
15792e4b7fcdSDave Hansen 	if (readonly_request)
15802e4b7fcdSDave Hansen 		error = mnt_make_readonly(mnt);
15812e4b7fcdSDave Hansen 	else
15822e4b7fcdSDave Hansen 		__mnt_unmake_readonly(mnt);
15832e4b7fcdSDave Hansen 	return error;
15842e4b7fcdSDave Hansen }
15852e4b7fcdSDave Hansen 
15861da177e4SLinus Torvalds /*
15871da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
15881da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
15891da177e4SLinus Torvalds  * on it - tough luck.
15901da177e4SLinus Torvalds  */
15910a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags,
15921da177e4SLinus Torvalds 		      void *data)
15931da177e4SLinus Torvalds {
15941da177e4SLinus Torvalds 	int err;
15952d92ab3cSAl Viro 	struct super_block *sb = path->mnt->mnt_sb;
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
15981da177e4SLinus Torvalds 		return -EPERM;
15991da177e4SLinus Torvalds 
16002d92ab3cSAl Viro 	if (!check_mnt(path->mnt))
16011da177e4SLinus Torvalds 		return -EINVAL;
16021da177e4SLinus Torvalds 
16032d92ab3cSAl Viro 	if (path->dentry != path->mnt->mnt_root)
16041da177e4SLinus Torvalds 		return -EINVAL;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	down_write(&sb->s_umount);
16072e4b7fcdSDave Hansen 	if (flags & MS_BIND)
16082d92ab3cSAl Viro 		err = change_mount_flags(path->mnt, flags);
16094aa98cf7SAl Viro 	else
16101da177e4SLinus Torvalds 		err = do_remount_sb(sb, flags, data, 0);
16117b43a79fSAl Viro 	if (!err) {
161299b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
1613495d6c9cSValerie Aurora 		mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK;
16142d92ab3cSAl Viro 		path->mnt->mnt_flags = mnt_flags;
161599b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
16167b43a79fSAl Viro 	}
16171da177e4SLinus Torvalds 	up_write(&sb->s_umount);
16180e55a7ccSDan Williams 	if (!err) {
161999b7db7bSNick Piggin 		br_write_lock(vfsmount_lock);
16200e55a7ccSDan Williams 		touch_mnt_namespace(path->mnt->mnt_ns);
162199b7db7bSNick Piggin 		br_write_unlock(vfsmount_lock);
16220e55a7ccSDan Williams 	}
16231da177e4SLinus Torvalds 	return err;
16241da177e4SLinus Torvalds }
16251da177e4SLinus Torvalds 
16269676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt)
16279676f0c6SRam Pai {
16289676f0c6SRam Pai 	struct vfsmount *p;
16299676f0c6SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
16309676f0c6SRam Pai 		if (IS_MNT_UNBINDABLE(p))
16319676f0c6SRam Pai 			return 1;
16329676f0c6SRam Pai 	}
16339676f0c6SRam Pai 	return 0;
16349676f0c6SRam Pai }
16359676f0c6SRam Pai 
16360a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name)
16371da177e4SLinus Torvalds {
16382d92ab3cSAl Viro 	struct path old_path, parent_path;
16391da177e4SLinus Torvalds 	struct vfsmount *p;
16401da177e4SLinus Torvalds 	int err = 0;
16411da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
16421da177e4SLinus Torvalds 		return -EPERM;
16431da177e4SLinus Torvalds 	if (!old_name || !*old_name)
16441da177e4SLinus Torvalds 		return -EINVAL;
16452d92ab3cSAl Viro 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
16461da177e4SLinus Torvalds 	if (err)
16471da177e4SLinus Torvalds 		return err;
16481da177e4SLinus Torvalds 
1649390c6843SRam Pai 	down_write(&namespace_sem);
16502d92ab3cSAl Viro 	while (d_mountpoint(path->dentry) &&
16519393bd07SAl Viro 	       follow_down(path))
16521da177e4SLinus Torvalds 		;
16531da177e4SLinus Torvalds 	err = -EINVAL;
16542d92ab3cSAl Viro 	if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt))
16551da177e4SLinus Torvalds 		goto out;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	err = -ENOENT;
16582d92ab3cSAl Viro 	mutex_lock(&path->dentry->d_inode->i_mutex);
1659d83c49f3SAl Viro 	if (cant_mount(path->dentry))
16601da177e4SLinus Torvalds 		goto out1;
16611da177e4SLinus Torvalds 
1662f3da392eSAlexey Dobriyan 	if (d_unlinked(path->dentry))
166321444403SRam Pai 		goto out1;
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	err = -EINVAL;
16662d92ab3cSAl Viro 	if (old_path.dentry != old_path.mnt->mnt_root)
166721444403SRam Pai 		goto out1;
16681da177e4SLinus Torvalds 
16692d92ab3cSAl Viro 	if (old_path.mnt == old_path.mnt->mnt_parent)
167021444403SRam Pai 		goto out1;
16711da177e4SLinus Torvalds 
16722d92ab3cSAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
16732d92ab3cSAl Viro 	      S_ISDIR(old_path.dentry->d_inode->i_mode))
167421444403SRam Pai 		goto out1;
167521444403SRam Pai 	/*
167621444403SRam Pai 	 * Don't move a mount residing in a shared parent.
167721444403SRam Pai 	 */
16782d92ab3cSAl Viro 	if (old_path.mnt->mnt_parent &&
16792d92ab3cSAl Viro 	    IS_MNT_SHARED(old_path.mnt->mnt_parent))
168021444403SRam Pai 		goto out1;
16819676f0c6SRam Pai 	/*
16829676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
16839676f0c6SRam Pai 	 * mount which is shared.
16849676f0c6SRam Pai 	 */
16852d92ab3cSAl Viro 	if (IS_MNT_SHARED(path->mnt) &&
16862d92ab3cSAl Viro 	    tree_contains_unbindable(old_path.mnt))
16879676f0c6SRam Pai 		goto out1;
16881da177e4SLinus Torvalds 	err = -ELOOP;
16892d92ab3cSAl Viro 	for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent)
16902d92ab3cSAl Viro 		if (p == old_path.mnt)
169121444403SRam Pai 			goto out1;
16921da177e4SLinus Torvalds 
16932d92ab3cSAl Viro 	err = attach_recursive_mnt(old_path.mnt, path, &parent_path);
16944ac91378SJan Blunck 	if (err)
169521444403SRam Pai 		goto out1;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
16981da177e4SLinus Torvalds 	 * automatically */
16992d92ab3cSAl Viro 	list_del_init(&old_path.mnt->mnt_expire);
17001da177e4SLinus Torvalds out1:
17012d92ab3cSAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
17021da177e4SLinus Torvalds out:
1703390c6843SRam Pai 	up_write(&namespace_sem);
17041da177e4SLinus Torvalds 	if (!err)
17051a390689SAl Viro 		path_put(&parent_path);
17062d92ab3cSAl Viro 	path_put(&old_path);
17071da177e4SLinus Torvalds 	return err;
17081da177e4SLinus Torvalds }
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds /*
17111da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
17121da177e4SLinus Torvalds  * namespace's tree
17131da177e4SLinus Torvalds  */
17140a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags,
17151da177e4SLinus Torvalds 			int mnt_flags, char *name, void *data)
17161da177e4SLinus Torvalds {
17171da177e4SLinus Torvalds 	struct vfsmount *mnt;
17181da177e4SLinus Torvalds 
1719eca6f534SVegard Nossum 	if (!type)
17201da177e4SLinus Torvalds 		return -EINVAL;
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	/* we need capabilities... */
17231da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
17241da177e4SLinus Torvalds 		return -EPERM;
17251da177e4SLinus Torvalds 
17267f78d4cdSAl Viro 	lock_kernel();
17271da177e4SLinus Torvalds 	mnt = do_kern_mount(type, flags, name, data);
17287f78d4cdSAl Viro 	unlock_kernel();
17291da177e4SLinus Torvalds 	if (IS_ERR(mnt))
17301da177e4SLinus Torvalds 		return PTR_ERR(mnt);
17311da177e4SLinus Torvalds 
17322d92ab3cSAl Viro 	return do_add_mount(mnt, path, mnt_flags, NULL);
17331da177e4SLinus Torvalds }
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds /*
17361da177e4SLinus Torvalds  * add a mount into a namespace's mount tree
17371da177e4SLinus Torvalds  * - provide the option of adding the new mount to an expiration list
17381da177e4SLinus Torvalds  */
17398d66bf54SAl Viro int do_add_mount(struct vfsmount *newmnt, struct path *path,
17401da177e4SLinus Torvalds 		 int mnt_flags, struct list_head *fslist)
17411da177e4SLinus Torvalds {
17421da177e4SLinus Torvalds 	int err;
17431da177e4SLinus Torvalds 
17448089352aSAl Viro 	mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
174527d55f1fSAl Viro 
1746390c6843SRam Pai 	down_write(&namespace_sem);
17471da177e4SLinus Torvalds 	/* Something was mounted here while we slept */
17488d66bf54SAl Viro 	while (d_mountpoint(path->dentry) &&
17499393bd07SAl Viro 	       follow_down(path))
17501da177e4SLinus Torvalds 		;
17511da177e4SLinus Torvalds 	err = -EINVAL;
1752dd5cae6eSAl Viro 	if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt))
17531da177e4SLinus Torvalds 		goto unlock;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	/* Refuse the same filesystem on the same mount point */
17561da177e4SLinus Torvalds 	err = -EBUSY;
17578d66bf54SAl Viro 	if (path->mnt->mnt_sb == newmnt->mnt_sb &&
17588d66bf54SAl Viro 	    path->mnt->mnt_root == path->dentry)
17591da177e4SLinus Torvalds 		goto unlock;
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	err = -EINVAL;
17621da177e4SLinus Torvalds 	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
17631da177e4SLinus Torvalds 		goto unlock;
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 	newmnt->mnt_flags = mnt_flags;
17668d66bf54SAl Viro 	if ((err = graft_tree(newmnt, path)))
17675b83d2c5SRam Pai 		goto unlock;
17681da177e4SLinus Torvalds 
17696758f953SAl Viro 	if (fslist) /* add to the specified expiration list */
177055e700b9SMiklos Szeredi 		list_add_tail(&newmnt->mnt_expire, fslist);
17716758f953SAl Viro 
1772390c6843SRam Pai 	up_write(&namespace_sem);
17735b83d2c5SRam Pai 	return 0;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds unlock:
1776390c6843SRam Pai 	up_write(&namespace_sem);
17771da177e4SLinus Torvalds 	mntput(newmnt);
17781da177e4SLinus Torvalds 	return err;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount);
17821da177e4SLinus Torvalds 
17835528f911STrond Myklebust /*
17841da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
17851da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
17861da177e4SLinus Torvalds  * here
17871da177e4SLinus Torvalds  */
17881da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
17891da177e4SLinus Torvalds {
17901da177e4SLinus Torvalds 	struct vfsmount *mnt, *next;
17911da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
1792bcc5c7d2SAl Viro 	LIST_HEAD(umounts);
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	if (list_empty(mounts))
17951da177e4SLinus Torvalds 		return;
17961da177e4SLinus Torvalds 
1797bcc5c7d2SAl Viro 	down_write(&namespace_sem);
179899b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
18011da177e4SLinus Torvalds 	 * following criteria:
18021da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
18031da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
18041da177e4SLinus Torvalds 	 *   cleared by mntput())
18051da177e4SLinus Torvalds 	 */
180655e700b9SMiklos Szeredi 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
18071da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1808bcc5c7d2SAl Viro 			propagate_mount_busy(mnt, 1))
18091da177e4SLinus Torvalds 			continue;
181055e700b9SMiklos Szeredi 		list_move(&mnt->mnt_expire, &graveyard);
18111da177e4SLinus Torvalds 	}
1812bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
1813bcc5c7d2SAl Viro 		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
1814bcc5c7d2SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1815bcc5c7d2SAl Viro 		umount_tree(mnt, 1, &umounts);
1816bcc5c7d2SAl Viro 	}
181799b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
1818bcc5c7d2SAl Viro 	up_write(&namespace_sem);
1819bcc5c7d2SAl Viro 
1820bcc5c7d2SAl Viro 	release_mounts(&umounts);
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds /*
18265528f911STrond Myklebust  * Ripoff of 'select_parent()'
18275528f911STrond Myklebust  *
18285528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
18295528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
18305528f911STrond Myklebust  */
18315528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
18325528f911STrond Myklebust {
18335528f911STrond Myklebust 	struct vfsmount *this_parent = parent;
18345528f911STrond Myklebust 	struct list_head *next;
18355528f911STrond Myklebust 	int found = 0;
18365528f911STrond Myklebust 
18375528f911STrond Myklebust repeat:
18385528f911STrond Myklebust 	next = this_parent->mnt_mounts.next;
18395528f911STrond Myklebust resume:
18405528f911STrond Myklebust 	while (next != &this_parent->mnt_mounts) {
18415528f911STrond Myklebust 		struct list_head *tmp = next;
18425528f911STrond Myklebust 		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
18435528f911STrond Myklebust 
18445528f911STrond Myklebust 		next = tmp->next;
18455528f911STrond Myklebust 		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
18465528f911STrond Myklebust 			continue;
18475528f911STrond Myklebust 		/*
18485528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
18495528f911STrond Myklebust 		 */
18505528f911STrond Myklebust 		if (!list_empty(&mnt->mnt_mounts)) {
18515528f911STrond Myklebust 			this_parent = mnt;
18525528f911STrond Myklebust 			goto repeat;
18535528f911STrond Myklebust 		}
18545528f911STrond Myklebust 
18555528f911STrond Myklebust 		if (!propagate_mount_busy(mnt, 1)) {
18565528f911STrond Myklebust 			list_move_tail(&mnt->mnt_expire, graveyard);
18575528f911STrond Myklebust 			found++;
18585528f911STrond Myklebust 		}
18595528f911STrond Myklebust 	}
18605528f911STrond Myklebust 	/*
18615528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
18625528f911STrond Myklebust 	 */
18635528f911STrond Myklebust 	if (this_parent != parent) {
18645528f911STrond Myklebust 		next = this_parent->mnt_child.next;
18655528f911STrond Myklebust 		this_parent = this_parent->mnt_parent;
18665528f911STrond Myklebust 		goto resume;
18675528f911STrond Myklebust 	}
18685528f911STrond Myklebust 	return found;
18695528f911STrond Myklebust }
18705528f911STrond Myklebust 
18715528f911STrond Myklebust /*
18725528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
18735528f911STrond Myklebust  * submounts of a specific parent mountpoint
187499b7db7bSNick Piggin  *
187599b7db7bSNick Piggin  * vfsmount_lock must be held for write
18765528f911STrond Myklebust  */
1877c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
18785528f911STrond Myklebust {
18795528f911STrond Myklebust 	LIST_HEAD(graveyard);
1880c35038beSAl Viro 	struct vfsmount *m;
18815528f911STrond Myklebust 
18825528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
1883c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
1884bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
1885c35038beSAl Viro 			m = list_first_entry(&graveyard, struct vfsmount,
1886bcc5c7d2SAl Viro 						mnt_expire);
1887afef80b3SEric W. Biederman 			touch_mnt_namespace(m->mnt_ns);
1888afef80b3SEric W. Biederman 			umount_tree(m, 1, umounts);
1889bcc5c7d2SAl Viro 		}
1890bcc5c7d2SAl Viro 	}
18915528f911STrond Myklebust }
18925528f911STrond Myklebust 
18935528f911STrond Myklebust /*
18941da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
18951da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
18961da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
18971da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
18981da177e4SLinus Torvalds  */
1899b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
1900b58fed8bSRam Pai 				 unsigned long n)
19011da177e4SLinus Torvalds {
19021da177e4SLinus Torvalds 	char *t = to;
19031da177e4SLinus Torvalds 	const char __user *f = from;
19041da177e4SLinus Torvalds 	char c;
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
19071da177e4SLinus Torvalds 		return n;
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	while (n) {
19101da177e4SLinus Torvalds 		if (__get_user(c, f)) {
19111da177e4SLinus Torvalds 			memset(t, 0, n);
19121da177e4SLinus Torvalds 			break;
19131da177e4SLinus Torvalds 		}
19141da177e4SLinus Torvalds 		*t++ = c;
19151da177e4SLinus Torvalds 		f++;
19161da177e4SLinus Torvalds 		n--;
19171da177e4SLinus Torvalds 	}
19181da177e4SLinus Torvalds 	return n;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
19221da177e4SLinus Torvalds {
19231da177e4SLinus Torvalds 	int i;
19241da177e4SLinus Torvalds 	unsigned long page;
19251da177e4SLinus Torvalds 	unsigned long size;
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 	*where = 0;
19281da177e4SLinus Torvalds 	if (!data)
19291da177e4SLinus Torvalds 		return 0;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
19321da177e4SLinus Torvalds 		return -ENOMEM;
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
19351da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
19361da177e4SLinus Torvalds 	 * the remainder of the page.
19371da177e4SLinus Torvalds 	 */
19381da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
19391da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
19401da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
19411da177e4SLinus Torvalds 		size = PAGE_SIZE;
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
19441da177e4SLinus Torvalds 	if (!i) {
19451da177e4SLinus Torvalds 		free_page(page);
19461da177e4SLinus Torvalds 		return -EFAULT;
19471da177e4SLinus Torvalds 	}
19481da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
19491da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
19501da177e4SLinus Torvalds 	*where = page;
19511da177e4SLinus Torvalds 	return 0;
19521da177e4SLinus Torvalds }
19531da177e4SLinus Torvalds 
1954eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where)
1955eca6f534SVegard Nossum {
1956eca6f534SVegard Nossum 	char *tmp;
1957eca6f534SVegard Nossum 
1958eca6f534SVegard Nossum 	if (!data) {
1959eca6f534SVegard Nossum 		*where = NULL;
1960eca6f534SVegard Nossum 		return 0;
1961eca6f534SVegard Nossum 	}
1962eca6f534SVegard Nossum 
1963eca6f534SVegard Nossum 	tmp = strndup_user(data, PAGE_SIZE);
1964eca6f534SVegard Nossum 	if (IS_ERR(tmp))
1965eca6f534SVegard Nossum 		return PTR_ERR(tmp);
1966eca6f534SVegard Nossum 
1967eca6f534SVegard Nossum 	*where = tmp;
1968eca6f534SVegard Nossum 	return 0;
1969eca6f534SVegard Nossum }
1970eca6f534SVegard Nossum 
19711da177e4SLinus Torvalds /*
19721da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
19731da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
19741da177e4SLinus Torvalds  *
19751da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
19761da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
19771da177e4SLinus Torvalds  * information (or be NULL).
19781da177e4SLinus Torvalds  *
19791da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
19801da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
19811da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
19821da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
19831da177e4SLinus Torvalds  * and must be discarded.
19841da177e4SLinus Torvalds  */
19851da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page,
19861da177e4SLinus Torvalds 		  unsigned long flags, void *data_page)
19871da177e4SLinus Torvalds {
19882d92ab3cSAl Viro 	struct path path;
19891da177e4SLinus Torvalds 	int retval = 0;
19901da177e4SLinus Torvalds 	int mnt_flags = 0;
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	/* Discard magic */
19931da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
19941da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	/* Basic sanity checks */
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
19991da177e4SLinus Torvalds 		return -EINVAL;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	if (data_page)
20021da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
20031da177e4SLinus Torvalds 
2004a27ab9f2STetsuo Handa 	/* ... and get the mountpoint */
2005a27ab9f2STetsuo Handa 	retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2006a27ab9f2STetsuo Handa 	if (retval)
2007a27ab9f2STetsuo Handa 		return retval;
2008a27ab9f2STetsuo Handa 
2009a27ab9f2STetsuo Handa 	retval = security_sb_mount(dev_name, &path,
2010a27ab9f2STetsuo Handa 				   type_page, flags, data_page);
2011a27ab9f2STetsuo Handa 	if (retval)
2012a27ab9f2STetsuo Handa 		goto dput_out;
2013a27ab9f2STetsuo Handa 
2014613cbe3dSAndi Kleen 	/* Default to relatime unless overriden */
2015613cbe3dSAndi Kleen 	if (!(flags & MS_NOATIME))
20160a1c01c9SMatthew Garrett 		mnt_flags |= MNT_RELATIME;
20170a1c01c9SMatthew Garrett 
20181da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
20191da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
20201da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
20211da177e4SLinus Torvalds 	if (flags & MS_NODEV)
20221da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
20231da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
20241da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
2025fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
2026fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
2027fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
2028fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
2029d0adde57SMatthew Garrett 	if (flags & MS_STRICTATIME)
2030d0adde57SMatthew Garrett 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
20312e4b7fcdSDave Hansen 	if (flags & MS_RDONLY)
20322e4b7fcdSDave Hansen 		mnt_flags |= MNT_READONLY;
2033fc33a7bbSChristoph Hellwig 
20347a4dec53SAl Viro 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2035d0adde57SMatthew Garrett 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2036d0adde57SMatthew Garrett 		   MS_STRICTATIME);
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
20392d92ab3cSAl Viro 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
20401da177e4SLinus Torvalds 				    data_page);
20411da177e4SLinus Torvalds 	else if (flags & MS_BIND)
20422d92ab3cSAl Viro 		retval = do_loopback(&path, dev_name, flags & MS_REC);
20439676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
20442d92ab3cSAl Viro 		retval = do_change_type(&path, flags);
20451da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
20462d92ab3cSAl Viro 		retval = do_move_mount(&path, dev_name);
20471da177e4SLinus Torvalds 	else
20482d92ab3cSAl Viro 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
20491da177e4SLinus Torvalds 				      dev_name, data_page);
20501da177e4SLinus Torvalds dput_out:
20512d92ab3cSAl Viro 	path_put(&path);
20521da177e4SLinus Torvalds 	return retval;
20531da177e4SLinus Torvalds }
20541da177e4SLinus Torvalds 
2055cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void)
2056cf8d2c11STrond Myklebust {
2057cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2058cf8d2c11STrond Myklebust 
2059cf8d2c11STrond Myklebust 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2060cf8d2c11STrond Myklebust 	if (!new_ns)
2061cf8d2c11STrond Myklebust 		return ERR_PTR(-ENOMEM);
2062cf8d2c11STrond Myklebust 	atomic_set(&new_ns->count, 1);
2063cf8d2c11STrond Myklebust 	new_ns->root = NULL;
2064cf8d2c11STrond Myklebust 	INIT_LIST_HEAD(&new_ns->list);
2065cf8d2c11STrond Myklebust 	init_waitqueue_head(&new_ns->poll);
2066cf8d2c11STrond Myklebust 	new_ns->event = 0;
2067cf8d2c11STrond Myklebust 	return new_ns;
2068cf8d2c11STrond Myklebust }
2069cf8d2c11STrond Myklebust 
2070741a2951SJANAK DESAI /*
2071741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
2072741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
2073741a2951SJANAK DESAI  */
2074e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
20756b3286edSKirill Korotaev 		struct fs_struct *fs)
20761da177e4SLinus Torvalds {
20776b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
20787f2da1e7SAl Viro 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
20791da177e4SLinus Torvalds 	struct vfsmount *p, *q;
20801da177e4SLinus Torvalds 
2081cf8d2c11STrond Myklebust 	new_ns = alloc_mnt_ns();
2082cf8d2c11STrond Myklebust 	if (IS_ERR(new_ns))
2083cf8d2c11STrond Myklebust 		return new_ns;
20841da177e4SLinus Torvalds 
2085390c6843SRam Pai 	down_write(&namespace_sem);
20861da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
20876b3286edSKirill Korotaev 	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
20889676f0c6SRam Pai 					CL_COPY_ALL | CL_EXPIRE);
20891da177e4SLinus Torvalds 	if (!new_ns->root) {
2090390c6843SRam Pai 		up_write(&namespace_sem);
20911da177e4SLinus Torvalds 		kfree(new_ns);
20925cc4a034SJulia Lawall 		return ERR_PTR(-ENOMEM);
20931da177e4SLinus Torvalds 	}
209499b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
20951da177e4SLinus Torvalds 	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
209699b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	/*
20991da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
21001da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
21011da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
21021da177e4SLinus Torvalds 	 */
21036b3286edSKirill Korotaev 	p = mnt_ns->root;
21041da177e4SLinus Torvalds 	q = new_ns->root;
21051da177e4SLinus Torvalds 	while (p) {
21066b3286edSKirill Korotaev 		q->mnt_ns = new_ns;
21071da177e4SLinus Torvalds 		if (fs) {
21086ac08c39SJan Blunck 			if (p == fs->root.mnt) {
21091da177e4SLinus Torvalds 				rootmnt = p;
21106ac08c39SJan Blunck 				fs->root.mnt = mntget(q);
21111da177e4SLinus Torvalds 			}
21126ac08c39SJan Blunck 			if (p == fs->pwd.mnt) {
21131da177e4SLinus Torvalds 				pwdmnt = p;
21146ac08c39SJan Blunck 				fs->pwd.mnt = mntget(q);
21151da177e4SLinus Torvalds 			}
21161da177e4SLinus Torvalds 		}
21176b3286edSKirill Korotaev 		p = next_mnt(p, mnt_ns->root);
21181da177e4SLinus Torvalds 		q = next_mnt(q, new_ns->root);
21191da177e4SLinus Torvalds 	}
2120390c6843SRam Pai 	up_write(&namespace_sem);
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 	if (rootmnt)
21231da177e4SLinus Torvalds 		mntput(rootmnt);
21241da177e4SLinus Torvalds 	if (pwdmnt)
21251da177e4SLinus Torvalds 		mntput(pwdmnt);
21261da177e4SLinus Torvalds 
2127741a2951SJANAK DESAI 	return new_ns;
2128741a2951SJANAK DESAI }
2129741a2951SJANAK DESAI 
2130213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2131e3222c4eSBadari Pulavarty 		struct fs_struct *new_fs)
2132741a2951SJANAK DESAI {
21336b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
2134741a2951SJANAK DESAI 
2135e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
21366b3286edSKirill Korotaev 	get_mnt_ns(ns);
2137741a2951SJANAK DESAI 
2138741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
2139e3222c4eSBadari Pulavarty 		return ns;
2140741a2951SJANAK DESAI 
2141e3222c4eSBadari Pulavarty 	new_ns = dup_mnt_ns(ns, new_fs);
2142741a2951SJANAK DESAI 
21436b3286edSKirill Korotaev 	put_mnt_ns(ns);
2144e3222c4eSBadari Pulavarty 	return new_ns;
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
2147cf8d2c11STrond Myklebust /**
2148cf8d2c11STrond Myklebust  * create_mnt_ns - creates a private namespace and adds a root filesystem
2149cf8d2c11STrond Myklebust  * @mnt: pointer to the new root filesystem mountpoint
2150cf8d2c11STrond Myklebust  */
2151a2770d86SLinus Torvalds struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
2152cf8d2c11STrond Myklebust {
2153cf8d2c11STrond Myklebust 	struct mnt_namespace *new_ns;
2154cf8d2c11STrond Myklebust 
2155cf8d2c11STrond Myklebust 	new_ns = alloc_mnt_ns();
2156cf8d2c11STrond Myklebust 	if (!IS_ERR(new_ns)) {
2157cf8d2c11STrond Myklebust 		mnt->mnt_ns = new_ns;
2158cf8d2c11STrond Myklebust 		new_ns->root = mnt;
2159cf8d2c11STrond Myklebust 		list_add(&new_ns->list, &new_ns->root->mnt_list);
2160cf8d2c11STrond Myklebust 	}
2161cf8d2c11STrond Myklebust 	return new_ns;
2162cf8d2c11STrond Myklebust }
2163a2770d86SLinus Torvalds EXPORT_SYMBOL(create_mnt_ns);
2164cf8d2c11STrond Myklebust 
2165bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2166bdc480e3SHeiko Carstens 		char __user *, type, unsigned long, flags, void __user *, data)
21671da177e4SLinus Torvalds {
2168eca6f534SVegard Nossum 	int ret;
2169eca6f534SVegard Nossum 	char *kernel_type;
2170eca6f534SVegard Nossum 	char *kernel_dir;
2171eca6f534SVegard Nossum 	char *kernel_dev;
21721da177e4SLinus Torvalds 	unsigned long data_page;
21731da177e4SLinus Torvalds 
2174eca6f534SVegard Nossum 	ret = copy_mount_string(type, &kernel_type);
2175eca6f534SVegard Nossum 	if (ret < 0)
2176eca6f534SVegard Nossum 		goto out_type;
21771da177e4SLinus Torvalds 
2178eca6f534SVegard Nossum 	kernel_dir = getname(dir_name);
2179eca6f534SVegard Nossum 	if (IS_ERR(kernel_dir)) {
2180eca6f534SVegard Nossum 		ret = PTR_ERR(kernel_dir);
2181eca6f534SVegard Nossum 		goto out_dir;
2182eca6f534SVegard Nossum 	}
21831da177e4SLinus Torvalds 
2184eca6f534SVegard Nossum 	ret = copy_mount_string(dev_name, &kernel_dev);
2185eca6f534SVegard Nossum 	if (ret < 0)
2186eca6f534SVegard Nossum 		goto out_dev;
21871da177e4SLinus Torvalds 
2188eca6f534SVegard Nossum 	ret = copy_mount_options(data, &data_page);
2189eca6f534SVegard Nossum 	if (ret < 0)
2190eca6f534SVegard Nossum 		goto out_data;
21911da177e4SLinus Torvalds 
2192eca6f534SVegard Nossum 	ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
2193eca6f534SVegard Nossum 		(void *) data_page);
2194eca6f534SVegard Nossum 
21951da177e4SLinus Torvalds 	free_page(data_page);
2196eca6f534SVegard Nossum out_data:
2197eca6f534SVegard Nossum 	kfree(kernel_dev);
2198eca6f534SVegard Nossum out_dev:
2199eca6f534SVegard Nossum 	putname(kernel_dir);
2200eca6f534SVegard Nossum out_dir:
2201eca6f534SVegard Nossum 	kfree(kernel_type);
2202eca6f534SVegard Nossum out_type:
2203eca6f534SVegard Nossum 	return ret;
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds /*
22071da177e4SLinus Torvalds  * pivot_root Semantics:
22081da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
22091da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
22101da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
22111da177e4SLinus Torvalds  *
22121da177e4SLinus Torvalds  * Restrictions:
22131da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
22141da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
22151da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
22161da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
22171da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
22181da177e4SLinus Torvalds  *
22194a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
22204a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
22214a0d11faSNeil Brown  * in this situation.
22224a0d11faSNeil Brown  *
22231da177e4SLinus Torvalds  * Notes:
22241da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
22251da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
22261da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
22271da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
22281da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
22291da177e4SLinus Torvalds  *    first.
22301da177e4SLinus Torvalds  */
22313480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
22323480b257SHeiko Carstens 		const char __user *, put_old)
22331da177e4SLinus Torvalds {
22341da177e4SLinus Torvalds 	struct vfsmount *tmp;
22352d8f3038SAl Viro 	struct path new, old, parent_path, root_parent, root;
22361da177e4SLinus Torvalds 	int error;
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
22391da177e4SLinus Torvalds 		return -EPERM;
22401da177e4SLinus Torvalds 
22412d8f3038SAl Viro 	error = user_path_dir(new_root, &new);
22421da177e4SLinus Torvalds 	if (error)
22431da177e4SLinus Torvalds 		goto out0;
22441da177e4SLinus Torvalds 	error = -EINVAL;
22452d8f3038SAl Viro 	if (!check_mnt(new.mnt))
22461da177e4SLinus Torvalds 		goto out1;
22471da177e4SLinus Torvalds 
22482d8f3038SAl Viro 	error = user_path_dir(put_old, &old);
22491da177e4SLinus Torvalds 	if (error)
22501da177e4SLinus Torvalds 		goto out1;
22511da177e4SLinus Torvalds 
22522d8f3038SAl Viro 	error = security_sb_pivotroot(&old, &new);
22531da177e4SLinus Torvalds 	if (error) {
22542d8f3038SAl Viro 		path_put(&old);
22551da177e4SLinus Torvalds 		goto out1;
22561da177e4SLinus Torvalds 	}
22571da177e4SLinus Torvalds 
2258f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &root);
2259390c6843SRam Pai 	down_write(&namespace_sem);
22602d8f3038SAl Viro 	mutex_lock(&old.dentry->d_inode->i_mutex);
22611da177e4SLinus Torvalds 	error = -EINVAL;
22622d8f3038SAl Viro 	if (IS_MNT_SHARED(old.mnt) ||
22632d8f3038SAl Viro 		IS_MNT_SHARED(new.mnt->mnt_parent) ||
22648c3ee42eSAl Viro 		IS_MNT_SHARED(root.mnt->mnt_parent))
226521444403SRam Pai 		goto out2;
22668c3ee42eSAl Viro 	if (!check_mnt(root.mnt))
22671da177e4SLinus Torvalds 		goto out2;
22681da177e4SLinus Torvalds 	error = -ENOENT;
2269d83c49f3SAl Viro 	if (cant_mount(old.dentry))
22701da177e4SLinus Torvalds 		goto out2;
2271f3da392eSAlexey Dobriyan 	if (d_unlinked(new.dentry))
22721da177e4SLinus Torvalds 		goto out2;
2273f3da392eSAlexey Dobriyan 	if (d_unlinked(old.dentry))
22741da177e4SLinus Torvalds 		goto out2;
22751da177e4SLinus Torvalds 	error = -EBUSY;
22762d8f3038SAl Viro 	if (new.mnt == root.mnt ||
22772d8f3038SAl Viro 	    old.mnt == root.mnt)
22781da177e4SLinus Torvalds 		goto out2; /* loop, on the same file system  */
22791da177e4SLinus Torvalds 	error = -EINVAL;
22808c3ee42eSAl Viro 	if (root.mnt->mnt_root != root.dentry)
22811da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
22828c3ee42eSAl Viro 	if (root.mnt->mnt_parent == root.mnt)
22830bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
22842d8f3038SAl Viro 	if (new.mnt->mnt_root != new.dentry)
22851da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
22862d8f3038SAl Viro 	if (new.mnt->mnt_parent == new.mnt)
22870bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
22884ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
22892d8f3038SAl Viro 	tmp = old.mnt;
229099b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
22912d8f3038SAl Viro 	if (tmp != new.mnt) {
22921da177e4SLinus Torvalds 		for (;;) {
22931da177e4SLinus Torvalds 			if (tmp->mnt_parent == tmp)
22941da177e4SLinus Torvalds 				goto out3; /* already mounted on put_old */
22952d8f3038SAl Viro 			if (tmp->mnt_parent == new.mnt)
22961da177e4SLinus Torvalds 				break;
22971da177e4SLinus Torvalds 			tmp = tmp->mnt_parent;
22981da177e4SLinus Torvalds 		}
22992d8f3038SAl Viro 		if (!is_subdir(tmp->mnt_mountpoint, new.dentry))
23001da177e4SLinus Torvalds 			goto out3;
23012d8f3038SAl Viro 	} else if (!is_subdir(old.dentry, new.dentry))
23021da177e4SLinus Torvalds 		goto out3;
23032d8f3038SAl Viro 	detach_mnt(new.mnt, &parent_path);
23048c3ee42eSAl Viro 	detach_mnt(root.mnt, &root_parent);
23054ac91378SJan Blunck 	/* mount old root on put_old */
23062d8f3038SAl Viro 	attach_mnt(root.mnt, &old);
23074ac91378SJan Blunck 	/* mount new_root on / */
23082d8f3038SAl Viro 	attach_mnt(new.mnt, &root_parent);
23096b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
231099b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
23112d8f3038SAl Viro 	chroot_fs_refs(&root, &new);
23121da177e4SLinus Torvalds 	error = 0;
23131a390689SAl Viro 	path_put(&root_parent);
23141a390689SAl Viro 	path_put(&parent_path);
23151da177e4SLinus Torvalds out2:
23162d8f3038SAl Viro 	mutex_unlock(&old.dentry->d_inode->i_mutex);
2317390c6843SRam Pai 	up_write(&namespace_sem);
23188c3ee42eSAl Viro 	path_put(&root);
23192d8f3038SAl Viro 	path_put(&old);
23201da177e4SLinus Torvalds out1:
23212d8f3038SAl Viro 	path_put(&new);
23221da177e4SLinus Torvalds out0:
23231da177e4SLinus Torvalds 	return error;
23241da177e4SLinus Torvalds out3:
232599b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
23261da177e4SLinus Torvalds 	goto out2;
23271da177e4SLinus Torvalds }
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds static void __init init_mount_tree(void)
23301da177e4SLinus Torvalds {
23311da177e4SLinus Torvalds 	struct vfsmount *mnt;
23326b3286edSKirill Korotaev 	struct mnt_namespace *ns;
2333ac748a09SJan Blunck 	struct path root;
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
23361da177e4SLinus Torvalds 	if (IS_ERR(mnt))
23371da177e4SLinus Torvalds 		panic("Can't create rootfs");
23383b22edc5STrond Myklebust 	ns = create_mnt_ns(mnt);
23393b22edc5STrond Myklebust 	if (IS_ERR(ns))
23401da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
23411da177e4SLinus Torvalds 
23426b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
23436b3286edSKirill Korotaev 	get_mnt_ns(ns);
23441da177e4SLinus Torvalds 
2345ac748a09SJan Blunck 	root.mnt = ns->root;
2346ac748a09SJan Blunck 	root.dentry = ns->root->mnt_root;
2347ac748a09SJan Blunck 
2348ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
2349ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
23501da177e4SLinus Torvalds }
23511da177e4SLinus Torvalds 
235274bf17cfSDenis Cheng void __init mnt_init(void)
23531da177e4SLinus Torvalds {
235413f14b4dSEric Dumazet 	unsigned u;
235515a67dd8SRandy Dunlap 	int err;
23561da177e4SLinus Torvalds 
2357390c6843SRam Pai 	init_rwsem(&namespace_sem);
2358390c6843SRam Pai 
23591da177e4SLinus Torvalds 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
236020c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
23611da177e4SLinus Torvalds 
2362b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 	if (!mount_hashtable)
23651da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
23661da177e4SLinus Torvalds 
236713f14b4dSEric Dumazet 	printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
23681da177e4SLinus Torvalds 
236913f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
237013f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
23711da177e4SLinus Torvalds 
237299b7db7bSNick Piggin 	br_lock_init(vfsmount_lock);
237399b7db7bSNick Piggin 
237415a67dd8SRandy Dunlap 	err = sysfs_init();
237515a67dd8SRandy Dunlap 	if (err)
237615a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
23778e24eea7SHarvey Harrison 			__func__, err);
237800d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
237900d26666SGreg Kroah-Hartman 	if (!fs_kobj)
23808e24eea7SHarvey Harrison 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
23811da177e4SLinus Torvalds 	init_rootfs();
23821da177e4SLinus Torvalds 	init_mount_tree();
23831da177e4SLinus Torvalds }
23841da177e4SLinus Torvalds 
2385616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns)
23861da177e4SLinus Torvalds {
238770fbcdf4SRam Pai 	LIST_HEAD(umount_list);
2388616511d0STrond Myklebust 
2389d498b25aSAl Viro 	if (!atomic_dec_and_test(&ns->count))
2390616511d0STrond Myklebust 		return;
2391390c6843SRam Pai 	down_write(&namespace_sem);
239299b7db7bSNick Piggin 	br_write_lock(vfsmount_lock);
2393d498b25aSAl Viro 	umount_tree(ns->root, 0, &umount_list);
239499b7db7bSNick Piggin 	br_write_unlock(vfsmount_lock);
2395390c6843SRam Pai 	up_write(&namespace_sem);
239670fbcdf4SRam Pai 	release_mounts(&umount_list);
23976b3286edSKirill Korotaev 	kfree(ns);
23981da177e4SLinus Torvalds }
2399cf8d2c11STrond Myklebust EXPORT_SYMBOL(put_mnt_ns);
2400