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/acct.h> 1816f7e0feSRandy Dunlap #include <linux/capability.h> 193d733633SDave Hansen #include <linux/cpumask.h> 201da177e4SLinus Torvalds #include <linux/module.h> 21f20a9eadSAndrew Morton #include <linux/sysfs.h> 221da177e4SLinus Torvalds #include <linux/seq_file.h> 236b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 241da177e4SLinus Torvalds #include <linux/namei.h> 25b43f3cbdSAlexey Dobriyan #include <linux/nsproxy.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> 3073cd49ecSMiklos Szeredi #include <linux/idr.h> 315ad4e53bSAl Viro #include <linux/fs_struct.h> 322504c5d6SAndreas Gruenbacher #include <linux/fsnotify.h> 331da177e4SLinus Torvalds #include <asm/uaccess.h> 341da177e4SLinus Torvalds #include <asm/unistd.h> 3507b20889SRam Pai #include "pnode.h" 36948730b0SAdrian Bunk #include "internal.h" 371da177e4SLinus Torvalds 3813f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head)) 3913f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT) 4013f14b4dSEric Dumazet 411da177e4SLinus Torvalds /* spinlock for vfsmount related operations, inplace of dcache_lock */ 421da177e4SLinus Torvalds __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock); 431da177e4SLinus Torvalds 445addc5ddSAl Viro static int event; 4573cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 46719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 47f21f6220SAl Viro static int mnt_id_start = 0; 48f21f6220SAl Viro static int mnt_group_start = 1; 495addc5ddSAl Viro 50fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly; 51e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 52390c6843SRam Pai static struct rw_semaphore namespace_sem; 531da177e4SLinus Torvalds 54f87fd4c2SMiklos Szeredi /* /sys/fs */ 5500d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 5600d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 57f87fd4c2SMiklos Szeredi 581da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 611da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 6213f14b4dSEric Dumazet tmp = tmp + (tmp >> HASH_SHIFT); 6313f14b4dSEric Dumazet return tmp & (HASH_SIZE - 1); 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 663d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16) 673d733633SDave Hansen 6873cd49ecSMiklos Szeredi /* allocation is serialized by namespace_sem */ 6973cd49ecSMiklos Szeredi static int mnt_alloc_id(struct vfsmount *mnt) 7073cd49ecSMiklos Szeredi { 7173cd49ecSMiklos Szeredi int res; 7273cd49ecSMiklos Szeredi 7373cd49ecSMiklos Szeredi retry: 7473cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 7573cd49ecSMiklos Szeredi spin_lock(&vfsmount_lock); 76f21f6220SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 77f21f6220SAl Viro if (!res) 78f21f6220SAl Viro mnt_id_start = mnt->mnt_id + 1; 7973cd49ecSMiklos Szeredi spin_unlock(&vfsmount_lock); 8073cd49ecSMiklos Szeredi if (res == -EAGAIN) 8173cd49ecSMiklos Szeredi goto retry; 8273cd49ecSMiklos Szeredi 8373cd49ecSMiklos Szeredi return res; 8473cd49ecSMiklos Szeredi } 8573cd49ecSMiklos Szeredi 8673cd49ecSMiklos Szeredi static void mnt_free_id(struct vfsmount *mnt) 8773cd49ecSMiklos Szeredi { 88f21f6220SAl Viro int id = mnt->mnt_id; 8973cd49ecSMiklos Szeredi spin_lock(&vfsmount_lock); 90f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 91f21f6220SAl Viro if (mnt_id_start > id) 92f21f6220SAl Viro mnt_id_start = id; 9373cd49ecSMiklos Szeredi spin_unlock(&vfsmount_lock); 9473cd49ecSMiklos Szeredi } 9573cd49ecSMiklos Szeredi 96719f5d7fSMiklos Szeredi /* 97719f5d7fSMiklos Szeredi * Allocate a new peer group ID 98719f5d7fSMiklos Szeredi * 99719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 100719f5d7fSMiklos Szeredi */ 101719f5d7fSMiklos Szeredi static int mnt_alloc_group_id(struct vfsmount *mnt) 102719f5d7fSMiklos Szeredi { 103f21f6220SAl Viro int res; 104f21f6220SAl Viro 105719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 106719f5d7fSMiklos Szeredi return -ENOMEM; 107719f5d7fSMiklos Szeredi 108f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 109f21f6220SAl Viro mnt_group_start, 110f21f6220SAl Viro &mnt->mnt_group_id); 111f21f6220SAl Viro if (!res) 112f21f6220SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 113f21f6220SAl Viro 114f21f6220SAl Viro return res; 115719f5d7fSMiklos Szeredi } 116719f5d7fSMiklos Szeredi 117719f5d7fSMiklos Szeredi /* 118719f5d7fSMiklos Szeredi * Release a peer group ID 119719f5d7fSMiklos Szeredi */ 120719f5d7fSMiklos Szeredi void mnt_release_group_id(struct vfsmount *mnt) 121719f5d7fSMiklos Szeredi { 122f21f6220SAl Viro int id = mnt->mnt_group_id; 123f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 124f21f6220SAl Viro if (mnt_group_start > id) 125f21f6220SAl Viro mnt_group_start = id; 126719f5d7fSMiklos Szeredi mnt->mnt_group_id = 0; 127719f5d7fSMiklos Szeredi } 128719f5d7fSMiklos Szeredi 1291da177e4SLinus Torvalds struct vfsmount *alloc_vfsmnt(const char *name) 1301da177e4SLinus Torvalds { 131c3762229SRobert P. J. Day struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 1321da177e4SLinus Torvalds if (mnt) { 13373cd49ecSMiklos Szeredi int err; 13473cd49ecSMiklos Szeredi 13573cd49ecSMiklos Szeredi err = mnt_alloc_id(mnt); 13688b38782SLi Zefan if (err) 13788b38782SLi Zefan goto out_free_cache; 13888b38782SLi Zefan 13988b38782SLi Zefan if (name) { 14088b38782SLi Zefan mnt->mnt_devname = kstrdup(name, GFP_KERNEL); 14188b38782SLi Zefan if (!mnt->mnt_devname) 14288b38782SLi Zefan goto out_free_id; 14373cd49ecSMiklos Szeredi } 14473cd49ecSMiklos Szeredi 1451da177e4SLinus Torvalds atomic_set(&mnt->mnt_count, 1); 1461da177e4SLinus Torvalds INIT_LIST_HEAD(&mnt->mnt_hash); 1471da177e4SLinus Torvalds INIT_LIST_HEAD(&mnt->mnt_child); 1481da177e4SLinus Torvalds INIT_LIST_HEAD(&mnt->mnt_mounts); 1491da177e4SLinus Torvalds INIT_LIST_HEAD(&mnt->mnt_list); 15055e700b9SMiklos Szeredi INIT_LIST_HEAD(&mnt->mnt_expire); 15103e06e68SRam Pai INIT_LIST_HEAD(&mnt->mnt_share); 152a58b0eb8SRam Pai INIT_LIST_HEAD(&mnt->mnt_slave_list); 153a58b0eb8SRam Pai INIT_LIST_HEAD(&mnt->mnt_slave); 1542504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 1552504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 1562504c5d6SAndreas Gruenbacher #endif 157d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 158d3ef3d73Snpiggin@suse.de mnt->mnt_writers = alloc_percpu(int); 159d3ef3d73Snpiggin@suse.de if (!mnt->mnt_writers) 160d3ef3d73Snpiggin@suse.de goto out_free_devname; 161d3ef3d73Snpiggin@suse.de #else 162d3ef3d73Snpiggin@suse.de mnt->mnt_writers = 0; 163d3ef3d73Snpiggin@suse.de #endif 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds return mnt; 16688b38782SLi Zefan 167d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 168d3ef3d73Snpiggin@suse.de out_free_devname: 169d3ef3d73Snpiggin@suse.de kfree(mnt->mnt_devname); 170d3ef3d73Snpiggin@suse.de #endif 17188b38782SLi Zefan out_free_id: 17288b38782SLi Zefan mnt_free_id(mnt); 17388b38782SLi Zefan out_free_cache: 17488b38782SLi Zefan kmem_cache_free(mnt_cache, mnt); 17588b38782SLi Zefan return NULL; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1788366025eSDave Hansen /* 1798366025eSDave Hansen * Most r/o checks on a fs are for operations that take 1808366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 1818366025eSDave Hansen * We must keep track of when those operations start 1828366025eSDave Hansen * (for permission checks) and when they end, so that 1838366025eSDave Hansen * we can determine when writes are able to occur to 1848366025eSDave Hansen * a filesystem. 1858366025eSDave Hansen */ 1863d733633SDave Hansen /* 1873d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 1883d733633SDave Hansen * @mnt: the mount to check for its write status 1893d733633SDave Hansen * 1903d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 1913d733633SDave Hansen * It does not guarantee that the filesystem will stay 1923d733633SDave Hansen * r/w, just that it is right *now*. This can not and 1933d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 1943d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 1953d733633SDave Hansen * r/w. 1963d733633SDave Hansen */ 1973d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 1983d733633SDave Hansen { 1992e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2002e4b7fcdSDave Hansen return 1; 2012e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2022e4b7fcdSDave Hansen return 1; 2032e4b7fcdSDave Hansen return 0; 2043d733633SDave Hansen } 2053d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2063d733633SDave Hansen 207d3ef3d73Snpiggin@suse.de static inline void inc_mnt_writers(struct vfsmount *mnt) 2083d733633SDave Hansen { 209d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 210d3ef3d73Snpiggin@suse.de (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))++; 211d3ef3d73Snpiggin@suse.de #else 212d3ef3d73Snpiggin@suse.de mnt->mnt_writers++; 213d3ef3d73Snpiggin@suse.de #endif 2143d733633SDave Hansen } 2153d733633SDave Hansen 216d3ef3d73Snpiggin@suse.de static inline void dec_mnt_writers(struct vfsmount *mnt) 2173d733633SDave Hansen { 218d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 219d3ef3d73Snpiggin@suse.de (*per_cpu_ptr(mnt->mnt_writers, smp_processor_id()))--; 220d3ef3d73Snpiggin@suse.de #else 221d3ef3d73Snpiggin@suse.de mnt->mnt_writers--; 222d3ef3d73Snpiggin@suse.de #endif 223d3ef3d73Snpiggin@suse.de } 224d3ef3d73Snpiggin@suse.de 225d3ef3d73Snpiggin@suse.de static unsigned int count_mnt_writers(struct vfsmount *mnt) 226d3ef3d73Snpiggin@suse.de { 227d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 228d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2293d733633SDave Hansen int cpu; 2303d733633SDave Hansen 2313d733633SDave Hansen for_each_possible_cpu(cpu) { 232d3ef3d73Snpiggin@suse.de count += *per_cpu_ptr(mnt->mnt_writers, cpu); 2333d733633SDave Hansen } 2343d733633SDave Hansen 235d3ef3d73Snpiggin@suse.de return count; 236d3ef3d73Snpiggin@suse.de #else 237d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 238d3ef3d73Snpiggin@suse.de #endif 2393d733633SDave Hansen } 2403d733633SDave Hansen 2413d733633SDave Hansen /* 2423d733633SDave Hansen * Most r/o checks on a fs are for operations that take 2433d733633SDave Hansen * discrete amounts of time, like a write() or unlink(). 2443d733633SDave Hansen * We must keep track of when those operations start 2453d733633SDave Hansen * (for permission checks) and when they end, so that 2463d733633SDave Hansen * we can determine when writes are able to occur to 2473d733633SDave Hansen * a filesystem. 2483d733633SDave Hansen */ 2498366025eSDave Hansen /** 2508366025eSDave Hansen * mnt_want_write - get write access to a mount 2518366025eSDave Hansen * @mnt: the mount on which to take a write 2528366025eSDave Hansen * 2538366025eSDave Hansen * This tells the low-level filesystem that a write is 2548366025eSDave Hansen * about to be performed to it, and makes sure that 2558366025eSDave Hansen * writes are allowed before returning success. When 2568366025eSDave Hansen * the write operation is finished, mnt_drop_write() 2578366025eSDave Hansen * must be called. This is effectively a refcount. 2588366025eSDave Hansen */ 2598366025eSDave Hansen int mnt_want_write(struct vfsmount *mnt) 2608366025eSDave Hansen { 2613d733633SDave Hansen int ret = 0; 2623d733633SDave Hansen 263d3ef3d73Snpiggin@suse.de preempt_disable(); 264d3ef3d73Snpiggin@suse.de inc_mnt_writers(mnt); 265d3ef3d73Snpiggin@suse.de /* 266d3ef3d73Snpiggin@suse.de * The store to inc_mnt_writers must be visible before we pass 267d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 268d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 269d3ef3d73Snpiggin@suse.de */ 270d3ef3d73Snpiggin@suse.de smp_mb(); 271d3ef3d73Snpiggin@suse.de while (mnt->mnt_flags & MNT_WRITE_HOLD) 272d3ef3d73Snpiggin@suse.de cpu_relax(); 273d3ef3d73Snpiggin@suse.de /* 274d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 275d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 276d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 277d3ef3d73Snpiggin@suse.de */ 278d3ef3d73Snpiggin@suse.de smp_rmb(); 2793d733633SDave Hansen if (__mnt_is_readonly(mnt)) { 280d3ef3d73Snpiggin@suse.de dec_mnt_writers(mnt); 2813d733633SDave Hansen ret = -EROFS; 2823d733633SDave Hansen goto out; 2833d733633SDave Hansen } 2843d733633SDave Hansen out: 285d3ef3d73Snpiggin@suse.de preempt_enable(); 2863d733633SDave Hansen return ret; 2878366025eSDave Hansen } 2888366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 2898366025eSDave Hansen 2908366025eSDave Hansen /** 29196029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 29296029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 29396029c4eSnpiggin@suse.de * 29496029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 29596029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 29696029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 29796029c4eSnpiggin@suse.de * on it. This allows some optimisation. 29896029c4eSnpiggin@suse.de * 29996029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 30096029c4eSnpiggin@suse.de * drop the reference. 30196029c4eSnpiggin@suse.de */ 30296029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 30396029c4eSnpiggin@suse.de { 30496029c4eSnpiggin@suse.de /* superblock may be r/o */ 30596029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 30696029c4eSnpiggin@suse.de return -EROFS; 30796029c4eSnpiggin@suse.de preempt_disable(); 30896029c4eSnpiggin@suse.de inc_mnt_writers(mnt); 30996029c4eSnpiggin@suse.de preempt_enable(); 31096029c4eSnpiggin@suse.de return 0; 31196029c4eSnpiggin@suse.de } 31296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 31396029c4eSnpiggin@suse.de 31496029c4eSnpiggin@suse.de /** 31596029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 31696029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 31796029c4eSnpiggin@suse.de * 31896029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 31996029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 32096029c4eSnpiggin@suse.de */ 32196029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 32296029c4eSnpiggin@suse.de { 3232d8dd38aSOGAWA Hirofumi struct inode *inode = file->f_dentry->d_inode; 3242d8dd38aSOGAWA Hirofumi if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 32596029c4eSnpiggin@suse.de return mnt_want_write(file->f_path.mnt); 32696029c4eSnpiggin@suse.de else 32796029c4eSnpiggin@suse.de return mnt_clone_write(file->f_path.mnt); 32896029c4eSnpiggin@suse.de } 32996029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 33096029c4eSnpiggin@suse.de 33196029c4eSnpiggin@suse.de /** 3328366025eSDave Hansen * mnt_drop_write - give up write access to a mount 3338366025eSDave Hansen * @mnt: the mount on which to give up write access 3348366025eSDave Hansen * 3358366025eSDave Hansen * Tells the low-level filesystem that we are done 3368366025eSDave Hansen * performing writes to it. Must be matched with 3378366025eSDave Hansen * mnt_want_write() call above. 3388366025eSDave Hansen */ 3398366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt) 3408366025eSDave Hansen { 341d3ef3d73Snpiggin@suse.de preempt_disable(); 342d3ef3d73Snpiggin@suse.de dec_mnt_writers(mnt); 343d3ef3d73Snpiggin@suse.de preempt_enable(); 3448366025eSDave Hansen } 3458366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 3468366025eSDave Hansen 3472e4b7fcdSDave Hansen static int mnt_make_readonly(struct vfsmount *mnt) 3488366025eSDave Hansen { 3493d733633SDave Hansen int ret = 0; 3503d733633SDave Hansen 3512e4b7fcdSDave Hansen spin_lock(&vfsmount_lock); 352d3ef3d73Snpiggin@suse.de mnt->mnt_flags |= MNT_WRITE_HOLD; 353d3ef3d73Snpiggin@suse.de /* 354d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 355d3ef3d73Snpiggin@suse.de * should be visible before we do. 356d3ef3d73Snpiggin@suse.de */ 357d3ef3d73Snpiggin@suse.de smp_mb(); 358d3ef3d73Snpiggin@suse.de 359d3ef3d73Snpiggin@suse.de /* 360d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 361d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 362d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 363d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 364d3ef3d73Snpiggin@suse.de * 365d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 366d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 367d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 368d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 369d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 370d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 371d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 372d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 373d3ef3d73Snpiggin@suse.de * we're counting up here. 374d3ef3d73Snpiggin@suse.de */ 375d3ef3d73Snpiggin@suse.de if (count_mnt_writers(mnt) > 0) 376d3ef3d73Snpiggin@suse.de ret = -EBUSY; 377d3ef3d73Snpiggin@suse.de else 3782e4b7fcdSDave Hansen mnt->mnt_flags |= MNT_READONLY; 379d3ef3d73Snpiggin@suse.de /* 380d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 381d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 382d3ef3d73Snpiggin@suse.de */ 383d3ef3d73Snpiggin@suse.de smp_wmb(); 384d3ef3d73Snpiggin@suse.de mnt->mnt_flags &= ~MNT_WRITE_HOLD; 3852e4b7fcdSDave Hansen spin_unlock(&vfsmount_lock); 3863d733633SDave Hansen return ret; 3873d733633SDave Hansen } 3888366025eSDave Hansen 3892e4b7fcdSDave Hansen static void __mnt_unmake_readonly(struct vfsmount *mnt) 3902e4b7fcdSDave Hansen { 3912e4b7fcdSDave Hansen spin_lock(&vfsmount_lock); 3922e4b7fcdSDave Hansen mnt->mnt_flags &= ~MNT_READONLY; 3932e4b7fcdSDave Hansen spin_unlock(&vfsmount_lock); 3942e4b7fcdSDave Hansen } 3952e4b7fcdSDave Hansen 396a3ec947cSSukadev Bhattiprolu void simple_set_mnt(struct vfsmount *mnt, struct super_block *sb) 397454e2398SDavid Howells { 398454e2398SDavid Howells mnt->mnt_sb = sb; 399454e2398SDavid Howells mnt->mnt_root = dget(sb->s_root); 400454e2398SDavid Howells } 401454e2398SDavid Howells 402454e2398SDavid Howells EXPORT_SYMBOL(simple_set_mnt); 403454e2398SDavid Howells 4041da177e4SLinus Torvalds void free_vfsmnt(struct vfsmount *mnt) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds kfree(mnt->mnt_devname); 40773cd49ecSMiklos Szeredi mnt_free_id(mnt); 408d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 409d3ef3d73Snpiggin@suse.de free_percpu(mnt->mnt_writers); 410d3ef3d73Snpiggin@suse.de #endif 4111da177e4SLinus Torvalds kmem_cache_free(mnt_cache, mnt); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /* 415a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 416a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 4171da177e4SLinus Torvalds */ 418a05964f3SRam Pai struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 419a05964f3SRam Pai int dir) 4201da177e4SLinus Torvalds { 4211da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 4221da177e4SLinus Torvalds struct list_head *tmp = head; 4231da177e4SLinus Torvalds struct vfsmount *p, *found = NULL; 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds for (;;) { 426a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 4271da177e4SLinus Torvalds p = NULL; 4281da177e4SLinus Torvalds if (tmp == head) 4291da177e4SLinus Torvalds break; 4301da177e4SLinus Torvalds p = list_entry(tmp, struct vfsmount, mnt_hash); 4311da177e4SLinus Torvalds if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) { 432a05964f3SRam Pai found = p; 4331da177e4SLinus Torvalds break; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds return found; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 439a05964f3SRam Pai /* 440a05964f3SRam Pai * lookup_mnt increments the ref count before returning 441a05964f3SRam Pai * the vfsmount struct. 442a05964f3SRam Pai */ 4431c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 444a05964f3SRam Pai { 445a05964f3SRam Pai struct vfsmount *child_mnt; 446a05964f3SRam Pai spin_lock(&vfsmount_lock); 4471c755af4SAl Viro if ((child_mnt = __lookup_mnt(path->mnt, path->dentry, 1))) 448a05964f3SRam Pai mntget(child_mnt); 449a05964f3SRam Pai spin_unlock(&vfsmount_lock); 450a05964f3SRam Pai return child_mnt; 451a05964f3SRam Pai } 452a05964f3SRam Pai 4531da177e4SLinus Torvalds static inline int check_mnt(struct vfsmount *mnt) 4541da177e4SLinus Torvalds { 4556b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4586b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 4595addc5ddSAl Viro { 4605addc5ddSAl Viro if (ns) { 4615addc5ddSAl Viro ns->event = ++event; 4625addc5ddSAl Viro wake_up_interruptible(&ns->poll); 4635addc5ddSAl Viro } 4645addc5ddSAl Viro } 4655addc5ddSAl Viro 4666b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 4675addc5ddSAl Viro { 4685addc5ddSAl Viro if (ns && ns->event != event) { 4695addc5ddSAl Viro ns->event = event; 4705addc5ddSAl Viro wake_up_interruptible(&ns->poll); 4715addc5ddSAl Viro } 4725addc5ddSAl Viro } 4735addc5ddSAl Viro 4741a390689SAl Viro static void detach_mnt(struct vfsmount *mnt, struct path *old_path) 4751da177e4SLinus Torvalds { 4761a390689SAl Viro old_path->dentry = mnt->mnt_mountpoint; 4771a390689SAl Viro old_path->mnt = mnt->mnt_parent; 4781da177e4SLinus Torvalds mnt->mnt_parent = mnt; 4791da177e4SLinus Torvalds mnt->mnt_mountpoint = mnt->mnt_root; 4801da177e4SLinus Torvalds list_del_init(&mnt->mnt_child); 4811da177e4SLinus Torvalds list_del_init(&mnt->mnt_hash); 4821a390689SAl Viro old_path->dentry->d_mounted--; 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 485b90fa9aeSRam Pai void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry, 486b90fa9aeSRam Pai struct vfsmount *child_mnt) 487b90fa9aeSRam Pai { 488b90fa9aeSRam Pai child_mnt->mnt_parent = mntget(mnt); 489b90fa9aeSRam Pai child_mnt->mnt_mountpoint = dget(dentry); 490b90fa9aeSRam Pai dentry->d_mounted++; 491b90fa9aeSRam Pai } 492b90fa9aeSRam Pai 4931a390689SAl Viro static void attach_mnt(struct vfsmount *mnt, struct path *path) 4941da177e4SLinus Torvalds { 4951a390689SAl Viro mnt_set_mountpoint(path->mnt, path->dentry, mnt); 496b90fa9aeSRam Pai list_add_tail(&mnt->mnt_hash, mount_hashtable + 4971a390689SAl Viro hash(path->mnt, path->dentry)); 4981a390689SAl Viro list_add_tail(&mnt->mnt_child, &path->mnt->mnt_mounts); 499b90fa9aeSRam Pai } 500b90fa9aeSRam Pai 501b90fa9aeSRam Pai /* 502b90fa9aeSRam Pai * the caller must hold vfsmount_lock 503b90fa9aeSRam Pai */ 504b90fa9aeSRam Pai static void commit_tree(struct vfsmount *mnt) 505b90fa9aeSRam Pai { 506b90fa9aeSRam Pai struct vfsmount *parent = mnt->mnt_parent; 507b90fa9aeSRam Pai struct vfsmount *m; 508b90fa9aeSRam Pai LIST_HEAD(head); 5096b3286edSKirill Korotaev struct mnt_namespace *n = parent->mnt_ns; 510b90fa9aeSRam Pai 511b90fa9aeSRam Pai BUG_ON(parent == mnt); 512b90fa9aeSRam Pai 513b90fa9aeSRam Pai list_add_tail(&head, &mnt->mnt_list); 514b90fa9aeSRam Pai list_for_each_entry(m, &head, mnt_list) 5156b3286edSKirill Korotaev m->mnt_ns = n; 516b90fa9aeSRam Pai list_splice(&head, n->list.prev); 517b90fa9aeSRam Pai 518b90fa9aeSRam Pai list_add_tail(&mnt->mnt_hash, mount_hashtable + 519b90fa9aeSRam Pai hash(parent, mnt->mnt_mountpoint)); 520b90fa9aeSRam Pai list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 5216b3286edSKirill Korotaev touch_mnt_namespace(n); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root) 5251da177e4SLinus Torvalds { 5261da177e4SLinus Torvalds struct list_head *next = p->mnt_mounts.next; 5271da177e4SLinus Torvalds if (next == &p->mnt_mounts) { 5281da177e4SLinus Torvalds while (1) { 5291da177e4SLinus Torvalds if (p == root) 5301da177e4SLinus Torvalds return NULL; 5311da177e4SLinus Torvalds next = p->mnt_child.next; 5321da177e4SLinus Torvalds if (next != &p->mnt_parent->mnt_mounts) 5331da177e4SLinus Torvalds break; 5341da177e4SLinus Torvalds p = p->mnt_parent; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds return list_entry(next, struct vfsmount, mnt_child); 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5409676f0c6SRam Pai static struct vfsmount *skip_mnt_tree(struct vfsmount *p) 5419676f0c6SRam Pai { 5429676f0c6SRam Pai struct list_head *prev = p->mnt_mounts.prev; 5439676f0c6SRam Pai while (prev != &p->mnt_mounts) { 5449676f0c6SRam Pai p = list_entry(prev, struct vfsmount, mnt_child); 5459676f0c6SRam Pai prev = p->mnt_mounts.prev; 5469676f0c6SRam Pai } 5479676f0c6SRam Pai return p; 5489676f0c6SRam Pai } 5499676f0c6SRam Pai 55036341f64SRam Pai static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root, 55136341f64SRam Pai int flag) 5521da177e4SLinus Torvalds { 5531da177e4SLinus Torvalds struct super_block *sb = old->mnt_sb; 5541da177e4SLinus Torvalds struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname); 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds if (mnt) { 557719f5d7fSMiklos Szeredi if (flag & (CL_SLAVE | CL_PRIVATE)) 558719f5d7fSMiklos Szeredi mnt->mnt_group_id = 0; /* not a peer of original */ 559719f5d7fSMiklos Szeredi else 560719f5d7fSMiklos Szeredi mnt->mnt_group_id = old->mnt_group_id; 561719f5d7fSMiklos Szeredi 562719f5d7fSMiklos Szeredi if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 563719f5d7fSMiklos Szeredi int err = mnt_alloc_group_id(mnt); 564719f5d7fSMiklos Szeredi if (err) 565719f5d7fSMiklos Szeredi goto out_free; 566719f5d7fSMiklos Szeredi } 567719f5d7fSMiklos Szeredi 5681da177e4SLinus Torvalds mnt->mnt_flags = old->mnt_flags; 5691da177e4SLinus Torvalds atomic_inc(&sb->s_active); 5701da177e4SLinus Torvalds mnt->mnt_sb = sb; 5711da177e4SLinus Torvalds mnt->mnt_root = dget(root); 5721da177e4SLinus Torvalds mnt->mnt_mountpoint = mnt->mnt_root; 5731da177e4SLinus Torvalds mnt->mnt_parent = mnt; 574b90fa9aeSRam Pai 5755afe0022SRam Pai if (flag & CL_SLAVE) { 5765afe0022SRam Pai list_add(&mnt->mnt_slave, &old->mnt_slave_list); 5775afe0022SRam Pai mnt->mnt_master = old; 5785afe0022SRam Pai CLEAR_MNT_SHARED(mnt); 5798aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 580796a6b52SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 581b90fa9aeSRam Pai list_add(&mnt->mnt_share, &old->mnt_share); 5825afe0022SRam Pai if (IS_MNT_SLAVE(old)) 5835afe0022SRam Pai list_add(&mnt->mnt_slave, &old->mnt_slave); 5845afe0022SRam Pai mnt->mnt_master = old->mnt_master; 5855afe0022SRam Pai } 586b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 587b90fa9aeSRam Pai set_mnt_shared(mnt); 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 5901da177e4SLinus Torvalds * as the original if that was on one */ 59136341f64SRam Pai if (flag & CL_EXPIRE) { 59255e700b9SMiklos Szeredi if (!list_empty(&old->mnt_expire)) 59355e700b9SMiklos Szeredi list_add(&mnt->mnt_expire, &old->mnt_expire); 5941da177e4SLinus Torvalds } 59536341f64SRam Pai } 5961da177e4SLinus Torvalds return mnt; 597719f5d7fSMiklos Szeredi 598719f5d7fSMiklos Szeredi out_free: 599719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 600719f5d7fSMiklos Szeredi return NULL; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6037b7b1aceSAl Viro static inline void __mntput(struct vfsmount *mnt) 6041da177e4SLinus Torvalds { 6051da177e4SLinus Torvalds struct super_block *sb = mnt->mnt_sb; 6063d733633SDave Hansen /* 6073d733633SDave Hansen * This probably indicates that somebody messed 6083d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 6093d733633SDave Hansen * happens, the filesystem was probably unable 6103d733633SDave Hansen * to make r/w->r/o transitions. 6113d733633SDave Hansen */ 612d3ef3d73Snpiggin@suse.de /* 613d3ef3d73Snpiggin@suse.de * atomic_dec_and_lock() used to deal with ->mnt_count decrements 614d3ef3d73Snpiggin@suse.de * provides barriers, so count_mnt_writers() below is safe. AV 615d3ef3d73Snpiggin@suse.de */ 616d3ef3d73Snpiggin@suse.de WARN_ON(count_mnt_writers(mnt)); 617ca9c726eSAndreas Gruenbacher fsnotify_vfsmount_delete(mnt); 6181da177e4SLinus Torvalds dput(mnt->mnt_root); 6191da177e4SLinus Torvalds free_vfsmnt(mnt); 6201da177e4SLinus Torvalds deactivate_super(sb); 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6237b7b1aceSAl Viro void mntput_no_expire(struct vfsmount *mnt) 6247b7b1aceSAl Viro { 6257b7b1aceSAl Viro repeat: 6267b7b1aceSAl Viro if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) { 6277b7b1aceSAl Viro if (likely(!mnt->mnt_pinned)) { 6287b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 6297b7b1aceSAl Viro __mntput(mnt); 6307b7b1aceSAl Viro return; 6317b7b1aceSAl Viro } 6327b7b1aceSAl Viro atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count); 6337b7b1aceSAl Viro mnt->mnt_pinned = 0; 6347b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 6357b7b1aceSAl Viro acct_auto_close_mnt(mnt); 6367b7b1aceSAl Viro goto repeat; 6377b7b1aceSAl Viro } 6387b7b1aceSAl Viro } 6397b7b1aceSAl Viro 6407b7b1aceSAl Viro EXPORT_SYMBOL(mntput_no_expire); 6417b7b1aceSAl Viro 6427b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 6437b7b1aceSAl Viro { 6447b7b1aceSAl Viro spin_lock(&vfsmount_lock); 6457b7b1aceSAl Viro mnt->mnt_pinned++; 6467b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 6477b7b1aceSAl Viro } 6487b7b1aceSAl Viro 6497b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 6507b7b1aceSAl Viro 6517b7b1aceSAl Viro void mnt_unpin(struct vfsmount *mnt) 6527b7b1aceSAl Viro { 6537b7b1aceSAl Viro spin_lock(&vfsmount_lock); 6547b7b1aceSAl Viro if (mnt->mnt_pinned) { 6557b7b1aceSAl Viro atomic_inc(&mnt->mnt_count); 6567b7b1aceSAl Viro mnt->mnt_pinned--; 6577b7b1aceSAl Viro } 6587b7b1aceSAl Viro spin_unlock(&vfsmount_lock); 6597b7b1aceSAl Viro } 6607b7b1aceSAl Viro 6617b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 6621da177e4SLinus Torvalds 663b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 664b3b304a2SMiklos Szeredi { 665b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 666b3b304a2SMiklos Szeredi } 667b3b304a2SMiklos Szeredi 668b3b304a2SMiklos Szeredi /* 669b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 670b3b304a2SMiklos Szeredi * implement more complex mount option showing. 671b3b304a2SMiklos Szeredi * 672b3b304a2SMiklos Szeredi * See also save_mount_options(). 673b3b304a2SMiklos Szeredi */ 674b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt) 675b3b304a2SMiklos Szeredi { 6762a32cebdSAl Viro const char *options; 6772a32cebdSAl Viro 6782a32cebdSAl Viro rcu_read_lock(); 6792a32cebdSAl Viro options = rcu_dereference(mnt->mnt_sb->s_options); 680b3b304a2SMiklos Szeredi 681b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 682b3b304a2SMiklos Szeredi seq_putc(m, ','); 683b3b304a2SMiklos Szeredi mangle(m, options); 684b3b304a2SMiklos Szeredi } 6852a32cebdSAl Viro rcu_read_unlock(); 686b3b304a2SMiklos Szeredi 687b3b304a2SMiklos Szeredi return 0; 688b3b304a2SMiklos Szeredi } 689b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 690b3b304a2SMiklos Szeredi 691b3b304a2SMiklos Szeredi /* 692b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 693b3b304a2SMiklos Szeredi * called from the fill_super() callback. 694b3b304a2SMiklos Szeredi * 695b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 696b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 697b3b304a2SMiklos Szeredi * remount fails. 698b3b304a2SMiklos Szeredi * 699b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 700b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 701b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 702b3b304a2SMiklos Szeredi * any more. 703b3b304a2SMiklos Szeredi */ 704b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 705b3b304a2SMiklos Szeredi { 7062a32cebdSAl Viro BUG_ON(sb->s_options); 7072a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 708b3b304a2SMiklos Szeredi } 709b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 710b3b304a2SMiklos Szeredi 7112a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 7122a32cebdSAl Viro { 7132a32cebdSAl Viro char *old = sb->s_options; 7142a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 7152a32cebdSAl Viro if (old) { 7162a32cebdSAl Viro synchronize_rcu(); 7172a32cebdSAl Viro kfree(old); 7182a32cebdSAl Viro } 7192a32cebdSAl Viro } 7202a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 7212a32cebdSAl Viro 722a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 7231da177e4SLinus Torvalds /* iterator */ 7241da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 7251da177e4SLinus Torvalds { 726a1a2c409SMiklos Szeredi struct proc_mounts *p = m->private; 7271da177e4SLinus Torvalds 728390c6843SRam Pai down_read(&namespace_sem); 729a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 7331da177e4SLinus Torvalds { 734a1a2c409SMiklos Szeredi struct proc_mounts *p = m->private; 735b0765fb8SPavel Emelianov 736a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 7401da177e4SLinus Torvalds { 741390c6843SRam Pai up_read(&namespace_sem); 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 7449f5596afSAl Viro int mnt_had_events(struct proc_mounts *p) 7459f5596afSAl Viro { 7469f5596afSAl Viro struct mnt_namespace *ns = p->ns; 7479f5596afSAl Viro int res = 0; 7489f5596afSAl Viro 7499f5596afSAl Viro spin_lock(&vfsmount_lock); 7509f5596afSAl Viro if (p->event != ns->event) { 7519f5596afSAl Viro p->event = ns->event; 7529f5596afSAl Viro res = 1; 7539f5596afSAl Viro } 7549f5596afSAl Viro spin_unlock(&vfsmount_lock); 7559f5596afSAl Viro 7569f5596afSAl Viro return res; 7579f5596afSAl Viro } 7589f5596afSAl Viro 7592d4d4864SRam Pai struct proc_fs_info { 7601da177e4SLinus Torvalds int flag; 7612d4d4864SRam Pai const char *str; 7622d4d4864SRam Pai }; 7632d4d4864SRam Pai 7642069f457SEric Paris static int show_sb_opts(struct seq_file *m, struct super_block *sb) 7652d4d4864SRam Pai { 7662d4d4864SRam Pai static const struct proc_fs_info fs_info[] = { 7671da177e4SLinus Torvalds { MS_SYNCHRONOUS, ",sync" }, 7681da177e4SLinus Torvalds { MS_DIRSYNC, ",dirsync" }, 7691da177e4SLinus Torvalds { MS_MANDLOCK, ",mand" }, 7701da177e4SLinus Torvalds { 0, NULL } 7711da177e4SLinus Torvalds }; 7722d4d4864SRam Pai const struct proc_fs_info *fs_infop; 7732d4d4864SRam Pai 7742d4d4864SRam Pai for (fs_infop = fs_info; fs_infop->flag; fs_infop++) { 7752d4d4864SRam Pai if (sb->s_flags & fs_infop->flag) 7762d4d4864SRam Pai seq_puts(m, fs_infop->str); 7772d4d4864SRam Pai } 7782069f457SEric Paris 7792069f457SEric Paris return security_sb_show_options(m, sb); 7802d4d4864SRam Pai } 7812d4d4864SRam Pai 7822d4d4864SRam Pai static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt) 7832d4d4864SRam Pai { 7842d4d4864SRam Pai static const struct proc_fs_info mnt_info[] = { 7851da177e4SLinus Torvalds { MNT_NOSUID, ",nosuid" }, 7861da177e4SLinus Torvalds { MNT_NODEV, ",nodev" }, 7871da177e4SLinus Torvalds { MNT_NOEXEC, ",noexec" }, 788fc33a7bbSChristoph Hellwig { MNT_NOATIME, ",noatime" }, 789fc33a7bbSChristoph Hellwig { MNT_NODIRATIME, ",nodiratime" }, 79047ae32d6SValerie Henson { MNT_RELATIME, ",relatime" }, 791d0adde57SMatthew Garrett { MNT_STRICTATIME, ",strictatime" }, 7921da177e4SLinus Torvalds { 0, NULL } 7931da177e4SLinus Torvalds }; 7942d4d4864SRam Pai const struct proc_fs_info *fs_infop; 7952d4d4864SRam Pai 7962d4d4864SRam Pai for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) { 7972d4d4864SRam Pai if (mnt->mnt_flags & fs_infop->flag) 7982d4d4864SRam Pai seq_puts(m, fs_infop->str); 7992d4d4864SRam Pai } 8002d4d4864SRam Pai } 8012d4d4864SRam Pai 8022d4d4864SRam Pai static void show_type(struct seq_file *m, struct super_block *sb) 8032d4d4864SRam Pai { 8042d4d4864SRam Pai mangle(m, sb->s_type->name); 8052d4d4864SRam Pai if (sb->s_subtype && sb->s_subtype[0]) { 8062d4d4864SRam Pai seq_putc(m, '.'); 8072d4d4864SRam Pai mangle(m, sb->s_subtype); 8082d4d4864SRam Pai } 8092d4d4864SRam Pai } 8102d4d4864SRam Pai 8112d4d4864SRam Pai static int show_vfsmnt(struct seq_file *m, void *v) 8122d4d4864SRam Pai { 8132d4d4864SRam Pai struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); 8142d4d4864SRam Pai int err = 0; 815c32c2f63SJan Blunck struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none"); 8181da177e4SLinus Torvalds seq_putc(m, ' '); 819c32c2f63SJan Blunck seq_path(m, &mnt_path, " \t\n\\"); 8201da177e4SLinus Torvalds seq_putc(m, ' '); 8212d4d4864SRam Pai show_type(m, mnt->mnt_sb); 8222e4b7fcdSDave Hansen seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw"); 8232069f457SEric Paris err = show_sb_opts(m, mnt->mnt_sb); 8242069f457SEric Paris if (err) 8252069f457SEric Paris goto out; 8262d4d4864SRam Pai show_mnt_opts(m, mnt); 8271da177e4SLinus Torvalds if (mnt->mnt_sb->s_op->show_options) 8281da177e4SLinus Torvalds err = mnt->mnt_sb->s_op->show_options(m, mnt); 8291da177e4SLinus Torvalds seq_puts(m, " 0 0\n"); 8302069f457SEric Paris out: 8311da177e4SLinus Torvalds return err; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 834a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 8351da177e4SLinus Torvalds .start = m_start, 8361da177e4SLinus Torvalds .next = m_next, 8371da177e4SLinus Torvalds .stop = m_stop, 8381da177e4SLinus Torvalds .show = show_vfsmnt 8391da177e4SLinus Torvalds }; 8401da177e4SLinus Torvalds 8412d4d4864SRam Pai static int show_mountinfo(struct seq_file *m, void *v) 8422d4d4864SRam Pai { 8432d4d4864SRam Pai struct proc_mounts *p = m->private; 8442d4d4864SRam Pai struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); 8452d4d4864SRam Pai struct super_block *sb = mnt->mnt_sb; 8462d4d4864SRam Pai struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; 8472d4d4864SRam Pai struct path root = p->root; 8482d4d4864SRam Pai int err = 0; 8492d4d4864SRam Pai 8502d4d4864SRam Pai seq_printf(m, "%i %i %u:%u ", mnt->mnt_id, mnt->mnt_parent->mnt_id, 8512d4d4864SRam Pai MAJOR(sb->s_dev), MINOR(sb->s_dev)); 8522d4d4864SRam Pai seq_dentry(m, mnt->mnt_root, " \t\n\\"); 8532d4d4864SRam Pai seq_putc(m, ' '); 8542d4d4864SRam Pai seq_path_root(m, &mnt_path, &root, " \t\n\\"); 8552d4d4864SRam Pai if (root.mnt != p->root.mnt || root.dentry != p->root.dentry) { 8562d4d4864SRam Pai /* 8572d4d4864SRam Pai * Mountpoint is outside root, discard that one. Ugly, 8582d4d4864SRam Pai * but less so than trying to do that in iterator in a 8592d4d4864SRam Pai * race-free way (due to renames). 8602d4d4864SRam Pai */ 8612d4d4864SRam Pai return SEQ_SKIP; 8622d4d4864SRam Pai } 8632d4d4864SRam Pai seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw"); 8642d4d4864SRam Pai show_mnt_opts(m, mnt); 8652d4d4864SRam Pai 8662d4d4864SRam Pai /* Tagged fields ("foo:X" or "bar") */ 8672d4d4864SRam Pai if (IS_MNT_SHARED(mnt)) 8682d4d4864SRam Pai seq_printf(m, " shared:%i", mnt->mnt_group_id); 86997e7e0f7SMiklos Szeredi if (IS_MNT_SLAVE(mnt)) { 87097e7e0f7SMiklos Szeredi int master = mnt->mnt_master->mnt_group_id; 87197e7e0f7SMiklos Szeredi int dom = get_dominating_id(mnt, &p->root); 87297e7e0f7SMiklos Szeredi seq_printf(m, " master:%i", master); 87397e7e0f7SMiklos Szeredi if (dom && dom != master) 87497e7e0f7SMiklos Szeredi seq_printf(m, " propagate_from:%i", dom); 87597e7e0f7SMiklos Szeredi } 8762d4d4864SRam Pai if (IS_MNT_UNBINDABLE(mnt)) 8772d4d4864SRam Pai seq_puts(m, " unbindable"); 8782d4d4864SRam Pai 8792d4d4864SRam Pai /* Filesystem specific data */ 8802d4d4864SRam Pai seq_puts(m, " - "); 8812d4d4864SRam Pai show_type(m, sb); 8822d4d4864SRam Pai seq_putc(m, ' '); 8832d4d4864SRam Pai mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none"); 8842d4d4864SRam Pai seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw"); 8852069f457SEric Paris err = show_sb_opts(m, sb); 8862069f457SEric Paris if (err) 8872069f457SEric Paris goto out; 8882d4d4864SRam Pai if (sb->s_op->show_options) 8892d4d4864SRam Pai err = sb->s_op->show_options(m, mnt); 8902d4d4864SRam Pai seq_putc(m, '\n'); 8912069f457SEric Paris out: 8922d4d4864SRam Pai return err; 8932d4d4864SRam Pai } 8942d4d4864SRam Pai 8952d4d4864SRam Pai const struct seq_operations mountinfo_op = { 8962d4d4864SRam Pai .start = m_start, 8972d4d4864SRam Pai .next = m_next, 8982d4d4864SRam Pai .stop = m_stop, 8992d4d4864SRam Pai .show = show_mountinfo, 9002d4d4864SRam Pai }; 9012d4d4864SRam Pai 902b4629fe2SChuck Lever static int show_vfsstat(struct seq_file *m, void *v) 903b4629fe2SChuck Lever { 904b0765fb8SPavel Emelianov struct vfsmount *mnt = list_entry(v, struct vfsmount, mnt_list); 905c32c2f63SJan Blunck struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; 906b4629fe2SChuck Lever int err = 0; 907b4629fe2SChuck Lever 908b4629fe2SChuck Lever /* device */ 909b4629fe2SChuck Lever if (mnt->mnt_devname) { 910b4629fe2SChuck Lever seq_puts(m, "device "); 911b4629fe2SChuck Lever mangle(m, mnt->mnt_devname); 912b4629fe2SChuck Lever } else 913b4629fe2SChuck Lever seq_puts(m, "no device"); 914b4629fe2SChuck Lever 915b4629fe2SChuck Lever /* mount point */ 916b4629fe2SChuck Lever seq_puts(m, " mounted on "); 917c32c2f63SJan Blunck seq_path(m, &mnt_path, " \t\n\\"); 918b4629fe2SChuck Lever seq_putc(m, ' '); 919b4629fe2SChuck Lever 920b4629fe2SChuck Lever /* file system type */ 921b4629fe2SChuck Lever seq_puts(m, "with fstype "); 9222d4d4864SRam Pai show_type(m, mnt->mnt_sb); 923b4629fe2SChuck Lever 924b4629fe2SChuck Lever /* optional statistics */ 925b4629fe2SChuck Lever if (mnt->mnt_sb->s_op->show_stats) { 926b4629fe2SChuck Lever seq_putc(m, ' '); 927b4629fe2SChuck Lever err = mnt->mnt_sb->s_op->show_stats(m, mnt); 928b4629fe2SChuck Lever } 929b4629fe2SChuck Lever 930b4629fe2SChuck Lever seq_putc(m, '\n'); 931b4629fe2SChuck Lever return err; 932b4629fe2SChuck Lever } 933b4629fe2SChuck Lever 934a1a2c409SMiklos Szeredi const struct seq_operations mountstats_op = { 935b4629fe2SChuck Lever .start = m_start, 936b4629fe2SChuck Lever .next = m_next, 937b4629fe2SChuck Lever .stop = m_stop, 938b4629fe2SChuck Lever .show = show_vfsstat, 939b4629fe2SChuck Lever }; 940a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 941b4629fe2SChuck Lever 9421da177e4SLinus Torvalds /** 9431da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 9441da177e4SLinus Torvalds * @mnt: root of mount tree 9451da177e4SLinus Torvalds * 9461da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 9471da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 9481da177e4SLinus Torvalds * busy. 9491da177e4SLinus Torvalds */ 9501da177e4SLinus Torvalds int may_umount_tree(struct vfsmount *mnt) 9511da177e4SLinus Torvalds { 95236341f64SRam Pai int actual_refs = 0; 95336341f64SRam Pai int minimum_refs = 0; 95436341f64SRam Pai struct vfsmount *p; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 95736341f64SRam Pai for (p = mnt; p; p = next_mnt(p, mnt)) { 9581da177e4SLinus Torvalds actual_refs += atomic_read(&p->mnt_count); 9591da177e4SLinus Torvalds minimum_refs += 2; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds if (actual_refs > minimum_refs) 9641da177e4SLinus Torvalds return 0; 965e3474a8eSIan Kent 966e3474a8eSIan Kent return 1; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds /** 9721da177e4SLinus Torvalds * may_umount - check if a mount point is busy 9731da177e4SLinus Torvalds * @mnt: root of mount 9741da177e4SLinus Torvalds * 9751da177e4SLinus Torvalds * This is called to check if a mount point has any 9761da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 9771da177e4SLinus Torvalds * mount has sub mounts this will return busy 9781da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 9791da177e4SLinus Torvalds * 9801da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 9811da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 9821da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 9831da177e4SLinus Torvalds */ 9841da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 9851da177e4SLinus Torvalds { 986e3474a8eSIan Kent int ret = 1; 9878ad08d8aSAl Viro down_read(&namespace_sem); 988a05964f3SRam Pai spin_lock(&vfsmount_lock); 989a05964f3SRam Pai if (propagate_mount_busy(mnt, 2)) 990e3474a8eSIan Kent ret = 0; 991a05964f3SRam Pai spin_unlock(&vfsmount_lock); 9928ad08d8aSAl Viro up_read(&namespace_sem); 993a05964f3SRam Pai return ret; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 9971da177e4SLinus Torvalds 998b90fa9aeSRam Pai void release_mounts(struct list_head *head) 9991da177e4SLinus Torvalds { 100070fbcdf4SRam Pai struct vfsmount *mnt; 100170fbcdf4SRam Pai while (!list_empty(head)) { 1002b5e61818SPavel Emelianov mnt = list_first_entry(head, struct vfsmount, mnt_hash); 100370fbcdf4SRam Pai list_del_init(&mnt->mnt_hash); 100470fbcdf4SRam Pai if (mnt->mnt_parent != mnt) { 100570fbcdf4SRam Pai struct dentry *dentry; 100670fbcdf4SRam Pai struct vfsmount *m; 100770fbcdf4SRam Pai spin_lock(&vfsmount_lock); 100870fbcdf4SRam Pai dentry = mnt->mnt_mountpoint; 100970fbcdf4SRam Pai m = mnt->mnt_parent; 101070fbcdf4SRam Pai mnt->mnt_mountpoint = mnt->mnt_root; 101170fbcdf4SRam Pai mnt->mnt_parent = mnt; 10127c4b93d8SAl Viro m->mnt_ghosts--; 10131da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 101470fbcdf4SRam Pai dput(dentry); 101570fbcdf4SRam Pai mntput(m); 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds mntput(mnt); 101870fbcdf4SRam Pai } 101970fbcdf4SRam Pai } 102070fbcdf4SRam Pai 1021a05964f3SRam Pai void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill) 102270fbcdf4SRam Pai { 102370fbcdf4SRam Pai struct vfsmount *p; 102470fbcdf4SRam Pai 10251bfba4e8SAkinobu Mita for (p = mnt; p; p = next_mnt(p, mnt)) 10261bfba4e8SAkinobu Mita list_move(&p->mnt_hash, kill); 102770fbcdf4SRam Pai 1028a05964f3SRam Pai if (propagate) 1029a05964f3SRam Pai propagate_umount(kill); 1030a05964f3SRam Pai 103170fbcdf4SRam Pai list_for_each_entry(p, kill, mnt_hash) { 103270fbcdf4SRam Pai list_del_init(&p->mnt_expire); 103370fbcdf4SRam Pai list_del_init(&p->mnt_list); 10346b3286edSKirill Korotaev __touch_mnt_namespace(p->mnt_ns); 10356b3286edSKirill Korotaev p->mnt_ns = NULL; 103670fbcdf4SRam Pai list_del_init(&p->mnt_child); 10377c4b93d8SAl Viro if (p->mnt_parent != p) { 10387c4b93d8SAl Viro p->mnt_parent->mnt_ghosts++; 1039f30ac319SAl Viro p->mnt_mountpoint->d_mounted--; 10407c4b93d8SAl Viro } 1041a05964f3SRam Pai change_mnt_propagation(p, MS_PRIVATE); 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 1045c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts); 1046c35038beSAl Viro 10471da177e4SLinus Torvalds static int do_umount(struct vfsmount *mnt, int flags) 10481da177e4SLinus Torvalds { 10491da177e4SLinus Torvalds struct super_block *sb = mnt->mnt_sb; 10501da177e4SLinus Torvalds int retval; 105170fbcdf4SRam Pai LIST_HEAD(umount_list); 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds retval = security_sb_umount(mnt, flags); 10541da177e4SLinus Torvalds if (retval) 10551da177e4SLinus Torvalds return retval; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* 10581da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 10591da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 10601da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 10611da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 10621da177e4SLinus Torvalds */ 10631da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 10646ac08c39SJan Blunck if (mnt == current->fs->root.mnt || 10651da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 10661da177e4SLinus Torvalds return -EINVAL; 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds if (atomic_read(&mnt->mnt_count) != 2) 10691da177e4SLinus Torvalds return -EBUSY; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds if (!xchg(&mnt->mnt_expiry_mark, 1)) 10721da177e4SLinus Torvalds return -EAGAIN; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds /* 10761da177e4SLinus Torvalds * If we may have to abort operations to get out of this 10771da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 10781da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 10791da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 10801da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 10811da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 10821da177e4SLinus Torvalds * about for the moment. 10831da177e4SLinus Torvalds */ 10841da177e4SLinus Torvalds 108542faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 108642faad99SAl Viro sb->s_op->umount_begin(sb); 108742faad99SAl Viro } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* 10901da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 10911da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 10921da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 10931da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 10941da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 10951da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 10961da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 10971da177e4SLinus Torvalds */ 10986ac08c39SJan Blunck if (mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 10991da177e4SLinus Torvalds /* 11001da177e4SLinus Torvalds * Special case for "unmounting" root ... 11011da177e4SLinus Torvalds * we just try to remount it readonly. 11021da177e4SLinus Torvalds */ 11031da177e4SLinus Torvalds down_write(&sb->s_umount); 11044aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 11051da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 11061da177e4SLinus Torvalds up_write(&sb->s_umount); 11071da177e4SLinus Torvalds return retval; 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds 1110390c6843SRam Pai down_write(&namespace_sem); 11111da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 11125addc5ddSAl Viro event++; 11131da177e4SLinus Torvalds 1114c35038beSAl Viro if (!(flags & MNT_DETACH)) 1115c35038beSAl Viro shrink_submounts(mnt, &umount_list); 1116c35038beSAl Viro 11171da177e4SLinus Torvalds retval = -EBUSY; 1118a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 11191da177e4SLinus Torvalds if (!list_empty(&mnt->mnt_list)) 1120a05964f3SRam Pai umount_tree(mnt, 1, &umount_list); 11211da177e4SLinus Torvalds retval = 0; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 1124390c6843SRam Pai up_write(&namespace_sem); 112570fbcdf4SRam Pai release_mounts(&umount_list); 11261da177e4SLinus Torvalds return retval; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds /* 11301da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 11311da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 11321da177e4SLinus Torvalds * 11331da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 11341da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds 1137bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 11381da177e4SLinus Torvalds { 11392d8f3038SAl Viro struct path path; 11401da177e4SLinus Torvalds int retval; 1141db1f05bbSMiklos Szeredi int lookup_flags = 0; 11421da177e4SLinus Torvalds 1143db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1144db1f05bbSMiklos Szeredi return -EINVAL; 1145db1f05bbSMiklos Szeredi 1146db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1147db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1148db1f05bbSMiklos Szeredi 1149db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 11501da177e4SLinus Torvalds if (retval) 11511da177e4SLinus Torvalds goto out; 11521da177e4SLinus Torvalds retval = -EINVAL; 11532d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 11541da177e4SLinus Torvalds goto dput_and_out; 11552d8f3038SAl Viro if (!check_mnt(path.mnt)) 11561da177e4SLinus Torvalds goto dput_and_out; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds retval = -EPERM; 11591da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11601da177e4SLinus Torvalds goto dput_and_out; 11611da177e4SLinus Torvalds 11622d8f3038SAl Viro retval = do_umount(path.mnt, flags); 11631da177e4SLinus Torvalds dput_and_out: 1164429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 11652d8f3038SAl Viro dput(path.dentry); 11662d8f3038SAl Viro mntput_no_expire(path.mnt); 11671da177e4SLinus Torvalds out: 11681da177e4SLinus Torvalds return retval; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /* 11741da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 11751da177e4SLinus Torvalds */ 1176bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 11771da177e4SLinus Torvalds { 11781da177e4SLinus Torvalds return sys_umount(name, 0); 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds #endif 11821da177e4SLinus Torvalds 11832d92ab3cSAl Viro static int mount_is_safe(struct path *path) 11841da177e4SLinus Torvalds { 11851da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 11861da177e4SLinus Torvalds return 0; 11871da177e4SLinus Torvalds return -EPERM; 11881da177e4SLinus Torvalds #ifdef notyet 11892d92ab3cSAl Viro if (S_ISLNK(path->dentry->d_inode->i_mode)) 11901da177e4SLinus Torvalds return -EPERM; 11912d92ab3cSAl Viro if (path->dentry->d_inode->i_mode & S_ISVTX) { 1192da9592edSDavid Howells if (current_uid() != path->dentry->d_inode->i_uid) 11931da177e4SLinus Torvalds return -EPERM; 11941da177e4SLinus Torvalds } 11952d92ab3cSAl Viro if (inode_permission(path->dentry->d_inode, MAY_WRITE)) 11961da177e4SLinus Torvalds return -EPERM; 11971da177e4SLinus Torvalds return 0; 11981da177e4SLinus Torvalds #endif 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 1201b90fa9aeSRam Pai struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry, 120236341f64SRam Pai int flag) 12031da177e4SLinus Torvalds { 12041da177e4SLinus Torvalds struct vfsmount *res, *p, *q, *r, *s; 12051a390689SAl Viro struct path path; 12061da177e4SLinus Torvalds 12079676f0c6SRam Pai if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 12089676f0c6SRam Pai return NULL; 12099676f0c6SRam Pai 121036341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 12111da177e4SLinus Torvalds if (!q) 12121da177e4SLinus Torvalds goto Enomem; 12131da177e4SLinus Torvalds q->mnt_mountpoint = mnt->mnt_mountpoint; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds p = mnt; 1216fdadd65fSDomen Puncer list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 12177ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 12181da177e4SLinus Torvalds continue; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds for (s = r; s; s = next_mnt(s, r)) { 12219676f0c6SRam Pai if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 12229676f0c6SRam Pai s = skip_mnt_tree(s); 12239676f0c6SRam Pai continue; 12249676f0c6SRam Pai } 12251da177e4SLinus Torvalds while (p != s->mnt_parent) { 12261da177e4SLinus Torvalds p = p->mnt_parent; 12271da177e4SLinus Torvalds q = q->mnt_parent; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds p = s; 12301a390689SAl Viro path.mnt = q; 12311a390689SAl Viro path.dentry = p->mnt_mountpoint; 123236341f64SRam Pai q = clone_mnt(p, p->mnt_root, flag); 12331da177e4SLinus Torvalds if (!q) 12341da177e4SLinus Torvalds goto Enomem; 12351da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 12361da177e4SLinus Torvalds list_add_tail(&q->mnt_list, &res->mnt_list); 12371a390689SAl Viro attach_mnt(q, &path); 12381da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds return res; 12421da177e4SLinus Torvalds Enomem: 12431da177e4SLinus Torvalds if (res) { 124470fbcdf4SRam Pai LIST_HEAD(umount_list); 12451da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 1246a05964f3SRam Pai umount_tree(res, 0, &umount_list); 12471da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 124870fbcdf4SRam Pai release_mounts(&umount_list); 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds return NULL; 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 1253589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 12548aec0809SAl Viro { 12558aec0809SAl Viro struct vfsmount *tree; 12561a60a280SAl Viro down_write(&namespace_sem); 1257589ff870SAl Viro tree = copy_tree(path->mnt, path->dentry, CL_COPY_ALL | CL_PRIVATE); 12581a60a280SAl Viro up_write(&namespace_sem); 12598aec0809SAl Viro return tree; 12608aec0809SAl Viro } 12618aec0809SAl Viro 12628aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 12638aec0809SAl Viro { 12648aec0809SAl Viro LIST_HEAD(umount_list); 12651a60a280SAl Viro down_write(&namespace_sem); 12668aec0809SAl Viro spin_lock(&vfsmount_lock); 12678aec0809SAl Viro umount_tree(mnt, 0, &umount_list); 12688aec0809SAl Viro spin_unlock(&vfsmount_lock); 12691a60a280SAl Viro up_write(&namespace_sem); 12708aec0809SAl Viro release_mounts(&umount_list); 12718aec0809SAl Viro } 12728aec0809SAl Viro 12731f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 12741f707137SAl Viro struct vfsmount *root) 12751f707137SAl Viro { 12761f707137SAl Viro struct vfsmount *mnt; 12771f707137SAl Viro int res = f(root, arg); 12781f707137SAl Viro if (res) 12791f707137SAl Viro return res; 12801f707137SAl Viro list_for_each_entry(mnt, &root->mnt_list, mnt_list) { 12811f707137SAl Viro res = f(mnt, arg); 12821f707137SAl Viro if (res) 12831f707137SAl Viro return res; 12841f707137SAl Viro } 12851f707137SAl Viro return 0; 12861f707137SAl Viro } 12871f707137SAl Viro 1288719f5d7fSMiklos Szeredi static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end) 1289719f5d7fSMiklos Szeredi { 1290719f5d7fSMiklos Szeredi struct vfsmount *p; 1291719f5d7fSMiklos Szeredi 1292719f5d7fSMiklos Szeredi for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1293719f5d7fSMiklos Szeredi if (p->mnt_group_id && !IS_MNT_SHARED(p)) 1294719f5d7fSMiklos Szeredi mnt_release_group_id(p); 1295719f5d7fSMiklos Szeredi } 1296719f5d7fSMiklos Szeredi } 1297719f5d7fSMiklos Szeredi 1298719f5d7fSMiklos Szeredi static int invent_group_ids(struct vfsmount *mnt, bool recurse) 1299719f5d7fSMiklos Szeredi { 1300719f5d7fSMiklos Szeredi struct vfsmount *p; 1301719f5d7fSMiklos Szeredi 1302719f5d7fSMiklos Szeredi for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1303719f5d7fSMiklos Szeredi if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 1304719f5d7fSMiklos Szeredi int err = mnt_alloc_group_id(p); 1305719f5d7fSMiklos Szeredi if (err) { 1306719f5d7fSMiklos Szeredi cleanup_group_ids(mnt, p); 1307719f5d7fSMiklos Szeredi return err; 1308719f5d7fSMiklos Szeredi } 1309719f5d7fSMiklos Szeredi } 1310719f5d7fSMiklos Szeredi } 1311719f5d7fSMiklos Szeredi 1312719f5d7fSMiklos Szeredi return 0; 1313719f5d7fSMiklos Szeredi } 1314719f5d7fSMiklos Szeredi 1315b90fa9aeSRam Pai /* 1316b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1317b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 131821444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 131921444403SRam Pai * store the parent mount and mountpoint dentry. 132021444403SRam Pai * (done when source_mnt is moved) 1321b90fa9aeSRam Pai * 1322b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1323b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 13249676f0c6SRam Pai * --------------------------------------------------------------------------- 1325b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 13269676f0c6SRam Pai * |************************************************************************** 13279676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13289676f0c6SRam Pai * | dest | | | | | 13299676f0c6SRam Pai * | | | | | | | 13309676f0c6SRam Pai * | v | | | | | 13319676f0c6SRam Pai * |************************************************************************** 13329676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 13335afe0022SRam Pai * | | | | | | 13349676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 13359676f0c6SRam Pai * *************************************************************************** 1336b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1337b90fa9aeSRam Pai * destination mount. 1338b90fa9aeSRam Pai * 1339b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1340b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1341b90fa9aeSRam Pai * the peer group of the source mount. 1342b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1343b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1344b90fa9aeSRam Pai * mount. 13455afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 13465afe0022SRam Pai * of the destination mount and the cloned mount is made slave 13475afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 13485afe0022SRam Pai * is marked as 'shared and slave'. 13495afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 13505afe0022SRam Pai * source mount. 13515afe0022SRam Pai * 13529676f0c6SRam Pai * --------------------------------------------------------------------------- 135321444403SRam Pai * | MOVE MOUNT OPERATION | 13549676f0c6SRam Pai * |************************************************************************** 13559676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13569676f0c6SRam Pai * | dest | | | | | 13579676f0c6SRam Pai * | | | | | | | 13589676f0c6SRam Pai * | v | | | | | 13599676f0c6SRam Pai * |************************************************************************** 13609676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 13615afe0022SRam Pai * | | | | | | 13629676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 13639676f0c6SRam Pai * *************************************************************************** 13645afe0022SRam Pai * 13655afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 13665afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 136721444403SRam Pai * (+*) the mount is moved to the destination. 13685afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 13695afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 13705afe0022SRam Pai * the mount is marked as 'shared and slave'. 13715afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1372b90fa9aeSRam Pai * 1373b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1374b90fa9aeSRam Pai * applied to each mount in the tree. 1375b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1376b90fa9aeSRam Pai * in allocations. 1377b90fa9aeSRam Pai */ 1378b90fa9aeSRam Pai static int attach_recursive_mnt(struct vfsmount *source_mnt, 13791a390689SAl Viro struct path *path, struct path *parent_path) 1380b90fa9aeSRam Pai { 1381b90fa9aeSRam Pai LIST_HEAD(tree_list); 13821a390689SAl Viro struct vfsmount *dest_mnt = path->mnt; 13831a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1384b90fa9aeSRam Pai struct vfsmount *child, *p; 1385719f5d7fSMiklos Szeredi int err; 1386b90fa9aeSRam Pai 1387719f5d7fSMiklos Szeredi if (IS_MNT_SHARED(dest_mnt)) { 1388719f5d7fSMiklos Szeredi err = invent_group_ids(source_mnt, true); 1389719f5d7fSMiklos Szeredi if (err) 1390719f5d7fSMiklos Szeredi goto out; 1391719f5d7fSMiklos Szeredi } 1392719f5d7fSMiklos Szeredi err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); 1393719f5d7fSMiklos Szeredi if (err) 1394719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1395b90fa9aeSRam Pai 1396df1a1ad2SAl Viro spin_lock(&vfsmount_lock); 1397df1a1ad2SAl Viro 1398b90fa9aeSRam Pai if (IS_MNT_SHARED(dest_mnt)) { 1399b90fa9aeSRam Pai for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 1400b90fa9aeSRam Pai set_mnt_shared(p); 1401b90fa9aeSRam Pai } 14021a390689SAl Viro if (parent_path) { 14031a390689SAl Viro detach_mnt(source_mnt, parent_path); 14041a390689SAl Viro attach_mnt(source_mnt, path); 1405e5d67f07SAl Viro touch_mnt_namespace(parent_path->mnt->mnt_ns); 140621444403SRam Pai } else { 1407b90fa9aeSRam Pai mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 1408b90fa9aeSRam Pai commit_tree(source_mnt); 140921444403SRam Pai } 1410b90fa9aeSRam Pai 1411b90fa9aeSRam Pai list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 1412b90fa9aeSRam Pai list_del_init(&child->mnt_hash); 1413b90fa9aeSRam Pai commit_tree(child); 1414b90fa9aeSRam Pai } 1415b90fa9aeSRam Pai spin_unlock(&vfsmount_lock); 1416b90fa9aeSRam Pai return 0; 1417719f5d7fSMiklos Szeredi 1418719f5d7fSMiklos Szeredi out_cleanup_ids: 1419719f5d7fSMiklos Szeredi if (IS_MNT_SHARED(dest_mnt)) 1420719f5d7fSMiklos Szeredi cleanup_group_ids(source_mnt, NULL); 1421719f5d7fSMiklos Szeredi out: 1422719f5d7fSMiklos Szeredi return err; 1423b90fa9aeSRam Pai } 1424b90fa9aeSRam Pai 14258c3ee42eSAl Viro static int graft_tree(struct vfsmount *mnt, struct path *path) 14261da177e4SLinus Torvalds { 14271da177e4SLinus Torvalds int err; 14281da177e4SLinus Torvalds if (mnt->mnt_sb->s_flags & MS_NOUSER) 14291da177e4SLinus Torvalds return -EINVAL; 14301da177e4SLinus Torvalds 14318c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 14321da177e4SLinus Torvalds S_ISDIR(mnt->mnt_root->d_inode->i_mode)) 14331da177e4SLinus Torvalds return -ENOTDIR; 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds err = -ENOENT; 14368c3ee42eSAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1437d83c49f3SAl Viro if (cant_mount(path->dentry)) 14381da177e4SLinus Torvalds goto out_unlock; 14391da177e4SLinus Torvalds 1440f3da392eSAlexey Dobriyan if (!d_unlinked(path->dentry)) 14418c3ee42eSAl Viro err = attach_recursive_mnt(mnt, path, NULL); 14421da177e4SLinus Torvalds out_unlock: 14438c3ee42eSAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 14441da177e4SLinus Torvalds return err; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds /* 144807b20889SRam Pai * recursively change the type of the mountpoint. 144907b20889SRam Pai */ 14500a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 145107b20889SRam Pai { 14522d92ab3cSAl Viro struct vfsmount *m, *mnt = path->mnt; 145307b20889SRam Pai int recurse = flag & MS_REC; 145407b20889SRam Pai int type = flag & ~MS_REC; 1455719f5d7fSMiklos Szeredi int err = 0; 145607b20889SRam Pai 1457ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 1458ee6f9582SMiklos Szeredi return -EPERM; 1459ee6f9582SMiklos Szeredi 14602d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 146107b20889SRam Pai return -EINVAL; 146207b20889SRam Pai 146307b20889SRam Pai down_write(&namespace_sem); 1464719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1465719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1466719f5d7fSMiklos Szeredi if (err) 1467719f5d7fSMiklos Szeredi goto out_unlock; 1468719f5d7fSMiklos Szeredi } 1469719f5d7fSMiklos Szeredi 147007b20889SRam Pai spin_lock(&vfsmount_lock); 147107b20889SRam Pai for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 147207b20889SRam Pai change_mnt_propagation(m, type); 147307b20889SRam Pai spin_unlock(&vfsmount_lock); 1474719f5d7fSMiklos Szeredi 1475719f5d7fSMiklos Szeredi out_unlock: 147607b20889SRam Pai up_write(&namespace_sem); 1477719f5d7fSMiklos Szeredi return err; 147807b20889SRam Pai } 147907b20889SRam Pai 148007b20889SRam Pai /* 14811da177e4SLinus Torvalds * do loopback mount. 14821da177e4SLinus Torvalds */ 14830a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name, 14842dafe1c4SEric Sandeen int recurse) 14851da177e4SLinus Torvalds { 14862d92ab3cSAl Viro struct path old_path; 14871da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 14882d92ab3cSAl Viro int err = mount_is_safe(path); 14891da177e4SLinus Torvalds if (err) 14901da177e4SLinus Torvalds return err; 14911da177e4SLinus Torvalds if (!old_name || !*old_name) 14921da177e4SLinus Torvalds return -EINVAL; 14932d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 14941da177e4SLinus Torvalds if (err) 14951da177e4SLinus Torvalds return err; 14961da177e4SLinus Torvalds 1497390c6843SRam Pai down_write(&namespace_sem); 14981da177e4SLinus Torvalds err = -EINVAL; 14992d92ab3cSAl Viro if (IS_MNT_UNBINDABLE(old_path.mnt)) 15009676f0c6SRam Pai goto out; 15019676f0c6SRam Pai 15022d92ab3cSAl Viro if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt)) 1503ccd48bc7SAl Viro goto out; 1504ccd48bc7SAl Viro 15051da177e4SLinus Torvalds err = -ENOMEM; 15061da177e4SLinus Torvalds if (recurse) 15072d92ab3cSAl Viro mnt = copy_tree(old_path.mnt, old_path.dentry, 0); 15081da177e4SLinus Torvalds else 15092d92ab3cSAl Viro mnt = clone_mnt(old_path.mnt, old_path.dentry, 0); 15101da177e4SLinus Torvalds 1511ccd48bc7SAl Viro if (!mnt) 1512ccd48bc7SAl Viro goto out; 1513ccd48bc7SAl Viro 15142d92ab3cSAl Viro err = graft_tree(mnt, path); 15151da177e4SLinus Torvalds if (err) { 151670fbcdf4SRam Pai LIST_HEAD(umount_list); 15171da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 1518a05964f3SRam Pai umount_tree(mnt, 0, &umount_list); 15191da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 152070fbcdf4SRam Pai release_mounts(&umount_list); 15215b83d2c5SRam Pai } 15221da177e4SLinus Torvalds 1523ccd48bc7SAl Viro out: 1524390c6843SRam Pai up_write(&namespace_sem); 15252d92ab3cSAl Viro path_put(&old_path); 15261da177e4SLinus Torvalds return err; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds 15292e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 15302e4b7fcdSDave Hansen { 15312e4b7fcdSDave Hansen int error = 0; 15322e4b7fcdSDave Hansen int readonly_request = 0; 15332e4b7fcdSDave Hansen 15342e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 15352e4b7fcdSDave Hansen readonly_request = 1; 15362e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 15372e4b7fcdSDave Hansen return 0; 15382e4b7fcdSDave Hansen 15392e4b7fcdSDave Hansen if (readonly_request) 15402e4b7fcdSDave Hansen error = mnt_make_readonly(mnt); 15412e4b7fcdSDave Hansen else 15422e4b7fcdSDave Hansen __mnt_unmake_readonly(mnt); 15432e4b7fcdSDave Hansen return error; 15442e4b7fcdSDave Hansen } 15452e4b7fcdSDave Hansen 15461da177e4SLinus Torvalds /* 15471da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 15481da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 15491da177e4SLinus Torvalds * on it - tough luck. 15501da177e4SLinus Torvalds */ 15510a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 15521da177e4SLinus Torvalds void *data) 15531da177e4SLinus Torvalds { 15541da177e4SLinus Torvalds int err; 15552d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 15581da177e4SLinus Torvalds return -EPERM; 15591da177e4SLinus Torvalds 15602d92ab3cSAl Viro if (!check_mnt(path->mnt)) 15611da177e4SLinus Torvalds return -EINVAL; 15621da177e4SLinus Torvalds 15632d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 15641da177e4SLinus Torvalds return -EINVAL; 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds down_write(&sb->s_umount); 15672e4b7fcdSDave Hansen if (flags & MS_BIND) 15682d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 15694aa98cf7SAl Viro else 15701da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 15717b43a79fSAl Viro if (!err) { 15727b43a79fSAl Viro spin_lock(&vfsmount_lock); 1573495d6c9cSValerie Aurora mnt_flags |= path->mnt->mnt_flags & MNT_PROPAGATION_MASK; 15742d92ab3cSAl Viro path->mnt->mnt_flags = mnt_flags; 15757b43a79fSAl Viro spin_unlock(&vfsmount_lock); 15767b43a79fSAl Viro } 15771da177e4SLinus Torvalds up_write(&sb->s_umount); 15780e55a7ccSDan Williams if (!err) { 15790e55a7ccSDan Williams spin_lock(&vfsmount_lock); 15800e55a7ccSDan Williams touch_mnt_namespace(path->mnt->mnt_ns); 15810e55a7ccSDan Williams spin_unlock(&vfsmount_lock); 15820e55a7ccSDan Williams } 15831da177e4SLinus Torvalds return err; 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 15869676f0c6SRam Pai static inline int tree_contains_unbindable(struct vfsmount *mnt) 15879676f0c6SRam Pai { 15889676f0c6SRam Pai struct vfsmount *p; 15899676f0c6SRam Pai for (p = mnt; p; p = next_mnt(p, mnt)) { 15909676f0c6SRam Pai if (IS_MNT_UNBINDABLE(p)) 15919676f0c6SRam Pai return 1; 15929676f0c6SRam Pai } 15939676f0c6SRam Pai return 0; 15949676f0c6SRam Pai } 15959676f0c6SRam Pai 15960a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name) 15971da177e4SLinus Torvalds { 15982d92ab3cSAl Viro struct path old_path, parent_path; 15991da177e4SLinus Torvalds struct vfsmount *p; 16001da177e4SLinus Torvalds int err = 0; 16011da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16021da177e4SLinus Torvalds return -EPERM; 16031da177e4SLinus Torvalds if (!old_name || !*old_name) 16041da177e4SLinus Torvalds return -EINVAL; 16052d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 16061da177e4SLinus Torvalds if (err) 16071da177e4SLinus Torvalds return err; 16081da177e4SLinus Torvalds 1609390c6843SRam Pai down_write(&namespace_sem); 16102d92ab3cSAl Viro while (d_mountpoint(path->dentry) && 16119393bd07SAl Viro follow_down(path)) 16121da177e4SLinus Torvalds ; 16131da177e4SLinus Torvalds err = -EINVAL; 16142d92ab3cSAl Viro if (!check_mnt(path->mnt) || !check_mnt(old_path.mnt)) 16151da177e4SLinus Torvalds goto out; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds err = -ENOENT; 16182d92ab3cSAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1619d83c49f3SAl Viro if (cant_mount(path->dentry)) 16201da177e4SLinus Torvalds goto out1; 16211da177e4SLinus Torvalds 1622f3da392eSAlexey Dobriyan if (d_unlinked(path->dentry)) 162321444403SRam Pai goto out1; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds err = -EINVAL; 16262d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 162721444403SRam Pai goto out1; 16281da177e4SLinus Torvalds 16292d92ab3cSAl Viro if (old_path.mnt == old_path.mnt->mnt_parent) 163021444403SRam Pai goto out1; 16311da177e4SLinus Torvalds 16322d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 16332d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 163421444403SRam Pai goto out1; 163521444403SRam Pai /* 163621444403SRam Pai * Don't move a mount residing in a shared parent. 163721444403SRam Pai */ 16382d92ab3cSAl Viro if (old_path.mnt->mnt_parent && 16392d92ab3cSAl Viro IS_MNT_SHARED(old_path.mnt->mnt_parent)) 164021444403SRam Pai goto out1; 16419676f0c6SRam Pai /* 16429676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 16439676f0c6SRam Pai * mount which is shared. 16449676f0c6SRam Pai */ 16452d92ab3cSAl Viro if (IS_MNT_SHARED(path->mnt) && 16462d92ab3cSAl Viro tree_contains_unbindable(old_path.mnt)) 16479676f0c6SRam Pai goto out1; 16481da177e4SLinus Torvalds err = -ELOOP; 16492d92ab3cSAl Viro for (p = path->mnt; p->mnt_parent != p; p = p->mnt_parent) 16502d92ab3cSAl Viro if (p == old_path.mnt) 165121444403SRam Pai goto out1; 16521da177e4SLinus Torvalds 16532d92ab3cSAl Viro err = attach_recursive_mnt(old_path.mnt, path, &parent_path); 16544ac91378SJan Blunck if (err) 165521444403SRam Pai goto out1; 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 16581da177e4SLinus Torvalds * automatically */ 16592d92ab3cSAl Viro list_del_init(&old_path.mnt->mnt_expire); 16601da177e4SLinus Torvalds out1: 16612d92ab3cSAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 16621da177e4SLinus Torvalds out: 1663390c6843SRam Pai up_write(&namespace_sem); 16641da177e4SLinus Torvalds if (!err) 16651a390689SAl Viro path_put(&parent_path); 16662d92ab3cSAl Viro path_put(&old_path); 16671da177e4SLinus Torvalds return err; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* 16711da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 16721da177e4SLinus Torvalds * namespace's tree 16731da177e4SLinus Torvalds */ 16740a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags, 16751da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 16761da177e4SLinus Torvalds { 16771da177e4SLinus Torvalds struct vfsmount *mnt; 16781da177e4SLinus Torvalds 1679eca6f534SVegard Nossum if (!type) 16801da177e4SLinus Torvalds return -EINVAL; 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* we need capabilities... */ 16831da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16841da177e4SLinus Torvalds return -EPERM; 16851da177e4SLinus Torvalds 16867f78d4cdSAl Viro lock_kernel(); 16871da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 16887f78d4cdSAl Viro unlock_kernel(); 16891da177e4SLinus Torvalds if (IS_ERR(mnt)) 16901da177e4SLinus Torvalds return PTR_ERR(mnt); 16911da177e4SLinus Torvalds 16922d92ab3cSAl Viro return do_add_mount(mnt, path, mnt_flags, NULL); 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds /* 16961da177e4SLinus Torvalds * add a mount into a namespace's mount tree 16971da177e4SLinus Torvalds * - provide the option of adding the new mount to an expiration list 16981da177e4SLinus Torvalds */ 16998d66bf54SAl Viro int do_add_mount(struct vfsmount *newmnt, struct path *path, 17001da177e4SLinus Torvalds int mnt_flags, struct list_head *fslist) 17011da177e4SLinus Torvalds { 17021da177e4SLinus Torvalds int err; 17031da177e4SLinus Torvalds 17048089352aSAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 170527d55f1fSAl Viro 1706390c6843SRam Pai down_write(&namespace_sem); 17071da177e4SLinus Torvalds /* Something was mounted here while we slept */ 17088d66bf54SAl Viro while (d_mountpoint(path->dentry) && 17099393bd07SAl Viro follow_down(path)) 17101da177e4SLinus Torvalds ; 17111da177e4SLinus Torvalds err = -EINVAL; 1712dd5cae6eSAl Viro if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(path->mnt)) 17131da177e4SLinus Torvalds goto unlock; 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds /* Refuse the same filesystem on the same mount point */ 17161da177e4SLinus Torvalds err = -EBUSY; 17178d66bf54SAl Viro if (path->mnt->mnt_sb == newmnt->mnt_sb && 17188d66bf54SAl Viro path->mnt->mnt_root == path->dentry) 17191da177e4SLinus Torvalds goto unlock; 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds err = -EINVAL; 17221da177e4SLinus Torvalds if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode)) 17231da177e4SLinus Torvalds goto unlock; 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds newmnt->mnt_flags = mnt_flags; 17268d66bf54SAl Viro if ((err = graft_tree(newmnt, path))) 17275b83d2c5SRam Pai goto unlock; 17281da177e4SLinus Torvalds 17296758f953SAl Viro if (fslist) /* add to the specified expiration list */ 173055e700b9SMiklos Szeredi list_add_tail(&newmnt->mnt_expire, fslist); 17316758f953SAl Viro 1732390c6843SRam Pai up_write(&namespace_sem); 17335b83d2c5SRam Pai return 0; 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds unlock: 1736390c6843SRam Pai up_write(&namespace_sem); 17371da177e4SLinus Torvalds mntput(newmnt); 17381da177e4SLinus Torvalds return err; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(do_add_mount); 17421da177e4SLinus Torvalds 17435528f911STrond Myklebust /* 17441da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 17451da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 17461da177e4SLinus Torvalds * here 17471da177e4SLinus Torvalds */ 17481da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 17491da177e4SLinus Torvalds { 17501da177e4SLinus Torvalds struct vfsmount *mnt, *next; 17511da177e4SLinus Torvalds LIST_HEAD(graveyard); 1752bcc5c7d2SAl Viro LIST_HEAD(umounts); 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds if (list_empty(mounts)) 17551da177e4SLinus Torvalds return; 17561da177e4SLinus Torvalds 1757bcc5c7d2SAl Viro down_write(&namespace_sem); 17581da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 17611da177e4SLinus Torvalds * following criteria: 17621da177e4SLinus Torvalds * - only referenced by its parent vfsmount 17631da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 17641da177e4SLinus Torvalds * cleared by mntput()) 17651da177e4SLinus Torvalds */ 176655e700b9SMiklos Szeredi list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 17671da177e4SLinus Torvalds if (!xchg(&mnt->mnt_expiry_mark, 1) || 1768bcc5c7d2SAl Viro propagate_mount_busy(mnt, 1)) 17691da177e4SLinus Torvalds continue; 177055e700b9SMiklos Szeredi list_move(&mnt->mnt_expire, &graveyard); 17711da177e4SLinus Torvalds } 1772bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 1773bcc5c7d2SAl Viro mnt = list_first_entry(&graveyard, struct vfsmount, mnt_expire); 1774bcc5c7d2SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1775bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 1776bcc5c7d2SAl Viro } 17771da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 1778bcc5c7d2SAl Viro up_write(&namespace_sem); 1779bcc5c7d2SAl Viro 1780bcc5c7d2SAl Viro release_mounts(&umounts); 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds /* 17865528f911STrond Myklebust * Ripoff of 'select_parent()' 17875528f911STrond Myklebust * 17885528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 17895528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 17905528f911STrond Myklebust */ 17915528f911STrond Myklebust static int select_submounts(struct vfsmount *parent, struct list_head *graveyard) 17925528f911STrond Myklebust { 17935528f911STrond Myklebust struct vfsmount *this_parent = parent; 17945528f911STrond Myklebust struct list_head *next; 17955528f911STrond Myklebust int found = 0; 17965528f911STrond Myklebust 17975528f911STrond Myklebust repeat: 17985528f911STrond Myklebust next = this_parent->mnt_mounts.next; 17995528f911STrond Myklebust resume: 18005528f911STrond Myklebust while (next != &this_parent->mnt_mounts) { 18015528f911STrond Myklebust struct list_head *tmp = next; 18025528f911STrond Myklebust struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child); 18035528f911STrond Myklebust 18045528f911STrond Myklebust next = tmp->next; 18055528f911STrond Myklebust if (!(mnt->mnt_flags & MNT_SHRINKABLE)) 18065528f911STrond Myklebust continue; 18075528f911STrond Myklebust /* 18085528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 18095528f911STrond Myklebust */ 18105528f911STrond Myklebust if (!list_empty(&mnt->mnt_mounts)) { 18115528f911STrond Myklebust this_parent = mnt; 18125528f911STrond Myklebust goto repeat; 18135528f911STrond Myklebust } 18145528f911STrond Myklebust 18155528f911STrond Myklebust if (!propagate_mount_busy(mnt, 1)) { 18165528f911STrond Myklebust list_move_tail(&mnt->mnt_expire, graveyard); 18175528f911STrond Myklebust found++; 18185528f911STrond Myklebust } 18195528f911STrond Myklebust } 18205528f911STrond Myklebust /* 18215528f911STrond Myklebust * All done at this level ... ascend and resume the search 18225528f911STrond Myklebust */ 18235528f911STrond Myklebust if (this_parent != parent) { 18245528f911STrond Myklebust next = this_parent->mnt_child.next; 18255528f911STrond Myklebust this_parent = this_parent->mnt_parent; 18265528f911STrond Myklebust goto resume; 18275528f911STrond Myklebust } 18285528f911STrond Myklebust return found; 18295528f911STrond Myklebust } 18305528f911STrond Myklebust 18315528f911STrond Myklebust /* 18325528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 18335528f911STrond Myklebust * submounts of a specific parent mountpoint 18345528f911STrond Myklebust */ 1835c35038beSAl Viro static void shrink_submounts(struct vfsmount *mnt, struct list_head *umounts) 18365528f911STrond Myklebust { 18375528f911STrond Myklebust LIST_HEAD(graveyard); 1838c35038beSAl Viro struct vfsmount *m; 18395528f911STrond Myklebust 18405528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 1841c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 1842bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 1843c35038beSAl Viro m = list_first_entry(&graveyard, struct vfsmount, 1844bcc5c7d2SAl Viro mnt_expire); 1845afef80b3SEric W. Biederman touch_mnt_namespace(m->mnt_ns); 1846afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 1847bcc5c7d2SAl Viro } 1848bcc5c7d2SAl Viro } 18495528f911STrond Myklebust } 18505528f911STrond Myklebust 18515528f911STrond Myklebust /* 18521da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 18531da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 18541da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 18551da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 18561da177e4SLinus Torvalds */ 1857b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 1858b58fed8bSRam Pai unsigned long n) 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds char *t = to; 18611da177e4SLinus Torvalds const char __user *f = from; 18621da177e4SLinus Torvalds char c; 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 18651da177e4SLinus Torvalds return n; 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds while (n) { 18681da177e4SLinus Torvalds if (__get_user(c, f)) { 18691da177e4SLinus Torvalds memset(t, 0, n); 18701da177e4SLinus Torvalds break; 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds *t++ = c; 18731da177e4SLinus Torvalds f++; 18741da177e4SLinus Torvalds n--; 18751da177e4SLinus Torvalds } 18761da177e4SLinus Torvalds return n; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 18801da177e4SLinus Torvalds { 18811da177e4SLinus Torvalds int i; 18821da177e4SLinus Torvalds unsigned long page; 18831da177e4SLinus Torvalds unsigned long size; 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds *where = 0; 18861da177e4SLinus Torvalds if (!data) 18871da177e4SLinus Torvalds return 0; 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 18901da177e4SLinus Torvalds return -ENOMEM; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds /* We only care that *some* data at the address the user 18931da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 18941da177e4SLinus Torvalds * the remainder of the page. 18951da177e4SLinus Torvalds */ 18961da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 18971da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 18981da177e4SLinus Torvalds if (size > PAGE_SIZE) 18991da177e4SLinus Torvalds size = PAGE_SIZE; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 19021da177e4SLinus Torvalds if (!i) { 19031da177e4SLinus Torvalds free_page(page); 19041da177e4SLinus Torvalds return -EFAULT; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds if (i != PAGE_SIZE) 19071da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 19081da177e4SLinus Torvalds *where = page; 19091da177e4SLinus Torvalds return 0; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 1912eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 1913eca6f534SVegard Nossum { 1914eca6f534SVegard Nossum char *tmp; 1915eca6f534SVegard Nossum 1916eca6f534SVegard Nossum if (!data) { 1917eca6f534SVegard Nossum *where = NULL; 1918eca6f534SVegard Nossum return 0; 1919eca6f534SVegard Nossum } 1920eca6f534SVegard Nossum 1921eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 1922eca6f534SVegard Nossum if (IS_ERR(tmp)) 1923eca6f534SVegard Nossum return PTR_ERR(tmp); 1924eca6f534SVegard Nossum 1925eca6f534SVegard Nossum *where = tmp; 1926eca6f534SVegard Nossum return 0; 1927eca6f534SVegard Nossum } 1928eca6f534SVegard Nossum 19291da177e4SLinus Torvalds /* 19301da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 19311da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 19321da177e4SLinus Torvalds * 19331da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 19341da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 19351da177e4SLinus Torvalds * information (or be NULL). 19361da177e4SLinus Torvalds * 19371da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 19381da177e4SLinus Torvalds * When the flags word was introduced its top half was required 19391da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 19401da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 19411da177e4SLinus Torvalds * and must be discarded. 19421da177e4SLinus Torvalds */ 19431da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 19441da177e4SLinus Torvalds unsigned long flags, void *data_page) 19451da177e4SLinus Torvalds { 19462d92ab3cSAl Viro struct path path; 19471da177e4SLinus Torvalds int retval = 0; 19481da177e4SLinus Torvalds int mnt_flags = 0; 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds /* Discard magic */ 19511da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 19521da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds /* Basic sanity checks */ 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 19571da177e4SLinus Torvalds return -EINVAL; 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds if (data_page) 19601da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 19611da177e4SLinus Torvalds 1962a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 1963a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 1964a27ab9f2STetsuo Handa if (retval) 1965a27ab9f2STetsuo Handa return retval; 1966a27ab9f2STetsuo Handa 1967a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 1968a27ab9f2STetsuo Handa type_page, flags, data_page); 1969a27ab9f2STetsuo Handa if (retval) 1970a27ab9f2STetsuo Handa goto dput_out; 1971a27ab9f2STetsuo Handa 1972613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 1973613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 19740a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 19750a1c01c9SMatthew Garrett 19761da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 19771da177e4SLinus Torvalds if (flags & MS_NOSUID) 19781da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 19791da177e4SLinus Torvalds if (flags & MS_NODEV) 19801da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 19811da177e4SLinus Torvalds if (flags & MS_NOEXEC) 19821da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 1983fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 1984fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 1985fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 1986fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 1987d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 1988d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 19892e4b7fcdSDave Hansen if (flags & MS_RDONLY) 19902e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 1991fc33a7bbSChristoph Hellwig 19927a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 1993d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 1994d0adde57SMatthew Garrett MS_STRICTATIME); 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds if (flags & MS_REMOUNT) 19972d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 19981da177e4SLinus Torvalds data_page); 19991da177e4SLinus Torvalds else if (flags & MS_BIND) 20002d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 20019676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 20022d92ab3cSAl Viro retval = do_change_type(&path, flags); 20031da177e4SLinus Torvalds else if (flags & MS_MOVE) 20042d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 20051da177e4SLinus Torvalds else 20062d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 20071da177e4SLinus Torvalds dev_name, data_page); 20081da177e4SLinus Torvalds dput_out: 20092d92ab3cSAl Viro path_put(&path); 20101da177e4SLinus Torvalds return retval; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 2013cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void) 2014cf8d2c11STrond Myklebust { 2015cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2016cf8d2c11STrond Myklebust 2017cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2018cf8d2c11STrond Myklebust if (!new_ns) 2019cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2020cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2021cf8d2c11STrond Myklebust new_ns->root = NULL; 2022cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2023cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2024cf8d2c11STrond Myklebust new_ns->event = 0; 2025cf8d2c11STrond Myklebust return new_ns; 2026cf8d2c11STrond Myklebust } 2027cf8d2c11STrond Myklebust 2028741a2951SJANAK DESAI /* 2029741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2030741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2031741a2951SJANAK DESAI */ 2032e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 20336b3286edSKirill Korotaev struct fs_struct *fs) 20341da177e4SLinus Torvalds { 20356b3286edSKirill Korotaev struct mnt_namespace *new_ns; 20367f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 20371da177e4SLinus Torvalds struct vfsmount *p, *q; 20381da177e4SLinus Torvalds 2039cf8d2c11STrond Myklebust new_ns = alloc_mnt_ns(); 2040cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2041cf8d2c11STrond Myklebust return new_ns; 20421da177e4SLinus Torvalds 2043390c6843SRam Pai down_write(&namespace_sem); 20441da177e4SLinus Torvalds /* First pass: copy the tree topology */ 20456b3286edSKirill Korotaev new_ns->root = copy_tree(mnt_ns->root, mnt_ns->root->mnt_root, 20469676f0c6SRam Pai CL_COPY_ALL | CL_EXPIRE); 20471da177e4SLinus Torvalds if (!new_ns->root) { 2048390c6843SRam Pai up_write(&namespace_sem); 20491da177e4SLinus Torvalds kfree(new_ns); 20505cc4a034SJulia Lawall return ERR_PTR(-ENOMEM); 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 20531da177e4SLinus Torvalds list_add_tail(&new_ns->list, &new_ns->root->mnt_list); 20541da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds /* 20571da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 20581da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 20591da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 20601da177e4SLinus Torvalds */ 20616b3286edSKirill Korotaev p = mnt_ns->root; 20621da177e4SLinus Torvalds q = new_ns->root; 20631da177e4SLinus Torvalds while (p) { 20646b3286edSKirill Korotaev q->mnt_ns = new_ns; 20651da177e4SLinus Torvalds if (fs) { 20666ac08c39SJan Blunck if (p == fs->root.mnt) { 20671da177e4SLinus Torvalds rootmnt = p; 20686ac08c39SJan Blunck fs->root.mnt = mntget(q); 20691da177e4SLinus Torvalds } 20706ac08c39SJan Blunck if (p == fs->pwd.mnt) { 20711da177e4SLinus Torvalds pwdmnt = p; 20726ac08c39SJan Blunck fs->pwd.mnt = mntget(q); 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds } 20756b3286edSKirill Korotaev p = next_mnt(p, mnt_ns->root); 20761da177e4SLinus Torvalds q = next_mnt(q, new_ns->root); 20771da177e4SLinus Torvalds } 2078390c6843SRam Pai up_write(&namespace_sem); 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds if (rootmnt) 20811da177e4SLinus Torvalds mntput(rootmnt); 20821da177e4SLinus Torvalds if (pwdmnt) 20831da177e4SLinus Torvalds mntput(pwdmnt); 20841da177e4SLinus Torvalds 2085741a2951SJANAK DESAI return new_ns; 2086741a2951SJANAK DESAI } 2087741a2951SJANAK DESAI 2088213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2089e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 2090741a2951SJANAK DESAI { 20916b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2092741a2951SJANAK DESAI 2093e3222c4eSBadari Pulavarty BUG_ON(!ns); 20946b3286edSKirill Korotaev get_mnt_ns(ns); 2095741a2951SJANAK DESAI 2096741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2097e3222c4eSBadari Pulavarty return ns; 2098741a2951SJANAK DESAI 2099e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 2100741a2951SJANAK DESAI 21016b3286edSKirill Korotaev put_mnt_ns(ns); 2102e3222c4eSBadari Pulavarty return new_ns; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 2105cf8d2c11STrond Myklebust /** 2106cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2107cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2108cf8d2c11STrond Myklebust */ 2109a2770d86SLinus Torvalds struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt) 2110cf8d2c11STrond Myklebust { 2111cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2112cf8d2c11STrond Myklebust 2113cf8d2c11STrond Myklebust new_ns = alloc_mnt_ns(); 2114cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 2115cf8d2c11STrond Myklebust mnt->mnt_ns = new_ns; 2116cf8d2c11STrond Myklebust new_ns->root = mnt; 2117cf8d2c11STrond Myklebust list_add(&new_ns->list, &new_ns->root->mnt_list); 2118cf8d2c11STrond Myklebust } 2119cf8d2c11STrond Myklebust return new_ns; 2120cf8d2c11STrond Myklebust } 2121a2770d86SLinus Torvalds EXPORT_SYMBOL(create_mnt_ns); 2122cf8d2c11STrond Myklebust 2123bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2124bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 21251da177e4SLinus Torvalds { 2126eca6f534SVegard Nossum int ret; 2127eca6f534SVegard Nossum char *kernel_type; 2128eca6f534SVegard Nossum char *kernel_dir; 2129eca6f534SVegard Nossum char *kernel_dev; 21301da177e4SLinus Torvalds unsigned long data_page; 21311da177e4SLinus Torvalds 2132eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2133eca6f534SVegard Nossum if (ret < 0) 2134eca6f534SVegard Nossum goto out_type; 21351da177e4SLinus Torvalds 2136eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2137eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2138eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2139eca6f534SVegard Nossum goto out_dir; 2140eca6f534SVegard Nossum } 21411da177e4SLinus Torvalds 2142eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2143eca6f534SVegard Nossum if (ret < 0) 2144eca6f534SVegard Nossum goto out_dev; 21451da177e4SLinus Torvalds 2146eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2147eca6f534SVegard Nossum if (ret < 0) 2148eca6f534SVegard Nossum goto out_data; 21491da177e4SLinus Torvalds 2150eca6f534SVegard Nossum ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags, 2151eca6f534SVegard Nossum (void *) data_page); 2152eca6f534SVegard Nossum 21531da177e4SLinus Torvalds free_page(data_page); 2154eca6f534SVegard Nossum out_data: 2155eca6f534SVegard Nossum kfree(kernel_dev); 2156eca6f534SVegard Nossum out_dev: 2157eca6f534SVegard Nossum putname(kernel_dir); 2158eca6f534SVegard Nossum out_dir: 2159eca6f534SVegard Nossum kfree(kernel_type); 2160eca6f534SVegard Nossum out_type: 2161eca6f534SVegard Nossum return ret; 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds /* 21651da177e4SLinus Torvalds * pivot_root Semantics: 21661da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 21671da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 21681da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 21691da177e4SLinus Torvalds * 21701da177e4SLinus Torvalds * Restrictions: 21711da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 21721da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 21731da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 21741da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 21751da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 21761da177e4SLinus Torvalds * 21774a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 21784a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 21794a0d11faSNeil Brown * in this situation. 21804a0d11faSNeil Brown * 21811da177e4SLinus Torvalds * Notes: 21821da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 21831da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 21841da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 21851da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 21861da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 21871da177e4SLinus Torvalds * first. 21881da177e4SLinus Torvalds */ 21893480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 21903480b257SHeiko Carstens const char __user *, put_old) 21911da177e4SLinus Torvalds { 21921da177e4SLinus Torvalds struct vfsmount *tmp; 21932d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 21941da177e4SLinus Torvalds int error; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 21971da177e4SLinus Torvalds return -EPERM; 21981da177e4SLinus Torvalds 21992d8f3038SAl Viro error = user_path_dir(new_root, &new); 22001da177e4SLinus Torvalds if (error) 22011da177e4SLinus Torvalds goto out0; 22021da177e4SLinus Torvalds error = -EINVAL; 22032d8f3038SAl Viro if (!check_mnt(new.mnt)) 22041da177e4SLinus Torvalds goto out1; 22051da177e4SLinus Torvalds 22062d8f3038SAl Viro error = user_path_dir(put_old, &old); 22071da177e4SLinus Torvalds if (error) 22081da177e4SLinus Torvalds goto out1; 22091da177e4SLinus Torvalds 22102d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 22111da177e4SLinus Torvalds if (error) { 22122d8f3038SAl Viro path_put(&old); 22131da177e4SLinus Torvalds goto out1; 22141da177e4SLinus Torvalds } 22151da177e4SLinus Torvalds 2216f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 2217390c6843SRam Pai down_write(&namespace_sem); 22182d8f3038SAl Viro mutex_lock(&old.dentry->d_inode->i_mutex); 22191da177e4SLinus Torvalds error = -EINVAL; 22202d8f3038SAl Viro if (IS_MNT_SHARED(old.mnt) || 22212d8f3038SAl Viro IS_MNT_SHARED(new.mnt->mnt_parent) || 22228c3ee42eSAl Viro IS_MNT_SHARED(root.mnt->mnt_parent)) 222321444403SRam Pai goto out2; 22248c3ee42eSAl Viro if (!check_mnt(root.mnt)) 22251da177e4SLinus Torvalds goto out2; 22261da177e4SLinus Torvalds error = -ENOENT; 2227d83c49f3SAl Viro if (cant_mount(old.dentry)) 22281da177e4SLinus Torvalds goto out2; 2229f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 22301da177e4SLinus Torvalds goto out2; 2231f3da392eSAlexey Dobriyan if (d_unlinked(old.dentry)) 22321da177e4SLinus Torvalds goto out2; 22331da177e4SLinus Torvalds error = -EBUSY; 22342d8f3038SAl Viro if (new.mnt == root.mnt || 22352d8f3038SAl Viro old.mnt == root.mnt) 22361da177e4SLinus Torvalds goto out2; /* loop, on the same file system */ 22371da177e4SLinus Torvalds error = -EINVAL; 22388c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 22391da177e4SLinus Torvalds goto out2; /* not a mountpoint */ 22408c3ee42eSAl Viro if (root.mnt->mnt_parent == root.mnt) 22410bb6fcc1SMiklos Szeredi goto out2; /* not attached */ 22422d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 22431da177e4SLinus Torvalds goto out2; /* not a mountpoint */ 22442d8f3038SAl Viro if (new.mnt->mnt_parent == new.mnt) 22450bb6fcc1SMiklos Szeredi goto out2; /* not attached */ 22464ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 22472d8f3038SAl Viro tmp = old.mnt; 22481da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 22492d8f3038SAl Viro if (tmp != new.mnt) { 22501da177e4SLinus Torvalds for (;;) { 22511da177e4SLinus Torvalds if (tmp->mnt_parent == tmp) 22521da177e4SLinus Torvalds goto out3; /* already mounted on put_old */ 22532d8f3038SAl Viro if (tmp->mnt_parent == new.mnt) 22541da177e4SLinus Torvalds break; 22551da177e4SLinus Torvalds tmp = tmp->mnt_parent; 22561da177e4SLinus Torvalds } 22572d8f3038SAl Viro if (!is_subdir(tmp->mnt_mountpoint, new.dentry)) 22581da177e4SLinus Torvalds goto out3; 22592d8f3038SAl Viro } else if (!is_subdir(old.dentry, new.dentry)) 22601da177e4SLinus Torvalds goto out3; 22612d8f3038SAl Viro detach_mnt(new.mnt, &parent_path); 22628c3ee42eSAl Viro detach_mnt(root.mnt, &root_parent); 22634ac91378SJan Blunck /* mount old root on put_old */ 22642d8f3038SAl Viro attach_mnt(root.mnt, &old); 22654ac91378SJan Blunck /* mount new_root on / */ 22662d8f3038SAl Viro attach_mnt(new.mnt, &root_parent); 22676b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 22681da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 22692d8f3038SAl Viro chroot_fs_refs(&root, &new); 22701da177e4SLinus Torvalds error = 0; 22711a390689SAl Viro path_put(&root_parent); 22721a390689SAl Viro path_put(&parent_path); 22731da177e4SLinus Torvalds out2: 22742d8f3038SAl Viro mutex_unlock(&old.dentry->d_inode->i_mutex); 2275390c6843SRam Pai up_write(&namespace_sem); 22768c3ee42eSAl Viro path_put(&root); 22772d8f3038SAl Viro path_put(&old); 22781da177e4SLinus Torvalds out1: 22792d8f3038SAl Viro path_put(&new); 22801da177e4SLinus Torvalds out0: 22811da177e4SLinus Torvalds return error; 22821da177e4SLinus Torvalds out3: 22831da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 22841da177e4SLinus Torvalds goto out2; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds static void __init init_mount_tree(void) 22881da177e4SLinus Torvalds { 22891da177e4SLinus Torvalds struct vfsmount *mnt; 22906b3286edSKirill Korotaev struct mnt_namespace *ns; 2291ac748a09SJan Blunck struct path root; 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 22941da177e4SLinus Torvalds if (IS_ERR(mnt)) 22951da177e4SLinus Torvalds panic("Can't create rootfs"); 22963b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 22973b22edc5STrond Myklebust if (IS_ERR(ns)) 22981da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 22991da177e4SLinus Torvalds 23006b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 23016b3286edSKirill Korotaev get_mnt_ns(ns); 23021da177e4SLinus Torvalds 2303ac748a09SJan Blunck root.mnt = ns->root; 2304ac748a09SJan Blunck root.dentry = ns->root->mnt_root; 2305ac748a09SJan Blunck 2306ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2307ac748a09SJan Blunck set_fs_root(current->fs, &root); 23081da177e4SLinus Torvalds } 23091da177e4SLinus Torvalds 231074bf17cfSDenis Cheng void __init mnt_init(void) 23111da177e4SLinus Torvalds { 231213f14b4dSEric Dumazet unsigned u; 231315a67dd8SRandy Dunlap int err; 23141da177e4SLinus Torvalds 2315390c6843SRam Pai init_rwsem(&namespace_sem); 2316390c6843SRam Pai 23171da177e4SLinus Torvalds mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount), 231820c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 23191da177e4SLinus Torvalds 2320b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds if (!mount_hashtable) 23231da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 23241da177e4SLinus Torvalds 232513f14b4dSEric Dumazet printk("Mount-cache hash table entries: %lu\n", HASH_SIZE); 23261da177e4SLinus Torvalds 232713f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 232813f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 23291da177e4SLinus Torvalds 233015a67dd8SRandy Dunlap err = sysfs_init(); 233115a67dd8SRandy Dunlap if (err) 233215a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 23338e24eea7SHarvey Harrison __func__, err); 233400d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 233500d26666SGreg Kroah-Hartman if (!fs_kobj) 23368e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 23371da177e4SLinus Torvalds init_rootfs(); 23381da177e4SLinus Torvalds init_mount_tree(); 23391da177e4SLinus Torvalds } 23401da177e4SLinus Torvalds 2341616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 23421da177e4SLinus Torvalds { 234370fbcdf4SRam Pai LIST_HEAD(umount_list); 2344616511d0STrond Myklebust 2345d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2346616511d0STrond Myklebust return; 2347390c6843SRam Pai down_write(&namespace_sem); 23481da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 2349d498b25aSAl Viro umount_tree(ns->root, 0, &umount_list); 23501da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 2351390c6843SRam Pai up_write(&namespace_sem); 235270fbcdf4SRam Pai release_mounts(&umount_list); 23536b3286edSKirill Korotaev kfree(ns); 23541da177e4SLinus Torvalds } 2355cf8d2c11STrond Myklebust EXPORT_SYMBOL(put_mnt_ns); 2356