xref: /openbmc/linux/fs/namespace.c (revision c35038be)
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 
83454e2398SDavid Howells int simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
84454e2398SDavid Howells {
85454e2398SDavid Howells 	mnt->mnt_sb = sb;
86454e2398SDavid Howells 	mnt->mnt_root = dget(sb->s_root);
87454e2398SDavid Howells 	return 0;
88454e2398SDavid Howells }
89454e2398SDavid Howells 
90454e2398SDavid Howells EXPORT_SYMBOL(simple_set_mnt);
91454e2398SDavid Howells 
921da177e4SLinus Torvalds void free_vfsmnt(struct vfsmount *mnt)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	kfree(mnt->mnt_devname);
951da177e4SLinus Torvalds 	kmem_cache_free(mnt_cache, mnt);
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
99a05964f3SRam Pai  * find the first or last mount at @dentry on vfsmount @mnt depending on
100a05964f3SRam Pai  * @dir. If @dir is set return the first mount else return the last mount.
1011da177e4SLinus Torvalds  */
102a05964f3SRam Pai struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
103a05964f3SRam Pai 			      int dir)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	struct list_head *head = mount_hashtable + hash(mnt, dentry);
1061da177e4SLinus Torvalds 	struct list_head *tmp = head;
1071da177e4SLinus Torvalds 	struct vfsmount *p, *found = NULL;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	for (;;) {
110a05964f3SRam Pai 		tmp = dir ? tmp->next : tmp->prev;
1111da177e4SLinus Torvalds 		p = NULL;
1121da177e4SLinus Torvalds 		if (tmp == head)
1131da177e4SLinus Torvalds 			break;
1141da177e4SLinus Torvalds 		p = list_entry(tmp, struct vfsmount, mnt_hash);
1151da177e4SLinus Torvalds 		if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
116a05964f3SRam Pai 			found = p;
1171da177e4SLinus Torvalds 			break;
1181da177e4SLinus Torvalds 		}
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds 	return found;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
123a05964f3SRam Pai /*
124a05964f3SRam Pai  * lookup_mnt increments the ref count before returning
125a05964f3SRam Pai  * the vfsmount struct.
126a05964f3SRam Pai  */
127a05964f3SRam Pai struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
128a05964f3SRam Pai {
129a05964f3SRam Pai 	struct vfsmount *child_mnt;
130a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
131a05964f3SRam Pai 	if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
132a05964f3SRam Pai 		mntget(child_mnt);
133a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
134a05964f3SRam Pai 	return child_mnt;
135a05964f3SRam Pai }
136a05964f3SRam Pai 
1371da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt)
1381da177e4SLinus Torvalds {
1396b3286edSKirill Korotaev 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1426b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns)
1435addc5ddSAl Viro {
1445addc5ddSAl Viro 	if (ns) {
1455addc5ddSAl Viro 		ns->event = ++event;
1465addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
1475addc5ddSAl Viro 	}
1485addc5ddSAl Viro }
1495addc5ddSAl Viro 
1506b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns)
1515addc5ddSAl Viro {
1525addc5ddSAl Viro 	if (ns && ns->event != event) {
1535addc5ddSAl Viro 		ns->event = event;
1545addc5ddSAl Viro 		wake_up_interruptible(&ns->poll);
1555addc5ddSAl Viro 	}
1565addc5ddSAl Viro }
1575addc5ddSAl Viro 
1581a390689SAl Viro static void detach_mnt(struct vfsmount *mnt, struct path *old_path)
1591da177e4SLinus Torvalds {
1601a390689SAl Viro 	old_path->dentry = mnt->mnt_mountpoint;
1611a390689SAl Viro 	old_path->mnt = mnt->mnt_parent;
1621da177e4SLinus Torvalds 	mnt->mnt_parent = mnt;
1631da177e4SLinus Torvalds 	mnt->mnt_mountpoint = mnt->mnt_root;
1641da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_child);
1651da177e4SLinus Torvalds 	list_del_init(&mnt->mnt_hash);
1661a390689SAl Viro 	old_path->dentry->d_mounted--;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
169b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
170b90fa9aeSRam Pai 			struct vfsmount *child_mnt)
171b90fa9aeSRam Pai {
172b90fa9aeSRam Pai 	child_mnt->mnt_parent = mntget(mnt);
173b90fa9aeSRam Pai 	child_mnt->mnt_mountpoint = dget(dentry);
174b90fa9aeSRam Pai 	dentry->d_mounted++;
175b90fa9aeSRam Pai }
176b90fa9aeSRam Pai 
1771a390689SAl Viro static void attach_mnt(struct vfsmount *mnt, struct path *path)
1781da177e4SLinus Torvalds {
1791a390689SAl Viro 	mnt_set_mountpoint(path->mnt, path->dentry, mnt);
180b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
1811a390689SAl Viro 			hash(path->mnt, path->dentry));
1821a390689SAl Viro 	list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts);
183b90fa9aeSRam Pai }
184b90fa9aeSRam Pai 
185b90fa9aeSRam Pai /*
186b90fa9aeSRam Pai  * the caller must hold vfsmount_lock
187b90fa9aeSRam Pai  */
188b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt)
189b90fa9aeSRam Pai {
190b90fa9aeSRam Pai 	struct vfsmount *parent = mnt->mnt_parent;
191b90fa9aeSRam Pai 	struct vfsmount *m;
192b90fa9aeSRam Pai 	LIST_HEAD(head);
1936b3286edSKirill Korotaev 	struct mnt_namespace *n = parent->mnt_ns;
194b90fa9aeSRam Pai 
195b90fa9aeSRam Pai 	BUG_ON(parent == mnt);
196b90fa9aeSRam Pai 
197b90fa9aeSRam Pai 	list_add_tail(&head, &mnt->mnt_list);
198b90fa9aeSRam Pai 	list_for_each_entry(m, &head, mnt_list)
1996b3286edSKirill Korotaev 		m->mnt_ns = n;
200b90fa9aeSRam Pai 	list_splice(&head, n->list.prev);
201b90fa9aeSRam Pai 
202b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_hash, mount_hashtable +
203b90fa9aeSRam Pai 				hash(parent, mnt->mnt_mountpoint));
204b90fa9aeSRam Pai 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
2056b3286edSKirill Korotaev 	touch_mnt_namespace(n);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	struct list_head *next = p->mnt_mounts.next;
2111da177e4SLinus Torvalds 	if (next == &p->mnt_mounts) {
2121da177e4SLinus Torvalds 		while (1) {
2131da177e4SLinus Torvalds 			if (p == root)
2141da177e4SLinus Torvalds 				return NULL;
2151da177e4SLinus Torvalds 			next = p->mnt_child.next;
2161da177e4SLinus Torvalds 			if (next != &p->mnt_parent->mnt_mounts)
2171da177e4SLinus Torvalds 				break;
2181da177e4SLinus Torvalds 			p = p->mnt_parent;
2191da177e4SLinus Torvalds 		}
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 	return list_entry(next, struct vfsmount, mnt_child);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2249676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
2259676f0c6SRam Pai {
2269676f0c6SRam Pai 	struct list_head *prev = p->mnt_mounts.prev;
2279676f0c6SRam Pai 	while (prev != &p->mnt_mounts) {
2289676f0c6SRam Pai 		p = list_entry(prev, struct vfsmount, mnt_child);
2299676f0c6SRam Pai 		prev = p->mnt_mounts.prev;
2309676f0c6SRam Pai 	}
2319676f0c6SRam Pai 	return p;
2329676f0c6SRam Pai }
2339676f0c6SRam Pai 
23436341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
23536341f64SRam Pai 					int flag)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct super_block *sb = old->mnt_sb;
2381da177e4SLinus Torvalds 	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (mnt) {
2411da177e4SLinus Torvalds 		mnt->mnt_flags = old->mnt_flags;
2421da177e4SLinus Torvalds 		atomic_inc(&sb->s_active);
2431da177e4SLinus Torvalds 		mnt->mnt_sb = sb;
2441da177e4SLinus Torvalds 		mnt->mnt_root = dget(root);
2451da177e4SLinus Torvalds 		mnt->mnt_mountpoint = mnt->mnt_root;
2461da177e4SLinus Torvalds 		mnt->mnt_parent = mnt;
247b90fa9aeSRam Pai 
2485afe0022SRam Pai 		if (flag & CL_SLAVE) {
2495afe0022SRam Pai 			list_add(&mnt->mnt_slave, &old->mnt_slave_list);
2505afe0022SRam Pai 			mnt->mnt_master = old;
2515afe0022SRam Pai 			CLEAR_MNT_SHARED(mnt);
2528aec0809SAl Viro 		} else if (!(flag & CL_PRIVATE)) {
253b90fa9aeSRam Pai 			if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
254b90fa9aeSRam Pai 				list_add(&mnt->mnt_share, &old->mnt_share);
2555afe0022SRam Pai 			if (IS_MNT_SLAVE(old))
2565afe0022SRam Pai 				list_add(&mnt->mnt_slave, &old->mnt_slave);
2575afe0022SRam Pai 			mnt->mnt_master = old->mnt_master;
2585afe0022SRam Pai 		}
259b90fa9aeSRam Pai 		if (flag & CL_MAKE_SHARED)
260b90fa9aeSRam Pai 			set_mnt_shared(mnt);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 		/* stick the duplicate mount on the same expiry list
2631da177e4SLinus Torvalds 		 * as the original if that was on one */
26436341f64SRam Pai 		if (flag & CL_EXPIRE) {
2651da177e4SLinus Torvalds 			spin_lock(&vfsmount_lock);
26655e700b9SMiklos Szeredi 			if (!list_empty(&old->mnt_expire))
26755e700b9SMiklos Szeredi 				list_add(&mnt->mnt_expire, &old->mnt_expire);
2681da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
2691da177e4SLinus Torvalds 		}
27036341f64SRam Pai 	}
2711da177e4SLinus Torvalds 	return mnt;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2747b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
2771da177e4SLinus Torvalds 	dput(mnt->mnt_root);
2781da177e4SLinus Torvalds 	free_vfsmnt(mnt);
2791da177e4SLinus Torvalds 	deactivate_super(sb);
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2827b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt)
2837b7b1aceSAl Viro {
2847b7b1aceSAl Viro repeat:
2857b7b1aceSAl Viro 	if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
2867b7b1aceSAl Viro 		if (likely(!mnt->mnt_pinned)) {
2877b7b1aceSAl Viro 			spin_unlock(&vfsmount_lock);
2887b7b1aceSAl Viro 			__mntput(mnt);
2897b7b1aceSAl Viro 			return;
2907b7b1aceSAl Viro 		}
2917b7b1aceSAl Viro 		atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
2927b7b1aceSAl Viro 		mnt->mnt_pinned = 0;
2937b7b1aceSAl Viro 		spin_unlock(&vfsmount_lock);
2947b7b1aceSAl Viro 		acct_auto_close_mnt(mnt);
2957b7b1aceSAl Viro 		security_sb_umount_close(mnt);
2967b7b1aceSAl Viro 		goto repeat;
2977b7b1aceSAl Viro 	}
2987b7b1aceSAl Viro }
2997b7b1aceSAl Viro 
3007b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire);
3017b7b1aceSAl Viro 
3027b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
3037b7b1aceSAl Viro {
3047b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
3057b7b1aceSAl Viro 	mnt->mnt_pinned++;
3067b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
3077b7b1aceSAl Viro }
3087b7b1aceSAl Viro 
3097b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
3107b7b1aceSAl Viro 
3117b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt)
3127b7b1aceSAl Viro {
3137b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
3147b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
3157b7b1aceSAl Viro 		atomic_inc(&mnt->mnt_count);
3167b7b1aceSAl Viro 		mnt->mnt_pinned--;
3177b7b1aceSAl Viro 	}
3187b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
3197b7b1aceSAl Viro }
3207b7b1aceSAl Viro 
3217b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
3221da177e4SLinus Torvalds 
323b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
324b3b304a2SMiklos Szeredi {
325b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
326b3b304a2SMiklos Szeredi }
327b3b304a2SMiklos Szeredi 
328b3b304a2SMiklos Szeredi /*
329b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
330b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
331b3b304a2SMiklos Szeredi  *
332b3b304a2SMiklos Szeredi  * See also save_mount_options().
333b3b304a2SMiklos Szeredi  */
334b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
335b3b304a2SMiklos Szeredi {
336b3b304a2SMiklos Szeredi 	const char *options = mnt->mnt_sb->s_options;
337b3b304a2SMiklos Szeredi 
338b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
339b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
340b3b304a2SMiklos Szeredi 		mangle(m, options);
341b3b304a2SMiklos Szeredi 	}
342b3b304a2SMiklos Szeredi 
343b3b304a2SMiklos Szeredi 	return 0;
344b3b304a2SMiklos Szeredi }
345b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
346b3b304a2SMiklos Szeredi 
347b3b304a2SMiklos Szeredi /*
348b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
349b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
350b3b304a2SMiklos Szeredi  *
351b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
352b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
353b3b304a2SMiklos Szeredi  * remount fails.
354b3b304a2SMiklos Szeredi  *
355b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
356b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
357b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
358b3b304a2SMiklos Szeredi  * any more.
359b3b304a2SMiklos Szeredi  */
360b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
361b3b304a2SMiklos Szeredi {
362b3b304a2SMiklos Szeredi 	kfree(sb->s_options);
363b3b304a2SMiklos Szeredi 	sb->s_options = kstrdup(options, GFP_KERNEL);
364b3b304a2SMiklos Szeredi }
365b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
366b3b304a2SMiklos Szeredi 
3671da177e4SLinus Torvalds /* iterator */
3681da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
3691da177e4SLinus Torvalds {
3706b3286edSKirill Korotaev 	struct mnt_namespace *n = m->private;
3711da177e4SLinus Torvalds 
372390c6843SRam Pai 	down_read(&namespace_sem);
373b0765fb8SPavel Emelianov 	return seq_list_start(&n->list, *pos);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
3771da177e4SLinus Torvalds {
3786b3286edSKirill Korotaev 	struct mnt_namespace *n = m->private;
379b0765fb8SPavel Emelianov 
380b0765fb8SPavel Emelianov 	return seq_list_next(v, &n->list, pos);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
3841da177e4SLinus Torvalds {
385390c6843SRam Pai 	up_read(&namespace_sem);
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds static int show_vfsmnt(struct seq_file *m, void *v)
3891da177e4SLinus Torvalds {
390b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
3911da177e4SLinus Torvalds 	int err = 0;
3921da177e4SLinus Torvalds 	static struct proc_fs_info {
3931da177e4SLinus Torvalds 		int flag;
3941da177e4SLinus Torvalds 		char *str;
3951da177e4SLinus Torvalds 	} fs_info[] = {
3961da177e4SLinus Torvalds 		{ MS_SYNCHRONOUS, ",sync" },
3971da177e4SLinus Torvalds 		{ MS_DIRSYNC, ",dirsync" },
3981da177e4SLinus Torvalds 		{ MS_MANDLOCK, ",mand" },
3991da177e4SLinus Torvalds 		{ 0, NULL }
4001da177e4SLinus Torvalds 	};
4011da177e4SLinus Torvalds 	static struct proc_fs_info mnt_info[] = {
4021da177e4SLinus Torvalds 		{ MNT_NOSUID, ",nosuid" },
4031da177e4SLinus Torvalds 		{ MNT_NODEV, ",nodev" },
4041da177e4SLinus Torvalds 		{ MNT_NOEXEC, ",noexec" },
405fc33a7bbSChristoph Hellwig 		{ MNT_NOATIME, ",noatime" },
406fc33a7bbSChristoph Hellwig 		{ MNT_NODIRATIME, ",nodiratime" },
40747ae32d6SValerie Henson 		{ MNT_RELATIME, ",relatime" },
4081da177e4SLinus Torvalds 		{ 0, NULL }
4091da177e4SLinus Torvalds 	};
4101da177e4SLinus Torvalds 	struct proc_fs_info *fs_infop;
411c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
4141da177e4SLinus Torvalds 	seq_putc(m, ' ');
415c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
4161da177e4SLinus Torvalds 	seq_putc(m, ' ');
4171da177e4SLinus Torvalds 	mangle(m, mnt->mnt_sb->s_type->name);
41879c0b2dfSMiklos Szeredi 	if (mnt->mnt_sb->s_subtype && mnt->mnt_sb->s_subtype[0]) {
41979c0b2dfSMiklos Szeredi 		seq_putc(m, '.');
42079c0b2dfSMiklos Szeredi 		mangle(m, mnt->mnt_sb->s_subtype);
42179c0b2dfSMiklos Szeredi 	}
4221da177e4SLinus Torvalds 	seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
4231da177e4SLinus Torvalds 	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
4241da177e4SLinus Torvalds 		if (mnt->mnt_sb->s_flags & fs_infop->flag)
4251da177e4SLinus Torvalds 			seq_puts(m, fs_infop->str);
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
4281da177e4SLinus Torvalds 		if (mnt->mnt_flags & fs_infop->flag)
4291da177e4SLinus Torvalds 			seq_puts(m, fs_infop->str);
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_op->show_options)
4321da177e4SLinus Torvalds 		err = mnt->mnt_sb->s_op->show_options(m, mnt);
4331da177e4SLinus Torvalds 	seq_puts(m, " 0 0\n");
4341da177e4SLinus Torvalds 	return err;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds struct seq_operations mounts_op = {
4381da177e4SLinus Torvalds 	.start	= m_start,
4391da177e4SLinus Torvalds 	.next	= m_next,
4401da177e4SLinus Torvalds 	.stop	= m_stop,
4411da177e4SLinus Torvalds 	.show	= show_vfsmnt
4421da177e4SLinus Torvalds };
4431da177e4SLinus Torvalds 
444b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v)
445b4629fe2SChuck Lever {
446b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
447c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
448b4629fe2SChuck Lever 	int err = 0;
449b4629fe2SChuck Lever 
450b4629fe2SChuck Lever 	/* device */
451b4629fe2SChuck Lever 	if (mnt->mnt_devname) {
452b4629fe2SChuck Lever 		seq_puts(m, "device ");
453b4629fe2SChuck Lever 		mangle(m, mnt->mnt_devname);
454b4629fe2SChuck Lever 	} else
455b4629fe2SChuck Lever 		seq_puts(m, "no device");
456b4629fe2SChuck Lever 
457b4629fe2SChuck Lever 	/* mount point */
458b4629fe2SChuck Lever 	seq_puts(m, " mounted on ");
459c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
460b4629fe2SChuck Lever 	seq_putc(m, ' ');
461b4629fe2SChuck Lever 
462b4629fe2SChuck Lever 	/* file system type */
463b4629fe2SChuck Lever 	seq_puts(m, "with fstype ");
464b4629fe2SChuck Lever 	mangle(m, mnt->mnt_sb->s_type->name);
465b4629fe2SChuck Lever 
466b4629fe2SChuck Lever 	/* optional statistics */
467b4629fe2SChuck Lever 	if (mnt->mnt_sb->s_op->show_stats) {
468b4629fe2SChuck Lever 		seq_putc(m, ' ');
469b4629fe2SChuck Lever 		err = mnt->mnt_sb->s_op->show_stats(m, mnt);
470b4629fe2SChuck Lever 	}
471b4629fe2SChuck Lever 
472b4629fe2SChuck Lever 	seq_putc(m, '\n');
473b4629fe2SChuck Lever 	return err;
474b4629fe2SChuck Lever }
475b4629fe2SChuck Lever 
476b4629fe2SChuck Lever struct seq_operations mountstats_op = {
477b4629fe2SChuck Lever 	.start	= m_start,
478b4629fe2SChuck Lever 	.next	= m_next,
479b4629fe2SChuck Lever 	.stop	= m_stop,
480b4629fe2SChuck Lever 	.show	= show_vfsstat,
481b4629fe2SChuck Lever };
482b4629fe2SChuck Lever 
4831da177e4SLinus Torvalds /**
4841da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
4851da177e4SLinus Torvalds  * @mnt: root of mount tree
4861da177e4SLinus Torvalds  *
4871da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
4881da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
4891da177e4SLinus Torvalds  * busy.
4901da177e4SLinus Torvalds  */
4911da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt)
4921da177e4SLinus Torvalds {
49336341f64SRam Pai 	int actual_refs = 0;
49436341f64SRam Pai 	int minimum_refs = 0;
49536341f64SRam Pai 	struct vfsmount *p;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
49836341f64SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
4991da177e4SLinus Torvalds 		actual_refs += atomic_read(&p->mnt_count);
5001da177e4SLinus Torvalds 		minimum_refs += 2;
5011da177e4SLinus Torvalds 	}
5021da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
5051da177e4SLinus Torvalds 		return 0;
506e3474a8eSIan Kent 
507e3474a8eSIan Kent 	return 1;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds /**
5131da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
5141da177e4SLinus Torvalds  * @mnt: root of mount
5151da177e4SLinus Torvalds  *
5161da177e4SLinus Torvalds  * This is called to check if a mount point has any
5171da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
5181da177e4SLinus Torvalds  * mount has sub mounts this will return busy
5191da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
5201da177e4SLinus Torvalds  *
5211da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
5221da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
5231da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
5241da177e4SLinus Torvalds  */
5251da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
5261da177e4SLinus Torvalds {
527e3474a8eSIan Kent 	int ret = 1;
528a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
529a05964f3SRam Pai 	if (propagate_mount_busy(mnt, 2))
530e3474a8eSIan Kent 		ret = 0;
531a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
532a05964f3SRam Pai 	return ret;
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
5361da177e4SLinus Torvalds 
537b90fa9aeSRam Pai void release_mounts(struct list_head *head)
5381da177e4SLinus Torvalds {
53970fbcdf4SRam Pai 	struct vfsmount *mnt;
54070fbcdf4SRam Pai 	while (!list_empty(head)) {
541b5e61818SPavel Emelianov 		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
54270fbcdf4SRam Pai 		list_del_init(&mnt->mnt_hash);
54370fbcdf4SRam Pai 		if (mnt->mnt_parent != mnt) {
54470fbcdf4SRam Pai 			struct dentry *dentry;
54570fbcdf4SRam Pai 			struct vfsmount *m;
54670fbcdf4SRam Pai 			spin_lock(&vfsmount_lock);
54770fbcdf4SRam Pai 			dentry = mnt->mnt_mountpoint;
54870fbcdf4SRam Pai 			m = mnt->mnt_parent;
54970fbcdf4SRam Pai 			mnt->mnt_mountpoint = mnt->mnt_root;
55070fbcdf4SRam Pai 			mnt->mnt_parent = mnt;
5517c4b93d8SAl Viro 			m->mnt_ghosts--;
5521da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
55370fbcdf4SRam Pai 			dput(dentry);
55470fbcdf4SRam Pai 			mntput(m);
5551da177e4SLinus Torvalds 		}
5561da177e4SLinus Torvalds 		mntput(mnt);
55770fbcdf4SRam Pai 	}
55870fbcdf4SRam Pai }
55970fbcdf4SRam Pai 
560a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
56170fbcdf4SRam Pai {
56270fbcdf4SRam Pai 	struct vfsmount *p;
56370fbcdf4SRam Pai 
5641bfba4e8SAkinobu Mita 	for (p = mnt; p; p = next_mnt(p, mnt))
5651bfba4e8SAkinobu Mita 		list_move(&p->mnt_hash, kill);
56670fbcdf4SRam Pai 
567a05964f3SRam Pai 	if (propagate)
568a05964f3SRam Pai 		propagate_umount(kill);
569a05964f3SRam Pai 
57070fbcdf4SRam Pai 	list_for_each_entry(p, kill, mnt_hash) {
57170fbcdf4SRam Pai 		list_del_init(&p->mnt_expire);
57270fbcdf4SRam Pai 		list_del_init(&p->mnt_list);
5736b3286edSKirill Korotaev 		__touch_mnt_namespace(p->mnt_ns);
5746b3286edSKirill Korotaev 		p->mnt_ns = NULL;
57570fbcdf4SRam Pai 		list_del_init(&p->mnt_child);
5767c4b93d8SAl Viro 		if (p->mnt_parent != p) {
5777c4b93d8SAl Viro 			p->mnt_parent->mnt_ghosts++;
578f30ac319SAl Viro 			p->mnt_mountpoint->d_mounted--;
5797c4b93d8SAl Viro 		}
580a05964f3SRam Pai 		change_mnt_propagation(p, MS_PRIVATE);
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
584c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
585c35038beSAl Viro 
5861da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
5891da177e4SLinus Torvalds 	int retval;
59070fbcdf4SRam Pai 	LIST_HEAD(umount_list);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	retval = security_sb_umount(mnt, flags);
5931da177e4SLinus Torvalds 	if (retval)
5941da177e4SLinus Torvalds 		return retval;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	/*
5971da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
5981da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
5991da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
6001da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
6011da177e4SLinus Torvalds 	 */
6021da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
6036ac08c39SJan Blunck 		if (mnt == current->fs->root.mnt ||
6041da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
6051da177e4SLinus Torvalds 			return -EINVAL;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 		if (atomic_read(&mnt->mnt_count) != 2)
6081da177e4SLinus Torvalds 			return -EBUSY;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1))
6111da177e4SLinus Torvalds 			return -EAGAIN;
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	/*
6151da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
6161da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
6171da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
6181da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
6191da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
6201da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
6211da177e4SLinus Torvalds 	 * about for the moment.
6221da177e4SLinus Torvalds 	 */
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	lock_kernel();
6258b512d9aSTrond Myklebust 	if (sb->s_op->umount_begin)
6268b512d9aSTrond Myklebust 		sb->s_op->umount_begin(mnt, flags);
6271da177e4SLinus Torvalds 	unlock_kernel();
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	/*
6301da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
6311da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
6321da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
6331da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
6341da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
6351da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
6361da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
6371da177e4SLinus Torvalds 	 */
6386ac08c39SJan Blunck 	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
6391da177e4SLinus Torvalds 		/*
6401da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
6411da177e4SLinus Torvalds 		 * we just try to remount it readonly.
6421da177e4SLinus Torvalds 		 */
6431da177e4SLinus Torvalds 		down_write(&sb->s_umount);
6441da177e4SLinus Torvalds 		if (!(sb->s_flags & MS_RDONLY)) {
6451da177e4SLinus Torvalds 			lock_kernel();
6461da177e4SLinus Torvalds 			DQUOT_OFF(sb);
6471da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
6481da177e4SLinus Torvalds 			unlock_kernel();
6491da177e4SLinus Torvalds 		}
6501da177e4SLinus Torvalds 		up_write(&sb->s_umount);
6511da177e4SLinus Torvalds 		return retval;
6521da177e4SLinus Torvalds 	}
6531da177e4SLinus Torvalds 
654390c6843SRam Pai 	down_write(&namespace_sem);
6551da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
6565addc5ddSAl Viro 	event++;
6571da177e4SLinus Torvalds 
658c35038beSAl Viro 	if (!(flags & MNT_DETACH))
659c35038beSAl Viro 		shrink_submounts(mnt, &umount_list);
660c35038beSAl Viro 
6611da177e4SLinus Torvalds 	retval = -EBUSY;
662a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
6631da177e4SLinus Torvalds 		if (!list_empty(&mnt->mnt_list))
664a05964f3SRam Pai 			umount_tree(mnt, 1, &umount_list);
6651da177e4SLinus Torvalds 		retval = 0;
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
6681da177e4SLinus Torvalds 	if (retval)
6691da177e4SLinus Torvalds 		security_sb_umount_busy(mnt);
670390c6843SRam Pai 	up_write(&namespace_sem);
67170fbcdf4SRam Pai 	release_mounts(&umount_list);
6721da177e4SLinus Torvalds 	return retval;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds /*
6761da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
6771da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
6781da177e4SLinus Torvalds  *
6791da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
6801da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
6811da177e4SLinus Torvalds  */
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds asmlinkage long sys_umount(char __user * name, int flags)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	struct nameidata nd;
6861da177e4SLinus Torvalds 	int retval;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
6891da177e4SLinus Torvalds 	if (retval)
6901da177e4SLinus Torvalds 		goto out;
6911da177e4SLinus Torvalds 	retval = -EINVAL;
6924ac91378SJan Blunck 	if (nd.path.dentry != nd.path.mnt->mnt_root)
6931da177e4SLinus Torvalds 		goto dput_and_out;
6944ac91378SJan Blunck 	if (!check_mnt(nd.path.mnt))
6951da177e4SLinus Torvalds 		goto dput_and_out;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	retval = -EPERM;
6981da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
6991da177e4SLinus Torvalds 		goto dput_and_out;
7001da177e4SLinus Torvalds 
7014ac91378SJan Blunck 	retval = do_umount(nd.path.mnt, flags);
7021da177e4SLinus Torvalds dput_and_out:
703429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
7044ac91378SJan Blunck 	dput(nd.path.dentry);
7054ac91378SJan Blunck 	mntput_no_expire(nd.path.mnt);
7061da177e4SLinus Torvalds out:
7071da177e4SLinus Torvalds 	return retval;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds /*
7131da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
7141da177e4SLinus Torvalds  */
7151da177e4SLinus Torvalds asmlinkage long sys_oldumount(char __user * name)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	return sys_umount(name, 0);
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds #endif
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds static int mount_is_safe(struct nameidata *nd)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	if (capable(CAP_SYS_ADMIN))
7251da177e4SLinus Torvalds 		return 0;
7261da177e4SLinus Torvalds 	return -EPERM;
7271da177e4SLinus Torvalds #ifdef notyet
7284ac91378SJan Blunck 	if (S_ISLNK(nd->path.dentry->d_inode->i_mode))
7291da177e4SLinus Torvalds 		return -EPERM;
7304ac91378SJan Blunck 	if (nd->path.dentry->d_inode->i_mode & S_ISVTX) {
7314ac91378SJan Blunck 		if (current->uid != nd->path.dentry->d_inode->i_uid)
7321da177e4SLinus Torvalds 			return -EPERM;
7331da177e4SLinus Torvalds 	}
734e4543eddSChristoph Hellwig 	if (vfs_permission(nd, MAY_WRITE))
7351da177e4SLinus Torvalds 		return -EPERM;
7361da177e4SLinus Torvalds 	return 0;
7371da177e4SLinus Torvalds #endif
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
740b58fed8bSRam Pai static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	while (1) {
7431da177e4SLinus Torvalds 		if (d == dentry)
7441da177e4SLinus Torvalds 			return 1;
7451da177e4SLinus Torvalds 		if (d == NULL || d == d->d_parent)
7461da177e4SLinus Torvalds 			return 0;
7471da177e4SLinus Torvalds 		d = d->d_parent;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
751b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
75236341f64SRam Pai 					int flag)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	struct vfsmount *res, *p, *q, *r, *s;
7551a390689SAl Viro 	struct path path;
7561da177e4SLinus Torvalds 
7579676f0c6SRam Pai 	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
7589676f0c6SRam Pai 		return NULL;
7599676f0c6SRam Pai 
76036341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
7611da177e4SLinus Torvalds 	if (!q)
7621da177e4SLinus Torvalds 		goto Enomem;
7631da177e4SLinus Torvalds 	q->mnt_mountpoint = mnt->mnt_mountpoint;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	p = mnt;
766fdadd65fSDomen Puncer 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
7671da177e4SLinus Torvalds 		if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
7681da177e4SLinus Torvalds 			continue;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 		for (s = r; s; s = next_mnt(s, r)) {
7719676f0c6SRam Pai 			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
7729676f0c6SRam Pai 				s = skip_mnt_tree(s);
7739676f0c6SRam Pai 				continue;
7749676f0c6SRam Pai 			}
7751da177e4SLinus Torvalds 			while (p != s->mnt_parent) {
7761da177e4SLinus Torvalds 				p = p->mnt_parent;
7771da177e4SLinus Torvalds 				q = q->mnt_parent;
7781da177e4SLinus Torvalds 			}
7791da177e4SLinus Torvalds 			p = s;
7801a390689SAl Viro 			path.mnt = q;
7811a390689SAl Viro 			path.dentry = p->mnt_mountpoint;
78236341f64SRam Pai 			q = clone_mnt(p, p->mnt_root, flag);
7831da177e4SLinus Torvalds 			if (!q)
7841da177e4SLinus Torvalds 				goto Enomem;
7851da177e4SLinus Torvalds 			spin_lock(&vfsmount_lock);
7861da177e4SLinus Torvalds 			list_add_tail(&q->mnt_list, &res->mnt_list);
7871a390689SAl Viro 			attach_mnt(q, &path);
7881da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
7891da177e4SLinus Torvalds 		}
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 	return res;
7921da177e4SLinus Torvalds Enomem:
7931da177e4SLinus Torvalds 	if (res) {
79470fbcdf4SRam Pai 		LIST_HEAD(umount_list);
7951da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
796a05964f3SRam Pai 		umount_tree(res, 0, &umount_list);
7971da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
79870fbcdf4SRam Pai 		release_mounts(&umount_list);
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 	return NULL;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8038aec0809SAl Viro struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry)
8048aec0809SAl Viro {
8058aec0809SAl Viro 	struct vfsmount *tree;
8068aec0809SAl Viro 	down_read(&namespace_sem);
8078aec0809SAl Viro 	tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE);
8088aec0809SAl Viro 	up_read(&namespace_sem);
8098aec0809SAl Viro 	return tree;
8108aec0809SAl Viro }
8118aec0809SAl Viro 
8128aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
8138aec0809SAl Viro {
8148aec0809SAl Viro 	LIST_HEAD(umount_list);
8158aec0809SAl Viro 	down_read(&namespace_sem);
8168aec0809SAl Viro 	spin_lock(&vfsmount_lock);
8178aec0809SAl Viro 	umount_tree(mnt, 0, &umount_list);
8188aec0809SAl Viro 	spin_unlock(&vfsmount_lock);
8198aec0809SAl Viro 	up_read(&namespace_sem);
8208aec0809SAl Viro 	release_mounts(&umount_list);
8218aec0809SAl Viro }
8228aec0809SAl Viro 
823b90fa9aeSRam Pai /*
824b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
825b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
82621444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
82721444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
82821444403SRam Pai  *  		   (done when source_mnt is moved)
829b90fa9aeSRam Pai  *
830b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
831b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
8329676f0c6SRam Pai  * ---------------------------------------------------------------------------
833b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
8349676f0c6SRam Pai  * |**************************************************************************
8359676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
8369676f0c6SRam Pai  * | dest     |               |                |                |            |
8379676f0c6SRam Pai  * |   |      |               |                |                |            |
8389676f0c6SRam Pai  * |   v      |               |                |                |            |
8399676f0c6SRam Pai  * |**************************************************************************
8409676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
8415afe0022SRam Pai  * |          |               |                |                |            |
8429676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
8439676f0c6SRam Pai  * ***************************************************************************
844b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
845b90fa9aeSRam Pai  * destination mount.
846b90fa9aeSRam Pai  *
847b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
848b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
849b90fa9aeSRam Pai  * 	 the peer group of the source mount.
850b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
851b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
852b90fa9aeSRam Pai  *       mount.
8535afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
8545afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
8555afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
8565afe0022SRam Pai  *       is marked as 'shared and slave'.
8575afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
8585afe0022SRam Pai  * 	 source mount.
8595afe0022SRam Pai  *
8609676f0c6SRam Pai  * ---------------------------------------------------------------------------
86121444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
8629676f0c6SRam Pai  * |**************************************************************************
8639676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
8649676f0c6SRam Pai  * | dest     |               |                |                |            |
8659676f0c6SRam Pai  * |   |      |               |                |                |            |
8669676f0c6SRam Pai  * |   v      |               |                |                |            |
8679676f0c6SRam Pai  * |**************************************************************************
8689676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
8695afe0022SRam Pai  * |          |               |                |                |            |
8709676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
8719676f0c6SRam Pai  * ***************************************************************************
8725afe0022SRam Pai  *
8735afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
8745afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
87521444403SRam Pai  * (+*)  the mount is moved to the destination.
8765afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
8775afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
8785afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
8795afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
880b90fa9aeSRam Pai  *
881b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
882b90fa9aeSRam Pai  * applied to each mount in the tree.
883b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
884b90fa9aeSRam Pai  * in allocations.
885b90fa9aeSRam Pai  */
886b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt,
8871a390689SAl Viro 			struct path *path, struct path *parent_path)
888b90fa9aeSRam Pai {
889b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
8901a390689SAl Viro 	struct vfsmount *dest_mnt = path->mnt;
8911a390689SAl Viro 	struct dentry *dest_dentry = path->dentry;
892b90fa9aeSRam Pai 	struct vfsmount *child, *p;
893b90fa9aeSRam Pai 
894b90fa9aeSRam Pai 	if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
895b90fa9aeSRam Pai 		return -EINVAL;
896b90fa9aeSRam Pai 
897b90fa9aeSRam Pai 	if (IS_MNT_SHARED(dest_mnt)) {
898b90fa9aeSRam Pai 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
899b90fa9aeSRam Pai 			set_mnt_shared(p);
900b90fa9aeSRam Pai 	}
901b90fa9aeSRam Pai 
902b90fa9aeSRam Pai 	spin_lock(&vfsmount_lock);
9031a390689SAl Viro 	if (parent_path) {
9041a390689SAl Viro 		detach_mnt(source_mnt, parent_path);
9051a390689SAl Viro 		attach_mnt(source_mnt, path);
9066b3286edSKirill Korotaev 		touch_mnt_namespace(current->nsproxy->mnt_ns);
90721444403SRam Pai 	} else {
908b90fa9aeSRam Pai 		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
909b90fa9aeSRam Pai 		commit_tree(source_mnt);
91021444403SRam Pai 	}
911b90fa9aeSRam Pai 
912b90fa9aeSRam Pai 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
913b90fa9aeSRam Pai 		list_del_init(&child->mnt_hash);
914b90fa9aeSRam Pai 		commit_tree(child);
915b90fa9aeSRam Pai 	}
916b90fa9aeSRam Pai 	spin_unlock(&vfsmount_lock);
917b90fa9aeSRam Pai 	return 0;
918b90fa9aeSRam Pai }
919b90fa9aeSRam Pai 
9201da177e4SLinus Torvalds static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
9211da177e4SLinus Torvalds {
9221da177e4SLinus Torvalds 	int err;
9231da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_flags & MS_NOUSER)
9241da177e4SLinus Torvalds 		return -EINVAL;
9251da177e4SLinus Torvalds 
9264ac91378SJan Blunck 	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
9271da177e4SLinus Torvalds 	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
9281da177e4SLinus Torvalds 		return -ENOTDIR;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	err = -ENOENT;
9314ac91378SJan Blunck 	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
9324ac91378SJan Blunck 	if (IS_DEADDIR(nd->path.dentry->d_inode))
9331da177e4SLinus Torvalds 		goto out_unlock;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	err = security_sb_check_sb(mnt, nd);
9361da177e4SLinus Torvalds 	if (err)
9371da177e4SLinus Torvalds 		goto out_unlock;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	err = -ENOENT;
9404ac91378SJan Blunck 	if (IS_ROOT(nd->path.dentry) || !d_unhashed(nd->path.dentry))
9411a390689SAl Viro 		err = attach_recursive_mnt(mnt, &nd->path, NULL);
9421da177e4SLinus Torvalds out_unlock:
9434ac91378SJan Blunck 	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
9441da177e4SLinus Torvalds 	if (!err)
9451da177e4SLinus Torvalds 		security_sb_post_addmount(mnt, nd);
9461da177e4SLinus Torvalds 	return err;
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds /*
95007b20889SRam Pai  * recursively change the type of the mountpoint.
9512dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
95207b20889SRam Pai  */
9532dafe1c4SEric Sandeen static noinline int do_change_type(struct nameidata *nd, int flag)
95407b20889SRam Pai {
9554ac91378SJan Blunck 	struct vfsmount *m, *mnt = nd->path.mnt;
95607b20889SRam Pai 	int recurse = flag & MS_REC;
95707b20889SRam Pai 	int type = flag & ~MS_REC;
95807b20889SRam Pai 
959ee6f9582SMiklos Szeredi 	if (!capable(CAP_SYS_ADMIN))
960ee6f9582SMiklos Szeredi 		return -EPERM;
961ee6f9582SMiklos Szeredi 
9624ac91378SJan Blunck 	if (nd->path.dentry != nd->path.mnt->mnt_root)
96307b20889SRam Pai 		return -EINVAL;
96407b20889SRam Pai 
96507b20889SRam Pai 	down_write(&namespace_sem);
96607b20889SRam Pai 	spin_lock(&vfsmount_lock);
96707b20889SRam Pai 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
96807b20889SRam Pai 		change_mnt_propagation(m, type);
96907b20889SRam Pai 	spin_unlock(&vfsmount_lock);
97007b20889SRam Pai 	up_write(&namespace_sem);
97107b20889SRam Pai 	return 0;
97207b20889SRam Pai }
97307b20889SRam Pai 
97407b20889SRam Pai /*
9751da177e4SLinus Torvalds  * do loopback mount.
9762dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
9771da177e4SLinus Torvalds  */
9782dafe1c4SEric Sandeen static noinline int do_loopback(struct nameidata *nd, char *old_name,
9792dafe1c4SEric Sandeen 				int recurse)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	struct nameidata old_nd;
9821da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
9831da177e4SLinus Torvalds 	int err = mount_is_safe(nd);
9841da177e4SLinus Torvalds 	if (err)
9851da177e4SLinus Torvalds 		return err;
9861da177e4SLinus Torvalds 	if (!old_name || !*old_name)
9871da177e4SLinus Torvalds 		return -EINVAL;
9881da177e4SLinus Torvalds 	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
9891da177e4SLinus Torvalds 	if (err)
9901da177e4SLinus Torvalds 		return err;
9911da177e4SLinus Torvalds 
992390c6843SRam Pai 	down_write(&namespace_sem);
9931da177e4SLinus Torvalds 	err = -EINVAL;
9944ac91378SJan Blunck 	if (IS_MNT_UNBINDABLE(old_nd.path.mnt))
9959676f0c6SRam Pai 		goto out;
9969676f0c6SRam Pai 
9974ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
998ccd48bc7SAl Viro 		goto out;
999ccd48bc7SAl Viro 
10001da177e4SLinus Torvalds 	err = -ENOMEM;
10011da177e4SLinus Torvalds 	if (recurse)
10024ac91378SJan Blunck 		mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0);
10031da177e4SLinus Torvalds 	else
10044ac91378SJan Blunck 		mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0);
10051da177e4SLinus Torvalds 
1006ccd48bc7SAl Viro 	if (!mnt)
1007ccd48bc7SAl Viro 		goto out;
1008ccd48bc7SAl Viro 
10091da177e4SLinus Torvalds 	err = graft_tree(mnt, nd);
10101da177e4SLinus Torvalds 	if (err) {
101170fbcdf4SRam Pai 		LIST_HEAD(umount_list);
10121da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
1013a05964f3SRam Pai 		umount_tree(mnt, 0, &umount_list);
10141da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
101570fbcdf4SRam Pai 		release_mounts(&umount_list);
10165b83d2c5SRam Pai 	}
10171da177e4SLinus Torvalds 
1018ccd48bc7SAl Viro out:
1019390c6843SRam Pai 	up_write(&namespace_sem);
10201d957f9bSJan Blunck 	path_put(&old_nd.path);
10211da177e4SLinus Torvalds 	return err;
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds /*
10251da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
10261da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
10271da177e4SLinus Torvalds  * on it - tough luck.
10282dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
10291da177e4SLinus Torvalds  */
10302dafe1c4SEric Sandeen static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags,
10311da177e4SLinus Torvalds 		      void *data)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds 	int err;
10344ac91378SJan Blunck 	struct super_block *sb = nd->path.mnt->mnt_sb;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
10371da177e4SLinus Torvalds 		return -EPERM;
10381da177e4SLinus Torvalds 
10394ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt))
10401da177e4SLinus Torvalds 		return -EINVAL;
10411da177e4SLinus Torvalds 
10424ac91378SJan Blunck 	if (nd->path.dentry != nd->path.mnt->mnt_root)
10431da177e4SLinus Torvalds 		return -EINVAL;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	down_write(&sb->s_umount);
10461da177e4SLinus Torvalds 	err = do_remount_sb(sb, flags, data, 0);
10471da177e4SLinus Torvalds 	if (!err)
10484ac91378SJan Blunck 		nd->path.mnt->mnt_flags = mnt_flags;
10491da177e4SLinus Torvalds 	up_write(&sb->s_umount);
10501da177e4SLinus Torvalds 	if (!err)
10514ac91378SJan Blunck 		security_sb_post_remount(nd->path.mnt, flags, data);
10521da177e4SLinus Torvalds 	return err;
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
10559676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt)
10569676f0c6SRam Pai {
10579676f0c6SRam Pai 	struct vfsmount *p;
10589676f0c6SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
10599676f0c6SRam Pai 		if (IS_MNT_UNBINDABLE(p))
10609676f0c6SRam Pai 			return 1;
10619676f0c6SRam Pai 	}
10629676f0c6SRam Pai 	return 0;
10639676f0c6SRam Pai }
10649676f0c6SRam Pai 
10652dafe1c4SEric Sandeen /*
10662dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
10672dafe1c4SEric Sandeen  */
10682dafe1c4SEric Sandeen static noinline int do_move_mount(struct nameidata *nd, char *old_name)
10691da177e4SLinus Torvalds {
10701a390689SAl Viro 	struct nameidata old_nd;
10711a390689SAl Viro 	struct path parent_path;
10721da177e4SLinus Torvalds 	struct vfsmount *p;
10731da177e4SLinus Torvalds 	int err = 0;
10741da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
10751da177e4SLinus Torvalds 		return -EPERM;
10761da177e4SLinus Torvalds 	if (!old_name || !*old_name)
10771da177e4SLinus Torvalds 		return -EINVAL;
10781da177e4SLinus Torvalds 	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
10791da177e4SLinus Torvalds 	if (err)
10801da177e4SLinus Torvalds 		return err;
10811da177e4SLinus Torvalds 
1082390c6843SRam Pai 	down_write(&namespace_sem);
10834ac91378SJan Blunck 	while (d_mountpoint(nd->path.dentry) &&
10844ac91378SJan Blunck 	       follow_down(&nd->path.mnt, &nd->path.dentry))
10851da177e4SLinus Torvalds 		;
10861da177e4SLinus Torvalds 	err = -EINVAL;
10874ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
10881da177e4SLinus Torvalds 		goto out;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	err = -ENOENT;
10914ac91378SJan Blunck 	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
10924ac91378SJan Blunck 	if (IS_DEADDIR(nd->path.dentry->d_inode))
10931da177e4SLinus Torvalds 		goto out1;
10941da177e4SLinus Torvalds 
10954ac91378SJan Blunck 	if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry))
109621444403SRam Pai 		goto out1;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	err = -EINVAL;
10994ac91378SJan Blunck 	if (old_nd.path.dentry != old_nd.path.mnt->mnt_root)
110021444403SRam Pai 		goto out1;
11011da177e4SLinus Torvalds 
11024ac91378SJan Blunck 	if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent)
110321444403SRam Pai 		goto out1;
11041da177e4SLinus Torvalds 
11054ac91378SJan Blunck 	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
11064ac91378SJan Blunck 	      S_ISDIR(old_nd.path.dentry->d_inode->i_mode))
110721444403SRam Pai 		goto out1;
110821444403SRam Pai 	/*
110921444403SRam Pai 	 * Don't move a mount residing in a shared parent.
111021444403SRam Pai 	 */
11114ac91378SJan Blunck 	if (old_nd.path.mnt->mnt_parent &&
11124ac91378SJan Blunck 	    IS_MNT_SHARED(old_nd.path.mnt->mnt_parent))
111321444403SRam Pai 		goto out1;
11149676f0c6SRam Pai 	/*
11159676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
11169676f0c6SRam Pai 	 * mount which is shared.
11179676f0c6SRam Pai 	 */
11184ac91378SJan Blunck 	if (IS_MNT_SHARED(nd->path.mnt) &&
11194ac91378SJan Blunck 	    tree_contains_unbindable(old_nd.path.mnt))
11209676f0c6SRam Pai 		goto out1;
11211da177e4SLinus Torvalds 	err = -ELOOP;
11224ac91378SJan Blunck 	for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent)
11234ac91378SJan Blunck 		if (p == old_nd.path.mnt)
112421444403SRam Pai 			goto out1;
11251da177e4SLinus Torvalds 
11261a390689SAl Viro 	err = attach_recursive_mnt(old_nd.path.mnt, &nd->path, &parent_path);
11274ac91378SJan Blunck 	if (err)
112821444403SRam Pai 		goto out1;
11291da177e4SLinus Torvalds 
113021444403SRam Pai 	spin_lock(&vfsmount_lock);
11311da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
11321da177e4SLinus Torvalds 	 * automatically */
11334ac91378SJan Blunck 	list_del_init(&old_nd.path.mnt->mnt_expire);
11341da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
11351da177e4SLinus Torvalds out1:
11364ac91378SJan Blunck 	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
11371da177e4SLinus Torvalds out:
1138390c6843SRam Pai 	up_write(&namespace_sem);
11391da177e4SLinus Torvalds 	if (!err)
11401a390689SAl Viro 		path_put(&parent_path);
11411d957f9bSJan Blunck 	path_put(&old_nd.path);
11421da177e4SLinus Torvalds 	return err;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds /*
11461da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
11471da177e4SLinus Torvalds  * namespace's tree
11482dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
11491da177e4SLinus Torvalds  */
11502dafe1c4SEric Sandeen static noinline int do_new_mount(struct nameidata *nd, char *type, int flags,
11511da177e4SLinus Torvalds 			int mnt_flags, char *name, void *data)
11521da177e4SLinus Torvalds {
11531da177e4SLinus Torvalds 	struct vfsmount *mnt;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	if (!type || !memchr(type, 0, PAGE_SIZE))
11561da177e4SLinus Torvalds 		return -EINVAL;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	/* we need capabilities... */
11591da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
11601da177e4SLinus Torvalds 		return -EPERM;
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	mnt = do_kern_mount(type, flags, name, data);
11631da177e4SLinus Torvalds 	if (IS_ERR(mnt))
11641da177e4SLinus Torvalds 		return PTR_ERR(mnt);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	return do_add_mount(mnt, nd, mnt_flags, NULL);
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds /*
11701da177e4SLinus Torvalds  * add a mount into a namespace's mount tree
11711da177e4SLinus Torvalds  * - provide the option of adding the new mount to an expiration list
11721da177e4SLinus Torvalds  */
11731da177e4SLinus Torvalds int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
11741da177e4SLinus Torvalds 		 int mnt_flags, struct list_head *fslist)
11751da177e4SLinus Torvalds {
11761da177e4SLinus Torvalds 	int err;
11771da177e4SLinus Torvalds 
1178390c6843SRam Pai 	down_write(&namespace_sem);
11791da177e4SLinus Torvalds 	/* Something was mounted here while we slept */
11804ac91378SJan Blunck 	while (d_mountpoint(nd->path.dentry) &&
11814ac91378SJan Blunck 	       follow_down(&nd->path.mnt, &nd->path.dentry))
11821da177e4SLinus Torvalds 		;
11831da177e4SLinus Torvalds 	err = -EINVAL;
11844ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt))
11851da177e4SLinus Torvalds 		goto unlock;
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	/* Refuse the same filesystem on the same mount point */
11881da177e4SLinus Torvalds 	err = -EBUSY;
11894ac91378SJan Blunck 	if (nd->path.mnt->mnt_sb == newmnt->mnt_sb &&
11904ac91378SJan Blunck 	    nd->path.mnt->mnt_root == nd->path.dentry)
11911da177e4SLinus Torvalds 		goto unlock;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	err = -EINVAL;
11941da177e4SLinus Torvalds 	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
11951da177e4SLinus Torvalds 		goto unlock;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	newmnt->mnt_flags = mnt_flags;
11985b83d2c5SRam Pai 	if ((err = graft_tree(newmnt, nd)))
11995b83d2c5SRam Pai 		goto unlock;
12001da177e4SLinus Torvalds 
12015b83d2c5SRam Pai 	if (fslist) {
12021da177e4SLinus Torvalds 		/* add to the specified expiration list */
12031da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
120455e700b9SMiklos Szeredi 		list_add_tail(&newmnt->mnt_expire, fslist);
12051da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
12061da177e4SLinus Torvalds 	}
1207390c6843SRam Pai 	up_write(&namespace_sem);
12085b83d2c5SRam Pai 	return 0;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds unlock:
1211390c6843SRam Pai 	up_write(&namespace_sem);
12121da177e4SLinus Torvalds 	mntput(newmnt);
12131da177e4SLinus Torvalds 	return err;
12141da177e4SLinus Torvalds }
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount);
12171da177e4SLinus Torvalds 
12185528f911STrond Myklebust /*
12191da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
12201da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
12211da177e4SLinus Torvalds  * here
12221da177e4SLinus Torvalds  */
12231da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
12241da177e4SLinus Torvalds {
12251da177e4SLinus Torvalds 	struct vfsmount *mnt, *next;
12261da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
1227bcc5c7d2SAl Viro 	LIST_HEAD(umounts);
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	if (list_empty(mounts))
12301da177e4SLinus Torvalds 		return;
12311da177e4SLinus Torvalds 
1232bcc5c7d2SAl Viro 	down_write(&namespace_sem);
12331da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
12361da177e4SLinus Torvalds 	 * following criteria:
12371da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
12381da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
12391da177e4SLinus Torvalds 	 *   cleared by mntput())
12401da177e4SLinus Torvalds 	 */
124155e700b9SMiklos Szeredi 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
12421da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1243bcc5c7d2SAl Viro 			propagate_mount_busy(mnt, 1))
12441da177e4SLinus Torvalds 			continue;
124555e700b9SMiklos Szeredi 		list_move(&mnt->mnt_expire, &graveyard);
12461da177e4SLinus Torvalds 	}
1247bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
1248bcc5c7d2SAl Viro 		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
1249bcc5c7d2SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1250bcc5c7d2SAl Viro 		umount_tree(mnt, 1, &umounts);
1251bcc5c7d2SAl Viro 	}
12521da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
1253bcc5c7d2SAl Viro 	up_write(&namespace_sem);
1254bcc5c7d2SAl Viro 
1255bcc5c7d2SAl Viro 	release_mounts(&umounts);
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds /*
12615528f911STrond Myklebust  * Ripoff of 'select_parent()'
12625528f911STrond Myklebust  *
12635528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
12645528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
12655528f911STrond Myklebust  */
12665528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
12675528f911STrond Myklebust {
12685528f911STrond Myklebust 	struct vfsmount *this_parent = parent;
12695528f911STrond Myklebust 	struct list_head *next;
12705528f911STrond Myklebust 	int found = 0;
12715528f911STrond Myklebust 
12725528f911STrond Myklebust repeat:
12735528f911STrond Myklebust 	next = this_parent->mnt_mounts.next;
12745528f911STrond Myklebust resume:
12755528f911STrond Myklebust 	while (next != &this_parent->mnt_mounts) {
12765528f911STrond Myklebust 		struct list_head *tmp = next;
12775528f911STrond Myklebust 		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
12785528f911STrond Myklebust 
12795528f911STrond Myklebust 		next = tmp->next;
12805528f911STrond Myklebust 		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
12815528f911STrond Myklebust 			continue;
12825528f911STrond Myklebust 		/*
12835528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
12845528f911STrond Myklebust 		 */
12855528f911STrond Myklebust 		if (!list_empty(&mnt->mnt_mounts)) {
12865528f911STrond Myklebust 			this_parent = mnt;
12875528f911STrond Myklebust 			goto repeat;
12885528f911STrond Myklebust 		}
12895528f911STrond Myklebust 
12905528f911STrond Myklebust 		if (!propagate_mount_busy(mnt, 1)) {
12915528f911STrond Myklebust 			list_move_tail(&mnt->mnt_expire, graveyard);
12925528f911STrond Myklebust 			found++;
12935528f911STrond Myklebust 		}
12945528f911STrond Myklebust 	}
12955528f911STrond Myklebust 	/*
12965528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
12975528f911STrond Myklebust 	 */
12985528f911STrond Myklebust 	if (this_parent != parent) {
12995528f911STrond Myklebust 		next = this_parent->mnt_child.next;
13005528f911STrond Myklebust 		this_parent = this_parent->mnt_parent;
13015528f911STrond Myklebust 		goto resume;
13025528f911STrond Myklebust 	}
13035528f911STrond Myklebust 	return found;
13045528f911STrond Myklebust }
13055528f911STrond Myklebust 
13065528f911STrond Myklebust /*
13075528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
13085528f911STrond Myklebust  * submounts of a specific parent mountpoint
13095528f911STrond Myklebust  */
1310c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
13115528f911STrond Myklebust {
13125528f911STrond Myklebust 	LIST_HEAD(graveyard);
1313c35038beSAl Viro 	struct vfsmount *m;
13145528f911STrond Myklebust 
13155528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
1316c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
1317bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
1318c35038beSAl Viro 			m = list_first_entry(&graveyard, struct vfsmount,
1319bcc5c7d2SAl Viro 						mnt_expire);
1320bcc5c7d2SAl Viro 			touch_mnt_namespace(mnt->mnt_ns);
1321c35038beSAl Viro 			umount_tree(mnt, 1, umounts);
1322bcc5c7d2SAl Viro 		}
1323bcc5c7d2SAl Viro 	}
13245528f911STrond Myklebust }
13255528f911STrond Myklebust 
13265528f911STrond Myklebust /*
13271da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
13281da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
13291da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
13301da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
13311da177e4SLinus Torvalds  */
1332b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
1333b58fed8bSRam Pai 				 unsigned long n)
13341da177e4SLinus Torvalds {
13351da177e4SLinus Torvalds 	char *t = to;
13361da177e4SLinus Torvalds 	const char __user *f = from;
13371da177e4SLinus Torvalds 	char c;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
13401da177e4SLinus Torvalds 		return n;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	while (n) {
13431da177e4SLinus Torvalds 		if (__get_user(c, f)) {
13441da177e4SLinus Torvalds 			memset(t, 0, n);
13451da177e4SLinus Torvalds 			break;
13461da177e4SLinus Torvalds 		}
13471da177e4SLinus Torvalds 		*t++ = c;
13481da177e4SLinus Torvalds 		f++;
13491da177e4SLinus Torvalds 		n--;
13501da177e4SLinus Torvalds 	}
13511da177e4SLinus Torvalds 	return n;
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	int i;
13571da177e4SLinus Torvalds 	unsigned long page;
13581da177e4SLinus Torvalds 	unsigned long size;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	*where = 0;
13611da177e4SLinus Torvalds 	if (!data)
13621da177e4SLinus Torvalds 		return 0;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
13651da177e4SLinus Torvalds 		return -ENOMEM;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
13681da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
13691da177e4SLinus Torvalds 	 * the remainder of the page.
13701da177e4SLinus Torvalds 	 */
13711da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
13721da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
13731da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
13741da177e4SLinus Torvalds 		size = PAGE_SIZE;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
13771da177e4SLinus Torvalds 	if (!i) {
13781da177e4SLinus Torvalds 		free_page(page);
13791da177e4SLinus Torvalds 		return -EFAULT;
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
13821da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
13831da177e4SLinus Torvalds 	*where = page;
13841da177e4SLinus Torvalds 	return 0;
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds /*
13881da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
13891da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
13901da177e4SLinus Torvalds  *
13911da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
13921da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
13931da177e4SLinus Torvalds  * information (or be NULL).
13941da177e4SLinus Torvalds  *
13951da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
13961da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
13971da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
13981da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
13991da177e4SLinus Torvalds  * and must be discarded.
14001da177e4SLinus Torvalds  */
14011da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page,
14021da177e4SLinus Torvalds 		  unsigned long flags, void *data_page)
14031da177e4SLinus Torvalds {
14041da177e4SLinus Torvalds 	struct nameidata nd;
14051da177e4SLinus Torvalds 	int retval = 0;
14061da177e4SLinus Torvalds 	int mnt_flags = 0;
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	/* Discard magic */
14091da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
14101da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	/* Basic sanity checks */
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
14151da177e4SLinus Torvalds 		return -EINVAL;
14161da177e4SLinus Torvalds 	if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
14171da177e4SLinus Torvalds 		return -EINVAL;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	if (data_page)
14201da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
14231da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
14241da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
14251da177e4SLinus Torvalds 	if (flags & MS_NODEV)
14261da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
14271da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
14281da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
1429fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
1430fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
1431fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
1432fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
143347ae32d6SValerie Henson 	if (flags & MS_RELATIME)
143447ae32d6SValerie Henson 		mnt_flags |= MNT_RELATIME;
1435fc33a7bbSChristoph Hellwig 
1436fc33a7bbSChristoph Hellwig 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
14378bf9725cSPavel Emelyanov 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 	/* ... and get the mountpoint */
14401da177e4SLinus Torvalds 	retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
14411da177e4SLinus Torvalds 	if (retval)
14421da177e4SLinus Torvalds 		return retval;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
14451da177e4SLinus Torvalds 	if (retval)
14461da177e4SLinus Torvalds 		goto dput_out;
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
14491da177e4SLinus Torvalds 		retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
14501da177e4SLinus Torvalds 				    data_page);
14511da177e4SLinus Torvalds 	else if (flags & MS_BIND)
1452eee391a6SAndrew Morton 		retval = do_loopback(&nd, dev_name, flags & MS_REC);
14539676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
145407b20889SRam Pai 		retval = do_change_type(&nd, flags);
14551da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
14561da177e4SLinus Torvalds 		retval = do_move_mount(&nd, dev_name);
14571da177e4SLinus Torvalds 	else
14581da177e4SLinus Torvalds 		retval = do_new_mount(&nd, type_page, flags, mnt_flags,
14591da177e4SLinus Torvalds 				      dev_name, data_page);
14601da177e4SLinus Torvalds dput_out:
14611d957f9bSJan Blunck 	path_put(&nd.path);
14621da177e4SLinus Torvalds 	return retval;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
1465741a2951SJANAK DESAI /*
1466741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
1467741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
1468741a2951SJANAK DESAI  */
1469e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
14706b3286edSKirill Korotaev 		struct fs_struct *fs)
14711da177e4SLinus Torvalds {
14726b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
14731da177e4SLinus Torvalds 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
14741da177e4SLinus Torvalds 	struct vfsmount *p, *q;
14751da177e4SLinus Torvalds 
14766b3286edSKirill Korotaev 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
14771da177e4SLinus Torvalds 	if (!new_ns)
1478467e9f4bSCedric Le Goater 		return ERR_PTR(-ENOMEM);
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	atomic_set(&new_ns->count, 1);
14811da177e4SLinus Torvalds 	INIT_LIST_HEAD(&new_ns->list);
14825addc5ddSAl Viro 	init_waitqueue_head(&new_ns->poll);
14835addc5ddSAl Viro 	new_ns->event = 0;
14841da177e4SLinus Torvalds 
1485390c6843SRam Pai 	down_write(&namespace_sem);
14861da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
14876b3286edSKirill Korotaev 	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
14889676f0c6SRam Pai 					CL_COPY_ALL | CL_EXPIRE);
14891da177e4SLinus Torvalds 	if (!new_ns->root) {
1490390c6843SRam Pai 		up_write(&namespace_sem);
14911da177e4SLinus Torvalds 		kfree(new_ns);
1492467e9f4bSCedric Le Goater 		return ERR_PTR(-ENOMEM);;
14931da177e4SLinus Torvalds 	}
14941da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
14951da177e4SLinus Torvalds 	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
14961da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	/*
14991da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
15001da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
15011da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
15021da177e4SLinus Torvalds 	 */
15036b3286edSKirill Korotaev 	p = mnt_ns->root;
15041da177e4SLinus Torvalds 	q = new_ns->root;
15051da177e4SLinus Torvalds 	while (p) {
15066b3286edSKirill Korotaev 		q->mnt_ns = new_ns;
15071da177e4SLinus Torvalds 		if (fs) {
15086ac08c39SJan Blunck 			if (p == fs->root.mnt) {
15091da177e4SLinus Torvalds 				rootmnt = p;
15106ac08c39SJan Blunck 				fs->root.mnt = mntget(q);
15111da177e4SLinus Torvalds 			}
15126ac08c39SJan Blunck 			if (p == fs->pwd.mnt) {
15131da177e4SLinus Torvalds 				pwdmnt = p;
15146ac08c39SJan Blunck 				fs->pwd.mnt = mntget(q);
15151da177e4SLinus Torvalds 			}
15166ac08c39SJan Blunck 			if (p == fs->altroot.mnt) {
15171da177e4SLinus Torvalds 				altrootmnt = p;
15186ac08c39SJan Blunck 				fs->altroot.mnt = mntget(q);
15191da177e4SLinus Torvalds 			}
15201da177e4SLinus Torvalds 		}
15216b3286edSKirill Korotaev 		p = next_mnt(p, mnt_ns->root);
15221da177e4SLinus Torvalds 		q = next_mnt(q, new_ns->root);
15231da177e4SLinus Torvalds 	}
1524390c6843SRam Pai 	up_write(&namespace_sem);
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	if (rootmnt)
15271da177e4SLinus Torvalds 		mntput(rootmnt);
15281da177e4SLinus Torvalds 	if (pwdmnt)
15291da177e4SLinus Torvalds 		mntput(pwdmnt);
15301da177e4SLinus Torvalds 	if (altrootmnt)
15311da177e4SLinus Torvalds 		mntput(altrootmnt);
15321da177e4SLinus Torvalds 
1533741a2951SJANAK DESAI 	return new_ns;
1534741a2951SJANAK DESAI }
1535741a2951SJANAK DESAI 
1536213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
1537e3222c4eSBadari Pulavarty 		struct fs_struct *new_fs)
1538741a2951SJANAK DESAI {
15396b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
1540741a2951SJANAK DESAI 
1541e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
15426b3286edSKirill Korotaev 	get_mnt_ns(ns);
1543741a2951SJANAK DESAI 
1544741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
1545e3222c4eSBadari Pulavarty 		return ns;
1546741a2951SJANAK DESAI 
1547e3222c4eSBadari Pulavarty 	new_ns = dup_mnt_ns(ns, new_fs);
1548741a2951SJANAK DESAI 
15496b3286edSKirill Korotaev 	put_mnt_ns(ns);
1550e3222c4eSBadari Pulavarty 	return new_ns;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
15541da177e4SLinus Torvalds 			  char __user * type, unsigned long flags,
15551da177e4SLinus Torvalds 			  void __user * data)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	int retval;
15581da177e4SLinus Torvalds 	unsigned long data_page;
15591da177e4SLinus Torvalds 	unsigned long type_page;
15601da177e4SLinus Torvalds 	unsigned long dev_page;
15611da177e4SLinus Torvalds 	char *dir_page;
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	retval = copy_mount_options(type, &type_page);
15641da177e4SLinus Torvalds 	if (retval < 0)
15651da177e4SLinus Torvalds 		return retval;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	dir_page = getname(dir_name);
15681da177e4SLinus Torvalds 	retval = PTR_ERR(dir_page);
15691da177e4SLinus Torvalds 	if (IS_ERR(dir_page))
15701da177e4SLinus Torvalds 		goto out1;
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	retval = copy_mount_options(dev_name, &dev_page);
15731da177e4SLinus Torvalds 	if (retval < 0)
15741da177e4SLinus Torvalds 		goto out2;
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	retval = copy_mount_options(data, &data_page);
15771da177e4SLinus Torvalds 	if (retval < 0)
15781da177e4SLinus Torvalds 		goto out3;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	lock_kernel();
15811da177e4SLinus Torvalds 	retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
15821da177e4SLinus Torvalds 			  flags, (void *)data_page);
15831da177e4SLinus Torvalds 	unlock_kernel();
15841da177e4SLinus Torvalds 	free_page(data_page);
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds out3:
15871da177e4SLinus Torvalds 	free_page(dev_page);
15881da177e4SLinus Torvalds out2:
15891da177e4SLinus Torvalds 	putname(dir_page);
15901da177e4SLinus Torvalds out1:
15911da177e4SLinus Torvalds 	free_page(type_page);
15921da177e4SLinus Torvalds 	return retval;
15931da177e4SLinus Torvalds }
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds /*
15961da177e4SLinus Torvalds  * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
15971da177e4SLinus Torvalds  * It can block. Requires the big lock held.
15981da177e4SLinus Torvalds  */
1599ac748a09SJan Blunck void set_fs_root(struct fs_struct *fs, struct path *path)
16001da177e4SLinus Torvalds {
16016ac08c39SJan Blunck 	struct path old_root;
16026ac08c39SJan Blunck 
16031da177e4SLinus Torvalds 	write_lock(&fs->lock);
16041da177e4SLinus Torvalds 	old_root = fs->root;
1605ac748a09SJan Blunck 	fs->root = *path;
1606ac748a09SJan Blunck 	path_get(path);
16071da177e4SLinus Torvalds 	write_unlock(&fs->lock);
16086ac08c39SJan Blunck 	if (old_root.dentry)
16096ac08c39SJan Blunck 		path_put(&old_root);
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds /*
16131da177e4SLinus Torvalds  * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
16141da177e4SLinus Torvalds  * It can block. Requires the big lock held.
16151da177e4SLinus Torvalds  */
1616ac748a09SJan Blunck void set_fs_pwd(struct fs_struct *fs, struct path *path)
16171da177e4SLinus Torvalds {
16186ac08c39SJan Blunck 	struct path old_pwd;
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	write_lock(&fs->lock);
16211da177e4SLinus Torvalds 	old_pwd = fs->pwd;
1622ac748a09SJan Blunck 	fs->pwd = *path;
1623ac748a09SJan Blunck 	path_get(path);
16241da177e4SLinus Torvalds 	write_unlock(&fs->lock);
16251da177e4SLinus Torvalds 
16266ac08c39SJan Blunck 	if (old_pwd.dentry)
16276ac08c39SJan Blunck 		path_put(&old_pwd);
16281da177e4SLinus Torvalds }
16291da177e4SLinus Torvalds 
16301a390689SAl Viro static void chroot_fs_refs(struct path *old_root, struct path *new_root)
16311da177e4SLinus Torvalds {
16321da177e4SLinus Torvalds 	struct task_struct *g, *p;
16331da177e4SLinus Torvalds 	struct fs_struct *fs;
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
16361da177e4SLinus Torvalds 	do_each_thread(g, p) {
16371da177e4SLinus Torvalds 		task_lock(p);
16381da177e4SLinus Torvalds 		fs = p->fs;
16391da177e4SLinus Torvalds 		if (fs) {
16401da177e4SLinus Torvalds 			atomic_inc(&fs->count);
16411da177e4SLinus Torvalds 			task_unlock(p);
16421a390689SAl Viro 			if (fs->root.dentry == old_root->dentry
16431a390689SAl Viro 			    && fs->root.mnt == old_root->mnt)
16441a390689SAl Viro 				set_fs_root(fs, new_root);
16451a390689SAl Viro 			if (fs->pwd.dentry == old_root->dentry
16461a390689SAl Viro 			    && fs->pwd.mnt == old_root->mnt)
16471a390689SAl Viro 				set_fs_pwd(fs, new_root);
16481da177e4SLinus Torvalds 			put_fs_struct(fs);
16491da177e4SLinus Torvalds 		} else
16501da177e4SLinus Torvalds 			task_unlock(p);
16511da177e4SLinus Torvalds 	} while_each_thread(g, p);
16521da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /*
16561da177e4SLinus Torvalds  * pivot_root Semantics:
16571da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
16581da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
16591da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
16601da177e4SLinus Torvalds  *
16611da177e4SLinus Torvalds  * Restrictions:
16621da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
16631da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
16641da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
16651da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
16661da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
16671da177e4SLinus Torvalds  *
16684a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
16694a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
16704a0d11faSNeil Brown  * in this situation.
16714a0d11faSNeil Brown  *
16721da177e4SLinus Torvalds  * Notes:
16731da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
16741da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
16751da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
16761da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
16771da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
16781da177e4SLinus Torvalds  *    first.
16791da177e4SLinus Torvalds  */
1680b58fed8bSRam Pai asmlinkage long sys_pivot_root(const char __user * new_root,
1681b58fed8bSRam Pai 			       const char __user * put_old)
16821da177e4SLinus Torvalds {
16831da177e4SLinus Torvalds 	struct vfsmount *tmp;
16841a390689SAl Viro 	struct nameidata new_nd, old_nd, user_nd;
16851a390689SAl Viro 	struct path parent_path, root_parent;
16861da177e4SLinus Torvalds 	int error;
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
16891da177e4SLinus Torvalds 		return -EPERM;
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	lock_kernel();
16921da177e4SLinus Torvalds 
1693b58fed8bSRam Pai 	error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1694b58fed8bSRam Pai 			    &new_nd);
16951da177e4SLinus Torvalds 	if (error)
16961da177e4SLinus Torvalds 		goto out0;
16971da177e4SLinus Torvalds 	error = -EINVAL;
16984ac91378SJan Blunck 	if (!check_mnt(new_nd.path.mnt))
16991da177e4SLinus Torvalds 		goto out1;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
17021da177e4SLinus Torvalds 	if (error)
17031da177e4SLinus Torvalds 		goto out1;
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	error = security_sb_pivotroot(&old_nd, &new_nd);
17061da177e4SLinus Torvalds 	if (error) {
17071d957f9bSJan Blunck 		path_put(&old_nd.path);
17081da177e4SLinus Torvalds 		goto out1;
17091da177e4SLinus Torvalds 	}
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	read_lock(&current->fs->lock);
17126ac08c39SJan Blunck 	user_nd.path = current->fs->root;
17136ac08c39SJan Blunck 	path_get(&current->fs->root);
17141da177e4SLinus Torvalds 	read_unlock(&current->fs->lock);
1715390c6843SRam Pai 	down_write(&namespace_sem);
17164ac91378SJan Blunck 	mutex_lock(&old_nd.path.dentry->d_inode->i_mutex);
17171da177e4SLinus Torvalds 	error = -EINVAL;
17184ac91378SJan Blunck 	if (IS_MNT_SHARED(old_nd.path.mnt) ||
17194ac91378SJan Blunck 		IS_MNT_SHARED(new_nd.path.mnt->mnt_parent) ||
17204ac91378SJan Blunck 		IS_MNT_SHARED(user_nd.path.mnt->mnt_parent))
172121444403SRam Pai 		goto out2;
17224ac91378SJan Blunck 	if (!check_mnt(user_nd.path.mnt))
17231da177e4SLinus Torvalds 		goto out2;
17241da177e4SLinus Torvalds 	error = -ENOENT;
17254ac91378SJan Blunck 	if (IS_DEADDIR(new_nd.path.dentry->d_inode))
17261da177e4SLinus Torvalds 		goto out2;
17274ac91378SJan Blunck 	if (d_unhashed(new_nd.path.dentry) && !IS_ROOT(new_nd.path.dentry))
17281da177e4SLinus Torvalds 		goto out2;
17294ac91378SJan Blunck 	if (d_unhashed(old_nd.path.dentry) && !IS_ROOT(old_nd.path.dentry))
17301da177e4SLinus Torvalds 		goto out2;
17311da177e4SLinus Torvalds 	error = -EBUSY;
17324ac91378SJan Blunck 	if (new_nd.path.mnt == user_nd.path.mnt ||
17334ac91378SJan Blunck 	    old_nd.path.mnt == user_nd.path.mnt)
17341da177e4SLinus Torvalds 		goto out2; /* loop, on the same file system  */
17351da177e4SLinus Torvalds 	error = -EINVAL;
17364ac91378SJan Blunck 	if (user_nd.path.mnt->mnt_root != user_nd.path.dentry)
17371da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
17384ac91378SJan Blunck 	if (user_nd.path.mnt->mnt_parent == user_nd.path.mnt)
17390bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
17404ac91378SJan Blunck 	if (new_nd.path.mnt->mnt_root != new_nd.path.dentry)
17411da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
17424ac91378SJan Blunck 	if (new_nd.path.mnt->mnt_parent == new_nd.path.mnt)
17430bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
17444ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
17454ac91378SJan Blunck 	tmp = old_nd.path.mnt;
17461da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
17474ac91378SJan Blunck 	if (tmp != new_nd.path.mnt) {
17481da177e4SLinus Torvalds 		for (;;) {
17491da177e4SLinus Torvalds 			if (tmp->mnt_parent == tmp)
17501da177e4SLinus Torvalds 				goto out3; /* already mounted on put_old */
17514ac91378SJan Blunck 			if (tmp->mnt_parent == new_nd.path.mnt)
17521da177e4SLinus Torvalds 				break;
17531da177e4SLinus Torvalds 			tmp = tmp->mnt_parent;
17541da177e4SLinus Torvalds 		}
17554ac91378SJan Blunck 		if (!is_subdir(tmp->mnt_mountpoint, new_nd.path.dentry))
17561da177e4SLinus Torvalds 			goto out3;
17574ac91378SJan Blunck 	} else if (!is_subdir(old_nd.path.dentry, new_nd.path.dentry))
17581da177e4SLinus Torvalds 		goto out3;
17591a390689SAl Viro 	detach_mnt(new_nd.path.mnt, &parent_path);
17604ac91378SJan Blunck 	detach_mnt(user_nd.path.mnt, &root_parent);
17614ac91378SJan Blunck 	/* mount old root on put_old */
17621a390689SAl Viro 	attach_mnt(user_nd.path.mnt, &old_nd.path);
17634ac91378SJan Blunck 	/* mount new_root on / */
17644ac91378SJan Blunck 	attach_mnt(new_nd.path.mnt, &root_parent);
17656b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
17661da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
17671a390689SAl Viro 	chroot_fs_refs(&user_nd.path, &new_nd.path);
17681da177e4SLinus Torvalds 	security_sb_post_pivotroot(&user_nd, &new_nd);
17691da177e4SLinus Torvalds 	error = 0;
17701a390689SAl Viro 	path_put(&root_parent);
17711a390689SAl Viro 	path_put(&parent_path);
17721da177e4SLinus Torvalds out2:
17734ac91378SJan Blunck 	mutex_unlock(&old_nd.path.dentry->d_inode->i_mutex);
1774390c6843SRam Pai 	up_write(&namespace_sem);
17751d957f9bSJan Blunck 	path_put(&user_nd.path);
17761d957f9bSJan Blunck 	path_put(&old_nd.path);
17771da177e4SLinus Torvalds out1:
17781d957f9bSJan Blunck 	path_put(&new_nd.path);
17791da177e4SLinus Torvalds out0:
17801da177e4SLinus Torvalds 	unlock_kernel();
17811da177e4SLinus Torvalds 	return error;
17821da177e4SLinus Torvalds out3:
17831da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
17841da177e4SLinus Torvalds 	goto out2;
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds static void __init init_mount_tree(void)
17881da177e4SLinus Torvalds {
17891da177e4SLinus Torvalds 	struct vfsmount *mnt;
17906b3286edSKirill Korotaev 	struct mnt_namespace *ns;
1791ac748a09SJan Blunck 	struct path root;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
17941da177e4SLinus Torvalds 	if (IS_ERR(mnt))
17951da177e4SLinus Torvalds 		panic("Can't create rootfs");
17966b3286edSKirill Korotaev 	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
17976b3286edSKirill Korotaev 	if (!ns)
17981da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
17996b3286edSKirill Korotaev 	atomic_set(&ns->count, 1);
18006b3286edSKirill Korotaev 	INIT_LIST_HEAD(&ns->list);
18016b3286edSKirill Korotaev 	init_waitqueue_head(&ns->poll);
18026b3286edSKirill Korotaev 	ns->event = 0;
18036b3286edSKirill Korotaev 	list_add(&mnt->mnt_list, &ns->list);
18046b3286edSKirill Korotaev 	ns->root = mnt;
18056b3286edSKirill Korotaev 	mnt->mnt_ns = ns;
18061da177e4SLinus Torvalds 
18076b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
18086b3286edSKirill Korotaev 	get_mnt_ns(ns);
18091da177e4SLinus Torvalds 
1810ac748a09SJan Blunck 	root.mnt = ns->root;
1811ac748a09SJan Blunck 	root.dentry = ns->root->mnt_root;
1812ac748a09SJan Blunck 
1813ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
1814ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
181774bf17cfSDenis Cheng void __init mnt_init(void)
18181da177e4SLinus Torvalds {
181913f14b4dSEric Dumazet 	unsigned u;
182015a67dd8SRandy Dunlap 	int err;
18211da177e4SLinus Torvalds 
1822390c6843SRam Pai 	init_rwsem(&namespace_sem);
1823390c6843SRam Pai 
18241da177e4SLinus Torvalds 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
182520c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
18261da177e4SLinus Torvalds 
1827b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if (!mount_hashtable)
18301da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
18311da177e4SLinus Torvalds 
183213f14b4dSEric Dumazet 	printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
18331da177e4SLinus Torvalds 
183413f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
183513f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
18361da177e4SLinus Torvalds 
183715a67dd8SRandy Dunlap 	err = sysfs_init();
183815a67dd8SRandy Dunlap 	if (err)
183915a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
184015a67dd8SRandy Dunlap 			__FUNCTION__, err);
184100d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
184200d26666SGreg Kroah-Hartman 	if (!fs_kobj)
184300d26666SGreg Kroah-Hartman 		printk(KERN_WARNING "%s: kobj create error\n", __FUNCTION__);
18441da177e4SLinus Torvalds 	init_rootfs();
18451da177e4SLinus Torvalds 	init_mount_tree();
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
18486b3286edSKirill Korotaev void __put_mnt_ns(struct mnt_namespace *ns)
18491da177e4SLinus Torvalds {
18506b3286edSKirill Korotaev 	struct vfsmount *root = ns->root;
185170fbcdf4SRam Pai 	LIST_HEAD(umount_list);
18526b3286edSKirill Korotaev 	ns->root = NULL;
18531ce88cf4SMiklos Szeredi 	spin_unlock(&vfsmount_lock);
1854390c6843SRam Pai 	down_write(&namespace_sem);
18551da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
1856a05964f3SRam Pai 	umount_tree(root, 0, &umount_list);
18571da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
1858390c6843SRam Pai 	up_write(&namespace_sem);
185970fbcdf4SRam Pai 	release_mounts(&umount_list);
18606b3286edSKirill Korotaev 	kfree(ns);
18611da177e4SLinus Torvalds }
1862