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 1581da177e4SLinus Torvalds static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd) 1591da177e4SLinus Torvalds { 1604ac91378SJan Blunck old_nd->path.dentry = mnt->mnt_mountpoint; 1614ac91378SJan Blunck old_nd->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); 1664ac91378SJan Blunck old_nd->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 1771da177e4SLinus Torvalds static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd) 1781da177e4SLinus Torvalds { 1794ac91378SJan Blunck mnt_set_mountpoint(nd->path.mnt, nd->path.dentry, mnt); 180b90fa9aeSRam Pai list_add_tail(&mnt->mnt_hash, mount_hashtable + 1814ac91378SJan Blunck hash(nd->path.mnt, nd->path.dentry)); 1824ac91378SJan Blunck list_add_tail(&mnt->mnt_child, &nd->path.mnt->mnt_mounts); 183b90fa9aeSRam Pai } 184b90fa9aeSRam Pai 185b90fa9aeSRam Pai /* 186b90fa9aeSRam Pai * the caller must hold vfsmount_lock 187b90fa9aeSRam Pai */ 188b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt) 189b90fa9aeSRam Pai { 190b90fa9aeSRam Pai struct vfsmount *parent = mnt->mnt_parent; 191b90fa9aeSRam Pai struct vfsmount *m; 192b90fa9aeSRam Pai LIST_HEAD(head); 1936b3286edSKirill Korotaev struct mnt_namespace *n = parent->mnt_ns; 194b90fa9aeSRam Pai 195b90fa9aeSRam Pai BUG_ON(parent == mnt); 196b90fa9aeSRam Pai 197b90fa9aeSRam Pai list_add_tail(&head, &mnt->mnt_list); 198b90fa9aeSRam Pai list_for_each_entry(m, &head, mnt_list) 1996b3286edSKirill Korotaev m->mnt_ns = n; 200b90fa9aeSRam Pai list_splice(&head, n->list.prev); 201b90fa9aeSRam Pai 202b90fa9aeSRam Pai list_add_tail(&mnt->mnt_hash, mount_hashtable + 203b90fa9aeSRam Pai hash(parent, mnt->mnt_mountpoint)); 204b90fa9aeSRam Pai list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 2056b3286edSKirill Korotaev touch_mnt_namespace(n); 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds struct list_head *next = p->mnt_mounts.next; 2111da177e4SLinus Torvalds if (next == &p->mnt_mounts) { 2121da177e4SLinus Torvalds while (1) { 2131da177e4SLinus Torvalds if (p == root) 2141da177e4SLinus Torvalds return NULL; 2151da177e4SLinus Torvalds next = p->mnt_child.next; 2161da177e4SLinus Torvalds if (next != &p->mnt_parent->mnt_mounts) 2171da177e4SLinus Torvalds break; 2181da177e4SLinus Torvalds p = p->mnt_parent; 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds return list_entry(next, struct vfsmount, mnt_child); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2249676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p) 2259676f0c6SRam Pai { 2269676f0c6SRam Pai struct list_head *prev = p->mnt_mounts.prev; 2279676f0c6SRam Pai while (prev != &p->mnt_mounts) { 2289676f0c6SRam Pai p = list_entry(prev, struct vfsmount, mnt_child); 2299676f0c6SRam Pai prev = p->mnt_mounts.prev; 2309676f0c6SRam Pai } 2319676f0c6SRam Pai return p; 2329676f0c6SRam Pai } 2339676f0c6SRam Pai 23436341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root, 23536341f64SRam Pai int flag) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds struct super_block *sb = old->mnt_sb; 2381da177e4SLinus Torvalds struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds if (mnt) { 2411da177e4SLinus Torvalds mnt->mnt_flags = old->mnt_flags; 2421da177e4SLinus Torvalds atomic_inc(&sb->s_active); 2431da177e4SLinus Torvalds mnt->mnt_sb = sb; 2441da177e4SLinus Torvalds mnt->mnt_root = dget(root); 2451da177e4SLinus Torvalds mnt->mnt_mountpoint = mnt->mnt_root; 2461da177e4SLinus Torvalds mnt->mnt_parent = mnt; 247b90fa9aeSRam Pai 2485afe0022SRam Pai if (flag & CL_SLAVE) { 2495afe0022SRam Pai list_add(&mnt->mnt_slave, &old->mnt_slave_list); 2505afe0022SRam Pai mnt->mnt_master = old; 2515afe0022SRam Pai CLEAR_MNT_SHARED(mnt); 2528aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 253b90fa9aeSRam Pai if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old)) 254b90fa9aeSRam Pai list_add(&mnt->mnt_share, &old->mnt_share); 2555afe0022SRam Pai if (IS_MNT_SLAVE(old)) 2565afe0022SRam Pai list_add(&mnt->mnt_slave, &old->mnt_slave); 2575afe0022SRam Pai mnt->mnt_master = old->mnt_master; 2585afe0022SRam Pai } 259b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 260b90fa9aeSRam Pai set_mnt_shared(mnt); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 2631da177e4SLinus Torvalds * as the original if that was on one */ 26436341f64SRam Pai if (flag & CL_EXPIRE) { 2651da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 26655e700b9SMiklos Szeredi if (!list_empty(&old->mnt_expire)) 26755e700b9SMiklos Szeredi list_add(&mnt->mnt_expire, &old->mnt_expire); 2681da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 2691da177e4SLinus Torvalds } 27036341f64SRam Pai } 2711da177e4SLinus Torvalds return mnt; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2747b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt) 2751da177e4SLinus Torvalds { 2761da177e4SLinus Torvalds struct super_block *sb = mnt->mnt_sb; 2771da177e4SLinus Torvalds dput(mnt->mnt_root); 2781da177e4SLinus Torvalds free_vfsmnt(mnt); 2791da177e4SLinus Torvalds deactivate_super(sb); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2827b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt) 2837b7b1aceSAl Viro { 2847b7b1aceSAl Viro repeat: 2857b7b1aceSAl Viro if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) { 2867b7b1aceSAl Viro if (likely(!mnt->mnt_pinned)) { 2877b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 2887b7b1aceSAl Viro __mntput(mnt); 2897b7b1aceSAl Viro return; 2907b7b1aceSAl Viro } 2917b7b1aceSAl Viro atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count); 2927b7b1aceSAl Viro mnt->mnt_pinned = 0; 2937b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 2947b7b1aceSAl Viro acct_auto_close_mnt(mnt); 2957b7b1aceSAl Viro security_sb_umount_close(mnt); 2967b7b1aceSAl Viro goto repeat; 2977b7b1aceSAl Viro } 2987b7b1aceSAl Viro } 2997b7b1aceSAl Viro 3007b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire); 3017b7b1aceSAl Viro 3027b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 3037b7b1aceSAl Viro { 3047b7b1aceSAl Viro spin_lock(&vfsmount_lock); 3057b7b1aceSAl Viro mnt->mnt_pinned++; 3067b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 3077b7b1aceSAl Viro } 3087b7b1aceSAl Viro 3097b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 3107b7b1aceSAl Viro 3117b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt) 3127b7b1aceSAl Viro { 3137b7b1aceSAl Viro spin_lock(&vfsmount_lock); 3147b7b1aceSAl Viro if (mnt->mnt_pinned) { 3157b7b1aceSAl Viro atomic_inc(&mnt->mnt_count); 3167b7b1aceSAl Viro mnt->mnt_pinned--; 3177b7b1aceSAl Viro } 3187b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 3197b7b1aceSAl Viro } 3207b7b1aceSAl Viro 3217b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 3221da177e4SLinus Torvalds 323b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 324b3b304a2SMiklos Szeredi { 325b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 326b3b304a2SMiklos Szeredi } 327b3b304a2SMiklos Szeredi 328b3b304a2SMiklos Szeredi /* 329b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 330b3b304a2SMiklos Szeredi * implement more complex mount option showing. 331b3b304a2SMiklos Szeredi * 332b3b304a2SMiklos Szeredi * See also save_mount_options(). 333b3b304a2SMiklos Szeredi */ 334b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt) 335b3b304a2SMiklos Szeredi { 336b3b304a2SMiklos Szeredi const char *options = mnt->mnt_sb->s_options; 337b3b304a2SMiklos Szeredi 338b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 339b3b304a2SMiklos Szeredi seq_putc(m, ','); 340b3b304a2SMiklos Szeredi mangle(m, options); 341b3b304a2SMiklos Szeredi } 342b3b304a2SMiklos Szeredi 343b3b304a2SMiklos Szeredi return 0; 344b3b304a2SMiklos Szeredi } 345b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 346b3b304a2SMiklos Szeredi 347b3b304a2SMiklos Szeredi /* 348b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 349b3b304a2SMiklos Szeredi * called from the fill_super() callback. 350b3b304a2SMiklos Szeredi * 351b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 352b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 353b3b304a2SMiklos Szeredi * remount fails. 354b3b304a2SMiklos Szeredi * 355b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 356b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 357b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 358b3b304a2SMiklos Szeredi * any more. 359b3b304a2SMiklos Szeredi */ 360b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 361b3b304a2SMiklos Szeredi { 362b3b304a2SMiklos Szeredi kfree(sb->s_options); 363b3b304a2SMiklos Szeredi sb->s_options = kstrdup(options, GFP_KERNEL); 364b3b304a2SMiklos Szeredi } 365b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 366b3b304a2SMiklos Szeredi 3671da177e4SLinus Torvalds /* iterator */ 3681da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 3691da177e4SLinus Torvalds { 3706b3286edSKirill Korotaev struct mnt_namespace *n = m->private; 3711da177e4SLinus Torvalds 372390c6843SRam Pai down_read(&namespace_sem); 373b0765fb8SPavel Emelianov return seq_list_start(&n->list, *pos); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 3771da177e4SLinus Torvalds { 3786b3286edSKirill Korotaev struct mnt_namespace *n = m->private; 379b0765fb8SPavel Emelianov 380b0765fb8SPavel Emelianov return seq_list_next(v, &n->list, pos); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 3841da177e4SLinus Torvalds { 385390c6843SRam Pai up_read(&namespace_sem); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds static int show_vfsmnt(struct seq_file *m, void *v) 3891da177e4SLinus Torvalds { 390b0765fb8SPavel Emelianov struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); 3911da177e4SLinus Torvalds int err = 0; 3921da177e4SLinus Torvalds static struct proc_fs_info { 3931da177e4SLinus Torvalds int flag; 3941da177e4SLinus Torvalds char *str; 3951da177e4SLinus Torvalds } fs_info[] = { 3961da177e4SLinus Torvalds { MS_SYNCHRONOUS, ",sync" }, 3971da177e4SLinus Torvalds { MS_DIRSYNC, ",dirsync" }, 3981da177e4SLinus Torvalds { MS_MANDLOCK, ",mand" }, 3991da177e4SLinus Torvalds { 0, NULL } 4001da177e4SLinus Torvalds }; 4011da177e4SLinus Torvalds static struct proc_fs_info mnt_info[] = { 4021da177e4SLinus Torvalds { MNT_NOSUID, ",nosuid" }, 4031da177e4SLinus Torvalds { MNT_NODEV, ",nodev" }, 4041da177e4SLinus Torvalds { MNT_NOEXEC, ",noexec" }, 405fc33a7bbSChristoph Hellwig { MNT_NOATIME, ",noatime" }, 406fc33a7bbSChristoph Hellwig { MNT_NODIRATIME, ",nodiratime" }, 40747ae32d6SValerie Henson { MNT_RELATIME, ",relatime" }, 4081da177e4SLinus Torvalds { 0, NULL } 4091da177e4SLinus Torvalds }; 4101da177e4SLinus Torvalds struct proc_fs_info *fs_infop; 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none"); 4131da177e4SLinus Torvalds seq_putc(m, ' '); 4141da177e4SLinus Torvalds seq_path(m, mnt, mnt->mnt_root, " \t\n\\"); 4151da177e4SLinus Torvalds seq_putc(m, ' '); 4161da177e4SLinus Torvalds mangle(m, mnt->mnt_sb->s_type->name); 41779c0b2dfSMiklos Szeredi if (mnt->mnt_sb->s_subtype && mnt->mnt_sb->s_subtype[0]) { 41879c0b2dfSMiklos Szeredi seq_putc(m, '.'); 41979c0b2dfSMiklos Szeredi mangle(m, mnt->mnt_sb->s_subtype); 42079c0b2dfSMiklos Szeredi } 4211da177e4SLinus Torvalds seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw"); 4221da177e4SLinus Torvalds for (fs_infop = fs_info; fs_infop->flag; fs_infop++) { 4231da177e4SLinus Torvalds if (mnt->mnt_sb->s_flags & fs_infop->flag) 4241da177e4SLinus Torvalds seq_puts(m, fs_infop->str); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) { 4271da177e4SLinus Torvalds if (mnt->mnt_flags & fs_infop->flag) 4281da177e4SLinus Torvalds seq_puts(m, fs_infop->str); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds if (mnt->mnt_sb->s_op->show_options) 4311da177e4SLinus Torvalds err = mnt->mnt_sb->s_op->show_options(m, mnt); 4321da177e4SLinus Torvalds seq_puts(m, " 0 0\n"); 4331da177e4SLinus Torvalds return err; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds struct seq_operations mounts_op = { 4371da177e4SLinus Torvalds .start = m_start, 4381da177e4SLinus Torvalds .next = m_next, 4391da177e4SLinus Torvalds .stop = m_stop, 4401da177e4SLinus Torvalds .show = show_vfsmnt 4411da177e4SLinus Torvalds }; 4421da177e4SLinus Torvalds 443b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v) 444b4629fe2SChuck Lever { 445b0765fb8SPavel Emelianov struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); 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 "); 457b4629fe2SChuck Lever seq_path(m, mnt, mnt->mnt_root, " \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; 5491da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 55070fbcdf4SRam Pai dput(dentry); 55170fbcdf4SRam Pai mntput(m); 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds mntput(mnt); 55470fbcdf4SRam Pai } 55570fbcdf4SRam Pai } 55670fbcdf4SRam Pai 557a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill) 55870fbcdf4SRam Pai { 55970fbcdf4SRam Pai struct vfsmount *p; 56070fbcdf4SRam Pai 5611bfba4e8SAkinobu Mita for (p = mnt; p; p = next_mnt(p, mnt)) 5621bfba4e8SAkinobu Mita list_move(&p->mnt_hash, kill); 56370fbcdf4SRam Pai 564a05964f3SRam Pai if (propagate) 565a05964f3SRam Pai propagate_umount(kill); 566a05964f3SRam Pai 56770fbcdf4SRam Pai list_for_each_entry(p, kill, mnt_hash) { 56870fbcdf4SRam Pai list_del_init(&p->mnt_expire); 56970fbcdf4SRam Pai list_del_init(&p->mnt_list); 5706b3286edSKirill Korotaev __touch_mnt_namespace(p->mnt_ns); 5716b3286edSKirill Korotaev p->mnt_ns = NULL; 57270fbcdf4SRam Pai list_del_init(&p->mnt_child); 57370fbcdf4SRam Pai if (p->mnt_parent != p) 574f30ac319SAl Viro p->mnt_mountpoint->d_mounted--; 575a05964f3SRam Pai change_mnt_propagation(p, MS_PRIVATE); 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags) 5801da177e4SLinus Torvalds { 5811da177e4SLinus Torvalds struct super_block *sb = mnt->mnt_sb; 5821da177e4SLinus Torvalds int retval; 58370fbcdf4SRam Pai LIST_HEAD(umount_list); 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds retval = security_sb_umount(mnt, flags); 5861da177e4SLinus Torvalds if (retval) 5871da177e4SLinus Torvalds return retval; 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* 5901da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 5911da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 5921da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 5931da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 5941da177e4SLinus Torvalds */ 5951da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 5966ac08c39SJan Blunck if (mnt == current->fs->root.mnt || 5971da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 5981da177e4SLinus Torvalds return -EINVAL; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds if (atomic_read(&mnt->mnt_count) != 2) 6011da177e4SLinus Torvalds return -EBUSY; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds if (!xchg(&mnt->mnt_expiry_mark, 1)) 6041da177e4SLinus Torvalds return -EAGAIN; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* 6081da177e4SLinus Torvalds * If we may have to abort operations to get out of this 6091da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 6101da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 6111da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 6121da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 6131da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 6141da177e4SLinus Torvalds * about for the moment. 6151da177e4SLinus Torvalds */ 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds lock_kernel(); 6188b512d9aSTrond Myklebust if (sb->s_op->umount_begin) 6198b512d9aSTrond Myklebust sb->s_op->umount_begin(mnt, flags); 6201da177e4SLinus Torvalds unlock_kernel(); 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds /* 6231da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 6241da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 6251da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 6261da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 6271da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 6281da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 6291da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 6301da177e4SLinus Torvalds */ 6316ac08c39SJan Blunck if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 6321da177e4SLinus Torvalds /* 6331da177e4SLinus Torvalds * Special case for "unmounting" root ... 6341da177e4SLinus Torvalds * we just try to remount it readonly. 6351da177e4SLinus Torvalds */ 6361da177e4SLinus Torvalds down_write(&sb->s_umount); 6371da177e4SLinus Torvalds if (!(sb->s_flags & MS_RDONLY)) { 6381da177e4SLinus Torvalds lock_kernel(); 6391da177e4SLinus Torvalds DQUOT_OFF(sb); 6401da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 6411da177e4SLinus Torvalds unlock_kernel(); 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds up_write(&sb->s_umount); 6441da177e4SLinus Torvalds return retval; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 647390c6843SRam Pai down_write(&namespace_sem); 6481da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 6495addc5ddSAl Viro event++; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds retval = -EBUSY; 652a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 6531da177e4SLinus Torvalds if (!list_empty(&mnt->mnt_list)) 654a05964f3SRam Pai umount_tree(mnt, 1, &umount_list); 6551da177e4SLinus Torvalds retval = 0; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 6581da177e4SLinus Torvalds if (retval) 6591da177e4SLinus Torvalds security_sb_umount_busy(mnt); 660390c6843SRam Pai up_write(&namespace_sem); 66170fbcdf4SRam Pai release_mounts(&umount_list); 6621da177e4SLinus Torvalds return retval; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* 6661da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 6671da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 6681da177e4SLinus Torvalds * 6691da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 6701da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 6711da177e4SLinus Torvalds */ 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds asmlinkage long sys_umount(char __user * name, int flags) 6741da177e4SLinus Torvalds { 6751da177e4SLinus Torvalds struct nameidata nd; 6761da177e4SLinus Torvalds int retval; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds retval = __user_walk(name, LOOKUP_FOLLOW, &nd); 6791da177e4SLinus Torvalds if (retval) 6801da177e4SLinus Torvalds goto out; 6811da177e4SLinus Torvalds retval = -EINVAL; 6824ac91378SJan Blunck if (nd.path.dentry != nd.path.mnt->mnt_root) 6831da177e4SLinus Torvalds goto dput_and_out; 6844ac91378SJan Blunck if (!check_mnt(nd.path.mnt)) 6851da177e4SLinus Torvalds goto dput_and_out; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds retval = -EPERM; 6881da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 6891da177e4SLinus Torvalds goto dput_and_out; 6901da177e4SLinus Torvalds 6914ac91378SJan Blunck retval = do_umount(nd.path.mnt, flags); 6921da177e4SLinus Torvalds dput_and_out: 693429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 6944ac91378SJan Blunck dput(nd.path.dentry); 6954ac91378SJan Blunck mntput_no_expire(nd.path.mnt); 6961da177e4SLinus Torvalds out: 6971da177e4SLinus Torvalds return retval; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds /* 7031da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 7041da177e4SLinus Torvalds */ 7051da177e4SLinus Torvalds asmlinkage long sys_oldumount(char __user * name) 7061da177e4SLinus Torvalds { 7071da177e4SLinus Torvalds return sys_umount(name, 0); 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds #endif 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds static int mount_is_safe(struct nameidata *nd) 7131da177e4SLinus Torvalds { 7141da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 7151da177e4SLinus Torvalds return 0; 7161da177e4SLinus Torvalds return -EPERM; 7171da177e4SLinus Torvalds #ifdef notyet 7184ac91378SJan Blunck if (S_ISLNK(nd->path.dentry->d_inode->i_mode)) 7191da177e4SLinus Torvalds return -EPERM; 7204ac91378SJan Blunck if (nd->path.dentry->d_inode->i_mode & S_ISVTX) { 7214ac91378SJan Blunck if (current->uid != nd->path.dentry->d_inode->i_uid) 7221da177e4SLinus Torvalds return -EPERM; 7231da177e4SLinus Torvalds } 724e4543eddSChristoph Hellwig if (vfs_permission(nd, MAY_WRITE)) 7251da177e4SLinus Torvalds return -EPERM; 7261da177e4SLinus Torvalds return 0; 7271da177e4SLinus Torvalds #endif 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 730b58fed8bSRam Pai static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds while (1) { 7331da177e4SLinus Torvalds if (d == dentry) 7341da177e4SLinus Torvalds return 1; 7351da177e4SLinus Torvalds if (d == NULL || d == d->d_parent) 7361da177e4SLinus Torvalds return 0; 7371da177e4SLinus Torvalds d = d->d_parent; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 741b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry, 74236341f64SRam Pai int flag) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds struct vfsmount *res, *p, *q, *r, *s; 7451da177e4SLinus Torvalds struct nameidata nd; 7461da177e4SLinus Torvalds 7479676f0c6SRam Pai if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 7489676f0c6SRam Pai return NULL; 7499676f0c6SRam Pai 75036341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 7511da177e4SLinus Torvalds if (!q) 7521da177e4SLinus Torvalds goto Enomem; 7531da177e4SLinus Torvalds q->mnt_mountpoint = mnt->mnt_mountpoint; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds p = mnt; 756fdadd65fSDomen Puncer list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 7571da177e4SLinus Torvalds if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry)) 7581da177e4SLinus Torvalds continue; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds for (s = r; s; s = next_mnt(s, r)) { 7619676f0c6SRam Pai if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 7629676f0c6SRam Pai s = skip_mnt_tree(s); 7639676f0c6SRam Pai continue; 7649676f0c6SRam Pai } 7651da177e4SLinus Torvalds while (p != s->mnt_parent) { 7661da177e4SLinus Torvalds p = p->mnt_parent; 7671da177e4SLinus Torvalds q = q->mnt_parent; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds p = s; 7704ac91378SJan Blunck nd.path.mnt = q; 7714ac91378SJan Blunck nd.path.dentry = p->mnt_mountpoint; 77236341f64SRam Pai q = clone_mnt(p, p->mnt_root, flag); 7731da177e4SLinus Torvalds if (!q) 7741da177e4SLinus Torvalds goto Enomem; 7751da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 7761da177e4SLinus Torvalds list_add_tail(&q->mnt_list, &res->mnt_list); 7771da177e4SLinus Torvalds attach_mnt(q, &nd); 7781da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds return res; 7821da177e4SLinus Torvalds Enomem: 7831da177e4SLinus Torvalds if (res) { 78470fbcdf4SRam Pai LIST_HEAD(umount_list); 7851da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 786a05964f3SRam Pai umount_tree(res, 0, &umount_list); 7871da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 78870fbcdf4SRam Pai release_mounts(&umount_list); 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds return NULL; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7938aec0809SAl Viro struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry) 7948aec0809SAl Viro { 7958aec0809SAl Viro struct vfsmount *tree; 7968aec0809SAl Viro down_read(&namespace_sem); 7978aec0809SAl Viro tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE); 7988aec0809SAl Viro up_read(&namespace_sem); 7998aec0809SAl Viro return tree; 8008aec0809SAl Viro } 8018aec0809SAl Viro 8028aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 8038aec0809SAl Viro { 8048aec0809SAl Viro LIST_HEAD(umount_list); 8058aec0809SAl Viro down_read(&namespace_sem); 8068aec0809SAl Viro spin_lock(&vfsmount_lock); 8078aec0809SAl Viro umount_tree(mnt, 0, &umount_list); 8088aec0809SAl Viro spin_unlock(&vfsmount_lock); 8098aec0809SAl Viro up_read(&namespace_sem); 8108aec0809SAl Viro release_mounts(&umount_list); 8118aec0809SAl Viro } 8128aec0809SAl Viro 813b90fa9aeSRam Pai /* 814b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 815b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 81621444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 81721444403SRam Pai * store the parent mount and mountpoint dentry. 81821444403SRam Pai * (done when source_mnt is moved) 819b90fa9aeSRam Pai * 820b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 821b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 8229676f0c6SRam Pai * --------------------------------------------------------------------------- 823b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 8249676f0c6SRam Pai * |************************************************************************** 8259676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 8269676f0c6SRam Pai * | dest | | | | | 8279676f0c6SRam Pai * | | | | | | | 8289676f0c6SRam Pai * | v | | | | | 8299676f0c6SRam Pai * |************************************************************************** 8309676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 8315afe0022SRam Pai * | | | | | | 8329676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 8339676f0c6SRam Pai * *************************************************************************** 834b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 835b90fa9aeSRam Pai * destination mount. 836b90fa9aeSRam Pai * 837b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 838b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 839b90fa9aeSRam Pai * the peer group of the source mount. 840b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 841b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 842b90fa9aeSRam Pai * mount. 8435afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 8445afe0022SRam Pai * of the destination mount and the cloned mount is made slave 8455afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 8465afe0022SRam Pai * is marked as 'shared and slave'. 8475afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 8485afe0022SRam Pai * source mount. 8495afe0022SRam Pai * 8509676f0c6SRam Pai * --------------------------------------------------------------------------- 85121444403SRam Pai * | MOVE MOUNT OPERATION | 8529676f0c6SRam Pai * |************************************************************************** 8539676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 8549676f0c6SRam Pai * | dest | | | | | 8559676f0c6SRam Pai * | | | | | | | 8569676f0c6SRam Pai * | v | | | | | 8579676f0c6SRam Pai * |************************************************************************** 8589676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 8595afe0022SRam Pai * | | | | | | 8609676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 8619676f0c6SRam Pai * *************************************************************************** 8625afe0022SRam Pai * 8635afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 8645afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 86521444403SRam Pai * (+*) the mount is moved to the destination. 8665afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 8675afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 8685afe0022SRam Pai * the mount is marked as 'shared and slave'. 8695afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 870b90fa9aeSRam Pai * 871b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 872b90fa9aeSRam Pai * applied to each mount in the tree. 873b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 874b90fa9aeSRam Pai * in allocations. 875b90fa9aeSRam Pai */ 876b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt, 87721444403SRam Pai struct nameidata *nd, struct nameidata *parent_nd) 878b90fa9aeSRam Pai { 879b90fa9aeSRam Pai LIST_HEAD(tree_list); 8804ac91378SJan Blunck struct vfsmount *dest_mnt = nd->path.mnt; 8814ac91378SJan Blunck struct dentry *dest_dentry = nd->path.dentry; 882b90fa9aeSRam Pai struct vfsmount *child, *p; 883b90fa9aeSRam Pai 884b90fa9aeSRam Pai if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list)) 885b90fa9aeSRam Pai return -EINVAL; 886b90fa9aeSRam Pai 887b90fa9aeSRam Pai if (IS_MNT_SHARED(dest_mnt)) { 888b90fa9aeSRam Pai for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 889b90fa9aeSRam Pai set_mnt_shared(p); 890b90fa9aeSRam Pai } 891b90fa9aeSRam Pai 892b90fa9aeSRam Pai spin_lock(&vfsmount_lock); 89321444403SRam Pai if (parent_nd) { 89421444403SRam Pai detach_mnt(source_mnt, parent_nd); 89521444403SRam Pai attach_mnt(source_mnt, nd); 8966b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 89721444403SRam Pai } else { 898b90fa9aeSRam Pai mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 899b90fa9aeSRam Pai commit_tree(source_mnt); 90021444403SRam Pai } 901b90fa9aeSRam Pai 902b90fa9aeSRam Pai list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 903b90fa9aeSRam Pai list_del_init(&child->mnt_hash); 904b90fa9aeSRam Pai commit_tree(child); 905b90fa9aeSRam Pai } 906b90fa9aeSRam Pai spin_unlock(&vfsmount_lock); 907b90fa9aeSRam Pai return 0; 908b90fa9aeSRam Pai } 909b90fa9aeSRam Pai 9101da177e4SLinus Torvalds static int graft_tree(struct vfsmount *mnt, struct nameidata *nd) 9111da177e4SLinus Torvalds { 9121da177e4SLinus Torvalds int err; 9131da177e4SLinus Torvalds if (mnt->mnt_sb->s_flags & MS_NOUSER) 9141da177e4SLinus Torvalds return -EINVAL; 9151da177e4SLinus Torvalds 9164ac91378SJan Blunck if (S_ISDIR(nd->path.dentry->d_inode->i_mode) != 9171da177e4SLinus Torvalds S_ISDIR(mnt->mnt_root->d_inode->i_mode)) 9181da177e4SLinus Torvalds return -ENOTDIR; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds err = -ENOENT; 9214ac91378SJan Blunck mutex_lock(&nd->path.dentry->d_inode->i_mutex); 9224ac91378SJan Blunck if (IS_DEADDIR(nd->path.dentry->d_inode)) 9231da177e4SLinus Torvalds goto out_unlock; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds err = security_sb_check_sb(mnt, nd); 9261da177e4SLinus Torvalds if (err) 9271da177e4SLinus Torvalds goto out_unlock; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds err = -ENOENT; 9304ac91378SJan Blunck if (IS_ROOT(nd->path.dentry) || !d_unhashed(nd->path.dentry)) 93121444403SRam Pai err = attach_recursive_mnt(mnt, nd, NULL); 9321da177e4SLinus Torvalds out_unlock: 9334ac91378SJan Blunck mutex_unlock(&nd->path.dentry->d_inode->i_mutex); 9341da177e4SLinus Torvalds if (!err) 9351da177e4SLinus Torvalds security_sb_post_addmount(mnt, nd); 9361da177e4SLinus Torvalds return err; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds /* 94007b20889SRam Pai * recursively change the type of the mountpoint. 9412dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 94207b20889SRam Pai */ 9432dafe1c4SEric Sandeen static noinline int do_change_type(struct nameidata *nd, int flag) 94407b20889SRam Pai { 9454ac91378SJan Blunck struct vfsmount *m, *mnt = nd->path.mnt; 94607b20889SRam Pai int recurse = flag & MS_REC; 94707b20889SRam Pai int type = flag & ~MS_REC; 94807b20889SRam Pai 949ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 950ee6f9582SMiklos Szeredi return -EPERM; 951ee6f9582SMiklos Szeredi 9524ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) 95307b20889SRam Pai return -EINVAL; 95407b20889SRam Pai 95507b20889SRam Pai down_write(&namespace_sem); 95607b20889SRam Pai spin_lock(&vfsmount_lock); 95707b20889SRam Pai for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 95807b20889SRam Pai change_mnt_propagation(m, type); 95907b20889SRam Pai spin_unlock(&vfsmount_lock); 96007b20889SRam Pai up_write(&namespace_sem); 96107b20889SRam Pai return 0; 96207b20889SRam Pai } 96307b20889SRam Pai 96407b20889SRam Pai /* 9651da177e4SLinus Torvalds * do loopback mount. 9662dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 9671da177e4SLinus Torvalds */ 9682dafe1c4SEric Sandeen static noinline int do_loopback(struct nameidata *nd, char *old_name, 9692dafe1c4SEric Sandeen int recurse) 9701da177e4SLinus Torvalds { 9711da177e4SLinus Torvalds struct nameidata old_nd; 9721da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 9731da177e4SLinus Torvalds int err = mount_is_safe(nd); 9741da177e4SLinus Torvalds if (err) 9751da177e4SLinus Torvalds return err; 9761da177e4SLinus Torvalds if (!old_name || !*old_name) 9771da177e4SLinus Torvalds return -EINVAL; 9781da177e4SLinus Torvalds err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd); 9791da177e4SLinus Torvalds if (err) 9801da177e4SLinus Torvalds return err; 9811da177e4SLinus Torvalds 982390c6843SRam Pai down_write(&namespace_sem); 9831da177e4SLinus Torvalds err = -EINVAL; 9844ac91378SJan Blunck if (IS_MNT_UNBINDABLE(old_nd.path.mnt)) 9859676f0c6SRam Pai goto out; 9869676f0c6SRam Pai 9874ac91378SJan Blunck if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt)) 988ccd48bc7SAl Viro goto out; 989ccd48bc7SAl Viro 9901da177e4SLinus Torvalds err = -ENOMEM; 9911da177e4SLinus Torvalds if (recurse) 9924ac91378SJan Blunck mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0); 9931da177e4SLinus Torvalds else 9944ac91378SJan Blunck mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0); 9951da177e4SLinus Torvalds 996ccd48bc7SAl Viro if (!mnt) 997ccd48bc7SAl Viro goto out; 998ccd48bc7SAl Viro 9991da177e4SLinus Torvalds err = graft_tree(mnt, nd); 10001da177e4SLinus Torvalds if (err) { 100170fbcdf4SRam Pai LIST_HEAD(umount_list); 10021da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 1003a05964f3SRam Pai umount_tree(mnt, 0, &umount_list); 10041da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 100570fbcdf4SRam Pai release_mounts(&umount_list); 10065b83d2c5SRam Pai } 10071da177e4SLinus Torvalds 1008ccd48bc7SAl Viro out: 1009390c6843SRam Pai up_write(&namespace_sem); 10101d957f9bSJan Blunck path_put(&old_nd.path); 10111da177e4SLinus Torvalds return err; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds /* 10151da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 10161da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 10171da177e4SLinus Torvalds * on it - tough luck. 10182dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 10191da177e4SLinus Torvalds */ 10202dafe1c4SEric Sandeen static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags, 10211da177e4SLinus Torvalds void *data) 10221da177e4SLinus Torvalds { 10231da177e4SLinus Torvalds int err; 10244ac91378SJan Blunck struct super_block *sb = nd->path.mnt->mnt_sb; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 10271da177e4SLinus Torvalds return -EPERM; 10281da177e4SLinus Torvalds 10294ac91378SJan Blunck if (!check_mnt(nd->path.mnt)) 10301da177e4SLinus Torvalds return -EINVAL; 10311da177e4SLinus Torvalds 10324ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) 10331da177e4SLinus Torvalds return -EINVAL; 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds down_write(&sb->s_umount); 10361da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 10371da177e4SLinus Torvalds if (!err) 10384ac91378SJan Blunck nd->path.mnt->mnt_flags = mnt_flags; 10391da177e4SLinus Torvalds up_write(&sb->s_umount); 10401da177e4SLinus Torvalds if (!err) 10414ac91378SJan Blunck security_sb_post_remount(nd->path.mnt, flags, data); 10421da177e4SLinus Torvalds return err; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10459676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt) 10469676f0c6SRam Pai { 10479676f0c6SRam Pai struct vfsmount *p; 10489676f0c6SRam Pai for (p = mnt; p; p = next_mnt(p, mnt)) { 10499676f0c6SRam Pai if (IS_MNT_UNBINDABLE(p)) 10509676f0c6SRam Pai return 1; 10519676f0c6SRam Pai } 10529676f0c6SRam Pai return 0; 10539676f0c6SRam Pai } 10549676f0c6SRam Pai 10552dafe1c4SEric Sandeen /* 10562dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 10572dafe1c4SEric Sandeen */ 10582dafe1c4SEric Sandeen static noinline int do_move_mount(struct nameidata *nd, char *old_name) 10591da177e4SLinus Torvalds { 10601da177e4SLinus Torvalds struct nameidata old_nd, parent_nd; 10611da177e4SLinus Torvalds struct vfsmount *p; 10621da177e4SLinus Torvalds int err = 0; 10631da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 10641da177e4SLinus Torvalds return -EPERM; 10651da177e4SLinus Torvalds if (!old_name || !*old_name) 10661da177e4SLinus Torvalds return -EINVAL; 10671da177e4SLinus Torvalds err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd); 10681da177e4SLinus Torvalds if (err) 10691da177e4SLinus Torvalds return err; 10701da177e4SLinus Torvalds 1071390c6843SRam Pai down_write(&namespace_sem); 10724ac91378SJan Blunck while (d_mountpoint(nd->path.dentry) && 10734ac91378SJan Blunck follow_down(&nd->path.mnt, &nd->path.dentry)) 10741da177e4SLinus Torvalds ; 10751da177e4SLinus Torvalds err = -EINVAL; 10764ac91378SJan Blunck if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt)) 10771da177e4SLinus Torvalds goto out; 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds err = -ENOENT; 10804ac91378SJan Blunck mutex_lock(&nd->path.dentry->d_inode->i_mutex); 10814ac91378SJan Blunck if (IS_DEADDIR(nd->path.dentry->d_inode)) 10821da177e4SLinus Torvalds goto out1; 10831da177e4SLinus Torvalds 10844ac91378SJan Blunck if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry)) 108521444403SRam Pai goto out1; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds err = -EINVAL; 10884ac91378SJan Blunck if (old_nd.path.dentry != old_nd.path.mnt->mnt_root) 108921444403SRam Pai goto out1; 10901da177e4SLinus Torvalds 10914ac91378SJan Blunck if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent) 109221444403SRam Pai goto out1; 10931da177e4SLinus Torvalds 10944ac91378SJan Blunck if (S_ISDIR(nd->path.dentry->d_inode->i_mode) != 10954ac91378SJan Blunck S_ISDIR(old_nd.path.dentry->d_inode->i_mode)) 109621444403SRam Pai goto out1; 109721444403SRam Pai /* 109821444403SRam Pai * Don't move a mount residing in a shared parent. 109921444403SRam Pai */ 11004ac91378SJan Blunck if (old_nd.path.mnt->mnt_parent && 11014ac91378SJan Blunck IS_MNT_SHARED(old_nd.path.mnt->mnt_parent)) 110221444403SRam Pai goto out1; 11039676f0c6SRam Pai /* 11049676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 11059676f0c6SRam Pai * mount which is shared. 11069676f0c6SRam Pai */ 11074ac91378SJan Blunck if (IS_MNT_SHARED(nd->path.mnt) && 11084ac91378SJan Blunck tree_contains_unbindable(old_nd.path.mnt)) 11099676f0c6SRam Pai goto out1; 11101da177e4SLinus Torvalds err = -ELOOP; 11114ac91378SJan Blunck for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent) 11124ac91378SJan Blunck if (p == old_nd.path.mnt) 111321444403SRam Pai goto out1; 11141da177e4SLinus Torvalds 11154ac91378SJan Blunck err = attach_recursive_mnt(old_nd.path.mnt, nd, &parent_nd); 11164ac91378SJan Blunck if (err) 111721444403SRam Pai goto out1; 11181da177e4SLinus Torvalds 111921444403SRam Pai spin_lock(&vfsmount_lock); 11201da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 11211da177e4SLinus Torvalds * automatically */ 11224ac91378SJan Blunck list_del_init(&old_nd.path.mnt->mnt_expire); 11231da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 11241da177e4SLinus Torvalds out1: 11254ac91378SJan Blunck mutex_unlock(&nd->path.dentry->d_inode->i_mutex); 11261da177e4SLinus Torvalds out: 1127390c6843SRam Pai up_write(&namespace_sem); 11281da177e4SLinus Torvalds if (!err) 11291d957f9bSJan Blunck path_put(&parent_nd.path); 11301d957f9bSJan Blunck path_put(&old_nd.path); 11311da177e4SLinus Torvalds return err; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds /* 11351da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 11361da177e4SLinus Torvalds * namespace's tree 11372dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 11381da177e4SLinus Torvalds */ 11392dafe1c4SEric Sandeen static noinline int do_new_mount(struct nameidata *nd, char *type, int flags, 11401da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 11411da177e4SLinus Torvalds { 11421da177e4SLinus Torvalds struct vfsmount *mnt; 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds if (!type || !memchr(type, 0, PAGE_SIZE)) 11451da177e4SLinus Torvalds return -EINVAL; 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /* we need capabilities... */ 11481da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11491da177e4SLinus Torvalds return -EPERM; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 11521da177e4SLinus Torvalds if (IS_ERR(mnt)) 11531da177e4SLinus Torvalds return PTR_ERR(mnt); 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds return do_add_mount(mnt, nd, mnt_flags, NULL); 11561da177e4SLinus Torvalds } 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds /* 11591da177e4SLinus Torvalds * add a mount into a namespace's mount tree 11601da177e4SLinus Torvalds * - provide the option of adding the new mount to an expiration list 11611da177e4SLinus Torvalds */ 11621da177e4SLinus Torvalds int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd, 11631da177e4SLinus Torvalds int mnt_flags, struct list_head *fslist) 11641da177e4SLinus Torvalds { 11651da177e4SLinus Torvalds int err; 11661da177e4SLinus Torvalds 1167390c6843SRam Pai down_write(&namespace_sem); 11681da177e4SLinus Torvalds /* Something was mounted here while we slept */ 11694ac91378SJan Blunck while (d_mountpoint(nd->path.dentry) && 11704ac91378SJan Blunck follow_down(&nd->path.mnt, &nd->path.dentry)) 11711da177e4SLinus Torvalds ; 11721da177e4SLinus Torvalds err = -EINVAL; 11734ac91378SJan Blunck if (!check_mnt(nd->path.mnt)) 11741da177e4SLinus Torvalds goto unlock; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* Refuse the same filesystem on the same mount point */ 11771da177e4SLinus Torvalds err = -EBUSY; 11784ac91378SJan Blunck if (nd->path.mnt->mnt_sb == newmnt->mnt_sb && 11794ac91378SJan Blunck nd->path.mnt->mnt_root == nd->path.dentry) 11801da177e4SLinus Torvalds goto unlock; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds err = -EINVAL; 11831da177e4SLinus Torvalds if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode)) 11841da177e4SLinus Torvalds goto unlock; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds newmnt->mnt_flags = mnt_flags; 11875b83d2c5SRam Pai if ((err = graft_tree(newmnt, nd))) 11885b83d2c5SRam Pai goto unlock; 11891da177e4SLinus Torvalds 11905b83d2c5SRam Pai if (fslist) { 11911da177e4SLinus Torvalds /* add to the specified expiration list */ 11921da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 119355e700b9SMiklos Szeredi list_add_tail(&newmnt->mnt_expire, fslist); 11941da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 11951da177e4SLinus Torvalds } 1196390c6843SRam Pai up_write(&namespace_sem); 11975b83d2c5SRam Pai return 0; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds unlock: 1200390c6843SRam Pai up_write(&namespace_sem); 12011da177e4SLinus Torvalds mntput(newmnt); 12021da177e4SLinus Torvalds return err; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount); 12061da177e4SLinus Torvalds 120770fbcdf4SRam Pai static void expire_mount(struct vfsmount *mnt, struct list_head *mounts, 120870fbcdf4SRam Pai struct list_head *umounts) 120924ca2af1SMiklos Szeredi { 121024ca2af1SMiklos Szeredi spin_lock(&vfsmount_lock); 121124ca2af1SMiklos Szeredi 121224ca2af1SMiklos Szeredi /* 1213ed42c879SMiklos Szeredi * Check if mount is still attached, if not, let whoever holds it deal 1214ed42c879SMiklos Szeredi * with the sucker 1215ed42c879SMiklos Szeredi */ 1216ed42c879SMiklos Szeredi if (mnt->mnt_parent == mnt) { 1217ed42c879SMiklos Szeredi spin_unlock(&vfsmount_lock); 1218ed42c879SMiklos Szeredi return; 1219ed42c879SMiklos Szeredi } 1220ed42c879SMiklos Szeredi 1221ed42c879SMiklos Szeredi /* 122224ca2af1SMiklos Szeredi * Check that it is still dead: the count should now be 2 - as 122324ca2af1SMiklos Szeredi * contributed by the vfsmount parent and the mntget above 122424ca2af1SMiklos Szeredi */ 1225a05964f3SRam Pai if (!propagate_mount_busy(mnt, 2)) { 122624ca2af1SMiklos Szeredi /* delete from the namespace */ 12276b3286edSKirill Korotaev touch_mnt_namespace(mnt->mnt_ns); 122824ca2af1SMiklos Szeredi list_del_init(&mnt->mnt_list); 12296b3286edSKirill Korotaev mnt->mnt_ns = NULL; 1230a05964f3SRam Pai umount_tree(mnt, 1, umounts); 123124ca2af1SMiklos Szeredi spin_unlock(&vfsmount_lock); 123224ca2af1SMiklos Szeredi } else { 123324ca2af1SMiklos Szeredi /* 123424ca2af1SMiklos Szeredi * Someone brought it back to life whilst we didn't have any 123524ca2af1SMiklos Szeredi * locks held so return it to the expiration list 123624ca2af1SMiklos Szeredi */ 123755e700b9SMiklos Szeredi list_add_tail(&mnt->mnt_expire, mounts); 123824ca2af1SMiklos Szeredi spin_unlock(&vfsmount_lock); 123924ca2af1SMiklos Szeredi } 124024ca2af1SMiklos Szeredi } 124124ca2af1SMiklos Szeredi 12421da177e4SLinus Torvalds /* 12435528f911STrond Myklebust * go through the vfsmounts we've just consigned to the graveyard to 12445528f911STrond Myklebust * - check that they're still dead 12455528f911STrond Myklebust * - delete the vfsmount from the appropriate namespace under lock 12465528f911STrond Myklebust * - dispose of the corpse 12475528f911STrond Myklebust */ 12485528f911STrond Myklebust static void expire_mount_list(struct list_head *graveyard, struct list_head *mounts) 12495528f911STrond Myklebust { 12506b3286edSKirill Korotaev struct mnt_namespace *ns; 12515528f911STrond Myklebust struct vfsmount *mnt; 12525528f911STrond Myklebust 12535528f911STrond Myklebust while (!list_empty(graveyard)) { 12545528f911STrond Myklebust LIST_HEAD(umounts); 1255b5e61818SPavel Emelianov mnt = list_first_entry(graveyard, struct vfsmount, mnt_expire); 12565528f911STrond Myklebust list_del_init(&mnt->mnt_expire); 12575528f911STrond Myklebust 12585528f911STrond Myklebust /* don't do anything if the namespace is dead - all the 12595528f911STrond Myklebust * vfsmounts from it are going away anyway */ 12606b3286edSKirill Korotaev ns = mnt->mnt_ns; 12616b3286edSKirill Korotaev if (!ns || !ns->root) 12625528f911STrond Myklebust continue; 12636b3286edSKirill Korotaev get_mnt_ns(ns); 12645528f911STrond Myklebust 12655528f911STrond Myklebust spin_unlock(&vfsmount_lock); 12665528f911STrond Myklebust down_write(&namespace_sem); 12675528f911STrond Myklebust expire_mount(mnt, mounts, &umounts); 12685528f911STrond Myklebust up_write(&namespace_sem); 12695528f911STrond Myklebust release_mounts(&umounts); 12705528f911STrond Myklebust mntput(mnt); 12716b3286edSKirill Korotaev put_mnt_ns(ns); 12725528f911STrond Myklebust spin_lock(&vfsmount_lock); 12735528f911STrond Myklebust } 12745528f911STrond Myklebust } 12755528f911STrond Myklebust 12765528f911STrond Myklebust /* 12771da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 12781da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 12791da177e4SLinus Torvalds * here 12801da177e4SLinus Torvalds */ 12811da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 12821da177e4SLinus Torvalds { 12831da177e4SLinus Torvalds struct vfsmount *mnt, *next; 12841da177e4SLinus Torvalds LIST_HEAD(graveyard); 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds if (list_empty(mounts)) 12871da177e4SLinus Torvalds return; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 12921da177e4SLinus Torvalds * following criteria: 12931da177e4SLinus Torvalds * - only referenced by its parent vfsmount 12941da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 12951da177e4SLinus Torvalds * cleared by mntput()) 12961da177e4SLinus Torvalds */ 129755e700b9SMiklos Szeredi list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 12981da177e4SLinus Torvalds if (!xchg(&mnt->mnt_expiry_mark, 1) || 12991da177e4SLinus Torvalds atomic_read(&mnt->mnt_count) != 1) 13001da177e4SLinus Torvalds continue; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds mntget(mnt); 130355e700b9SMiklos Szeredi list_move(&mnt->mnt_expire, &graveyard); 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13065528f911STrond Myklebust expire_mount_list(&graveyard, mounts); 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 13145528f911STrond Myklebust * Ripoff of 'select_parent()' 13155528f911STrond Myklebust * 13165528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 13175528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 13185528f911STrond Myklebust */ 13195528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard) 13205528f911STrond Myklebust { 13215528f911STrond Myklebust struct vfsmount *this_parent = parent; 13225528f911STrond Myklebust struct list_head *next; 13235528f911STrond Myklebust int found = 0; 13245528f911STrond Myklebust 13255528f911STrond Myklebust repeat: 13265528f911STrond Myklebust next = this_parent->mnt_mounts.next; 13275528f911STrond Myklebust resume: 13285528f911STrond Myklebust while (next != &this_parent->mnt_mounts) { 13295528f911STrond Myklebust struct list_head *tmp = next; 13305528f911STrond Myklebust struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child); 13315528f911STrond Myklebust 13325528f911STrond Myklebust next = tmp->next; 13335528f911STrond Myklebust if (!(mnt->mnt_flags & MNT_SHRINKABLE)) 13345528f911STrond Myklebust continue; 13355528f911STrond Myklebust /* 13365528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 13375528f911STrond Myklebust */ 13385528f911STrond Myklebust if (!list_empty(&mnt->mnt_mounts)) { 13395528f911STrond Myklebust this_parent = mnt; 13405528f911STrond Myklebust goto repeat; 13415528f911STrond Myklebust } 13425528f911STrond Myklebust 13435528f911STrond Myklebust if (!propagate_mount_busy(mnt, 1)) { 13445528f911STrond Myklebust mntget(mnt); 13455528f911STrond Myklebust list_move_tail(&mnt->mnt_expire, graveyard); 13465528f911STrond Myklebust found++; 13475528f911STrond Myklebust } 13485528f911STrond Myklebust } 13495528f911STrond Myklebust /* 13505528f911STrond Myklebust * All done at this level ... ascend and resume the search 13515528f911STrond Myklebust */ 13525528f911STrond Myklebust if (this_parent != parent) { 13535528f911STrond Myklebust next = this_parent->mnt_child.next; 13545528f911STrond Myklebust this_parent = this_parent->mnt_parent; 13555528f911STrond Myklebust goto resume; 13565528f911STrond Myklebust } 13575528f911STrond Myklebust return found; 13585528f911STrond Myklebust } 13595528f911STrond Myklebust 13605528f911STrond Myklebust /* 13615528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 13625528f911STrond Myklebust * submounts of a specific parent mountpoint 13635528f911STrond Myklebust */ 13645528f911STrond Myklebust void shrink_submounts(struct vfsmount *mountpoint, struct list_head *mounts) 13655528f911STrond Myklebust { 13665528f911STrond Myklebust LIST_HEAD(graveyard); 13675528f911STrond Myklebust int found; 13685528f911STrond Myklebust 13695528f911STrond Myklebust spin_lock(&vfsmount_lock); 13705528f911STrond Myklebust 13715528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 13725528f911STrond Myklebust while ((found = select_submounts(mountpoint, &graveyard)) != 0) 13735528f911STrond Myklebust expire_mount_list(&graveyard, mounts); 13745528f911STrond Myklebust 13755528f911STrond Myklebust spin_unlock(&vfsmount_lock); 13765528f911STrond Myklebust } 13775528f911STrond Myklebust 13785528f911STrond Myklebust EXPORT_SYMBOL_GPL(shrink_submounts); 13795528f911STrond Myklebust 13805528f911STrond Myklebust /* 13811da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 13821da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 13831da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 13841da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 13851da177e4SLinus Torvalds */ 1386b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 1387b58fed8bSRam Pai unsigned long n) 13881da177e4SLinus Torvalds { 13891da177e4SLinus Torvalds char *t = to; 13901da177e4SLinus Torvalds const char __user *f = from; 13911da177e4SLinus Torvalds char c; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 13941da177e4SLinus Torvalds return n; 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds while (n) { 13971da177e4SLinus Torvalds if (__get_user(c, f)) { 13981da177e4SLinus Torvalds memset(t, 0, n); 13991da177e4SLinus Torvalds break; 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds *t++ = c; 14021da177e4SLinus Torvalds f++; 14031da177e4SLinus Torvalds n--; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds return n; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 14091da177e4SLinus Torvalds { 14101da177e4SLinus Torvalds int i; 14111da177e4SLinus Torvalds unsigned long page; 14121da177e4SLinus Torvalds unsigned long size; 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds *where = 0; 14151da177e4SLinus Torvalds if (!data) 14161da177e4SLinus Torvalds return 0; 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 14191da177e4SLinus Torvalds return -ENOMEM; 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds /* We only care that *some* data at the address the user 14221da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 14231da177e4SLinus Torvalds * the remainder of the page. 14241da177e4SLinus Torvalds */ 14251da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 14261da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 14271da177e4SLinus Torvalds if (size > PAGE_SIZE) 14281da177e4SLinus Torvalds size = PAGE_SIZE; 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 14311da177e4SLinus Torvalds if (!i) { 14321da177e4SLinus Torvalds free_page(page); 14331da177e4SLinus Torvalds return -EFAULT; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds if (i != PAGE_SIZE) 14361da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 14371da177e4SLinus Torvalds *where = page; 14381da177e4SLinus Torvalds return 0; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds /* 14421da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 14431da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 14441da177e4SLinus Torvalds * 14451da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 14461da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 14471da177e4SLinus Torvalds * information (or be NULL). 14481da177e4SLinus Torvalds * 14491da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 14501da177e4SLinus Torvalds * When the flags word was introduced its top half was required 14511da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 14521da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 14531da177e4SLinus Torvalds * and must be discarded. 14541da177e4SLinus Torvalds */ 14551da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 14561da177e4SLinus Torvalds unsigned long flags, void *data_page) 14571da177e4SLinus Torvalds { 14581da177e4SLinus Torvalds struct nameidata nd; 14591da177e4SLinus Torvalds int retval = 0; 14601da177e4SLinus Torvalds int mnt_flags = 0; 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds /* Discard magic */ 14631da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 14641da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds /* Basic sanity checks */ 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 14691da177e4SLinus Torvalds return -EINVAL; 14701da177e4SLinus Torvalds if (dev_name && !memchr(dev_name, 0, PAGE_SIZE)) 14711da177e4SLinus Torvalds return -EINVAL; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds if (data_page) 14741da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 14771da177e4SLinus Torvalds if (flags & MS_NOSUID) 14781da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 14791da177e4SLinus Torvalds if (flags & MS_NODEV) 14801da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 14811da177e4SLinus Torvalds if (flags & MS_NOEXEC) 14821da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 1483fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 1484fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 1485fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 1486fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 148747ae32d6SValerie Henson if (flags & MS_RELATIME) 148847ae32d6SValerie Henson mnt_flags |= MNT_RELATIME; 1489fc33a7bbSChristoph Hellwig 1490fc33a7bbSChristoph Hellwig flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | 14918bf9725cSPavel Emelyanov MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT); 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds /* ... and get the mountpoint */ 14941da177e4SLinus Torvalds retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd); 14951da177e4SLinus Torvalds if (retval) 14961da177e4SLinus Torvalds return retval; 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page); 14991da177e4SLinus Torvalds if (retval) 15001da177e4SLinus Torvalds goto dput_out; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds if (flags & MS_REMOUNT) 15031da177e4SLinus Torvalds retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags, 15041da177e4SLinus Torvalds data_page); 15051da177e4SLinus Torvalds else if (flags & MS_BIND) 1506eee391a6SAndrew Morton retval = do_loopback(&nd, dev_name, flags & MS_REC); 15079676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 150807b20889SRam Pai retval = do_change_type(&nd, flags); 15091da177e4SLinus Torvalds else if (flags & MS_MOVE) 15101da177e4SLinus Torvalds retval = do_move_mount(&nd, dev_name); 15111da177e4SLinus Torvalds else 15121da177e4SLinus Torvalds retval = do_new_mount(&nd, type_page, flags, mnt_flags, 15131da177e4SLinus Torvalds dev_name, data_page); 15141da177e4SLinus Torvalds dput_out: 15151d957f9bSJan Blunck path_put(&nd.path); 15161da177e4SLinus Torvalds return retval; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 1519741a2951SJANAK DESAI /* 1520741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 1521741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 1522741a2951SJANAK DESAI */ 1523e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 15246b3286edSKirill Korotaev struct fs_struct *fs) 15251da177e4SLinus Torvalds { 15266b3286edSKirill Korotaev struct mnt_namespace *new_ns; 15271da177e4SLinus Torvalds struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL; 15281da177e4SLinus Torvalds struct vfsmount *p, *q; 15291da177e4SLinus Torvalds 15306b3286edSKirill Korotaev new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 15311da177e4SLinus Torvalds if (!new_ns) 1532467e9f4bSCedric Le Goater return ERR_PTR(-ENOMEM); 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds atomic_set(&new_ns->count, 1); 15351da177e4SLinus Torvalds INIT_LIST_HEAD(&new_ns->list); 15365addc5ddSAl Viro init_waitqueue_head(&new_ns->poll); 15375addc5ddSAl Viro new_ns->event = 0; 15381da177e4SLinus Torvalds 1539390c6843SRam Pai down_write(&namespace_sem); 15401da177e4SLinus Torvalds /* First pass: copy the tree topology */ 15416b3286edSKirill Korotaev new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root, 15429676f0c6SRam Pai CL_COPY_ALL | CL_EXPIRE); 15431da177e4SLinus Torvalds if (!new_ns->root) { 1544390c6843SRam Pai up_write(&namespace_sem); 15451da177e4SLinus Torvalds kfree(new_ns); 1546467e9f4bSCedric Le Goater return ERR_PTR(-ENOMEM);; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 15491da177e4SLinus Torvalds list_add_tail(&new_ns->list, &new_ns->root->mnt_list); 15501da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds /* 15531da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 15541da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 15551da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 15561da177e4SLinus Torvalds */ 15576b3286edSKirill Korotaev p = mnt_ns->root; 15581da177e4SLinus Torvalds q = new_ns->root; 15591da177e4SLinus Torvalds while (p) { 15606b3286edSKirill Korotaev q->mnt_ns = new_ns; 15611da177e4SLinus Torvalds if (fs) { 15626ac08c39SJan Blunck if (p == fs->root.mnt) { 15631da177e4SLinus Torvalds rootmnt = p; 15646ac08c39SJan Blunck fs->root.mnt = mntget(q); 15651da177e4SLinus Torvalds } 15666ac08c39SJan Blunck if (p == fs->pwd.mnt) { 15671da177e4SLinus Torvalds pwdmnt = p; 15686ac08c39SJan Blunck fs->pwd.mnt = mntget(q); 15691da177e4SLinus Torvalds } 15706ac08c39SJan Blunck if (p == fs->altroot.mnt) { 15711da177e4SLinus Torvalds altrootmnt = p; 15726ac08c39SJan Blunck fs->altroot.mnt = mntget(q); 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds } 15756b3286edSKirill Korotaev p = next_mnt(p, mnt_ns->root); 15761da177e4SLinus Torvalds q = next_mnt(q, new_ns->root); 15771da177e4SLinus Torvalds } 1578390c6843SRam Pai up_write(&namespace_sem); 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds if (rootmnt) 15811da177e4SLinus Torvalds mntput(rootmnt); 15821da177e4SLinus Torvalds if (pwdmnt) 15831da177e4SLinus Torvalds mntput(pwdmnt); 15841da177e4SLinus Torvalds if (altrootmnt) 15851da177e4SLinus Torvalds mntput(altrootmnt); 15861da177e4SLinus Torvalds 1587741a2951SJANAK DESAI return new_ns; 1588741a2951SJANAK DESAI } 1589741a2951SJANAK DESAI 1590213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 1591e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 1592741a2951SJANAK DESAI { 15936b3286edSKirill Korotaev struct mnt_namespace *new_ns; 1594741a2951SJANAK DESAI 1595e3222c4eSBadari Pulavarty BUG_ON(!ns); 15966b3286edSKirill Korotaev get_mnt_ns(ns); 1597741a2951SJANAK DESAI 1598741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 1599e3222c4eSBadari Pulavarty return ns; 1600741a2951SJANAK DESAI 1601e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 1602741a2951SJANAK DESAI 16036b3286edSKirill Korotaev put_mnt_ns(ns); 1604e3222c4eSBadari Pulavarty return new_ns; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name, 16081da177e4SLinus Torvalds char __user * type, unsigned long flags, 16091da177e4SLinus Torvalds void __user * data) 16101da177e4SLinus Torvalds { 16111da177e4SLinus Torvalds int retval; 16121da177e4SLinus Torvalds unsigned long data_page; 16131da177e4SLinus Torvalds unsigned long type_page; 16141da177e4SLinus Torvalds unsigned long dev_page; 16151da177e4SLinus Torvalds char *dir_page; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds retval = copy_mount_options(type, &type_page); 16181da177e4SLinus Torvalds if (retval < 0) 16191da177e4SLinus Torvalds return retval; 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds dir_page = getname(dir_name); 16221da177e4SLinus Torvalds retval = PTR_ERR(dir_page); 16231da177e4SLinus Torvalds if (IS_ERR(dir_page)) 16241da177e4SLinus Torvalds goto out1; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds retval = copy_mount_options(dev_name, &dev_page); 16271da177e4SLinus Torvalds if (retval < 0) 16281da177e4SLinus Torvalds goto out2; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds retval = copy_mount_options(data, &data_page); 16311da177e4SLinus Torvalds if (retval < 0) 16321da177e4SLinus Torvalds goto out3; 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds lock_kernel(); 16351da177e4SLinus Torvalds retval = do_mount((char *)dev_page, dir_page, (char *)type_page, 16361da177e4SLinus Torvalds flags, (void *)data_page); 16371da177e4SLinus Torvalds unlock_kernel(); 16381da177e4SLinus Torvalds free_page(data_page); 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds out3: 16411da177e4SLinus Torvalds free_page(dev_page); 16421da177e4SLinus Torvalds out2: 16431da177e4SLinus Torvalds putname(dir_page); 16441da177e4SLinus Torvalds out1: 16451da177e4SLinus Torvalds free_page(type_page); 16461da177e4SLinus Torvalds return retval; 16471da177e4SLinus Torvalds } 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds /* 16501da177e4SLinus Torvalds * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values. 16511da177e4SLinus Torvalds * It can block. Requires the big lock held. 16521da177e4SLinus Torvalds */ 1653*ac748a09SJan Blunck void set_fs_root(struct fs_struct *fs, struct path *path) 16541da177e4SLinus Torvalds { 16556ac08c39SJan Blunck struct path old_root; 16566ac08c39SJan Blunck 16571da177e4SLinus Torvalds write_lock(&fs->lock); 16581da177e4SLinus Torvalds old_root = fs->root; 1659*ac748a09SJan Blunck fs->root = *path; 1660*ac748a09SJan Blunck path_get(path); 16611da177e4SLinus Torvalds write_unlock(&fs->lock); 16626ac08c39SJan Blunck if (old_root.dentry) 16636ac08c39SJan Blunck path_put(&old_root); 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds /* 16671da177e4SLinus Torvalds * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values. 16681da177e4SLinus Torvalds * It can block. Requires the big lock held. 16691da177e4SLinus Torvalds */ 1670*ac748a09SJan Blunck void set_fs_pwd(struct fs_struct *fs, struct path *path) 16711da177e4SLinus Torvalds { 16726ac08c39SJan Blunck struct path old_pwd; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds write_lock(&fs->lock); 16751da177e4SLinus Torvalds old_pwd = fs->pwd; 1676*ac748a09SJan Blunck fs->pwd = *path; 1677*ac748a09SJan Blunck path_get(path); 16781da177e4SLinus Torvalds write_unlock(&fs->lock); 16791da177e4SLinus Torvalds 16806ac08c39SJan Blunck if (old_pwd.dentry) 16816ac08c39SJan Blunck path_put(&old_pwd); 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd) 16851da177e4SLinus Torvalds { 16861da177e4SLinus Torvalds struct task_struct *g, *p; 16871da177e4SLinus Torvalds struct fs_struct *fs; 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds read_lock(&tasklist_lock); 16901da177e4SLinus Torvalds do_each_thread(g, p) { 16911da177e4SLinus Torvalds task_lock(p); 16921da177e4SLinus Torvalds fs = p->fs; 16931da177e4SLinus Torvalds if (fs) { 16941da177e4SLinus Torvalds atomic_inc(&fs->count); 16951da177e4SLinus Torvalds task_unlock(p); 16966ac08c39SJan Blunck if (fs->root.dentry == old_nd->path.dentry 16976ac08c39SJan Blunck && fs->root.mnt == old_nd->path.mnt) 1698*ac748a09SJan Blunck set_fs_root(fs, &new_nd->path); 16996ac08c39SJan Blunck if (fs->pwd.dentry == old_nd->path.dentry 17006ac08c39SJan Blunck && fs->pwd.mnt == old_nd->path.mnt) 1701*ac748a09SJan Blunck set_fs_pwd(fs, &new_nd->path); 17021da177e4SLinus Torvalds put_fs_struct(fs); 17031da177e4SLinus Torvalds } else 17041da177e4SLinus Torvalds task_unlock(p); 17051da177e4SLinus Torvalds } while_each_thread(g, p); 17061da177e4SLinus Torvalds read_unlock(&tasklist_lock); 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds /* 17101da177e4SLinus Torvalds * pivot_root Semantics: 17111da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 17121da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 17131da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 17141da177e4SLinus Torvalds * 17151da177e4SLinus Torvalds * Restrictions: 17161da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 17171da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 17181da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 17191da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 17201da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 17211da177e4SLinus Torvalds * 17224a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 17234a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 17244a0d11faSNeil Brown * in this situation. 17254a0d11faSNeil Brown * 17261da177e4SLinus Torvalds * Notes: 17271da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 17281da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 17291da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 17301da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 17311da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 17321da177e4SLinus Torvalds * first. 17331da177e4SLinus Torvalds */ 1734b58fed8bSRam Pai asmlinkage long sys_pivot_root(const char __user * new_root, 1735b58fed8bSRam Pai const char __user * put_old) 17361da177e4SLinus Torvalds { 17371da177e4SLinus Torvalds struct vfsmount *tmp; 17381da177e4SLinus Torvalds struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd; 17391da177e4SLinus Torvalds int error; 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17421da177e4SLinus Torvalds return -EPERM; 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds lock_kernel(); 17451da177e4SLinus Torvalds 1746b58fed8bSRam Pai error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, 1747b58fed8bSRam Pai &new_nd); 17481da177e4SLinus Torvalds if (error) 17491da177e4SLinus Torvalds goto out0; 17501da177e4SLinus Torvalds error = -EINVAL; 17514ac91378SJan Blunck if (!check_mnt(new_nd.path.mnt)) 17521da177e4SLinus Torvalds goto out1; 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd); 17551da177e4SLinus Torvalds if (error) 17561da177e4SLinus Torvalds goto out1; 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds error = security_sb_pivotroot(&old_nd, &new_nd); 17591da177e4SLinus Torvalds if (error) { 17601d957f9bSJan Blunck path_put(&old_nd.path); 17611da177e4SLinus Torvalds goto out1; 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds read_lock(¤t->fs->lock); 17656ac08c39SJan Blunck user_nd.path = current->fs->root; 17666ac08c39SJan Blunck path_get(¤t->fs->root); 17671da177e4SLinus Torvalds read_unlock(¤t->fs->lock); 1768390c6843SRam Pai down_write(&namespace_sem); 17694ac91378SJan Blunck mutex_lock(&old_nd.path.dentry->d_inode->i_mutex); 17701da177e4SLinus Torvalds error = -EINVAL; 17714ac91378SJan Blunck if (IS_MNT_SHARED(old_nd.path.mnt) || 17724ac91378SJan Blunck IS_MNT_SHARED(new_nd.path.mnt->mnt_parent) || 17734ac91378SJan Blunck IS_MNT_SHARED(user_nd.path.mnt->mnt_parent)) 177421444403SRam Pai goto out2; 17754ac91378SJan Blunck if (!check_mnt(user_nd.path.mnt)) 17761da177e4SLinus Torvalds goto out2; 17771da177e4SLinus Torvalds error = -ENOENT; 17784ac91378SJan Blunck if (IS_DEADDIR(new_nd.path.dentry->d_inode)) 17791da177e4SLinus Torvalds goto out2; 17804ac91378SJan Blunck if (d_unhashed(new_nd.path.dentry) && !IS_ROOT(new_nd.path.dentry)) 17811da177e4SLinus Torvalds goto out2; 17824ac91378SJan Blunck if (d_unhashed(old_nd.path.dentry) && !IS_ROOT(old_nd.path.dentry)) 17831da177e4SLinus Torvalds goto out2; 17841da177e4SLinus Torvalds error = -EBUSY; 17854ac91378SJan Blunck if (new_nd.path.mnt == user_nd.path.mnt || 17864ac91378SJan Blunck old_nd.path.mnt == user_nd.path.mnt) 17871da177e4SLinus Torvalds goto out2; /* loop, on the same file system */ 17881da177e4SLinus Torvalds error = -EINVAL; 17894ac91378SJan Blunck if (user_nd.path.mnt->mnt_root != user_nd.path.dentry) 17901da177e4SLinus Torvalds goto out2; /* not a mountpoint */ 17914ac91378SJan Blunck if (user_nd.path.mnt->mnt_parent == user_nd.path.mnt) 17920bb6fcc1SMiklos Szeredi goto out2; /* not attached */ 17934ac91378SJan Blunck if (new_nd.path.mnt->mnt_root != new_nd.path.dentry) 17941da177e4SLinus Torvalds goto out2; /* not a mountpoint */ 17954ac91378SJan Blunck if (new_nd.path.mnt->mnt_parent == new_nd.path.mnt) 17960bb6fcc1SMiklos Szeredi goto out2; /* not attached */ 17974ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 17984ac91378SJan Blunck tmp = old_nd.path.mnt; 17991da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 18004ac91378SJan Blunck if (tmp != new_nd.path.mnt) { 18011da177e4SLinus Torvalds for (;;) { 18021da177e4SLinus Torvalds if (tmp->mnt_parent == tmp) 18031da177e4SLinus Torvalds goto out3; /* already mounted on put_old */ 18044ac91378SJan Blunck if (tmp->mnt_parent == new_nd.path.mnt) 18051da177e4SLinus Torvalds break; 18061da177e4SLinus Torvalds tmp = tmp->mnt_parent; 18071da177e4SLinus Torvalds } 18084ac91378SJan Blunck if (!is_subdir(tmp->mnt_mountpoint, new_nd.path.dentry)) 18091da177e4SLinus Torvalds goto out3; 18104ac91378SJan Blunck } else if (!is_subdir(old_nd.path.dentry, new_nd.path.dentry)) 18111da177e4SLinus Torvalds goto out3; 18124ac91378SJan Blunck detach_mnt(new_nd.path.mnt, &parent_nd); 18134ac91378SJan Blunck detach_mnt(user_nd.path.mnt, &root_parent); 18144ac91378SJan Blunck /* mount old root on put_old */ 18154ac91378SJan Blunck attach_mnt(user_nd.path.mnt, &old_nd); 18164ac91378SJan Blunck /* mount new_root on / */ 18174ac91378SJan Blunck attach_mnt(new_nd.path.mnt, &root_parent); 18186b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 18191da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 18201da177e4SLinus Torvalds chroot_fs_refs(&user_nd, &new_nd); 18211da177e4SLinus Torvalds security_sb_post_pivotroot(&user_nd, &new_nd); 18221da177e4SLinus Torvalds error = 0; 18231d957f9bSJan Blunck path_put(&root_parent.path); 18241d957f9bSJan Blunck path_put(&parent_nd.path); 18251da177e4SLinus Torvalds out2: 18264ac91378SJan Blunck mutex_unlock(&old_nd.path.dentry->d_inode->i_mutex); 1827390c6843SRam Pai up_write(&namespace_sem); 18281d957f9bSJan Blunck path_put(&user_nd.path); 18291d957f9bSJan Blunck path_put(&old_nd.path); 18301da177e4SLinus Torvalds out1: 18311d957f9bSJan Blunck path_put(&new_nd.path); 18321da177e4SLinus Torvalds out0: 18331da177e4SLinus Torvalds unlock_kernel(); 18341da177e4SLinus Torvalds return error; 18351da177e4SLinus Torvalds out3: 18361da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 18371da177e4SLinus Torvalds goto out2; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds static void __init init_mount_tree(void) 18411da177e4SLinus Torvalds { 18421da177e4SLinus Torvalds struct vfsmount *mnt; 18436b3286edSKirill Korotaev struct mnt_namespace *ns; 1844*ac748a09SJan Blunck struct path root; 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 18471da177e4SLinus Torvalds if (IS_ERR(mnt)) 18481da177e4SLinus Torvalds panic("Can't create rootfs"); 18496b3286edSKirill Korotaev ns = kmalloc(sizeof(*ns), GFP_KERNEL); 18506b3286edSKirill Korotaev if (!ns) 18511da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 18526b3286edSKirill Korotaev atomic_set(&ns->count, 1); 18536b3286edSKirill Korotaev INIT_LIST_HEAD(&ns->list); 18546b3286edSKirill Korotaev init_waitqueue_head(&ns->poll); 18556b3286edSKirill Korotaev ns->event = 0; 18566b3286edSKirill Korotaev list_add(&mnt->mnt_list, &ns->list); 18576b3286edSKirill Korotaev ns->root = mnt; 18586b3286edSKirill Korotaev mnt->mnt_ns = ns; 18591da177e4SLinus Torvalds 18606b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 18616b3286edSKirill Korotaev get_mnt_ns(ns); 18621da177e4SLinus Torvalds 1863*ac748a09SJan Blunck root.mnt = ns->root; 1864*ac748a09SJan Blunck root.dentry = ns->root->mnt_root; 1865*ac748a09SJan Blunck 1866*ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 1867*ac748a09SJan Blunck set_fs_root(current->fs, &root); 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 187074bf17cfSDenis Cheng void __init mnt_init(void) 18711da177e4SLinus Torvalds { 187213f14b4dSEric Dumazet unsigned u; 187315a67dd8SRandy Dunlap int err; 18741da177e4SLinus Torvalds 1875390c6843SRam Pai init_rwsem(&namespace_sem); 1876390c6843SRam Pai 18771da177e4SLinus Torvalds mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount), 187820c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 18791da177e4SLinus Torvalds 1880b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds if (!mount_hashtable) 18831da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 18841da177e4SLinus Torvalds 188513f14b4dSEric Dumazet printk("Mount-cache hash table entries: %lu\n", HASH_SIZE); 18861da177e4SLinus Torvalds 188713f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 188813f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 18891da177e4SLinus Torvalds 189015a67dd8SRandy Dunlap err = sysfs_init(); 189115a67dd8SRandy Dunlap if (err) 189215a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 189315a67dd8SRandy Dunlap __FUNCTION__, err); 189400d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 189500d26666SGreg Kroah-Hartman if (!fs_kobj) 189600d26666SGreg Kroah-Hartman printk(KERN_WARNING "%s: kobj create error\n", __FUNCTION__); 18971da177e4SLinus Torvalds init_rootfs(); 18981da177e4SLinus Torvalds init_mount_tree(); 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 19016b3286edSKirill Korotaev void __put_mnt_ns(struct mnt_namespace *ns) 19021da177e4SLinus Torvalds { 19036b3286edSKirill Korotaev struct vfsmount *root = ns->root; 190470fbcdf4SRam Pai LIST_HEAD(umount_list); 19056b3286edSKirill Korotaev ns->root = NULL; 19061ce88cf4SMiklos Szeredi spin_unlock(&vfsmount_lock); 1907390c6843SRam Pai down_write(&namespace_sem); 19081da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 1909a05964f3SRam Pai umount_tree(root, 0, &umount_list); 19101da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 1911390c6843SRam Pai up_write(&namespace_sem); 191270fbcdf4SRam Pai release_mounts(&umount_list); 19136b3286edSKirill Korotaev kfree(ns); 19141da177e4SLinus Torvalds } 1915