xref: /openbmc/linux/fs/namespace.c (revision 6758f953)
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) {
26555e700b9SMiklos Szeredi 			if (!list_empty(&old->mnt_expire))
26655e700b9SMiklos Szeredi 				list_add(&mnt->mnt_expire, &old->mnt_expire);
2671da177e4SLinus Torvalds 		}
26836341f64SRam Pai 	}
2691da177e4SLinus Torvalds 	return mnt;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2727b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
2751da177e4SLinus Torvalds 	dput(mnt->mnt_root);
2761da177e4SLinus Torvalds 	free_vfsmnt(mnt);
2771da177e4SLinus Torvalds 	deactivate_super(sb);
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2807b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt)
2817b7b1aceSAl Viro {
2827b7b1aceSAl Viro repeat:
2837b7b1aceSAl Viro 	if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
2847b7b1aceSAl Viro 		if (likely(!mnt->mnt_pinned)) {
2857b7b1aceSAl Viro 			spin_unlock(&vfsmount_lock);
2867b7b1aceSAl Viro 			__mntput(mnt);
2877b7b1aceSAl Viro 			return;
2887b7b1aceSAl Viro 		}
2897b7b1aceSAl Viro 		atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
2907b7b1aceSAl Viro 		mnt->mnt_pinned = 0;
2917b7b1aceSAl Viro 		spin_unlock(&vfsmount_lock);
2927b7b1aceSAl Viro 		acct_auto_close_mnt(mnt);
2937b7b1aceSAl Viro 		security_sb_umount_close(mnt);
2947b7b1aceSAl Viro 		goto repeat;
2957b7b1aceSAl Viro 	}
2967b7b1aceSAl Viro }
2977b7b1aceSAl Viro 
2987b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire);
2997b7b1aceSAl Viro 
3007b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt)
3017b7b1aceSAl Viro {
3027b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
3037b7b1aceSAl Viro 	mnt->mnt_pinned++;
3047b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
3057b7b1aceSAl Viro }
3067b7b1aceSAl Viro 
3077b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin);
3087b7b1aceSAl Viro 
3097b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt)
3107b7b1aceSAl Viro {
3117b7b1aceSAl Viro 	spin_lock(&vfsmount_lock);
3127b7b1aceSAl Viro 	if (mnt->mnt_pinned) {
3137b7b1aceSAl Viro 		atomic_inc(&mnt->mnt_count);
3147b7b1aceSAl Viro 		mnt->mnt_pinned--;
3157b7b1aceSAl Viro 	}
3167b7b1aceSAl Viro 	spin_unlock(&vfsmount_lock);
3177b7b1aceSAl Viro }
3187b7b1aceSAl Viro 
3197b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin);
3201da177e4SLinus Torvalds 
321b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s)
322b3b304a2SMiklos Szeredi {
323b3b304a2SMiklos Szeredi 	seq_escape(m, s, " \t\n\\");
324b3b304a2SMiklos Szeredi }
325b3b304a2SMiklos Szeredi 
326b3b304a2SMiklos Szeredi /*
327b3b304a2SMiklos Szeredi  * Simple .show_options callback for filesystems which don't want to
328b3b304a2SMiklos Szeredi  * implement more complex mount option showing.
329b3b304a2SMiklos Szeredi  *
330b3b304a2SMiklos Szeredi  * See also save_mount_options().
331b3b304a2SMiklos Szeredi  */
332b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt)
333b3b304a2SMiklos Szeredi {
334b3b304a2SMiklos Szeredi 	const char *options = mnt->mnt_sb->s_options;
335b3b304a2SMiklos Szeredi 
336b3b304a2SMiklos Szeredi 	if (options != NULL && options[0]) {
337b3b304a2SMiklos Szeredi 		seq_putc(m, ',');
338b3b304a2SMiklos Szeredi 		mangle(m, options);
339b3b304a2SMiklos Szeredi 	}
340b3b304a2SMiklos Szeredi 
341b3b304a2SMiklos Szeredi 	return 0;
342b3b304a2SMiklos Szeredi }
343b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options);
344b3b304a2SMiklos Szeredi 
345b3b304a2SMiklos Szeredi /*
346b3b304a2SMiklos Szeredi  * If filesystem uses generic_show_options(), this function should be
347b3b304a2SMiklos Szeredi  * called from the fill_super() callback.
348b3b304a2SMiklos Szeredi  *
349b3b304a2SMiklos Szeredi  * The .remount_fs callback usually needs to be handled in a special
350b3b304a2SMiklos Szeredi  * way, to make sure, that previous options are not overwritten if the
351b3b304a2SMiklos Szeredi  * remount fails.
352b3b304a2SMiklos Szeredi  *
353b3b304a2SMiklos Szeredi  * Also note, that if the filesystem's .remount_fs function doesn't
354b3b304a2SMiklos Szeredi  * reset all options to their default value, but changes only newly
355b3b304a2SMiklos Szeredi  * given options, then the displayed options will not reflect reality
356b3b304a2SMiklos Szeredi  * any more.
357b3b304a2SMiklos Szeredi  */
358b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options)
359b3b304a2SMiklos Szeredi {
360b3b304a2SMiklos Szeredi 	kfree(sb->s_options);
361b3b304a2SMiklos Szeredi 	sb->s_options = kstrdup(options, GFP_KERNEL);
362b3b304a2SMiklos Szeredi }
363b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options);
364b3b304a2SMiklos Szeredi 
3651da177e4SLinus Torvalds /* iterator */
3661da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos)
3671da177e4SLinus Torvalds {
3686b3286edSKirill Korotaev 	struct mnt_namespace *n = m->private;
3691da177e4SLinus Torvalds 
370390c6843SRam Pai 	down_read(&namespace_sem);
371b0765fb8SPavel Emelianov 	return seq_list_start(&n->list, *pos);
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos)
3751da177e4SLinus Torvalds {
3766b3286edSKirill Korotaev 	struct mnt_namespace *n = m->private;
377b0765fb8SPavel Emelianov 
378b0765fb8SPavel Emelianov 	return seq_list_next(v, &n->list, pos);
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v)
3821da177e4SLinus Torvalds {
383390c6843SRam Pai 	up_read(&namespace_sem);
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds static int show_vfsmnt(struct seq_file *m, void *v)
3871da177e4SLinus Torvalds {
388b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
3891da177e4SLinus Torvalds 	int err = 0;
3901da177e4SLinus Torvalds 	static struct proc_fs_info {
3911da177e4SLinus Torvalds 		int flag;
3921da177e4SLinus Torvalds 		char *str;
3931da177e4SLinus Torvalds 	} fs_info[] = {
3941da177e4SLinus Torvalds 		{ MS_SYNCHRONOUS, ",sync" },
3951da177e4SLinus Torvalds 		{ MS_DIRSYNC, ",dirsync" },
3961da177e4SLinus Torvalds 		{ MS_MANDLOCK, ",mand" },
3971da177e4SLinus Torvalds 		{ 0, NULL }
3981da177e4SLinus Torvalds 	};
3991da177e4SLinus Torvalds 	static struct proc_fs_info mnt_info[] = {
4001da177e4SLinus Torvalds 		{ MNT_NOSUID, ",nosuid" },
4011da177e4SLinus Torvalds 		{ MNT_NODEV, ",nodev" },
4021da177e4SLinus Torvalds 		{ MNT_NOEXEC, ",noexec" },
403fc33a7bbSChristoph Hellwig 		{ MNT_NOATIME, ",noatime" },
404fc33a7bbSChristoph Hellwig 		{ MNT_NODIRATIME, ",nodiratime" },
40547ae32d6SValerie Henson 		{ MNT_RELATIME, ",relatime" },
4061da177e4SLinus Torvalds 		{ 0, NULL }
4071da177e4SLinus Torvalds 	};
4081da177e4SLinus Torvalds 	struct proc_fs_info *fs_infop;
409c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
4121da177e4SLinus Torvalds 	seq_putc(m, ' ');
413c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
4141da177e4SLinus Torvalds 	seq_putc(m, ' ');
4151da177e4SLinus Torvalds 	mangle(m, mnt->mnt_sb->s_type->name);
41679c0b2dfSMiklos Szeredi 	if (mnt->mnt_sb->s_subtype && mnt->mnt_sb->s_subtype[0]) {
41779c0b2dfSMiklos Szeredi 		seq_putc(m, '.');
41879c0b2dfSMiklos Szeredi 		mangle(m, mnt->mnt_sb->s_subtype);
41979c0b2dfSMiklos Szeredi 	}
4201da177e4SLinus Torvalds 	seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
4211da177e4SLinus Torvalds 	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
4221da177e4SLinus Torvalds 		if (mnt->mnt_sb->s_flags & fs_infop->flag)
4231da177e4SLinus Torvalds 			seq_puts(m, fs_infop->str);
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
4261da177e4SLinus Torvalds 		if (mnt->mnt_flags & fs_infop->flag)
4271da177e4SLinus Torvalds 			seq_puts(m, fs_infop->str);
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_op->show_options)
4301da177e4SLinus Torvalds 		err = mnt->mnt_sb->s_op->show_options(m, mnt);
4311da177e4SLinus Torvalds 	seq_puts(m, " 0 0\n");
4321da177e4SLinus Torvalds 	return err;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds struct seq_operations mounts_op = {
4361da177e4SLinus Torvalds 	.start	= m_start,
4371da177e4SLinus Torvalds 	.next	= m_next,
4381da177e4SLinus Torvalds 	.stop	= m_stop,
4391da177e4SLinus Torvalds 	.show	= show_vfsmnt
4401da177e4SLinus Torvalds };
4411da177e4SLinus Torvalds 
442b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v)
443b4629fe2SChuck Lever {
444b0765fb8SPavel Emelianov 	struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list);
445c32c2f63SJan Blunck 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
446b4629fe2SChuck Lever 	int err = 0;
447b4629fe2SChuck Lever 
448b4629fe2SChuck Lever 	/* device */
449b4629fe2SChuck Lever 	if (mnt->mnt_devname) {
450b4629fe2SChuck Lever 		seq_puts(m, "device ");
451b4629fe2SChuck Lever 		mangle(m, mnt->mnt_devname);
452b4629fe2SChuck Lever 	} else
453b4629fe2SChuck Lever 		seq_puts(m, "no device");
454b4629fe2SChuck Lever 
455b4629fe2SChuck Lever 	/* mount point */
456b4629fe2SChuck Lever 	seq_puts(m, " mounted on ");
457c32c2f63SJan Blunck 	seq_path(m, &mnt_path, " \t\n\\");
458b4629fe2SChuck Lever 	seq_putc(m, ' ');
459b4629fe2SChuck Lever 
460b4629fe2SChuck Lever 	/* file system type */
461b4629fe2SChuck Lever 	seq_puts(m, "with fstype ");
462b4629fe2SChuck Lever 	mangle(m, mnt->mnt_sb->s_type->name);
463b4629fe2SChuck Lever 
464b4629fe2SChuck Lever 	/* optional statistics */
465b4629fe2SChuck Lever 	if (mnt->mnt_sb->s_op->show_stats) {
466b4629fe2SChuck Lever 		seq_putc(m, ' ');
467b4629fe2SChuck Lever 		err = mnt->mnt_sb->s_op->show_stats(m, mnt);
468b4629fe2SChuck Lever 	}
469b4629fe2SChuck Lever 
470b4629fe2SChuck Lever 	seq_putc(m, '\n');
471b4629fe2SChuck Lever 	return err;
472b4629fe2SChuck Lever }
473b4629fe2SChuck Lever 
474b4629fe2SChuck Lever struct seq_operations mountstats_op = {
475b4629fe2SChuck Lever 	.start	= m_start,
476b4629fe2SChuck Lever 	.next	= m_next,
477b4629fe2SChuck Lever 	.stop	= m_stop,
478b4629fe2SChuck Lever 	.show	= show_vfsstat,
479b4629fe2SChuck Lever };
480b4629fe2SChuck Lever 
4811da177e4SLinus Torvalds /**
4821da177e4SLinus Torvalds  * may_umount_tree - check if a mount tree is busy
4831da177e4SLinus Torvalds  * @mnt: root of mount tree
4841da177e4SLinus Torvalds  *
4851da177e4SLinus Torvalds  * This is called to check if a tree of mounts has any
4861da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts that are
4871da177e4SLinus Torvalds  * busy.
4881da177e4SLinus Torvalds  */
4891da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt)
4901da177e4SLinus Torvalds {
49136341f64SRam Pai 	int actual_refs = 0;
49236341f64SRam Pai 	int minimum_refs = 0;
49336341f64SRam Pai 	struct vfsmount *p;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
49636341f64SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
4971da177e4SLinus Torvalds 		actual_refs += atomic_read(&p->mnt_count);
4981da177e4SLinus Torvalds 		minimum_refs += 2;
4991da177e4SLinus Torvalds 	}
5001da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	if (actual_refs > minimum_refs)
5031da177e4SLinus Torvalds 		return 0;
504e3474a8eSIan Kent 
505e3474a8eSIan Kent 	return 1;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds /**
5111da177e4SLinus Torvalds  * may_umount - check if a mount point is busy
5121da177e4SLinus Torvalds  * @mnt: root of mount
5131da177e4SLinus Torvalds  *
5141da177e4SLinus Torvalds  * This is called to check if a mount point has any
5151da177e4SLinus Torvalds  * open files, pwds, chroots or sub mounts. If the
5161da177e4SLinus Torvalds  * mount has sub mounts this will return busy
5171da177e4SLinus Torvalds  * regardless of whether the sub mounts are busy.
5181da177e4SLinus Torvalds  *
5191da177e4SLinus Torvalds  * Doesn't take quota and stuff into account. IOW, in some cases it will
5201da177e4SLinus Torvalds  * give false negatives. The main reason why it's here is that we need
5211da177e4SLinus Torvalds  * a non-destructive way to look for easily umountable filesystems.
5221da177e4SLinus Torvalds  */
5231da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt)
5241da177e4SLinus Torvalds {
525e3474a8eSIan Kent 	int ret = 1;
526a05964f3SRam Pai 	spin_lock(&vfsmount_lock);
527a05964f3SRam Pai 	if (propagate_mount_busy(mnt, 2))
528e3474a8eSIan Kent 		ret = 0;
529a05964f3SRam Pai 	spin_unlock(&vfsmount_lock);
530a05964f3SRam Pai 	return ret;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount);
5341da177e4SLinus Torvalds 
535b90fa9aeSRam Pai void release_mounts(struct list_head *head)
5361da177e4SLinus Torvalds {
53770fbcdf4SRam Pai 	struct vfsmount *mnt;
53870fbcdf4SRam Pai 	while (!list_empty(head)) {
539b5e61818SPavel Emelianov 		mnt = list_first_entry(head, struct vfsmount, mnt_hash);
54070fbcdf4SRam Pai 		list_del_init(&mnt->mnt_hash);
54170fbcdf4SRam Pai 		if (mnt->mnt_parent != mnt) {
54270fbcdf4SRam Pai 			struct dentry *dentry;
54370fbcdf4SRam Pai 			struct vfsmount *m;
54470fbcdf4SRam Pai 			spin_lock(&vfsmount_lock);
54570fbcdf4SRam Pai 			dentry = mnt->mnt_mountpoint;
54670fbcdf4SRam Pai 			m = mnt->mnt_parent;
54770fbcdf4SRam Pai 			mnt->mnt_mountpoint = mnt->mnt_root;
54870fbcdf4SRam Pai 			mnt->mnt_parent = mnt;
5497c4b93d8SAl Viro 			m->mnt_ghosts--;
5501da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
55170fbcdf4SRam Pai 			dput(dentry);
55270fbcdf4SRam Pai 			mntput(m);
5531da177e4SLinus Torvalds 		}
5541da177e4SLinus Torvalds 		mntput(mnt);
55570fbcdf4SRam Pai 	}
55670fbcdf4SRam Pai }
55770fbcdf4SRam Pai 
558a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
55970fbcdf4SRam Pai {
56070fbcdf4SRam Pai 	struct vfsmount *p;
56170fbcdf4SRam Pai 
5621bfba4e8SAkinobu Mita 	for (p = mnt; p; p = next_mnt(p, mnt))
5631bfba4e8SAkinobu Mita 		list_move(&p->mnt_hash, kill);
56470fbcdf4SRam Pai 
565a05964f3SRam Pai 	if (propagate)
566a05964f3SRam Pai 		propagate_umount(kill);
567a05964f3SRam Pai 
56870fbcdf4SRam Pai 	list_for_each_entry(p, kill, mnt_hash) {
56970fbcdf4SRam Pai 		list_del_init(&p->mnt_expire);
57070fbcdf4SRam Pai 		list_del_init(&p->mnt_list);
5716b3286edSKirill Korotaev 		__touch_mnt_namespace(p->mnt_ns);
5726b3286edSKirill Korotaev 		p->mnt_ns = NULL;
57370fbcdf4SRam Pai 		list_del_init(&p->mnt_child);
5747c4b93d8SAl Viro 		if (p->mnt_parent != p) {
5757c4b93d8SAl Viro 			p->mnt_parent->mnt_ghosts++;
576f30ac319SAl Viro 			p->mnt_mountpoint->d_mounted--;
5777c4b93d8SAl Viro 		}
578a05964f3SRam Pai 		change_mnt_propagation(p, MS_PRIVATE);
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
582c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts);
583c35038beSAl Viro 
5841da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	struct super_block *sb = mnt->mnt_sb;
5871da177e4SLinus Torvalds 	int retval;
58870fbcdf4SRam Pai 	LIST_HEAD(umount_list);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	retval = security_sb_umount(mnt, flags);
5911da177e4SLinus Torvalds 	if (retval)
5921da177e4SLinus Torvalds 		return retval;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/*
5951da177e4SLinus Torvalds 	 * Allow userspace to request a mountpoint be expired rather than
5961da177e4SLinus Torvalds 	 * unmounting unconditionally. Unmount only happens if:
5971da177e4SLinus Torvalds 	 *  (1) the mark is already set (the mark is cleared by mntput())
5981da177e4SLinus Torvalds 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
5991da177e4SLinus Torvalds 	 */
6001da177e4SLinus Torvalds 	if (flags & MNT_EXPIRE) {
6016ac08c39SJan Blunck 		if (mnt == current->fs->root.mnt ||
6021da177e4SLinus Torvalds 		    flags & (MNT_FORCE | MNT_DETACH))
6031da177e4SLinus Torvalds 			return -EINVAL;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 		if (atomic_read(&mnt->mnt_count) != 2)
6061da177e4SLinus Torvalds 			return -EBUSY;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1))
6091da177e4SLinus Torvalds 			return -EAGAIN;
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	/*
6131da177e4SLinus Torvalds 	 * If we may have to abort operations to get out of this
6141da177e4SLinus Torvalds 	 * mount, and they will themselves hold resources we must
6151da177e4SLinus Torvalds 	 * allow the fs to do things. In the Unix tradition of
6161da177e4SLinus Torvalds 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
6171da177e4SLinus Torvalds 	 * might fail to complete on the first run through as other tasks
6181da177e4SLinus Torvalds 	 * must return, and the like. Thats for the mount program to worry
6191da177e4SLinus Torvalds 	 * about for the moment.
6201da177e4SLinus Torvalds 	 */
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	lock_kernel();
6238b512d9aSTrond Myklebust 	if (sb->s_op->umount_begin)
6248b512d9aSTrond Myklebust 		sb->s_op->umount_begin(mnt, flags);
6251da177e4SLinus Torvalds 	unlock_kernel();
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	/*
6281da177e4SLinus Torvalds 	 * No sense to grab the lock for this test, but test itself looks
6291da177e4SLinus Torvalds 	 * somewhat bogus. Suggestions for better replacement?
6301da177e4SLinus Torvalds 	 * Ho-hum... In principle, we might treat that as umount + switch
6311da177e4SLinus Torvalds 	 * to rootfs. GC would eventually take care of the old vfsmount.
6321da177e4SLinus Torvalds 	 * Actually it makes sense, especially if rootfs would contain a
6331da177e4SLinus Torvalds 	 * /reboot - static binary that would close all descriptors and
6341da177e4SLinus Torvalds 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
6351da177e4SLinus Torvalds 	 */
6366ac08c39SJan Blunck 	if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
6371da177e4SLinus Torvalds 		/*
6381da177e4SLinus Torvalds 		 * Special case for "unmounting" root ...
6391da177e4SLinus Torvalds 		 * we just try to remount it readonly.
6401da177e4SLinus Torvalds 		 */
6411da177e4SLinus Torvalds 		down_write(&sb->s_umount);
6421da177e4SLinus Torvalds 		if (!(sb->s_flags & MS_RDONLY)) {
6431da177e4SLinus Torvalds 			lock_kernel();
6441da177e4SLinus Torvalds 			DQUOT_OFF(sb);
6451da177e4SLinus Torvalds 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
6461da177e4SLinus Torvalds 			unlock_kernel();
6471da177e4SLinus Torvalds 		}
6481da177e4SLinus Torvalds 		up_write(&sb->s_umount);
6491da177e4SLinus Torvalds 		return retval;
6501da177e4SLinus Torvalds 	}
6511da177e4SLinus Torvalds 
652390c6843SRam Pai 	down_write(&namespace_sem);
6531da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
6545addc5ddSAl Viro 	event++;
6551da177e4SLinus Torvalds 
656c35038beSAl Viro 	if (!(flags & MNT_DETACH))
657c35038beSAl Viro 		shrink_submounts(mnt, &umount_list);
658c35038beSAl Viro 
6591da177e4SLinus Torvalds 	retval = -EBUSY;
660a05964f3SRam Pai 	if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
6611da177e4SLinus Torvalds 		if (!list_empty(&mnt->mnt_list))
662a05964f3SRam Pai 			umount_tree(mnt, 1, &umount_list);
6631da177e4SLinus Torvalds 		retval = 0;
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
6661da177e4SLinus Torvalds 	if (retval)
6671da177e4SLinus Torvalds 		security_sb_umount_busy(mnt);
668390c6843SRam Pai 	up_write(&namespace_sem);
66970fbcdf4SRam Pai 	release_mounts(&umount_list);
6701da177e4SLinus Torvalds 	return retval;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds /*
6741da177e4SLinus Torvalds  * Now umount can handle mount points as well as block devices.
6751da177e4SLinus Torvalds  * This is important for filesystems which use unnamed block devices.
6761da177e4SLinus Torvalds  *
6771da177e4SLinus Torvalds  * We now support a flag for forced unmount like the other 'big iron'
6781da177e4SLinus Torvalds  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
6791da177e4SLinus Torvalds  */
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds asmlinkage long sys_umount(char __user * name, int flags)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	struct nameidata nd;
6841da177e4SLinus Torvalds 	int retval;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
6871da177e4SLinus Torvalds 	if (retval)
6881da177e4SLinus Torvalds 		goto out;
6891da177e4SLinus Torvalds 	retval = -EINVAL;
6904ac91378SJan Blunck 	if (nd.path.dentry != nd.path.mnt->mnt_root)
6911da177e4SLinus Torvalds 		goto dput_and_out;
6924ac91378SJan Blunck 	if (!check_mnt(nd.path.mnt))
6931da177e4SLinus Torvalds 		goto dput_and_out;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	retval = -EPERM;
6961da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
6971da177e4SLinus Torvalds 		goto dput_and_out;
6981da177e4SLinus Torvalds 
6994ac91378SJan Blunck 	retval = do_umount(nd.path.mnt, flags);
7001da177e4SLinus Torvalds dput_and_out:
701429731b1SJan Blunck 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
7024ac91378SJan Blunck 	dput(nd.path.dentry);
7034ac91378SJan Blunck 	mntput_no_expire(nd.path.mnt);
7041da177e4SLinus Torvalds out:
7051da177e4SLinus Torvalds 	return retval;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds /*
7111da177e4SLinus Torvalds  *	The 2.0 compatible umount. No flags.
7121da177e4SLinus Torvalds  */
7131da177e4SLinus Torvalds asmlinkage long sys_oldumount(char __user * name)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	return sys_umount(name, 0);
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds #endif
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds static int mount_is_safe(struct nameidata *nd)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	if (capable(CAP_SYS_ADMIN))
7231da177e4SLinus Torvalds 		return 0;
7241da177e4SLinus Torvalds 	return -EPERM;
7251da177e4SLinus Torvalds #ifdef notyet
7264ac91378SJan Blunck 	if (S_ISLNK(nd->path.dentry->d_inode->i_mode))
7271da177e4SLinus Torvalds 		return -EPERM;
7284ac91378SJan Blunck 	if (nd->path.dentry->d_inode->i_mode & S_ISVTX) {
7294ac91378SJan Blunck 		if (current->uid != nd->path.dentry->d_inode->i_uid)
7301da177e4SLinus Torvalds 			return -EPERM;
7311da177e4SLinus Torvalds 	}
732e4543eddSChristoph Hellwig 	if (vfs_permission(nd, MAY_WRITE))
7331da177e4SLinus Torvalds 		return -EPERM;
7341da177e4SLinus Torvalds 	return 0;
7351da177e4SLinus Torvalds #endif
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
738b58fed8bSRam Pai static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	while (1) {
7411da177e4SLinus Torvalds 		if (d == dentry)
7421da177e4SLinus Torvalds 			return 1;
7431da177e4SLinus Torvalds 		if (d == NULL || d == d->d_parent)
7441da177e4SLinus Torvalds 			return 0;
7451da177e4SLinus Torvalds 		d = d->d_parent;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
749b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
75036341f64SRam Pai 					int flag)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	struct vfsmount *res, *p, *q, *r, *s;
7531a390689SAl Viro 	struct path path;
7541da177e4SLinus Torvalds 
7559676f0c6SRam Pai 	if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
7569676f0c6SRam Pai 		return NULL;
7579676f0c6SRam Pai 
75836341f64SRam Pai 	res = q = clone_mnt(mnt, dentry, flag);
7591da177e4SLinus Torvalds 	if (!q)
7601da177e4SLinus Torvalds 		goto Enomem;
7611da177e4SLinus Torvalds 	q->mnt_mountpoint = mnt->mnt_mountpoint;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	p = mnt;
764fdadd65fSDomen Puncer 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
7651da177e4SLinus Torvalds 		if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
7661da177e4SLinus Torvalds 			continue;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 		for (s = r; s; s = next_mnt(s, r)) {
7699676f0c6SRam Pai 			if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
7709676f0c6SRam Pai 				s = skip_mnt_tree(s);
7719676f0c6SRam Pai 				continue;
7729676f0c6SRam Pai 			}
7731da177e4SLinus Torvalds 			while (p != s->mnt_parent) {
7741da177e4SLinus Torvalds 				p = p->mnt_parent;
7751da177e4SLinus Torvalds 				q = q->mnt_parent;
7761da177e4SLinus Torvalds 			}
7771da177e4SLinus Torvalds 			p = s;
7781a390689SAl Viro 			path.mnt = q;
7791a390689SAl Viro 			path.dentry = p->mnt_mountpoint;
78036341f64SRam Pai 			q = clone_mnt(p, p->mnt_root, flag);
7811da177e4SLinus Torvalds 			if (!q)
7821da177e4SLinus Torvalds 				goto Enomem;
7831da177e4SLinus Torvalds 			spin_lock(&vfsmount_lock);
7841da177e4SLinus Torvalds 			list_add_tail(&q->mnt_list, &res->mnt_list);
7851a390689SAl Viro 			attach_mnt(q, &path);
7861da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
7871da177e4SLinus Torvalds 		}
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 	return res;
7901da177e4SLinus Torvalds Enomem:
7911da177e4SLinus Torvalds 	if (res) {
79270fbcdf4SRam Pai 		LIST_HEAD(umount_list);
7931da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
794a05964f3SRam Pai 		umount_tree(res, 0, &umount_list);
7951da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
79670fbcdf4SRam Pai 		release_mounts(&umount_list);
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 	return NULL;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8018aec0809SAl Viro struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry)
8028aec0809SAl Viro {
8038aec0809SAl Viro 	struct vfsmount *tree;
8048aec0809SAl Viro 	down_read(&namespace_sem);
8058aec0809SAl Viro 	tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE);
8068aec0809SAl Viro 	up_read(&namespace_sem);
8078aec0809SAl Viro 	return tree;
8088aec0809SAl Viro }
8098aec0809SAl Viro 
8108aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt)
8118aec0809SAl Viro {
8128aec0809SAl Viro 	LIST_HEAD(umount_list);
8138aec0809SAl Viro 	down_read(&namespace_sem);
8148aec0809SAl Viro 	spin_lock(&vfsmount_lock);
8158aec0809SAl Viro 	umount_tree(mnt, 0, &umount_list);
8168aec0809SAl Viro 	spin_unlock(&vfsmount_lock);
8178aec0809SAl Viro 	up_read(&namespace_sem);
8188aec0809SAl Viro 	release_mounts(&umount_list);
8198aec0809SAl Viro }
8208aec0809SAl Viro 
821b90fa9aeSRam Pai /*
822b90fa9aeSRam Pai  *  @source_mnt : mount tree to be attached
823b90fa9aeSRam Pai  *  @nd         : place the mount tree @source_mnt is attached
82421444403SRam Pai  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
82521444403SRam Pai  *  		   store the parent mount and mountpoint dentry.
82621444403SRam Pai  *  		   (done when source_mnt is moved)
827b90fa9aeSRam Pai  *
828b90fa9aeSRam Pai  *  NOTE: in the table below explains the semantics when a source mount
829b90fa9aeSRam Pai  *  of a given type is attached to a destination mount of a given type.
8309676f0c6SRam Pai  * ---------------------------------------------------------------------------
831b90fa9aeSRam Pai  * |         BIND MOUNT OPERATION                                            |
8329676f0c6SRam Pai  * |**************************************************************************
8339676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
8349676f0c6SRam Pai  * | dest     |               |                |                |            |
8359676f0c6SRam Pai  * |   |      |               |                |                |            |
8369676f0c6SRam Pai  * |   v      |               |                |                |            |
8379676f0c6SRam Pai  * |**************************************************************************
8389676f0c6SRam Pai  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
8395afe0022SRam Pai  * |          |               |                |                |            |
8409676f0c6SRam Pai  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
8419676f0c6SRam Pai  * ***************************************************************************
842b90fa9aeSRam Pai  * A bind operation clones the source mount and mounts the clone on the
843b90fa9aeSRam Pai  * destination mount.
844b90fa9aeSRam Pai  *
845b90fa9aeSRam Pai  * (++)  the cloned mount is propagated to all the mounts in the propagation
846b90fa9aeSRam Pai  * 	 tree of the destination mount and the cloned mount is added to
847b90fa9aeSRam Pai  * 	 the peer group of the source mount.
848b90fa9aeSRam Pai  * (+)   the cloned mount is created under the destination mount and is marked
849b90fa9aeSRam Pai  *       as shared. The cloned mount is added to the peer group of the source
850b90fa9aeSRam Pai  *       mount.
8515afe0022SRam Pai  * (+++) the mount is propagated to all the mounts in the propagation tree
8525afe0022SRam Pai  *       of the destination mount and the cloned mount is made slave
8535afe0022SRam Pai  *       of the same master as that of the source mount. The cloned mount
8545afe0022SRam Pai  *       is marked as 'shared and slave'.
8555afe0022SRam Pai  * (*)   the cloned mount is made a slave of the same master as that of the
8565afe0022SRam Pai  * 	 source mount.
8575afe0022SRam Pai  *
8589676f0c6SRam Pai  * ---------------------------------------------------------------------------
85921444403SRam Pai  * |         		MOVE MOUNT OPERATION                                 |
8609676f0c6SRam Pai  * |**************************************************************************
8619676f0c6SRam Pai  * | source-->| shared        |       private  |       slave    | unbindable |
8629676f0c6SRam Pai  * | dest     |               |                |                |            |
8639676f0c6SRam Pai  * |   |      |               |                |                |            |
8649676f0c6SRam Pai  * |   v      |               |                |                |            |
8659676f0c6SRam Pai  * |**************************************************************************
8669676f0c6SRam Pai  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
8675afe0022SRam Pai  * |          |               |                |                |            |
8689676f0c6SRam Pai  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
8699676f0c6SRam Pai  * ***************************************************************************
8705afe0022SRam Pai  *
8715afe0022SRam Pai  * (+)  the mount is moved to the destination. And is then propagated to
8725afe0022SRam Pai  * 	all the mounts in the propagation tree of the destination mount.
87321444403SRam Pai  * (+*)  the mount is moved to the destination.
8745afe0022SRam Pai  * (+++)  the mount is moved to the destination and is then propagated to
8755afe0022SRam Pai  * 	all the mounts belonging to the destination mount's propagation tree.
8765afe0022SRam Pai  * 	the mount is marked as 'shared and slave'.
8775afe0022SRam Pai  * (*)	the mount continues to be a slave at the new location.
878b90fa9aeSRam Pai  *
879b90fa9aeSRam Pai  * if the source mount is a tree, the operations explained above is
880b90fa9aeSRam Pai  * applied to each mount in the tree.
881b90fa9aeSRam Pai  * Must be called without spinlocks held, since this function can sleep
882b90fa9aeSRam Pai  * in allocations.
883b90fa9aeSRam Pai  */
884b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt,
8851a390689SAl Viro 			struct path *path, struct path *parent_path)
886b90fa9aeSRam Pai {
887b90fa9aeSRam Pai 	LIST_HEAD(tree_list);
8881a390689SAl Viro 	struct vfsmount *dest_mnt = path->mnt;
8891a390689SAl Viro 	struct dentry *dest_dentry = path->dentry;
890b90fa9aeSRam Pai 	struct vfsmount *child, *p;
891b90fa9aeSRam Pai 
892b90fa9aeSRam Pai 	if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
893b90fa9aeSRam Pai 		return -EINVAL;
894b90fa9aeSRam Pai 
895b90fa9aeSRam Pai 	if (IS_MNT_SHARED(dest_mnt)) {
896b90fa9aeSRam Pai 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
897b90fa9aeSRam Pai 			set_mnt_shared(p);
898b90fa9aeSRam Pai 	}
899b90fa9aeSRam Pai 
900b90fa9aeSRam Pai 	spin_lock(&vfsmount_lock);
9011a390689SAl Viro 	if (parent_path) {
9021a390689SAl Viro 		detach_mnt(source_mnt, parent_path);
9031a390689SAl Viro 		attach_mnt(source_mnt, path);
9046b3286edSKirill Korotaev 		touch_mnt_namespace(current->nsproxy->mnt_ns);
90521444403SRam Pai 	} else {
906b90fa9aeSRam Pai 		mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
907b90fa9aeSRam Pai 		commit_tree(source_mnt);
90821444403SRam Pai 	}
909b90fa9aeSRam Pai 
910b90fa9aeSRam Pai 	list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
911b90fa9aeSRam Pai 		list_del_init(&child->mnt_hash);
912b90fa9aeSRam Pai 		commit_tree(child);
913b90fa9aeSRam Pai 	}
914b90fa9aeSRam Pai 	spin_unlock(&vfsmount_lock);
915b90fa9aeSRam Pai 	return 0;
916b90fa9aeSRam Pai }
917b90fa9aeSRam Pai 
9181da177e4SLinus Torvalds static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	int err;
9211da177e4SLinus Torvalds 	if (mnt->mnt_sb->s_flags & MS_NOUSER)
9221da177e4SLinus Torvalds 		return -EINVAL;
9231da177e4SLinus Torvalds 
9244ac91378SJan Blunck 	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
9251da177e4SLinus Torvalds 	      S_ISDIR(mnt->mnt_root->d_inode->i_mode))
9261da177e4SLinus Torvalds 		return -ENOTDIR;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	err = -ENOENT;
9294ac91378SJan Blunck 	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
9304ac91378SJan Blunck 	if (IS_DEADDIR(nd->path.dentry->d_inode))
9311da177e4SLinus Torvalds 		goto out_unlock;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	err = security_sb_check_sb(mnt, nd);
9341da177e4SLinus Torvalds 	if (err)
9351da177e4SLinus Torvalds 		goto out_unlock;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	err = -ENOENT;
9384ac91378SJan Blunck 	if (IS_ROOT(nd->path.dentry) || !d_unhashed(nd->path.dentry))
9391a390689SAl Viro 		err = attach_recursive_mnt(mnt, &nd->path, NULL);
9401da177e4SLinus Torvalds out_unlock:
9414ac91378SJan Blunck 	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
9421da177e4SLinus Torvalds 	if (!err)
9431da177e4SLinus Torvalds 		security_sb_post_addmount(mnt, nd);
9441da177e4SLinus Torvalds 	return err;
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds /*
94807b20889SRam Pai  * recursively change the type of the mountpoint.
9492dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
95007b20889SRam Pai  */
9512dafe1c4SEric Sandeen static noinline int do_change_type(struct nameidata *nd, int flag)
95207b20889SRam Pai {
9534ac91378SJan Blunck 	struct vfsmount *m, *mnt = nd->path.mnt;
95407b20889SRam Pai 	int recurse = flag & MS_REC;
95507b20889SRam Pai 	int type = flag & ~MS_REC;
95607b20889SRam Pai 
957ee6f9582SMiklos Szeredi 	if (!capable(CAP_SYS_ADMIN))
958ee6f9582SMiklos Szeredi 		return -EPERM;
959ee6f9582SMiklos Szeredi 
9604ac91378SJan Blunck 	if (nd->path.dentry != nd->path.mnt->mnt_root)
96107b20889SRam Pai 		return -EINVAL;
96207b20889SRam Pai 
96307b20889SRam Pai 	down_write(&namespace_sem);
96407b20889SRam Pai 	spin_lock(&vfsmount_lock);
96507b20889SRam Pai 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
96607b20889SRam Pai 		change_mnt_propagation(m, type);
96707b20889SRam Pai 	spin_unlock(&vfsmount_lock);
96807b20889SRam Pai 	up_write(&namespace_sem);
96907b20889SRam Pai 	return 0;
97007b20889SRam Pai }
97107b20889SRam Pai 
97207b20889SRam Pai /*
9731da177e4SLinus Torvalds  * do loopback mount.
9742dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
9751da177e4SLinus Torvalds  */
9762dafe1c4SEric Sandeen static noinline int do_loopback(struct nameidata *nd, char *old_name,
9772dafe1c4SEric Sandeen 				int recurse)
9781da177e4SLinus Torvalds {
9791da177e4SLinus Torvalds 	struct nameidata old_nd;
9801da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
9811da177e4SLinus Torvalds 	int err = mount_is_safe(nd);
9821da177e4SLinus Torvalds 	if (err)
9831da177e4SLinus Torvalds 		return err;
9841da177e4SLinus Torvalds 	if (!old_name || !*old_name)
9851da177e4SLinus Torvalds 		return -EINVAL;
9861da177e4SLinus Torvalds 	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
9871da177e4SLinus Torvalds 	if (err)
9881da177e4SLinus Torvalds 		return err;
9891da177e4SLinus Torvalds 
990390c6843SRam Pai 	down_write(&namespace_sem);
9911da177e4SLinus Torvalds 	err = -EINVAL;
9924ac91378SJan Blunck 	if (IS_MNT_UNBINDABLE(old_nd.path.mnt))
9939676f0c6SRam Pai 		goto out;
9949676f0c6SRam Pai 
9954ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
996ccd48bc7SAl Viro 		goto out;
997ccd48bc7SAl Viro 
9981da177e4SLinus Torvalds 	err = -ENOMEM;
9991da177e4SLinus Torvalds 	if (recurse)
10004ac91378SJan Blunck 		mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0);
10011da177e4SLinus Torvalds 	else
10024ac91378SJan Blunck 		mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0);
10031da177e4SLinus Torvalds 
1004ccd48bc7SAl Viro 	if (!mnt)
1005ccd48bc7SAl Viro 		goto out;
1006ccd48bc7SAl Viro 
10071da177e4SLinus Torvalds 	err = graft_tree(mnt, nd);
10081da177e4SLinus Torvalds 	if (err) {
100970fbcdf4SRam Pai 		LIST_HEAD(umount_list);
10101da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
1011a05964f3SRam Pai 		umount_tree(mnt, 0, &umount_list);
10121da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
101370fbcdf4SRam Pai 		release_mounts(&umount_list);
10145b83d2c5SRam Pai 	}
10151da177e4SLinus Torvalds 
1016ccd48bc7SAl Viro out:
1017390c6843SRam Pai 	up_write(&namespace_sem);
10181d957f9bSJan Blunck 	path_put(&old_nd.path);
10191da177e4SLinus Torvalds 	return err;
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds /*
10231da177e4SLinus Torvalds  * change filesystem flags. dir should be a physical root of filesystem.
10241da177e4SLinus Torvalds  * If you've mounted a non-root directory somewhere and want to do remount
10251da177e4SLinus Torvalds  * on it - tough luck.
10262dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
10271da177e4SLinus Torvalds  */
10282dafe1c4SEric Sandeen static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags,
10291da177e4SLinus Torvalds 		      void *data)
10301da177e4SLinus Torvalds {
10311da177e4SLinus Torvalds 	int err;
10324ac91378SJan Blunck 	struct super_block *sb = nd->path.mnt->mnt_sb;
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
10351da177e4SLinus Torvalds 		return -EPERM;
10361da177e4SLinus Torvalds 
10374ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt))
10381da177e4SLinus Torvalds 		return -EINVAL;
10391da177e4SLinus Torvalds 
10404ac91378SJan Blunck 	if (nd->path.dentry != nd->path.mnt->mnt_root)
10411da177e4SLinus Torvalds 		return -EINVAL;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	down_write(&sb->s_umount);
10441da177e4SLinus Torvalds 	err = do_remount_sb(sb, flags, data, 0);
10451da177e4SLinus Torvalds 	if (!err)
10464ac91378SJan Blunck 		nd->path.mnt->mnt_flags = mnt_flags;
10471da177e4SLinus Torvalds 	up_write(&sb->s_umount);
10481da177e4SLinus Torvalds 	if (!err)
10494ac91378SJan Blunck 		security_sb_post_remount(nd->path.mnt, flags, data);
10501da177e4SLinus Torvalds 	return err;
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
10539676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt)
10549676f0c6SRam Pai {
10559676f0c6SRam Pai 	struct vfsmount *p;
10569676f0c6SRam Pai 	for (p = mnt; p; p = next_mnt(p, mnt)) {
10579676f0c6SRam Pai 		if (IS_MNT_UNBINDABLE(p))
10589676f0c6SRam Pai 			return 1;
10599676f0c6SRam Pai 	}
10609676f0c6SRam Pai 	return 0;
10619676f0c6SRam Pai }
10629676f0c6SRam Pai 
10632dafe1c4SEric Sandeen /*
10642dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
10652dafe1c4SEric Sandeen  */
10662dafe1c4SEric Sandeen static noinline int do_move_mount(struct nameidata *nd, char *old_name)
10671da177e4SLinus Torvalds {
10681a390689SAl Viro 	struct nameidata old_nd;
10691a390689SAl Viro 	struct path parent_path;
10701da177e4SLinus Torvalds 	struct vfsmount *p;
10711da177e4SLinus Torvalds 	int err = 0;
10721da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
10731da177e4SLinus Torvalds 		return -EPERM;
10741da177e4SLinus Torvalds 	if (!old_name || !*old_name)
10751da177e4SLinus Torvalds 		return -EINVAL;
10761da177e4SLinus Torvalds 	err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
10771da177e4SLinus Torvalds 	if (err)
10781da177e4SLinus Torvalds 		return err;
10791da177e4SLinus Torvalds 
1080390c6843SRam Pai 	down_write(&namespace_sem);
10814ac91378SJan Blunck 	while (d_mountpoint(nd->path.dentry) &&
10824ac91378SJan Blunck 	       follow_down(&nd->path.mnt, &nd->path.dentry))
10831da177e4SLinus Torvalds 		;
10841da177e4SLinus Torvalds 	err = -EINVAL;
10854ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt))
10861da177e4SLinus Torvalds 		goto out;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	err = -ENOENT;
10894ac91378SJan Blunck 	mutex_lock(&nd->path.dentry->d_inode->i_mutex);
10904ac91378SJan Blunck 	if (IS_DEADDIR(nd->path.dentry->d_inode))
10911da177e4SLinus Torvalds 		goto out1;
10921da177e4SLinus Torvalds 
10934ac91378SJan Blunck 	if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry))
109421444403SRam Pai 		goto out1;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	err = -EINVAL;
10974ac91378SJan Blunck 	if (old_nd.path.dentry != old_nd.path.mnt->mnt_root)
109821444403SRam Pai 		goto out1;
10991da177e4SLinus Torvalds 
11004ac91378SJan Blunck 	if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent)
110121444403SRam Pai 		goto out1;
11021da177e4SLinus Torvalds 
11034ac91378SJan Blunck 	if (S_ISDIR(nd->path.dentry->d_inode->i_mode) !=
11044ac91378SJan Blunck 	      S_ISDIR(old_nd.path.dentry->d_inode->i_mode))
110521444403SRam Pai 		goto out1;
110621444403SRam Pai 	/*
110721444403SRam Pai 	 * Don't move a mount residing in a shared parent.
110821444403SRam Pai 	 */
11094ac91378SJan Blunck 	if (old_nd.path.mnt->mnt_parent &&
11104ac91378SJan Blunck 	    IS_MNT_SHARED(old_nd.path.mnt->mnt_parent))
111121444403SRam Pai 		goto out1;
11129676f0c6SRam Pai 	/*
11139676f0c6SRam Pai 	 * Don't move a mount tree containing unbindable mounts to a destination
11149676f0c6SRam Pai 	 * mount which is shared.
11159676f0c6SRam Pai 	 */
11164ac91378SJan Blunck 	if (IS_MNT_SHARED(nd->path.mnt) &&
11174ac91378SJan Blunck 	    tree_contains_unbindable(old_nd.path.mnt))
11189676f0c6SRam Pai 		goto out1;
11191da177e4SLinus Torvalds 	err = -ELOOP;
11204ac91378SJan Blunck 	for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent)
11214ac91378SJan Blunck 		if (p == old_nd.path.mnt)
112221444403SRam Pai 			goto out1;
11231da177e4SLinus Torvalds 
11241a390689SAl Viro 	err = attach_recursive_mnt(old_nd.path.mnt, &nd->path, &parent_path);
11254ac91378SJan Blunck 	if (err)
112621444403SRam Pai 		goto out1;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	/* if the mount is moved, it should no longer be expire
11291da177e4SLinus Torvalds 	 * automatically */
11304ac91378SJan Blunck 	list_del_init(&old_nd.path.mnt->mnt_expire);
11311da177e4SLinus Torvalds out1:
11324ac91378SJan Blunck 	mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
11331da177e4SLinus Torvalds out:
1134390c6843SRam Pai 	up_write(&namespace_sem);
11351da177e4SLinus Torvalds 	if (!err)
11361a390689SAl Viro 		path_put(&parent_path);
11371d957f9bSJan Blunck 	path_put(&old_nd.path);
11381da177e4SLinus Torvalds 	return err;
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds /*
11421da177e4SLinus Torvalds  * create a new mount for userspace and request it to be added into the
11431da177e4SLinus Torvalds  * namespace's tree
11442dafe1c4SEric Sandeen  * noinline this do_mount helper to save do_mount stack space.
11451da177e4SLinus Torvalds  */
11462dafe1c4SEric Sandeen static noinline int do_new_mount(struct nameidata *nd, char *type, int flags,
11471da177e4SLinus Torvalds 			int mnt_flags, char *name, void *data)
11481da177e4SLinus Torvalds {
11491da177e4SLinus Torvalds 	struct vfsmount *mnt;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	if (!type || !memchr(type, 0, PAGE_SIZE))
11521da177e4SLinus Torvalds 		return -EINVAL;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	/* we need capabilities... */
11551da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
11561da177e4SLinus Torvalds 		return -EPERM;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	mnt = do_kern_mount(type, flags, name, data);
11591da177e4SLinus Torvalds 	if (IS_ERR(mnt))
11601da177e4SLinus Torvalds 		return PTR_ERR(mnt);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	return do_add_mount(mnt, nd, mnt_flags, NULL);
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds /*
11661da177e4SLinus Torvalds  * add a mount into a namespace's mount tree
11671da177e4SLinus Torvalds  * - provide the option of adding the new mount to an expiration list
11681da177e4SLinus Torvalds  */
11691da177e4SLinus Torvalds int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
11701da177e4SLinus Torvalds 		 int mnt_flags, struct list_head *fslist)
11711da177e4SLinus Torvalds {
11721da177e4SLinus Torvalds 	int err;
11731da177e4SLinus Torvalds 
1174390c6843SRam Pai 	down_write(&namespace_sem);
11751da177e4SLinus Torvalds 	/* Something was mounted here while we slept */
11764ac91378SJan Blunck 	while (d_mountpoint(nd->path.dentry) &&
11774ac91378SJan Blunck 	       follow_down(&nd->path.mnt, &nd->path.dentry))
11781da177e4SLinus Torvalds 		;
11791da177e4SLinus Torvalds 	err = -EINVAL;
11804ac91378SJan Blunck 	if (!check_mnt(nd->path.mnt))
11811da177e4SLinus Torvalds 		goto unlock;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	/* Refuse the same filesystem on the same mount point */
11841da177e4SLinus Torvalds 	err = -EBUSY;
11854ac91378SJan Blunck 	if (nd->path.mnt->mnt_sb == newmnt->mnt_sb &&
11864ac91378SJan Blunck 	    nd->path.mnt->mnt_root == nd->path.dentry)
11871da177e4SLinus Torvalds 		goto unlock;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	err = -EINVAL;
11901da177e4SLinus Torvalds 	if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
11911da177e4SLinus Torvalds 		goto unlock;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	newmnt->mnt_flags = mnt_flags;
11945b83d2c5SRam Pai 	if ((err = graft_tree(newmnt, nd)))
11955b83d2c5SRam Pai 		goto unlock;
11961da177e4SLinus Torvalds 
11976758f953SAl Viro 	if (fslist) /* add to the specified expiration list */
119855e700b9SMiklos Szeredi 		list_add_tail(&newmnt->mnt_expire, fslist);
11996758f953SAl Viro 
1200390c6843SRam Pai 	up_write(&namespace_sem);
12015b83d2c5SRam Pai 	return 0;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds unlock:
1204390c6843SRam Pai 	up_write(&namespace_sem);
12051da177e4SLinus Torvalds 	mntput(newmnt);
12061da177e4SLinus Torvalds 	return err;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount);
12101da177e4SLinus Torvalds 
12115528f911STrond Myklebust /*
12121da177e4SLinus Torvalds  * process a list of expirable mountpoints with the intent of discarding any
12131da177e4SLinus Torvalds  * mountpoints that aren't in use and haven't been touched since last we came
12141da177e4SLinus Torvalds  * here
12151da177e4SLinus Torvalds  */
12161da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts)
12171da177e4SLinus Torvalds {
12181da177e4SLinus Torvalds 	struct vfsmount *mnt, *next;
12191da177e4SLinus Torvalds 	LIST_HEAD(graveyard);
1220bcc5c7d2SAl Viro 	LIST_HEAD(umounts);
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	if (list_empty(mounts))
12231da177e4SLinus Torvalds 		return;
12241da177e4SLinus Torvalds 
1225bcc5c7d2SAl Viro 	down_write(&namespace_sem);
12261da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	/* extract from the expiration list every vfsmount that matches the
12291da177e4SLinus Torvalds 	 * following criteria:
12301da177e4SLinus Torvalds 	 * - only referenced by its parent vfsmount
12311da177e4SLinus Torvalds 	 * - still marked for expiry (marked on the last call here; marks are
12321da177e4SLinus Torvalds 	 *   cleared by mntput())
12331da177e4SLinus Torvalds 	 */
123455e700b9SMiklos Szeredi 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
12351da177e4SLinus Torvalds 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1236bcc5c7d2SAl Viro 			propagate_mount_busy(mnt, 1))
12371da177e4SLinus Torvalds 			continue;
123855e700b9SMiklos Szeredi 		list_move(&mnt->mnt_expire, &graveyard);
12391da177e4SLinus Torvalds 	}
1240bcc5c7d2SAl Viro 	while (!list_empty(&graveyard)) {
1241bcc5c7d2SAl Viro 		mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire);
1242bcc5c7d2SAl Viro 		touch_mnt_namespace(mnt->mnt_ns);
1243bcc5c7d2SAl Viro 		umount_tree(mnt, 1, &umounts);
1244bcc5c7d2SAl Viro 	}
12451da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
1246bcc5c7d2SAl Viro 	up_write(&namespace_sem);
1247bcc5c7d2SAl Viro 
1248bcc5c7d2SAl Viro 	release_mounts(&umounts);
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds /*
12545528f911STrond Myklebust  * Ripoff of 'select_parent()'
12555528f911STrond Myklebust  *
12565528f911STrond Myklebust  * search the list of submounts for a given mountpoint, and move any
12575528f911STrond Myklebust  * shrinkable submounts to the 'graveyard' list.
12585528f911STrond Myklebust  */
12595528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
12605528f911STrond Myklebust {
12615528f911STrond Myklebust 	struct vfsmount *this_parent = parent;
12625528f911STrond Myklebust 	struct list_head *next;
12635528f911STrond Myklebust 	int found = 0;
12645528f911STrond Myklebust 
12655528f911STrond Myklebust repeat:
12665528f911STrond Myklebust 	next = this_parent->mnt_mounts.next;
12675528f911STrond Myklebust resume:
12685528f911STrond Myklebust 	while (next != &this_parent->mnt_mounts) {
12695528f911STrond Myklebust 		struct list_head *tmp = next;
12705528f911STrond Myklebust 		struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
12715528f911STrond Myklebust 
12725528f911STrond Myklebust 		next = tmp->next;
12735528f911STrond Myklebust 		if (!(mnt->mnt_flags & MNT_SHRINKABLE))
12745528f911STrond Myklebust 			continue;
12755528f911STrond Myklebust 		/*
12765528f911STrond Myklebust 		 * Descend a level if the d_mounts list is non-empty.
12775528f911STrond Myklebust 		 */
12785528f911STrond Myklebust 		if (!list_empty(&mnt->mnt_mounts)) {
12795528f911STrond Myklebust 			this_parent = mnt;
12805528f911STrond Myklebust 			goto repeat;
12815528f911STrond Myklebust 		}
12825528f911STrond Myklebust 
12835528f911STrond Myklebust 		if (!propagate_mount_busy(mnt, 1)) {
12845528f911STrond Myklebust 			list_move_tail(&mnt->mnt_expire, graveyard);
12855528f911STrond Myklebust 			found++;
12865528f911STrond Myklebust 		}
12875528f911STrond Myklebust 	}
12885528f911STrond Myklebust 	/*
12895528f911STrond Myklebust 	 * All done at this level ... ascend and resume the search
12905528f911STrond Myklebust 	 */
12915528f911STrond Myklebust 	if (this_parent != parent) {
12925528f911STrond Myklebust 		next = this_parent->mnt_child.next;
12935528f911STrond Myklebust 		this_parent = this_parent->mnt_parent;
12945528f911STrond Myklebust 		goto resume;
12955528f911STrond Myklebust 	}
12965528f911STrond Myklebust 	return found;
12975528f911STrond Myklebust }
12985528f911STrond Myklebust 
12995528f911STrond Myklebust /*
13005528f911STrond Myklebust  * process a list of expirable mountpoints with the intent of discarding any
13015528f911STrond Myklebust  * submounts of a specific parent mountpoint
13025528f911STrond Myklebust  */
1303c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts)
13045528f911STrond Myklebust {
13055528f911STrond Myklebust 	LIST_HEAD(graveyard);
1306c35038beSAl Viro 	struct vfsmount *m;
13075528f911STrond Myklebust 
13085528f911STrond Myklebust 	/* extract submounts of 'mountpoint' from the expiration list */
1309c35038beSAl Viro 	while (select_submounts(mnt, &graveyard)) {
1310bcc5c7d2SAl Viro 		while (!list_empty(&graveyard)) {
1311c35038beSAl Viro 			m = list_first_entry(&graveyard, struct vfsmount,
1312bcc5c7d2SAl Viro 						mnt_expire);
1313bcc5c7d2SAl Viro 			touch_mnt_namespace(mnt->mnt_ns);
1314c35038beSAl Viro 			umount_tree(mnt, 1, umounts);
1315bcc5c7d2SAl Viro 		}
1316bcc5c7d2SAl Viro 	}
13175528f911STrond Myklebust }
13185528f911STrond Myklebust 
13195528f911STrond Myklebust /*
13201da177e4SLinus Torvalds  * Some copy_from_user() implementations do not return the exact number of
13211da177e4SLinus Torvalds  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
13221da177e4SLinus Torvalds  * Note that this function differs from copy_from_user() in that it will oops
13231da177e4SLinus Torvalds  * on bad values of `to', rather than returning a short copy.
13241da177e4SLinus Torvalds  */
1325b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from,
1326b58fed8bSRam Pai 				 unsigned long n)
13271da177e4SLinus Torvalds {
13281da177e4SLinus Torvalds 	char *t = to;
13291da177e4SLinus Torvalds 	const char __user *f = from;
13301da177e4SLinus Torvalds 	char c;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, from, n))
13331da177e4SLinus Torvalds 		return n;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	while (n) {
13361da177e4SLinus Torvalds 		if (__get_user(c, f)) {
13371da177e4SLinus Torvalds 			memset(t, 0, n);
13381da177e4SLinus Torvalds 			break;
13391da177e4SLinus Torvalds 		}
13401da177e4SLinus Torvalds 		*t++ = c;
13411da177e4SLinus Torvalds 		f++;
13421da177e4SLinus Torvalds 		n--;
13431da177e4SLinus Torvalds 	}
13441da177e4SLinus Torvalds 	return n;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where)
13481da177e4SLinus Torvalds {
13491da177e4SLinus Torvalds 	int i;
13501da177e4SLinus Torvalds 	unsigned long page;
13511da177e4SLinus Torvalds 	unsigned long size;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	*where = 0;
13541da177e4SLinus Torvalds 	if (!data)
13551da177e4SLinus Torvalds 		return 0;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	if (!(page = __get_free_page(GFP_KERNEL)))
13581da177e4SLinus Torvalds 		return -ENOMEM;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	/* We only care that *some* data at the address the user
13611da177e4SLinus Torvalds 	 * gave us is valid.  Just in case, we'll zero
13621da177e4SLinus Torvalds 	 * the remainder of the page.
13631da177e4SLinus Torvalds 	 */
13641da177e4SLinus Torvalds 	/* copy_from_user cannot cross TASK_SIZE ! */
13651da177e4SLinus Torvalds 	size = TASK_SIZE - (unsigned long)data;
13661da177e4SLinus Torvalds 	if (size > PAGE_SIZE)
13671da177e4SLinus Torvalds 		size = PAGE_SIZE;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	i = size - exact_copy_from_user((void *)page, data, size);
13701da177e4SLinus Torvalds 	if (!i) {
13711da177e4SLinus Torvalds 		free_page(page);
13721da177e4SLinus Torvalds 		return -EFAULT;
13731da177e4SLinus Torvalds 	}
13741da177e4SLinus Torvalds 	if (i != PAGE_SIZE)
13751da177e4SLinus Torvalds 		memset((char *)page + i, 0, PAGE_SIZE - i);
13761da177e4SLinus Torvalds 	*where = page;
13771da177e4SLinus Torvalds 	return 0;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds /*
13811da177e4SLinus Torvalds  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
13821da177e4SLinus Torvalds  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
13831da177e4SLinus Torvalds  *
13841da177e4SLinus Torvalds  * data is a (void *) that can point to any structure up to
13851da177e4SLinus Torvalds  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
13861da177e4SLinus Torvalds  * information (or be NULL).
13871da177e4SLinus Torvalds  *
13881da177e4SLinus Torvalds  * Pre-0.97 versions of mount() didn't have a flags word.
13891da177e4SLinus Torvalds  * When the flags word was introduced its top half was required
13901da177e4SLinus Torvalds  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
13911da177e4SLinus Torvalds  * Therefore, if this magic number is present, it carries no information
13921da177e4SLinus Torvalds  * and must be discarded.
13931da177e4SLinus Torvalds  */
13941da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page,
13951da177e4SLinus Torvalds 		  unsigned long flags, void *data_page)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds 	struct nameidata nd;
13981da177e4SLinus Torvalds 	int retval = 0;
13991da177e4SLinus Torvalds 	int mnt_flags = 0;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	/* Discard magic */
14021da177e4SLinus Torvalds 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
14031da177e4SLinus Torvalds 		flags &= ~MS_MGC_MSK;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	/* Basic sanity checks */
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
14081da177e4SLinus Torvalds 		return -EINVAL;
14091da177e4SLinus Torvalds 	if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
14101da177e4SLinus Torvalds 		return -EINVAL;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	if (data_page)
14131da177e4SLinus Torvalds 		((char *)data_page)[PAGE_SIZE - 1] = 0;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	/* Separate the per-mountpoint flags */
14161da177e4SLinus Torvalds 	if (flags & MS_NOSUID)
14171da177e4SLinus Torvalds 		mnt_flags |= MNT_NOSUID;
14181da177e4SLinus Torvalds 	if (flags & MS_NODEV)
14191da177e4SLinus Torvalds 		mnt_flags |= MNT_NODEV;
14201da177e4SLinus Torvalds 	if (flags & MS_NOEXEC)
14211da177e4SLinus Torvalds 		mnt_flags |= MNT_NOEXEC;
1422fc33a7bbSChristoph Hellwig 	if (flags & MS_NOATIME)
1423fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NOATIME;
1424fc33a7bbSChristoph Hellwig 	if (flags & MS_NODIRATIME)
1425fc33a7bbSChristoph Hellwig 		mnt_flags |= MNT_NODIRATIME;
142647ae32d6SValerie Henson 	if (flags & MS_RELATIME)
142747ae32d6SValerie Henson 		mnt_flags |= MNT_RELATIME;
1428fc33a7bbSChristoph Hellwig 
1429fc33a7bbSChristoph Hellwig 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
14308bf9725cSPavel Emelyanov 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT);
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	/* ... and get the mountpoint */
14331da177e4SLinus Torvalds 	retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
14341da177e4SLinus Torvalds 	if (retval)
14351da177e4SLinus Torvalds 		return retval;
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
14381da177e4SLinus Torvalds 	if (retval)
14391da177e4SLinus Torvalds 		goto dput_out;
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
14421da177e4SLinus Torvalds 		retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
14431da177e4SLinus Torvalds 				    data_page);
14441da177e4SLinus Torvalds 	else if (flags & MS_BIND)
1445eee391a6SAndrew Morton 		retval = do_loopback(&nd, dev_name, flags & MS_REC);
14469676f0c6SRam Pai 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
144707b20889SRam Pai 		retval = do_change_type(&nd, flags);
14481da177e4SLinus Torvalds 	else if (flags & MS_MOVE)
14491da177e4SLinus Torvalds 		retval = do_move_mount(&nd, dev_name);
14501da177e4SLinus Torvalds 	else
14511da177e4SLinus Torvalds 		retval = do_new_mount(&nd, type_page, flags, mnt_flags,
14521da177e4SLinus Torvalds 				      dev_name, data_page);
14531da177e4SLinus Torvalds dput_out:
14541d957f9bSJan Blunck 	path_put(&nd.path);
14551da177e4SLinus Torvalds 	return retval;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
1458741a2951SJANAK DESAI /*
1459741a2951SJANAK DESAI  * Allocate a new namespace structure and populate it with contents
1460741a2951SJANAK DESAI  * copied from the namespace of the passed in task structure.
1461741a2951SJANAK DESAI  */
1462e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
14636b3286edSKirill Korotaev 		struct fs_struct *fs)
14641da177e4SLinus Torvalds {
14656b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
14661da177e4SLinus Torvalds 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
14671da177e4SLinus Torvalds 	struct vfsmount *p, *q;
14681da177e4SLinus Torvalds 
14696b3286edSKirill Korotaev 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
14701da177e4SLinus Torvalds 	if (!new_ns)
1471467e9f4bSCedric Le Goater 		return ERR_PTR(-ENOMEM);
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	atomic_set(&new_ns->count, 1);
14741da177e4SLinus Torvalds 	INIT_LIST_HEAD(&new_ns->list);
14755addc5ddSAl Viro 	init_waitqueue_head(&new_ns->poll);
14765addc5ddSAl Viro 	new_ns->event = 0;
14771da177e4SLinus Torvalds 
1478390c6843SRam Pai 	down_write(&namespace_sem);
14791da177e4SLinus Torvalds 	/* First pass: copy the tree topology */
14806b3286edSKirill Korotaev 	new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root,
14819676f0c6SRam Pai 					CL_COPY_ALL | CL_EXPIRE);
14821da177e4SLinus Torvalds 	if (!new_ns->root) {
1483390c6843SRam Pai 		up_write(&namespace_sem);
14841da177e4SLinus Torvalds 		kfree(new_ns);
1485467e9f4bSCedric Le Goater 		return ERR_PTR(-ENOMEM);;
14861da177e4SLinus Torvalds 	}
14871da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
14881da177e4SLinus Torvalds 	list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
14891da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	/*
14921da177e4SLinus Torvalds 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
14931da177e4SLinus Torvalds 	 * as belonging to new namespace.  We have already acquired a private
14941da177e4SLinus Torvalds 	 * fs_struct, so tsk->fs->lock is not needed.
14951da177e4SLinus Torvalds 	 */
14966b3286edSKirill Korotaev 	p = mnt_ns->root;
14971da177e4SLinus Torvalds 	q = new_ns->root;
14981da177e4SLinus Torvalds 	while (p) {
14996b3286edSKirill Korotaev 		q->mnt_ns = new_ns;
15001da177e4SLinus Torvalds 		if (fs) {
15016ac08c39SJan Blunck 			if (p == fs->root.mnt) {
15021da177e4SLinus Torvalds 				rootmnt = p;
15036ac08c39SJan Blunck 				fs->root.mnt = mntget(q);
15041da177e4SLinus Torvalds 			}
15056ac08c39SJan Blunck 			if (p == fs->pwd.mnt) {
15061da177e4SLinus Torvalds 				pwdmnt = p;
15076ac08c39SJan Blunck 				fs->pwd.mnt = mntget(q);
15081da177e4SLinus Torvalds 			}
15096ac08c39SJan Blunck 			if (p == fs->altroot.mnt) {
15101da177e4SLinus Torvalds 				altrootmnt = p;
15116ac08c39SJan Blunck 				fs->altroot.mnt = mntget(q);
15121da177e4SLinus Torvalds 			}
15131da177e4SLinus Torvalds 		}
15146b3286edSKirill Korotaev 		p = next_mnt(p, mnt_ns->root);
15151da177e4SLinus Torvalds 		q = next_mnt(q, new_ns->root);
15161da177e4SLinus Torvalds 	}
1517390c6843SRam Pai 	up_write(&namespace_sem);
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds 	if (rootmnt)
15201da177e4SLinus Torvalds 		mntput(rootmnt);
15211da177e4SLinus Torvalds 	if (pwdmnt)
15221da177e4SLinus Torvalds 		mntput(pwdmnt);
15231da177e4SLinus Torvalds 	if (altrootmnt)
15241da177e4SLinus Torvalds 		mntput(altrootmnt);
15251da177e4SLinus Torvalds 
1526741a2951SJANAK DESAI 	return new_ns;
1527741a2951SJANAK DESAI }
1528741a2951SJANAK DESAI 
1529213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
1530e3222c4eSBadari Pulavarty 		struct fs_struct *new_fs)
1531741a2951SJANAK DESAI {
15326b3286edSKirill Korotaev 	struct mnt_namespace *new_ns;
1533741a2951SJANAK DESAI 
1534e3222c4eSBadari Pulavarty 	BUG_ON(!ns);
15356b3286edSKirill Korotaev 	get_mnt_ns(ns);
1536741a2951SJANAK DESAI 
1537741a2951SJANAK DESAI 	if (!(flags & CLONE_NEWNS))
1538e3222c4eSBadari Pulavarty 		return ns;
1539741a2951SJANAK DESAI 
1540e3222c4eSBadari Pulavarty 	new_ns = dup_mnt_ns(ns, new_fs);
1541741a2951SJANAK DESAI 
15426b3286edSKirill Korotaev 	put_mnt_ns(ns);
1543e3222c4eSBadari Pulavarty 	return new_ns;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
15471da177e4SLinus Torvalds 			  char __user * type, unsigned long flags,
15481da177e4SLinus Torvalds 			  void __user * data)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	int retval;
15511da177e4SLinus Torvalds 	unsigned long data_page;
15521da177e4SLinus Torvalds 	unsigned long type_page;
15531da177e4SLinus Torvalds 	unsigned long dev_page;
15541da177e4SLinus Torvalds 	char *dir_page;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	retval = copy_mount_options(type, &type_page);
15571da177e4SLinus Torvalds 	if (retval < 0)
15581da177e4SLinus Torvalds 		return retval;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	dir_page = getname(dir_name);
15611da177e4SLinus Torvalds 	retval = PTR_ERR(dir_page);
15621da177e4SLinus Torvalds 	if (IS_ERR(dir_page))
15631da177e4SLinus Torvalds 		goto out1;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	retval = copy_mount_options(dev_name, &dev_page);
15661da177e4SLinus Torvalds 	if (retval < 0)
15671da177e4SLinus Torvalds 		goto out2;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	retval = copy_mount_options(data, &data_page);
15701da177e4SLinus Torvalds 	if (retval < 0)
15711da177e4SLinus Torvalds 		goto out3;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	lock_kernel();
15741da177e4SLinus Torvalds 	retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
15751da177e4SLinus Torvalds 			  flags, (void *)data_page);
15761da177e4SLinus Torvalds 	unlock_kernel();
15771da177e4SLinus Torvalds 	free_page(data_page);
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds out3:
15801da177e4SLinus Torvalds 	free_page(dev_page);
15811da177e4SLinus Torvalds out2:
15821da177e4SLinus Torvalds 	putname(dir_page);
15831da177e4SLinus Torvalds out1:
15841da177e4SLinus Torvalds 	free_page(type_page);
15851da177e4SLinus Torvalds 	return retval;
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds /*
15891da177e4SLinus Torvalds  * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
15901da177e4SLinus Torvalds  * It can block. Requires the big lock held.
15911da177e4SLinus Torvalds  */
1592ac748a09SJan Blunck void set_fs_root(struct fs_struct *fs, struct path *path)
15931da177e4SLinus Torvalds {
15946ac08c39SJan Blunck 	struct path old_root;
15956ac08c39SJan Blunck 
15961da177e4SLinus Torvalds 	write_lock(&fs->lock);
15971da177e4SLinus Torvalds 	old_root = fs->root;
1598ac748a09SJan Blunck 	fs->root = *path;
1599ac748a09SJan Blunck 	path_get(path);
16001da177e4SLinus Torvalds 	write_unlock(&fs->lock);
16016ac08c39SJan Blunck 	if (old_root.dentry)
16026ac08c39SJan Blunck 		path_put(&old_root);
16031da177e4SLinus Torvalds }
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds /*
16061da177e4SLinus Torvalds  * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
16071da177e4SLinus Torvalds  * It can block. Requires the big lock held.
16081da177e4SLinus Torvalds  */
1609ac748a09SJan Blunck void set_fs_pwd(struct fs_struct *fs, struct path *path)
16101da177e4SLinus Torvalds {
16116ac08c39SJan Blunck 	struct path old_pwd;
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	write_lock(&fs->lock);
16141da177e4SLinus Torvalds 	old_pwd = fs->pwd;
1615ac748a09SJan Blunck 	fs->pwd = *path;
1616ac748a09SJan Blunck 	path_get(path);
16171da177e4SLinus Torvalds 	write_unlock(&fs->lock);
16181da177e4SLinus Torvalds 
16196ac08c39SJan Blunck 	if (old_pwd.dentry)
16206ac08c39SJan Blunck 		path_put(&old_pwd);
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds 
16231a390689SAl Viro static void chroot_fs_refs(struct path *old_root, struct path *new_root)
16241da177e4SLinus Torvalds {
16251da177e4SLinus Torvalds 	struct task_struct *g, *p;
16261da177e4SLinus Torvalds 	struct fs_struct *fs;
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
16291da177e4SLinus Torvalds 	do_each_thread(g, p) {
16301da177e4SLinus Torvalds 		task_lock(p);
16311da177e4SLinus Torvalds 		fs = p->fs;
16321da177e4SLinus Torvalds 		if (fs) {
16331da177e4SLinus Torvalds 			atomic_inc(&fs->count);
16341da177e4SLinus Torvalds 			task_unlock(p);
16351a390689SAl Viro 			if (fs->root.dentry == old_root->dentry
16361a390689SAl Viro 			    && fs->root.mnt == old_root->mnt)
16371a390689SAl Viro 				set_fs_root(fs, new_root);
16381a390689SAl Viro 			if (fs->pwd.dentry == old_root->dentry
16391a390689SAl Viro 			    && fs->pwd.mnt == old_root->mnt)
16401a390689SAl Viro 				set_fs_pwd(fs, new_root);
16411da177e4SLinus Torvalds 			put_fs_struct(fs);
16421da177e4SLinus Torvalds 		} else
16431da177e4SLinus Torvalds 			task_unlock(p);
16441da177e4SLinus Torvalds 	} while_each_thread(g, p);
16451da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
16461da177e4SLinus Torvalds }
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds /*
16491da177e4SLinus Torvalds  * pivot_root Semantics:
16501da177e4SLinus Torvalds  * Moves the root file system of the current process to the directory put_old,
16511da177e4SLinus Torvalds  * makes new_root as the new root file system of the current process, and sets
16521da177e4SLinus Torvalds  * root/cwd of all processes which had them on the current root to new_root.
16531da177e4SLinus Torvalds  *
16541da177e4SLinus Torvalds  * Restrictions:
16551da177e4SLinus Torvalds  * The new_root and put_old must be directories, and  must not be on the
16561da177e4SLinus Torvalds  * same file  system as the current process root. The put_old  must  be
16571da177e4SLinus Torvalds  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
16581da177e4SLinus Torvalds  * pointed to by put_old must yield the same directory as new_root. No other
16591da177e4SLinus Torvalds  * file system may be mounted on put_old. After all, new_root is a mountpoint.
16601da177e4SLinus Torvalds  *
16614a0d11faSNeil Brown  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
16624a0d11faSNeil Brown  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
16634a0d11faSNeil Brown  * in this situation.
16644a0d11faSNeil Brown  *
16651da177e4SLinus Torvalds  * Notes:
16661da177e4SLinus Torvalds  *  - we don't move root/cwd if they are not at the root (reason: if something
16671da177e4SLinus Torvalds  *    cared enough to change them, it's probably wrong to force them elsewhere)
16681da177e4SLinus Torvalds  *  - it's okay to pick a root that isn't the root of a file system, e.g.
16691da177e4SLinus Torvalds  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
16701da177e4SLinus Torvalds  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
16711da177e4SLinus Torvalds  *    first.
16721da177e4SLinus Torvalds  */
1673b58fed8bSRam Pai asmlinkage long sys_pivot_root(const char __user * new_root,
1674b58fed8bSRam Pai 			       const char __user * put_old)
16751da177e4SLinus Torvalds {
16761da177e4SLinus Torvalds 	struct vfsmount *tmp;
16771a390689SAl Viro 	struct nameidata new_nd, old_nd, user_nd;
16781a390689SAl Viro 	struct path parent_path, root_parent;
16791da177e4SLinus Torvalds 	int error;
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
16821da177e4SLinus Torvalds 		return -EPERM;
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	lock_kernel();
16851da177e4SLinus Torvalds 
1686b58fed8bSRam Pai 	error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1687b58fed8bSRam Pai 			    &new_nd);
16881da177e4SLinus Torvalds 	if (error)
16891da177e4SLinus Torvalds 		goto out0;
16901da177e4SLinus Torvalds 	error = -EINVAL;
16914ac91378SJan Blunck 	if (!check_mnt(new_nd.path.mnt))
16921da177e4SLinus Torvalds 		goto out1;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
16951da177e4SLinus Torvalds 	if (error)
16961da177e4SLinus Torvalds 		goto out1;
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	error = security_sb_pivotroot(&old_nd, &new_nd);
16991da177e4SLinus Torvalds 	if (error) {
17001d957f9bSJan Blunck 		path_put(&old_nd.path);
17011da177e4SLinus Torvalds 		goto out1;
17021da177e4SLinus Torvalds 	}
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 	read_lock(&current->fs->lock);
17056ac08c39SJan Blunck 	user_nd.path = current->fs->root;
17066ac08c39SJan Blunck 	path_get(&current->fs->root);
17071da177e4SLinus Torvalds 	read_unlock(&current->fs->lock);
1708390c6843SRam Pai 	down_write(&namespace_sem);
17094ac91378SJan Blunck 	mutex_lock(&old_nd.path.dentry->d_inode->i_mutex);
17101da177e4SLinus Torvalds 	error = -EINVAL;
17114ac91378SJan Blunck 	if (IS_MNT_SHARED(old_nd.path.mnt) ||
17124ac91378SJan Blunck 		IS_MNT_SHARED(new_nd.path.mnt->mnt_parent) ||
17134ac91378SJan Blunck 		IS_MNT_SHARED(user_nd.path.mnt->mnt_parent))
171421444403SRam Pai 		goto out2;
17154ac91378SJan Blunck 	if (!check_mnt(user_nd.path.mnt))
17161da177e4SLinus Torvalds 		goto out2;
17171da177e4SLinus Torvalds 	error = -ENOENT;
17184ac91378SJan Blunck 	if (IS_DEADDIR(new_nd.path.dentry->d_inode))
17191da177e4SLinus Torvalds 		goto out2;
17204ac91378SJan Blunck 	if (d_unhashed(new_nd.path.dentry) && !IS_ROOT(new_nd.path.dentry))
17211da177e4SLinus Torvalds 		goto out2;
17224ac91378SJan Blunck 	if (d_unhashed(old_nd.path.dentry) && !IS_ROOT(old_nd.path.dentry))
17231da177e4SLinus Torvalds 		goto out2;
17241da177e4SLinus Torvalds 	error = -EBUSY;
17254ac91378SJan Blunck 	if (new_nd.path.mnt == user_nd.path.mnt ||
17264ac91378SJan Blunck 	    old_nd.path.mnt == user_nd.path.mnt)
17271da177e4SLinus Torvalds 		goto out2; /* loop, on the same file system  */
17281da177e4SLinus Torvalds 	error = -EINVAL;
17294ac91378SJan Blunck 	if (user_nd.path.mnt->mnt_root != user_nd.path.dentry)
17301da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
17314ac91378SJan Blunck 	if (user_nd.path.mnt->mnt_parent == user_nd.path.mnt)
17320bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
17334ac91378SJan Blunck 	if (new_nd.path.mnt->mnt_root != new_nd.path.dentry)
17341da177e4SLinus Torvalds 		goto out2; /* not a mountpoint */
17354ac91378SJan Blunck 	if (new_nd.path.mnt->mnt_parent == new_nd.path.mnt)
17360bb6fcc1SMiklos Szeredi 		goto out2; /* not attached */
17374ac91378SJan Blunck 	/* make sure we can reach put_old from new_root */
17384ac91378SJan Blunck 	tmp = old_nd.path.mnt;
17391da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
17404ac91378SJan Blunck 	if (tmp != new_nd.path.mnt) {
17411da177e4SLinus Torvalds 		for (;;) {
17421da177e4SLinus Torvalds 			if (tmp->mnt_parent == tmp)
17431da177e4SLinus Torvalds 				goto out3; /* already mounted on put_old */
17444ac91378SJan Blunck 			if (tmp->mnt_parent == new_nd.path.mnt)
17451da177e4SLinus Torvalds 				break;
17461da177e4SLinus Torvalds 			tmp = tmp->mnt_parent;
17471da177e4SLinus Torvalds 		}
17484ac91378SJan Blunck 		if (!is_subdir(tmp->mnt_mountpoint, new_nd.path.dentry))
17491da177e4SLinus Torvalds 			goto out3;
17504ac91378SJan Blunck 	} else if (!is_subdir(old_nd.path.dentry, new_nd.path.dentry))
17511da177e4SLinus Torvalds 		goto out3;
17521a390689SAl Viro 	detach_mnt(new_nd.path.mnt, &parent_path);
17534ac91378SJan Blunck 	detach_mnt(user_nd.path.mnt, &root_parent);
17544ac91378SJan Blunck 	/* mount old root on put_old */
17551a390689SAl Viro 	attach_mnt(user_nd.path.mnt, &old_nd.path);
17564ac91378SJan Blunck 	/* mount new_root on / */
17574ac91378SJan Blunck 	attach_mnt(new_nd.path.mnt, &root_parent);
17586b3286edSKirill Korotaev 	touch_mnt_namespace(current->nsproxy->mnt_ns);
17591da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
17601a390689SAl Viro 	chroot_fs_refs(&user_nd.path, &new_nd.path);
17611da177e4SLinus Torvalds 	security_sb_post_pivotroot(&user_nd, &new_nd);
17621da177e4SLinus Torvalds 	error = 0;
17631a390689SAl Viro 	path_put(&root_parent);
17641a390689SAl Viro 	path_put(&parent_path);
17651da177e4SLinus Torvalds out2:
17664ac91378SJan Blunck 	mutex_unlock(&old_nd.path.dentry->d_inode->i_mutex);
1767390c6843SRam Pai 	up_write(&namespace_sem);
17681d957f9bSJan Blunck 	path_put(&user_nd.path);
17691d957f9bSJan Blunck 	path_put(&old_nd.path);
17701da177e4SLinus Torvalds out1:
17711d957f9bSJan Blunck 	path_put(&new_nd.path);
17721da177e4SLinus Torvalds out0:
17731da177e4SLinus Torvalds 	unlock_kernel();
17741da177e4SLinus Torvalds 	return error;
17751da177e4SLinus Torvalds out3:
17761da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
17771da177e4SLinus Torvalds 	goto out2;
17781da177e4SLinus Torvalds }
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds static void __init init_mount_tree(void)
17811da177e4SLinus Torvalds {
17821da177e4SLinus Torvalds 	struct vfsmount *mnt;
17836b3286edSKirill Korotaev 	struct mnt_namespace *ns;
1784ac748a09SJan Blunck 	struct path root;
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds 	mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
17871da177e4SLinus Torvalds 	if (IS_ERR(mnt))
17881da177e4SLinus Torvalds 		panic("Can't create rootfs");
17896b3286edSKirill Korotaev 	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
17906b3286edSKirill Korotaev 	if (!ns)
17911da177e4SLinus Torvalds 		panic("Can't allocate initial namespace");
17926b3286edSKirill Korotaev 	atomic_set(&ns->count, 1);
17936b3286edSKirill Korotaev 	INIT_LIST_HEAD(&ns->list);
17946b3286edSKirill Korotaev 	init_waitqueue_head(&ns->poll);
17956b3286edSKirill Korotaev 	ns->event = 0;
17966b3286edSKirill Korotaev 	list_add(&mnt->mnt_list, &ns->list);
17976b3286edSKirill Korotaev 	ns->root = mnt;
17986b3286edSKirill Korotaev 	mnt->mnt_ns = ns;
17991da177e4SLinus Torvalds 
18006b3286edSKirill Korotaev 	init_task.nsproxy->mnt_ns = ns;
18016b3286edSKirill Korotaev 	get_mnt_ns(ns);
18021da177e4SLinus Torvalds 
1803ac748a09SJan Blunck 	root.mnt = ns->root;
1804ac748a09SJan Blunck 	root.dentry = ns->root->mnt_root;
1805ac748a09SJan Blunck 
1806ac748a09SJan Blunck 	set_fs_pwd(current->fs, &root);
1807ac748a09SJan Blunck 	set_fs_root(current->fs, &root);
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds 
181074bf17cfSDenis Cheng void __init mnt_init(void)
18111da177e4SLinus Torvalds {
181213f14b4dSEric Dumazet 	unsigned u;
181315a67dd8SRandy Dunlap 	int err;
18141da177e4SLinus Torvalds 
1815390c6843SRam Pai 	init_rwsem(&namespace_sem);
1816390c6843SRam Pai 
18171da177e4SLinus Torvalds 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
181820c2df83SPaul Mundt 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
18191da177e4SLinus Torvalds 
1820b58fed8bSRam Pai 	mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	if (!mount_hashtable)
18231da177e4SLinus Torvalds 		panic("Failed to allocate mount hash table\n");
18241da177e4SLinus Torvalds 
182513f14b4dSEric Dumazet 	printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
18261da177e4SLinus Torvalds 
182713f14b4dSEric Dumazet 	for (u = 0; u < HASH_SIZE; u++)
182813f14b4dSEric Dumazet 		INIT_LIST_HEAD(&mount_hashtable[u]);
18291da177e4SLinus Torvalds 
183015a67dd8SRandy Dunlap 	err = sysfs_init();
183115a67dd8SRandy Dunlap 	if (err)
183215a67dd8SRandy Dunlap 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
183315a67dd8SRandy Dunlap 			__FUNCTION__, err);
183400d26666SGreg Kroah-Hartman 	fs_kobj = kobject_create_and_add("fs", NULL);
183500d26666SGreg Kroah-Hartman 	if (!fs_kobj)
183600d26666SGreg Kroah-Hartman 		printk(KERN_WARNING "%s: kobj create error\n", __FUNCTION__);
18371da177e4SLinus Torvalds 	init_rootfs();
18381da177e4SLinus Torvalds 	init_mount_tree();
18391da177e4SLinus Torvalds }
18401da177e4SLinus Torvalds 
18416b3286edSKirill Korotaev void __put_mnt_ns(struct mnt_namespace *ns)
18421da177e4SLinus Torvalds {
18436b3286edSKirill Korotaev 	struct vfsmount *root = ns->root;
184470fbcdf4SRam Pai 	LIST_HEAD(umount_list);
18456b3286edSKirill Korotaev 	ns->root = NULL;
18461ce88cf4SMiklos Szeredi 	spin_unlock(&vfsmount_lock);
1847390c6843SRam Pai 	down_write(&namespace_sem);
18481da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
1849a05964f3SRam Pai 	umount_tree(root, 0, &umount_list);
18501da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
1851390c6843SRam Pai 	up_write(&namespace_sem);
185270fbcdf4SRam Pai 	release_mounts(&umount_list);
18536b3286edSKirill Korotaev 	kfree(ns);
18541da177e4SLinus Torvalds }
1855