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> 203d733633SDave Hansen #include <linux/cpumask.h> 211da177e4SLinus Torvalds #include <linux/module.h> 22f20a9eadSAndrew Morton #include <linux/sysfs.h> 231da177e4SLinus Torvalds #include <linux/seq_file.h> 246b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 251da177e4SLinus Torvalds #include <linux/namei.h> 261da177e4SLinus Torvalds #include <linux/security.h> 271da177e4SLinus Torvalds #include <linux/mount.h> 2807f3f05cSDavid Howells #include <linux/ramfs.h> 2913f14b4dSEric Dumazet #include <linux/log2.h> 301da177e4SLinus Torvalds #include <asm/uaccess.h> 311da177e4SLinus Torvalds #include <asm/unistd.h> 3207b20889SRam Pai #include "pnode.h" 33948730b0SAdrian Bunk #include "internal.h" 341da177e4SLinus Torvalds 3513f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head)) 3613f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT) 3713f14b4dSEric Dumazet 381da177e4SLinus Torvalds /* spinlock for vfsmount related operations, inplace of dcache_lock */ 391da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock); 401da177e4SLinus Torvalds 415addc5ddSAl Viro static int event; 425addc5ddSAl Viro 43fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly; 44e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 45390c6843SRam Pai static struct rw_semaphore namespace_sem; 461da177e4SLinus Torvalds 47f87fd4c2SMiklos Szeredi /* /sys/fs */ 4800d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 4900d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 50f87fd4c2SMiklos Szeredi 511da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) 521da177e4SLinus Torvalds { 531da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 541da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 5513f14b4dSEric Dumazet tmp = tmp + (tmp >> HASH_SHIFT); 5613f14b4dSEric Dumazet return tmp & (HASH_SIZE - 1); 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 593d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16) 603d733633SDave Hansen 611da177e4SLinus Torvalds struct vfsmount *alloc_vfsmnt(const char *name) 621da177e4SLinus Torvalds { 63c3762229SRobert P. J. Day struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 641da177e4SLinus Torvalds if (mnt) { 651da177e4SLinus Torvalds atomic_set(&mnt->mnt_count, 1); 661da177e4SLinus Torvalds INIT_LIST_HEAD(&mnt->mnt_hash); 671da177e4SLinus Torvalds INIT_LIST_HEAD(&mnt->mnt_child); 681da177e4SLinus Torvalds INIT_LIST_HEAD(&mnt->mnt_mounts); 691da177e4SLinus Torvalds INIT_LIST_HEAD(&mnt->mnt_list); 7055e700b9SMiklos Szeredi INIT_LIST_HEAD(&mnt->mnt_expire); 7103e06e68SRam Pai INIT_LIST_HEAD(&mnt->mnt_share); 72a58b0eb8SRam Pai INIT_LIST_HEAD(&mnt->mnt_slave_list); 73a58b0eb8SRam Pai INIT_LIST_HEAD(&mnt->mnt_slave); 743d733633SDave Hansen atomic_set(&mnt->__mnt_writers, 0); 751da177e4SLinus Torvalds if (name) { 761da177e4SLinus Torvalds int size = strlen(name) + 1; 771da177e4SLinus Torvalds char *newname = kmalloc(size, GFP_KERNEL); 781da177e4SLinus Torvalds if (newname) { 791da177e4SLinus Torvalds memcpy(newname, name, size); 801da177e4SLinus Torvalds mnt->mnt_devname = newname; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds return mnt; 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 878366025eSDave Hansen /* 888366025eSDave Hansen * Most r/o checks on a fs are for operations that take 898366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 908366025eSDave Hansen * We must keep track of when those operations start 918366025eSDave Hansen * (for permission checks) and when they end, so that 928366025eSDave Hansen * we can determine when writes are able to occur to 938366025eSDave Hansen * a filesystem. 948366025eSDave Hansen */ 953d733633SDave Hansen /* 963d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 973d733633SDave Hansen * @mnt: the mount to check for its write status 983d733633SDave Hansen * 993d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 1003d733633SDave Hansen * It does not guarantee that the filesystem will stay 1013d733633SDave Hansen * r/w, just that it is right *now*. This can not and 1023d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 1033d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 1043d733633SDave Hansen * r/w. 1053d733633SDave Hansen */ 1063d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 1073d733633SDave Hansen { 1082e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 1092e4b7fcdSDave Hansen return 1; 1102e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 1112e4b7fcdSDave Hansen return 1; 1122e4b7fcdSDave Hansen return 0; 1133d733633SDave Hansen } 1143d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 1153d733633SDave Hansen 1163d733633SDave Hansen struct mnt_writer { 1173d733633SDave Hansen /* 1183d733633SDave Hansen * If holding multiple instances of this lock, they 1193d733633SDave Hansen * must be ordered by cpu number. 1203d733633SDave Hansen */ 1213d733633SDave Hansen spinlock_t lock; 1223d733633SDave Hansen struct lock_class_key lock_class; /* compiles out with !lockdep */ 1233d733633SDave Hansen unsigned long count; 1243d733633SDave Hansen struct vfsmount *mnt; 1253d733633SDave Hansen } ____cacheline_aligned_in_smp; 1263d733633SDave Hansen static DEFINE_PER_CPU(struct mnt_writer, mnt_writers); 1273d733633SDave Hansen 1283d733633SDave Hansen static int __init init_mnt_writers(void) 1293d733633SDave Hansen { 1303d733633SDave Hansen int cpu; 1313d733633SDave Hansen for_each_possible_cpu(cpu) { 1323d733633SDave Hansen struct mnt_writer *writer = &per_cpu(mnt_writers, cpu); 1333d733633SDave Hansen spin_lock_init(&writer->lock); 1343d733633SDave Hansen lockdep_set_class(&writer->lock, &writer->lock_class); 1353d733633SDave Hansen writer->count = 0; 1363d733633SDave Hansen } 1373d733633SDave Hansen return 0; 1383d733633SDave Hansen } 1393d733633SDave Hansen fs_initcall(init_mnt_writers); 1403d733633SDave Hansen 1413d733633SDave Hansen static void unlock_mnt_writers(void) 1423d733633SDave Hansen { 1433d733633SDave Hansen int cpu; 1443d733633SDave Hansen struct mnt_writer *cpu_writer; 1453d733633SDave Hansen 1463d733633SDave Hansen for_each_possible_cpu(cpu) { 1473d733633SDave Hansen cpu_writer = &per_cpu(mnt_writers, cpu); 1483d733633SDave Hansen spin_unlock(&cpu_writer->lock); 1493d733633SDave Hansen } 1503d733633SDave Hansen } 1513d733633SDave Hansen 1523d733633SDave Hansen static inline void __clear_mnt_count(struct mnt_writer *cpu_writer) 1533d733633SDave Hansen { 1543d733633SDave Hansen if (!cpu_writer->mnt) 1553d733633SDave Hansen return; 1563d733633SDave Hansen /* 1573d733633SDave Hansen * This is in case anyone ever leaves an invalid, 1583d733633SDave Hansen * old ->mnt and a count of 0. 1593d733633SDave Hansen */ 1603d733633SDave Hansen if (!cpu_writer->count) 1613d733633SDave Hansen return; 1623d733633SDave Hansen atomic_add(cpu_writer->count, &cpu_writer->mnt->__mnt_writers); 1633d733633SDave Hansen cpu_writer->count = 0; 1643d733633SDave Hansen } 1653d733633SDave Hansen /* 1663d733633SDave Hansen * must hold cpu_writer->lock 1673d733633SDave Hansen */ 1683d733633SDave Hansen static inline void use_cpu_writer_for_mount(struct mnt_writer *cpu_writer, 1693d733633SDave Hansen struct vfsmount *mnt) 1703d733633SDave Hansen { 1713d733633SDave Hansen if (cpu_writer->mnt == mnt) 1723d733633SDave Hansen return; 1733d733633SDave Hansen __clear_mnt_count(cpu_writer); 1743d733633SDave Hansen cpu_writer->mnt = mnt; 1753d733633SDave Hansen } 1763d733633SDave Hansen 1773d733633SDave Hansen /* 1783d733633SDave Hansen * Most r/o checks on a fs are for operations that take 1793d733633SDave Hansen * discrete amounts of time, like a write() or unlink(). 1803d733633SDave Hansen * We must keep track of when those operations start 1813d733633SDave Hansen * (for permission checks) and when they end, so that 1823d733633SDave Hansen * we can determine when writes are able to occur to 1833d733633SDave Hansen * a filesystem. 1843d733633SDave Hansen */ 1858366025eSDave Hansen /** 1868366025eSDave Hansen * mnt_want_write - get write access to a mount 1878366025eSDave Hansen * @mnt: the mount on which to take a write 1888366025eSDave Hansen * 1898366025eSDave Hansen * This tells the low-level filesystem that a write is 1908366025eSDave Hansen * about to be performed to it, and makes sure that 1918366025eSDave Hansen * writes are allowed before returning success. When 1928366025eSDave Hansen * the write operation is finished, mnt_drop_write() 1938366025eSDave Hansen * must be called. This is effectively a refcount. 1948366025eSDave Hansen */ 1958366025eSDave Hansen int mnt_want_write(struct vfsmount *mnt) 1968366025eSDave Hansen { 1973d733633SDave Hansen int ret = 0; 1983d733633SDave Hansen struct mnt_writer *cpu_writer; 1993d733633SDave Hansen 2003d733633SDave Hansen cpu_writer = &get_cpu_var(mnt_writers); 2013d733633SDave Hansen spin_lock(&cpu_writer->lock); 2023d733633SDave Hansen if (__mnt_is_readonly(mnt)) { 2033d733633SDave Hansen ret = -EROFS; 2043d733633SDave Hansen goto out; 2053d733633SDave Hansen } 2063d733633SDave Hansen use_cpu_writer_for_mount(cpu_writer, mnt); 2073d733633SDave Hansen cpu_writer->count++; 2083d733633SDave Hansen out: 2093d733633SDave Hansen spin_unlock(&cpu_writer->lock); 2103d733633SDave Hansen put_cpu_var(mnt_writers); 2113d733633SDave Hansen return ret; 2128366025eSDave Hansen } 2138366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 2148366025eSDave Hansen 2153d733633SDave Hansen static void lock_mnt_writers(void) 2163d733633SDave Hansen { 2173d733633SDave Hansen int cpu; 2183d733633SDave Hansen struct mnt_writer *cpu_writer; 2193d733633SDave Hansen 2203d733633SDave Hansen for_each_possible_cpu(cpu) { 2213d733633SDave Hansen cpu_writer = &per_cpu(mnt_writers, cpu); 2223d733633SDave Hansen spin_lock(&cpu_writer->lock); 2233d733633SDave Hansen __clear_mnt_count(cpu_writer); 2243d733633SDave Hansen cpu_writer->mnt = NULL; 2253d733633SDave Hansen } 2263d733633SDave Hansen } 2273d733633SDave Hansen 2283d733633SDave Hansen /* 2293d733633SDave Hansen * These per-cpu write counts are not guaranteed to have 2303d733633SDave Hansen * matched increments and decrements on any given cpu. 2313d733633SDave Hansen * A file open()ed for write on one cpu and close()d on 2323d733633SDave Hansen * another cpu will imbalance this count. Make sure it 2333d733633SDave Hansen * does not get too far out of whack. 2343d733633SDave Hansen */ 2353d733633SDave Hansen static void handle_write_count_underflow(struct vfsmount *mnt) 2363d733633SDave Hansen { 2373d733633SDave Hansen if (atomic_read(&mnt->__mnt_writers) >= 2383d733633SDave Hansen MNT_WRITER_UNDERFLOW_LIMIT) 2393d733633SDave Hansen return; 2403d733633SDave Hansen /* 2413d733633SDave Hansen * It isn't necessary to hold all of the locks 2423d733633SDave Hansen * at the same time, but doing it this way makes 2433d733633SDave Hansen * us share a lot more code. 2443d733633SDave Hansen */ 2453d733633SDave Hansen lock_mnt_writers(); 2463d733633SDave Hansen /* 2473d733633SDave Hansen * vfsmount_lock is for mnt_flags. 2483d733633SDave Hansen */ 2493d733633SDave Hansen spin_lock(&vfsmount_lock); 2503d733633SDave Hansen /* 2513d733633SDave Hansen * If coalescing the per-cpu writer counts did not 2523d733633SDave Hansen * get us back to a positive writer count, we have 2533d733633SDave Hansen * a bug. 2543d733633SDave Hansen */ 2553d733633SDave Hansen if ((atomic_read(&mnt->__mnt_writers) < 0) && 2563d733633SDave Hansen !(mnt->mnt_flags & MNT_IMBALANCED_WRITE_COUNT)) { 2573d733633SDave Hansen printk(KERN_DEBUG "leak detected on mount(%p) writers " 2583d733633SDave Hansen "count: %d\n", 2593d733633SDave Hansen mnt, atomic_read(&mnt->__mnt_writers)); 2603d733633SDave Hansen WARN_ON(1); 2613d733633SDave Hansen /* use the flag to keep the dmesg spam down */ 2623d733633SDave Hansen mnt->mnt_flags |= MNT_IMBALANCED_WRITE_COUNT; 2633d733633SDave Hansen } 2643d733633SDave Hansen spin_unlock(&vfsmount_lock); 2653d733633SDave Hansen unlock_mnt_writers(); 2663d733633SDave Hansen } 2673d733633SDave Hansen 2688366025eSDave Hansen /** 2698366025eSDave Hansen * mnt_drop_write - give up write access to a mount 2708366025eSDave Hansen * @mnt: the mount on which to give up write access 2718366025eSDave Hansen * 2728366025eSDave Hansen * Tells the low-level filesystem that we are done 2738366025eSDave Hansen * performing writes to it. Must be matched with 2748366025eSDave Hansen * mnt_want_write() call above. 2758366025eSDave Hansen */ 2768366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt) 2778366025eSDave Hansen { 2783d733633SDave Hansen int must_check_underflow = 0; 2793d733633SDave Hansen struct mnt_writer *cpu_writer; 2803d733633SDave Hansen 2813d733633SDave Hansen cpu_writer = &get_cpu_var(mnt_writers); 2823d733633SDave Hansen spin_lock(&cpu_writer->lock); 2833d733633SDave Hansen 2843d733633SDave Hansen use_cpu_writer_for_mount(cpu_writer, mnt); 2853d733633SDave Hansen if (cpu_writer->count > 0) { 2863d733633SDave Hansen cpu_writer->count--; 2873d733633SDave Hansen } else { 2883d733633SDave Hansen must_check_underflow = 1; 2893d733633SDave Hansen atomic_dec(&mnt->__mnt_writers); 2903d733633SDave Hansen } 2913d733633SDave Hansen 2923d733633SDave Hansen spin_unlock(&cpu_writer->lock); 2933d733633SDave Hansen /* 2943d733633SDave Hansen * Logically, we could call this each time, 2953d733633SDave Hansen * but the __mnt_writers cacheline tends to 2963d733633SDave Hansen * be cold, and makes this expensive. 2973d733633SDave Hansen */ 2983d733633SDave Hansen if (must_check_underflow) 2993d733633SDave Hansen handle_write_count_underflow(mnt); 3003d733633SDave Hansen /* 3013d733633SDave Hansen * This could be done right after the spinlock 3023d733633SDave Hansen * is taken because the spinlock keeps us on 3033d733633SDave Hansen * the cpu, and disables preemption. However, 3043d733633SDave Hansen * putting it here bounds the amount that 3053d733633SDave Hansen * __mnt_writers can underflow. Without it, 3063d733633SDave Hansen * we could theoretically wrap __mnt_writers. 3073d733633SDave Hansen */ 3083d733633SDave Hansen put_cpu_var(mnt_writers); 3098366025eSDave Hansen } 3108366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 3118366025eSDave Hansen 3122e4b7fcdSDave Hansen static int mnt_make_readonly(struct vfsmount *mnt) 3138366025eSDave Hansen { 3143d733633SDave Hansen int ret = 0; 3153d733633SDave Hansen 3163d733633SDave Hansen lock_mnt_writers(); 3173d733633SDave Hansen /* 3183d733633SDave Hansen * With all the locks held, this value is stable 3193d733633SDave Hansen */ 3203d733633SDave Hansen if (atomic_read(&mnt->__mnt_writers) > 0) { 3213d733633SDave Hansen ret = -EBUSY; 3223d733633SDave Hansen goto out; 3238366025eSDave Hansen } 3243d733633SDave Hansen /* 3252e4b7fcdSDave Hansen * nobody can do a successful mnt_want_write() with all 3262e4b7fcdSDave Hansen * of the counts in MNT_DENIED_WRITE and the locks held. 3273d733633SDave Hansen */ 3282e4b7fcdSDave Hansen spin_lock(&vfsmount_lock); 3292e4b7fcdSDave Hansen if (!ret) 3302e4b7fcdSDave Hansen mnt->mnt_flags |= MNT_READONLY; 3312e4b7fcdSDave Hansen spin_unlock(&vfsmount_lock); 3323d733633SDave Hansen out: 3333d733633SDave Hansen unlock_mnt_writers(); 3343d733633SDave Hansen return ret; 3353d733633SDave Hansen } 3368366025eSDave Hansen 3372e4b7fcdSDave Hansen static void __mnt_unmake_readonly(struct vfsmount *mnt) 3382e4b7fcdSDave Hansen { 3392e4b7fcdSDave Hansen spin_lock(&vfsmount_lock); 3402e4b7fcdSDave Hansen mnt->mnt_flags &= ~MNT_READONLY; 3412e4b7fcdSDave Hansen spin_unlock(&vfsmount_lock); 3422e4b7fcdSDave Hansen } 3432e4b7fcdSDave Hansen 344454e2398SDavid Howells int simple_set_mnt(struct vfsmount *mnt, struct super_block *sb) 345454e2398SDavid Howells { 346454e2398SDavid Howells mnt->mnt_sb = sb; 347454e2398SDavid Howells mnt->mnt_root = dget(sb->s_root); 348454e2398SDavid Howells return 0; 349454e2398SDavid Howells } 350454e2398SDavid Howells 351454e2398SDavid Howells EXPORT_SYMBOL(simple_set_mnt); 352454e2398SDavid Howells 3531da177e4SLinus Torvalds void free_vfsmnt(struct vfsmount *mnt) 3541da177e4SLinus Torvalds { 3551da177e4SLinus Torvalds kfree(mnt->mnt_devname); 3561da177e4SLinus Torvalds kmem_cache_free(mnt_cache, mnt); 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /* 360a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 361a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 3621da177e4SLinus Torvalds */ 363a05964f3SRam Pai struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 364a05964f3SRam Pai int dir) 3651da177e4SLinus Torvalds { 3661da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 3671da177e4SLinus Torvalds struct list_head *tmp = head; 3681da177e4SLinus Torvalds struct vfsmount *p, *found = NULL; 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds for (;;) { 371a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 3721da177e4SLinus Torvalds p = NULL; 3731da177e4SLinus Torvalds if (tmp == head) 3741da177e4SLinus Torvalds break; 3751da177e4SLinus Torvalds p = list_entry(tmp, struct vfsmount, mnt_hash); 3761da177e4SLinus Torvalds if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) { 377a05964f3SRam Pai found = p; 3781da177e4SLinus Torvalds break; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds return found; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 384a05964f3SRam Pai /* 385a05964f3SRam Pai * lookup_mnt increments the ref count before returning 386a05964f3SRam Pai * the vfsmount struct. 387a05964f3SRam Pai */ 388a05964f3SRam Pai struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 389a05964f3SRam Pai { 390a05964f3SRam Pai struct vfsmount *child_mnt; 391a05964f3SRam Pai spin_lock(&vfsmount_lock); 392a05964f3SRam Pai if ((child_mnt = __lookup_mnt(mnt, dentry, 1))) 393a05964f3SRam Pai mntget(child_mnt); 394a05964f3SRam Pai spin_unlock(&vfsmount_lock); 395a05964f3SRam Pai return child_mnt; 396a05964f3SRam Pai } 397a05964f3SRam Pai 3981da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt) 3991da177e4SLinus Torvalds { 4006b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4036b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 4045addc5ddSAl Viro { 4055addc5ddSAl Viro if (ns) { 4065addc5ddSAl Viro ns->event = ++event; 4075addc5ddSAl Viro wake_up_interruptible(&ns->poll); 4085addc5ddSAl Viro } 4095addc5ddSAl Viro } 4105addc5ddSAl Viro 4116b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 4125addc5ddSAl Viro { 4135addc5ddSAl Viro if (ns && ns->event != event) { 4145addc5ddSAl Viro ns->event = event; 4155addc5ddSAl Viro wake_up_interruptible(&ns->poll); 4165addc5ddSAl Viro } 4175addc5ddSAl Viro } 4185addc5ddSAl Viro 4191a390689SAl Viro static void detach_mnt(struct vfsmount *mnt, struct path *old_path) 4201da177e4SLinus Torvalds { 4211a390689SAl Viro old_path->dentry = mnt->mnt_mountpoint; 4221a390689SAl Viro old_path->mnt = mnt->mnt_parent; 4231da177e4SLinus Torvalds mnt->mnt_parent = mnt; 4241da177e4SLinus Torvalds mnt->mnt_mountpoint = mnt->mnt_root; 4251da177e4SLinus Torvalds list_del_init(&mnt->mnt_child); 4261da177e4SLinus Torvalds list_del_init(&mnt->mnt_hash); 4271a390689SAl Viro old_path->dentry->d_mounted--; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 430b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry, 431b90fa9aeSRam Pai struct vfsmount *child_mnt) 432b90fa9aeSRam Pai { 433b90fa9aeSRam Pai child_mnt->mnt_parent = mntget(mnt); 434b90fa9aeSRam Pai child_mnt->mnt_mountpoint = dget(dentry); 435b90fa9aeSRam Pai dentry->d_mounted++; 436b90fa9aeSRam Pai } 437b90fa9aeSRam Pai 4381a390689SAl Viro static void attach_mnt(struct vfsmount *mnt, struct path *path) 4391da177e4SLinus Torvalds { 4401a390689SAl Viro mnt_set_mountpoint(path->mnt, path->dentry, mnt); 441b90fa9aeSRam Pai list_add_tail(&mnt->mnt_hash, mount_hashtable + 4421a390689SAl Viro hash(path->mnt, path->dentry)); 4431a390689SAl Viro list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts); 444b90fa9aeSRam Pai } 445b90fa9aeSRam Pai 446b90fa9aeSRam Pai /* 447b90fa9aeSRam Pai * the caller must hold vfsmount_lock 448b90fa9aeSRam Pai */ 449b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt) 450b90fa9aeSRam Pai { 451b90fa9aeSRam Pai struct vfsmount *parent = mnt->mnt_parent; 452b90fa9aeSRam Pai struct vfsmount *m; 453b90fa9aeSRam Pai LIST_HEAD(head); 4546b3286edSKirill Korotaev struct mnt_namespace *n = parent->mnt_ns; 455b90fa9aeSRam Pai 456b90fa9aeSRam Pai BUG_ON(parent == mnt); 457b90fa9aeSRam Pai 458b90fa9aeSRam Pai list_add_tail(&head, &mnt->mnt_list); 459b90fa9aeSRam Pai list_for_each_entry(m, &head, mnt_list) 4606b3286edSKirill Korotaev m->mnt_ns = n; 461b90fa9aeSRam Pai list_splice(&head, n->list.prev); 462b90fa9aeSRam Pai 463b90fa9aeSRam Pai list_add_tail(&mnt->mnt_hash, mount_hashtable + 464b90fa9aeSRam Pai hash(parent, mnt->mnt_mountpoint)); 465b90fa9aeSRam Pai list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 4666b3286edSKirill Korotaev touch_mnt_namespace(n); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root) 4701da177e4SLinus Torvalds { 4711da177e4SLinus Torvalds struct list_head *next = p->mnt_mounts.next; 4721da177e4SLinus Torvalds if (next == &p->mnt_mounts) { 4731da177e4SLinus Torvalds while (1) { 4741da177e4SLinus Torvalds if (p == root) 4751da177e4SLinus Torvalds return NULL; 4761da177e4SLinus Torvalds next = p->mnt_child.next; 4771da177e4SLinus Torvalds if (next != &p->mnt_parent->mnt_mounts) 4781da177e4SLinus Torvalds break; 4791da177e4SLinus Torvalds p = p->mnt_parent; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds return list_entry(next, struct vfsmount, mnt_child); 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 4859676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p) 4869676f0c6SRam Pai { 4879676f0c6SRam Pai struct list_head *prev = p->mnt_mounts.prev; 4889676f0c6SRam Pai while (prev != &p->mnt_mounts) { 4899676f0c6SRam Pai p = list_entry(prev, struct vfsmount, mnt_child); 4909676f0c6SRam Pai prev = p->mnt_mounts.prev; 4919676f0c6SRam Pai } 4929676f0c6SRam Pai return p; 4939676f0c6SRam Pai } 4949676f0c6SRam Pai 49536341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root, 49636341f64SRam Pai int flag) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds struct super_block *sb = old->mnt_sb; 4991da177e4SLinus Torvalds struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds if (mnt) { 5021da177e4SLinus Torvalds mnt->mnt_flags = old->mnt_flags; 5031da177e4SLinus Torvalds atomic_inc(&sb->s_active); 5041da177e4SLinus Torvalds mnt->mnt_sb = sb; 5051da177e4SLinus Torvalds mnt->mnt_root = dget(root); 5061da177e4SLinus Torvalds mnt->mnt_mountpoint = mnt->mnt_root; 5071da177e4SLinus Torvalds mnt->mnt_parent = mnt; 508b90fa9aeSRam Pai 5095afe0022SRam Pai if (flag & CL_SLAVE) { 5105afe0022SRam Pai list_add(&mnt->mnt_slave, &old->mnt_slave_list); 5115afe0022SRam Pai mnt->mnt_master = old; 5125afe0022SRam Pai CLEAR_MNT_SHARED(mnt); 5138aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 514b90fa9aeSRam Pai if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old)) 515b90fa9aeSRam Pai list_add(&mnt->mnt_share, &old->mnt_share); 5165afe0022SRam Pai if (IS_MNT_SLAVE(old)) 5175afe0022SRam Pai list_add(&mnt->mnt_slave, &old->mnt_slave); 5185afe0022SRam Pai mnt->mnt_master = old->mnt_master; 5195afe0022SRam Pai } 520b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 521b90fa9aeSRam Pai set_mnt_shared(mnt); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 5241da177e4SLinus Torvalds * as the original if that was on one */ 52536341f64SRam Pai if (flag & CL_EXPIRE) { 52655e700b9SMiklos Szeredi if (!list_empty(&old->mnt_expire)) 52755e700b9SMiklos Szeredi list_add(&mnt->mnt_expire, &old->mnt_expire); 5281da177e4SLinus Torvalds } 52936341f64SRam Pai } 5301da177e4SLinus Torvalds return mnt; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5337b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt) 5341da177e4SLinus Torvalds { 5353d733633SDave Hansen int cpu; 5361da177e4SLinus Torvalds struct super_block *sb = mnt->mnt_sb; 5373d733633SDave Hansen /* 5383d733633SDave Hansen * We don't have to hold all of the locks at the 5393d733633SDave Hansen * same time here because we know that we're the 5403d733633SDave Hansen * last reference to mnt and that no new writers 5413d733633SDave Hansen * can come in. 5423d733633SDave Hansen */ 5433d733633SDave Hansen for_each_possible_cpu(cpu) { 5443d733633SDave Hansen struct mnt_writer *cpu_writer = &per_cpu(mnt_writers, cpu); 5453d733633SDave Hansen if (cpu_writer->mnt != mnt) 5463d733633SDave Hansen continue; 5473d733633SDave Hansen spin_lock(&cpu_writer->lock); 5483d733633SDave Hansen atomic_add(cpu_writer->count, &mnt->__mnt_writers); 5493d733633SDave Hansen cpu_writer->count = 0; 5503d733633SDave Hansen /* 5513d733633SDave Hansen * Might as well do this so that no one 5523d733633SDave Hansen * ever sees the pointer and expects 5533d733633SDave Hansen * it to be valid. 5543d733633SDave Hansen */ 5553d733633SDave Hansen cpu_writer->mnt = NULL; 5563d733633SDave Hansen spin_unlock(&cpu_writer->lock); 5573d733633SDave Hansen } 5583d733633SDave Hansen /* 5593d733633SDave Hansen * This probably indicates that somebody messed 5603d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 5613d733633SDave Hansen * happens, the filesystem was probably unable 5623d733633SDave Hansen * to make r/w->r/o transitions. 5633d733633SDave Hansen */ 5643d733633SDave Hansen WARN_ON(atomic_read(&mnt->__mnt_writers)); 5651da177e4SLinus Torvalds dput(mnt->mnt_root); 5661da177e4SLinus Torvalds free_vfsmnt(mnt); 5671da177e4SLinus Torvalds deactivate_super(sb); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5707b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt) 5717b7b1aceSAl Viro { 5727b7b1aceSAl Viro repeat: 5737b7b1aceSAl Viro if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) { 5747b7b1aceSAl Viro if (likely(!mnt->mnt_pinned)) { 5757b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 5767b7b1aceSAl Viro __mntput(mnt); 5777b7b1aceSAl Viro return; 5787b7b1aceSAl Viro } 5797b7b1aceSAl Viro atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count); 5807b7b1aceSAl Viro mnt->mnt_pinned = 0; 5817b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 5827b7b1aceSAl Viro acct_auto_close_mnt(mnt); 5837b7b1aceSAl Viro security_sb_umount_close(mnt); 5847b7b1aceSAl Viro goto repeat; 5857b7b1aceSAl Viro } 5867b7b1aceSAl Viro } 5877b7b1aceSAl Viro 5887b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire); 5897b7b1aceSAl Viro 5907b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 5917b7b1aceSAl Viro { 5927b7b1aceSAl Viro spin_lock(&vfsmount_lock); 5937b7b1aceSAl Viro mnt->mnt_pinned++; 5947b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 5957b7b1aceSAl Viro } 5967b7b1aceSAl Viro 5977b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 5987b7b1aceSAl Viro 5997b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt) 6007b7b1aceSAl Viro { 6017b7b1aceSAl Viro spin_lock(&vfsmount_lock); 6027b7b1aceSAl Viro if (mnt->mnt_pinned) { 6037b7b1aceSAl Viro atomic_inc(&mnt->mnt_count); 6047b7b1aceSAl Viro mnt->mnt_pinned--; 6057b7b1aceSAl Viro } 6067b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 6077b7b1aceSAl Viro } 6087b7b1aceSAl Viro 6097b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 6101da177e4SLinus Torvalds 611b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 612b3b304a2SMiklos Szeredi { 613b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 614b3b304a2SMiklos Szeredi } 615b3b304a2SMiklos Szeredi 616b3b304a2SMiklos Szeredi /* 617b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 618b3b304a2SMiklos Szeredi * implement more complex mount option showing. 619b3b304a2SMiklos Szeredi * 620b3b304a2SMiklos Szeredi * See also save_mount_options(). 621b3b304a2SMiklos Szeredi */ 622b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt) 623b3b304a2SMiklos Szeredi { 624b3b304a2SMiklos Szeredi const char *options = mnt->mnt_sb->s_options; 625b3b304a2SMiklos Szeredi 626b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 627b3b304a2SMiklos Szeredi seq_putc(m, ','); 628b3b304a2SMiklos Szeredi mangle(m, options); 629b3b304a2SMiklos Szeredi } 630b3b304a2SMiklos Szeredi 631b3b304a2SMiklos Szeredi return 0; 632b3b304a2SMiklos Szeredi } 633b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 634b3b304a2SMiklos Szeredi 635b3b304a2SMiklos Szeredi /* 636b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 637b3b304a2SMiklos Szeredi * called from the fill_super() callback. 638b3b304a2SMiklos Szeredi * 639b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 640b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 641b3b304a2SMiklos Szeredi * remount fails. 642b3b304a2SMiklos Szeredi * 643b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 644b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 645b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 646b3b304a2SMiklos Szeredi * any more. 647b3b304a2SMiklos Szeredi */ 648b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 649b3b304a2SMiklos Szeredi { 650b3b304a2SMiklos Szeredi kfree(sb->s_options); 651b3b304a2SMiklos Szeredi sb->s_options = kstrdup(options, GFP_KERNEL); 652b3b304a2SMiklos Szeredi } 653b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 654b3b304a2SMiklos Szeredi 6551da177e4SLinus Torvalds /* iterator */ 6561da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 6571da177e4SLinus Torvalds { 6586b3286edSKirill Korotaev struct mnt_namespace *n = m->private; 6591da177e4SLinus Torvalds 660390c6843SRam Pai down_read(&namespace_sem); 661b0765fb8SPavel Emelianov return seq_list_start(&n->list, *pos); 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 6651da177e4SLinus Torvalds { 6666b3286edSKirill Korotaev struct mnt_namespace *n = m->private; 667b0765fb8SPavel Emelianov 668b0765fb8SPavel Emelianov return seq_list_next(v, &n->list, pos); 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 6721da177e4SLinus Torvalds { 673390c6843SRam Pai up_read(&namespace_sem); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds static int show_vfsmnt(struct seq_file *m, void *v) 6771da177e4SLinus Torvalds { 678b0765fb8SPavel Emelianov struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); 6791da177e4SLinus Torvalds int err = 0; 6801da177e4SLinus Torvalds static struct proc_fs_info { 6811da177e4SLinus Torvalds int flag; 6821da177e4SLinus Torvalds char *str; 6831da177e4SLinus Torvalds } fs_info[] = { 6841da177e4SLinus Torvalds { MS_SYNCHRONOUS, ",sync" }, 6851da177e4SLinus Torvalds { MS_DIRSYNC, ",dirsync" }, 6861da177e4SLinus Torvalds { MS_MANDLOCK, ",mand" }, 6871da177e4SLinus Torvalds { 0, NULL } 6881da177e4SLinus Torvalds }; 6891da177e4SLinus Torvalds static struct proc_fs_info mnt_info[] = { 6901da177e4SLinus Torvalds { MNT_NOSUID, ",nosuid" }, 6911da177e4SLinus Torvalds { MNT_NODEV, ",nodev" }, 6921da177e4SLinus Torvalds { MNT_NOEXEC, ",noexec" }, 693fc33a7bbSChristoph Hellwig { MNT_NOATIME, ",noatime" }, 694fc33a7bbSChristoph Hellwig { MNT_NODIRATIME, ",nodiratime" }, 69547ae32d6SValerie Henson { MNT_RELATIME, ",relatime" }, 6961da177e4SLinus Torvalds { 0, NULL } 6971da177e4SLinus Torvalds }; 6981da177e4SLinus Torvalds struct proc_fs_info *fs_infop; 699c32c2f63SJan Blunck struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none"); 7021da177e4SLinus Torvalds seq_putc(m, ' '); 703c32c2f63SJan Blunck seq_path(m, &mnt_path, " \t\n\\"); 7041da177e4SLinus Torvalds seq_putc(m, ' '); 7051da177e4SLinus Torvalds mangle(m, mnt->mnt_sb->s_type->name); 70679c0b2dfSMiklos Szeredi if (mnt->mnt_sb->s_subtype && mnt->mnt_sb->s_subtype[0]) { 70779c0b2dfSMiklos Szeredi seq_putc(m, '.'); 70879c0b2dfSMiklos Szeredi mangle(m, mnt->mnt_sb->s_subtype); 70979c0b2dfSMiklos Szeredi } 7102e4b7fcdSDave Hansen seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw"); 7111da177e4SLinus Torvalds for (fs_infop = fs_info; fs_infop->flag; fs_infop++) { 7121da177e4SLinus Torvalds if (mnt->mnt_sb->s_flags & fs_infop->flag) 7131da177e4SLinus Torvalds seq_puts(m, fs_infop->str); 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) { 7161da177e4SLinus Torvalds if (mnt->mnt_flags & fs_infop->flag) 7171da177e4SLinus Torvalds seq_puts(m, fs_infop->str); 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds if (mnt->mnt_sb->s_op->show_options) 7201da177e4SLinus Torvalds err = mnt->mnt_sb->s_op->show_options(m, mnt); 7211da177e4SLinus Torvalds seq_puts(m, " 0 0\n"); 7221da177e4SLinus Torvalds return err; 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds struct seq_operations mounts_op = { 7261da177e4SLinus Torvalds .start = m_start, 7271da177e4SLinus Torvalds .next = m_next, 7281da177e4SLinus Torvalds .stop = m_stop, 7291da177e4SLinus Torvalds .show = show_vfsmnt 7301da177e4SLinus Torvalds }; 7311da177e4SLinus Torvalds 732b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v) 733b4629fe2SChuck Lever { 734b0765fb8SPavel Emelianov struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); 735c32c2f63SJan Blunck struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; 736b4629fe2SChuck Lever int err = 0; 737b4629fe2SChuck Lever 738b4629fe2SChuck Lever /* device */ 739b4629fe2SChuck Lever if (mnt->mnt_devname) { 740b4629fe2SChuck Lever seq_puts(m, "device "); 741b4629fe2SChuck Lever mangle(m, mnt->mnt_devname); 742b4629fe2SChuck Lever } else 743b4629fe2SChuck Lever seq_puts(m, "no device"); 744b4629fe2SChuck Lever 745b4629fe2SChuck Lever /* mount point */ 746b4629fe2SChuck Lever seq_puts(m, " mounted on "); 747c32c2f63SJan Blunck seq_path(m, &mnt_path, " \t\n\\"); 748b4629fe2SChuck Lever seq_putc(m, ' '); 749b4629fe2SChuck Lever 750b4629fe2SChuck Lever /* file system type */ 751b4629fe2SChuck Lever seq_puts(m, "with fstype "); 752b4629fe2SChuck Lever mangle(m, mnt->mnt_sb->s_type->name); 753b4629fe2SChuck Lever 754b4629fe2SChuck Lever /* optional statistics */ 755b4629fe2SChuck Lever if (mnt->mnt_sb->s_op->show_stats) { 756b4629fe2SChuck Lever seq_putc(m, ' '); 757b4629fe2SChuck Lever err = mnt->mnt_sb->s_op->show_stats(m, mnt); 758b4629fe2SChuck Lever } 759b4629fe2SChuck Lever 760b4629fe2SChuck Lever seq_putc(m, '\n'); 761b4629fe2SChuck Lever return err; 762b4629fe2SChuck Lever } 763b4629fe2SChuck Lever 764b4629fe2SChuck Lever struct seq_operations mountstats_op = { 765b4629fe2SChuck Lever .start = m_start, 766b4629fe2SChuck Lever .next = m_next, 767b4629fe2SChuck Lever .stop = m_stop, 768b4629fe2SChuck Lever .show = show_vfsstat, 769b4629fe2SChuck Lever }; 770b4629fe2SChuck Lever 7711da177e4SLinus Torvalds /** 7721da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 7731da177e4SLinus Torvalds * @mnt: root of mount tree 7741da177e4SLinus Torvalds * 7751da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 7761da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 7771da177e4SLinus Torvalds * busy. 7781da177e4SLinus Torvalds */ 7791da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt) 7801da177e4SLinus Torvalds { 78136341f64SRam Pai int actual_refs = 0; 78236341f64SRam Pai int minimum_refs = 0; 78336341f64SRam Pai struct vfsmount *p; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 78636341f64SRam Pai for (p = mnt; p; p = next_mnt(p, mnt)) { 7871da177e4SLinus Torvalds actual_refs += atomic_read(&p->mnt_count); 7881da177e4SLinus Torvalds minimum_refs += 2; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds if (actual_refs > minimum_refs) 7931da177e4SLinus Torvalds return 0; 794e3474a8eSIan Kent 795e3474a8eSIan Kent return 1; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds /** 8011da177e4SLinus Torvalds * may_umount - check if a mount point is busy 8021da177e4SLinus Torvalds * @mnt: root of mount 8031da177e4SLinus Torvalds * 8041da177e4SLinus Torvalds * This is called to check if a mount point has any 8051da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 8061da177e4SLinus Torvalds * mount has sub mounts this will return busy 8071da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 8081da177e4SLinus Torvalds * 8091da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 8101da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 8111da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 8121da177e4SLinus Torvalds */ 8131da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 8141da177e4SLinus Torvalds { 815e3474a8eSIan Kent int ret = 1; 816a05964f3SRam Pai spin_lock(&vfsmount_lock); 817a05964f3SRam Pai if (propagate_mount_busy(mnt, 2)) 818e3474a8eSIan Kent ret = 0; 819a05964f3SRam Pai spin_unlock(&vfsmount_lock); 820a05964f3SRam Pai return ret; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 8241da177e4SLinus Torvalds 825b90fa9aeSRam Pai void release_mounts(struct list_head *head) 8261da177e4SLinus Torvalds { 82770fbcdf4SRam Pai struct vfsmount *mnt; 82870fbcdf4SRam Pai while (!list_empty(head)) { 829b5e61818SPavel Emelianov mnt = list_first_entry(head, struct vfsmount, mnt_hash); 83070fbcdf4SRam Pai list_del_init(&mnt->mnt_hash); 83170fbcdf4SRam Pai if (mnt->mnt_parent != mnt) { 83270fbcdf4SRam Pai struct dentry *dentry; 83370fbcdf4SRam Pai struct vfsmount *m; 83470fbcdf4SRam Pai spin_lock(&vfsmount_lock); 83570fbcdf4SRam Pai dentry = mnt->mnt_mountpoint; 83670fbcdf4SRam Pai m = mnt->mnt_parent; 83770fbcdf4SRam Pai mnt->mnt_mountpoint = mnt->mnt_root; 83870fbcdf4SRam Pai mnt->mnt_parent = mnt; 8397c4b93d8SAl Viro m->mnt_ghosts--; 8401da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 84170fbcdf4SRam Pai dput(dentry); 84270fbcdf4SRam Pai mntput(m); 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds mntput(mnt); 84570fbcdf4SRam Pai } 84670fbcdf4SRam Pai } 84770fbcdf4SRam Pai 848a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill) 84970fbcdf4SRam Pai { 85070fbcdf4SRam Pai struct vfsmount *p; 85170fbcdf4SRam Pai 8521bfba4e8SAkinobu Mita for (p = mnt; p; p = next_mnt(p, mnt)) 8531bfba4e8SAkinobu Mita list_move(&p->mnt_hash, kill); 85470fbcdf4SRam Pai 855a05964f3SRam Pai if (propagate) 856a05964f3SRam Pai propagate_umount(kill); 857a05964f3SRam Pai 85870fbcdf4SRam Pai list_for_each_entry(p, kill, mnt_hash) { 85970fbcdf4SRam Pai list_del_init(&p->mnt_expire); 86070fbcdf4SRam Pai list_del_init(&p->mnt_list); 8616b3286edSKirill Korotaev __touch_mnt_namespace(p->mnt_ns); 8626b3286edSKirill Korotaev p->mnt_ns = NULL; 86370fbcdf4SRam Pai list_del_init(&p->mnt_child); 8647c4b93d8SAl Viro if (p->mnt_parent != p) { 8657c4b93d8SAl Viro p->mnt_parent->mnt_ghosts++; 866f30ac319SAl Viro p->mnt_mountpoint->d_mounted--; 8677c4b93d8SAl Viro } 868a05964f3SRam Pai change_mnt_propagation(p, MS_PRIVATE); 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 872c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts); 873c35038beSAl Viro 8741da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags) 8751da177e4SLinus Torvalds { 8761da177e4SLinus Torvalds struct super_block *sb = mnt->mnt_sb; 8771da177e4SLinus Torvalds int retval; 87870fbcdf4SRam Pai LIST_HEAD(umount_list); 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds retval = security_sb_umount(mnt, flags); 8811da177e4SLinus Torvalds if (retval) 8821da177e4SLinus Torvalds return retval; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds /* 8851da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 8861da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 8871da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 8881da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 8891da177e4SLinus Torvalds */ 8901da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 8916ac08c39SJan Blunck if (mnt == current->fs->root.mnt || 8921da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 8931da177e4SLinus Torvalds return -EINVAL; 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds if (atomic_read(&mnt->mnt_count) != 2) 8961da177e4SLinus Torvalds return -EBUSY; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds if (!xchg(&mnt->mnt_expiry_mark, 1)) 8991da177e4SLinus Torvalds return -EAGAIN; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* 9031da177e4SLinus Torvalds * If we may have to abort operations to get out of this 9041da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 9051da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 9061da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 9071da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 9081da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 9091da177e4SLinus Torvalds * about for the moment. 9101da177e4SLinus Torvalds */ 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds lock_kernel(); 9138b512d9aSTrond Myklebust if (sb->s_op->umount_begin) 9148b512d9aSTrond Myklebust sb->s_op->umount_begin(mnt, flags); 9151da177e4SLinus Torvalds unlock_kernel(); 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds /* 9181da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 9191da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 9201da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 9211da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 9221da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 9231da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 9241da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 9251da177e4SLinus Torvalds */ 9266ac08c39SJan Blunck if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 9271da177e4SLinus Torvalds /* 9281da177e4SLinus Torvalds * Special case for "unmounting" root ... 9291da177e4SLinus Torvalds * we just try to remount it readonly. 9301da177e4SLinus Torvalds */ 9311da177e4SLinus Torvalds down_write(&sb->s_umount); 9321da177e4SLinus Torvalds if (!(sb->s_flags & MS_RDONLY)) { 9331da177e4SLinus Torvalds lock_kernel(); 9341da177e4SLinus Torvalds DQUOT_OFF(sb); 9351da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 9361da177e4SLinus Torvalds unlock_kernel(); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds up_write(&sb->s_umount); 9391da177e4SLinus Torvalds return retval; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 942390c6843SRam Pai down_write(&namespace_sem); 9431da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 9445addc5ddSAl Viro event++; 9451da177e4SLinus Torvalds 946c35038beSAl Viro if (!(flags & MNT_DETACH)) 947c35038beSAl Viro shrink_submounts(mnt, &umount_list); 948c35038beSAl Viro 9491da177e4SLinus Torvalds retval = -EBUSY; 950a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 9511da177e4SLinus Torvalds if (!list_empty(&mnt->mnt_list)) 952a05964f3SRam Pai umount_tree(mnt, 1, &umount_list); 9531da177e4SLinus Torvalds retval = 0; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 9561da177e4SLinus Torvalds if (retval) 9571da177e4SLinus Torvalds security_sb_umount_busy(mnt); 958390c6843SRam Pai up_write(&namespace_sem); 95970fbcdf4SRam Pai release_mounts(&umount_list); 9601da177e4SLinus Torvalds return retval; 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds /* 9641da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 9651da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 9661da177e4SLinus Torvalds * 9671da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 9681da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 9691da177e4SLinus Torvalds */ 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds asmlinkage long sys_umount(char __user * name, int flags) 9721da177e4SLinus Torvalds { 9731da177e4SLinus Torvalds struct nameidata nd; 9741da177e4SLinus Torvalds int retval; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds retval = __user_walk(name, LOOKUP_FOLLOW, &nd); 9771da177e4SLinus Torvalds if (retval) 9781da177e4SLinus Torvalds goto out; 9791da177e4SLinus Torvalds retval = -EINVAL; 9804ac91378SJan Blunck if (nd.path.dentry != nd.path.mnt->mnt_root) 9811da177e4SLinus Torvalds goto dput_and_out; 9824ac91378SJan Blunck if (!check_mnt(nd.path.mnt)) 9831da177e4SLinus Torvalds goto dput_and_out; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds retval = -EPERM; 9861da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 9871da177e4SLinus Torvalds goto dput_and_out; 9881da177e4SLinus Torvalds 9894ac91378SJan Blunck retval = do_umount(nd.path.mnt, flags); 9901da177e4SLinus Torvalds dput_and_out: 991429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 9924ac91378SJan Blunck dput(nd.path.dentry); 9934ac91378SJan Blunck mntput_no_expire(nd.path.mnt); 9941da177e4SLinus Torvalds out: 9951da177e4SLinus Torvalds return retval; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds /* 10011da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 10021da177e4SLinus Torvalds */ 10031da177e4SLinus Torvalds asmlinkage long sys_oldumount(char __user * name) 10041da177e4SLinus Torvalds { 10051da177e4SLinus Torvalds return sys_umount(name, 0); 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds #endif 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds static int mount_is_safe(struct nameidata *nd) 10111da177e4SLinus Torvalds { 10121da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 10131da177e4SLinus Torvalds return 0; 10141da177e4SLinus Torvalds return -EPERM; 10151da177e4SLinus Torvalds #ifdef notyet 10164ac91378SJan Blunck if (S_ISLNK(nd->path.dentry->d_inode->i_mode)) 10171da177e4SLinus Torvalds return -EPERM; 10184ac91378SJan Blunck if (nd->path.dentry->d_inode->i_mode & S_ISVTX) { 10194ac91378SJan Blunck if (current->uid != nd->path.dentry->d_inode->i_uid) 10201da177e4SLinus Torvalds return -EPERM; 10211da177e4SLinus Torvalds } 1022e4543eddSChristoph Hellwig if (vfs_permission(nd, MAY_WRITE)) 10231da177e4SLinus Torvalds return -EPERM; 10241da177e4SLinus Torvalds return 0; 10251da177e4SLinus Torvalds #endif 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 1028b58fed8bSRam Pai static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry) 10291da177e4SLinus Torvalds { 10301da177e4SLinus Torvalds while (1) { 10311da177e4SLinus Torvalds if (d == dentry) 10321da177e4SLinus Torvalds return 1; 10331da177e4SLinus Torvalds if (d == NULL || d == d->d_parent) 10341da177e4SLinus Torvalds return 0; 10351da177e4SLinus Torvalds d = d->d_parent; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 1039b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry, 104036341f64SRam Pai int flag) 10411da177e4SLinus Torvalds { 10421da177e4SLinus Torvalds struct vfsmount *res, *p, *q, *r, *s; 10431a390689SAl Viro struct path path; 10441da177e4SLinus Torvalds 10459676f0c6SRam Pai if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 10469676f0c6SRam Pai return NULL; 10479676f0c6SRam Pai 104836341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 10491da177e4SLinus Torvalds if (!q) 10501da177e4SLinus Torvalds goto Enomem; 10511da177e4SLinus Torvalds q->mnt_mountpoint = mnt->mnt_mountpoint; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds p = mnt; 1054fdadd65fSDomen Puncer list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 10551da177e4SLinus Torvalds if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry)) 10561da177e4SLinus Torvalds continue; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds for (s = r; s; s = next_mnt(s, r)) { 10599676f0c6SRam Pai if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 10609676f0c6SRam Pai s = skip_mnt_tree(s); 10619676f0c6SRam Pai continue; 10629676f0c6SRam Pai } 10631da177e4SLinus Torvalds while (p != s->mnt_parent) { 10641da177e4SLinus Torvalds p = p->mnt_parent; 10651da177e4SLinus Torvalds q = q->mnt_parent; 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds p = s; 10681a390689SAl Viro path.mnt = q; 10691a390689SAl Viro path.dentry = p->mnt_mountpoint; 107036341f64SRam Pai q = clone_mnt(p, p->mnt_root, flag); 10711da177e4SLinus Torvalds if (!q) 10721da177e4SLinus Torvalds goto Enomem; 10731da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 10741da177e4SLinus Torvalds list_add_tail(&q->mnt_list, &res->mnt_list); 10751a390689SAl Viro attach_mnt(q, &path); 10761da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds return res; 10801da177e4SLinus Torvalds Enomem: 10811da177e4SLinus Torvalds if (res) { 108270fbcdf4SRam Pai LIST_HEAD(umount_list); 10831da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 1084a05964f3SRam Pai umount_tree(res, 0, &umount_list); 10851da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 108670fbcdf4SRam Pai release_mounts(&umount_list); 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds return NULL; 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 10918aec0809SAl Viro struct vfsmount *collect_mounts(struct vfsmount *mnt, struct dentry *dentry) 10928aec0809SAl Viro { 10938aec0809SAl Viro struct vfsmount *tree; 10941a60a280SAl Viro down_write(&namespace_sem); 10958aec0809SAl Viro tree = copy_tree(mnt, dentry, CL_COPY_ALL | CL_PRIVATE); 10961a60a280SAl Viro up_write(&namespace_sem); 10978aec0809SAl Viro return tree; 10988aec0809SAl Viro } 10998aec0809SAl Viro 11008aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 11018aec0809SAl Viro { 11028aec0809SAl Viro LIST_HEAD(umount_list); 11031a60a280SAl Viro down_write(&namespace_sem); 11048aec0809SAl Viro spin_lock(&vfsmount_lock); 11058aec0809SAl Viro umount_tree(mnt, 0, &umount_list); 11068aec0809SAl Viro spin_unlock(&vfsmount_lock); 11071a60a280SAl Viro up_write(&namespace_sem); 11088aec0809SAl Viro release_mounts(&umount_list); 11098aec0809SAl Viro } 11108aec0809SAl Viro 1111b90fa9aeSRam Pai /* 1112b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1113b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 111421444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 111521444403SRam Pai * store the parent mount and mountpoint dentry. 111621444403SRam Pai * (done when source_mnt is moved) 1117b90fa9aeSRam Pai * 1118b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1119b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 11209676f0c6SRam Pai * --------------------------------------------------------------------------- 1121b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 11229676f0c6SRam Pai * |************************************************************************** 11239676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 11249676f0c6SRam Pai * | dest | | | | | 11259676f0c6SRam Pai * | | | | | | | 11269676f0c6SRam Pai * | v | | | | | 11279676f0c6SRam Pai * |************************************************************************** 11289676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 11295afe0022SRam Pai * | | | | | | 11309676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 11319676f0c6SRam Pai * *************************************************************************** 1132b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1133b90fa9aeSRam Pai * destination mount. 1134b90fa9aeSRam Pai * 1135b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1136b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1137b90fa9aeSRam Pai * the peer group of the source mount. 1138b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1139b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1140b90fa9aeSRam Pai * mount. 11415afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 11425afe0022SRam Pai * of the destination mount and the cloned mount is made slave 11435afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 11445afe0022SRam Pai * is marked as 'shared and slave'. 11455afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 11465afe0022SRam Pai * source mount. 11475afe0022SRam Pai * 11489676f0c6SRam Pai * --------------------------------------------------------------------------- 114921444403SRam Pai * | MOVE MOUNT OPERATION | 11509676f0c6SRam Pai * |************************************************************************** 11519676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 11529676f0c6SRam Pai * | dest | | | | | 11539676f0c6SRam Pai * | | | | | | | 11549676f0c6SRam Pai * | v | | | | | 11559676f0c6SRam Pai * |************************************************************************** 11569676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 11575afe0022SRam Pai * | | | | | | 11589676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 11599676f0c6SRam Pai * *************************************************************************** 11605afe0022SRam Pai * 11615afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 11625afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 116321444403SRam Pai * (+*) the mount is moved to the destination. 11645afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 11655afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 11665afe0022SRam Pai * the mount is marked as 'shared and slave'. 11675afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1168b90fa9aeSRam Pai * 1169b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1170b90fa9aeSRam Pai * applied to each mount in the tree. 1171b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1172b90fa9aeSRam Pai * in allocations. 1173b90fa9aeSRam Pai */ 1174b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt, 11751a390689SAl Viro struct path *path, struct path *parent_path) 1176b90fa9aeSRam Pai { 1177b90fa9aeSRam Pai LIST_HEAD(tree_list); 11781a390689SAl Viro struct vfsmount *dest_mnt = path->mnt; 11791a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1180b90fa9aeSRam Pai struct vfsmount *child, *p; 1181b90fa9aeSRam Pai 1182b90fa9aeSRam Pai if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list)) 1183b90fa9aeSRam Pai return -EINVAL; 1184b90fa9aeSRam Pai 1185b90fa9aeSRam Pai if (IS_MNT_SHARED(dest_mnt)) { 1186b90fa9aeSRam Pai for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 1187b90fa9aeSRam Pai set_mnt_shared(p); 1188b90fa9aeSRam Pai } 1189b90fa9aeSRam Pai 1190b90fa9aeSRam Pai spin_lock(&vfsmount_lock); 11911a390689SAl Viro if (parent_path) { 11921a390689SAl Viro detach_mnt(source_mnt, parent_path); 11931a390689SAl Viro attach_mnt(source_mnt, path); 11946b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 119521444403SRam Pai } else { 1196b90fa9aeSRam Pai mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 1197b90fa9aeSRam Pai commit_tree(source_mnt); 119821444403SRam Pai } 1199b90fa9aeSRam Pai 1200b90fa9aeSRam Pai list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 1201b90fa9aeSRam Pai list_del_init(&child->mnt_hash); 1202b90fa9aeSRam Pai commit_tree(child); 1203b90fa9aeSRam Pai } 1204b90fa9aeSRam Pai spin_unlock(&vfsmount_lock); 1205b90fa9aeSRam Pai return 0; 1206b90fa9aeSRam Pai } 1207b90fa9aeSRam Pai 1208*8c3ee42eSAl Viro static int graft_tree(struct vfsmount *mnt, struct path *path) 12091da177e4SLinus Torvalds { 12101da177e4SLinus Torvalds int err; 12111da177e4SLinus Torvalds if (mnt->mnt_sb->s_flags & MS_NOUSER) 12121da177e4SLinus Torvalds return -EINVAL; 12131da177e4SLinus Torvalds 1214*8c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 12151da177e4SLinus Torvalds S_ISDIR(mnt->mnt_root->d_inode->i_mode)) 12161da177e4SLinus Torvalds return -ENOTDIR; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds err = -ENOENT; 1219*8c3ee42eSAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1220*8c3ee42eSAl Viro if (IS_DEADDIR(path->dentry->d_inode)) 12211da177e4SLinus Torvalds goto out_unlock; 12221da177e4SLinus Torvalds 1223*8c3ee42eSAl Viro err = security_sb_check_sb(mnt, path); 12241da177e4SLinus Torvalds if (err) 12251da177e4SLinus Torvalds goto out_unlock; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds err = -ENOENT; 1228*8c3ee42eSAl Viro if (IS_ROOT(path->dentry) || !d_unhashed(path->dentry)) 1229*8c3ee42eSAl Viro err = attach_recursive_mnt(mnt, path, NULL); 12301da177e4SLinus Torvalds out_unlock: 1231*8c3ee42eSAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 12321da177e4SLinus Torvalds if (!err) 1233*8c3ee42eSAl Viro security_sb_post_addmount(mnt, path); 12341da177e4SLinus Torvalds return err; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds /* 123807b20889SRam Pai * recursively change the type of the mountpoint. 12392dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 124007b20889SRam Pai */ 12412dafe1c4SEric Sandeen static noinline int do_change_type(struct nameidata *nd, int flag) 124207b20889SRam Pai { 12434ac91378SJan Blunck struct vfsmount *m, *mnt = nd->path.mnt; 124407b20889SRam Pai int recurse = flag & MS_REC; 124507b20889SRam Pai int type = flag & ~MS_REC; 124607b20889SRam Pai 1247ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 1248ee6f9582SMiklos Szeredi return -EPERM; 1249ee6f9582SMiklos Szeredi 12504ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) 125107b20889SRam Pai return -EINVAL; 125207b20889SRam Pai 125307b20889SRam Pai down_write(&namespace_sem); 125407b20889SRam Pai spin_lock(&vfsmount_lock); 125507b20889SRam Pai for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 125607b20889SRam Pai change_mnt_propagation(m, type); 125707b20889SRam Pai spin_unlock(&vfsmount_lock); 125807b20889SRam Pai up_write(&namespace_sem); 125907b20889SRam Pai return 0; 126007b20889SRam Pai } 126107b20889SRam Pai 126207b20889SRam Pai /* 12631da177e4SLinus Torvalds * do loopback mount. 12642dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 12651da177e4SLinus Torvalds */ 12662dafe1c4SEric Sandeen static noinline int do_loopback(struct nameidata *nd, char *old_name, 12672dafe1c4SEric Sandeen int recurse) 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds struct nameidata old_nd; 12701da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 12711da177e4SLinus Torvalds int err = mount_is_safe(nd); 12721da177e4SLinus Torvalds if (err) 12731da177e4SLinus Torvalds return err; 12741da177e4SLinus Torvalds if (!old_name || !*old_name) 12751da177e4SLinus Torvalds return -EINVAL; 12761da177e4SLinus Torvalds err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd); 12771da177e4SLinus Torvalds if (err) 12781da177e4SLinus Torvalds return err; 12791da177e4SLinus Torvalds 1280390c6843SRam Pai down_write(&namespace_sem); 12811da177e4SLinus Torvalds err = -EINVAL; 12824ac91378SJan Blunck if (IS_MNT_UNBINDABLE(old_nd.path.mnt)) 12839676f0c6SRam Pai goto out; 12849676f0c6SRam Pai 12854ac91378SJan Blunck if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt)) 1286ccd48bc7SAl Viro goto out; 1287ccd48bc7SAl Viro 12881da177e4SLinus Torvalds err = -ENOMEM; 12891da177e4SLinus Torvalds if (recurse) 12904ac91378SJan Blunck mnt = copy_tree(old_nd.path.mnt, old_nd.path.dentry, 0); 12911da177e4SLinus Torvalds else 12924ac91378SJan Blunck mnt = clone_mnt(old_nd.path.mnt, old_nd.path.dentry, 0); 12931da177e4SLinus Torvalds 1294ccd48bc7SAl Viro if (!mnt) 1295ccd48bc7SAl Viro goto out; 1296ccd48bc7SAl Viro 1297*8c3ee42eSAl Viro err = graft_tree(mnt, &nd->path); 12981da177e4SLinus Torvalds if (err) { 129970fbcdf4SRam Pai LIST_HEAD(umount_list); 13001da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 1301a05964f3SRam Pai umount_tree(mnt, 0, &umount_list); 13021da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 130370fbcdf4SRam Pai release_mounts(&umount_list); 13045b83d2c5SRam Pai } 13051da177e4SLinus Torvalds 1306ccd48bc7SAl Viro out: 1307390c6843SRam Pai up_write(&namespace_sem); 13081d957f9bSJan Blunck path_put(&old_nd.path); 13091da177e4SLinus Torvalds return err; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13122e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 13132e4b7fcdSDave Hansen { 13142e4b7fcdSDave Hansen int error = 0; 13152e4b7fcdSDave Hansen int readonly_request = 0; 13162e4b7fcdSDave Hansen 13172e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 13182e4b7fcdSDave Hansen readonly_request = 1; 13192e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 13202e4b7fcdSDave Hansen return 0; 13212e4b7fcdSDave Hansen 13222e4b7fcdSDave Hansen if (readonly_request) 13232e4b7fcdSDave Hansen error = mnt_make_readonly(mnt); 13242e4b7fcdSDave Hansen else 13252e4b7fcdSDave Hansen __mnt_unmake_readonly(mnt); 13262e4b7fcdSDave Hansen return error; 13272e4b7fcdSDave Hansen } 13282e4b7fcdSDave Hansen 13291da177e4SLinus Torvalds /* 13301da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 13311da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 13321da177e4SLinus Torvalds * on it - tough luck. 13332dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 13341da177e4SLinus Torvalds */ 13352dafe1c4SEric Sandeen static noinline int do_remount(struct nameidata *nd, int flags, int mnt_flags, 13361da177e4SLinus Torvalds void *data) 13371da177e4SLinus Torvalds { 13381da177e4SLinus Torvalds int err; 13394ac91378SJan Blunck struct super_block *sb = nd->path.mnt->mnt_sb; 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 13421da177e4SLinus Torvalds return -EPERM; 13431da177e4SLinus Torvalds 13444ac91378SJan Blunck if (!check_mnt(nd->path.mnt)) 13451da177e4SLinus Torvalds return -EINVAL; 13461da177e4SLinus Torvalds 13474ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) 13481da177e4SLinus Torvalds return -EINVAL; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds down_write(&sb->s_umount); 13512e4b7fcdSDave Hansen if (flags & MS_BIND) 13522e4b7fcdSDave Hansen err = change_mount_flags(nd->path.mnt, flags); 13532e4b7fcdSDave Hansen else 13541da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 13551da177e4SLinus Torvalds if (!err) 13564ac91378SJan Blunck nd->path.mnt->mnt_flags = mnt_flags; 13571da177e4SLinus Torvalds up_write(&sb->s_umount); 13581da177e4SLinus Torvalds if (!err) 13594ac91378SJan Blunck security_sb_post_remount(nd->path.mnt, flags, data); 13601da177e4SLinus Torvalds return err; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 13639676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt) 13649676f0c6SRam Pai { 13659676f0c6SRam Pai struct vfsmount *p; 13669676f0c6SRam Pai for (p = mnt; p; p = next_mnt(p, mnt)) { 13679676f0c6SRam Pai if (IS_MNT_UNBINDABLE(p)) 13689676f0c6SRam Pai return 1; 13699676f0c6SRam Pai } 13709676f0c6SRam Pai return 0; 13719676f0c6SRam Pai } 13729676f0c6SRam Pai 13732dafe1c4SEric Sandeen /* 13742dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 13752dafe1c4SEric Sandeen */ 13762dafe1c4SEric Sandeen static noinline int do_move_mount(struct nameidata *nd, char *old_name) 13771da177e4SLinus Torvalds { 13781a390689SAl Viro struct nameidata old_nd; 13791a390689SAl Viro struct path parent_path; 13801da177e4SLinus Torvalds struct vfsmount *p; 13811da177e4SLinus Torvalds int err = 0; 13821da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 13831da177e4SLinus Torvalds return -EPERM; 13841da177e4SLinus Torvalds if (!old_name || !*old_name) 13851da177e4SLinus Torvalds return -EINVAL; 13861da177e4SLinus Torvalds err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd); 13871da177e4SLinus Torvalds if (err) 13881da177e4SLinus Torvalds return err; 13891da177e4SLinus Torvalds 1390390c6843SRam Pai down_write(&namespace_sem); 13914ac91378SJan Blunck while (d_mountpoint(nd->path.dentry) && 13924ac91378SJan Blunck follow_down(&nd->path.mnt, &nd->path.dentry)) 13931da177e4SLinus Torvalds ; 13941da177e4SLinus Torvalds err = -EINVAL; 13954ac91378SJan Blunck if (!check_mnt(nd->path.mnt) || !check_mnt(old_nd.path.mnt)) 13961da177e4SLinus Torvalds goto out; 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds err = -ENOENT; 13994ac91378SJan Blunck mutex_lock(&nd->path.dentry->d_inode->i_mutex); 14004ac91378SJan Blunck if (IS_DEADDIR(nd->path.dentry->d_inode)) 14011da177e4SLinus Torvalds goto out1; 14021da177e4SLinus Torvalds 14034ac91378SJan Blunck if (!IS_ROOT(nd->path.dentry) && d_unhashed(nd->path.dentry)) 140421444403SRam Pai goto out1; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds err = -EINVAL; 14074ac91378SJan Blunck if (old_nd.path.dentry != old_nd.path.mnt->mnt_root) 140821444403SRam Pai goto out1; 14091da177e4SLinus Torvalds 14104ac91378SJan Blunck if (old_nd.path.mnt == old_nd.path.mnt->mnt_parent) 141121444403SRam Pai goto out1; 14121da177e4SLinus Torvalds 14134ac91378SJan Blunck if (S_ISDIR(nd->path.dentry->d_inode->i_mode) != 14144ac91378SJan Blunck S_ISDIR(old_nd.path.dentry->d_inode->i_mode)) 141521444403SRam Pai goto out1; 141621444403SRam Pai /* 141721444403SRam Pai * Don't move a mount residing in a shared parent. 141821444403SRam Pai */ 14194ac91378SJan Blunck if (old_nd.path.mnt->mnt_parent && 14204ac91378SJan Blunck IS_MNT_SHARED(old_nd.path.mnt->mnt_parent)) 142121444403SRam Pai goto out1; 14229676f0c6SRam Pai /* 14239676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 14249676f0c6SRam Pai * mount which is shared. 14259676f0c6SRam Pai */ 14264ac91378SJan Blunck if (IS_MNT_SHARED(nd->path.mnt) && 14274ac91378SJan Blunck tree_contains_unbindable(old_nd.path.mnt)) 14289676f0c6SRam Pai goto out1; 14291da177e4SLinus Torvalds err = -ELOOP; 14304ac91378SJan Blunck for (p = nd->path.mnt; p->mnt_parent != p; p = p->mnt_parent) 14314ac91378SJan Blunck if (p == old_nd.path.mnt) 143221444403SRam Pai goto out1; 14331da177e4SLinus Torvalds 14341a390689SAl Viro err = attach_recursive_mnt(old_nd.path.mnt, &nd->path, &parent_path); 14354ac91378SJan Blunck if (err) 143621444403SRam Pai goto out1; 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 14391da177e4SLinus Torvalds * automatically */ 14404ac91378SJan Blunck list_del_init(&old_nd.path.mnt->mnt_expire); 14411da177e4SLinus Torvalds out1: 14424ac91378SJan Blunck mutex_unlock(&nd->path.dentry->d_inode->i_mutex); 14431da177e4SLinus Torvalds out: 1444390c6843SRam Pai up_write(&namespace_sem); 14451da177e4SLinus Torvalds if (!err) 14461a390689SAl Viro path_put(&parent_path); 14471d957f9bSJan Blunck path_put(&old_nd.path); 14481da177e4SLinus Torvalds return err; 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds /* 14521da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 14531da177e4SLinus Torvalds * namespace's tree 14542dafe1c4SEric Sandeen * noinline this do_mount helper to save do_mount stack space. 14551da177e4SLinus Torvalds */ 14562dafe1c4SEric Sandeen static noinline int do_new_mount(struct nameidata *nd, char *type, int flags, 14571da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 14581da177e4SLinus Torvalds { 14591da177e4SLinus Torvalds struct vfsmount *mnt; 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds if (!type || !memchr(type, 0, PAGE_SIZE)) 14621da177e4SLinus Torvalds return -EINVAL; 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds /* we need capabilities... */ 14651da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 14661da177e4SLinus Torvalds return -EPERM; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 14691da177e4SLinus Torvalds if (IS_ERR(mnt)) 14701da177e4SLinus Torvalds return PTR_ERR(mnt); 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds return do_add_mount(mnt, nd, mnt_flags, NULL); 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds /* 14761da177e4SLinus Torvalds * add a mount into a namespace's mount tree 14771da177e4SLinus Torvalds * - provide the option of adding the new mount to an expiration list 14781da177e4SLinus Torvalds */ 14791da177e4SLinus Torvalds int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd, 14801da177e4SLinus Torvalds int mnt_flags, struct list_head *fslist) 14811da177e4SLinus Torvalds { 14821da177e4SLinus Torvalds int err; 14831da177e4SLinus Torvalds 1484390c6843SRam Pai down_write(&namespace_sem); 14851da177e4SLinus Torvalds /* Something was mounted here while we slept */ 14864ac91378SJan Blunck while (d_mountpoint(nd->path.dentry) && 14874ac91378SJan Blunck follow_down(&nd->path.mnt, &nd->path.dentry)) 14881da177e4SLinus Torvalds ; 14891da177e4SLinus Torvalds err = -EINVAL; 14904ac91378SJan Blunck if (!check_mnt(nd->path.mnt)) 14911da177e4SLinus Torvalds goto unlock; 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds /* Refuse the same filesystem on the same mount point */ 14941da177e4SLinus Torvalds err = -EBUSY; 14954ac91378SJan Blunck if (nd->path.mnt->mnt_sb == newmnt->mnt_sb && 14964ac91378SJan Blunck nd->path.mnt->mnt_root == nd->path.dentry) 14971da177e4SLinus Torvalds goto unlock; 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds err = -EINVAL; 15001da177e4SLinus Torvalds if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode)) 15011da177e4SLinus Torvalds goto unlock; 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds newmnt->mnt_flags = mnt_flags; 1504*8c3ee42eSAl Viro if ((err = graft_tree(newmnt, &nd->path))) 15055b83d2c5SRam Pai goto unlock; 15061da177e4SLinus Torvalds 15076758f953SAl Viro if (fslist) /* add to the specified expiration list */ 150855e700b9SMiklos Szeredi list_add_tail(&newmnt->mnt_expire, fslist); 15096758f953SAl Viro 1510390c6843SRam Pai up_write(&namespace_sem); 15115b83d2c5SRam Pai return 0; 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds unlock: 1514390c6843SRam Pai up_write(&namespace_sem); 15151da177e4SLinus Torvalds mntput(newmnt); 15161da177e4SLinus Torvalds return err; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount); 15201da177e4SLinus Torvalds 15215528f911STrond Myklebust /* 15221da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 15231da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 15241da177e4SLinus Torvalds * here 15251da177e4SLinus Torvalds */ 15261da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 15271da177e4SLinus Torvalds { 15281da177e4SLinus Torvalds struct vfsmount *mnt, *next; 15291da177e4SLinus Torvalds LIST_HEAD(graveyard); 1530bcc5c7d2SAl Viro LIST_HEAD(umounts); 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds if (list_empty(mounts)) 15331da177e4SLinus Torvalds return; 15341da177e4SLinus Torvalds 1535bcc5c7d2SAl Viro down_write(&namespace_sem); 15361da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 15391da177e4SLinus Torvalds * following criteria: 15401da177e4SLinus Torvalds * - only referenced by its parent vfsmount 15411da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 15421da177e4SLinus Torvalds * cleared by mntput()) 15431da177e4SLinus Torvalds */ 154455e700b9SMiklos Szeredi list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 15451da177e4SLinus Torvalds if (!xchg(&mnt->mnt_expiry_mark, 1) || 1546bcc5c7d2SAl Viro propagate_mount_busy(mnt, 1)) 15471da177e4SLinus Torvalds continue; 154855e700b9SMiklos Szeredi list_move(&mnt->mnt_expire, &graveyard); 15491da177e4SLinus Torvalds } 1550bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 1551bcc5c7d2SAl Viro mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire); 1552bcc5c7d2SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1553bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 1554bcc5c7d2SAl Viro } 15551da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 1556bcc5c7d2SAl Viro up_write(&namespace_sem); 1557bcc5c7d2SAl Viro 1558bcc5c7d2SAl Viro release_mounts(&umounts); 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* 15645528f911STrond Myklebust * Ripoff of 'select_parent()' 15655528f911STrond Myklebust * 15665528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 15675528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 15685528f911STrond Myklebust */ 15695528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard) 15705528f911STrond Myklebust { 15715528f911STrond Myklebust struct vfsmount *this_parent = parent; 15725528f911STrond Myklebust struct list_head *next; 15735528f911STrond Myklebust int found = 0; 15745528f911STrond Myklebust 15755528f911STrond Myklebust repeat: 15765528f911STrond Myklebust next = this_parent->mnt_mounts.next; 15775528f911STrond Myklebust resume: 15785528f911STrond Myklebust while (next != &this_parent->mnt_mounts) { 15795528f911STrond Myklebust struct list_head *tmp = next; 15805528f911STrond Myklebust struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child); 15815528f911STrond Myklebust 15825528f911STrond Myklebust next = tmp->next; 15835528f911STrond Myklebust if (!(mnt->mnt_flags & MNT_SHRINKABLE)) 15845528f911STrond Myklebust continue; 15855528f911STrond Myklebust /* 15865528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 15875528f911STrond Myklebust */ 15885528f911STrond Myklebust if (!list_empty(&mnt->mnt_mounts)) { 15895528f911STrond Myklebust this_parent = mnt; 15905528f911STrond Myklebust goto repeat; 15915528f911STrond Myklebust } 15925528f911STrond Myklebust 15935528f911STrond Myklebust if (!propagate_mount_busy(mnt, 1)) { 15945528f911STrond Myklebust list_move_tail(&mnt->mnt_expire, graveyard); 15955528f911STrond Myklebust found++; 15965528f911STrond Myklebust } 15975528f911STrond Myklebust } 15985528f911STrond Myklebust /* 15995528f911STrond Myklebust * All done at this level ... ascend and resume the search 16005528f911STrond Myklebust */ 16015528f911STrond Myklebust if (this_parent != parent) { 16025528f911STrond Myklebust next = this_parent->mnt_child.next; 16035528f911STrond Myklebust this_parent = this_parent->mnt_parent; 16045528f911STrond Myklebust goto resume; 16055528f911STrond Myklebust } 16065528f911STrond Myklebust return found; 16075528f911STrond Myklebust } 16085528f911STrond Myklebust 16095528f911STrond Myklebust /* 16105528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 16115528f911STrond Myklebust * submounts of a specific parent mountpoint 16125528f911STrond Myklebust */ 1613c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts) 16145528f911STrond Myklebust { 16155528f911STrond Myklebust LIST_HEAD(graveyard); 1616c35038beSAl Viro struct vfsmount *m; 16175528f911STrond Myklebust 16185528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 1619c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 1620bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 1621c35038beSAl Viro m = list_first_entry(&graveyard, struct vfsmount, 1622bcc5c7d2SAl Viro mnt_expire); 1623bcc5c7d2SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1624c35038beSAl Viro umount_tree(mnt, 1, umounts); 1625bcc5c7d2SAl Viro } 1626bcc5c7d2SAl Viro } 16275528f911STrond Myklebust } 16285528f911STrond Myklebust 16295528f911STrond Myklebust /* 16301da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 16311da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 16321da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 16331da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 16341da177e4SLinus Torvalds */ 1635b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 1636b58fed8bSRam Pai unsigned long n) 16371da177e4SLinus Torvalds { 16381da177e4SLinus Torvalds char *t = to; 16391da177e4SLinus Torvalds const char __user *f = from; 16401da177e4SLinus Torvalds char c; 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 16431da177e4SLinus Torvalds return n; 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds while (n) { 16461da177e4SLinus Torvalds if (__get_user(c, f)) { 16471da177e4SLinus Torvalds memset(t, 0, n); 16481da177e4SLinus Torvalds break; 16491da177e4SLinus Torvalds } 16501da177e4SLinus Torvalds *t++ = c; 16511da177e4SLinus Torvalds f++; 16521da177e4SLinus Torvalds n--; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds return n; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 16581da177e4SLinus Torvalds { 16591da177e4SLinus Torvalds int i; 16601da177e4SLinus Torvalds unsigned long page; 16611da177e4SLinus Torvalds unsigned long size; 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds *where = 0; 16641da177e4SLinus Torvalds if (!data) 16651da177e4SLinus Torvalds return 0; 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 16681da177e4SLinus Torvalds return -ENOMEM; 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* We only care that *some* data at the address the user 16711da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 16721da177e4SLinus Torvalds * the remainder of the page. 16731da177e4SLinus Torvalds */ 16741da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 16751da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 16761da177e4SLinus Torvalds if (size > PAGE_SIZE) 16771da177e4SLinus Torvalds size = PAGE_SIZE; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 16801da177e4SLinus Torvalds if (!i) { 16811da177e4SLinus Torvalds free_page(page); 16821da177e4SLinus Torvalds return -EFAULT; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds if (i != PAGE_SIZE) 16851da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 16861da177e4SLinus Torvalds *where = page; 16871da177e4SLinus Torvalds return 0; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds /* 16911da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 16921da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 16931da177e4SLinus Torvalds * 16941da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 16951da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 16961da177e4SLinus Torvalds * information (or be NULL). 16971da177e4SLinus Torvalds * 16981da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 16991da177e4SLinus Torvalds * When the flags word was introduced its top half was required 17001da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 17011da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 17021da177e4SLinus Torvalds * and must be discarded. 17031da177e4SLinus Torvalds */ 17041da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 17051da177e4SLinus Torvalds unsigned long flags, void *data_page) 17061da177e4SLinus Torvalds { 17071da177e4SLinus Torvalds struct nameidata nd; 17081da177e4SLinus Torvalds int retval = 0; 17091da177e4SLinus Torvalds int mnt_flags = 0; 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds /* Discard magic */ 17121da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 17131da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds /* Basic sanity checks */ 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 17181da177e4SLinus Torvalds return -EINVAL; 17191da177e4SLinus Torvalds if (dev_name && !memchr(dev_name, 0, PAGE_SIZE)) 17201da177e4SLinus Torvalds return -EINVAL; 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds if (data_page) 17231da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 17261da177e4SLinus Torvalds if (flags & MS_NOSUID) 17271da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 17281da177e4SLinus Torvalds if (flags & MS_NODEV) 17291da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 17301da177e4SLinus Torvalds if (flags & MS_NOEXEC) 17311da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 1732fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 1733fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 1734fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 1735fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 173647ae32d6SValerie Henson if (flags & MS_RELATIME) 173747ae32d6SValerie Henson mnt_flags |= MNT_RELATIME; 17382e4b7fcdSDave Hansen if (flags & MS_RDONLY) 17392e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 1740fc33a7bbSChristoph Hellwig 1741fc33a7bbSChristoph Hellwig flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | 17428bf9725cSPavel Emelyanov MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT); 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds /* ... and get the mountpoint */ 17451da177e4SLinus Torvalds retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd); 17461da177e4SLinus Torvalds if (retval) 17471da177e4SLinus Torvalds return retval; 17481da177e4SLinus Torvalds 1749b5266eb4SAl Viro retval = security_sb_mount(dev_name, &nd.path, 1750b5266eb4SAl Viro type_page, flags, data_page); 17511da177e4SLinus Torvalds if (retval) 17521da177e4SLinus Torvalds goto dput_out; 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds if (flags & MS_REMOUNT) 17551da177e4SLinus Torvalds retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags, 17561da177e4SLinus Torvalds data_page); 17571da177e4SLinus Torvalds else if (flags & MS_BIND) 1758eee391a6SAndrew Morton retval = do_loopback(&nd, dev_name, flags & MS_REC); 17599676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 176007b20889SRam Pai retval = do_change_type(&nd, flags); 17611da177e4SLinus Torvalds else if (flags & MS_MOVE) 17621da177e4SLinus Torvalds retval = do_move_mount(&nd, dev_name); 17631da177e4SLinus Torvalds else 17641da177e4SLinus Torvalds retval = do_new_mount(&nd, type_page, flags, mnt_flags, 17651da177e4SLinus Torvalds dev_name, data_page); 17661da177e4SLinus Torvalds dput_out: 17671d957f9bSJan Blunck path_put(&nd.path); 17681da177e4SLinus Torvalds return retval; 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds 1771741a2951SJANAK DESAI /* 1772741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 1773741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 1774741a2951SJANAK DESAI */ 1775e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 17766b3286edSKirill Korotaev struct fs_struct *fs) 17771da177e4SLinus Torvalds { 17786b3286edSKirill Korotaev struct mnt_namespace *new_ns; 17791da177e4SLinus Torvalds struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL; 17801da177e4SLinus Torvalds struct vfsmount *p, *q; 17811da177e4SLinus Torvalds 17826b3286edSKirill Korotaev new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 17831da177e4SLinus Torvalds if (!new_ns) 1784467e9f4bSCedric Le Goater return ERR_PTR(-ENOMEM); 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds atomic_set(&new_ns->count, 1); 17871da177e4SLinus Torvalds INIT_LIST_HEAD(&new_ns->list); 17885addc5ddSAl Viro init_waitqueue_head(&new_ns->poll); 17895addc5ddSAl Viro new_ns->event = 0; 17901da177e4SLinus Torvalds 1791390c6843SRam Pai down_write(&namespace_sem); 17921da177e4SLinus Torvalds /* First pass: copy the tree topology */ 17936b3286edSKirill Korotaev new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root, 17949676f0c6SRam Pai CL_COPY_ALL | CL_EXPIRE); 17951da177e4SLinus Torvalds if (!new_ns->root) { 1796390c6843SRam Pai up_write(&namespace_sem); 17971da177e4SLinus Torvalds kfree(new_ns); 1798467e9f4bSCedric Le Goater return ERR_PTR(-ENOMEM);; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 18011da177e4SLinus Torvalds list_add_tail(&new_ns->list, &new_ns->root->mnt_list); 18021da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds /* 18051da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 18061da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 18071da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 18081da177e4SLinus Torvalds */ 18096b3286edSKirill Korotaev p = mnt_ns->root; 18101da177e4SLinus Torvalds q = new_ns->root; 18111da177e4SLinus Torvalds while (p) { 18126b3286edSKirill Korotaev q->mnt_ns = new_ns; 18131da177e4SLinus Torvalds if (fs) { 18146ac08c39SJan Blunck if (p == fs->root.mnt) { 18151da177e4SLinus Torvalds rootmnt = p; 18166ac08c39SJan Blunck fs->root.mnt = mntget(q); 18171da177e4SLinus Torvalds } 18186ac08c39SJan Blunck if (p == fs->pwd.mnt) { 18191da177e4SLinus Torvalds pwdmnt = p; 18206ac08c39SJan Blunck fs->pwd.mnt = mntget(q); 18211da177e4SLinus Torvalds } 18226ac08c39SJan Blunck if (p == fs->altroot.mnt) { 18231da177e4SLinus Torvalds altrootmnt = p; 18246ac08c39SJan Blunck fs->altroot.mnt = mntget(q); 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds } 18276b3286edSKirill Korotaev p = next_mnt(p, mnt_ns->root); 18281da177e4SLinus Torvalds q = next_mnt(q, new_ns->root); 18291da177e4SLinus Torvalds } 1830390c6843SRam Pai up_write(&namespace_sem); 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds if (rootmnt) 18331da177e4SLinus Torvalds mntput(rootmnt); 18341da177e4SLinus Torvalds if (pwdmnt) 18351da177e4SLinus Torvalds mntput(pwdmnt); 18361da177e4SLinus Torvalds if (altrootmnt) 18371da177e4SLinus Torvalds mntput(altrootmnt); 18381da177e4SLinus Torvalds 1839741a2951SJANAK DESAI return new_ns; 1840741a2951SJANAK DESAI } 1841741a2951SJANAK DESAI 1842213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 1843e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 1844741a2951SJANAK DESAI { 18456b3286edSKirill Korotaev struct mnt_namespace *new_ns; 1846741a2951SJANAK DESAI 1847e3222c4eSBadari Pulavarty BUG_ON(!ns); 18486b3286edSKirill Korotaev get_mnt_ns(ns); 1849741a2951SJANAK DESAI 1850741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 1851e3222c4eSBadari Pulavarty return ns; 1852741a2951SJANAK DESAI 1853e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 1854741a2951SJANAK DESAI 18556b3286edSKirill Korotaev put_mnt_ns(ns); 1856e3222c4eSBadari Pulavarty return new_ns; 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name, 18601da177e4SLinus Torvalds char __user * type, unsigned long flags, 18611da177e4SLinus Torvalds void __user * data) 18621da177e4SLinus Torvalds { 18631da177e4SLinus Torvalds int retval; 18641da177e4SLinus Torvalds unsigned long data_page; 18651da177e4SLinus Torvalds unsigned long type_page; 18661da177e4SLinus Torvalds unsigned long dev_page; 18671da177e4SLinus Torvalds char *dir_page; 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds retval = copy_mount_options(type, &type_page); 18701da177e4SLinus Torvalds if (retval < 0) 18711da177e4SLinus Torvalds return retval; 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds dir_page = getname(dir_name); 18741da177e4SLinus Torvalds retval = PTR_ERR(dir_page); 18751da177e4SLinus Torvalds if (IS_ERR(dir_page)) 18761da177e4SLinus Torvalds goto out1; 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds retval = copy_mount_options(dev_name, &dev_page); 18791da177e4SLinus Torvalds if (retval < 0) 18801da177e4SLinus Torvalds goto out2; 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds retval = copy_mount_options(data, &data_page); 18831da177e4SLinus Torvalds if (retval < 0) 18841da177e4SLinus Torvalds goto out3; 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds lock_kernel(); 18871da177e4SLinus Torvalds retval = do_mount((char *)dev_page, dir_page, (char *)type_page, 18881da177e4SLinus Torvalds flags, (void *)data_page); 18891da177e4SLinus Torvalds unlock_kernel(); 18901da177e4SLinus Torvalds free_page(data_page); 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds out3: 18931da177e4SLinus Torvalds free_page(dev_page); 18941da177e4SLinus Torvalds out2: 18951da177e4SLinus Torvalds putname(dir_page); 18961da177e4SLinus Torvalds out1: 18971da177e4SLinus Torvalds free_page(type_page); 18981da177e4SLinus Torvalds return retval; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds /* 19021da177e4SLinus Torvalds * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values. 19031da177e4SLinus Torvalds * It can block. Requires the big lock held. 19041da177e4SLinus Torvalds */ 1905ac748a09SJan Blunck void set_fs_root(struct fs_struct *fs, struct path *path) 19061da177e4SLinus Torvalds { 19076ac08c39SJan Blunck struct path old_root; 19086ac08c39SJan Blunck 19091da177e4SLinus Torvalds write_lock(&fs->lock); 19101da177e4SLinus Torvalds old_root = fs->root; 1911ac748a09SJan Blunck fs->root = *path; 1912ac748a09SJan Blunck path_get(path); 19131da177e4SLinus Torvalds write_unlock(&fs->lock); 19146ac08c39SJan Blunck if (old_root.dentry) 19156ac08c39SJan Blunck path_put(&old_root); 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds /* 19191da177e4SLinus Torvalds * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values. 19201da177e4SLinus Torvalds * It can block. Requires the big lock held. 19211da177e4SLinus Torvalds */ 1922ac748a09SJan Blunck void set_fs_pwd(struct fs_struct *fs, struct path *path) 19231da177e4SLinus Torvalds { 19246ac08c39SJan Blunck struct path old_pwd; 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds write_lock(&fs->lock); 19271da177e4SLinus Torvalds old_pwd = fs->pwd; 1928ac748a09SJan Blunck fs->pwd = *path; 1929ac748a09SJan Blunck path_get(path); 19301da177e4SLinus Torvalds write_unlock(&fs->lock); 19311da177e4SLinus Torvalds 19326ac08c39SJan Blunck if (old_pwd.dentry) 19336ac08c39SJan Blunck path_put(&old_pwd); 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds 19361a390689SAl Viro static void chroot_fs_refs(struct path *old_root, struct path *new_root) 19371da177e4SLinus Torvalds { 19381da177e4SLinus Torvalds struct task_struct *g, *p; 19391da177e4SLinus Torvalds struct fs_struct *fs; 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds read_lock(&tasklist_lock); 19421da177e4SLinus Torvalds do_each_thread(g, p) { 19431da177e4SLinus Torvalds task_lock(p); 19441da177e4SLinus Torvalds fs = p->fs; 19451da177e4SLinus Torvalds if (fs) { 19461da177e4SLinus Torvalds atomic_inc(&fs->count); 19471da177e4SLinus Torvalds task_unlock(p); 19481a390689SAl Viro if (fs->root.dentry == old_root->dentry 19491a390689SAl Viro && fs->root.mnt == old_root->mnt) 19501a390689SAl Viro set_fs_root(fs, new_root); 19511a390689SAl Viro if (fs->pwd.dentry == old_root->dentry 19521a390689SAl Viro && fs->pwd.mnt == old_root->mnt) 19531a390689SAl Viro set_fs_pwd(fs, new_root); 19541da177e4SLinus Torvalds put_fs_struct(fs); 19551da177e4SLinus Torvalds } else 19561da177e4SLinus Torvalds task_unlock(p); 19571da177e4SLinus Torvalds } while_each_thread(g, p); 19581da177e4SLinus Torvalds read_unlock(&tasklist_lock); 19591da177e4SLinus Torvalds } 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds /* 19621da177e4SLinus Torvalds * pivot_root Semantics: 19631da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 19641da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 19651da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 19661da177e4SLinus Torvalds * 19671da177e4SLinus Torvalds * Restrictions: 19681da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 19691da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 19701da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 19711da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 19721da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 19731da177e4SLinus Torvalds * 19744a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 19754a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 19764a0d11faSNeil Brown * in this situation. 19774a0d11faSNeil Brown * 19781da177e4SLinus Torvalds * Notes: 19791da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 19801da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 19811da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 19821da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 19831da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 19841da177e4SLinus Torvalds * first. 19851da177e4SLinus Torvalds */ 1986b58fed8bSRam Pai asmlinkage long sys_pivot_root(const char __user * new_root, 1987b58fed8bSRam Pai const char __user * put_old) 19881da177e4SLinus Torvalds { 19891da177e4SLinus Torvalds struct vfsmount *tmp; 1990*8c3ee42eSAl Viro struct nameidata new_nd, old_nd; 1991*8c3ee42eSAl Viro struct path parent_path, root_parent, root; 19921da177e4SLinus Torvalds int error; 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 19951da177e4SLinus Torvalds return -EPERM; 19961da177e4SLinus Torvalds 1997b58fed8bSRam Pai error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, 1998b58fed8bSRam Pai &new_nd); 19991da177e4SLinus Torvalds if (error) 20001da177e4SLinus Torvalds goto out0; 20011da177e4SLinus Torvalds error = -EINVAL; 20024ac91378SJan Blunck if (!check_mnt(new_nd.path.mnt)) 20031da177e4SLinus Torvalds goto out1; 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd); 20061da177e4SLinus Torvalds if (error) 20071da177e4SLinus Torvalds goto out1; 20081da177e4SLinus Torvalds 2009b5266eb4SAl Viro error = security_sb_pivotroot(&old_nd.path, &new_nd.path); 20101da177e4SLinus Torvalds if (error) { 20111d957f9bSJan Blunck path_put(&old_nd.path); 20121da177e4SLinus Torvalds goto out1; 20131da177e4SLinus Torvalds } 20141da177e4SLinus Torvalds 20151da177e4SLinus Torvalds read_lock(¤t->fs->lock); 2016*8c3ee42eSAl Viro root = current->fs->root; 20176ac08c39SJan Blunck path_get(¤t->fs->root); 20181da177e4SLinus Torvalds read_unlock(¤t->fs->lock); 2019390c6843SRam Pai down_write(&namespace_sem); 20204ac91378SJan Blunck mutex_lock(&old_nd.path.dentry->d_inode->i_mutex); 20211da177e4SLinus Torvalds error = -EINVAL; 20224ac91378SJan Blunck if (IS_MNT_SHARED(old_nd.path.mnt) || 20234ac91378SJan Blunck IS_MNT_SHARED(new_nd.path.mnt->mnt_parent) || 2024*8c3ee42eSAl Viro IS_MNT_SHARED(root.mnt->mnt_parent)) 202521444403SRam Pai goto out2; 2026*8c3ee42eSAl Viro if (!check_mnt(root.mnt)) 20271da177e4SLinus Torvalds goto out2; 20281da177e4SLinus Torvalds error = -ENOENT; 20294ac91378SJan Blunck if (IS_DEADDIR(new_nd.path.dentry->d_inode)) 20301da177e4SLinus Torvalds goto out2; 20314ac91378SJan Blunck if (d_unhashed(new_nd.path.dentry) && !IS_ROOT(new_nd.path.dentry)) 20321da177e4SLinus Torvalds goto out2; 20334ac91378SJan Blunck if (d_unhashed(old_nd.path.dentry) && !IS_ROOT(old_nd.path.dentry)) 20341da177e4SLinus Torvalds goto out2; 20351da177e4SLinus Torvalds error = -EBUSY; 2036*8c3ee42eSAl Viro if (new_nd.path.mnt == root.mnt || 2037*8c3ee42eSAl Viro old_nd.path.mnt == root.mnt) 20381da177e4SLinus Torvalds goto out2; /* loop, on the same file system */ 20391da177e4SLinus Torvalds error = -EINVAL; 2040*8c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 20411da177e4SLinus Torvalds goto out2; /* not a mountpoint */ 2042*8c3ee42eSAl Viro if (root.mnt->mnt_parent == root.mnt) 20430bb6fcc1SMiklos Szeredi goto out2; /* not attached */ 20444ac91378SJan Blunck if (new_nd.path.mnt->mnt_root != new_nd.path.dentry) 20451da177e4SLinus Torvalds goto out2; /* not a mountpoint */ 20464ac91378SJan Blunck if (new_nd.path.mnt->mnt_parent == new_nd.path.mnt) 20470bb6fcc1SMiklos Szeredi goto out2; /* not attached */ 20484ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 20494ac91378SJan Blunck tmp = old_nd.path.mnt; 20501da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 20514ac91378SJan Blunck if (tmp != new_nd.path.mnt) { 20521da177e4SLinus Torvalds for (;;) { 20531da177e4SLinus Torvalds if (tmp->mnt_parent == tmp) 20541da177e4SLinus Torvalds goto out3; /* already mounted on put_old */ 20554ac91378SJan Blunck if (tmp->mnt_parent == new_nd.path.mnt) 20561da177e4SLinus Torvalds break; 20571da177e4SLinus Torvalds tmp = tmp->mnt_parent; 20581da177e4SLinus Torvalds } 20594ac91378SJan Blunck if (!is_subdir(tmp->mnt_mountpoint, new_nd.path.dentry)) 20601da177e4SLinus Torvalds goto out3; 20614ac91378SJan Blunck } else if (!is_subdir(old_nd.path.dentry, new_nd.path.dentry)) 20621da177e4SLinus Torvalds goto out3; 20631a390689SAl Viro detach_mnt(new_nd.path.mnt, &parent_path); 2064*8c3ee42eSAl Viro detach_mnt(root.mnt, &root_parent); 20654ac91378SJan Blunck /* mount old root on put_old */ 2066*8c3ee42eSAl Viro attach_mnt(root.mnt, &old_nd.path); 20674ac91378SJan Blunck /* mount new_root on / */ 20684ac91378SJan Blunck attach_mnt(new_nd.path.mnt, &root_parent); 20696b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 20701da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 2071*8c3ee42eSAl Viro chroot_fs_refs(&root, &new_nd.path); 2072*8c3ee42eSAl Viro security_sb_post_pivotroot(&root, &new_nd.path); 20731da177e4SLinus Torvalds error = 0; 20741a390689SAl Viro path_put(&root_parent); 20751a390689SAl Viro path_put(&parent_path); 20761da177e4SLinus Torvalds out2: 20774ac91378SJan Blunck mutex_unlock(&old_nd.path.dentry->d_inode->i_mutex); 2078390c6843SRam Pai up_write(&namespace_sem); 2079*8c3ee42eSAl Viro path_put(&root); 20801d957f9bSJan Blunck path_put(&old_nd.path); 20811da177e4SLinus Torvalds out1: 20821d957f9bSJan Blunck path_put(&new_nd.path); 20831da177e4SLinus Torvalds out0: 20841da177e4SLinus Torvalds return error; 20851da177e4SLinus Torvalds out3: 20861da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 20871da177e4SLinus Torvalds goto out2; 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds static void __init init_mount_tree(void) 20911da177e4SLinus Torvalds { 20921da177e4SLinus Torvalds struct vfsmount *mnt; 20936b3286edSKirill Korotaev struct mnt_namespace *ns; 2094ac748a09SJan Blunck struct path root; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 20971da177e4SLinus Torvalds if (IS_ERR(mnt)) 20981da177e4SLinus Torvalds panic("Can't create rootfs"); 20996b3286edSKirill Korotaev ns = kmalloc(sizeof(*ns), GFP_KERNEL); 21006b3286edSKirill Korotaev if (!ns) 21011da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 21026b3286edSKirill Korotaev atomic_set(&ns->count, 1); 21036b3286edSKirill Korotaev INIT_LIST_HEAD(&ns->list); 21046b3286edSKirill Korotaev init_waitqueue_head(&ns->poll); 21056b3286edSKirill Korotaev ns->event = 0; 21066b3286edSKirill Korotaev list_add(&mnt->mnt_list, &ns->list); 21076b3286edSKirill Korotaev ns->root = mnt; 21086b3286edSKirill Korotaev mnt->mnt_ns = ns; 21091da177e4SLinus Torvalds 21106b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 21116b3286edSKirill Korotaev get_mnt_ns(ns); 21121da177e4SLinus Torvalds 2113ac748a09SJan Blunck root.mnt = ns->root; 2114ac748a09SJan Blunck root.dentry = ns->root->mnt_root; 2115ac748a09SJan Blunck 2116ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2117ac748a09SJan Blunck set_fs_root(current->fs, &root); 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds 212074bf17cfSDenis Cheng void __init mnt_init(void) 21211da177e4SLinus Torvalds { 212213f14b4dSEric Dumazet unsigned u; 212315a67dd8SRandy Dunlap int err; 21241da177e4SLinus Torvalds 2125390c6843SRam Pai init_rwsem(&namespace_sem); 2126390c6843SRam Pai 21271da177e4SLinus Torvalds mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount), 212820c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 21291da177e4SLinus Torvalds 2130b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds if (!mount_hashtable) 21331da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 21341da177e4SLinus Torvalds 213513f14b4dSEric Dumazet printk("Mount-cache hash table entries: %lu\n", HASH_SIZE); 21361da177e4SLinus Torvalds 213713f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 213813f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 21391da177e4SLinus Torvalds 214015a67dd8SRandy Dunlap err = sysfs_init(); 214115a67dd8SRandy Dunlap if (err) 214215a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 214315a67dd8SRandy Dunlap __FUNCTION__, err); 214400d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 214500d26666SGreg Kroah-Hartman if (!fs_kobj) 214600d26666SGreg Kroah-Hartman printk(KERN_WARNING "%s: kobj create error\n", __FUNCTION__); 21471da177e4SLinus Torvalds init_rootfs(); 21481da177e4SLinus Torvalds init_mount_tree(); 21491da177e4SLinus Torvalds } 21501da177e4SLinus Torvalds 21516b3286edSKirill Korotaev void __put_mnt_ns(struct mnt_namespace *ns) 21521da177e4SLinus Torvalds { 21536b3286edSKirill Korotaev struct vfsmount *root = ns->root; 215470fbcdf4SRam Pai LIST_HEAD(umount_list); 21556b3286edSKirill Korotaev ns->root = NULL; 21561ce88cf4SMiklos Szeredi spin_unlock(&vfsmount_lock); 2157390c6843SRam Pai down_write(&namespace_sem); 21581da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 2159a05964f3SRam Pai umount_tree(root, 0, &umount_list); 21601da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 2161390c6843SRam Pai up_write(&namespace_sem); 216270fbcdf4SRam Pai release_mounts(&umount_list); 21636b3286edSKirill Korotaev kfree(ns); 21641da177e4SLinus Torvalds } 2165