xref: /openbmc/linux/fs/namespace.c (revision 8366025e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namespace.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright Al Viro 2000, 2001
51da177e4SLinus Torvalds  *	Released under GPL v2.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Based on code from fs/super.c, copyright Linus Torvalds and others.
81da177e4SLinus Torvalds  * Heavily rewritten.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/syscalls.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/sched.h>
141da177e4SLinus Torvalds #include <linux/smp_lock.h>
151da177e4SLinus Torvalds #include <linux/init.h>
1615a67dd8SRandy Dunlap #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/quotaops.h>
181da177e4SLinus Torvalds #include <linux/acct.h>
1916f7e0feSRandy Dunlap #include <linux/capability.h>
201da177e4SLinus Torvalds #include <linux/module.h>
21f20a9eadSAndrew Morton #include <linux/sysfs.h>
221da177e4SLinus Torvalds #include <linux/seq_file.h>
236b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
241da177e4SLinus Torvalds #include <linux/namei.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <linux/mount.h>
2707f3f05cSDavid Howells #include <linux/ramfs.h>
2813f14b4dSEric Dumazet #include <linux/log2.h>
291da177e4SLinus Torvalds #include <asm/uaccess.h>
301da177e4SLinus Torvalds #include <asm/unistd.h>
3107b20889SRam Pai #include "pnode.h"
32948730b0SAdrian Bunk #include "internal.h"
331da177e4SLinus Torvalds 
3413f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
3513f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT)
3613f14b4dSEric Dumazet 
371da177e4SLinus Torvalds /* spinlock for vfsmount related operations, inplace of dcache_lock */
381da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
391da177e4SLinus Torvalds 
405addc5ddSAl Viro static int event;
415addc5ddSAl Viro 
42fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly;
43e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly;
44390c6843SRam Pai static struct rw_semaphore namespace_sem;
451da177e4SLinus Torvalds 
46f87fd4c2SMiklos Szeredi /* /sys/fs */
4700d26666SGreg Kroah-Hartman struct kobject *fs_kobj;
4800d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj);
49f87fd4c2SMiklos Szeredi 
501da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
531da177e4SLinus Torvalds 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
5413f14b4dSEric Dumazet 	tmp = tmp + (tmp >> HASH_SHIFT);
5513f14b4dSEric Dumazet 	return tmp & (HASH_SIZE - 1);
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds struct vfsmount *alloc_vfsmnt(const char *name)
591da177e4SLinus Torvalds {
60c3762229SRobert P. J. Day 	struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
611da177e4SLinus Torvalds 	if (mnt) {
621da177e4SLinus Torvalds 		atomic_set(&mnt->mnt_count, 1);
631da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_hash);
641da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_child);
651da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_mounts);
661da177e4SLinus Torvalds 		INIT_LIST_HEAD(&mnt->mnt_list);
6755e700b9SMiklos Szeredi 		INIT_LIST_HEAD(&mnt->mnt_expire);
6803e06e68SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_share);
69a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
70a58b0eb8SRam Pai 		INIT_LIST_HEAD(&mnt->mnt_slave);
711da177e4SLinus Torvalds 		if (name) {
721da177e4SLinus Torvalds 			int size = strlen(name) + 1;
731da177e4SLinus Torvalds 			char *newname = kmalloc(size, GFP_KERNEL);
741da177e4SLinus Torvalds 			if (newname) {
751da177e4SLinus Torvalds 				memcpy(newname, name, size);
761da177e4SLinus Torvalds 				mnt->mnt_devname = newname;
771da177e4SLinus Torvalds 			}
781da177e4SLinus Torvalds 		}
791da177e4SLinus Torvalds 	}
801da177e4SLinus Torvalds 	return mnt;
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
838366025eSDave Hansen /*
848366025eSDave Hansen  * Most r/o checks on a fs are for operations that take
858366025eSDave Hansen  * discrete amounts of time, like a write() or unlink().
868366025eSDave Hansen  * We must keep track of when those operations start
878366025eSDave Hansen  * (for permission checks) and when they end, so that
888366025eSDave Hansen  * we can determine when writes are able to occur to
898366025eSDave Hansen  * a filesystem.
908366025eSDave Hansen  */
918366025eSDave Hansen /**
928366025eSDave Hansen  * mnt_want_write - get write access to a mount
938366025eSDave Hansen  * @mnt: the mount on which to take a write
948366025eSDave Hansen  *
958366025eSDave Hansen  * This tells the low-level filesystem that a write is
968366025eSDave Hansen  * about to be performed to it, and makes sure that
978366025eSDave Hansen  * writes are allowed before returning success.  When
988366025eSDave Hansen  * the write operation is finished, mnt_drop_write()
998366025eSDave Hansen  * must be called.  This is effectively a refcount.
1008366025eSDave Hansen  */
1018366025eSDave Hansen int mnt_want_write(struct vfsmount *mnt)
1028366025eSDave Hansen {
1038366025eSDave Hansen 	if (__mnt_is_readonly(mnt))
1048366025eSDave Hansen 		return -EROFS;
1058366025eSDave Hansen 	return 0;
1068366025eSDave Hansen }
1078366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write);
1088366025eSDave Hansen 
1098366025eSDave Hansen /**
1108366025eSDave Hansen  * mnt_drop_write - give up write access to a mount
1118366025eSDave Hansen  * @mnt: the mount on which to give up write access
1128366025eSDave Hansen  *
1138366025eSDave Hansen  * Tells the low-level filesystem that we are done
1148366025eSDave Hansen  * performing writes to it.  Must be matched with
1158366025eSDave Hansen  * mnt_want_write() call above.
1168366025eSDave Hansen  */
1178366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt)
1188366025eSDave Hansen {
1198366025eSDave Hansen }
1208366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write);
1218366025eSDave Hansen 
1228366025eSDave Hansen /*
1238366025eSDave Hansen  * __mnt_is_readonly: check whether a mount is read-only
1248366025eSDave Hansen  * @mnt: the mount to check for its write status
1258366025eSDave Hansen  *
1268366025eSDave Hansen  * This shouldn't be used directly ouside of the VFS.
1278366025eSDave Hansen  * It does not guarantee that the filesystem will stay
1288366025eSDave Hansen  * r/w, just that it is right *now*.  This can not and
1298366025eSDave Hansen  * should not be used in place of IS_RDONLY(inode).
1308366025eSDave Hansen  */
1318366025eSDave Hansen int __mnt_is_readonly(struct vfsmount *mnt)
1328366025eSDave Hansen {
1338366025eSDave Hansen 	return (mnt->mnt_sb->s_flags & MS_RDONLY);
1348366025eSDave Hansen }
1358366025eSDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly);
1368366025eSDave Hansen 
137454e2398SDavid Howells int simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
138454e2398SDavid Howells {
139454e2398SDavid Howells 	mnt->mnt_sb = sb;
140454e2398SDavid Howells 	mnt->mnt_root = dget(sb->s_root);
141454e2398SDavid Howells 	return 0;
142454e2398SDavid Howells }
143454e2398SDavid Howells 
144454e2398SDavid Howells EXPORT_SYMBOL(simple_set_mnt);
145454e2398SDavid Howells 
1461da177e4SLinus Torvalds void free_vfsmnt(struct vfsmount *mnt)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	kfree(mnt->mnt_devname);
1491da177e4SLinus Torvalds 	kmem_cache_free(mnt_cache, mnt);
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds /*
153a05964f3SRam Pai  * find the first or last mount at @dentry on vfsmount @mnt depending on
154a05964f3SRam Pai  * @dir. If @dir is set return the first mount else return the last mount.
1551da177e4SLinus Torvalds  */
156a05964f3SRam Pai struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
157a05964f3SRam Pai 			      int dir)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
1601da177e4SLinus Torvalds 	struct list_head *tmp = head;
1611da177e4SLinus Torvalds 	struct vfsmount *p, *found = NULL;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	for (;;) {
164a05964f3SRam Pai 		tmp = dir ? tmp->next : tmp->prev;
1651da177e4SLinus Torvalds 		p = NULL;
1661da177e4SLinus Torvalds 		if (tmp == head)
1671da177e4SLinus Torvalds 			break;
1681da177e4SLinus Torvalds 		p = list_entry(tmp, struct vfsmount, mnt_hash);
1691da177e4SLinus Torvalds 		if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
170a05964f3SRam Pai 			found = p;
1711da177e4SLinus Torvalds 			break;
1721da177e4SLinus Torvalds 		}
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 	return found;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
177a05964f3SRam Pai /*
178a05964f3SRam Pai  * lookup_mnt increments the ref count before returning
179a05964f3SRam Pai  * the vfsmount struct.
180a05964f3SRam Pai  */
181a05964f3SRam Pai struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
182a05964f3SRam Pai {
183a05964f3SRam Pai 	struct vfsmount *child_mnt;
184a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
185a05964f3SRam Pai 	if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
186a05964f3SRam Pai 		mntget(child_mnt);
187a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
188a05964f3SRam Pai 	return child_mnt;
189a05964f3SRam Pai }
190a05964f3SRam Pai 
1911da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt)
1921da177e4SLinus Torvalds {
1936b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1966b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
1975addc5ddSAl Viro {
1985addc5ddSAl Viro 	if (ns) {
1995addc5ddSAl Viro 		ns->event = ++event;
2005addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
2015addc5ddSAl Viro 	}
2025addc5ddSAl Viro }
2035addc5ddSAl Viro 
2046b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
2055addc5ddSAl Viro {
2065addc5ddSAl Viro 	if (ns && ns->event != event) {
2075addc5ddSAl Viro 		ns->event = event;
2085addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
2095addc5ddSAl Viro 	}
2105addc5ddSAl Viro }
2115addc5ddSAl Viro 
2121a390689SAl Viro static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
2131da177e4SLinus Torvalds {
2141a390689SAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
2151a390689SAl Viro 	old_path->mnt = mnt->mnt_parent;
2161da177e4SLinus Torvalds 	mnt->mnt_parent = mnt;
2171da177e4SLinus Torvalds 	mnt->mnt_mountpoint = mnt->mnt_root;
2181da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_child);
2191da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_hash);
2201a390689SAl Viro 	old_path->dentry->d_mounted--;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
223b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
224b90fa9aeSRam Pai 			struct vfsmount *child_mnt)
225b90fa9aeSRam Pai {
226b90fa9aeSRam Pai 	child_mnt->mnt_parent = mntget(mnt);
227b90fa9aeSRam Pai 	child_mnt->mnt_mountpoint = dget(dentry);
228b90fa9aeSRam Pai 	dentry->d_mounted++;
229b90fa9aeSRam Pai }
230b90fa9aeSRam Pai 
2311a390689SAl Viro static void attach_mnt(struct vfsmount *mnt, struct path *path)
2321da177e4SLinus Torvalds {
2331a390689SAl Viro 	mnt_set_mountpoint(path->mnt, path->dentry, mnt);
234b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
2351a390689SAl Viro 			hash(path->mnt, path->dentry));
2361a390689SAl Viro 	list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
237b90fa9aeSRam Pai }
238b90fa9aeSRam Pai 
239b90fa9aeSRam Pai /*
240b90fa9aeSRam Pai  * the caller must hold vfsmount_lock
241b90fa9aeSRam Pai  */
242b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt)
243b90fa9aeSRam Pai {
244b90fa9aeSRam Pai 	struct vfsmount *parent = mnt->mnt_parent;
245b90fa9aeSRam Pai 	struct vfsmount *m;
246b90fa9aeSRam Pai 	LIST_HEAD(head);
2476b3286edSKirill Korotaev 	struct mnt_namespace *n = parent->mnt_ns;
248b90fa9aeSRam Pai 
249b90fa9aeSRam Pai 	BUG_ON(parent == mnt);
250b90fa9aeSRam Pai 
251b90fa9aeSRam Pai 	list_add_tail(&head, &mnt->mnt_list);
252b90fa9aeSRam Pai 	list_for_each_entry(m, &head, mnt_list)
2536b3286edSKirill Korotaev 		m->mnt_ns = n;
254b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
255b90fa9aeSRam Pai 
256b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
257b90fa9aeSRam Pai 				hash(parent, mnt->mnt_mountpoint));
258b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
2596b3286edSKirill Korotaev 	touch_mnt_namespace(n);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	struct list_head *next = p->mnt_mounts.next;
2651da177e4SLinus Torvalds 	if (next == &p->mnt_mounts) {
2661da177e4SLinus Torvalds 		while (1) {
2671da177e4SLinus Torvalds 			if (p == root)
2681da177e4SLinus Torvalds 				return NULL;
2691da177e4SLinus Torvalds 			next = p->mnt_child.next;
2701da177e4SLinus Torvalds 			if (next != &p->mnt_parent->mnt_mounts)
2711da177e4SLinus Torvalds 				break;
2721da177e4SLinus Torvalds 			p = p->mnt_parent;
2731da177e4SLinus Torvalds 		}
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	return list_entry(next, struct vfsmount, mnt_child);
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2789676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
2799676f0c6SRam Pai {
2809676f0c6SRam Pai 	struct list_head *prev = p->mnt_mounts.prev;
2819676f0c6SRam Pai 	while (prev != &p->mnt_mounts) {
2829676f0c6SRam Pai 		p = list_entry(prev, struct vfsmount, mnt_child);
2839676f0c6SRam Pai 		prev = p->mnt_mounts.prev;
2849676f0c6SRam Pai 	}
2859676f0c6SRam Pai 	return p;
2869676f0c6SRam Pai }
2879676f0c6SRam Pai 
28836341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
28936341f64SRam Pai 					int flag)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct super_block *sb = old->mnt_sb;
2921da177e4SLinus Torvalds 	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (mnt) {
2951da177e4SLinus Torvalds 		mnt->mnt_flags = old->mnt_flags;
2961da177e4SLinus Torvalds 		atomic_inc(&sb->s_active);
2971da177e4SLinus Torvalds 		mnt->mnt_sb = sb;
2981da177e4SLinus Torvalds 		mnt->mnt_root = dget(root);
2991da177e4SLinus Torvalds 		mnt->mnt_mountpoint = mnt->mnt_root;
3001da177e4SLinus Torvalds 		mnt->mnt_parent = mnt;
301b90fa9aeSRam Pai 
3025afe0022SRam Pai 		if (flag & CL_SLAVE) {
3035afe0022SRam Pai 			list_add(&mnt->mnt_slave, &old->mnt_slave_list);
3045afe0022SRam Pai 			mnt->mnt_master = old;
3055afe0022SRam Pai 			CLEAR_MNT_SHARED(mnt);
3068aec0809SAl Viro 		} else if (!(flag & CL_PRIVATE)) {
307b90fa9aeSRam Pai 			if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
308b90fa9aeSRam Pai 				list_add(&mnt->mnt_share, &old->mnt_share);
3095afe0022SRam Pai 			if (IS_MNT_SLAVE(old))
3105afe0022SRam Pai 				list_add(&mnt->mnt_slave, &old->mnt_slave);
3115afe0022SRam Pai 			mnt->mnt_master = old->mnt_master;
3125afe0022SRam Pai 		}
313b90fa9aeSRam Pai 		if (flag & CL_MAKE_SHARED)
314b90fa9aeSRam Pai 			set_mnt_shared(mnt);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 		/* stick the duplicate mount on the same expiry list
3171da177e4SLinus Torvalds 		 * as the original if that was on one */
31836341f64SRam Pai 		if (flag & CL_EXPIRE) {
31955e700b9SMiklos Szeredi 			if (!list_empty(&old->mnt_expire))
32055e700b9SMiklos Szeredi 				list_add(&mnt->mnt_expire, &old->mnt_expire);
3211da177e4SLinus Torvalds 		}
32236341f64SRam Pai 	}
3231da177e4SLinus Torvalds 	return mnt;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
3267b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
3291da177e4SLinus Torvalds 	dput(mnt->mnt_root);
3301da177e4SLinus Torvalds 	free_vfsmnt(mnt);
3311da177e4SLinus Torvalds 	deactivate_super(sb);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3347b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt)
3357b7b1aceSAl Viro {
3367b7b1aceSAl Viro repeat:
3377b7b1aceSAl Viro 	if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
3387b7b1aceSAl Viro 		if (likely(!mnt->mnt_pinned)) {
3397b7b1aceSAl Viro 			spin_unlock(&vfsmount_lock);
3407b7b1aceSAl Viro 			__mntput(mnt);
3417b7b1aceSAl Viro 			return;
3427b7b1aceSAl Viro 		}
3437b7b1aceSAl Viro 		atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
3447b7b1aceSAl Viro 		mnt->mnt_pinned = 0;
3457b7b1aceSAl Viro 		spin_unlock(&vfsmount_lock);
3467b7b1aceSAl Viro 		acct_auto_close_mnt(mnt);
3477b7b1aceSAl Viro 		security_sb_umount_close(mnt);
3487b7b1aceSAl Viro 		goto repeat;
3497b7b1aceSAl Viro 	}
3507b7b1aceSAl Viro }
3517b7b1aceSAl Viro 
3527b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire);
3537b7b1aceSAl Viro 
3547b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
3557b7b1aceSAl Viro {
3567b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
3577b7b1aceSAl Viro 	mnt->mnt_pinned++;
3587b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
3597b7b1aceSAl Viro }
3607b7b1aceSAl Viro 
3617b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
3627b7b1aceSAl Viro 
3637b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt)
3647b7b1aceSAl Viro {
3657b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
3667b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
3677b7b1aceSAl Viro 		atomic_inc(&mnt->mnt_count);
3687b7b1aceSAl Viro 		mnt->mnt_pinned--;
3697b7b1aceSAl Viro 	}
3707b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
3717b7b1aceSAl Viro }
3727b7b1aceSAl Viro 
3737b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
3741da177e4SLinus Torvalds 
375b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
376b3b304a2SMiklos Szeredi {
377b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
378b3b304a2SMiklos Szeredi }
379b3b304a2SMiklos Szeredi 
380b3b304a2SMiklos Szeredi /*
381b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
382b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
383b3b304a2SMiklos Szeredi  *
384b3b304a2SMiklos Szeredi  * See also save_mount_options().
385b3b304a2SMiklos Szeredi  */
386b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
387b3b304a2SMiklos Szeredi {
388b3b304a2SMiklos Szeredi 	const char *options = mnt->mnt_sb->s_options;
389b3b304a2SMiklos Szeredi 
390b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
391b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
392b3b304a2SMiklos Szeredi 		mangle(m, options);
393b3b304a2SMiklos Szeredi 	}
394b3b304a2SMiklos Szeredi 
395b3b304a2SMiklos Szeredi 	return 0;
396b3b304a2SMiklos Szeredi }
397b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
398b3b304a2SMiklos Szeredi 
399b3b304a2SMiklos Szeredi /*
400b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
401b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
402b3b304a2SMiklos Szeredi  *
403b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
404b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
405b3b304a2SMiklos Szeredi  * remount fails.
406b3b304a2SMiklos Szeredi  *
407b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
408b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
409b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
410b3b304a2SMiklos Szeredi  * any more.
411b3b304a2SMiklos Szeredi  */
412b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
413b3b304a2SMiklos Szeredi {
414b3b304a2SMiklos Szeredi 	kfree(sb->s_options);
415b3b304a2SMiklos Szeredi 	sb->s_options = kstrdup(options, GFP_KERNEL);
416b3b304a2SMiklos Szeredi }
417b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
418b3b304a2SMiklos Szeredi 
4191da177e4SLinus Torvalds /* iterator */
4201da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
4211da177e4SLinus Torvalds {
4226b3286edSKirill Korotaev 	struct mnt_namespace *n = m->private;
4231da177e4SLinus Torvalds 
424390c6843SRam Pai 	down_read(&namespace_sem);
425b0765fb8SPavel Emelianov 	return seq_list_start(&n->list, *pos);
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
4291da177e4SLinus Torvalds {
4306b3286edSKirill Korotaev 	struct mnt_namespace *n = m->private;
431b0765fb8SPavel Emelianov 
432b0765fb8SPavel Emelianov 	return seq_list_next(v, &n->list, pos);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
4361da177e4SLinus Torvalds {
437390c6843SRam Pai 	up_read(&namespace_sem);
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds static int show_vfsmnt(struct seq_file *m, void *v)
4411da177e4SLinus Torvalds {
442b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
4431da177e4SLinus Torvalds 	int err = 0;
4441da177e4SLinus Torvalds 	static struct proc_fs_info {
4451da177e4SLinus Torvalds 		int flag;
4461da177e4SLinus Torvalds 		char *str;
4471da177e4SLinus Torvalds 	} fs_info[] = {
4481da177e4SLinus Torvalds 		{ MS_SYNCHRONOUS, ",sync" },
4491da177e4SLinus Torvalds 		{ MS_DIRSYNC, ",dirsync" },
4501da177e4SLinus Torvalds 		{ MS_MANDLOCK, ",mand" },
4511da177e4SLinus Torvalds 		{ 0, NULL }
4521da177e4SLinus Torvalds 	};
4531da177e4SLinus Torvalds 	static struct proc_fs_info mnt_info[] = {
4541da177e4SLinus Torvalds 		{ MNT_NOSUID, ",nosuid" },
4551da177e4SLinus Torvalds 		{ MNT_NODEV, ",nodev" },
4561da177e4SLinus Torvalds 		{ MNT_NOEXEC, ",noexec" },
457fc33a7bbSChristoph Hellwig 		{ MNT_NOATIME, ",noatime" },
458fc33a7bbSChristoph Hellwig 		{ MNT_NODIRATIME, ",nodiratime" },
45947ae32d6SValerie Henson 		{ MNT_RELATIME, ",relatime" },
4601da177e4SLinus Torvalds 		{ 0, NULL }
4611da177e4SLinus Torvalds 	};
4621da177e4SLinus Torvalds 	struct proc_fs_info *fs_infop;
463c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
4661da177e4SLinus Torvalds 	seq_putc(m, ' ');
467c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
4681da177e4SLinus Torvalds 	seq_putc(m, ' ');
4691da177e4SLinus Torvalds 	mangle(m, mnt->mnt_sb->s_type->name);
47079c0b2dfSMiklos Szeredi 	if (mnt->mnt_sb->s_subtype && mnt->mnt_sb->s_subtype[0]) {
47179c0b2dfSMiklos Szeredi 		seq_putc(m, '.');
47279c0b2dfSMiklos Szeredi 		mangle(m, mnt->mnt_sb->s_subtype);
47379c0b2dfSMiklos Szeredi 	}
4741da177e4SLinus Torvalds 	seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
4751da177e4SLinus Torvalds 	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
4761da177e4SLinus Torvalds 		if (mnt->mnt_sb->s_flags & fs_infop->flag)
4771da177e4SLinus Torvalds 			seq_puts(m, fs_infop->str);
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
4801da177e4SLinus Torvalds 		if (mnt->mnt_flags & fs_infop->flag)
4811da177e4SLinus Torvalds 			seq_puts(m, fs_infop->str);
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_op->show_options)
4841da177e4SLinus Torvalds 		err = mnt->mnt_sb->s_op->show_options(m, mnt);
4851da177e4SLinus Torvalds 	seq_puts(m, " 0 0\n");
4861da177e4SLinus Torvalds 	return err;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds struct seq_operations mounts_op = {
4901da177e4SLinus Torvalds 	.start	= m_start,
4911da177e4SLinus Torvalds 	.next	= m_next,
4921da177e4SLinus Torvalds 	.stop	= m_stop,
4931da177e4SLinus Torvalds 	.show	= show_vfsmnt
4941da177e4SLinus Torvalds };
4951da177e4SLinus Torvalds 
496b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v)
497b4629fe2SChuck Lever {
498b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
499c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
500b4629fe2SChuck Lever 	int err = 0;
501b4629fe2SChuck Lever 
502b4629fe2SChuck Lever 	/* device */
503b4629fe2SChuck Lever 	if (mnt->mnt_devname) {
504b4629fe2SChuck Lever 		seq_puts(m, "device ");
505b4629fe2SChuck Lever 		mangle(m, mnt->mnt_devname);
506b4629fe2SChuck Lever 	} else
507b4629fe2SChuck Lever 		seq_puts(m, "no device");
508b4629fe2SChuck Lever 
509b4629fe2SChuck Lever 	/* mount point */
510b4629fe2SChuck Lever 	seq_puts(m, " mounted on ");
511c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
512b4629fe2SChuck Lever 	seq_putc(m, ' ');
513b4629fe2SChuck Lever 
514b4629fe2SChuck Lever 	/* file system type */
515b4629fe2SChuck Lever 	seq_puts(m, "with fstype ");
516b4629fe2SChuck Lever 	mangle(m, mnt->mnt_sb->s_type->name);
517b4629fe2SChuck Lever 
518b4629fe2SChuck Lever 	/* optional statistics */
519b4629fe2SChuck Lever 	if (mnt->mnt_sb->s_op->show_stats) {
520b4629fe2SChuck Lever 		seq_putc(m, ' ');
521b4629fe2SChuck Lever 		err = mnt->mnt_sb->s_op->show_stats(m, mnt);
522b4629fe2SChuck Lever 	}
523b4629fe2SChuck Lever 
524b4629fe2SChuck Lever 	seq_putc(m, '\n');
525b4629fe2SChuck Lever 	return err;
526b4629fe2SChuck Lever }
527b4629fe2SChuck Lever 
528b4629fe2SChuck Lever struct seq_operations mountstats_op = {
529b4629fe2SChuck Lever 	.start	= m_start,
530b4629fe2SChuck Lever 	.next	= m_next,
531b4629fe2SChuck Lever 	.stop	= m_stop,
532b4629fe2SChuck Lever 	.show	= show_vfsstat,
533b4629fe2SChuck Lever };
534b4629fe2SChuck Lever 
5351da177e4SLinus Torvalds /**
5361da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
5371da177e4SLinus Torvalds  * @mnt: root of mount tree
5381da177e4SLinus Torvalds  *
5391da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
5401da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
5411da177e4SLinus Torvalds  * busy.
5421da177e4SLinus Torvalds  */
5431da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt)
5441da177e4SLinus Torvalds {
54536341f64SRam Pai 	int actual_refs = 0;
54636341f64SRam Pai 	int minimum_refs = 0;
54736341f64SRam Pai 	struct vfsmount *p;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
55036341f64SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
5511da177e4SLinus Torvalds 		actual_refs += atomic_read(&p->mnt_count);
5521da177e4SLinus Torvalds 		minimum_refs += 2;
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
5571da177e4SLinus Torvalds 		return 0;
558e3474a8eSIan Kent 
559e3474a8eSIan Kent 	return 1;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds /**
5651da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
5661da177e4SLinus Torvalds  * @mnt: root of mount
5671da177e4SLinus Torvalds  *
5681da177e4SLinus Torvalds  * This is called to check if a mount point has any
5691da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
5701da177e4SLinus Torvalds  * mount has sub mounts this will return busy
5711da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
5721da177e4SLinus Torvalds  *
5731da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
5741da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
5751da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
5761da177e4SLinus Torvalds  */
5771da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
5781da177e4SLinus Torvalds {
579e3474a8eSIan Kent 	int ret = 1;
580a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
581a05964f3SRam Pai 	if (propagate_mount_busy(mnt, 2))
582e3474a8eSIan Kent 		ret = 0;
583a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
584a05964f3SRam Pai 	return ret;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
5881da177e4SLinus Torvalds 
589b90fa9aeSRam Pai void release_mounts(struct list_head *head)
5901da177e4SLinus Torvalds {
59170fbcdf4SRam Pai 	struct vfsmount *mnt;
59270fbcdf4SRam Pai 	while (!list_empty(head)) {
593b5e61818SPavel Emelianov 		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
59470fbcdf4SRam Pai 		list_del_init(&mnt->mnt_hash);
59570fbcdf4SRam Pai 		if (mnt->mnt_parent != mnt) {
59670fbcdf4SRam Pai 			struct dentry *dentry;
59770fbcdf4SRam Pai 			struct vfsmount *m;
59870fbcdf4SRam Pai 			spin_lock(&vfsmount_lock);
59970fbcdf4SRam Pai 			dentry = mnt->mnt_mountpoint;
60070fbcdf4SRam Pai 			m = mnt->mnt_parent;
60170fbcdf4SRam Pai 			mnt->mnt_mountpoint = mnt->mnt_root;
60270fbcdf4SRam Pai 			mnt->mnt_parent = mnt;
6037c4b93d8SAl Viro 			m->mnt_ghosts--;
6041da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
60570fbcdf4SRam Pai 			dput(dentry);
60670fbcdf4SRam Pai 			mntput(m);
6071da177e4SLinus Torvalds 		}
6081da177e4SLinus Torvalds 		mntput(mnt);
60970fbcdf4SRam Pai 	}
61070fbcdf4SRam Pai }
61170fbcdf4SRam Pai 
612a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
61370fbcdf4SRam Pai {
61470fbcdf4SRam Pai 	struct vfsmount *p;
61570fbcdf4SRam Pai 
6161bfba4e8SAkinobu Mita 	for (p = mnt; p; p = next_mnt(p, mnt))
6171bfba4e8SAkinobu Mita 		list_move(&p->mnt_hash, kill);
61870fbcdf4SRam Pai 
619a05964f3SRam Pai 	if (propagate)
620a05964f3SRam Pai 		propagate_umount(kill);
621a05964f3SRam Pai 
62270fbcdf4SRam Pai 	list_for_each_entry(p, kill, mnt_hash) {
62370fbcdf4SRam Pai 		list_del_init(&p->mnt_expire);
62470fbcdf4SRam Pai 		list_del_init(&p->mnt_list);
6256b3286edSKirill Korotaev 		__touch_mnt_namespace(p->mnt_ns);
6266b3286edSKirill Korotaev 		p->mnt_ns = NULL;
62770fbcdf4SRam Pai 		list_del_init(&p->mnt_child);
6287c4b93d8SAl Viro 		if (p->mnt_parent != p) {
6297c4b93d8SAl Viro 			p->mnt_parent->mnt_ghosts++;
630f30ac319SAl Viro 			p->mnt_mountpoint->d_mounted--;
6317c4b93d8SAl Viro 		}
632a05964f3SRam Pai 		change_mnt_propagation(p, MS_PRIVATE);
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
636c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
637c35038beSAl Viro 
6381da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
6411da177e4SLinus Torvalds 	int retval;
64270fbcdf4SRam Pai 	LIST_HEAD(umount_list);
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	retval = security_sb_umount(mnt, flags);
6451da177e4SLinus Torvalds 	if (retval)
6461da177e4SLinus Torvalds 		return retval;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	/*
6491da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
6501da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
6511da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
6521da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
6531da177e4SLinus Torvalds 	 */
6541da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
6556ac08c39SJan Blunck 		if (mnt == current->fs->root.mnt ||
6561da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
6571da177e4SLinus Torvalds 			return -EINVAL;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 		if (atomic_read(&mnt->mnt_count) != 2)
6601da177e4SLinus Torvalds 			return -EBUSY;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1))
6631da177e4SLinus Torvalds 			return -EAGAIN;
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/*
6671da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
6681da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
6691da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
6701da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
6711da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
6721da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
6731da177e4SLinus Torvalds 	 * about for the moment.
6741da177e4SLinus Torvalds 	 */
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	lock_kernel();
6778b512d9aSTrond Myklebust 	if (sb->s_op->umount_begin)
6788b512d9aSTrond Myklebust 		sb->s_op->umount_begin(mnt, flags);
6791da177e4SLinus Torvalds 	unlock_kernel();
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	/*
6821da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
6831da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
6841da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
6851da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
6861da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
6871da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
6881da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
6891da177e4SLinus Torvalds 	 */
6906ac08c39SJan Blunck 	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
6911da177e4SLinus Torvalds 		/*
6921da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
6931da177e4SLinus Torvalds 		 * we just try to remount it readonly.
6941da177e4SLinus Torvalds 		 */
6951da177e4SLinus Torvalds 		down_write(&sb->s_umount);
6961da177e4SLinus Torvalds 		if (!(sb->s_flags & MS_RDONLY)) {
6971da177e4SLinus Torvalds 			lock_kernel();
6981da177e4SLinus Torvalds 			DQUOT_OFF(sb);
6991da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
7001da177e4SLinus Torvalds 			unlock_kernel();
7011da177e4SLinus Torvalds 		}
7021da177e4SLinus Torvalds 		up_write(&sb->s_umount);
7031da177e4SLinus Torvalds 		return retval;
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 
706390c6843SRam Pai 	down_write(&namespace_sem);
7071da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
7085addc5ddSAl Viro 	event++;
7091da177e4SLinus Torvalds 
710c35038beSAl Viro 	if (!(flags & MNT_DETACH))
711c35038beSAl Viro 		shrink_submounts(mnt, &umount_list);
712c35038beSAl Viro 
7131da177e4SLinus Torvalds 	retval = -EBUSY;
714a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
7151da177e4SLinus Torvalds 		if (!list_empty(&mnt->mnt_list))
716a05964f3SRam Pai 			umount_tree(mnt, 1, &umount_list);
7171da177e4SLinus Torvalds 		retval = 0;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
7201da177e4SLinus Torvalds 	if (retval)
7211da177e4SLinus Torvalds 		security_sb_umount_busy(mnt);
722390c6843SRam Pai 	up_write(&namespace_sem);
72370fbcdf4SRam Pai 	release_mounts(&umount_list);
7241da177e4SLinus Torvalds 	return retval;
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds /*
7281da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
7291da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
7301da177e4SLinus Torvalds  *
7311da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
7321da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
7331da177e4SLinus Torvalds  */
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds asmlinkage long sys_umount(char __user * name, int flags)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	struct nameidata nd;
7381da177e4SLinus Torvalds 	int retval;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
7411da177e4SLinus Torvalds 	if (retval)
7421da177e4SLinus Torvalds 		goto out;
7431da177e4SLinus Torvalds 	retval = -EINVAL;
7444ac91378SJan Blunck 	if (nd.path.dentry != nd.path.mnt->mnt_root)
7451da177e4SLinus Torvalds 		goto dput_and_out;
7464ac91378SJan Blunck 	if (!check_mnt(nd.path.mnt))
7471da177e4SLinus Torvalds 		goto dput_and_out;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	retval = -EPERM;
7501da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
7511da177e4SLinus Torvalds 		goto dput_and_out;
7521da177e4SLinus Torvalds 
7534ac91378SJan Blunck 	retval = do_umount(nd.path.mnt, flags);
7541da177e4SLinus Torvalds dput_and_out:
755429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
7564ac91378SJan Blunck 	dput(nd.path.dentry);
7574ac91378SJan Blunck 	mntput_no_expire(nd.path.mnt);
7581da177e4SLinus Torvalds out:
7591da177e4SLinus Torvalds 	return retval;
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /*
7651da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
7661da177e4SLinus Torvalds  */
7671da177e4SLinus Torvalds asmlinkage long sys_oldumount(char __user * name)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	return sys_umount(name, 0);
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds #endif
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds static int mount_is_safe(struct nameidata *nd)
7751da177e4SLinus Torvalds {
7761da177e4SLinus Torvalds 	if (capable(CAP_SYS_ADMIN))
7771da177e4SLinus Torvalds 		return 0;
7781da177e4SLinus Torvalds 	return -EPERM;
7791da177e4SLinus Torvalds #ifdef notyet
7804ac91378SJan Blunck 	if (S_ISLNK(nd->path.dentry->d_inode->i_mode))
7811da177e4SLinus Torvalds 		return -EPERM;
7824ac91378SJan Blunck 	if (nd->path.dentry->d_inode->i_mode & S_ISVTX) {
7834ac91378SJan Blunck 		if (current->uid != nd->path.dentry->d_inode->i_uid)
7841da177e4SLinus Torvalds 			return -EPERM;
7851da177e4SLinus Torvalds 	}
786e4543eddSChristoph Hellwig 	if (vfs_permission(nd, MAY_WRITE))
7871da177e4SLinus Torvalds 		return -EPERM;
7881da177e4SLinus Torvalds 	return 0;
7891da177e4SLinus Torvalds #endif
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
792b58fed8bSRam Pai static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
7931da177e4SLinus Torvalds {
7941da177e4SLinus Torvalds 	while (1) {
7951da177e4SLinus Torvalds 		if (d == dentry)
7961da177e4SLinus Torvalds 			return 1;
7971da177e4SLinus Torvalds 		if (d == NULL || d == d->d_parent)
7981da177e4SLinus Torvalds 			return 0;
7991da177e4SLinus Torvalds 		d = d->d_parent;
8001da177e4SLinus Torvalds 	}
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
803b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
80436341f64SRam Pai 					int flag)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	struct vfsmount *res, *p, *q, *r, *s;
8071a390689SAl Viro 	struct path path;
8081da177e4SLinus Torvalds 
8099676f0c6SRam Pai 	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
8109676f0c6SRam Pai 		return NULL;
8119676f0c6SRam Pai 
81236341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
8131da177e4SLinus Torvalds 	if (!q)
8141da177e4SLinus Torvalds 		goto Enomem;
8151da177e4SLinus Torvalds 	q->mnt_mountpoint = mnt->mnt_mountpoint;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	p = mnt;
818fdadd65fSDomen Puncer 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
8191da177e4SLinus Torvalds 		if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
8201da177e4SLinus Torvalds 			continue;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 		for (s = r; s; s = next_mnt(s, r)) {
8239676f0c6SRam Pai 			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
8249676f0c6SRam Pai 				s = skip_mnt_tree(s);
8259676f0c6SRam Pai 				continue;
8269676f0c6SRam Pai 			}
8271da177e4SLinus Torvalds 			while (p != s->mnt_parent) {
8281da177e4SLinus Torvalds 				p = p->mnt_parent;
8291da177e4SLinus Torvalds 				q = q->mnt_parent;
8301da177e4SLinus Torvalds 			}
8311da177e4SLinus Torvalds 			p = s;
8321a390689SAl Viro 			path.mnt = q;
8331a390689SAl Viro 			path.dentry = p->mnt_mountpoint;
83436341f64SRam Pai 			q = clone_mnt(p, p->mnt_root, flag);
8351da177e4SLinus Torvalds 			if (!q)
8361da177e4SLinus Torvalds 				goto Enomem;
8371da177e4SLinus Torvalds 			spin_lock(&vfsmount_lock);
8381da177e4SLinus Torvalds 			list_add_tail(&q->mnt_list, &res->mnt_list);
8391a390689SAl Viro 			attach_mnt(q, &path);
8401da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
8411da177e4SLinus Torvalds 		}
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 	return res;
8441da177e4SLinus Torvalds Enomem:
8451da177e4SLinus Torvalds 	if (res) {
84670fbcdf4SRam Pai 		LIST_HEAD(umount_list);
8471da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
848a05964f3SRam Pai 		umount_tree(res, 0, &umount_list);
8491da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
85070fbcdf4SRam Pai 		release_mounts(&umount_list);
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 	return NULL;
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8558aec0809SAl Viro struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry)
8568aec0809SAl Viro {
8578aec0809SAl Viro 	struct vfsmount *tree;
8588aec0809SAl Viro 	down_read(&namespace_sem);
8598aec0809SAl Viro 	tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE);
8608aec0809SAl Viro 	up_read(&namespace_sem);
8618aec0809SAl Viro 	return tree;
8628aec0809SAl Viro }
8638aec0809SAl Viro 
8648aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
8658aec0809SAl Viro {
8668aec0809SAl Viro 	LIST_HEAD(umount_list);
8678aec0809SAl Viro 	down_read(&namespace_sem);
8688aec0809SAl Viro 	spin_lock(&vfsmount_lock);
8698aec0809SAl Viro 	umount_tree(mnt, 0, &umount_list);
8708aec0809SAl Viro 	spin_unlock(&vfsmount_lock);
8718aec0809SAl Viro 	up_read(&namespace_sem);
8728aec0809SAl Viro 	release_mounts(&umount_list);
8738aec0809SAl Viro }
8748aec0809SAl Viro 
875b90fa9aeSRam Pai /*
876b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
877b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
87821444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
87921444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
88021444403SRam Pai  *  		   (done when source_mnt is moved)
881b90fa9aeSRam Pai  *
882b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
883b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
8849676f0c6SRam Pai  * ---------------------------------------------------------------------------
885b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
8869676f0c6SRam Pai  * |**************************************************************************
8879676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
8889676f0c6SRam Pai  * | dest     |               |                |                |            |
8899676f0c6SRam Pai  * |   |      |               |                |                |            |
8909676f0c6SRam Pai  * |   v      |               |                |                |            |
8919676f0c6SRam Pai  * |**************************************************************************
8929676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
8935afe0022SRam Pai  * |          |               |                |                |            |
8949676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
8959676f0c6SRam Pai  * ***************************************************************************
896b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
897b90fa9aeSRam Pai  * destination mount.
898b90fa9aeSRam Pai  *
899b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
900b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
901b90fa9aeSRam Pai  * 	 the peer group of the source mount.
902b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
903b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
904b90fa9aeSRam Pai  *       mount.
9055afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
9065afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
9075afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
9085afe0022SRam Pai  *       is marked as 'shared and slave'.
9095afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
9105afe0022SRam Pai  * 	 source mount.
9115afe0022SRam Pai  *
9129676f0c6SRam Pai  * ---------------------------------------------------------------------------
91321444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
9149676f0c6SRam Pai  * |**************************************************************************
9159676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
9169676f0c6SRam Pai  * | dest     |               |                |                |            |
9179676f0c6SRam Pai  * |   |      |               |                |                |            |
9189676f0c6SRam Pai  * |   v      |               |                |                |            |
9199676f0c6SRam Pai  * |**************************************************************************
9209676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
9215afe0022SRam Pai  * |          |               |                |                |            |
9229676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
9239676f0c6SRam Pai  * ***************************************************************************
9245afe0022SRam Pai  *
9255afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
9265afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
92721444403SRam Pai  * (+*)  the mount is moved to the destination.
9285afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
9295afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
9305afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
9315afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
932b90fa9aeSRam Pai  *
933b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
934b90fa9aeSRam Pai  * applied to each mount in the tree.
935b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
936b90fa9aeSRam Pai  * in allocations.
937b90fa9aeSRam Pai  */
938b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt,
9391a390689SAl Viro 			struct path *path, struct path *parent_path)
940b90fa9aeSRam Pai {
941b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
9421a390689SAl Viro 	struct vfsmount *dest_mnt = path->mnt;
9431a390689SAl Viro 	struct dentry *dest_dentry = path->dentry;
944b90fa9aeSRam Pai 	struct vfsmount *child, *p;
945b90fa9aeSRam Pai 
946b90fa9aeSRam Pai 	if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
947b90fa9aeSRam Pai 		return -EINVAL;
948b90fa9aeSRam Pai 
949b90fa9aeSRam Pai 	if (IS_MNT_SHARED(dest_mnt)) {
950b90fa9aeSRam Pai 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
951b90fa9aeSRam Pai 			set_mnt_shared(p);
952b90fa9aeSRam Pai 	}
953b90fa9aeSRam Pai 
954b90fa9aeSRam Pai 	spin_lock(&vfsmount_lock);
9551a390689SAl Viro 	if (parent_path) {
9561a390689SAl Viro 		detach_mnt(source_mnt, parent_path);
9571a390689SAl Viro 		attach_mnt(source_mnt, path);
9586b3286edSKirill Korotaev 		touch_mnt_namespace(current->nsproxy->mnt_ns);
95921444403SRam Pai 	} else {
960b90fa9aeSRam Pai 		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
961b90fa9aeSRam Pai 		commit_tree(source_mnt);
96221444403SRam Pai 	}
963b90fa9aeSRam Pai 
964b90fa9aeSRam Pai 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
965b90fa9aeSRam Pai 		list_del_init(&child->mnt_hash);
966b90fa9aeSRam Pai 		commit_tree(child);
967b90fa9aeSRam Pai 	}
968b90fa9aeSRam Pai 	spin_unlock(&vfsmount_lock);
969b90fa9aeSRam Pai 	return 0;
970b90fa9aeSRam Pai }
971b90fa9aeSRam Pai 
9721da177e4SLinus Torvalds static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds 	int err;
9751da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_flags & MS_NOUSER)
9761da177e4SLinus Torvalds 		return -EINVAL;
9771da177e4SLinus Torvalds 
9784ac91378SJan Blunck 	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
9791da177e4SLinus Torvalds 	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
9801da177e4SLinus Torvalds 		return -ENOTDIR;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	err = -ENOENT;
9834ac91378SJan Blunck 	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
9844ac91378SJan Blunck 	if (IS_DEADDIR(nd->path.dentry->d_inode))
9851da177e4SLinus Torvalds 		goto out_unlock;
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	err = security_sb_check_sb(mnt, nd);
9881da177e4SLinus Torvalds 	if (err)
9891da177e4SLinus Torvalds 		goto out_unlock;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	err = -ENOENT;
9924ac91378SJan Blunck 	if (IS_ROOT(nd->path.dentry) || !d_unhashed(nd->path.dentry))
9931a390689SAl Viro 		err = attach_recursive_mnt(mnt, &nd->path, NULL);
9941da177e4SLinus Torvalds out_unlock:
9954ac91378SJan Blunck 	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
9961da177e4SLinus Torvalds 	if (!err)
9971da177e4SLinus Torvalds 		security_sb_post_addmount(mnt, nd);
9981da177e4SLinus Torvalds 	return err;
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds /*
100207b20889SRam Pai  * recursively change the type of the mountpoint.
10032dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
100407b20889SRam Pai  */
10052dafe1c4SEric Sandeen static noinline int do_change_type(struct nameidata *nd, int flag)
100607b20889SRam Pai {
10074ac91378SJan Blunck 	struct vfsmount *m, *mnt = nd->path.mnt;
100807b20889SRam Pai 	int recurse = flag & MS_REC;
100907b20889SRam Pai 	int type = flag & ~MS_REC;
101007b20889SRam Pai 
1011ee6f9582SMiklos Szeredi 	if (!capable(CAP_SYS_ADMIN))
1012ee6f9582SMiklos Szeredi 		return -EPERM;
1013ee6f9582SMiklos Szeredi 
10144ac91378SJan Blunck 	if (nd->path.dentry != nd->path.mnt->mnt_root)
101507b20889SRam Pai 		return -EINVAL;
101607b20889SRam Pai 
101707b20889SRam Pai 	down_write(&namespace_sem);
101807b20889SRam Pai 	spin_lock(&vfsmount_lock);
101907b20889SRam Pai 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
102007b20889SRam Pai 		change_mnt_propagation(m, type);
102107b20889SRam Pai 	spin_unlock(&vfsmount_lock);
102207b20889SRam Pai 	up_write(&namespace_sem);
102307b20889SRam Pai 	return 0;
102407b20889SRam Pai }
102507b20889SRam Pai 
102607b20889SRam Pai /*
10271da177e4SLinus Torvalds  * do loopback mount.
10282dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
10291da177e4SLinus Torvalds  */
10302dafe1c4SEric Sandeen static noinline int do_loopback(struct nameidata *nd, char *old_name,
10312dafe1c4SEric Sandeen 				int recurse)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds 	struct nameidata old_nd;
10341da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
10351da177e4SLinus Torvalds 	int err = mount_is_safe(nd);
10361da177e4SLinus Torvalds 	if (err)
10371da177e4SLinus Torvalds 		return err;
10381da177e4SLinus Torvalds 	if (!old_name || !*old_name)
10391da177e4SLinus Torvalds 		return -EINVAL;
10401da177e4SLinus Torvalds 	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
10411da177e4SLinus Torvalds 	if (err)
10421da177e4SLinus Torvalds 		return err;
10431da177e4SLinus Torvalds 
1044390c6843SRam Pai 	down_write(&namespace_sem);
10451da177e4SLinus Torvalds 	err = -EINVAL;
10464ac91378SJan Blunck 	if (IS_MNT_UNBINDABLE(old_nd.path.mnt))
10479676f0c6SRam Pai 		goto out;
10489676f0c6SRam Pai 
10494ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
1050ccd48bc7SAl Viro 		goto out;
1051ccd48bc7SAl Viro 
10521da177e4SLinus Torvalds 	err = -ENOMEM;
10531da177e4SLinus Torvalds 	if (recurse)
10544ac91378SJan Blunck 		mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0);
10551da177e4SLinus Torvalds 	else
10564ac91378SJan Blunck 		mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0);
10571da177e4SLinus Torvalds 
1058ccd48bc7SAl Viro 	if (!mnt)
1059ccd48bc7SAl Viro 		goto out;
1060ccd48bc7SAl Viro 
10611da177e4SLinus Torvalds 	err = graft_tree(mnt, nd);
10621da177e4SLinus Torvalds 	if (err) {
106370fbcdf4SRam Pai 		LIST_HEAD(umount_list);
10641da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
1065a05964f3SRam Pai 		umount_tree(mnt, 0, &umount_list);
10661da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
106770fbcdf4SRam Pai 		release_mounts(&umount_list);
10685b83d2c5SRam Pai 	}
10691da177e4SLinus Torvalds 
1070ccd48bc7SAl Viro out:
1071390c6843SRam Pai 	up_write(&namespace_sem);
10721d957f9bSJan Blunck 	path_put(&old_nd.path);
10731da177e4SLinus Torvalds 	return err;
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds /*
10771da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
10781da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
10791da177e4SLinus Torvalds  * on it - tough luck.
10802dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
10811da177e4SLinus Torvalds  */
10822dafe1c4SEric Sandeen static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags,
10831da177e4SLinus Torvalds 		      void *data)
10841da177e4SLinus Torvalds {
10851da177e4SLinus Torvalds 	int err;
10864ac91378SJan Blunck 	struct super_block *sb = nd->path.mnt->mnt_sb;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
10891da177e4SLinus Torvalds 		return -EPERM;
10901da177e4SLinus Torvalds 
10914ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt))
10921da177e4SLinus Torvalds 		return -EINVAL;
10931da177e4SLinus Torvalds 
10944ac91378SJan Blunck 	if (nd->path.dentry != nd->path.mnt->mnt_root)
10951da177e4SLinus Torvalds 		return -EINVAL;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	down_write(&sb->s_umount);
10981da177e4SLinus Torvalds 	err = do_remount_sb(sb, flags, data, 0);
10991da177e4SLinus Torvalds 	if (!err)
11004ac91378SJan Blunck 		nd->path.mnt->mnt_flags = mnt_flags;
11011da177e4SLinus Torvalds 	up_write(&sb->s_umount);
11021da177e4SLinus Torvalds 	if (!err)
11034ac91378SJan Blunck 		security_sb_post_remount(nd->path.mnt, flags, data);
11041da177e4SLinus Torvalds 	return err;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11079676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt)
11089676f0c6SRam Pai {
11099676f0c6SRam Pai 	struct vfsmount *p;
11109676f0c6SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
11119676f0c6SRam Pai 		if (IS_MNT_UNBINDABLE(p))
11129676f0c6SRam Pai 			return 1;
11139676f0c6SRam Pai 	}
11149676f0c6SRam Pai 	return 0;
11159676f0c6SRam Pai }
11169676f0c6SRam Pai 
11172dafe1c4SEric Sandeen /*
11182dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
11192dafe1c4SEric Sandeen  */
11202dafe1c4SEric Sandeen static noinline int do_move_mount(struct nameidata *nd, char *old_name)
11211da177e4SLinus Torvalds {
11221a390689SAl Viro 	struct nameidata old_nd;
11231a390689SAl Viro 	struct path parent_path;
11241da177e4SLinus Torvalds 	struct vfsmount *p;
11251da177e4SLinus Torvalds 	int err = 0;
11261da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
11271da177e4SLinus Torvalds 		return -EPERM;
11281da177e4SLinus Torvalds 	if (!old_name || !*old_name)
11291da177e4SLinus Torvalds 		return -EINVAL;
11301da177e4SLinus Torvalds 	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
11311da177e4SLinus Torvalds 	if (err)
11321da177e4SLinus Torvalds 		return err;
11331da177e4SLinus Torvalds 
1134390c6843SRam Pai 	down_write(&namespace_sem);
11354ac91378SJan Blunck 	while (d_mountpoint(nd->path.dentry) &&
11364ac91378SJan Blunck 	       follow_down(&nd->path.mnt, &nd->path.dentry))
11371da177e4SLinus Torvalds 		;
11381da177e4SLinus Torvalds 	err = -EINVAL;
11394ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
11401da177e4SLinus Torvalds 		goto out;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	err = -ENOENT;
11434ac91378SJan Blunck 	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
11444ac91378SJan Blunck 	if (IS_DEADDIR(nd->path.dentry->d_inode))
11451da177e4SLinus Torvalds 		goto out1;
11461da177e4SLinus Torvalds 
11474ac91378SJan Blunck 	if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry))
114821444403SRam Pai 		goto out1;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	err = -EINVAL;
11514ac91378SJan Blunck 	if (old_nd.path.dentry != old_nd.path.mnt->mnt_root)
115221444403SRam Pai 		goto out1;
11531da177e4SLinus Torvalds 
11544ac91378SJan Blunck 	if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent)
115521444403SRam Pai 		goto out1;
11561da177e4SLinus Torvalds 
11574ac91378SJan Blunck 	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
11584ac91378SJan Blunck 	      S_ISDIR(old_nd.path.dentry->d_inode->i_mode))
115921444403SRam Pai 		goto out1;
116021444403SRam Pai 	/*
116121444403SRam Pai 	 * Don't move a mount residing in a shared parent.
116221444403SRam Pai 	 */
11634ac91378SJan Blunck 	if (old_nd.path.mnt->mnt_parent &&
11644ac91378SJan Blunck 	    IS_MNT_SHARED(old_nd.path.mnt->mnt_parent))
116521444403SRam Pai 		goto out1;
11669676f0c6SRam Pai 	/*
11679676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
11689676f0c6SRam Pai 	 * mount which is shared.
11699676f0c6SRam Pai 	 */
11704ac91378SJan Blunck 	if (IS_MNT_SHARED(nd->path.mnt) &&
11714ac91378SJan Blunck 	    tree_contains_unbindable(old_nd.path.mnt))
11729676f0c6SRam Pai 		goto out1;
11731da177e4SLinus Torvalds 	err = -ELOOP;
11744ac91378SJan Blunck 	for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent)
11754ac91378SJan Blunck 		if (p == old_nd.path.mnt)
117621444403SRam Pai 			goto out1;
11771da177e4SLinus Torvalds 
11781a390689SAl Viro 	err = attach_recursive_mnt(old_nd.path.mnt, &nd->path, &parent_path);
11794ac91378SJan Blunck 	if (err)
118021444403SRam Pai 		goto out1;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
11831da177e4SLinus Torvalds 	 * automatically */
11844ac91378SJan Blunck 	list_del_init(&old_nd.path.mnt->mnt_expire);
11851da177e4SLinus Torvalds out1:
11864ac91378SJan Blunck 	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
11871da177e4SLinus Torvalds out:
1188390c6843SRam Pai 	up_write(&namespace_sem);
11891da177e4SLinus Torvalds 	if (!err)
11901a390689SAl Viro 		path_put(&parent_path);
11911d957f9bSJan Blunck 	path_put(&old_nd.path);
11921da177e4SLinus Torvalds 	return err;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds /*
11961da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
11971da177e4SLinus Torvalds  * namespace's tree
11982dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
11991da177e4SLinus Torvalds  */
12002dafe1c4SEric Sandeen static noinline int do_new_mount(struct nameidata *nd, char *type, int flags,
12011da177e4SLinus Torvalds 			int mnt_flags, char *name, void *data)
12021da177e4SLinus Torvalds {
12031da177e4SLinus Torvalds 	struct vfsmount *mnt;
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	if (!type || !memchr(type, 0, PAGE_SIZE))
12061da177e4SLinus Torvalds 		return -EINVAL;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	/* we need capabilities... */
12091da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
12101da177e4SLinus Torvalds 		return -EPERM;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	mnt = do_kern_mount(type, flags, name, data);
12131da177e4SLinus Torvalds 	if (IS_ERR(mnt))
12141da177e4SLinus Torvalds 		return PTR_ERR(mnt);
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	return do_add_mount(mnt, nd, mnt_flags, NULL);
12171da177e4SLinus Torvalds }
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds /*
12201da177e4SLinus Torvalds  * add a mount into a namespace's mount tree
12211da177e4SLinus Torvalds  * - provide the option of adding the new mount to an expiration list
12221da177e4SLinus Torvalds  */
12231da177e4SLinus Torvalds int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
12241da177e4SLinus Torvalds 		 int mnt_flags, struct list_head *fslist)
12251da177e4SLinus Torvalds {
12261da177e4SLinus Torvalds 	int err;
12271da177e4SLinus Torvalds 
1228390c6843SRam Pai 	down_write(&namespace_sem);
12291da177e4SLinus Torvalds 	/* Something was mounted here while we slept */
12304ac91378SJan Blunck 	while (d_mountpoint(nd->path.dentry) &&
12314ac91378SJan Blunck 	       follow_down(&nd->path.mnt, &nd->path.dentry))
12321da177e4SLinus Torvalds 		;
12331da177e4SLinus Torvalds 	err = -EINVAL;
12344ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt))
12351da177e4SLinus Torvalds 		goto unlock;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	/* Refuse the same filesystem on the same mount point */
12381da177e4SLinus Torvalds 	err = -EBUSY;
12394ac91378SJan Blunck 	if (nd->path.mnt->mnt_sb == newmnt->mnt_sb &&
12404ac91378SJan Blunck 	    nd->path.mnt->mnt_root == nd->path.dentry)
12411da177e4SLinus Torvalds 		goto unlock;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	err = -EINVAL;
12441da177e4SLinus Torvalds 	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
12451da177e4SLinus Torvalds 		goto unlock;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	newmnt->mnt_flags = mnt_flags;
12485b83d2c5SRam Pai 	if ((err = graft_tree(newmnt, nd)))
12495b83d2c5SRam Pai 		goto unlock;
12501da177e4SLinus Torvalds 
12516758f953SAl Viro 	if (fslist) /* add to the specified expiration list */
125255e700b9SMiklos Szeredi 		list_add_tail(&newmnt->mnt_expire, fslist);
12536758f953SAl Viro 
1254390c6843SRam Pai 	up_write(&namespace_sem);
12555b83d2c5SRam Pai 	return 0;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds unlock:
1258390c6843SRam Pai 	up_write(&namespace_sem);
12591da177e4SLinus Torvalds 	mntput(newmnt);
12601da177e4SLinus Torvalds 	return err;
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount);
12641da177e4SLinus Torvalds 
12655528f911STrond Myklebust /*
12661da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
12671da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
12681da177e4SLinus Torvalds  * here
12691da177e4SLinus Torvalds  */
12701da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
12711da177e4SLinus Torvalds {
12721da177e4SLinus Torvalds 	struct vfsmount *mnt, *next;
12731da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
1274bcc5c7d2SAl Viro 	LIST_HEAD(umounts);
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	if (list_empty(mounts))
12771da177e4SLinus Torvalds 		return;
12781da177e4SLinus Torvalds 
1279bcc5c7d2SAl Viro 	down_write(&namespace_sem);
12801da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
12831da177e4SLinus Torvalds 	 * following criteria:
12841da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
12851da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
12861da177e4SLinus Torvalds 	 *   cleared by mntput())
12871da177e4SLinus Torvalds 	 */
128855e700b9SMiklos Szeredi 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
12891da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1290bcc5c7d2SAl Viro 			propagate_mount_busy(mnt, 1))
12911da177e4SLinus Torvalds 			continue;
129255e700b9SMiklos Szeredi 		list_move(&mnt->mnt_expire, &graveyard);
12931da177e4SLinus Torvalds 	}
1294bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
1295bcc5c7d2SAl Viro 		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
1296bcc5c7d2SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1297bcc5c7d2SAl Viro 		umount_tree(mnt, 1, &umounts);
1298bcc5c7d2SAl Viro 	}
12991da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
1300bcc5c7d2SAl Viro 	up_write(&namespace_sem);
1301bcc5c7d2SAl Viro 
1302bcc5c7d2SAl Viro 	release_mounts(&umounts);
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds /*
13085528f911STrond Myklebust  * Ripoff of 'select_parent()'
13095528f911STrond Myklebust  *
13105528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
13115528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
13125528f911STrond Myklebust  */
13135528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
13145528f911STrond Myklebust {
13155528f911STrond Myklebust 	struct vfsmount *this_parent = parent;
13165528f911STrond Myklebust 	struct list_head *next;
13175528f911STrond Myklebust 	int found = 0;
13185528f911STrond Myklebust 
13195528f911STrond Myklebust repeat:
13205528f911STrond Myklebust 	next = this_parent->mnt_mounts.next;
13215528f911STrond Myklebust resume:
13225528f911STrond Myklebust 	while (next != &this_parent->mnt_mounts) {
13235528f911STrond Myklebust 		struct list_head *tmp = next;
13245528f911STrond Myklebust 		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
13255528f911STrond Myklebust 
13265528f911STrond Myklebust 		next = tmp->next;
13275528f911STrond Myklebust 		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
13285528f911STrond Myklebust 			continue;
13295528f911STrond Myklebust 		/*
13305528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
13315528f911STrond Myklebust 		 */
13325528f911STrond Myklebust 		if (!list_empty(&mnt->mnt_mounts)) {
13335528f911STrond Myklebust 			this_parent = mnt;
13345528f911STrond Myklebust 			goto repeat;
13355528f911STrond Myklebust 		}
13365528f911STrond Myklebust 
13375528f911STrond Myklebust 		if (!propagate_mount_busy(mnt, 1)) {
13385528f911STrond Myklebust 			list_move_tail(&mnt->mnt_expire, graveyard);
13395528f911STrond Myklebust 			found++;
13405528f911STrond Myklebust 		}
13415528f911STrond Myklebust 	}
13425528f911STrond Myklebust 	/*
13435528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
13445528f911STrond Myklebust 	 */
13455528f911STrond Myklebust 	if (this_parent != parent) {
13465528f911STrond Myklebust 		next = this_parent->mnt_child.next;
13475528f911STrond Myklebust 		this_parent = this_parent->mnt_parent;
13485528f911STrond Myklebust 		goto resume;
13495528f911STrond Myklebust 	}
13505528f911STrond Myklebust 	return found;
13515528f911STrond Myklebust }
13525528f911STrond Myklebust 
13535528f911STrond Myklebust /*
13545528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
13555528f911STrond Myklebust  * submounts of a specific parent mountpoint
13565528f911STrond Myklebust  */
1357c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
13585528f911STrond Myklebust {
13595528f911STrond Myklebust 	LIST_HEAD(graveyard);
1360c35038beSAl Viro 	struct vfsmount *m;
13615528f911STrond Myklebust 
13625528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
1363c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
1364bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
1365c35038beSAl Viro 			m = list_first_entry(&graveyard, struct vfsmount,
1366bcc5c7d2SAl Viro 						mnt_expire);
1367bcc5c7d2SAl Viro 			touch_mnt_namespace(mnt->mnt_ns);
1368c35038beSAl Viro 			umount_tree(mnt, 1, umounts);
1369bcc5c7d2SAl Viro 		}
1370bcc5c7d2SAl Viro 	}
13715528f911STrond Myklebust }
13725528f911STrond Myklebust 
13735528f911STrond Myklebust /*
13741da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
13751da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
13761da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
13771da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
13781da177e4SLinus Torvalds  */
1379b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
1380b58fed8bSRam Pai 				 unsigned long n)
13811da177e4SLinus Torvalds {
13821da177e4SLinus Torvalds 	char *t = to;
13831da177e4SLinus Torvalds 	const char __user *f = from;
13841da177e4SLinus Torvalds 	char c;
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
13871da177e4SLinus Torvalds 		return n;
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	while (n) {
13901da177e4SLinus Torvalds 		if (__get_user(c, f)) {
13911da177e4SLinus Torvalds 			memset(t, 0, n);
13921da177e4SLinus Torvalds 			break;
13931da177e4SLinus Torvalds 		}
13941da177e4SLinus Torvalds 		*t++ = c;
13951da177e4SLinus Torvalds 		f++;
13961da177e4SLinus Torvalds 		n--;
13971da177e4SLinus Torvalds 	}
13981da177e4SLinus Torvalds 	return n;
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
14021da177e4SLinus Torvalds {
14031da177e4SLinus Torvalds 	int i;
14041da177e4SLinus Torvalds 	unsigned long page;
14051da177e4SLinus Torvalds 	unsigned long size;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	*where = 0;
14081da177e4SLinus Torvalds 	if (!data)
14091da177e4SLinus Torvalds 		return 0;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
14121da177e4SLinus Torvalds 		return -ENOMEM;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
14151da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
14161da177e4SLinus Torvalds 	 * the remainder of the page.
14171da177e4SLinus Torvalds 	 */
14181da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
14191da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
14201da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
14211da177e4SLinus Torvalds 		size = PAGE_SIZE;
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
14241da177e4SLinus Torvalds 	if (!i) {
14251da177e4SLinus Torvalds 		free_page(page);
14261da177e4SLinus Torvalds 		return -EFAULT;
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
14291da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
14301da177e4SLinus Torvalds 	*where = page;
14311da177e4SLinus Torvalds 	return 0;
14321da177e4SLinus Torvalds }
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds /*
14351da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
14361da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
14371da177e4SLinus Torvalds  *
14381da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
14391da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
14401da177e4SLinus Torvalds  * information (or be NULL).
14411da177e4SLinus Torvalds  *
14421da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
14431da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
14441da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
14451da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
14461da177e4SLinus Torvalds  * and must be discarded.
14471da177e4SLinus Torvalds  */
14481da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page,
14491da177e4SLinus Torvalds 		  unsigned long flags, void *data_page)
14501da177e4SLinus Torvalds {
14511da177e4SLinus Torvalds 	struct nameidata nd;
14521da177e4SLinus Torvalds 	int retval = 0;
14531da177e4SLinus Torvalds 	int mnt_flags = 0;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	/* Discard magic */
14561da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
14571da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	/* Basic sanity checks */
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
14621da177e4SLinus Torvalds 		return -EINVAL;
14631da177e4SLinus Torvalds 	if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
14641da177e4SLinus Torvalds 		return -EINVAL;
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds 	if (data_page)
14671da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
14701da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
14711da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
14721da177e4SLinus Torvalds 	if (flags & MS_NODEV)
14731da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
14741da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
14751da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
1476fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
1477fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
1478fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
1479fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
148047ae32d6SValerie Henson 	if (flags & MS_RELATIME)
148147ae32d6SValerie Henson 		mnt_flags |= MNT_RELATIME;
1482fc33a7bbSChristoph Hellwig 
1483fc33a7bbSChristoph Hellwig 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
14848bf9725cSPavel Emelyanov 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 	/* ... and get the mountpoint */
14871da177e4SLinus Torvalds 	retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
14881da177e4SLinus Torvalds 	if (retval)
14891da177e4SLinus Torvalds 		return retval;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
14921da177e4SLinus Torvalds 	if (retval)
14931da177e4SLinus Torvalds 		goto dput_out;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
14961da177e4SLinus Torvalds 		retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
14971da177e4SLinus Torvalds 				    data_page);
14981da177e4SLinus Torvalds 	else if (flags & MS_BIND)
1499eee391a6SAndrew Morton 		retval = do_loopback(&nd, dev_name, flags & MS_REC);
15009676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
150107b20889SRam Pai 		retval = do_change_type(&nd, flags);
15021da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
15031da177e4SLinus Torvalds 		retval = do_move_mount(&nd, dev_name);
15041da177e4SLinus Torvalds 	else
15051da177e4SLinus Torvalds 		retval = do_new_mount(&nd, type_page, flags, mnt_flags,
15061da177e4SLinus Torvalds 				      dev_name, data_page);
15071da177e4SLinus Torvalds dput_out:
15081d957f9bSJan Blunck 	path_put(&nd.path);
15091da177e4SLinus Torvalds 	return retval;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
1512741a2951SJANAK DESAI /*
1513741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
1514741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
1515741a2951SJANAK DESAI  */
1516e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
15176b3286edSKirill Korotaev 		struct fs_struct *fs)
15181da177e4SLinus Torvalds {
15196b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
15201da177e4SLinus Torvalds 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
15211da177e4SLinus Torvalds 	struct vfsmount *p, *q;
15221da177e4SLinus Torvalds 
15236b3286edSKirill Korotaev 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
15241da177e4SLinus Torvalds 	if (!new_ns)
1525467e9f4bSCedric Le Goater 		return ERR_PTR(-ENOMEM);
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	atomic_set(&new_ns->count, 1);
15281da177e4SLinus Torvalds 	INIT_LIST_HEAD(&new_ns->list);
15295addc5ddSAl Viro 	init_waitqueue_head(&new_ns->poll);
15305addc5ddSAl Viro 	new_ns->event = 0;
15311da177e4SLinus Torvalds 
1532390c6843SRam Pai 	down_write(&namespace_sem);
15331da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
15346b3286edSKirill Korotaev 	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
15359676f0c6SRam Pai 					CL_COPY_ALL | CL_EXPIRE);
15361da177e4SLinus Torvalds 	if (!new_ns->root) {
1537390c6843SRam Pai 		up_write(&namespace_sem);
15381da177e4SLinus Torvalds 		kfree(new_ns);
1539467e9f4bSCedric Le Goater 		return ERR_PTR(-ENOMEM);;
15401da177e4SLinus Torvalds 	}
15411da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
15421da177e4SLinus Torvalds 	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
15431da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	/*
15461da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
15471da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
15481da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
15491da177e4SLinus Torvalds 	 */
15506b3286edSKirill Korotaev 	p = mnt_ns->root;
15511da177e4SLinus Torvalds 	q = new_ns->root;
15521da177e4SLinus Torvalds 	while (p) {
15536b3286edSKirill Korotaev 		q->mnt_ns = new_ns;
15541da177e4SLinus Torvalds 		if (fs) {
15556ac08c39SJan Blunck 			if (p == fs->root.mnt) {
15561da177e4SLinus Torvalds 				rootmnt = p;
15576ac08c39SJan Blunck 				fs->root.mnt = mntget(q);
15581da177e4SLinus Torvalds 			}
15596ac08c39SJan Blunck 			if (p == fs->pwd.mnt) {
15601da177e4SLinus Torvalds 				pwdmnt = p;
15616ac08c39SJan Blunck 				fs->pwd.mnt = mntget(q);
15621da177e4SLinus Torvalds 			}
15636ac08c39SJan Blunck 			if (p == fs->altroot.mnt) {
15641da177e4SLinus Torvalds 				altrootmnt = p;
15656ac08c39SJan Blunck 				fs->altroot.mnt = mntget(q);
15661da177e4SLinus Torvalds 			}
15671da177e4SLinus Torvalds 		}
15686b3286edSKirill Korotaev 		p = next_mnt(p, mnt_ns->root);
15691da177e4SLinus Torvalds 		q = next_mnt(q, new_ns->root);
15701da177e4SLinus Torvalds 	}
1571390c6843SRam Pai 	up_write(&namespace_sem);
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	if (rootmnt)
15741da177e4SLinus Torvalds 		mntput(rootmnt);
15751da177e4SLinus Torvalds 	if (pwdmnt)
15761da177e4SLinus Torvalds 		mntput(pwdmnt);
15771da177e4SLinus Torvalds 	if (altrootmnt)
15781da177e4SLinus Torvalds 		mntput(altrootmnt);
15791da177e4SLinus Torvalds 
1580741a2951SJANAK DESAI 	return new_ns;
1581741a2951SJANAK DESAI }
1582741a2951SJANAK DESAI 
1583213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
1584e3222c4eSBadari Pulavarty 		struct fs_struct *new_fs)
1585741a2951SJANAK DESAI {
15866b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
1587741a2951SJANAK DESAI 
1588e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
15896b3286edSKirill Korotaev 	get_mnt_ns(ns);
1590741a2951SJANAK DESAI 
1591741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
1592e3222c4eSBadari Pulavarty 		return ns;
1593741a2951SJANAK DESAI 
1594e3222c4eSBadari Pulavarty 	new_ns = dup_mnt_ns(ns, new_fs);
1595741a2951SJANAK DESAI 
15966b3286edSKirill Korotaev 	put_mnt_ns(ns);
1597e3222c4eSBadari Pulavarty 	return new_ns;
15981da177e4SLinus Torvalds }
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
16011da177e4SLinus Torvalds 			  char __user * type, unsigned long flags,
16021da177e4SLinus Torvalds 			  void __user * data)
16031da177e4SLinus Torvalds {
16041da177e4SLinus Torvalds 	int retval;
16051da177e4SLinus Torvalds 	unsigned long data_page;
16061da177e4SLinus Torvalds 	unsigned long type_page;
16071da177e4SLinus Torvalds 	unsigned long dev_page;
16081da177e4SLinus Torvalds 	char *dir_page;
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	retval = copy_mount_options(type, &type_page);
16111da177e4SLinus Torvalds 	if (retval < 0)
16121da177e4SLinus Torvalds 		return retval;
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	dir_page = getname(dir_name);
16151da177e4SLinus Torvalds 	retval = PTR_ERR(dir_page);
16161da177e4SLinus Torvalds 	if (IS_ERR(dir_page))
16171da177e4SLinus Torvalds 		goto out1;
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds 	retval = copy_mount_options(dev_name, &dev_page);
16201da177e4SLinus Torvalds 	if (retval < 0)
16211da177e4SLinus Torvalds 		goto out2;
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	retval = copy_mount_options(data, &data_page);
16241da177e4SLinus Torvalds 	if (retval < 0)
16251da177e4SLinus Torvalds 		goto out3;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	lock_kernel();
16281da177e4SLinus Torvalds 	retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
16291da177e4SLinus Torvalds 			  flags, (void *)data_page);
16301da177e4SLinus Torvalds 	unlock_kernel();
16311da177e4SLinus Torvalds 	free_page(data_page);
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds out3:
16341da177e4SLinus Torvalds 	free_page(dev_page);
16351da177e4SLinus Torvalds out2:
16361da177e4SLinus Torvalds 	putname(dir_page);
16371da177e4SLinus Torvalds out1:
16381da177e4SLinus Torvalds 	free_page(type_page);
16391da177e4SLinus Torvalds 	return retval;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds /*
16431da177e4SLinus Torvalds  * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
16441da177e4SLinus Torvalds  * It can block. Requires the big lock held.
16451da177e4SLinus Torvalds  */
1646ac748a09SJan Blunck void set_fs_root(struct fs_struct *fs, struct path *path)
16471da177e4SLinus Torvalds {
16486ac08c39SJan Blunck 	struct path old_root;
16496ac08c39SJan Blunck 
16501da177e4SLinus Torvalds 	write_lock(&fs->lock);
16511da177e4SLinus Torvalds 	old_root = fs->root;
1652ac748a09SJan Blunck 	fs->root = *path;
1653ac748a09SJan Blunck 	path_get(path);
16541da177e4SLinus Torvalds 	write_unlock(&fs->lock);
16556ac08c39SJan Blunck 	if (old_root.dentry)
16566ac08c39SJan Blunck 		path_put(&old_root);
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds /*
16601da177e4SLinus Torvalds  * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
16611da177e4SLinus Torvalds  * It can block. Requires the big lock held.
16621da177e4SLinus Torvalds  */
1663ac748a09SJan Blunck void set_fs_pwd(struct fs_struct *fs, struct path *path)
16641da177e4SLinus Torvalds {
16656ac08c39SJan Blunck 	struct path old_pwd;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	write_lock(&fs->lock);
16681da177e4SLinus Torvalds 	old_pwd = fs->pwd;
1669ac748a09SJan Blunck 	fs->pwd = *path;
1670ac748a09SJan Blunck 	path_get(path);
16711da177e4SLinus Torvalds 	write_unlock(&fs->lock);
16721da177e4SLinus Torvalds 
16736ac08c39SJan Blunck 	if (old_pwd.dentry)
16746ac08c39SJan Blunck 		path_put(&old_pwd);
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
16771a390689SAl Viro static void chroot_fs_refs(struct path *old_root, struct path *new_root)
16781da177e4SLinus Torvalds {
16791da177e4SLinus Torvalds 	struct task_struct *g, *p;
16801da177e4SLinus Torvalds 	struct fs_struct *fs;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
16831da177e4SLinus Torvalds 	do_each_thread(g, p) {
16841da177e4SLinus Torvalds 		task_lock(p);
16851da177e4SLinus Torvalds 		fs = p->fs;
16861da177e4SLinus Torvalds 		if (fs) {
16871da177e4SLinus Torvalds 			atomic_inc(&fs->count);
16881da177e4SLinus Torvalds 			task_unlock(p);
16891a390689SAl Viro 			if (fs->root.dentry == old_root->dentry
16901a390689SAl Viro 			    && fs->root.mnt == old_root->mnt)
16911a390689SAl Viro 				set_fs_root(fs, new_root);
16921a390689SAl Viro 			if (fs->pwd.dentry == old_root->dentry
16931a390689SAl Viro 			    && fs->pwd.mnt == old_root->mnt)
16941a390689SAl Viro 				set_fs_pwd(fs, new_root);
16951da177e4SLinus Torvalds 			put_fs_struct(fs);
16961da177e4SLinus Torvalds 		} else
16971da177e4SLinus Torvalds 			task_unlock(p);
16981da177e4SLinus Torvalds 	} while_each_thread(g, p);
16991da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds /*
17031da177e4SLinus Torvalds  * pivot_root Semantics:
17041da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
17051da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
17061da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
17071da177e4SLinus Torvalds  *
17081da177e4SLinus Torvalds  * Restrictions:
17091da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
17101da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
17111da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
17121da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
17131da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
17141da177e4SLinus Torvalds  *
17154a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
17164a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
17174a0d11faSNeil Brown  * in this situation.
17184a0d11faSNeil Brown  *
17191da177e4SLinus Torvalds  * Notes:
17201da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
17211da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
17221da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
17231da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
17241da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
17251da177e4SLinus Torvalds  *    first.
17261da177e4SLinus Torvalds  */
1727b58fed8bSRam Pai asmlinkage long sys_pivot_root(const char __user * new_root,
1728b58fed8bSRam Pai 			       const char __user * put_old)
17291da177e4SLinus Torvalds {
17301da177e4SLinus Torvalds 	struct vfsmount *tmp;
17311a390689SAl Viro 	struct nameidata new_nd, old_nd, user_nd;
17321a390689SAl Viro 	struct path parent_path, root_parent;
17331da177e4SLinus Torvalds 	int error;
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
17361da177e4SLinus Torvalds 		return -EPERM;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	lock_kernel();
17391da177e4SLinus Torvalds 
1740b58fed8bSRam Pai 	error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1741b58fed8bSRam Pai 			    &new_nd);
17421da177e4SLinus Torvalds 	if (error)
17431da177e4SLinus Torvalds 		goto out0;
17441da177e4SLinus Torvalds 	error = -EINVAL;
17454ac91378SJan Blunck 	if (!check_mnt(new_nd.path.mnt))
17461da177e4SLinus Torvalds 		goto out1;
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
17491da177e4SLinus Torvalds 	if (error)
17501da177e4SLinus Torvalds 		goto out1;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	error = security_sb_pivotroot(&old_nd, &new_nd);
17531da177e4SLinus Torvalds 	if (error) {
17541d957f9bSJan Blunck 		path_put(&old_nd.path);
17551da177e4SLinus Torvalds 		goto out1;
17561da177e4SLinus Torvalds 	}
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	read_lock(&current->fs->lock);
17596ac08c39SJan Blunck 	user_nd.path = current->fs->root;
17606ac08c39SJan Blunck 	path_get(&current->fs->root);
17611da177e4SLinus Torvalds 	read_unlock(&current->fs->lock);
1762390c6843SRam Pai 	down_write(&namespace_sem);
17634ac91378SJan Blunck 	mutex_lock(&old_nd.path.dentry->d_inode->i_mutex);
17641da177e4SLinus Torvalds 	error = -EINVAL;
17654ac91378SJan Blunck 	if (IS_MNT_SHARED(old_nd.path.mnt) ||
17664ac91378SJan Blunck 		IS_MNT_SHARED(new_nd.path.mnt->mnt_parent) ||
17674ac91378SJan Blunck 		IS_MNT_SHARED(user_nd.path.mnt->mnt_parent))
176821444403SRam Pai 		goto out2;
17694ac91378SJan Blunck 	if (!check_mnt(user_nd.path.mnt))
17701da177e4SLinus Torvalds 		goto out2;
17711da177e4SLinus Torvalds 	error = -ENOENT;
17724ac91378SJan Blunck 	if (IS_DEADDIR(new_nd.path.dentry->d_inode))
17731da177e4SLinus Torvalds 		goto out2;
17744ac91378SJan Blunck 	if (d_unhashed(new_nd.path.dentry) && !IS_ROOT(new_nd.path.dentry))
17751da177e4SLinus Torvalds 		goto out2;
17764ac91378SJan Blunck 	if (d_unhashed(old_nd.path.dentry) && !IS_ROOT(old_nd.path.dentry))
17771da177e4SLinus Torvalds 		goto out2;
17781da177e4SLinus Torvalds 	error = -EBUSY;
17794ac91378SJan Blunck 	if (new_nd.path.mnt == user_nd.path.mnt ||
17804ac91378SJan Blunck 	    old_nd.path.mnt == user_nd.path.mnt)
17811da177e4SLinus Torvalds 		goto out2; /* loop, on the same file system  */
17821da177e4SLinus Torvalds 	error = -EINVAL;
17834ac91378SJan Blunck 	if (user_nd.path.mnt->mnt_root != user_nd.path.dentry)
17841da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
17854ac91378SJan Blunck 	if (user_nd.path.mnt->mnt_parent == user_nd.path.mnt)
17860bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
17874ac91378SJan Blunck 	if (new_nd.path.mnt->mnt_root != new_nd.path.dentry)
17881da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
17894ac91378SJan Blunck 	if (new_nd.path.mnt->mnt_parent == new_nd.path.mnt)
17900bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
17914ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
17924ac91378SJan Blunck 	tmp = old_nd.path.mnt;
17931da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
17944ac91378SJan Blunck 	if (tmp != new_nd.path.mnt) {
17951da177e4SLinus Torvalds 		for (;;) {
17961da177e4SLinus Torvalds 			if (tmp->mnt_parent == tmp)
17971da177e4SLinus Torvalds 				goto out3; /* already mounted on put_old */
17984ac91378SJan Blunck 			if (tmp->mnt_parent == new_nd.path.mnt)
17991da177e4SLinus Torvalds 				break;
18001da177e4SLinus Torvalds 			tmp = tmp->mnt_parent;
18011da177e4SLinus Torvalds 		}
18024ac91378SJan Blunck 		if (!is_subdir(tmp->mnt_mountpoint, new_nd.path.dentry))
18031da177e4SLinus Torvalds 			goto out3;
18044ac91378SJan Blunck 	} else if (!is_subdir(old_nd.path.dentry, new_nd.path.dentry))
18051da177e4SLinus Torvalds 		goto out3;
18061a390689SAl Viro 	detach_mnt(new_nd.path.mnt, &parent_path);
18074ac91378SJan Blunck 	detach_mnt(user_nd.path.mnt, &root_parent);
18084ac91378SJan Blunck 	/* mount old root on put_old */
18091a390689SAl Viro 	attach_mnt(user_nd.path.mnt, &old_nd.path);
18104ac91378SJan Blunck 	/* mount new_root on / */
18114ac91378SJan Blunck 	attach_mnt(new_nd.path.mnt, &root_parent);
18126b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
18131da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
18141a390689SAl Viro 	chroot_fs_refs(&user_nd.path, &new_nd.path);
18151da177e4SLinus Torvalds 	security_sb_post_pivotroot(&user_nd, &new_nd);
18161da177e4SLinus Torvalds 	error = 0;
18171a390689SAl Viro 	path_put(&root_parent);
18181a390689SAl Viro 	path_put(&parent_path);
18191da177e4SLinus Torvalds out2:
18204ac91378SJan Blunck 	mutex_unlock(&old_nd.path.dentry->d_inode->i_mutex);
1821390c6843SRam Pai 	up_write(&namespace_sem);
18221d957f9bSJan Blunck 	path_put(&user_nd.path);
18231d957f9bSJan Blunck 	path_put(&old_nd.path);
18241da177e4SLinus Torvalds out1:
18251d957f9bSJan Blunck 	path_put(&new_nd.path);
18261da177e4SLinus Torvalds out0:
18271da177e4SLinus Torvalds 	unlock_kernel();
18281da177e4SLinus Torvalds 	return error;
18291da177e4SLinus Torvalds out3:
18301da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
18311da177e4SLinus Torvalds 	goto out2;
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds static void __init init_mount_tree(void)
18351da177e4SLinus Torvalds {
18361da177e4SLinus Torvalds 	struct vfsmount *mnt;
18376b3286edSKirill Korotaev 	struct mnt_namespace *ns;
1838ac748a09SJan Blunck 	struct path root;
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
18411da177e4SLinus Torvalds 	if (IS_ERR(mnt))
18421da177e4SLinus Torvalds 		panic("Can't create rootfs");
18436b3286edSKirill Korotaev 	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
18446b3286edSKirill Korotaev 	if (!ns)
18451da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
18466b3286edSKirill Korotaev 	atomic_set(&ns->count, 1);
18476b3286edSKirill Korotaev 	INIT_LIST_HEAD(&ns->list);
18486b3286edSKirill Korotaev 	init_waitqueue_head(&ns->poll);
18496b3286edSKirill Korotaev 	ns->event = 0;
18506b3286edSKirill Korotaev 	list_add(&mnt->mnt_list, &ns->list);
18516b3286edSKirill Korotaev 	ns->root = mnt;
18526b3286edSKirill Korotaev 	mnt->mnt_ns = ns;
18531da177e4SLinus Torvalds 
18546b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
18556b3286edSKirill Korotaev 	get_mnt_ns(ns);
18561da177e4SLinus Torvalds 
1857ac748a09SJan Blunck 	root.mnt = ns->root;
1858ac748a09SJan Blunck 	root.dentry = ns->root->mnt_root;
1859ac748a09SJan Blunck 
1860ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
1861ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds 
186474bf17cfSDenis Cheng void __init mnt_init(void)
18651da177e4SLinus Torvalds {
186613f14b4dSEric Dumazet 	unsigned u;
186715a67dd8SRandy Dunlap 	int err;
18681da177e4SLinus Torvalds 
1869390c6843SRam Pai 	init_rwsem(&namespace_sem);
1870390c6843SRam Pai 
18711da177e4SLinus Torvalds 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
187220c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
18731da177e4SLinus Torvalds 
1874b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	if (!mount_hashtable)
18771da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
18781da177e4SLinus Torvalds 
187913f14b4dSEric Dumazet 	printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
18801da177e4SLinus Torvalds 
188113f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
188213f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
18831da177e4SLinus Torvalds 
188415a67dd8SRandy Dunlap 	err = sysfs_init();
188515a67dd8SRandy Dunlap 	if (err)
188615a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
188715a67dd8SRandy Dunlap 			__FUNCTION__, err);
188800d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
188900d26666SGreg Kroah-Hartman 	if (!fs_kobj)
189000d26666SGreg Kroah-Hartman 		printk(KERN_WARNING "%s: kobj create error\n", __FUNCTION__);
18911da177e4SLinus Torvalds 	init_rootfs();
18921da177e4SLinus Torvalds 	init_mount_tree();
18931da177e4SLinus Torvalds }
18941da177e4SLinus Torvalds 
18956b3286edSKirill Korotaev void __put_mnt_ns(struct mnt_namespace *ns)
18961da177e4SLinus Torvalds {
18976b3286edSKirill Korotaev 	struct vfsmount *root = ns->root;
189870fbcdf4SRam Pai 	LIST_HEAD(umount_list);
18996b3286edSKirill Korotaev 	ns->root = NULL;
19001ce88cf4SMiklos Szeredi 	spin_unlock(&vfsmount_lock);
1901390c6843SRam Pai 	down_write(&namespace_sem);
19021da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
1903a05964f3SRam Pai 	umount_tree(root, 0, &umount_list);
19041da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
1905390c6843SRam Pai 	up_write(&namespace_sem);
190670fbcdf4SRam Pai 	release_mounts(&umount_list);
19076b3286edSKirill Korotaev 	kfree(ns);
19081da177e4SLinus Torvalds }
1909