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> 1499b7db7bSNick Piggin #include <linux/spinlock.h> 1599b7db7bSNick Piggin #include <linux/percpu.h> 161da177e4SLinus Torvalds #include <linux/init.h> 1715a67dd8SRandy Dunlap #include <linux/kernel.h> 181da177e4SLinus Torvalds #include <linux/acct.h> 1916f7e0feSRandy Dunlap #include <linux/capability.h> 203d733633SDave Hansen #include <linux/cpumask.h> 211da177e4SLinus Torvalds #include <linux/module.h> 22f20a9eadSAndrew Morton #include <linux/sysfs.h> 231da177e4SLinus Torvalds #include <linux/seq_file.h> 246b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 251da177e4SLinus Torvalds #include <linux/namei.h> 26b43f3cbdSAlexey Dobriyan #include <linux/nsproxy.h> 271da177e4SLinus Torvalds #include <linux/security.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 2907f3f05cSDavid Howells #include <linux/ramfs.h> 3013f14b4dSEric Dumazet #include <linux/log2.h> 3173cd49ecSMiklos Szeredi #include <linux/idr.h> 325ad4e53bSAl Viro #include <linux/fs_struct.h> 332504c5d6SAndreas Gruenbacher #include <linux/fsnotify.h> 341da177e4SLinus Torvalds #include <asm/uaccess.h> 351da177e4SLinus Torvalds #include <asm/unistd.h> 3607b20889SRam Pai #include "pnode.h" 37948730b0SAdrian Bunk #include "internal.h" 381da177e4SLinus Torvalds 3913f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head)) 4013f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT) 4113f14b4dSEric Dumazet 425addc5ddSAl Viro static int event; 4373cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 44719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 4599b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock); 46f21f6220SAl Viro static int mnt_id_start = 0; 47f21f6220SAl Viro static int mnt_group_start = 1; 485addc5ddSAl Viro 49fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly; 50e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 51390c6843SRam Pai static struct rw_semaphore namespace_sem; 521da177e4SLinus Torvalds 53f87fd4c2SMiklos Szeredi /* /sys/fs */ 5400d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 5500d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 56f87fd4c2SMiklos Szeredi 5799b7db7bSNick Piggin /* 5899b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 5999b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 6099b7db7bSNick Piggin * up the tree. 6199b7db7bSNick Piggin * 6299b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 6399b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 6499b7db7bSNick Piggin */ 6599b7db7bSNick Piggin DEFINE_BRLOCK(vfsmount_lock); 6699b7db7bSNick Piggin 671da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 701da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 7113f14b4dSEric Dumazet tmp = tmp + (tmp >> HASH_SHIFT); 7213f14b4dSEric Dumazet return tmp & (HASH_SIZE - 1); 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 753d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16) 763d733633SDave Hansen 7799b7db7bSNick Piggin /* 7899b7db7bSNick Piggin * allocation is serialized by namespace_sem, but we need the spinlock to 7999b7db7bSNick Piggin * serialize with freeing. 8099b7db7bSNick Piggin */ 81b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 8273cd49ecSMiklos Szeredi { 8373cd49ecSMiklos Szeredi int res; 8473cd49ecSMiklos Szeredi 8573cd49ecSMiklos Szeredi retry: 8673cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 8799b7db7bSNick Piggin spin_lock(&mnt_id_lock); 8815169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 89f21f6220SAl Viro if (!res) 9015169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 9199b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 9273cd49ecSMiklos Szeredi if (res == -EAGAIN) 9373cd49ecSMiklos Szeredi goto retry; 9473cd49ecSMiklos Szeredi 9573cd49ecSMiklos Szeredi return res; 9673cd49ecSMiklos Szeredi } 9773cd49ecSMiklos Szeredi 98b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 9973cd49ecSMiklos Szeredi { 10015169fe7SAl Viro int id = mnt->mnt_id; 10199b7db7bSNick Piggin spin_lock(&mnt_id_lock); 102f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 103f21f6220SAl Viro if (mnt_id_start > id) 104f21f6220SAl Viro mnt_id_start = id; 10599b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 10673cd49ecSMiklos Szeredi } 10773cd49ecSMiklos Szeredi 108719f5d7fSMiklos Szeredi /* 109719f5d7fSMiklos Szeredi * Allocate a new peer group ID 110719f5d7fSMiklos Szeredi * 111719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 112719f5d7fSMiklos Szeredi */ 1134b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 114719f5d7fSMiklos Szeredi { 115f21f6220SAl Viro int res; 116f21f6220SAl Viro 117719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 118719f5d7fSMiklos Szeredi return -ENOMEM; 119719f5d7fSMiklos Szeredi 120f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 121f21f6220SAl Viro mnt_group_start, 12215169fe7SAl Viro &mnt->mnt_group_id); 123f21f6220SAl Viro if (!res) 12415169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 125f21f6220SAl Viro 126f21f6220SAl Viro return res; 127719f5d7fSMiklos Szeredi } 128719f5d7fSMiklos Szeredi 129719f5d7fSMiklos Szeredi /* 130719f5d7fSMiklos Szeredi * Release a peer group ID 131719f5d7fSMiklos Szeredi */ 1324b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 133719f5d7fSMiklos Szeredi { 13415169fe7SAl Viro int id = mnt->mnt_group_id; 135f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 136f21f6220SAl Viro if (mnt_group_start > id) 137f21f6220SAl Viro mnt_group_start = id; 13815169fe7SAl Viro mnt->mnt_group_id = 0; 139719f5d7fSMiklos Szeredi } 140719f5d7fSMiklos Szeredi 141b3e19d92SNick Piggin /* 142b3e19d92SNick Piggin * vfsmount lock must be held for read 143b3e19d92SNick Piggin */ 14483adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 145b3e19d92SNick Piggin { 146b3e19d92SNick Piggin #ifdef CONFIG_SMP 14768e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 148b3e19d92SNick Piggin #else 149b3e19d92SNick Piggin preempt_disable(); 15068e8a9feSAl Viro mnt->mnt_count += n; 151b3e19d92SNick Piggin preempt_enable(); 152b3e19d92SNick Piggin #endif 153b3e19d92SNick Piggin } 154b3e19d92SNick Piggin 155b3e19d92SNick Piggin /* 156b3e19d92SNick Piggin * vfsmount lock must be held for write 157b3e19d92SNick Piggin */ 15883adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 159b3e19d92SNick Piggin { 160b3e19d92SNick Piggin #ifdef CONFIG_SMP 161f03c6599SAl Viro unsigned int count = 0; 162b3e19d92SNick Piggin int cpu; 163b3e19d92SNick Piggin 164b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 16568e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 166b3e19d92SNick Piggin } 167b3e19d92SNick Piggin 168b3e19d92SNick Piggin return count; 169b3e19d92SNick Piggin #else 17068e8a9feSAl Viro return mnt->mnt_count; 171b3e19d92SNick Piggin #endif 172b3e19d92SNick Piggin } 173b3e19d92SNick Piggin 174b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1751da177e4SLinus Torvalds { 176c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 177c63181e6SAl Viro if (mnt) { 17873cd49ecSMiklos Szeredi int err; 17973cd49ecSMiklos Szeredi 180c63181e6SAl Viro err = mnt_alloc_id(mnt); 18188b38782SLi Zefan if (err) 18288b38782SLi Zefan goto out_free_cache; 18388b38782SLi Zefan 18488b38782SLi Zefan if (name) { 185c63181e6SAl Viro mnt->mnt_devname = kstrdup(name, GFP_KERNEL); 186c63181e6SAl Viro if (!mnt->mnt_devname) 18788b38782SLi Zefan goto out_free_id; 18873cd49ecSMiklos Szeredi } 18973cd49ecSMiklos Szeredi 190b3e19d92SNick Piggin #ifdef CONFIG_SMP 191c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 192c63181e6SAl Viro if (!mnt->mnt_pcp) 193b3e19d92SNick Piggin goto out_free_devname; 194b3e19d92SNick Piggin 195c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 196b3e19d92SNick Piggin #else 197c63181e6SAl Viro mnt->mnt_count = 1; 198c63181e6SAl Viro mnt->mnt_writers = 0; 199b3e19d92SNick Piggin #endif 200b3e19d92SNick Piggin 201c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_hash); 202c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 203c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 204c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 205c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 206c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 207c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 208c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2092504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 2102504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 2112504c5d6SAndreas Gruenbacher #endif 2121da177e4SLinus Torvalds } 213c63181e6SAl Viro return mnt; 21488b38782SLi Zefan 215d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 216d3ef3d73Snpiggin@suse.de out_free_devname: 217c63181e6SAl Viro kfree(mnt->mnt_devname); 218d3ef3d73Snpiggin@suse.de #endif 21988b38782SLi Zefan out_free_id: 220c63181e6SAl Viro mnt_free_id(mnt); 22188b38782SLi Zefan out_free_cache: 222c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 22388b38782SLi Zefan return NULL; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2268366025eSDave Hansen /* 2278366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2288366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2298366025eSDave Hansen * We must keep track of when those operations start 2308366025eSDave Hansen * (for permission checks) and when they end, so that 2318366025eSDave Hansen * we can determine when writes are able to occur to 2328366025eSDave Hansen * a filesystem. 2338366025eSDave Hansen */ 2343d733633SDave Hansen /* 2353d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2363d733633SDave Hansen * @mnt: the mount to check for its write status 2373d733633SDave Hansen * 2383d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2393d733633SDave Hansen * It does not guarantee that the filesystem will stay 2403d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2413d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2423d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2433d733633SDave Hansen * r/w. 2443d733633SDave Hansen */ 2453d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2463d733633SDave Hansen { 2472e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2482e4b7fcdSDave Hansen return 1; 2492e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2502e4b7fcdSDave Hansen return 1; 2512e4b7fcdSDave Hansen return 0; 2523d733633SDave Hansen } 2533d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2543d733633SDave Hansen 25583adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2563d733633SDave Hansen { 257d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 25868e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 259d3ef3d73Snpiggin@suse.de #else 26068e8a9feSAl Viro mnt->mnt_writers++; 261d3ef3d73Snpiggin@suse.de #endif 2623d733633SDave Hansen } 2633d733633SDave Hansen 26483adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2653d733633SDave Hansen { 266d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 26768e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 268d3ef3d73Snpiggin@suse.de #else 26968e8a9feSAl Viro mnt->mnt_writers--; 270d3ef3d73Snpiggin@suse.de #endif 271d3ef3d73Snpiggin@suse.de } 272d3ef3d73Snpiggin@suse.de 27383adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 274d3ef3d73Snpiggin@suse.de { 275d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 276d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2773d733633SDave Hansen int cpu; 2783d733633SDave Hansen 2793d733633SDave Hansen for_each_possible_cpu(cpu) { 28068e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2813d733633SDave Hansen } 2823d733633SDave Hansen 283d3ef3d73Snpiggin@suse.de return count; 284d3ef3d73Snpiggin@suse.de #else 285d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 286d3ef3d73Snpiggin@suse.de #endif 2873d733633SDave Hansen } 2883d733633SDave Hansen 2893d733633SDave Hansen /* 2903d733633SDave Hansen * Most r/o checks on a fs are for operations that take 2913d733633SDave Hansen * discrete amounts of time, like a write() or unlink(). 2923d733633SDave Hansen * We must keep track of when those operations start 2933d733633SDave Hansen * (for permission checks) and when they end, so that 2943d733633SDave Hansen * we can determine when writes are able to occur to 2953d733633SDave Hansen * a filesystem. 2963d733633SDave Hansen */ 2978366025eSDave Hansen /** 2988366025eSDave Hansen * mnt_want_write - get write access to a mount 29983adc753SAl Viro * @m: the mount on which to take a write 3008366025eSDave Hansen * 3018366025eSDave Hansen * This tells the low-level filesystem that a write is 3028366025eSDave Hansen * about to be performed to it, and makes sure that 3038366025eSDave Hansen * writes are allowed before returning success. When 3048366025eSDave Hansen * the write operation is finished, mnt_drop_write() 3058366025eSDave Hansen * must be called. This is effectively a refcount. 3068366025eSDave Hansen */ 30783adc753SAl Viro int mnt_want_write(struct vfsmount *m) 3088366025eSDave Hansen { 30983adc753SAl Viro struct mount *mnt = real_mount(m); 3103d733633SDave Hansen int ret = 0; 3113d733633SDave Hansen 312d3ef3d73Snpiggin@suse.de preempt_disable(); 313c6653a83SNick Piggin mnt_inc_writers(mnt); 314d3ef3d73Snpiggin@suse.de /* 315c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 316d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 317d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 318d3ef3d73Snpiggin@suse.de */ 319d3ef3d73Snpiggin@suse.de smp_mb(); 32083adc753SAl Viro while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 321d3ef3d73Snpiggin@suse.de cpu_relax(); 322d3ef3d73Snpiggin@suse.de /* 323d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 324d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 325d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 326d3ef3d73Snpiggin@suse.de */ 327d3ef3d73Snpiggin@suse.de smp_rmb(); 32883adc753SAl Viro if (__mnt_is_readonly(m)) { 329c6653a83SNick Piggin mnt_dec_writers(mnt); 3303d733633SDave Hansen ret = -EROFS; 3313d733633SDave Hansen goto out; 3323d733633SDave Hansen } 3333d733633SDave Hansen out: 334d3ef3d73Snpiggin@suse.de preempt_enable(); 3353d733633SDave Hansen return ret; 3368366025eSDave Hansen } 3378366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3388366025eSDave Hansen 3398366025eSDave Hansen /** 34096029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 34196029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 34296029c4eSnpiggin@suse.de * 34396029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 34496029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 34596029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 34696029c4eSnpiggin@suse.de * on it. This allows some optimisation. 34796029c4eSnpiggin@suse.de * 34896029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 34996029c4eSnpiggin@suse.de * drop the reference. 35096029c4eSnpiggin@suse.de */ 35196029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 35296029c4eSnpiggin@suse.de { 35396029c4eSnpiggin@suse.de /* superblock may be r/o */ 35496029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 35596029c4eSnpiggin@suse.de return -EROFS; 35696029c4eSnpiggin@suse.de preempt_disable(); 35783adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 35896029c4eSnpiggin@suse.de preempt_enable(); 35996029c4eSnpiggin@suse.de return 0; 36096029c4eSnpiggin@suse.de } 36196029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 36296029c4eSnpiggin@suse.de 36396029c4eSnpiggin@suse.de /** 36496029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 36596029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 36696029c4eSnpiggin@suse.de * 36796029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 36896029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 36996029c4eSnpiggin@suse.de */ 37096029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 37196029c4eSnpiggin@suse.de { 3722d8dd38aSOGAWA Hirofumi struct inode *inode = file->f_dentry->d_inode; 3732d8dd38aSOGAWA Hirofumi if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 37496029c4eSnpiggin@suse.de return mnt_want_write(file->f_path.mnt); 37596029c4eSnpiggin@suse.de else 37696029c4eSnpiggin@suse.de return mnt_clone_write(file->f_path.mnt); 37796029c4eSnpiggin@suse.de } 37896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 37996029c4eSnpiggin@suse.de 38096029c4eSnpiggin@suse.de /** 3818366025eSDave Hansen * mnt_drop_write - give up write access to a mount 3828366025eSDave Hansen * @mnt: the mount on which to give up write access 3838366025eSDave Hansen * 3848366025eSDave Hansen * Tells the low-level filesystem that we are done 3858366025eSDave Hansen * performing writes to it. Must be matched with 3868366025eSDave Hansen * mnt_want_write() call above. 3878366025eSDave Hansen */ 3888366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt) 3898366025eSDave Hansen { 390d3ef3d73Snpiggin@suse.de preempt_disable(); 39183adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 392d3ef3d73Snpiggin@suse.de preempt_enable(); 3938366025eSDave Hansen } 3948366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 3958366025eSDave Hansen 3962a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 3972a79f17eSAl Viro { 3982a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 3992a79f17eSAl Viro } 4002a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4012a79f17eSAl Viro 40283adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4038366025eSDave Hansen { 4043d733633SDave Hansen int ret = 0; 4053d733633SDave Hansen 40699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 40783adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 408d3ef3d73Snpiggin@suse.de /* 409d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 410d3ef3d73Snpiggin@suse.de * should be visible before we do. 411d3ef3d73Snpiggin@suse.de */ 412d3ef3d73Snpiggin@suse.de smp_mb(); 413d3ef3d73Snpiggin@suse.de 414d3ef3d73Snpiggin@suse.de /* 415d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 416d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 417d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 418d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 419d3ef3d73Snpiggin@suse.de * 420d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 421d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 422d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 423d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 424d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 425d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 426d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 427d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 428d3ef3d73Snpiggin@suse.de * we're counting up here. 429d3ef3d73Snpiggin@suse.de */ 430c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 431d3ef3d73Snpiggin@suse.de ret = -EBUSY; 432d3ef3d73Snpiggin@suse.de else 43383adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 434d3ef3d73Snpiggin@suse.de /* 435d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 436d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 437d3ef3d73Snpiggin@suse.de */ 438d3ef3d73Snpiggin@suse.de smp_wmb(); 43983adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 44099b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 4413d733633SDave Hansen return ret; 4423d733633SDave Hansen } 4438366025eSDave Hansen 44483adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 4452e4b7fcdSDave Hansen { 44699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 44783adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 44899b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 4492e4b7fcdSDave Hansen } 4502e4b7fcdSDave Hansen 451b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 4521da177e4SLinus Torvalds { 45352ba1621SAl Viro kfree(mnt->mnt_devname); 45473cd49ecSMiklos Szeredi mnt_free_id(mnt); 455d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 45668e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 457d3ef3d73Snpiggin@suse.de #endif 458b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds /* 462a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 463a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 46499b7db7bSNick Piggin * vfsmount_lock must be held for read or write. 4651da177e4SLinus Torvalds */ 466c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 467a05964f3SRam Pai int dir) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 4701da177e4SLinus Torvalds struct list_head *tmp = head; 471c7105365SAl Viro struct mount *p, *found = NULL; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds for (;;) { 474a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 4751da177e4SLinus Torvalds p = NULL; 4761da177e4SLinus Torvalds if (tmp == head) 4771da177e4SLinus Torvalds break; 4781b8e5564SAl Viro p = list_entry(tmp, struct mount, mnt_hash); 479a73324daSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) { 480a05964f3SRam Pai found = p; 4811da177e4SLinus Torvalds break; 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds return found; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 487a05964f3SRam Pai /* 488a05964f3SRam Pai * lookup_mnt increments the ref count before returning 489a05964f3SRam Pai * the vfsmount struct. 490a05964f3SRam Pai */ 4911c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 492a05964f3SRam Pai { 493c7105365SAl Viro struct mount *child_mnt; 49499b7db7bSNick Piggin 49599b7db7bSNick Piggin br_read_lock(vfsmount_lock); 496c7105365SAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry, 1); 497c7105365SAl Viro if (child_mnt) { 498c7105365SAl Viro mnt_add_count(child_mnt, 1); 49999b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 500c7105365SAl Viro return &child_mnt->mnt; 501c7105365SAl Viro } else { 502c7105365SAl Viro br_read_unlock(vfsmount_lock); 503c7105365SAl Viro return NULL; 504c7105365SAl Viro } 505a05964f3SRam Pai } 506a05964f3SRam Pai 507143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 5081da177e4SLinus Torvalds { 5096b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 51299b7db7bSNick Piggin /* 51399b7db7bSNick Piggin * vfsmount lock must be held for write 51499b7db7bSNick Piggin */ 5156b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 5165addc5ddSAl Viro { 5175addc5ddSAl Viro if (ns) { 5185addc5ddSAl Viro ns->event = ++event; 5195addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5205addc5ddSAl Viro } 5215addc5ddSAl Viro } 5225addc5ddSAl Viro 52399b7db7bSNick Piggin /* 52499b7db7bSNick Piggin * vfsmount lock must be held for write 52599b7db7bSNick Piggin */ 5266b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 5275addc5ddSAl Viro { 5285addc5ddSAl Viro if (ns && ns->event != event) { 5295addc5ddSAl Viro ns->event = event; 5305addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5315addc5ddSAl Viro } 5325addc5ddSAl Viro } 5335addc5ddSAl Viro 53499b7db7bSNick Piggin /* 5355f57cbccSNick Piggin * Clear dentry's mounted state if it has no remaining mounts. 5365f57cbccSNick Piggin * vfsmount_lock must be held for write. 5375f57cbccSNick Piggin */ 538aa0a4cf0SAl Viro static void dentry_reset_mounted(struct dentry *dentry) 5395f57cbccSNick Piggin { 5405f57cbccSNick Piggin unsigned u; 5415f57cbccSNick Piggin 5425f57cbccSNick Piggin for (u = 0; u < HASH_SIZE; u++) { 543d5e50f74SAl Viro struct mount *p; 5445f57cbccSNick Piggin 5451b8e5564SAl Viro list_for_each_entry(p, &mount_hashtable[u], mnt_hash) { 546a73324daSAl Viro if (p->mnt_mountpoint == dentry) 5475f57cbccSNick Piggin return; 5485f57cbccSNick Piggin } 5495f57cbccSNick Piggin } 5505f57cbccSNick Piggin spin_lock(&dentry->d_lock); 5515f57cbccSNick Piggin dentry->d_flags &= ~DCACHE_MOUNTED; 5525f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 5535f57cbccSNick Piggin } 5545f57cbccSNick Piggin 5555f57cbccSNick Piggin /* 55699b7db7bSNick Piggin * vfsmount lock must be held for write 55799b7db7bSNick Piggin */ 558419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 5591da177e4SLinus Torvalds { 560a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 5610714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 5620714a533SAl Viro mnt->mnt_parent = mnt; 563a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 5646b41d536SAl Viro list_del_init(&mnt->mnt_child); 5651b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 566aa0a4cf0SAl Viro dentry_reset_mounted(old_path->dentry); 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 56999b7db7bSNick Piggin /* 57099b7db7bSNick Piggin * vfsmount lock must be held for write 57199b7db7bSNick Piggin */ 57214cf1fa8SAl Viro void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry, 57344d964d6SAl Viro struct mount *child_mnt) 574b90fa9aeSRam Pai { 5753a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 576a73324daSAl Viro child_mnt->mnt_mountpoint = dget(dentry); 5773a2393d7SAl Viro child_mnt->mnt_parent = mnt; 5785f57cbccSNick Piggin spin_lock(&dentry->d_lock); 5795f57cbccSNick Piggin dentry->d_flags |= DCACHE_MOUNTED; 5805f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 581b90fa9aeSRam Pai } 582b90fa9aeSRam Pai 58399b7db7bSNick Piggin /* 58499b7db7bSNick Piggin * vfsmount lock must be held for write 58599b7db7bSNick Piggin */ 586419148daSAl Viro static void attach_mnt(struct mount *mnt, struct path *path) 5871da177e4SLinus Torvalds { 58814cf1fa8SAl Viro mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt); 5891b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 5901a390689SAl Viro hash(path->mnt, path->dentry)); 5916b41d536SAl Viro list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts); 592b90fa9aeSRam Pai } 593b90fa9aeSRam Pai 59483adc753SAl Viro static inline void __mnt_make_longterm(struct mount *mnt) 5957e3d0eb0SAl Viro { 5967e3d0eb0SAl Viro #ifdef CONFIG_SMP 59768e8a9feSAl Viro atomic_inc(&mnt->mnt_longterm); 5987e3d0eb0SAl Viro #endif 5997e3d0eb0SAl Viro } 6007e3d0eb0SAl Viro 6017e3d0eb0SAl Viro /* needs vfsmount lock for write */ 60283adc753SAl Viro static inline void __mnt_make_shortterm(struct mount *mnt) 6037e3d0eb0SAl Viro { 6047e3d0eb0SAl Viro #ifdef CONFIG_SMP 60568e8a9feSAl Viro atomic_dec(&mnt->mnt_longterm); 6067e3d0eb0SAl Viro #endif 6077e3d0eb0SAl Viro } 6087e3d0eb0SAl Viro 609b90fa9aeSRam Pai /* 61099b7db7bSNick Piggin * vfsmount lock must be held for write 611b90fa9aeSRam Pai */ 6124b2619a5SAl Viro static void commit_tree(struct mount *mnt) 613b90fa9aeSRam Pai { 6140714a533SAl Viro struct mount *parent = mnt->mnt_parent; 61583adc753SAl Viro struct mount *m; 616b90fa9aeSRam Pai LIST_HEAD(head); 617143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 618b90fa9aeSRam Pai 6190714a533SAl Viro BUG_ON(parent == mnt); 620b90fa9aeSRam Pai 6211a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 6221a4eeaf2SAl Viro list_for_each_entry(m, &head, mnt_list) { 623143c8c91SAl Viro m->mnt_ns = n; 6247e3d0eb0SAl Viro __mnt_make_longterm(m); 625f03c6599SAl Viro } 626f03c6599SAl Viro 627b90fa9aeSRam Pai list_splice(&head, n->list.prev); 628b90fa9aeSRam Pai 6291b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 630a73324daSAl Viro hash(&parent->mnt, mnt->mnt_mountpoint)); 6316b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 6326b3286edSKirill Korotaev touch_mnt_namespace(n); 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 635909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 6361da177e4SLinus Torvalds { 6376b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 6386b41d536SAl Viro if (next == &p->mnt_mounts) { 6391da177e4SLinus Torvalds while (1) { 640909b0a88SAl Viro if (p == root) 6411da177e4SLinus Torvalds return NULL; 6426b41d536SAl Viro next = p->mnt_child.next; 6436b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 6441da177e4SLinus Torvalds break; 6450714a533SAl Viro p = p->mnt_parent; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds } 6486b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 651315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 6529676f0c6SRam Pai { 6536b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 6546b41d536SAl Viro while (prev != &p->mnt_mounts) { 6556b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 6566b41d536SAl Viro prev = p->mnt_mounts.prev; 6579676f0c6SRam Pai } 6589676f0c6SRam Pai return p; 6599676f0c6SRam Pai } 6609676f0c6SRam Pai 6619d412a43SAl Viro struct vfsmount * 6629d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 6639d412a43SAl Viro { 664b105e270SAl Viro struct mount *mnt; 6659d412a43SAl Viro struct dentry *root; 6669d412a43SAl Viro 6679d412a43SAl Viro if (!type) 6689d412a43SAl Viro return ERR_PTR(-ENODEV); 6699d412a43SAl Viro 6709d412a43SAl Viro mnt = alloc_vfsmnt(name); 6719d412a43SAl Viro if (!mnt) 6729d412a43SAl Viro return ERR_PTR(-ENOMEM); 6739d412a43SAl Viro 6749d412a43SAl Viro if (flags & MS_KERNMOUNT) 675b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 6769d412a43SAl Viro 6779d412a43SAl Viro root = mount_fs(type, flags, name, data); 6789d412a43SAl Viro if (IS_ERR(root)) { 6799d412a43SAl Viro free_vfsmnt(mnt); 6809d412a43SAl Viro return ERR_CAST(root); 6819d412a43SAl Viro } 6829d412a43SAl Viro 683b105e270SAl Viro mnt->mnt.mnt_root = root; 684b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 685a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 6860714a533SAl Viro mnt->mnt_parent = mnt; 687b105e270SAl Viro return &mnt->mnt; 6889d412a43SAl Viro } 6899d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 6909d412a43SAl Viro 69187129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 69236341f64SRam Pai int flag) 6931da177e4SLinus Torvalds { 69487129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 69552ba1621SAl Viro struct mount *mnt = alloc_vfsmnt(old->mnt_devname); 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds if (mnt) { 698719f5d7fSMiklos Szeredi if (flag & (CL_SLAVE | CL_PRIVATE)) 69915169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 700719f5d7fSMiklos Szeredi else 70115169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 702719f5d7fSMiklos Szeredi 70315169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 704b105e270SAl Viro int err = mnt_alloc_group_id(mnt); 705719f5d7fSMiklos Szeredi if (err) 706719f5d7fSMiklos Szeredi goto out_free; 707719f5d7fSMiklos Szeredi } 708719f5d7fSMiklos Szeredi 70987129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 7101da177e4SLinus Torvalds atomic_inc(&sb->s_active); 711b105e270SAl Viro mnt->mnt.mnt_sb = sb; 712b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 713a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7140714a533SAl Viro mnt->mnt_parent = mnt; 715b90fa9aeSRam Pai 7165afe0022SRam Pai if (flag & CL_SLAVE) { 7176776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 71832301920SAl Viro mnt->mnt_master = old; 719fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 7208aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 721fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 7226776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 723d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 7246776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 725d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 7265afe0022SRam Pai } 727b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 7280f0afb1dSAl Viro set_mnt_shared(mnt); 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 7311da177e4SLinus Torvalds * as the original if that was on one */ 73236341f64SRam Pai if (flag & CL_EXPIRE) { 7336776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 7346776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 7351da177e4SLinus Torvalds } 73636341f64SRam Pai } 737cb338d06SAl Viro return mnt; 738719f5d7fSMiklos Szeredi 739719f5d7fSMiklos Szeredi out_free: 740719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 741719f5d7fSMiklos Szeredi return NULL; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 74483adc753SAl Viro static inline void mntfree(struct mount *mnt) 7451da177e4SLinus Torvalds { 74683adc753SAl Viro struct vfsmount *m = &mnt->mnt; 74783adc753SAl Viro struct super_block *sb = m->mnt_sb; 748b3e19d92SNick Piggin 7493d733633SDave Hansen /* 7503d733633SDave Hansen * This probably indicates that somebody messed 7513d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 7523d733633SDave Hansen * happens, the filesystem was probably unable 7533d733633SDave Hansen * to make r/w->r/o transitions. 7543d733633SDave Hansen */ 755d3ef3d73Snpiggin@suse.de /* 756b3e19d92SNick Piggin * The locking used to deal with mnt_count decrement provides barriers, 757b3e19d92SNick Piggin * so mnt_get_writers() below is safe. 758d3ef3d73Snpiggin@suse.de */ 759c6653a83SNick Piggin WARN_ON(mnt_get_writers(mnt)); 76083adc753SAl Viro fsnotify_vfsmount_delete(m); 76183adc753SAl Viro dput(m->mnt_root); 76283adc753SAl Viro free_vfsmnt(mnt); 7631da177e4SLinus Torvalds deactivate_super(sb); 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 766900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 7677b7b1aceSAl Viro { 768b3e19d92SNick Piggin put_again: 769f03c6599SAl Viro #ifdef CONFIG_SMP 770b3e19d92SNick Piggin br_read_lock(vfsmount_lock); 77168e8a9feSAl Viro if (likely(atomic_read(&mnt->mnt_longterm))) { 772aa9c0e07SAl Viro mnt_add_count(mnt, -1); 773b3e19d92SNick Piggin br_read_unlock(vfsmount_lock); 77499b7db7bSNick Piggin return; 775b3e19d92SNick Piggin } 776b3e19d92SNick Piggin br_read_unlock(vfsmount_lock); 777b3e19d92SNick Piggin 77899b7db7bSNick Piggin br_write_lock(vfsmount_lock); 779aa9c0e07SAl Viro mnt_add_count(mnt, -1); 780b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 78199b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 78299b7db7bSNick Piggin return; 78399b7db7bSNick Piggin } 784b3e19d92SNick Piggin #else 785aa9c0e07SAl Viro mnt_add_count(mnt, -1); 786b3e19d92SNick Piggin if (likely(mnt_get_count(mnt))) 787b3e19d92SNick Piggin return; 788b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 789f03c6599SAl Viro #endif 790863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 791863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 792863d684fSAl Viro mnt->mnt_pinned = 0; 793b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 794900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 795b3e19d92SNick Piggin goto put_again; 796b3e19d92SNick Piggin } 797b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 798b3e19d92SNick Piggin mntfree(mnt); 799b3e19d92SNick Piggin } 800b3e19d92SNick Piggin 801b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 802b3e19d92SNick Piggin { 803b3e19d92SNick Piggin if (mnt) { 804863d684fSAl Viro struct mount *m = real_mount(mnt); 805b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 806863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 807863d684fSAl Viro m->mnt_expiry_mark = 0; 808863d684fSAl Viro mntput_no_expire(m); 809b3e19d92SNick Piggin } 810b3e19d92SNick Piggin } 811b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 812b3e19d92SNick Piggin 813b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 814b3e19d92SNick Piggin { 815b3e19d92SNick Piggin if (mnt) 81683adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 817b3e19d92SNick Piggin return mnt; 818b3e19d92SNick Piggin } 819b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 820b3e19d92SNick Piggin 8217b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 8227b7b1aceSAl Viro { 82399b7db7bSNick Piggin br_write_lock(vfsmount_lock); 824863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 82599b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 8267b7b1aceSAl Viro } 8277b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 8287b7b1aceSAl Viro 829863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 8307b7b1aceSAl Viro { 831863d684fSAl Viro struct mount *mnt = real_mount(m); 83299b7db7bSNick Piggin br_write_lock(vfsmount_lock); 8337b7b1aceSAl Viro if (mnt->mnt_pinned) { 834863d684fSAl Viro mnt_add_count(mnt, 1); 8357b7b1aceSAl Viro mnt->mnt_pinned--; 8367b7b1aceSAl Viro } 83799b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 8387b7b1aceSAl Viro } 8397b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 8401da177e4SLinus Torvalds 841b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 842b3b304a2SMiklos Szeredi { 843b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 844b3b304a2SMiklos Szeredi } 845b3b304a2SMiklos Szeredi 846b3b304a2SMiklos Szeredi /* 847b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 848b3b304a2SMiklos Szeredi * implement more complex mount option showing. 849b3b304a2SMiklos Szeredi * 850b3b304a2SMiklos Szeredi * See also save_mount_options(). 851b3b304a2SMiklos Szeredi */ 852b3b304a2SMiklos Szeredi int generic_show_options(struct seq_file *m, struct vfsmount *mnt) 853b3b304a2SMiklos Szeredi { 8542a32cebdSAl Viro const char *options; 8552a32cebdSAl Viro 8562a32cebdSAl Viro rcu_read_lock(); 8572a32cebdSAl Viro options = rcu_dereference(mnt->mnt_sb->s_options); 858b3b304a2SMiklos Szeredi 859b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 860b3b304a2SMiklos Szeredi seq_putc(m, ','); 861b3b304a2SMiklos Szeredi mangle(m, options); 862b3b304a2SMiklos Szeredi } 8632a32cebdSAl Viro rcu_read_unlock(); 864b3b304a2SMiklos Szeredi 865b3b304a2SMiklos Szeredi return 0; 866b3b304a2SMiklos Szeredi } 867b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 868b3b304a2SMiklos Szeredi 869b3b304a2SMiklos Szeredi /* 870b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 871b3b304a2SMiklos Szeredi * called from the fill_super() callback. 872b3b304a2SMiklos Szeredi * 873b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 874b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 875b3b304a2SMiklos Szeredi * remount fails. 876b3b304a2SMiklos Szeredi * 877b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 878b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 879b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 880b3b304a2SMiklos Szeredi * any more. 881b3b304a2SMiklos Szeredi */ 882b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 883b3b304a2SMiklos Szeredi { 8842a32cebdSAl Viro BUG_ON(sb->s_options); 8852a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 886b3b304a2SMiklos Szeredi } 887b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 888b3b304a2SMiklos Szeredi 8892a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 8902a32cebdSAl Viro { 8912a32cebdSAl Viro char *old = sb->s_options; 8922a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 8932a32cebdSAl Viro if (old) { 8942a32cebdSAl Viro synchronize_rcu(); 8952a32cebdSAl Viro kfree(old); 8962a32cebdSAl Viro } 8972a32cebdSAl Viro } 8982a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 8992a32cebdSAl Viro 900a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 9010226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 9021da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 9031da177e4SLinus Torvalds { 9040226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 9051da177e4SLinus Torvalds 906390c6843SRam Pai down_read(&namespace_sem); 907a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 9111da177e4SLinus Torvalds { 9120226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 913b0765fb8SPavel Emelianov 914a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 9181da177e4SLinus Torvalds { 919390c6843SRam Pai up_read(&namespace_sem); 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds 9220226f492SAl Viro static int m_show(struct seq_file *m, void *v) 9239f5596afSAl Viro { 9240226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 9251a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 9260226f492SAl Viro return p->show(m, &r->mnt); 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 929a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 9301da177e4SLinus Torvalds .start = m_start, 9311da177e4SLinus Torvalds .next = m_next, 9321da177e4SLinus Torvalds .stop = m_stop, 9330226f492SAl Viro .show = m_show, 934b4629fe2SChuck Lever }; 935a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 936b4629fe2SChuck Lever 9371da177e4SLinus Torvalds /** 9381da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 9391da177e4SLinus Torvalds * @mnt: root of mount tree 9401da177e4SLinus Torvalds * 9411da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 9421da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 9431da177e4SLinus Torvalds * busy. 9441da177e4SLinus Torvalds */ 945909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 9461da177e4SLinus Torvalds { 947909b0a88SAl Viro struct mount *mnt = real_mount(m); 94836341f64SRam Pai int actual_refs = 0; 94936341f64SRam Pai int minimum_refs = 0; 950315fc83eSAl Viro struct mount *p; 951909b0a88SAl Viro BUG_ON(!m); 9521da177e4SLinus Torvalds 953b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 954b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 955909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 95683adc753SAl Viro actual_refs += mnt_get_count(p); 9571da177e4SLinus Torvalds minimum_refs += 2; 9581da177e4SLinus Torvalds } 959b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds if (actual_refs > minimum_refs) 9621da177e4SLinus Torvalds return 0; 963e3474a8eSIan Kent 964e3474a8eSIan Kent return 1; 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /** 9701da177e4SLinus Torvalds * may_umount - check if a mount point is busy 9711da177e4SLinus Torvalds * @mnt: root of mount 9721da177e4SLinus Torvalds * 9731da177e4SLinus Torvalds * This is called to check if a mount point has any 9741da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 9751da177e4SLinus Torvalds * mount has sub mounts this will return busy 9761da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 9771da177e4SLinus Torvalds * 9781da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 9791da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 9801da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 9811da177e4SLinus Torvalds */ 9821da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 9831da177e4SLinus Torvalds { 984e3474a8eSIan Kent int ret = 1; 9858ad08d8aSAl Viro down_read(&namespace_sem); 986b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 9871ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 988e3474a8eSIan Kent ret = 0; 989b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 9908ad08d8aSAl Viro up_read(&namespace_sem); 991a05964f3SRam Pai return ret; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 9951da177e4SLinus Torvalds 996b90fa9aeSRam Pai void release_mounts(struct list_head *head) 9971da177e4SLinus Torvalds { 998d5e50f74SAl Viro struct mount *mnt; 99970fbcdf4SRam Pai while (!list_empty(head)) { 10001b8e5564SAl Viro mnt = list_first_entry(head, struct mount, mnt_hash); 10011b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 1002676da58dSAl Viro if (mnt_has_parent(mnt)) { 100370fbcdf4SRam Pai struct dentry *dentry; 1004863d684fSAl Viro struct mount *m; 100599b7db7bSNick Piggin 100699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1007a73324daSAl Viro dentry = mnt->mnt_mountpoint; 1008863d684fSAl Viro m = mnt->mnt_parent; 1009a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10100714a533SAl Viro mnt->mnt_parent = mnt; 10117c4b93d8SAl Viro m->mnt_ghosts--; 101299b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 101370fbcdf4SRam Pai dput(dentry); 1014863d684fSAl Viro mntput(&m->mnt); 10151da177e4SLinus Torvalds } 1016d5e50f74SAl Viro mntput(&mnt->mnt); 101770fbcdf4SRam Pai } 101870fbcdf4SRam Pai } 101970fbcdf4SRam Pai 102099b7db7bSNick Piggin /* 102199b7db7bSNick Piggin * vfsmount lock must be held for write 102299b7db7bSNick Piggin * namespace_sem must be held for write 102399b7db7bSNick Piggin */ 1024761d5c38SAl Viro void umount_tree(struct mount *mnt, int propagate, struct list_head *kill) 102570fbcdf4SRam Pai { 10267b8a53fdSAl Viro LIST_HEAD(tmp_list); 1027315fc83eSAl Viro struct mount *p; 102870fbcdf4SRam Pai 1029909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) 10301b8e5564SAl Viro list_move(&p->mnt_hash, &tmp_list); 103170fbcdf4SRam Pai 1032a05964f3SRam Pai if (propagate) 10337b8a53fdSAl Viro propagate_umount(&tmp_list); 1034a05964f3SRam Pai 10351b8e5564SAl Viro list_for_each_entry(p, &tmp_list, mnt_hash) { 10366776db3dSAl Viro list_del_init(&p->mnt_expire); 10371a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1038143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 1039143c8c91SAl Viro p->mnt_ns = NULL; 104083adc753SAl Viro __mnt_make_shortterm(p); 10416b41d536SAl Viro list_del_init(&p->mnt_child); 1042676da58dSAl Viro if (mnt_has_parent(p)) { 1043863d684fSAl Viro p->mnt_parent->mnt_ghosts++; 1044a73324daSAl Viro dentry_reset_mounted(p->mnt_mountpoint); 10457c4b93d8SAl Viro } 10460f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 10471da177e4SLinus Torvalds } 10487b8a53fdSAl Viro list_splice(&tmp_list, kill); 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 1051692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts); 1052c35038beSAl Viro 10531ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 10541da177e4SLinus Torvalds { 10551ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 10561da177e4SLinus Torvalds int retval; 105770fbcdf4SRam Pai LIST_HEAD(umount_list); 10581da177e4SLinus Torvalds 10591ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 10601da177e4SLinus Torvalds if (retval) 10611da177e4SLinus Torvalds return retval; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds /* 10641da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 10651da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 10661da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 10671da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 10681da177e4SLinus Torvalds */ 10691da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 10701ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 10711da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 10721da177e4SLinus Torvalds return -EINVAL; 10731da177e4SLinus Torvalds 1074b3e19d92SNick Piggin /* 1075b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1076b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1077b3e19d92SNick Piggin */ 1078b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 107983adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1080bf9faa2aSJ. R. Okajima br_write_unlock(vfsmount_lock); 10811da177e4SLinus Torvalds return -EBUSY; 1082b3e19d92SNick Piggin } 1083b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 10841da177e4SLinus Torvalds 1085863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 10861da177e4SLinus Torvalds return -EAGAIN; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* 10901da177e4SLinus Torvalds * If we may have to abort operations to get out of this 10911da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 10921da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 10931da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 10941da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 10951da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 10961da177e4SLinus Torvalds * about for the moment. 10971da177e4SLinus Torvalds */ 10981da177e4SLinus Torvalds 109942faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 110042faad99SAl Viro sb->s_op->umount_begin(sb); 110142faad99SAl Viro } 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds /* 11041da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 11051da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 11061da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 11071da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 11081da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 11091da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 11101da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 11111da177e4SLinus Torvalds */ 11121ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 11131da177e4SLinus Torvalds /* 11141da177e4SLinus Torvalds * Special case for "unmounting" root ... 11151da177e4SLinus Torvalds * we just try to remount it readonly. 11161da177e4SLinus Torvalds */ 11171da177e4SLinus Torvalds down_write(&sb->s_umount); 11184aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 11191da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 11201da177e4SLinus Torvalds up_write(&sb->s_umount); 11211da177e4SLinus Torvalds return retval; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 1124390c6843SRam Pai down_write(&namespace_sem); 112599b7db7bSNick Piggin br_write_lock(vfsmount_lock); 11265addc5ddSAl Viro event++; 11271da177e4SLinus Torvalds 1128c35038beSAl Viro if (!(flags & MNT_DETACH)) 11291ab59738SAl Viro shrink_submounts(mnt, &umount_list); 1130c35038beSAl Viro 11311da177e4SLinus Torvalds retval = -EBUSY; 1132a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 11331a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 11341ab59738SAl Viro umount_tree(mnt, 1, &umount_list); 11351da177e4SLinus Torvalds retval = 0; 11361da177e4SLinus Torvalds } 113799b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1138390c6843SRam Pai up_write(&namespace_sem); 113970fbcdf4SRam Pai release_mounts(&umount_list); 11401da177e4SLinus Torvalds return retval; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds /* 11441da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 11451da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 11461da177e4SLinus Torvalds * 11471da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 11481da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 11491da177e4SLinus Torvalds */ 11501da177e4SLinus Torvalds 1151bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 11521da177e4SLinus Torvalds { 11532d8f3038SAl Viro struct path path; 1154900148dcSAl Viro struct mount *mnt; 11551da177e4SLinus Torvalds int retval; 1156db1f05bbSMiklos Szeredi int lookup_flags = 0; 11571da177e4SLinus Torvalds 1158db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1159db1f05bbSMiklos Szeredi return -EINVAL; 1160db1f05bbSMiklos Szeredi 1161db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1162db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1163db1f05bbSMiklos Szeredi 1164db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 11651da177e4SLinus Torvalds if (retval) 11661da177e4SLinus Torvalds goto out; 1167900148dcSAl Viro mnt = real_mount(path.mnt); 11681da177e4SLinus Torvalds retval = -EINVAL; 11692d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 11701da177e4SLinus Torvalds goto dput_and_out; 1171143c8c91SAl Viro if (!check_mnt(mnt)) 11721da177e4SLinus Torvalds goto dput_and_out; 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds retval = -EPERM; 11751da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11761da177e4SLinus Torvalds goto dput_and_out; 11771da177e4SLinus Torvalds 1178900148dcSAl Viro retval = do_umount(mnt, flags); 11791da177e4SLinus Torvalds dput_and_out: 1180429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 11812d8f3038SAl Viro dput(path.dentry); 1182900148dcSAl Viro mntput_no_expire(mnt); 11831da177e4SLinus Torvalds out: 11841da177e4SLinus Torvalds return retval; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds /* 11901da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 11911da177e4SLinus Torvalds */ 1192bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 11931da177e4SLinus Torvalds { 11941da177e4SLinus Torvalds return sys_umount(name, 0); 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds #endif 11981da177e4SLinus Torvalds 11992d92ab3cSAl Viro static int mount_is_safe(struct path *path) 12001da177e4SLinus Torvalds { 12011da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 12021da177e4SLinus Torvalds return 0; 12031da177e4SLinus Torvalds return -EPERM; 12041da177e4SLinus Torvalds #ifdef notyet 12052d92ab3cSAl Viro if (S_ISLNK(path->dentry->d_inode->i_mode)) 12061da177e4SLinus Torvalds return -EPERM; 12072d92ab3cSAl Viro if (path->dentry->d_inode->i_mode & S_ISVTX) { 1208da9592edSDavid Howells if (current_uid() != path->dentry->d_inode->i_uid) 12091da177e4SLinus Torvalds return -EPERM; 12101da177e4SLinus Torvalds } 12112d92ab3cSAl Viro if (inode_permission(path->dentry->d_inode, MAY_WRITE)) 12121da177e4SLinus Torvalds return -EPERM; 12131da177e4SLinus Torvalds return 0; 12141da177e4SLinus Torvalds #endif 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds 121787129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 121836341f64SRam Pai int flag) 12191da177e4SLinus Torvalds { 1220a73324daSAl Viro struct mount *res, *p, *q, *r; 12211a390689SAl Viro struct path path; 12221da177e4SLinus Torvalds 1223fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 12249676f0c6SRam Pai return NULL; 12259676f0c6SRam Pai 122636341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 12271da177e4SLinus Torvalds if (!q) 12281da177e4SLinus Torvalds goto Enomem; 1229a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds p = mnt; 12326b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1233315fc83eSAl Viro struct mount *s; 12347ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 12351da177e4SLinus Torvalds continue; 12361da177e4SLinus Torvalds 1237909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 1238fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 12399676f0c6SRam Pai s = skip_mnt_tree(s); 12409676f0c6SRam Pai continue; 12419676f0c6SRam Pai } 12420714a533SAl Viro while (p != s->mnt_parent) { 12430714a533SAl Viro p = p->mnt_parent; 12440714a533SAl Viro q = q->mnt_parent; 12451da177e4SLinus Torvalds } 124687129cc0SAl Viro p = s; 1247cb338d06SAl Viro path.mnt = &q->mnt; 1248a73324daSAl Viro path.dentry = p->mnt_mountpoint; 124987129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 12501da177e4SLinus Torvalds if (!q) 12511da177e4SLinus Torvalds goto Enomem; 125299b7db7bSNick Piggin br_write_lock(vfsmount_lock); 12531a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 1254cb338d06SAl Viro attach_mnt(q, &path); 125599b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds return res; 12591da177e4SLinus Torvalds Enomem: 12601da177e4SLinus Torvalds if (res) { 126170fbcdf4SRam Pai LIST_HEAD(umount_list); 126299b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1263761d5c38SAl Viro umount_tree(res, 0, &umount_list); 126499b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 126570fbcdf4SRam Pai release_mounts(&umount_list); 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds return NULL; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 1270589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 12718aec0809SAl Viro { 1272cb338d06SAl Viro struct mount *tree; 12731a60a280SAl Viro down_write(&namespace_sem); 127487129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 127587129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 12761a60a280SAl Viro up_write(&namespace_sem); 1277cb338d06SAl Viro return tree ? &tree->mnt : NULL; 12788aec0809SAl Viro } 12798aec0809SAl Viro 12808aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 12818aec0809SAl Viro { 12828aec0809SAl Viro LIST_HEAD(umount_list); 12831a60a280SAl Viro down_write(&namespace_sem); 128499b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1285761d5c38SAl Viro umount_tree(real_mount(mnt), 0, &umount_list); 128699b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 12871a60a280SAl Viro up_write(&namespace_sem); 12888aec0809SAl Viro release_mounts(&umount_list); 12898aec0809SAl Viro } 12908aec0809SAl Viro 12911f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 12921f707137SAl Viro struct vfsmount *root) 12931f707137SAl Viro { 12941a4eeaf2SAl Viro struct mount *mnt; 12951f707137SAl Viro int res = f(root, arg); 12961f707137SAl Viro if (res) 12971f707137SAl Viro return res; 12981a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 12991a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 13001f707137SAl Viro if (res) 13011f707137SAl Viro return res; 13021f707137SAl Viro } 13031f707137SAl Viro return 0; 13041f707137SAl Viro } 13051f707137SAl Viro 13064b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1307719f5d7fSMiklos Szeredi { 1308315fc83eSAl Viro struct mount *p; 1309719f5d7fSMiklos Szeredi 1310909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1311fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 13124b8b21f4SAl Viro mnt_release_group_id(p); 1313719f5d7fSMiklos Szeredi } 1314719f5d7fSMiklos Szeredi } 1315719f5d7fSMiklos Szeredi 13164b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1317719f5d7fSMiklos Szeredi { 1318315fc83eSAl Viro struct mount *p; 1319719f5d7fSMiklos Szeredi 1320909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1321fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 13224b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1323719f5d7fSMiklos Szeredi if (err) { 13244b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1325719f5d7fSMiklos Szeredi return err; 1326719f5d7fSMiklos Szeredi } 1327719f5d7fSMiklos Szeredi } 1328719f5d7fSMiklos Szeredi } 1329719f5d7fSMiklos Szeredi 1330719f5d7fSMiklos Szeredi return 0; 1331719f5d7fSMiklos Szeredi } 1332719f5d7fSMiklos Szeredi 1333b90fa9aeSRam Pai /* 1334b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1335b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 133621444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 133721444403SRam Pai * store the parent mount and mountpoint dentry. 133821444403SRam Pai * (done when source_mnt is moved) 1339b90fa9aeSRam Pai * 1340b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1341b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 13429676f0c6SRam Pai * --------------------------------------------------------------------------- 1343b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 13449676f0c6SRam Pai * |************************************************************************** 13459676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13469676f0c6SRam Pai * | dest | | | | | 13479676f0c6SRam Pai * | | | | | | | 13489676f0c6SRam Pai * | v | | | | | 13499676f0c6SRam Pai * |************************************************************************** 13509676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 13515afe0022SRam Pai * | | | | | | 13529676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 13539676f0c6SRam Pai * *************************************************************************** 1354b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1355b90fa9aeSRam Pai * destination mount. 1356b90fa9aeSRam Pai * 1357b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1358b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1359b90fa9aeSRam Pai * the peer group of the source mount. 1360b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1361b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1362b90fa9aeSRam Pai * mount. 13635afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 13645afe0022SRam Pai * of the destination mount and the cloned mount is made slave 13655afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 13665afe0022SRam Pai * is marked as 'shared and slave'. 13675afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 13685afe0022SRam Pai * source mount. 13695afe0022SRam Pai * 13709676f0c6SRam Pai * --------------------------------------------------------------------------- 137121444403SRam Pai * | MOVE MOUNT OPERATION | 13729676f0c6SRam Pai * |************************************************************************** 13739676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13749676f0c6SRam Pai * | dest | | | | | 13759676f0c6SRam Pai * | | | | | | | 13769676f0c6SRam Pai * | v | | | | | 13779676f0c6SRam Pai * |************************************************************************** 13789676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 13795afe0022SRam Pai * | | | | | | 13809676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 13819676f0c6SRam Pai * *************************************************************************** 13825afe0022SRam Pai * 13835afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 13845afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 138521444403SRam Pai * (+*) the mount is moved to the destination. 13865afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 13875afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 13885afe0022SRam Pai * the mount is marked as 'shared and slave'. 13895afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1390b90fa9aeSRam Pai * 1391b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1392b90fa9aeSRam Pai * applied to each mount in the tree. 1393b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1394b90fa9aeSRam Pai * in allocations. 1395b90fa9aeSRam Pai */ 13960fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 13971a390689SAl Viro struct path *path, struct path *parent_path) 1398b90fa9aeSRam Pai { 1399b90fa9aeSRam Pai LIST_HEAD(tree_list); 1400a8d56d8eSAl Viro struct mount *dest_mnt = real_mount(path->mnt); 14011a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1402315fc83eSAl Viro struct mount *child, *p; 1403719f5d7fSMiklos Szeredi int err; 1404b90fa9aeSRam Pai 1405fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 14060fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1407719f5d7fSMiklos Szeredi if (err) 1408719f5d7fSMiklos Szeredi goto out; 1409719f5d7fSMiklos Szeredi } 1410a8d56d8eSAl Viro err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); 1411719f5d7fSMiklos Szeredi if (err) 1412719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1413b90fa9aeSRam Pai 141499b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1415df1a1ad2SAl Viro 1416fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1417909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 14180f0afb1dSAl Viro set_mnt_shared(p); 1419b90fa9aeSRam Pai } 14201a390689SAl Viro if (parent_path) { 14210fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 14220fb54e50SAl Viro attach_mnt(source_mnt, path); 1423143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 142421444403SRam Pai } else { 142514cf1fa8SAl Viro mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 14260fb54e50SAl Viro commit_tree(source_mnt); 142721444403SRam Pai } 1428b90fa9aeSRam Pai 14291b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 14301b8e5564SAl Viro list_del_init(&child->mnt_hash); 14314b2619a5SAl Viro commit_tree(child); 1432b90fa9aeSRam Pai } 143399b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 143499b7db7bSNick Piggin 1435b90fa9aeSRam Pai return 0; 1436719f5d7fSMiklos Szeredi 1437719f5d7fSMiklos Szeredi out_cleanup_ids: 1438fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 14390fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1440719f5d7fSMiklos Szeredi out: 1441719f5d7fSMiklos Szeredi return err; 1442b90fa9aeSRam Pai } 1443b90fa9aeSRam Pai 1444b12cea91SAl Viro static int lock_mount(struct path *path) 1445b12cea91SAl Viro { 1446b12cea91SAl Viro struct vfsmount *mnt; 1447b12cea91SAl Viro retry: 1448b12cea91SAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1449b12cea91SAl Viro if (unlikely(cant_mount(path->dentry))) { 1450b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1451b12cea91SAl Viro return -ENOENT; 1452b12cea91SAl Viro } 1453b12cea91SAl Viro down_write(&namespace_sem); 1454b12cea91SAl Viro mnt = lookup_mnt(path); 1455b12cea91SAl Viro if (likely(!mnt)) 1456b12cea91SAl Viro return 0; 1457b12cea91SAl Viro up_write(&namespace_sem); 1458b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1459b12cea91SAl Viro path_put(path); 1460b12cea91SAl Viro path->mnt = mnt; 1461b12cea91SAl Viro path->dentry = dget(mnt->mnt_root); 1462b12cea91SAl Viro goto retry; 1463b12cea91SAl Viro } 1464b12cea91SAl Viro 1465b12cea91SAl Viro static void unlock_mount(struct path *path) 1466b12cea91SAl Viro { 1467b12cea91SAl Viro up_write(&namespace_sem); 1468b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1469b12cea91SAl Viro } 1470b12cea91SAl Viro 147195bc5f25SAl Viro static int graft_tree(struct mount *mnt, struct path *path) 14721da177e4SLinus Torvalds { 147395bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 14741da177e4SLinus Torvalds return -EINVAL; 14751da177e4SLinus Torvalds 14768c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 147795bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 14781da177e4SLinus Torvalds return -ENOTDIR; 14791da177e4SLinus Torvalds 1480b12cea91SAl Viro if (d_unlinked(path->dentry)) 1481b12cea91SAl Viro return -ENOENT; 14821da177e4SLinus Torvalds 148395bc5f25SAl Viro return attach_recursive_mnt(mnt, path, NULL); 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds /* 14877a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 14887a2e8a8fSValerie Aurora */ 14897a2e8a8fSValerie Aurora 14907a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 14917a2e8a8fSValerie Aurora { 14927c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 14937a2e8a8fSValerie Aurora 14947a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 14957a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 14967a2e8a8fSValerie Aurora return 0; 14977a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 14987a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 14997a2e8a8fSValerie Aurora return 0; 15007a2e8a8fSValerie Aurora return type; 15017a2e8a8fSValerie Aurora } 15027a2e8a8fSValerie Aurora 15037a2e8a8fSValerie Aurora /* 150407b20889SRam Pai * recursively change the type of the mountpoint. 150507b20889SRam Pai */ 15060a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 150707b20889SRam Pai { 1508315fc83eSAl Viro struct mount *m; 15094b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 151007b20889SRam Pai int recurse = flag & MS_REC; 15117a2e8a8fSValerie Aurora int type; 1512719f5d7fSMiklos Szeredi int err = 0; 151307b20889SRam Pai 1514ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 1515ee6f9582SMiklos Szeredi return -EPERM; 1516ee6f9582SMiklos Szeredi 15172d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 151807b20889SRam Pai return -EINVAL; 151907b20889SRam Pai 15207a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 15217a2e8a8fSValerie Aurora if (!type) 15227a2e8a8fSValerie Aurora return -EINVAL; 15237a2e8a8fSValerie Aurora 152407b20889SRam Pai down_write(&namespace_sem); 1525719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1526719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1527719f5d7fSMiklos Szeredi if (err) 1528719f5d7fSMiklos Szeredi goto out_unlock; 1529719f5d7fSMiklos Szeredi } 1530719f5d7fSMiklos Szeredi 153199b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1532909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 15330f0afb1dSAl Viro change_mnt_propagation(m, type); 153499b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1535719f5d7fSMiklos Szeredi 1536719f5d7fSMiklos Szeredi out_unlock: 153707b20889SRam Pai up_write(&namespace_sem); 1538719f5d7fSMiklos Szeredi return err; 153907b20889SRam Pai } 154007b20889SRam Pai 154107b20889SRam Pai /* 15421da177e4SLinus Torvalds * do loopback mount. 15431da177e4SLinus Torvalds */ 15440a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name, 15452dafe1c4SEric Sandeen int recurse) 15461da177e4SLinus Torvalds { 1547b12cea91SAl Viro LIST_HEAD(umount_list); 15482d92ab3cSAl Viro struct path old_path; 154987129cc0SAl Viro struct mount *mnt = NULL, *old; 15502d92ab3cSAl Viro int err = mount_is_safe(path); 15511da177e4SLinus Torvalds if (err) 15521da177e4SLinus Torvalds return err; 15531da177e4SLinus Torvalds if (!old_name || !*old_name) 15541da177e4SLinus Torvalds return -EINVAL; 1555815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 15561da177e4SLinus Torvalds if (err) 15571da177e4SLinus Torvalds return err; 15581da177e4SLinus Torvalds 1559b12cea91SAl Viro err = lock_mount(path); 1560b12cea91SAl Viro if (err) 15619676f0c6SRam Pai goto out; 15629676f0c6SRam Pai 156387129cc0SAl Viro old = real_mount(old_path.mnt); 156487129cc0SAl Viro 1565b12cea91SAl Viro err = -EINVAL; 1566fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1567b12cea91SAl Viro goto out2; 1568b12cea91SAl Viro 1569143c8c91SAl Viro if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old)) 1570b12cea91SAl Viro goto out2; 1571ccd48bc7SAl Viro 15721da177e4SLinus Torvalds err = -ENOMEM; 15731da177e4SLinus Torvalds if (recurse) 157487129cc0SAl Viro mnt = copy_tree(old, old_path.dentry, 0); 15751da177e4SLinus Torvalds else 157687129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 15771da177e4SLinus Torvalds 1578ccd48bc7SAl Viro if (!mnt) 1579b12cea91SAl Viro goto out2; 1580ccd48bc7SAl Viro 158195bc5f25SAl Viro err = graft_tree(mnt, path); 15821da177e4SLinus Torvalds if (err) { 158399b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1584761d5c38SAl Viro umount_tree(mnt, 0, &umount_list); 158599b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 15865b83d2c5SRam Pai } 1587b12cea91SAl Viro out2: 1588b12cea91SAl Viro unlock_mount(path); 1589b12cea91SAl Viro release_mounts(&umount_list); 1590ccd48bc7SAl Viro out: 15912d92ab3cSAl Viro path_put(&old_path); 15921da177e4SLinus Torvalds return err; 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 15952e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 15962e4b7fcdSDave Hansen { 15972e4b7fcdSDave Hansen int error = 0; 15982e4b7fcdSDave Hansen int readonly_request = 0; 15992e4b7fcdSDave Hansen 16002e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 16012e4b7fcdSDave Hansen readonly_request = 1; 16022e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 16032e4b7fcdSDave Hansen return 0; 16042e4b7fcdSDave Hansen 16052e4b7fcdSDave Hansen if (readonly_request) 160683adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 16072e4b7fcdSDave Hansen else 160883adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 16092e4b7fcdSDave Hansen return error; 16102e4b7fcdSDave Hansen } 16112e4b7fcdSDave Hansen 16121da177e4SLinus Torvalds /* 16131da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 16141da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 16151da177e4SLinus Torvalds * on it - tough luck. 16161da177e4SLinus Torvalds */ 16170a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 16181da177e4SLinus Torvalds void *data) 16191da177e4SLinus Torvalds { 16201da177e4SLinus Torvalds int err; 16212d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1622143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16251da177e4SLinus Torvalds return -EPERM; 16261da177e4SLinus Torvalds 1627143c8c91SAl Viro if (!check_mnt(mnt)) 16281da177e4SLinus Torvalds return -EINVAL; 16291da177e4SLinus Torvalds 16302d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 16311da177e4SLinus Torvalds return -EINVAL; 16321da177e4SLinus Torvalds 1633ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1634ff36fe2cSEric Paris if (err) 1635ff36fe2cSEric Paris return err; 1636ff36fe2cSEric Paris 16371da177e4SLinus Torvalds down_write(&sb->s_umount); 16382e4b7fcdSDave Hansen if (flags & MS_BIND) 16392d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 16404aa98cf7SAl Viro else 16411da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 16427b43a79fSAl Viro if (!err) { 164399b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1644143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1645143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 164699b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 16477b43a79fSAl Viro } 16481da177e4SLinus Torvalds up_write(&sb->s_umount); 16490e55a7ccSDan Williams if (!err) { 165099b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1651143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 165299b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 16530e55a7ccSDan Williams } 16541da177e4SLinus Torvalds return err; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 1657cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 16589676f0c6SRam Pai { 1659315fc83eSAl Viro struct mount *p; 1660909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1661fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 16629676f0c6SRam Pai return 1; 16639676f0c6SRam Pai } 16649676f0c6SRam Pai return 0; 16659676f0c6SRam Pai } 16669676f0c6SRam Pai 16670a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name) 16681da177e4SLinus Torvalds { 16692d92ab3cSAl Viro struct path old_path, parent_path; 1670676da58dSAl Viro struct mount *p; 16710fb54e50SAl Viro struct mount *old; 16721da177e4SLinus Torvalds int err = 0; 16731da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16741da177e4SLinus Torvalds return -EPERM; 16751da177e4SLinus Torvalds if (!old_name || !*old_name) 16761da177e4SLinus Torvalds return -EINVAL; 16772d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 16781da177e4SLinus Torvalds if (err) 16791da177e4SLinus Torvalds return err; 16801da177e4SLinus Torvalds 1681b12cea91SAl Viro err = lock_mount(path); 1682cc53ce53SDavid Howells if (err < 0) 1683cc53ce53SDavid Howells goto out; 1684cc53ce53SDavid Howells 1685143c8c91SAl Viro old = real_mount(old_path.mnt); 1686fc7be130SAl Viro p = real_mount(path->mnt); 1687143c8c91SAl Viro 16881da177e4SLinus Torvalds err = -EINVAL; 1689fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 16901da177e4SLinus Torvalds goto out1; 16911da177e4SLinus Torvalds 1692f3da392eSAlexey Dobriyan if (d_unlinked(path->dentry)) 169321444403SRam Pai goto out1; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds err = -EINVAL; 16962d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 169721444403SRam Pai goto out1; 16981da177e4SLinus Torvalds 1699676da58dSAl Viro if (!mnt_has_parent(old)) 170021444403SRam Pai goto out1; 17011da177e4SLinus Torvalds 17022d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 17032d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 170421444403SRam Pai goto out1; 170521444403SRam Pai /* 170621444403SRam Pai * Don't move a mount residing in a shared parent. 170721444403SRam Pai */ 1708fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 170921444403SRam Pai goto out1; 17109676f0c6SRam Pai /* 17119676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 17129676f0c6SRam Pai * mount which is shared. 17139676f0c6SRam Pai */ 1714fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 17159676f0c6SRam Pai goto out1; 17161da177e4SLinus Torvalds err = -ELOOP; 1717fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1718676da58dSAl Viro if (p == old) 171921444403SRam Pai goto out1; 17201da177e4SLinus Torvalds 17210fb54e50SAl Viro err = attach_recursive_mnt(old, path, &parent_path); 17224ac91378SJan Blunck if (err) 172321444403SRam Pai goto out1; 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 17261da177e4SLinus Torvalds * automatically */ 17276776db3dSAl Viro list_del_init(&old->mnt_expire); 17281da177e4SLinus Torvalds out1: 1729b12cea91SAl Viro unlock_mount(path); 17301da177e4SLinus Torvalds out: 17311da177e4SLinus Torvalds if (!err) 17321a390689SAl Viro path_put(&parent_path); 17332d92ab3cSAl Viro path_put(&old_path); 17341da177e4SLinus Torvalds return err; 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 17379d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 17389d412a43SAl Viro { 17399d412a43SAl Viro int err; 17409d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 17419d412a43SAl Viro if (subtype) { 17429d412a43SAl Viro subtype++; 17439d412a43SAl Viro err = -EINVAL; 17449d412a43SAl Viro if (!subtype[0]) 17459d412a43SAl Viro goto err; 17469d412a43SAl Viro } else 17479d412a43SAl Viro subtype = ""; 17489d412a43SAl Viro 17499d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 17509d412a43SAl Viro err = -ENOMEM; 17519d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 17529d412a43SAl Viro goto err; 17539d412a43SAl Viro return mnt; 17549d412a43SAl Viro 17559d412a43SAl Viro err: 17569d412a43SAl Viro mntput(mnt); 17579d412a43SAl Viro return ERR_PTR(err); 17589d412a43SAl Viro } 17599d412a43SAl Viro 176079e801a9SAl Viro static struct vfsmount * 17619d412a43SAl Viro do_kern_mount(const char *fstype, int flags, const char *name, void *data) 17629d412a43SAl Viro { 17639d412a43SAl Viro struct file_system_type *type = get_fs_type(fstype); 17649d412a43SAl Viro struct vfsmount *mnt; 17659d412a43SAl Viro if (!type) 17669d412a43SAl Viro return ERR_PTR(-ENODEV); 17679d412a43SAl Viro mnt = vfs_kern_mount(type, flags, name, data); 17689d412a43SAl Viro if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 17699d412a43SAl Viro !mnt->mnt_sb->s_subtype) 17709d412a43SAl Viro mnt = fs_set_subtype(mnt, fstype); 17719d412a43SAl Viro put_filesystem(type); 17729d412a43SAl Viro return mnt; 17739d412a43SAl Viro } 17749d412a43SAl Viro 17759d412a43SAl Viro /* 17769d412a43SAl Viro * add a mount into a namespace's mount tree 17779d412a43SAl Viro */ 177895bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 17799d412a43SAl Viro { 17809d412a43SAl Viro int err; 17819d412a43SAl Viro 17829d412a43SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 17839d412a43SAl Viro 1784b12cea91SAl Viro err = lock_mount(path); 1785b12cea91SAl Viro if (err) 1786b12cea91SAl Viro return err; 17879d412a43SAl Viro 17889d412a43SAl Viro err = -EINVAL; 1789143c8c91SAl Viro if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt))) 17909d412a43SAl Viro goto unlock; 17919d412a43SAl Viro 17929d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 17939d412a43SAl Viro err = -EBUSY; 179495bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 17959d412a43SAl Viro path->mnt->mnt_root == path->dentry) 17969d412a43SAl Viro goto unlock; 17979d412a43SAl Viro 17989d412a43SAl Viro err = -EINVAL; 179995bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 18009d412a43SAl Viro goto unlock; 18019d412a43SAl Viro 180295bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 18039d412a43SAl Viro err = graft_tree(newmnt, path); 18049d412a43SAl Viro 18059d412a43SAl Viro unlock: 1806b12cea91SAl Viro unlock_mount(path); 18079d412a43SAl Viro return err; 18089d412a43SAl Viro } 1809b1e75df4SAl Viro 18101da177e4SLinus Torvalds /* 18111da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 18121da177e4SLinus Torvalds * namespace's tree 18131da177e4SLinus Torvalds */ 18140a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags, 18151da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 18161da177e4SLinus Torvalds { 18171da177e4SLinus Torvalds struct vfsmount *mnt; 181815f9a3f3SAl Viro int err; 18191da177e4SLinus Torvalds 1820eca6f534SVegard Nossum if (!type) 18211da177e4SLinus Torvalds return -EINVAL; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds /* we need capabilities... */ 18241da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 18251da177e4SLinus Torvalds return -EPERM; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 18281da177e4SLinus Torvalds if (IS_ERR(mnt)) 18291da177e4SLinus Torvalds return PTR_ERR(mnt); 18301da177e4SLinus Torvalds 183195bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 183215f9a3f3SAl Viro if (err) 183315f9a3f3SAl Viro mntput(mnt); 183415f9a3f3SAl Viro return err; 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds 183719a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 183819a167afSAl Viro { 18396776db3dSAl Viro struct mount *mnt = real_mount(m); 184019a167afSAl Viro int err; 184119a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 184219a167afSAl Viro * expired before we get a chance to add it 184319a167afSAl Viro */ 18446776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 184519a167afSAl Viro 184619a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 184719a167afSAl Viro m->mnt_root == path->dentry) { 1848b1e75df4SAl Viro err = -ELOOP; 1849b1e75df4SAl Viro goto fail; 185019a167afSAl Viro } 185119a167afSAl Viro 185295bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 1853b1e75df4SAl Viro if (!err) 1854b1e75df4SAl Viro return 0; 1855b1e75df4SAl Viro fail: 1856b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 18576776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 1858b1e75df4SAl Viro down_write(&namespace_sem); 1859b1e75df4SAl Viro br_write_lock(vfsmount_lock); 18606776db3dSAl Viro list_del_init(&mnt->mnt_expire); 1861b1e75df4SAl Viro br_write_unlock(vfsmount_lock); 1862b1e75df4SAl Viro up_write(&namespace_sem); 186319a167afSAl Viro } 1864b1e75df4SAl Viro mntput(m); 1865b1e75df4SAl Viro mntput(m); 186619a167afSAl Viro return err; 186719a167afSAl Viro } 186819a167afSAl Viro 1869ea5b778aSDavid Howells /** 1870ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 1871ea5b778aSDavid Howells * @mnt: The mount to list. 1872ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 1873ea5b778aSDavid Howells */ 1874ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 1875ea5b778aSDavid Howells { 1876ea5b778aSDavid Howells down_write(&namespace_sem); 1877ea5b778aSDavid Howells br_write_lock(vfsmount_lock); 1878ea5b778aSDavid Howells 18796776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 1880ea5b778aSDavid Howells 1881ea5b778aSDavid Howells br_write_unlock(vfsmount_lock); 1882ea5b778aSDavid Howells up_write(&namespace_sem); 1883ea5b778aSDavid Howells } 1884ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 1885ea5b778aSDavid Howells 1886ea5b778aSDavid Howells /* 18871da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 18881da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 18891da177e4SLinus Torvalds * here 18901da177e4SLinus Torvalds */ 18911da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 18921da177e4SLinus Torvalds { 1893761d5c38SAl Viro struct mount *mnt, *next; 18941da177e4SLinus Torvalds LIST_HEAD(graveyard); 1895bcc5c7d2SAl Viro LIST_HEAD(umounts); 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds if (list_empty(mounts)) 18981da177e4SLinus Torvalds return; 18991da177e4SLinus Torvalds 1900bcc5c7d2SAl Viro down_write(&namespace_sem); 190199b7db7bSNick Piggin br_write_lock(vfsmount_lock); 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 19041da177e4SLinus Torvalds * following criteria: 19051da177e4SLinus Torvalds * - only referenced by its parent vfsmount 19061da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 19071da177e4SLinus Torvalds * cleared by mntput()) 19081da177e4SLinus Torvalds */ 19096776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 1910863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 19111ab59738SAl Viro propagate_mount_busy(mnt, 1)) 19121da177e4SLinus Torvalds continue; 19136776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 19141da177e4SLinus Torvalds } 1915bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 19166776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 1917143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1918bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 1919bcc5c7d2SAl Viro } 192099b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1921bcc5c7d2SAl Viro up_write(&namespace_sem); 1922bcc5c7d2SAl Viro 1923bcc5c7d2SAl Viro release_mounts(&umounts); 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds /* 19295528f911STrond Myklebust * Ripoff of 'select_parent()' 19305528f911STrond Myklebust * 19315528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 19325528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 19335528f911STrond Myklebust */ 1934692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 19355528f911STrond Myklebust { 1936692afc31SAl Viro struct mount *this_parent = parent; 19375528f911STrond Myklebust struct list_head *next; 19385528f911STrond Myklebust int found = 0; 19395528f911STrond Myklebust 19405528f911STrond Myklebust repeat: 19416b41d536SAl Viro next = this_parent->mnt_mounts.next; 19425528f911STrond Myklebust resume: 19436b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 19445528f911STrond Myklebust struct list_head *tmp = next; 19456b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 19465528f911STrond Myklebust 19475528f911STrond Myklebust next = tmp->next; 1948692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 19495528f911STrond Myklebust continue; 19505528f911STrond Myklebust /* 19515528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 19525528f911STrond Myklebust */ 19536b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 19545528f911STrond Myklebust this_parent = mnt; 19555528f911STrond Myklebust goto repeat; 19565528f911STrond Myklebust } 19575528f911STrond Myklebust 19581ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 19596776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 19605528f911STrond Myklebust found++; 19615528f911STrond Myklebust } 19625528f911STrond Myklebust } 19635528f911STrond Myklebust /* 19645528f911STrond Myklebust * All done at this level ... ascend and resume the search 19655528f911STrond Myklebust */ 19665528f911STrond Myklebust if (this_parent != parent) { 19676b41d536SAl Viro next = this_parent->mnt_child.next; 19680714a533SAl Viro this_parent = this_parent->mnt_parent; 19695528f911STrond Myklebust goto resume; 19705528f911STrond Myklebust } 19715528f911STrond Myklebust return found; 19725528f911STrond Myklebust } 19735528f911STrond Myklebust 19745528f911STrond Myklebust /* 19755528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 19765528f911STrond Myklebust * submounts of a specific parent mountpoint 197799b7db7bSNick Piggin * 197899b7db7bSNick Piggin * vfsmount_lock must be held for write 19795528f911STrond Myklebust */ 1980692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts) 19815528f911STrond Myklebust { 19825528f911STrond Myklebust LIST_HEAD(graveyard); 1983761d5c38SAl Viro struct mount *m; 19845528f911STrond Myklebust 19855528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 1986c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 1987bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 1988761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 19896776db3dSAl Viro mnt_expire); 1990143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 1991afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 1992bcc5c7d2SAl Viro } 1993bcc5c7d2SAl Viro } 19945528f911STrond Myklebust } 19955528f911STrond Myklebust 19965528f911STrond Myklebust /* 19971da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 19981da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 19991da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 20001da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 20011da177e4SLinus Torvalds */ 2002b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2003b58fed8bSRam Pai unsigned long n) 20041da177e4SLinus Torvalds { 20051da177e4SLinus Torvalds char *t = to; 20061da177e4SLinus Torvalds const char __user *f = from; 20071da177e4SLinus Torvalds char c; 20081da177e4SLinus Torvalds 20091da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 20101da177e4SLinus Torvalds return n; 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds while (n) { 20131da177e4SLinus Torvalds if (__get_user(c, f)) { 20141da177e4SLinus Torvalds memset(t, 0, n); 20151da177e4SLinus Torvalds break; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds *t++ = c; 20181da177e4SLinus Torvalds f++; 20191da177e4SLinus Torvalds n--; 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds return n; 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 20251da177e4SLinus Torvalds { 20261da177e4SLinus Torvalds int i; 20271da177e4SLinus Torvalds unsigned long page; 20281da177e4SLinus Torvalds unsigned long size; 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds *where = 0; 20311da177e4SLinus Torvalds if (!data) 20321da177e4SLinus Torvalds return 0; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 20351da177e4SLinus Torvalds return -ENOMEM; 20361da177e4SLinus Torvalds 20371da177e4SLinus Torvalds /* We only care that *some* data at the address the user 20381da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 20391da177e4SLinus Torvalds * the remainder of the page. 20401da177e4SLinus Torvalds */ 20411da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 20421da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 20431da177e4SLinus Torvalds if (size > PAGE_SIZE) 20441da177e4SLinus Torvalds size = PAGE_SIZE; 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 20471da177e4SLinus Torvalds if (!i) { 20481da177e4SLinus Torvalds free_page(page); 20491da177e4SLinus Torvalds return -EFAULT; 20501da177e4SLinus Torvalds } 20511da177e4SLinus Torvalds if (i != PAGE_SIZE) 20521da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 20531da177e4SLinus Torvalds *where = page; 20541da177e4SLinus Torvalds return 0; 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds 2057eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2058eca6f534SVegard Nossum { 2059eca6f534SVegard Nossum char *tmp; 2060eca6f534SVegard Nossum 2061eca6f534SVegard Nossum if (!data) { 2062eca6f534SVegard Nossum *where = NULL; 2063eca6f534SVegard Nossum return 0; 2064eca6f534SVegard Nossum } 2065eca6f534SVegard Nossum 2066eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2067eca6f534SVegard Nossum if (IS_ERR(tmp)) 2068eca6f534SVegard Nossum return PTR_ERR(tmp); 2069eca6f534SVegard Nossum 2070eca6f534SVegard Nossum *where = tmp; 2071eca6f534SVegard Nossum return 0; 2072eca6f534SVegard Nossum } 2073eca6f534SVegard Nossum 20741da177e4SLinus Torvalds /* 20751da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 20761da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 20771da177e4SLinus Torvalds * 20781da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 20791da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 20801da177e4SLinus Torvalds * information (or be NULL). 20811da177e4SLinus Torvalds * 20821da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 20831da177e4SLinus Torvalds * When the flags word was introduced its top half was required 20841da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 20851da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 20861da177e4SLinus Torvalds * and must be discarded. 20871da177e4SLinus Torvalds */ 20881da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 20891da177e4SLinus Torvalds unsigned long flags, void *data_page) 20901da177e4SLinus Torvalds { 20912d92ab3cSAl Viro struct path path; 20921da177e4SLinus Torvalds int retval = 0; 20931da177e4SLinus Torvalds int mnt_flags = 0; 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds /* Discard magic */ 20961da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 20971da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds /* Basic sanity checks */ 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 21021da177e4SLinus Torvalds return -EINVAL; 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds if (data_page) 21051da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 21061da177e4SLinus Torvalds 2107a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2108a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2109a27ab9f2STetsuo Handa if (retval) 2110a27ab9f2STetsuo Handa return retval; 2111a27ab9f2STetsuo Handa 2112a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2113a27ab9f2STetsuo Handa type_page, flags, data_page); 2114a27ab9f2STetsuo Handa if (retval) 2115a27ab9f2STetsuo Handa goto dput_out; 2116a27ab9f2STetsuo Handa 2117613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2118613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 21190a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 21200a1c01c9SMatthew Garrett 21211da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 21221da177e4SLinus Torvalds if (flags & MS_NOSUID) 21231da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 21241da177e4SLinus Torvalds if (flags & MS_NODEV) 21251da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 21261da177e4SLinus Torvalds if (flags & MS_NOEXEC) 21271da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2128fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2129fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2130fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2131fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2132d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2133d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 21342e4b7fcdSDave Hansen if (flags & MS_RDONLY) 21352e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2136fc33a7bbSChristoph Hellwig 21377a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2138d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2139d0adde57SMatthew Garrett MS_STRICTATIME); 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds if (flags & MS_REMOUNT) 21422d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 21431da177e4SLinus Torvalds data_page); 21441da177e4SLinus Torvalds else if (flags & MS_BIND) 21452d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 21469676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21472d92ab3cSAl Viro retval = do_change_type(&path, flags); 21481da177e4SLinus Torvalds else if (flags & MS_MOVE) 21492d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 21501da177e4SLinus Torvalds else 21512d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 21521da177e4SLinus Torvalds dev_name, data_page); 21531da177e4SLinus Torvalds dput_out: 21542d92ab3cSAl Viro path_put(&path); 21551da177e4SLinus Torvalds return retval; 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds 2158cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void) 2159cf8d2c11STrond Myklebust { 2160cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2161cf8d2c11STrond Myklebust 2162cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2163cf8d2c11STrond Myklebust if (!new_ns) 2164cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2165cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2166cf8d2c11STrond Myklebust new_ns->root = NULL; 2167cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2168cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2169cf8d2c11STrond Myklebust new_ns->event = 0; 2170cf8d2c11STrond Myklebust return new_ns; 2171cf8d2c11STrond Myklebust } 2172cf8d2c11STrond Myklebust 2173f03c6599SAl Viro void mnt_make_longterm(struct vfsmount *mnt) 2174f03c6599SAl Viro { 217583adc753SAl Viro __mnt_make_longterm(real_mount(mnt)); 2176f03c6599SAl Viro } 2177f03c6599SAl Viro 217883adc753SAl Viro void mnt_make_shortterm(struct vfsmount *m) 2179f03c6599SAl Viro { 21807e3d0eb0SAl Viro #ifdef CONFIG_SMP 218183adc753SAl Viro struct mount *mnt = real_mount(m); 218268e8a9feSAl Viro if (atomic_add_unless(&mnt->mnt_longterm, -1, 1)) 2183f03c6599SAl Viro return; 2184f03c6599SAl Viro br_write_lock(vfsmount_lock); 218568e8a9feSAl Viro atomic_dec(&mnt->mnt_longterm); 2186f03c6599SAl Viro br_write_unlock(vfsmount_lock); 21877e3d0eb0SAl Viro #endif 2188f03c6599SAl Viro } 2189f03c6599SAl Viro 2190741a2951SJANAK DESAI /* 2191741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2192741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2193741a2951SJANAK DESAI */ 2194e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 21956b3286edSKirill Korotaev struct fs_struct *fs) 21961da177e4SLinus Torvalds { 21976b3286edSKirill Korotaev struct mnt_namespace *new_ns; 21987f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2199315fc83eSAl Viro struct mount *p, *q; 2200be08d6d2SAl Viro struct mount *old = mnt_ns->root; 2201cb338d06SAl Viro struct mount *new; 22021da177e4SLinus Torvalds 2203cf8d2c11STrond Myklebust new_ns = alloc_mnt_ns(); 2204cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2205cf8d2c11STrond Myklebust return new_ns; 22061da177e4SLinus Torvalds 2207390c6843SRam Pai down_write(&namespace_sem); 22081da177e4SLinus Torvalds /* First pass: copy the tree topology */ 2209909b0a88SAl Viro new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE); 2210cb338d06SAl Viro if (!new) { 2211390c6843SRam Pai up_write(&namespace_sem); 22121da177e4SLinus Torvalds kfree(new_ns); 22135cc4a034SJulia Lawall return ERR_PTR(-ENOMEM); 22141da177e4SLinus Torvalds } 2215be08d6d2SAl Viro new_ns->root = new; 221699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 22171a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 221899b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds /* 22211da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 22221da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 22231da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 22241da177e4SLinus Torvalds */ 2225909b0a88SAl Viro p = old; 2226cb338d06SAl Viro q = new; 22271da177e4SLinus Torvalds while (p) { 2228143c8c91SAl Viro q->mnt_ns = new_ns; 222983adc753SAl Viro __mnt_make_longterm(q); 22301da177e4SLinus Torvalds if (fs) { 2231315fc83eSAl Viro if (&p->mnt == fs->root.mnt) { 2232315fc83eSAl Viro fs->root.mnt = mntget(&q->mnt); 223383adc753SAl Viro __mnt_make_longterm(q); 2234315fc83eSAl Viro mnt_make_shortterm(&p->mnt); 2235315fc83eSAl Viro rootmnt = &p->mnt; 22361da177e4SLinus Torvalds } 2237315fc83eSAl Viro if (&p->mnt == fs->pwd.mnt) { 2238315fc83eSAl Viro fs->pwd.mnt = mntget(&q->mnt); 223983adc753SAl Viro __mnt_make_longterm(q); 2240315fc83eSAl Viro mnt_make_shortterm(&p->mnt); 2241315fc83eSAl Viro pwdmnt = &p->mnt; 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds } 2244909b0a88SAl Viro p = next_mnt(p, old); 2245909b0a88SAl Viro q = next_mnt(q, new); 22461da177e4SLinus Torvalds } 2247390c6843SRam Pai up_write(&namespace_sem); 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds if (rootmnt) 2250f03c6599SAl Viro mntput(rootmnt); 22511da177e4SLinus Torvalds if (pwdmnt) 2252f03c6599SAl Viro mntput(pwdmnt); 22531da177e4SLinus Torvalds 2254741a2951SJANAK DESAI return new_ns; 2255741a2951SJANAK DESAI } 2256741a2951SJANAK DESAI 2257213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2258e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 2259741a2951SJANAK DESAI { 22606b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2261741a2951SJANAK DESAI 2262e3222c4eSBadari Pulavarty BUG_ON(!ns); 22636b3286edSKirill Korotaev get_mnt_ns(ns); 2264741a2951SJANAK DESAI 2265741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2266e3222c4eSBadari Pulavarty return ns; 2267741a2951SJANAK DESAI 2268e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 2269741a2951SJANAK DESAI 22706b3286edSKirill Korotaev put_mnt_ns(ns); 2271e3222c4eSBadari Pulavarty return new_ns; 22721da177e4SLinus Torvalds } 22731da177e4SLinus Torvalds 2274cf8d2c11STrond Myklebust /** 2275cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2276cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2277cf8d2c11STrond Myklebust */ 22781a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2279cf8d2c11STrond Myklebust { 22801a4eeaf2SAl Viro struct mnt_namespace *new_ns = alloc_mnt_ns(); 2281cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 22821a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 22831a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 22841a4eeaf2SAl Viro __mnt_make_longterm(mnt); 2285be08d6d2SAl Viro new_ns->root = mnt; 22861a4eeaf2SAl Viro list_add(&new_ns->list, &mnt->mnt_list); 2287c1334495SAl Viro } else { 22881a4eeaf2SAl Viro mntput(m); 2289cf8d2c11STrond Myklebust } 2290cf8d2c11STrond Myklebust return new_ns; 2291cf8d2c11STrond Myklebust } 2292cf8d2c11STrond Myklebust 2293ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2294ea441d11SAl Viro { 2295ea441d11SAl Viro struct mnt_namespace *ns; 2296d31da0f0SAl Viro struct super_block *s; 2297ea441d11SAl Viro struct path path; 2298ea441d11SAl Viro int err; 2299ea441d11SAl Viro 2300ea441d11SAl Viro ns = create_mnt_ns(mnt); 2301ea441d11SAl Viro if (IS_ERR(ns)) 2302ea441d11SAl Viro return ERR_CAST(ns); 2303ea441d11SAl Viro 2304ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2305ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2306ea441d11SAl Viro 2307ea441d11SAl Viro put_mnt_ns(ns); 2308ea441d11SAl Viro 2309ea441d11SAl Viro if (err) 2310ea441d11SAl Viro return ERR_PTR(err); 2311ea441d11SAl Viro 2312ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2313d31da0f0SAl Viro s = path.mnt->mnt_sb; 2314d31da0f0SAl Viro atomic_inc(&s->s_active); 2315ea441d11SAl Viro mntput(path.mnt); 2316ea441d11SAl Viro /* lock the sucker */ 2317d31da0f0SAl Viro down_write(&s->s_umount); 2318ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2319ea441d11SAl Viro return path.dentry; 2320ea441d11SAl Viro } 2321ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2322ea441d11SAl Viro 2323bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2324bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 23251da177e4SLinus Torvalds { 2326eca6f534SVegard Nossum int ret; 2327eca6f534SVegard Nossum char *kernel_type; 2328eca6f534SVegard Nossum char *kernel_dir; 2329eca6f534SVegard Nossum char *kernel_dev; 23301da177e4SLinus Torvalds unsigned long data_page; 23311da177e4SLinus Torvalds 2332eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2333eca6f534SVegard Nossum if (ret < 0) 2334eca6f534SVegard Nossum goto out_type; 23351da177e4SLinus Torvalds 2336eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2337eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2338eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2339eca6f534SVegard Nossum goto out_dir; 2340eca6f534SVegard Nossum } 23411da177e4SLinus Torvalds 2342eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2343eca6f534SVegard Nossum if (ret < 0) 2344eca6f534SVegard Nossum goto out_dev; 23451da177e4SLinus Torvalds 2346eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2347eca6f534SVegard Nossum if (ret < 0) 2348eca6f534SVegard Nossum goto out_data; 23491da177e4SLinus Torvalds 2350eca6f534SVegard Nossum ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags, 2351eca6f534SVegard Nossum (void *) data_page); 2352eca6f534SVegard Nossum 23531da177e4SLinus Torvalds free_page(data_page); 2354eca6f534SVegard Nossum out_data: 2355eca6f534SVegard Nossum kfree(kernel_dev); 2356eca6f534SVegard Nossum out_dev: 2357eca6f534SVegard Nossum putname(kernel_dir); 2358eca6f534SVegard Nossum out_dir: 2359eca6f534SVegard Nossum kfree(kernel_type); 2360eca6f534SVegard Nossum out_type: 2361eca6f534SVegard Nossum return ret; 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds /* 2365afac7cbaSAl Viro * Return true if path is reachable from root 2366afac7cbaSAl Viro * 2367afac7cbaSAl Viro * namespace_sem or vfsmount_lock is held 2368afac7cbaSAl Viro */ 2369643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2370afac7cbaSAl Viro const struct path *root) 2371afac7cbaSAl Viro { 2372643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2373a73324daSAl Viro dentry = mnt->mnt_mountpoint; 23740714a533SAl Viro mnt = mnt->mnt_parent; 2375afac7cbaSAl Viro } 2376643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2377afac7cbaSAl Viro } 2378afac7cbaSAl Viro 2379afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2380afac7cbaSAl Viro { 2381afac7cbaSAl Viro int res; 2382afac7cbaSAl Viro br_read_lock(vfsmount_lock); 2383643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 2384afac7cbaSAl Viro br_read_unlock(vfsmount_lock); 2385afac7cbaSAl Viro return res; 2386afac7cbaSAl Viro } 2387afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2388afac7cbaSAl Viro 2389afac7cbaSAl Viro /* 23901da177e4SLinus Torvalds * pivot_root Semantics: 23911da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 23921da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 23931da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 23941da177e4SLinus Torvalds * 23951da177e4SLinus Torvalds * Restrictions: 23961da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 23971da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 23981da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 23991da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 24001da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 24011da177e4SLinus Torvalds * 24024a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 24034a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 24044a0d11faSNeil Brown * in this situation. 24054a0d11faSNeil Brown * 24061da177e4SLinus Torvalds * Notes: 24071da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 24081da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 24091da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 24101da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 24111da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 24121da177e4SLinus Torvalds * first. 24131da177e4SLinus Torvalds */ 24143480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 24153480b257SHeiko Carstens const char __user *, put_old) 24161da177e4SLinus Torvalds { 24172d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 2418419148daSAl Viro struct mount *new_mnt, *root_mnt; 24191da177e4SLinus Torvalds int error; 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 24221da177e4SLinus Torvalds return -EPERM; 24231da177e4SLinus Torvalds 24242d8f3038SAl Viro error = user_path_dir(new_root, &new); 24251da177e4SLinus Torvalds if (error) 24261da177e4SLinus Torvalds goto out0; 24271da177e4SLinus Torvalds 24282d8f3038SAl Viro error = user_path_dir(put_old, &old); 24291da177e4SLinus Torvalds if (error) 24301da177e4SLinus Torvalds goto out1; 24311da177e4SLinus Torvalds 24322d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2433b12cea91SAl Viro if (error) 2434b12cea91SAl Viro goto out2; 24351da177e4SLinus Torvalds 2436f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 2437b12cea91SAl Viro error = lock_mount(&old); 2438b12cea91SAl Viro if (error) 2439b12cea91SAl Viro goto out3; 2440b12cea91SAl Viro 24411da177e4SLinus Torvalds error = -EINVAL; 2442419148daSAl Viro new_mnt = real_mount(new.mnt); 2443419148daSAl Viro root_mnt = real_mount(root.mnt); 2444fc7be130SAl Viro if (IS_MNT_SHARED(real_mount(old.mnt)) || 2445fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2446fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2447b12cea91SAl Viro goto out4; 2448143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2449b12cea91SAl Viro goto out4; 24501da177e4SLinus Torvalds error = -ENOENT; 2451f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2452b12cea91SAl Viro goto out4; 2453f3da392eSAlexey Dobriyan if (d_unlinked(old.dentry)) 2454b12cea91SAl Viro goto out4; 24551da177e4SLinus Torvalds error = -EBUSY; 24562d8f3038SAl Viro if (new.mnt == root.mnt || 24572d8f3038SAl Viro old.mnt == root.mnt) 2458b12cea91SAl Viro goto out4; /* loop, on the same file system */ 24591da177e4SLinus Torvalds error = -EINVAL; 24608c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2461b12cea91SAl Viro goto out4; /* not a mountpoint */ 2462676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2463b12cea91SAl Viro goto out4; /* not attached */ 24642d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2465b12cea91SAl Viro goto out4; /* not a mountpoint */ 2466676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2467b12cea91SAl Viro goto out4; /* not attached */ 24684ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 2469643822b4SAl Viro if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new)) 2470b12cea91SAl Viro goto out4; 247127cb1572SAl Viro br_write_lock(vfsmount_lock); 2472419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2473419148daSAl Viro detach_mnt(root_mnt, &root_parent); 24744ac91378SJan Blunck /* mount old root on put_old */ 2475419148daSAl Viro attach_mnt(root_mnt, &old); 24764ac91378SJan Blunck /* mount new_root on / */ 2477419148daSAl Viro attach_mnt(new_mnt, &root_parent); 24786b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 247999b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 24802d8f3038SAl Viro chroot_fs_refs(&root, &new); 24811da177e4SLinus Torvalds error = 0; 2482b12cea91SAl Viro out4: 2483b12cea91SAl Viro unlock_mount(&old); 2484b12cea91SAl Viro if (!error) { 24851a390689SAl Viro path_put(&root_parent); 24861a390689SAl Viro path_put(&parent_path); 2487b12cea91SAl Viro } 2488b12cea91SAl Viro out3: 24898c3ee42eSAl Viro path_put(&root); 2490b12cea91SAl Viro out2: 24912d8f3038SAl Viro path_put(&old); 24921da177e4SLinus Torvalds out1: 24932d8f3038SAl Viro path_put(&new); 24941da177e4SLinus Torvalds out0: 24951da177e4SLinus Torvalds return error; 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds static void __init init_mount_tree(void) 24991da177e4SLinus Torvalds { 25001da177e4SLinus Torvalds struct vfsmount *mnt; 25016b3286edSKirill Korotaev struct mnt_namespace *ns; 2502ac748a09SJan Blunck struct path root; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 25051da177e4SLinus Torvalds if (IS_ERR(mnt)) 25061da177e4SLinus Torvalds panic("Can't create rootfs"); 2507b3e19d92SNick Piggin 25083b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 25093b22edc5STrond Myklebust if (IS_ERR(ns)) 25101da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 25111da177e4SLinus Torvalds 25126b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 25136b3286edSKirill Korotaev get_mnt_ns(ns); 25141da177e4SLinus Torvalds 2515be08d6d2SAl Viro root.mnt = mnt; 2516be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2517ac748a09SJan Blunck 2518ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2519ac748a09SJan Blunck set_fs_root(current->fs, &root); 25201da177e4SLinus Torvalds } 25211da177e4SLinus Torvalds 252274bf17cfSDenis Cheng void __init mnt_init(void) 25231da177e4SLinus Torvalds { 252413f14b4dSEric Dumazet unsigned u; 252515a67dd8SRandy Dunlap int err; 25261da177e4SLinus Torvalds 2527390c6843SRam Pai init_rwsem(&namespace_sem); 2528390c6843SRam Pai 25297d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 253020c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 25311da177e4SLinus Torvalds 2532b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds if (!mount_hashtable) 25351da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 25361da177e4SLinus Torvalds 253780cdc6daSMandeep Singh Baines printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE); 25381da177e4SLinus Torvalds 253913f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 254013f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 25411da177e4SLinus Torvalds 254299b7db7bSNick Piggin br_lock_init(vfsmount_lock); 254399b7db7bSNick Piggin 254415a67dd8SRandy Dunlap err = sysfs_init(); 254515a67dd8SRandy Dunlap if (err) 254615a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 25478e24eea7SHarvey Harrison __func__, err); 254800d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 254900d26666SGreg Kroah-Hartman if (!fs_kobj) 25508e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 25511da177e4SLinus Torvalds init_rootfs(); 25521da177e4SLinus Torvalds init_mount_tree(); 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 2555616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 25561da177e4SLinus Torvalds { 255770fbcdf4SRam Pai LIST_HEAD(umount_list); 2558616511d0STrond Myklebust 2559d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2560616511d0STrond Myklebust return; 2561390c6843SRam Pai down_write(&namespace_sem); 256299b7db7bSNick Piggin br_write_lock(vfsmount_lock); 2563be08d6d2SAl Viro umount_tree(ns->root, 0, &umount_list); 256499b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 2565390c6843SRam Pai up_write(&namespace_sem); 256670fbcdf4SRam Pai release_mounts(&umount_list); 25676b3286edSKirill Korotaev kfree(ns); 25681da177e4SLinus Torvalds } 25699d412a43SAl Viro 25709d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 25719d412a43SAl Viro { 2572423e0ab0STim Chen struct vfsmount *mnt; 2573423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2574423e0ab0STim Chen if (!IS_ERR(mnt)) { 2575423e0ab0STim Chen /* 2576423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2577423e0ab0STim Chen * we unmount before file sys is unregistered 2578423e0ab0STim Chen */ 2579423e0ab0STim Chen mnt_make_longterm(mnt); 2580423e0ab0STim Chen } 2581423e0ab0STim Chen return mnt; 25829d412a43SAl Viro } 25839d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2584423e0ab0STim Chen 2585423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2586423e0ab0STim Chen { 2587423e0ab0STim Chen /* release long term mount so mount point can be released */ 2588423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2589423e0ab0STim Chen mnt_make_shortterm(mnt); 2590423e0ab0STim Chen mntput(mnt); 2591423e0ab0STim Chen } 2592423e0ab0STim Chen } 2593423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 259402125a82SAl Viro 259502125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 259602125a82SAl Viro { 2597143c8c91SAl Viro return check_mnt(real_mount(mnt)); 259802125a82SAl Viro } 2599