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> 12d10577a8SAl Viro #include <linux/export.h> 1316f7e0feSRandy Dunlap #include <linux/capability.h> 146b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 151da177e4SLinus Torvalds #include <linux/namei.h> 161da177e4SLinus Torvalds #include <linux/security.h> 1773cd49ecSMiklos Szeredi #include <linux/idr.h> 18d10577a8SAl Viro #include <linux/acct.h> /* acct_auto_close_mnt */ 19d10577a8SAl Viro #include <linux/ramfs.h> /* init_rootfs */ 20d10577a8SAl Viro #include <linux/fs_struct.h> /* get_fs_root et.al. */ 21d10577a8SAl Viro #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */ 22d10577a8SAl Viro #include <linux/uaccess.h> 2307b20889SRam Pai #include "pnode.h" 24948730b0SAdrian Bunk #include "internal.h" 251da177e4SLinus Torvalds 2613f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head)) 2713f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT) 2813f14b4dSEric Dumazet 295addc5ddSAl Viro static int event; 3073cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 31719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 3299b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock); 33f21f6220SAl Viro static int mnt_id_start = 0; 34f21f6220SAl Viro static int mnt_group_start = 1; 355addc5ddSAl Viro 36fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly; 37e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 38390c6843SRam Pai static struct rw_semaphore namespace_sem; 391da177e4SLinus Torvalds 40f87fd4c2SMiklos Szeredi /* /sys/fs */ 4100d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 4200d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 43f87fd4c2SMiklos Szeredi 4499b7db7bSNick Piggin /* 4599b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 4699b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 4799b7db7bSNick Piggin * up the tree. 4899b7db7bSNick Piggin * 4999b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 5099b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 5199b7db7bSNick Piggin */ 5299b7db7bSNick Piggin DEFINE_BRLOCK(vfsmount_lock); 5399b7db7bSNick Piggin 541da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) 551da177e4SLinus Torvalds { 561da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 571da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 5813f14b4dSEric Dumazet tmp = tmp + (tmp >> HASH_SHIFT); 5913f14b4dSEric Dumazet return tmp & (HASH_SIZE - 1); 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 623d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16) 633d733633SDave Hansen 6499b7db7bSNick Piggin /* 6599b7db7bSNick Piggin * allocation is serialized by namespace_sem, but we need the spinlock to 6699b7db7bSNick Piggin * serialize with freeing. 6799b7db7bSNick Piggin */ 68b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 6973cd49ecSMiklos Szeredi { 7073cd49ecSMiklos Szeredi int res; 7173cd49ecSMiklos Szeredi 7273cd49ecSMiklos Szeredi retry: 7373cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 7499b7db7bSNick Piggin spin_lock(&mnt_id_lock); 7515169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 76f21f6220SAl Viro if (!res) 7715169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 7899b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 7973cd49ecSMiklos Szeredi if (res == -EAGAIN) 8073cd49ecSMiklos Szeredi goto retry; 8173cd49ecSMiklos Szeredi 8273cd49ecSMiklos Szeredi return res; 8373cd49ecSMiklos Szeredi } 8473cd49ecSMiklos Szeredi 85b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 8673cd49ecSMiklos Szeredi { 8715169fe7SAl Viro int id = mnt->mnt_id; 8899b7db7bSNick Piggin spin_lock(&mnt_id_lock); 89f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 90f21f6220SAl Viro if (mnt_id_start > id) 91f21f6220SAl Viro mnt_id_start = id; 9299b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 9373cd49ecSMiklos Szeredi } 9473cd49ecSMiklos Szeredi 95719f5d7fSMiklos Szeredi /* 96719f5d7fSMiklos Szeredi * Allocate a new peer group ID 97719f5d7fSMiklos Szeredi * 98719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 99719f5d7fSMiklos Szeredi */ 1004b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 101719f5d7fSMiklos Szeredi { 102f21f6220SAl Viro int res; 103f21f6220SAl Viro 104719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 105719f5d7fSMiklos Szeredi return -ENOMEM; 106719f5d7fSMiklos Szeredi 107f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 108f21f6220SAl Viro mnt_group_start, 10915169fe7SAl Viro &mnt->mnt_group_id); 110f21f6220SAl Viro if (!res) 11115169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 112f21f6220SAl Viro 113f21f6220SAl Viro return res; 114719f5d7fSMiklos Szeredi } 115719f5d7fSMiklos Szeredi 116719f5d7fSMiklos Szeredi /* 117719f5d7fSMiklos Szeredi * Release a peer group ID 118719f5d7fSMiklos Szeredi */ 1194b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 120719f5d7fSMiklos Szeredi { 12115169fe7SAl Viro int id = mnt->mnt_group_id; 122f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 123f21f6220SAl Viro if (mnt_group_start > id) 124f21f6220SAl Viro mnt_group_start = id; 12515169fe7SAl Viro mnt->mnt_group_id = 0; 126719f5d7fSMiklos Szeredi } 127719f5d7fSMiklos Szeredi 128b3e19d92SNick Piggin /* 129b3e19d92SNick Piggin * vfsmount lock must be held for read 130b3e19d92SNick Piggin */ 13183adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 132b3e19d92SNick Piggin { 133b3e19d92SNick Piggin #ifdef CONFIG_SMP 13468e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 135b3e19d92SNick Piggin #else 136b3e19d92SNick Piggin preempt_disable(); 13768e8a9feSAl Viro mnt->mnt_count += n; 138b3e19d92SNick Piggin preempt_enable(); 139b3e19d92SNick Piggin #endif 140b3e19d92SNick Piggin } 141b3e19d92SNick Piggin 142b3e19d92SNick Piggin /* 143b3e19d92SNick Piggin * vfsmount lock must be held for write 144b3e19d92SNick Piggin */ 14583adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 146b3e19d92SNick Piggin { 147b3e19d92SNick Piggin #ifdef CONFIG_SMP 148f03c6599SAl Viro unsigned int count = 0; 149b3e19d92SNick Piggin int cpu; 150b3e19d92SNick Piggin 151b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 15268e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 153b3e19d92SNick Piggin } 154b3e19d92SNick Piggin 155b3e19d92SNick Piggin return count; 156b3e19d92SNick Piggin #else 15768e8a9feSAl Viro return mnt->mnt_count; 158b3e19d92SNick Piggin #endif 159b3e19d92SNick Piggin } 160b3e19d92SNick Piggin 161b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1621da177e4SLinus Torvalds { 163c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 164c63181e6SAl Viro if (mnt) { 16573cd49ecSMiklos Szeredi int err; 16673cd49ecSMiklos Szeredi 167c63181e6SAl Viro err = mnt_alloc_id(mnt); 16888b38782SLi Zefan if (err) 16988b38782SLi Zefan goto out_free_cache; 17088b38782SLi Zefan 17188b38782SLi Zefan if (name) { 172c63181e6SAl Viro mnt->mnt_devname = kstrdup(name, GFP_KERNEL); 173c63181e6SAl Viro if (!mnt->mnt_devname) 17488b38782SLi Zefan goto out_free_id; 17573cd49ecSMiklos Szeredi } 17673cd49ecSMiklos Szeredi 177b3e19d92SNick Piggin #ifdef CONFIG_SMP 178c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 179c63181e6SAl Viro if (!mnt->mnt_pcp) 180b3e19d92SNick Piggin goto out_free_devname; 181b3e19d92SNick Piggin 182c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 183b3e19d92SNick Piggin #else 184c63181e6SAl Viro mnt->mnt_count = 1; 185c63181e6SAl Viro mnt->mnt_writers = 0; 186b3e19d92SNick Piggin #endif 187b3e19d92SNick Piggin 188c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_hash); 189c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 190c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 191c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 192c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 193c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 194c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 195c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 1962504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 1972504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 1982504c5d6SAndreas Gruenbacher #endif 1991da177e4SLinus Torvalds } 200c63181e6SAl Viro return mnt; 20188b38782SLi Zefan 202d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 203d3ef3d73Snpiggin@suse.de out_free_devname: 204c63181e6SAl Viro kfree(mnt->mnt_devname); 205d3ef3d73Snpiggin@suse.de #endif 20688b38782SLi Zefan out_free_id: 207c63181e6SAl Viro mnt_free_id(mnt); 20888b38782SLi Zefan out_free_cache: 209c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 21088b38782SLi Zefan return NULL; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2138366025eSDave Hansen /* 2148366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2158366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2168366025eSDave Hansen * We must keep track of when those operations start 2178366025eSDave Hansen * (for permission checks) and when they end, so that 2188366025eSDave Hansen * we can determine when writes are able to occur to 2198366025eSDave Hansen * a filesystem. 2208366025eSDave Hansen */ 2213d733633SDave Hansen /* 2223d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2233d733633SDave Hansen * @mnt: the mount to check for its write status 2243d733633SDave Hansen * 2253d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2263d733633SDave Hansen * It does not guarantee that the filesystem will stay 2273d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2283d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2293d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2303d733633SDave Hansen * r/w. 2313d733633SDave Hansen */ 2323d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2333d733633SDave Hansen { 2342e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2352e4b7fcdSDave Hansen return 1; 2362e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2372e4b7fcdSDave Hansen return 1; 2382e4b7fcdSDave Hansen return 0; 2393d733633SDave Hansen } 2403d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2413d733633SDave Hansen 24283adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2433d733633SDave Hansen { 244d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 24568e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 246d3ef3d73Snpiggin@suse.de #else 24768e8a9feSAl Viro mnt->mnt_writers++; 248d3ef3d73Snpiggin@suse.de #endif 2493d733633SDave Hansen } 2503d733633SDave Hansen 25183adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2523d733633SDave Hansen { 253d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 25468e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 255d3ef3d73Snpiggin@suse.de #else 25668e8a9feSAl Viro mnt->mnt_writers--; 257d3ef3d73Snpiggin@suse.de #endif 258d3ef3d73Snpiggin@suse.de } 259d3ef3d73Snpiggin@suse.de 26083adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 261d3ef3d73Snpiggin@suse.de { 262d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 263d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2643d733633SDave Hansen int cpu; 2653d733633SDave Hansen 2663d733633SDave Hansen for_each_possible_cpu(cpu) { 26768e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2683d733633SDave Hansen } 2693d733633SDave Hansen 270d3ef3d73Snpiggin@suse.de return count; 271d3ef3d73Snpiggin@suse.de #else 272d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 273d3ef3d73Snpiggin@suse.de #endif 2743d733633SDave Hansen } 2753d733633SDave Hansen 2763d733633SDave Hansen /* 2773d733633SDave Hansen * Most r/o checks on a fs are for operations that take 2783d733633SDave Hansen * discrete amounts of time, like a write() or unlink(). 2793d733633SDave Hansen * We must keep track of when those operations start 2803d733633SDave Hansen * (for permission checks) and when they end, so that 2813d733633SDave Hansen * we can determine when writes are able to occur to 2823d733633SDave Hansen * a filesystem. 2833d733633SDave Hansen */ 2848366025eSDave Hansen /** 2858366025eSDave Hansen * mnt_want_write - get write access to a mount 28683adc753SAl Viro * @m: the mount on which to take a write 2878366025eSDave Hansen * 2888366025eSDave Hansen * This tells the low-level filesystem that a write is 2898366025eSDave Hansen * about to be performed to it, and makes sure that 2908366025eSDave Hansen * writes are allowed before returning success. When 2918366025eSDave Hansen * the write operation is finished, mnt_drop_write() 2928366025eSDave Hansen * must be called. This is effectively a refcount. 2938366025eSDave Hansen */ 29483adc753SAl Viro int mnt_want_write(struct vfsmount *m) 2958366025eSDave Hansen { 29683adc753SAl Viro struct mount *mnt = real_mount(m); 2973d733633SDave Hansen int ret = 0; 2983d733633SDave Hansen 299d3ef3d73Snpiggin@suse.de preempt_disable(); 300c6653a83SNick Piggin mnt_inc_writers(mnt); 301d3ef3d73Snpiggin@suse.de /* 302c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 303d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 304d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 305d3ef3d73Snpiggin@suse.de */ 306d3ef3d73Snpiggin@suse.de smp_mb(); 30783adc753SAl Viro while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 308d3ef3d73Snpiggin@suse.de cpu_relax(); 309d3ef3d73Snpiggin@suse.de /* 310d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 311d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 312d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 313d3ef3d73Snpiggin@suse.de */ 314d3ef3d73Snpiggin@suse.de smp_rmb(); 31583adc753SAl Viro if (__mnt_is_readonly(m)) { 316c6653a83SNick Piggin mnt_dec_writers(mnt); 3173d733633SDave Hansen ret = -EROFS; 3183d733633SDave Hansen goto out; 3193d733633SDave Hansen } 3203d733633SDave Hansen out: 321d3ef3d73Snpiggin@suse.de preempt_enable(); 3223d733633SDave Hansen return ret; 3238366025eSDave Hansen } 3248366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3258366025eSDave Hansen 3268366025eSDave Hansen /** 32796029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 32896029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 32996029c4eSnpiggin@suse.de * 33096029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 33196029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 33296029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 33396029c4eSnpiggin@suse.de * on it. This allows some optimisation. 33496029c4eSnpiggin@suse.de * 33596029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 33696029c4eSnpiggin@suse.de * drop the reference. 33796029c4eSnpiggin@suse.de */ 33896029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 33996029c4eSnpiggin@suse.de { 34096029c4eSnpiggin@suse.de /* superblock may be r/o */ 34196029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 34296029c4eSnpiggin@suse.de return -EROFS; 34396029c4eSnpiggin@suse.de preempt_disable(); 34483adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 34596029c4eSnpiggin@suse.de preempt_enable(); 34696029c4eSnpiggin@suse.de return 0; 34796029c4eSnpiggin@suse.de } 34896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 34996029c4eSnpiggin@suse.de 35096029c4eSnpiggin@suse.de /** 35196029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 35296029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 35396029c4eSnpiggin@suse.de * 35496029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 35596029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 35696029c4eSnpiggin@suse.de */ 35796029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 35896029c4eSnpiggin@suse.de { 3592d8dd38aSOGAWA Hirofumi struct inode *inode = file->f_dentry->d_inode; 3602d8dd38aSOGAWA Hirofumi if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 36196029c4eSnpiggin@suse.de return mnt_want_write(file->f_path.mnt); 36296029c4eSnpiggin@suse.de else 36396029c4eSnpiggin@suse.de return mnt_clone_write(file->f_path.mnt); 36496029c4eSnpiggin@suse.de } 36596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 36696029c4eSnpiggin@suse.de 36796029c4eSnpiggin@suse.de /** 3688366025eSDave Hansen * mnt_drop_write - give up write access to a mount 3698366025eSDave Hansen * @mnt: the mount on which to give up write access 3708366025eSDave Hansen * 3718366025eSDave Hansen * Tells the low-level filesystem that we are done 3728366025eSDave Hansen * performing writes to it. Must be matched with 3738366025eSDave Hansen * mnt_want_write() call above. 3748366025eSDave Hansen */ 3758366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt) 3768366025eSDave Hansen { 377d3ef3d73Snpiggin@suse.de preempt_disable(); 37883adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 379d3ef3d73Snpiggin@suse.de preempt_enable(); 3808366025eSDave Hansen } 3818366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 3828366025eSDave Hansen 3832a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 3842a79f17eSAl Viro { 3852a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 3862a79f17eSAl Viro } 3872a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 3882a79f17eSAl Viro 38983adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 3908366025eSDave Hansen { 3913d733633SDave Hansen int ret = 0; 3923d733633SDave Hansen 39399b7db7bSNick Piggin br_write_lock(vfsmount_lock); 39483adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 395d3ef3d73Snpiggin@suse.de /* 396d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 397d3ef3d73Snpiggin@suse.de * should be visible before we do. 398d3ef3d73Snpiggin@suse.de */ 399d3ef3d73Snpiggin@suse.de smp_mb(); 400d3ef3d73Snpiggin@suse.de 401d3ef3d73Snpiggin@suse.de /* 402d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 403d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 404d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 405d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 406d3ef3d73Snpiggin@suse.de * 407d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 408d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 409d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 410d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 411d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 412d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 413d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 414d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 415d3ef3d73Snpiggin@suse.de * we're counting up here. 416d3ef3d73Snpiggin@suse.de */ 417c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 418d3ef3d73Snpiggin@suse.de ret = -EBUSY; 419d3ef3d73Snpiggin@suse.de else 42083adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 421d3ef3d73Snpiggin@suse.de /* 422d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 423d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 424d3ef3d73Snpiggin@suse.de */ 425d3ef3d73Snpiggin@suse.de smp_wmb(); 42683adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 42799b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 4283d733633SDave Hansen return ret; 4293d733633SDave Hansen } 4308366025eSDave Hansen 43183adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 4322e4b7fcdSDave Hansen { 43399b7db7bSNick Piggin br_write_lock(vfsmount_lock); 43483adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 43599b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 4362e4b7fcdSDave Hansen } 4372e4b7fcdSDave Hansen 438b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 4391da177e4SLinus Torvalds { 44052ba1621SAl Viro kfree(mnt->mnt_devname); 44173cd49ecSMiklos Szeredi mnt_free_id(mnt); 442d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 44368e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 444d3ef3d73Snpiggin@suse.de #endif 445b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* 449a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 450a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 45199b7db7bSNick Piggin * vfsmount_lock must be held for read or write. 4521da177e4SLinus Torvalds */ 453c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 454a05964f3SRam Pai int dir) 4551da177e4SLinus Torvalds { 4561da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 4571da177e4SLinus Torvalds struct list_head *tmp = head; 458c7105365SAl Viro struct mount *p, *found = NULL; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds for (;;) { 461a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 4621da177e4SLinus Torvalds p = NULL; 4631da177e4SLinus Torvalds if (tmp == head) 4641da177e4SLinus Torvalds break; 4651b8e5564SAl Viro p = list_entry(tmp, struct mount, mnt_hash); 466a73324daSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) { 467a05964f3SRam Pai found = p; 4681da177e4SLinus Torvalds break; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds return found; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 474a05964f3SRam Pai /* 475a05964f3SRam Pai * lookup_mnt increments the ref count before returning 476a05964f3SRam Pai * the vfsmount struct. 477a05964f3SRam Pai */ 4781c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 479a05964f3SRam Pai { 480c7105365SAl Viro struct mount *child_mnt; 48199b7db7bSNick Piggin 48299b7db7bSNick Piggin br_read_lock(vfsmount_lock); 483c7105365SAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry, 1); 484c7105365SAl Viro if (child_mnt) { 485c7105365SAl Viro mnt_add_count(child_mnt, 1); 48699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 487c7105365SAl Viro return &child_mnt->mnt; 488c7105365SAl Viro } else { 489c7105365SAl Viro br_read_unlock(vfsmount_lock); 490c7105365SAl Viro return NULL; 491c7105365SAl Viro } 492a05964f3SRam Pai } 493a05964f3SRam Pai 494143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 4951da177e4SLinus Torvalds { 4966b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 49999b7db7bSNick Piggin /* 50099b7db7bSNick Piggin * vfsmount lock must be held for write 50199b7db7bSNick Piggin */ 5026b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 5035addc5ddSAl Viro { 5045addc5ddSAl Viro if (ns) { 5055addc5ddSAl Viro ns->event = ++event; 5065addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5075addc5ddSAl Viro } 5085addc5ddSAl Viro } 5095addc5ddSAl Viro 51099b7db7bSNick Piggin /* 51199b7db7bSNick Piggin * vfsmount lock must be held for write 51299b7db7bSNick Piggin */ 5136b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 5145addc5ddSAl Viro { 5155addc5ddSAl Viro if (ns && ns->event != event) { 5165addc5ddSAl Viro ns->event = event; 5175addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5185addc5ddSAl Viro } 5195addc5ddSAl Viro } 5205addc5ddSAl Viro 52199b7db7bSNick Piggin /* 5225f57cbccSNick Piggin * Clear dentry's mounted state if it has no remaining mounts. 5235f57cbccSNick Piggin * vfsmount_lock must be held for write. 5245f57cbccSNick Piggin */ 525aa0a4cf0SAl Viro static void dentry_reset_mounted(struct dentry *dentry) 5265f57cbccSNick Piggin { 5275f57cbccSNick Piggin unsigned u; 5285f57cbccSNick Piggin 5295f57cbccSNick Piggin for (u = 0; u < HASH_SIZE; u++) { 530d5e50f74SAl Viro struct mount *p; 5315f57cbccSNick Piggin 5321b8e5564SAl Viro list_for_each_entry(p, &mount_hashtable[u], mnt_hash) { 533a73324daSAl Viro if (p->mnt_mountpoint == dentry) 5345f57cbccSNick Piggin return; 5355f57cbccSNick Piggin } 5365f57cbccSNick Piggin } 5375f57cbccSNick Piggin spin_lock(&dentry->d_lock); 5385f57cbccSNick Piggin dentry->d_flags &= ~DCACHE_MOUNTED; 5395f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 5405f57cbccSNick Piggin } 5415f57cbccSNick Piggin 5425f57cbccSNick Piggin /* 54399b7db7bSNick Piggin * vfsmount lock must be held for write 54499b7db7bSNick Piggin */ 545419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 5461da177e4SLinus Torvalds { 547a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 5480714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 5490714a533SAl Viro mnt->mnt_parent = mnt; 550a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 5516b41d536SAl Viro list_del_init(&mnt->mnt_child); 5521b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 553aa0a4cf0SAl Viro dentry_reset_mounted(old_path->dentry); 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 55699b7db7bSNick Piggin /* 55799b7db7bSNick Piggin * vfsmount lock must be held for write 55899b7db7bSNick Piggin */ 55914cf1fa8SAl Viro void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry, 56044d964d6SAl Viro struct mount *child_mnt) 561b90fa9aeSRam Pai { 5623a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 563a73324daSAl Viro child_mnt->mnt_mountpoint = dget(dentry); 5643a2393d7SAl Viro child_mnt->mnt_parent = mnt; 5655f57cbccSNick Piggin spin_lock(&dentry->d_lock); 5665f57cbccSNick Piggin dentry->d_flags |= DCACHE_MOUNTED; 5675f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 568b90fa9aeSRam Pai } 569b90fa9aeSRam Pai 57099b7db7bSNick Piggin /* 57199b7db7bSNick Piggin * vfsmount lock must be held for write 57299b7db7bSNick Piggin */ 573419148daSAl Viro static void attach_mnt(struct mount *mnt, struct path *path) 5741da177e4SLinus Torvalds { 57514cf1fa8SAl Viro mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt); 5761b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 5771a390689SAl Viro hash(path->mnt, path->dentry)); 5786b41d536SAl Viro list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts); 579b90fa9aeSRam Pai } 580b90fa9aeSRam Pai 58183adc753SAl Viro static inline void __mnt_make_longterm(struct mount *mnt) 5827e3d0eb0SAl Viro { 5837e3d0eb0SAl Viro #ifdef CONFIG_SMP 58468e8a9feSAl Viro atomic_inc(&mnt->mnt_longterm); 5857e3d0eb0SAl Viro #endif 5867e3d0eb0SAl Viro } 5877e3d0eb0SAl Viro 5887e3d0eb0SAl Viro /* needs vfsmount lock for write */ 58983adc753SAl Viro static inline void __mnt_make_shortterm(struct mount *mnt) 5907e3d0eb0SAl Viro { 5917e3d0eb0SAl Viro #ifdef CONFIG_SMP 59268e8a9feSAl Viro atomic_dec(&mnt->mnt_longterm); 5937e3d0eb0SAl Viro #endif 5947e3d0eb0SAl Viro } 5957e3d0eb0SAl Viro 596b90fa9aeSRam Pai /* 59799b7db7bSNick Piggin * vfsmount lock must be held for write 598b90fa9aeSRam Pai */ 5994b2619a5SAl Viro static void commit_tree(struct mount *mnt) 600b90fa9aeSRam Pai { 6010714a533SAl Viro struct mount *parent = mnt->mnt_parent; 60283adc753SAl Viro struct mount *m; 603b90fa9aeSRam Pai LIST_HEAD(head); 604143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 605b90fa9aeSRam Pai 6060714a533SAl Viro BUG_ON(parent == mnt); 607b90fa9aeSRam Pai 6081a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 6091a4eeaf2SAl Viro list_for_each_entry(m, &head, mnt_list) { 610143c8c91SAl Viro m->mnt_ns = n; 6117e3d0eb0SAl Viro __mnt_make_longterm(m); 612f03c6599SAl Viro } 613f03c6599SAl Viro 614b90fa9aeSRam Pai list_splice(&head, n->list.prev); 615b90fa9aeSRam Pai 6161b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 617a73324daSAl Viro hash(&parent->mnt, mnt->mnt_mountpoint)); 6186b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 6196b3286edSKirill Korotaev touch_mnt_namespace(n); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 622909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 6231da177e4SLinus Torvalds { 6246b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 6256b41d536SAl Viro if (next == &p->mnt_mounts) { 6261da177e4SLinus Torvalds while (1) { 627909b0a88SAl Viro if (p == root) 6281da177e4SLinus Torvalds return NULL; 6296b41d536SAl Viro next = p->mnt_child.next; 6306b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 6311da177e4SLinus Torvalds break; 6320714a533SAl Viro p = p->mnt_parent; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds } 6356b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 638315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 6399676f0c6SRam Pai { 6406b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 6416b41d536SAl Viro while (prev != &p->mnt_mounts) { 6426b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 6436b41d536SAl Viro prev = p->mnt_mounts.prev; 6449676f0c6SRam Pai } 6459676f0c6SRam Pai return p; 6469676f0c6SRam Pai } 6479676f0c6SRam Pai 6489d412a43SAl Viro struct vfsmount * 6499d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 6509d412a43SAl Viro { 651b105e270SAl Viro struct mount *mnt; 6529d412a43SAl Viro struct dentry *root; 6539d412a43SAl Viro 6549d412a43SAl Viro if (!type) 6559d412a43SAl Viro return ERR_PTR(-ENODEV); 6569d412a43SAl Viro 6579d412a43SAl Viro mnt = alloc_vfsmnt(name); 6589d412a43SAl Viro if (!mnt) 6599d412a43SAl Viro return ERR_PTR(-ENOMEM); 6609d412a43SAl Viro 6619d412a43SAl Viro if (flags & MS_KERNMOUNT) 662b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 6639d412a43SAl Viro 6649d412a43SAl Viro root = mount_fs(type, flags, name, data); 6659d412a43SAl Viro if (IS_ERR(root)) { 6669d412a43SAl Viro free_vfsmnt(mnt); 6679d412a43SAl Viro return ERR_CAST(root); 6689d412a43SAl Viro } 6699d412a43SAl Viro 670b105e270SAl Viro mnt->mnt.mnt_root = root; 671b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 672a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 6730714a533SAl Viro mnt->mnt_parent = mnt; 67439f7c4dbSMiklos Szeredi br_write_lock(vfsmount_lock); 67539f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 67639f7c4dbSMiklos Szeredi br_write_unlock(vfsmount_lock); 677b105e270SAl Viro return &mnt->mnt; 6789d412a43SAl Viro } 6799d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 6809d412a43SAl Viro 68187129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 68236341f64SRam Pai int flag) 6831da177e4SLinus Torvalds { 68487129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 68552ba1621SAl Viro struct mount *mnt = alloc_vfsmnt(old->mnt_devname); 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds if (mnt) { 688719f5d7fSMiklos Szeredi if (flag & (CL_SLAVE | CL_PRIVATE)) 68915169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 690719f5d7fSMiklos Szeredi else 69115169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 692719f5d7fSMiklos Szeredi 69315169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 694b105e270SAl Viro int err = mnt_alloc_group_id(mnt); 695719f5d7fSMiklos Szeredi if (err) 696719f5d7fSMiklos Szeredi goto out_free; 697719f5d7fSMiklos Szeredi } 698719f5d7fSMiklos Szeredi 69987129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 7001da177e4SLinus Torvalds atomic_inc(&sb->s_active); 701b105e270SAl Viro mnt->mnt.mnt_sb = sb; 702b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 703a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7040714a533SAl Viro mnt->mnt_parent = mnt; 70539f7c4dbSMiklos Szeredi br_write_lock(vfsmount_lock); 70639f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 70739f7c4dbSMiklos Szeredi br_write_unlock(vfsmount_lock); 708b90fa9aeSRam Pai 7095afe0022SRam Pai if (flag & CL_SLAVE) { 7106776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 71132301920SAl Viro mnt->mnt_master = old; 712fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 7138aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 714fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 7156776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 716d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 7176776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 718d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 7195afe0022SRam Pai } 720b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 7210f0afb1dSAl Viro set_mnt_shared(mnt); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 7241da177e4SLinus Torvalds * as the original if that was on one */ 72536341f64SRam Pai if (flag & CL_EXPIRE) { 7266776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 7276776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 7281da177e4SLinus Torvalds } 72936341f64SRam Pai } 730cb338d06SAl Viro return mnt; 731719f5d7fSMiklos Szeredi 732719f5d7fSMiklos Szeredi out_free: 733719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 734719f5d7fSMiklos Szeredi return NULL; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 73783adc753SAl Viro static inline void mntfree(struct mount *mnt) 7381da177e4SLinus Torvalds { 73983adc753SAl Viro struct vfsmount *m = &mnt->mnt; 74083adc753SAl Viro struct super_block *sb = m->mnt_sb; 741b3e19d92SNick Piggin 7423d733633SDave Hansen /* 7433d733633SDave Hansen * This probably indicates that somebody messed 7443d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 7453d733633SDave Hansen * happens, the filesystem was probably unable 7463d733633SDave Hansen * to make r/w->r/o transitions. 7473d733633SDave Hansen */ 748d3ef3d73Snpiggin@suse.de /* 749b3e19d92SNick Piggin * The locking used to deal with mnt_count decrement provides barriers, 750b3e19d92SNick Piggin * so mnt_get_writers() below is safe. 751d3ef3d73Snpiggin@suse.de */ 752c6653a83SNick Piggin WARN_ON(mnt_get_writers(mnt)); 75383adc753SAl Viro fsnotify_vfsmount_delete(m); 75483adc753SAl Viro dput(m->mnt_root); 75583adc753SAl Viro free_vfsmnt(mnt); 7561da177e4SLinus Torvalds deactivate_super(sb); 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 759900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 7607b7b1aceSAl Viro { 761b3e19d92SNick Piggin put_again: 762f03c6599SAl Viro #ifdef CONFIG_SMP 763b3e19d92SNick Piggin br_read_lock(vfsmount_lock); 76468e8a9feSAl Viro if (likely(atomic_read(&mnt->mnt_longterm))) { 765aa9c0e07SAl Viro mnt_add_count(mnt, -1); 766b3e19d92SNick Piggin br_read_unlock(vfsmount_lock); 76799b7db7bSNick Piggin return; 768b3e19d92SNick Piggin } 769b3e19d92SNick Piggin br_read_unlock(vfsmount_lock); 770b3e19d92SNick Piggin 77199b7db7bSNick Piggin br_write_lock(vfsmount_lock); 772aa9c0e07SAl Viro mnt_add_count(mnt, -1); 773b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 77499b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 77599b7db7bSNick Piggin return; 77699b7db7bSNick Piggin } 777b3e19d92SNick Piggin #else 778aa9c0e07SAl Viro mnt_add_count(mnt, -1); 779b3e19d92SNick Piggin if (likely(mnt_get_count(mnt))) 780b3e19d92SNick Piggin return; 781b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 782f03c6599SAl Viro #endif 783863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 784863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 785863d684fSAl Viro mnt->mnt_pinned = 0; 786b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 787900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 788b3e19d92SNick Piggin goto put_again; 789b3e19d92SNick Piggin } 79039f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 791b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 792b3e19d92SNick Piggin mntfree(mnt); 793b3e19d92SNick Piggin } 794b3e19d92SNick Piggin 795b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 796b3e19d92SNick Piggin { 797b3e19d92SNick Piggin if (mnt) { 798863d684fSAl Viro struct mount *m = real_mount(mnt); 799b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 800863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 801863d684fSAl Viro m->mnt_expiry_mark = 0; 802863d684fSAl Viro mntput_no_expire(m); 803b3e19d92SNick Piggin } 804b3e19d92SNick Piggin } 805b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 806b3e19d92SNick Piggin 807b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 808b3e19d92SNick Piggin { 809b3e19d92SNick Piggin if (mnt) 81083adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 811b3e19d92SNick Piggin return mnt; 812b3e19d92SNick Piggin } 813b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 814b3e19d92SNick Piggin 8157b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 8167b7b1aceSAl Viro { 81799b7db7bSNick Piggin br_write_lock(vfsmount_lock); 818863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 81999b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 8207b7b1aceSAl Viro } 8217b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 8227b7b1aceSAl Viro 823863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 8247b7b1aceSAl Viro { 825863d684fSAl Viro struct mount *mnt = real_mount(m); 82699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 8277b7b1aceSAl Viro if (mnt->mnt_pinned) { 828863d684fSAl Viro mnt_add_count(mnt, 1); 8297b7b1aceSAl Viro mnt->mnt_pinned--; 8307b7b1aceSAl Viro } 83199b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 8327b7b1aceSAl Viro } 8337b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 8341da177e4SLinus Torvalds 835b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 836b3b304a2SMiklos Szeredi { 837b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 838b3b304a2SMiklos Szeredi } 839b3b304a2SMiklos Szeredi 840b3b304a2SMiklos Szeredi /* 841b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 842b3b304a2SMiklos Szeredi * implement more complex mount option showing. 843b3b304a2SMiklos Szeredi * 844b3b304a2SMiklos Szeredi * See also save_mount_options(). 845b3b304a2SMiklos Szeredi */ 84634c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 847b3b304a2SMiklos Szeredi { 8482a32cebdSAl Viro const char *options; 8492a32cebdSAl Viro 8502a32cebdSAl Viro rcu_read_lock(); 85134c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 852b3b304a2SMiklos Szeredi 853b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 854b3b304a2SMiklos Szeredi seq_putc(m, ','); 855b3b304a2SMiklos Szeredi mangle(m, options); 856b3b304a2SMiklos Szeredi } 8572a32cebdSAl Viro rcu_read_unlock(); 858b3b304a2SMiklos Szeredi 859b3b304a2SMiklos Szeredi return 0; 860b3b304a2SMiklos Szeredi } 861b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 862b3b304a2SMiklos Szeredi 863b3b304a2SMiklos Szeredi /* 864b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 865b3b304a2SMiklos Szeredi * called from the fill_super() callback. 866b3b304a2SMiklos Szeredi * 867b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 868b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 869b3b304a2SMiklos Szeredi * remount fails. 870b3b304a2SMiklos Szeredi * 871b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 872b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 873b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 874b3b304a2SMiklos Szeredi * any more. 875b3b304a2SMiklos Szeredi */ 876b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 877b3b304a2SMiklos Szeredi { 8782a32cebdSAl Viro BUG_ON(sb->s_options); 8792a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 880b3b304a2SMiklos Szeredi } 881b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 882b3b304a2SMiklos Szeredi 8832a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 8842a32cebdSAl Viro { 8852a32cebdSAl Viro char *old = sb->s_options; 8862a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 8872a32cebdSAl Viro if (old) { 8882a32cebdSAl Viro synchronize_rcu(); 8892a32cebdSAl Viro kfree(old); 8902a32cebdSAl Viro } 8912a32cebdSAl Viro } 8922a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 8932a32cebdSAl Viro 894a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 8950226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 8961da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 8971da177e4SLinus Torvalds { 8980226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 8991da177e4SLinus Torvalds 900390c6843SRam Pai down_read(&namespace_sem); 901a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 9051da177e4SLinus Torvalds { 9060226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 907b0765fb8SPavel Emelianov 908a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 9121da177e4SLinus Torvalds { 913390c6843SRam Pai up_read(&namespace_sem); 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds 9160226f492SAl Viro static int m_show(struct seq_file *m, void *v) 9179f5596afSAl Viro { 9180226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 9191a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 9200226f492SAl Viro return p->show(m, &r->mnt); 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 923a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 9241da177e4SLinus Torvalds .start = m_start, 9251da177e4SLinus Torvalds .next = m_next, 9261da177e4SLinus Torvalds .stop = m_stop, 9270226f492SAl Viro .show = m_show, 928b4629fe2SChuck Lever }; 929a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 930b4629fe2SChuck Lever 9311da177e4SLinus Torvalds /** 9321da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 9331da177e4SLinus Torvalds * @mnt: root of mount tree 9341da177e4SLinus Torvalds * 9351da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 9361da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 9371da177e4SLinus Torvalds * busy. 9381da177e4SLinus Torvalds */ 939909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 9401da177e4SLinus Torvalds { 941909b0a88SAl Viro struct mount *mnt = real_mount(m); 94236341f64SRam Pai int actual_refs = 0; 94336341f64SRam Pai int minimum_refs = 0; 944315fc83eSAl Viro struct mount *p; 945909b0a88SAl Viro BUG_ON(!m); 9461da177e4SLinus Torvalds 947b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 948b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 949909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 95083adc753SAl Viro actual_refs += mnt_get_count(p); 9511da177e4SLinus Torvalds minimum_refs += 2; 9521da177e4SLinus Torvalds } 953b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds if (actual_refs > minimum_refs) 9561da177e4SLinus Torvalds return 0; 957e3474a8eSIan Kent 958e3474a8eSIan Kent return 1; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds /** 9641da177e4SLinus Torvalds * may_umount - check if a mount point is busy 9651da177e4SLinus Torvalds * @mnt: root of mount 9661da177e4SLinus Torvalds * 9671da177e4SLinus Torvalds * This is called to check if a mount point has any 9681da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 9691da177e4SLinus Torvalds * mount has sub mounts this will return busy 9701da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 9711da177e4SLinus Torvalds * 9721da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 9731da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 9741da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 9751da177e4SLinus Torvalds */ 9761da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 9771da177e4SLinus Torvalds { 978e3474a8eSIan Kent int ret = 1; 9798ad08d8aSAl Viro down_read(&namespace_sem); 980b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 9811ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 982e3474a8eSIan Kent ret = 0; 983b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 9848ad08d8aSAl Viro up_read(&namespace_sem); 985a05964f3SRam Pai return ret; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 9891da177e4SLinus Torvalds 990b90fa9aeSRam Pai void release_mounts(struct list_head *head) 9911da177e4SLinus Torvalds { 992d5e50f74SAl Viro struct mount *mnt; 99370fbcdf4SRam Pai while (!list_empty(head)) { 9941b8e5564SAl Viro mnt = list_first_entry(head, struct mount, mnt_hash); 9951b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 996676da58dSAl Viro if (mnt_has_parent(mnt)) { 99770fbcdf4SRam Pai struct dentry *dentry; 998863d684fSAl Viro struct mount *m; 99999b7db7bSNick Piggin 100099b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1001a73324daSAl Viro dentry = mnt->mnt_mountpoint; 1002863d684fSAl Viro m = mnt->mnt_parent; 1003a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10040714a533SAl Viro mnt->mnt_parent = mnt; 10057c4b93d8SAl Viro m->mnt_ghosts--; 100699b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 100770fbcdf4SRam Pai dput(dentry); 1008863d684fSAl Viro mntput(&m->mnt); 10091da177e4SLinus Torvalds } 1010d5e50f74SAl Viro mntput(&mnt->mnt); 101170fbcdf4SRam Pai } 101270fbcdf4SRam Pai } 101370fbcdf4SRam Pai 101499b7db7bSNick Piggin /* 101599b7db7bSNick Piggin * vfsmount lock must be held for write 101699b7db7bSNick Piggin * namespace_sem must be held for write 101799b7db7bSNick Piggin */ 1018761d5c38SAl Viro void umount_tree(struct mount *mnt, int propagate, struct list_head *kill) 101970fbcdf4SRam Pai { 10207b8a53fdSAl Viro LIST_HEAD(tmp_list); 1021315fc83eSAl Viro struct mount *p; 102270fbcdf4SRam Pai 1023909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) 10241b8e5564SAl Viro list_move(&p->mnt_hash, &tmp_list); 102570fbcdf4SRam Pai 1026a05964f3SRam Pai if (propagate) 10277b8a53fdSAl Viro propagate_umount(&tmp_list); 1028a05964f3SRam Pai 10291b8e5564SAl Viro list_for_each_entry(p, &tmp_list, mnt_hash) { 10306776db3dSAl Viro list_del_init(&p->mnt_expire); 10311a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1032143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 1033143c8c91SAl Viro p->mnt_ns = NULL; 103483adc753SAl Viro __mnt_make_shortterm(p); 10356b41d536SAl Viro list_del_init(&p->mnt_child); 1036676da58dSAl Viro if (mnt_has_parent(p)) { 1037863d684fSAl Viro p->mnt_parent->mnt_ghosts++; 1038a73324daSAl Viro dentry_reset_mounted(p->mnt_mountpoint); 10397c4b93d8SAl Viro } 10400f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 10411da177e4SLinus Torvalds } 10427b8a53fdSAl Viro list_splice(&tmp_list, kill); 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 1045692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts); 1046c35038beSAl Viro 10471ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 10481da177e4SLinus Torvalds { 10491ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 10501da177e4SLinus Torvalds int retval; 105170fbcdf4SRam Pai LIST_HEAD(umount_list); 10521da177e4SLinus Torvalds 10531ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 10541da177e4SLinus Torvalds if (retval) 10551da177e4SLinus Torvalds return retval; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* 10581da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 10591da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 10601da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 10611da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 10621da177e4SLinus Torvalds */ 10631da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 10641ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 10651da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 10661da177e4SLinus Torvalds return -EINVAL; 10671da177e4SLinus Torvalds 1068b3e19d92SNick Piggin /* 1069b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1070b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1071b3e19d92SNick Piggin */ 1072b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 107383adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1074bf9faa2aSJ. R. Okajima br_write_unlock(vfsmount_lock); 10751da177e4SLinus Torvalds return -EBUSY; 1076b3e19d92SNick Piggin } 1077b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 10781da177e4SLinus Torvalds 1079863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 10801da177e4SLinus Torvalds return -EAGAIN; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds /* 10841da177e4SLinus Torvalds * If we may have to abort operations to get out of this 10851da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 10861da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 10871da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 10881da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 10891da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 10901da177e4SLinus Torvalds * about for the moment. 10911da177e4SLinus Torvalds */ 10921da177e4SLinus Torvalds 109342faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 109442faad99SAl Viro sb->s_op->umount_begin(sb); 109542faad99SAl Viro } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds /* 10981da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 10991da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 11001da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 11011da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 11021da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 11031da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 11041da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 11051da177e4SLinus Torvalds */ 11061ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 11071da177e4SLinus Torvalds /* 11081da177e4SLinus Torvalds * Special case for "unmounting" root ... 11091da177e4SLinus Torvalds * we just try to remount it readonly. 11101da177e4SLinus Torvalds */ 11111da177e4SLinus Torvalds down_write(&sb->s_umount); 11124aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 11131da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 11141da177e4SLinus Torvalds up_write(&sb->s_umount); 11151da177e4SLinus Torvalds return retval; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 1118390c6843SRam Pai down_write(&namespace_sem); 111999b7db7bSNick Piggin br_write_lock(vfsmount_lock); 11205addc5ddSAl Viro event++; 11211da177e4SLinus Torvalds 1122c35038beSAl Viro if (!(flags & MNT_DETACH)) 11231ab59738SAl Viro shrink_submounts(mnt, &umount_list); 1124c35038beSAl Viro 11251da177e4SLinus Torvalds retval = -EBUSY; 1126a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 11271a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 11281ab59738SAl Viro umount_tree(mnt, 1, &umount_list); 11291da177e4SLinus Torvalds retval = 0; 11301da177e4SLinus Torvalds } 113199b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1132390c6843SRam Pai up_write(&namespace_sem); 113370fbcdf4SRam Pai release_mounts(&umount_list); 11341da177e4SLinus Torvalds return retval; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds /* 11381da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 11391da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 11401da177e4SLinus Torvalds * 11411da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 11421da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 11431da177e4SLinus Torvalds */ 11441da177e4SLinus Torvalds 1145bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 11461da177e4SLinus Torvalds { 11472d8f3038SAl Viro struct path path; 1148900148dcSAl Viro struct mount *mnt; 11491da177e4SLinus Torvalds int retval; 1150db1f05bbSMiklos Szeredi int lookup_flags = 0; 11511da177e4SLinus Torvalds 1152db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1153db1f05bbSMiklos Szeredi return -EINVAL; 1154db1f05bbSMiklos Szeredi 1155db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1156db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1157db1f05bbSMiklos Szeredi 1158db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 11591da177e4SLinus Torvalds if (retval) 11601da177e4SLinus Torvalds goto out; 1161900148dcSAl Viro mnt = real_mount(path.mnt); 11621da177e4SLinus Torvalds retval = -EINVAL; 11632d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 11641da177e4SLinus Torvalds goto dput_and_out; 1165143c8c91SAl Viro if (!check_mnt(mnt)) 11661da177e4SLinus Torvalds goto dput_and_out; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds retval = -EPERM; 11691da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11701da177e4SLinus Torvalds goto dput_and_out; 11711da177e4SLinus Torvalds 1172900148dcSAl Viro retval = do_umount(mnt, flags); 11731da177e4SLinus Torvalds dput_and_out: 1174429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 11752d8f3038SAl Viro dput(path.dentry); 1176900148dcSAl Viro mntput_no_expire(mnt); 11771da177e4SLinus Torvalds out: 11781da177e4SLinus Torvalds return retval; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds /* 11841da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 11851da177e4SLinus Torvalds */ 1186bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 11871da177e4SLinus Torvalds { 11881da177e4SLinus Torvalds return sys_umount(name, 0); 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds #endif 11921da177e4SLinus Torvalds 11932d92ab3cSAl Viro static int mount_is_safe(struct path *path) 11941da177e4SLinus Torvalds { 11951da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 11961da177e4SLinus Torvalds return 0; 11971da177e4SLinus Torvalds return -EPERM; 11981da177e4SLinus Torvalds #ifdef notyet 11992d92ab3cSAl Viro if (S_ISLNK(path->dentry->d_inode->i_mode)) 12001da177e4SLinus Torvalds return -EPERM; 12012d92ab3cSAl Viro if (path->dentry->d_inode->i_mode & S_ISVTX) { 1202da9592edSDavid Howells if (current_uid() != path->dentry->d_inode->i_uid) 12031da177e4SLinus Torvalds return -EPERM; 12041da177e4SLinus Torvalds } 12052d92ab3cSAl Viro if (inode_permission(path->dentry->d_inode, MAY_WRITE)) 12061da177e4SLinus Torvalds return -EPERM; 12071da177e4SLinus Torvalds return 0; 12081da177e4SLinus Torvalds #endif 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 121187129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 121236341f64SRam Pai int flag) 12131da177e4SLinus Torvalds { 1214a73324daSAl Viro struct mount *res, *p, *q, *r; 12151a390689SAl Viro struct path path; 12161da177e4SLinus Torvalds 1217fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 12189676f0c6SRam Pai return NULL; 12199676f0c6SRam Pai 122036341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 12211da177e4SLinus Torvalds if (!q) 12221da177e4SLinus Torvalds goto Enomem; 1223a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds p = mnt; 12266b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1227315fc83eSAl Viro struct mount *s; 12287ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 12291da177e4SLinus Torvalds continue; 12301da177e4SLinus Torvalds 1231909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 1232fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 12339676f0c6SRam Pai s = skip_mnt_tree(s); 12349676f0c6SRam Pai continue; 12359676f0c6SRam Pai } 12360714a533SAl Viro while (p != s->mnt_parent) { 12370714a533SAl Viro p = p->mnt_parent; 12380714a533SAl Viro q = q->mnt_parent; 12391da177e4SLinus Torvalds } 124087129cc0SAl Viro p = s; 1241cb338d06SAl Viro path.mnt = &q->mnt; 1242a73324daSAl Viro path.dentry = p->mnt_mountpoint; 124387129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 12441da177e4SLinus Torvalds if (!q) 12451da177e4SLinus Torvalds goto Enomem; 124699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 12471a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 1248cb338d06SAl Viro attach_mnt(q, &path); 124999b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds return res; 12531da177e4SLinus Torvalds Enomem: 12541da177e4SLinus Torvalds if (res) { 125570fbcdf4SRam Pai LIST_HEAD(umount_list); 125699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1257761d5c38SAl Viro umount_tree(res, 0, &umount_list); 125899b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 125970fbcdf4SRam Pai release_mounts(&umount_list); 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds return NULL; 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 1264589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 12658aec0809SAl Viro { 1266cb338d06SAl Viro struct mount *tree; 12671a60a280SAl Viro down_write(&namespace_sem); 126887129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 126987129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 12701a60a280SAl Viro up_write(&namespace_sem); 1271cb338d06SAl Viro return tree ? &tree->mnt : NULL; 12728aec0809SAl Viro } 12738aec0809SAl Viro 12748aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 12758aec0809SAl Viro { 12768aec0809SAl Viro LIST_HEAD(umount_list); 12771a60a280SAl Viro down_write(&namespace_sem); 127899b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1279761d5c38SAl Viro umount_tree(real_mount(mnt), 0, &umount_list); 128099b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 12811a60a280SAl Viro up_write(&namespace_sem); 12828aec0809SAl Viro release_mounts(&umount_list); 12838aec0809SAl Viro } 12848aec0809SAl Viro 12851f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 12861f707137SAl Viro struct vfsmount *root) 12871f707137SAl Viro { 12881a4eeaf2SAl Viro struct mount *mnt; 12891f707137SAl Viro int res = f(root, arg); 12901f707137SAl Viro if (res) 12911f707137SAl Viro return res; 12921a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 12931a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 12941f707137SAl Viro if (res) 12951f707137SAl Viro return res; 12961f707137SAl Viro } 12971f707137SAl Viro return 0; 12981f707137SAl Viro } 12991f707137SAl Viro 13004b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1301719f5d7fSMiklos Szeredi { 1302315fc83eSAl Viro struct mount *p; 1303719f5d7fSMiklos Szeredi 1304909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1305fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 13064b8b21f4SAl Viro mnt_release_group_id(p); 1307719f5d7fSMiklos Szeredi } 1308719f5d7fSMiklos Szeredi } 1309719f5d7fSMiklos Szeredi 13104b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1311719f5d7fSMiklos Szeredi { 1312315fc83eSAl Viro struct mount *p; 1313719f5d7fSMiklos Szeredi 1314909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1315fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 13164b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1317719f5d7fSMiklos Szeredi if (err) { 13184b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1319719f5d7fSMiklos Szeredi return err; 1320719f5d7fSMiklos Szeredi } 1321719f5d7fSMiklos Szeredi } 1322719f5d7fSMiklos Szeredi } 1323719f5d7fSMiklos Szeredi 1324719f5d7fSMiklos Szeredi return 0; 1325719f5d7fSMiklos Szeredi } 1326719f5d7fSMiklos Szeredi 1327b90fa9aeSRam Pai /* 1328b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1329b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 133021444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 133121444403SRam Pai * store the parent mount and mountpoint dentry. 133221444403SRam Pai * (done when source_mnt is moved) 1333b90fa9aeSRam Pai * 1334b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1335b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 13369676f0c6SRam Pai * --------------------------------------------------------------------------- 1337b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 13389676f0c6SRam Pai * |************************************************************************** 13399676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13409676f0c6SRam Pai * | dest | | | | | 13419676f0c6SRam Pai * | | | | | | | 13429676f0c6SRam Pai * | v | | | | | 13439676f0c6SRam Pai * |************************************************************************** 13449676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 13455afe0022SRam Pai * | | | | | | 13469676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 13479676f0c6SRam Pai * *************************************************************************** 1348b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1349b90fa9aeSRam Pai * destination mount. 1350b90fa9aeSRam Pai * 1351b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1352b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1353b90fa9aeSRam Pai * the peer group of the source mount. 1354b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1355b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1356b90fa9aeSRam Pai * mount. 13575afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 13585afe0022SRam Pai * of the destination mount and the cloned mount is made slave 13595afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 13605afe0022SRam Pai * is marked as 'shared and slave'. 13615afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 13625afe0022SRam Pai * source mount. 13635afe0022SRam Pai * 13649676f0c6SRam Pai * --------------------------------------------------------------------------- 136521444403SRam Pai * | MOVE MOUNT OPERATION | 13669676f0c6SRam Pai * |************************************************************************** 13679676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13689676f0c6SRam Pai * | dest | | | | | 13699676f0c6SRam Pai * | | | | | | | 13709676f0c6SRam Pai * | v | | | | | 13719676f0c6SRam Pai * |************************************************************************** 13729676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 13735afe0022SRam Pai * | | | | | | 13749676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 13759676f0c6SRam Pai * *************************************************************************** 13765afe0022SRam Pai * 13775afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 13785afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 137921444403SRam Pai * (+*) the mount is moved to the destination. 13805afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 13815afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 13825afe0022SRam Pai * the mount is marked as 'shared and slave'. 13835afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1384b90fa9aeSRam Pai * 1385b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1386b90fa9aeSRam Pai * applied to each mount in the tree. 1387b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1388b90fa9aeSRam Pai * in allocations. 1389b90fa9aeSRam Pai */ 13900fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 13911a390689SAl Viro struct path *path, struct path *parent_path) 1392b90fa9aeSRam Pai { 1393b90fa9aeSRam Pai LIST_HEAD(tree_list); 1394a8d56d8eSAl Viro struct mount *dest_mnt = real_mount(path->mnt); 13951a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1396315fc83eSAl Viro struct mount *child, *p; 1397719f5d7fSMiklos Szeredi int err; 1398b90fa9aeSRam Pai 1399fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 14000fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1401719f5d7fSMiklos Szeredi if (err) 1402719f5d7fSMiklos Szeredi goto out; 1403719f5d7fSMiklos Szeredi } 1404a8d56d8eSAl Viro err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); 1405719f5d7fSMiklos Szeredi if (err) 1406719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1407b90fa9aeSRam Pai 140899b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1409df1a1ad2SAl Viro 1410fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1411909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 14120f0afb1dSAl Viro set_mnt_shared(p); 1413b90fa9aeSRam Pai } 14141a390689SAl Viro if (parent_path) { 14150fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 14160fb54e50SAl Viro attach_mnt(source_mnt, path); 1417143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 141821444403SRam Pai } else { 141914cf1fa8SAl Viro mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 14200fb54e50SAl Viro commit_tree(source_mnt); 142121444403SRam Pai } 1422b90fa9aeSRam Pai 14231b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 14241b8e5564SAl Viro list_del_init(&child->mnt_hash); 14254b2619a5SAl Viro commit_tree(child); 1426b90fa9aeSRam Pai } 142799b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 142899b7db7bSNick Piggin 1429b90fa9aeSRam Pai return 0; 1430719f5d7fSMiklos Szeredi 1431719f5d7fSMiklos Szeredi out_cleanup_ids: 1432fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 14330fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1434719f5d7fSMiklos Szeredi out: 1435719f5d7fSMiklos Szeredi return err; 1436b90fa9aeSRam Pai } 1437b90fa9aeSRam Pai 1438b12cea91SAl Viro static int lock_mount(struct path *path) 1439b12cea91SAl Viro { 1440b12cea91SAl Viro struct vfsmount *mnt; 1441b12cea91SAl Viro retry: 1442b12cea91SAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1443b12cea91SAl Viro if (unlikely(cant_mount(path->dentry))) { 1444b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1445b12cea91SAl Viro return -ENOENT; 1446b12cea91SAl Viro } 1447b12cea91SAl Viro down_write(&namespace_sem); 1448b12cea91SAl Viro mnt = lookup_mnt(path); 1449b12cea91SAl Viro if (likely(!mnt)) 1450b12cea91SAl Viro return 0; 1451b12cea91SAl Viro up_write(&namespace_sem); 1452b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1453b12cea91SAl Viro path_put(path); 1454b12cea91SAl Viro path->mnt = mnt; 1455b12cea91SAl Viro path->dentry = dget(mnt->mnt_root); 1456b12cea91SAl Viro goto retry; 1457b12cea91SAl Viro } 1458b12cea91SAl Viro 1459b12cea91SAl Viro static void unlock_mount(struct path *path) 1460b12cea91SAl Viro { 1461b12cea91SAl Viro up_write(&namespace_sem); 1462b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1463b12cea91SAl Viro } 1464b12cea91SAl Viro 146595bc5f25SAl Viro static int graft_tree(struct mount *mnt, struct path *path) 14661da177e4SLinus Torvalds { 146795bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 14681da177e4SLinus Torvalds return -EINVAL; 14691da177e4SLinus Torvalds 14708c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 147195bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 14721da177e4SLinus Torvalds return -ENOTDIR; 14731da177e4SLinus Torvalds 1474b12cea91SAl Viro if (d_unlinked(path->dentry)) 1475b12cea91SAl Viro return -ENOENT; 14761da177e4SLinus Torvalds 147795bc5f25SAl Viro return attach_recursive_mnt(mnt, path, NULL); 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds /* 14817a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 14827a2e8a8fSValerie Aurora */ 14837a2e8a8fSValerie Aurora 14847a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 14857a2e8a8fSValerie Aurora { 14867c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 14877a2e8a8fSValerie Aurora 14887a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 14897a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 14907a2e8a8fSValerie Aurora return 0; 14917a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 14927a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 14937a2e8a8fSValerie Aurora return 0; 14947a2e8a8fSValerie Aurora return type; 14957a2e8a8fSValerie Aurora } 14967a2e8a8fSValerie Aurora 14977a2e8a8fSValerie Aurora /* 149807b20889SRam Pai * recursively change the type of the mountpoint. 149907b20889SRam Pai */ 15000a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 150107b20889SRam Pai { 1502315fc83eSAl Viro struct mount *m; 15034b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 150407b20889SRam Pai int recurse = flag & MS_REC; 15057a2e8a8fSValerie Aurora int type; 1506719f5d7fSMiklos Szeredi int err = 0; 150707b20889SRam Pai 1508ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 1509ee6f9582SMiklos Szeredi return -EPERM; 1510ee6f9582SMiklos Szeredi 15112d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 151207b20889SRam Pai return -EINVAL; 151307b20889SRam Pai 15147a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 15157a2e8a8fSValerie Aurora if (!type) 15167a2e8a8fSValerie Aurora return -EINVAL; 15177a2e8a8fSValerie Aurora 151807b20889SRam Pai down_write(&namespace_sem); 1519719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1520719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1521719f5d7fSMiklos Szeredi if (err) 1522719f5d7fSMiklos Szeredi goto out_unlock; 1523719f5d7fSMiklos Szeredi } 1524719f5d7fSMiklos Szeredi 152599b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1526909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 15270f0afb1dSAl Viro change_mnt_propagation(m, type); 152899b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1529719f5d7fSMiklos Szeredi 1530719f5d7fSMiklos Szeredi out_unlock: 153107b20889SRam Pai up_write(&namespace_sem); 1532719f5d7fSMiklos Szeredi return err; 153307b20889SRam Pai } 153407b20889SRam Pai 153507b20889SRam Pai /* 15361da177e4SLinus Torvalds * do loopback mount. 15371da177e4SLinus Torvalds */ 15380a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name, 15392dafe1c4SEric Sandeen int recurse) 15401da177e4SLinus Torvalds { 1541b12cea91SAl Viro LIST_HEAD(umount_list); 15422d92ab3cSAl Viro struct path old_path; 154387129cc0SAl Viro struct mount *mnt = NULL, *old; 15442d92ab3cSAl Viro int err = mount_is_safe(path); 15451da177e4SLinus Torvalds if (err) 15461da177e4SLinus Torvalds return err; 15471da177e4SLinus Torvalds if (!old_name || !*old_name) 15481da177e4SLinus Torvalds return -EINVAL; 1549815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 15501da177e4SLinus Torvalds if (err) 15511da177e4SLinus Torvalds return err; 15521da177e4SLinus Torvalds 1553b12cea91SAl Viro err = lock_mount(path); 1554b12cea91SAl Viro if (err) 15559676f0c6SRam Pai goto out; 15569676f0c6SRam Pai 155787129cc0SAl Viro old = real_mount(old_path.mnt); 155887129cc0SAl Viro 1559b12cea91SAl Viro err = -EINVAL; 1560fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1561b12cea91SAl Viro goto out2; 1562b12cea91SAl Viro 1563143c8c91SAl Viro if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old)) 1564b12cea91SAl Viro goto out2; 1565ccd48bc7SAl Viro 15661da177e4SLinus Torvalds err = -ENOMEM; 15671da177e4SLinus Torvalds if (recurse) 156887129cc0SAl Viro mnt = copy_tree(old, old_path.dentry, 0); 15691da177e4SLinus Torvalds else 157087129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 15711da177e4SLinus Torvalds 1572ccd48bc7SAl Viro if (!mnt) 1573b12cea91SAl Viro goto out2; 1574ccd48bc7SAl Viro 157595bc5f25SAl Viro err = graft_tree(mnt, path); 15761da177e4SLinus Torvalds if (err) { 157799b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1578761d5c38SAl Viro umount_tree(mnt, 0, &umount_list); 157999b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 15805b83d2c5SRam Pai } 1581b12cea91SAl Viro out2: 1582b12cea91SAl Viro unlock_mount(path); 1583b12cea91SAl Viro release_mounts(&umount_list); 1584ccd48bc7SAl Viro out: 15852d92ab3cSAl Viro path_put(&old_path); 15861da177e4SLinus Torvalds return err; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15892e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 15902e4b7fcdSDave Hansen { 15912e4b7fcdSDave Hansen int error = 0; 15922e4b7fcdSDave Hansen int readonly_request = 0; 15932e4b7fcdSDave Hansen 15942e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 15952e4b7fcdSDave Hansen readonly_request = 1; 15962e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 15972e4b7fcdSDave Hansen return 0; 15982e4b7fcdSDave Hansen 15992e4b7fcdSDave Hansen if (readonly_request) 160083adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 16012e4b7fcdSDave Hansen else 160283adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 16032e4b7fcdSDave Hansen return error; 16042e4b7fcdSDave Hansen } 16052e4b7fcdSDave Hansen 16061da177e4SLinus Torvalds /* 16071da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 16081da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 16091da177e4SLinus Torvalds * on it - tough luck. 16101da177e4SLinus Torvalds */ 16110a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 16121da177e4SLinus Torvalds void *data) 16131da177e4SLinus Torvalds { 16141da177e4SLinus Torvalds int err; 16152d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1616143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16191da177e4SLinus Torvalds return -EPERM; 16201da177e4SLinus Torvalds 1621143c8c91SAl Viro if (!check_mnt(mnt)) 16221da177e4SLinus Torvalds return -EINVAL; 16231da177e4SLinus Torvalds 16242d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 16251da177e4SLinus Torvalds return -EINVAL; 16261da177e4SLinus Torvalds 1627ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1628ff36fe2cSEric Paris if (err) 1629ff36fe2cSEric Paris return err; 1630ff36fe2cSEric Paris 16311da177e4SLinus Torvalds down_write(&sb->s_umount); 16322e4b7fcdSDave Hansen if (flags & MS_BIND) 16332d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 16344aa98cf7SAl Viro else 16351da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 16367b43a79fSAl Viro if (!err) { 163799b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1638143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1639143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 164099b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 16417b43a79fSAl Viro } 16421da177e4SLinus Torvalds up_write(&sb->s_umount); 16430e55a7ccSDan Williams if (!err) { 164499b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1645143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 164699b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 16470e55a7ccSDan Williams } 16481da177e4SLinus Torvalds return err; 16491da177e4SLinus Torvalds } 16501da177e4SLinus Torvalds 1651cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 16529676f0c6SRam Pai { 1653315fc83eSAl Viro struct mount *p; 1654909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1655fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 16569676f0c6SRam Pai return 1; 16579676f0c6SRam Pai } 16589676f0c6SRam Pai return 0; 16599676f0c6SRam Pai } 16609676f0c6SRam Pai 16610a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name) 16621da177e4SLinus Torvalds { 16632d92ab3cSAl Viro struct path old_path, parent_path; 1664676da58dSAl Viro struct mount *p; 16650fb54e50SAl Viro struct mount *old; 16661da177e4SLinus Torvalds int err = 0; 16671da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16681da177e4SLinus Torvalds return -EPERM; 16691da177e4SLinus Torvalds if (!old_name || !*old_name) 16701da177e4SLinus Torvalds return -EINVAL; 16712d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 16721da177e4SLinus Torvalds if (err) 16731da177e4SLinus Torvalds return err; 16741da177e4SLinus Torvalds 1675b12cea91SAl Viro err = lock_mount(path); 1676cc53ce53SDavid Howells if (err < 0) 1677cc53ce53SDavid Howells goto out; 1678cc53ce53SDavid Howells 1679143c8c91SAl Viro old = real_mount(old_path.mnt); 1680fc7be130SAl Viro p = real_mount(path->mnt); 1681143c8c91SAl Viro 16821da177e4SLinus Torvalds err = -EINVAL; 1683fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 16841da177e4SLinus Torvalds goto out1; 16851da177e4SLinus Torvalds 1686f3da392eSAlexey Dobriyan if (d_unlinked(path->dentry)) 168721444403SRam Pai goto out1; 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds err = -EINVAL; 16902d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 169121444403SRam Pai goto out1; 16921da177e4SLinus Torvalds 1693676da58dSAl Viro if (!mnt_has_parent(old)) 169421444403SRam Pai goto out1; 16951da177e4SLinus Torvalds 16962d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 16972d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 169821444403SRam Pai goto out1; 169921444403SRam Pai /* 170021444403SRam Pai * Don't move a mount residing in a shared parent. 170121444403SRam Pai */ 1702fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 170321444403SRam Pai goto out1; 17049676f0c6SRam Pai /* 17059676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 17069676f0c6SRam Pai * mount which is shared. 17079676f0c6SRam Pai */ 1708fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 17099676f0c6SRam Pai goto out1; 17101da177e4SLinus Torvalds err = -ELOOP; 1711fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1712676da58dSAl Viro if (p == old) 171321444403SRam Pai goto out1; 17141da177e4SLinus Torvalds 17150fb54e50SAl Viro err = attach_recursive_mnt(old, path, &parent_path); 17164ac91378SJan Blunck if (err) 171721444403SRam Pai goto out1; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 17201da177e4SLinus Torvalds * automatically */ 17216776db3dSAl Viro list_del_init(&old->mnt_expire); 17221da177e4SLinus Torvalds out1: 1723b12cea91SAl Viro unlock_mount(path); 17241da177e4SLinus Torvalds out: 17251da177e4SLinus Torvalds if (!err) 17261a390689SAl Viro path_put(&parent_path); 17272d92ab3cSAl Viro path_put(&old_path); 17281da177e4SLinus Torvalds return err; 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds 17319d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 17329d412a43SAl Viro { 17339d412a43SAl Viro int err; 17349d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 17359d412a43SAl Viro if (subtype) { 17369d412a43SAl Viro subtype++; 17379d412a43SAl Viro err = -EINVAL; 17389d412a43SAl Viro if (!subtype[0]) 17399d412a43SAl Viro goto err; 17409d412a43SAl Viro } else 17419d412a43SAl Viro subtype = ""; 17429d412a43SAl Viro 17439d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 17449d412a43SAl Viro err = -ENOMEM; 17459d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 17469d412a43SAl Viro goto err; 17479d412a43SAl Viro return mnt; 17489d412a43SAl Viro 17499d412a43SAl Viro err: 17509d412a43SAl Viro mntput(mnt); 17519d412a43SAl Viro return ERR_PTR(err); 17529d412a43SAl Viro } 17539d412a43SAl Viro 175479e801a9SAl Viro static struct vfsmount * 17559d412a43SAl Viro do_kern_mount(const char *fstype, int flags, const char *name, void *data) 17569d412a43SAl Viro { 17579d412a43SAl Viro struct file_system_type *type = get_fs_type(fstype); 17589d412a43SAl Viro struct vfsmount *mnt; 17599d412a43SAl Viro if (!type) 17609d412a43SAl Viro return ERR_PTR(-ENODEV); 17619d412a43SAl Viro mnt = vfs_kern_mount(type, flags, name, data); 17629d412a43SAl Viro if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 17639d412a43SAl Viro !mnt->mnt_sb->s_subtype) 17649d412a43SAl Viro mnt = fs_set_subtype(mnt, fstype); 17659d412a43SAl Viro put_filesystem(type); 17669d412a43SAl Viro return mnt; 17679d412a43SAl Viro } 17689d412a43SAl Viro 17699d412a43SAl Viro /* 17709d412a43SAl Viro * add a mount into a namespace's mount tree 17719d412a43SAl Viro */ 177295bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 17739d412a43SAl Viro { 17749d412a43SAl Viro int err; 17759d412a43SAl Viro 17769d412a43SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 17779d412a43SAl Viro 1778b12cea91SAl Viro err = lock_mount(path); 1779b12cea91SAl Viro if (err) 1780b12cea91SAl Viro return err; 17819d412a43SAl Viro 17829d412a43SAl Viro err = -EINVAL; 1783143c8c91SAl Viro if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt))) 17849d412a43SAl Viro goto unlock; 17859d412a43SAl Viro 17869d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 17879d412a43SAl Viro err = -EBUSY; 178895bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 17899d412a43SAl Viro path->mnt->mnt_root == path->dentry) 17909d412a43SAl Viro goto unlock; 17919d412a43SAl Viro 17929d412a43SAl Viro err = -EINVAL; 179395bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 17949d412a43SAl Viro goto unlock; 17959d412a43SAl Viro 179695bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 17979d412a43SAl Viro err = graft_tree(newmnt, path); 17989d412a43SAl Viro 17999d412a43SAl Viro unlock: 1800b12cea91SAl Viro unlock_mount(path); 18019d412a43SAl Viro return err; 18029d412a43SAl Viro } 1803b1e75df4SAl Viro 18041da177e4SLinus Torvalds /* 18051da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 18061da177e4SLinus Torvalds * namespace's tree 18071da177e4SLinus Torvalds */ 18080a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags, 18091da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 18101da177e4SLinus Torvalds { 18111da177e4SLinus Torvalds struct vfsmount *mnt; 181215f9a3f3SAl Viro int err; 18131da177e4SLinus Torvalds 1814eca6f534SVegard Nossum if (!type) 18151da177e4SLinus Torvalds return -EINVAL; 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds /* we need capabilities... */ 18181da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 18191da177e4SLinus Torvalds return -EPERM; 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 18221da177e4SLinus Torvalds if (IS_ERR(mnt)) 18231da177e4SLinus Torvalds return PTR_ERR(mnt); 18241da177e4SLinus Torvalds 182595bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 182615f9a3f3SAl Viro if (err) 182715f9a3f3SAl Viro mntput(mnt); 182815f9a3f3SAl Viro return err; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 183119a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 183219a167afSAl Viro { 18336776db3dSAl Viro struct mount *mnt = real_mount(m); 183419a167afSAl Viro int err; 183519a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 183619a167afSAl Viro * expired before we get a chance to add it 183719a167afSAl Viro */ 18386776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 183919a167afSAl Viro 184019a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 184119a167afSAl Viro m->mnt_root == path->dentry) { 1842b1e75df4SAl Viro err = -ELOOP; 1843b1e75df4SAl Viro goto fail; 184419a167afSAl Viro } 184519a167afSAl Viro 184695bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 1847b1e75df4SAl Viro if (!err) 1848b1e75df4SAl Viro return 0; 1849b1e75df4SAl Viro fail: 1850b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 18516776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 1852b1e75df4SAl Viro down_write(&namespace_sem); 1853b1e75df4SAl Viro br_write_lock(vfsmount_lock); 18546776db3dSAl Viro list_del_init(&mnt->mnt_expire); 1855b1e75df4SAl Viro br_write_unlock(vfsmount_lock); 1856b1e75df4SAl Viro up_write(&namespace_sem); 185719a167afSAl Viro } 1858b1e75df4SAl Viro mntput(m); 1859b1e75df4SAl Viro mntput(m); 186019a167afSAl Viro return err; 186119a167afSAl Viro } 186219a167afSAl Viro 1863ea5b778aSDavid Howells /** 1864ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 1865ea5b778aSDavid Howells * @mnt: The mount to list. 1866ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 1867ea5b778aSDavid Howells */ 1868ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 1869ea5b778aSDavid Howells { 1870ea5b778aSDavid Howells down_write(&namespace_sem); 1871ea5b778aSDavid Howells br_write_lock(vfsmount_lock); 1872ea5b778aSDavid Howells 18736776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 1874ea5b778aSDavid Howells 1875ea5b778aSDavid Howells br_write_unlock(vfsmount_lock); 1876ea5b778aSDavid Howells up_write(&namespace_sem); 1877ea5b778aSDavid Howells } 1878ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 1879ea5b778aSDavid Howells 1880ea5b778aSDavid Howells /* 18811da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 18821da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 18831da177e4SLinus Torvalds * here 18841da177e4SLinus Torvalds */ 18851da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 18861da177e4SLinus Torvalds { 1887761d5c38SAl Viro struct mount *mnt, *next; 18881da177e4SLinus Torvalds LIST_HEAD(graveyard); 1889bcc5c7d2SAl Viro LIST_HEAD(umounts); 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds if (list_empty(mounts)) 18921da177e4SLinus Torvalds return; 18931da177e4SLinus Torvalds 1894bcc5c7d2SAl Viro down_write(&namespace_sem); 189599b7db7bSNick Piggin br_write_lock(vfsmount_lock); 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 18981da177e4SLinus Torvalds * following criteria: 18991da177e4SLinus Torvalds * - only referenced by its parent vfsmount 19001da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 19011da177e4SLinus Torvalds * cleared by mntput()) 19021da177e4SLinus Torvalds */ 19036776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 1904863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 19051ab59738SAl Viro propagate_mount_busy(mnt, 1)) 19061da177e4SLinus Torvalds continue; 19076776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 19081da177e4SLinus Torvalds } 1909bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 19106776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 1911143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1912bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 1913bcc5c7d2SAl Viro } 191499b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1915bcc5c7d2SAl Viro up_write(&namespace_sem); 1916bcc5c7d2SAl Viro 1917bcc5c7d2SAl Viro release_mounts(&umounts); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds /* 19235528f911STrond Myklebust * Ripoff of 'select_parent()' 19245528f911STrond Myklebust * 19255528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 19265528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 19275528f911STrond Myklebust */ 1928692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 19295528f911STrond Myklebust { 1930692afc31SAl Viro struct mount *this_parent = parent; 19315528f911STrond Myklebust struct list_head *next; 19325528f911STrond Myklebust int found = 0; 19335528f911STrond Myklebust 19345528f911STrond Myklebust repeat: 19356b41d536SAl Viro next = this_parent->mnt_mounts.next; 19365528f911STrond Myklebust resume: 19376b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 19385528f911STrond Myklebust struct list_head *tmp = next; 19396b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 19405528f911STrond Myklebust 19415528f911STrond Myklebust next = tmp->next; 1942692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 19435528f911STrond Myklebust continue; 19445528f911STrond Myklebust /* 19455528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 19465528f911STrond Myklebust */ 19476b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 19485528f911STrond Myklebust this_parent = mnt; 19495528f911STrond Myklebust goto repeat; 19505528f911STrond Myklebust } 19515528f911STrond Myklebust 19521ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 19536776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 19545528f911STrond Myklebust found++; 19555528f911STrond Myklebust } 19565528f911STrond Myklebust } 19575528f911STrond Myklebust /* 19585528f911STrond Myklebust * All done at this level ... ascend and resume the search 19595528f911STrond Myklebust */ 19605528f911STrond Myklebust if (this_parent != parent) { 19616b41d536SAl Viro next = this_parent->mnt_child.next; 19620714a533SAl Viro this_parent = this_parent->mnt_parent; 19635528f911STrond Myklebust goto resume; 19645528f911STrond Myklebust } 19655528f911STrond Myklebust return found; 19665528f911STrond Myklebust } 19675528f911STrond Myklebust 19685528f911STrond Myklebust /* 19695528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 19705528f911STrond Myklebust * submounts of a specific parent mountpoint 197199b7db7bSNick Piggin * 197299b7db7bSNick Piggin * vfsmount_lock must be held for write 19735528f911STrond Myklebust */ 1974692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts) 19755528f911STrond Myklebust { 19765528f911STrond Myklebust LIST_HEAD(graveyard); 1977761d5c38SAl Viro struct mount *m; 19785528f911STrond Myklebust 19795528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 1980c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 1981bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 1982761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 19836776db3dSAl Viro mnt_expire); 1984143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 1985afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 1986bcc5c7d2SAl Viro } 1987bcc5c7d2SAl Viro } 19885528f911STrond Myklebust } 19895528f911STrond Myklebust 19905528f911STrond Myklebust /* 19911da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 19921da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 19931da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 19941da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 19951da177e4SLinus Torvalds */ 1996b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 1997b58fed8bSRam Pai unsigned long n) 19981da177e4SLinus Torvalds { 19991da177e4SLinus Torvalds char *t = to; 20001da177e4SLinus Torvalds const char __user *f = from; 20011da177e4SLinus Torvalds char c; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 20041da177e4SLinus Torvalds return n; 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds while (n) { 20071da177e4SLinus Torvalds if (__get_user(c, f)) { 20081da177e4SLinus Torvalds memset(t, 0, n); 20091da177e4SLinus Torvalds break; 20101da177e4SLinus Torvalds } 20111da177e4SLinus Torvalds *t++ = c; 20121da177e4SLinus Torvalds f++; 20131da177e4SLinus Torvalds n--; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds return n; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 20191da177e4SLinus Torvalds { 20201da177e4SLinus Torvalds int i; 20211da177e4SLinus Torvalds unsigned long page; 20221da177e4SLinus Torvalds unsigned long size; 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds *where = 0; 20251da177e4SLinus Torvalds if (!data) 20261da177e4SLinus Torvalds return 0; 20271da177e4SLinus Torvalds 20281da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 20291da177e4SLinus Torvalds return -ENOMEM; 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds /* We only care that *some* data at the address the user 20321da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 20331da177e4SLinus Torvalds * the remainder of the page. 20341da177e4SLinus Torvalds */ 20351da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 20361da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 20371da177e4SLinus Torvalds if (size > PAGE_SIZE) 20381da177e4SLinus Torvalds size = PAGE_SIZE; 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 20411da177e4SLinus Torvalds if (!i) { 20421da177e4SLinus Torvalds free_page(page); 20431da177e4SLinus Torvalds return -EFAULT; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds if (i != PAGE_SIZE) 20461da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 20471da177e4SLinus Torvalds *where = page; 20481da177e4SLinus Torvalds return 0; 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 2051eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2052eca6f534SVegard Nossum { 2053eca6f534SVegard Nossum char *tmp; 2054eca6f534SVegard Nossum 2055eca6f534SVegard Nossum if (!data) { 2056eca6f534SVegard Nossum *where = NULL; 2057eca6f534SVegard Nossum return 0; 2058eca6f534SVegard Nossum } 2059eca6f534SVegard Nossum 2060eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2061eca6f534SVegard Nossum if (IS_ERR(tmp)) 2062eca6f534SVegard Nossum return PTR_ERR(tmp); 2063eca6f534SVegard Nossum 2064eca6f534SVegard Nossum *where = tmp; 2065eca6f534SVegard Nossum return 0; 2066eca6f534SVegard Nossum } 2067eca6f534SVegard Nossum 20681da177e4SLinus Torvalds /* 20691da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 20701da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 20711da177e4SLinus Torvalds * 20721da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 20731da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 20741da177e4SLinus Torvalds * information (or be NULL). 20751da177e4SLinus Torvalds * 20761da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 20771da177e4SLinus Torvalds * When the flags word was introduced its top half was required 20781da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 20791da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 20801da177e4SLinus Torvalds * and must be discarded. 20811da177e4SLinus Torvalds */ 20821da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 20831da177e4SLinus Torvalds unsigned long flags, void *data_page) 20841da177e4SLinus Torvalds { 20852d92ab3cSAl Viro struct path path; 20861da177e4SLinus Torvalds int retval = 0; 20871da177e4SLinus Torvalds int mnt_flags = 0; 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds /* Discard magic */ 20901da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 20911da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds /* Basic sanity checks */ 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 20961da177e4SLinus Torvalds return -EINVAL; 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds if (data_page) 20991da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 21001da177e4SLinus Torvalds 2101a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2102a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2103a27ab9f2STetsuo Handa if (retval) 2104a27ab9f2STetsuo Handa return retval; 2105a27ab9f2STetsuo Handa 2106a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2107a27ab9f2STetsuo Handa type_page, flags, data_page); 2108a27ab9f2STetsuo Handa if (retval) 2109a27ab9f2STetsuo Handa goto dput_out; 2110a27ab9f2STetsuo Handa 2111613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2112613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 21130a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 21140a1c01c9SMatthew Garrett 21151da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 21161da177e4SLinus Torvalds if (flags & MS_NOSUID) 21171da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 21181da177e4SLinus Torvalds if (flags & MS_NODEV) 21191da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 21201da177e4SLinus Torvalds if (flags & MS_NOEXEC) 21211da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2122fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2123fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2124fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2125fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2126d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2127d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 21282e4b7fcdSDave Hansen if (flags & MS_RDONLY) 21292e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2130fc33a7bbSChristoph Hellwig 21317a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2132d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2133d0adde57SMatthew Garrett MS_STRICTATIME); 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds if (flags & MS_REMOUNT) 21362d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 21371da177e4SLinus Torvalds data_page); 21381da177e4SLinus Torvalds else if (flags & MS_BIND) 21392d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 21409676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21412d92ab3cSAl Viro retval = do_change_type(&path, flags); 21421da177e4SLinus Torvalds else if (flags & MS_MOVE) 21432d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 21441da177e4SLinus Torvalds else 21452d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 21461da177e4SLinus Torvalds dev_name, data_page); 21471da177e4SLinus Torvalds dput_out: 21482d92ab3cSAl Viro path_put(&path); 21491da177e4SLinus Torvalds return retval; 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 2152cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void) 2153cf8d2c11STrond Myklebust { 2154cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2155cf8d2c11STrond Myklebust 2156cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2157cf8d2c11STrond Myklebust if (!new_ns) 2158cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2159cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2160cf8d2c11STrond Myklebust new_ns->root = NULL; 2161cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2162cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2163cf8d2c11STrond Myklebust new_ns->event = 0; 2164cf8d2c11STrond Myklebust return new_ns; 2165cf8d2c11STrond Myklebust } 2166cf8d2c11STrond Myklebust 2167f03c6599SAl Viro void mnt_make_longterm(struct vfsmount *mnt) 2168f03c6599SAl Viro { 216983adc753SAl Viro __mnt_make_longterm(real_mount(mnt)); 2170f03c6599SAl Viro } 2171f03c6599SAl Viro 217283adc753SAl Viro void mnt_make_shortterm(struct vfsmount *m) 2173f03c6599SAl Viro { 21747e3d0eb0SAl Viro #ifdef CONFIG_SMP 217583adc753SAl Viro struct mount *mnt = real_mount(m); 217668e8a9feSAl Viro if (atomic_add_unless(&mnt->mnt_longterm, -1, 1)) 2177f03c6599SAl Viro return; 2178f03c6599SAl Viro br_write_lock(vfsmount_lock); 217968e8a9feSAl Viro atomic_dec(&mnt->mnt_longterm); 2180f03c6599SAl Viro br_write_unlock(vfsmount_lock); 21817e3d0eb0SAl Viro #endif 2182f03c6599SAl Viro } 2183f03c6599SAl Viro 2184741a2951SJANAK DESAI /* 2185741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2186741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2187741a2951SJANAK DESAI */ 2188e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 21896b3286edSKirill Korotaev struct fs_struct *fs) 21901da177e4SLinus Torvalds { 21916b3286edSKirill Korotaev struct mnt_namespace *new_ns; 21927f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2193315fc83eSAl Viro struct mount *p, *q; 2194be08d6d2SAl Viro struct mount *old = mnt_ns->root; 2195cb338d06SAl Viro struct mount *new; 21961da177e4SLinus Torvalds 2197cf8d2c11STrond Myklebust new_ns = alloc_mnt_ns(); 2198cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2199cf8d2c11STrond Myklebust return new_ns; 22001da177e4SLinus Torvalds 2201390c6843SRam Pai down_write(&namespace_sem); 22021da177e4SLinus Torvalds /* First pass: copy the tree topology */ 2203909b0a88SAl Viro new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE); 2204cb338d06SAl Viro if (!new) { 2205390c6843SRam Pai up_write(&namespace_sem); 22061da177e4SLinus Torvalds kfree(new_ns); 22075cc4a034SJulia Lawall return ERR_PTR(-ENOMEM); 22081da177e4SLinus Torvalds } 2209be08d6d2SAl Viro new_ns->root = new; 221099b7db7bSNick Piggin br_write_lock(vfsmount_lock); 22111a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 221299b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds /* 22151da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 22161da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 22171da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 22181da177e4SLinus Torvalds */ 2219909b0a88SAl Viro p = old; 2220cb338d06SAl Viro q = new; 22211da177e4SLinus Torvalds while (p) { 2222143c8c91SAl Viro q->mnt_ns = new_ns; 222383adc753SAl Viro __mnt_make_longterm(q); 22241da177e4SLinus Torvalds if (fs) { 2225315fc83eSAl Viro if (&p->mnt == fs->root.mnt) { 2226315fc83eSAl Viro fs->root.mnt = mntget(&q->mnt); 222783adc753SAl Viro __mnt_make_longterm(q); 2228315fc83eSAl Viro mnt_make_shortterm(&p->mnt); 2229315fc83eSAl Viro rootmnt = &p->mnt; 22301da177e4SLinus Torvalds } 2231315fc83eSAl Viro if (&p->mnt == fs->pwd.mnt) { 2232315fc83eSAl Viro fs->pwd.mnt = mntget(&q->mnt); 223383adc753SAl Viro __mnt_make_longterm(q); 2234315fc83eSAl Viro mnt_make_shortterm(&p->mnt); 2235315fc83eSAl Viro pwdmnt = &p->mnt; 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds } 2238909b0a88SAl Viro p = next_mnt(p, old); 2239909b0a88SAl Viro q = next_mnt(q, new); 22401da177e4SLinus Torvalds } 2241390c6843SRam Pai up_write(&namespace_sem); 22421da177e4SLinus Torvalds 22431da177e4SLinus Torvalds if (rootmnt) 2244f03c6599SAl Viro mntput(rootmnt); 22451da177e4SLinus Torvalds if (pwdmnt) 2246f03c6599SAl Viro mntput(pwdmnt); 22471da177e4SLinus Torvalds 2248741a2951SJANAK DESAI return new_ns; 2249741a2951SJANAK DESAI } 2250741a2951SJANAK DESAI 2251213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2252e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 2253741a2951SJANAK DESAI { 22546b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2255741a2951SJANAK DESAI 2256e3222c4eSBadari Pulavarty BUG_ON(!ns); 22576b3286edSKirill Korotaev get_mnt_ns(ns); 2258741a2951SJANAK DESAI 2259741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2260e3222c4eSBadari Pulavarty return ns; 2261741a2951SJANAK DESAI 2262e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 2263741a2951SJANAK DESAI 22646b3286edSKirill Korotaev put_mnt_ns(ns); 2265e3222c4eSBadari Pulavarty return new_ns; 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds 2268cf8d2c11STrond Myklebust /** 2269cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2270cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2271cf8d2c11STrond Myklebust */ 22721a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2273cf8d2c11STrond Myklebust { 22741a4eeaf2SAl Viro struct mnt_namespace *new_ns = alloc_mnt_ns(); 2275cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 22761a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 22771a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 22781a4eeaf2SAl Viro __mnt_make_longterm(mnt); 2279be08d6d2SAl Viro new_ns->root = mnt; 22801a4eeaf2SAl Viro list_add(&new_ns->list, &mnt->mnt_list); 2281c1334495SAl Viro } else { 22821a4eeaf2SAl Viro mntput(m); 2283cf8d2c11STrond Myklebust } 2284cf8d2c11STrond Myklebust return new_ns; 2285cf8d2c11STrond Myklebust } 2286cf8d2c11STrond Myklebust 2287ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2288ea441d11SAl Viro { 2289ea441d11SAl Viro struct mnt_namespace *ns; 2290d31da0f0SAl Viro struct super_block *s; 2291ea441d11SAl Viro struct path path; 2292ea441d11SAl Viro int err; 2293ea441d11SAl Viro 2294ea441d11SAl Viro ns = create_mnt_ns(mnt); 2295ea441d11SAl Viro if (IS_ERR(ns)) 2296ea441d11SAl Viro return ERR_CAST(ns); 2297ea441d11SAl Viro 2298ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2299ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2300ea441d11SAl Viro 2301ea441d11SAl Viro put_mnt_ns(ns); 2302ea441d11SAl Viro 2303ea441d11SAl Viro if (err) 2304ea441d11SAl Viro return ERR_PTR(err); 2305ea441d11SAl Viro 2306ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2307d31da0f0SAl Viro s = path.mnt->mnt_sb; 2308d31da0f0SAl Viro atomic_inc(&s->s_active); 2309ea441d11SAl Viro mntput(path.mnt); 2310ea441d11SAl Viro /* lock the sucker */ 2311d31da0f0SAl Viro down_write(&s->s_umount); 2312ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2313ea441d11SAl Viro return path.dentry; 2314ea441d11SAl Viro } 2315ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2316ea441d11SAl Viro 2317bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2318bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 23191da177e4SLinus Torvalds { 2320eca6f534SVegard Nossum int ret; 2321eca6f534SVegard Nossum char *kernel_type; 2322eca6f534SVegard Nossum char *kernel_dir; 2323eca6f534SVegard Nossum char *kernel_dev; 23241da177e4SLinus Torvalds unsigned long data_page; 23251da177e4SLinus Torvalds 2326eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2327eca6f534SVegard Nossum if (ret < 0) 2328eca6f534SVegard Nossum goto out_type; 23291da177e4SLinus Torvalds 2330eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2331eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2332eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2333eca6f534SVegard Nossum goto out_dir; 2334eca6f534SVegard Nossum } 23351da177e4SLinus Torvalds 2336eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2337eca6f534SVegard Nossum if (ret < 0) 2338eca6f534SVegard Nossum goto out_dev; 23391da177e4SLinus Torvalds 2340eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2341eca6f534SVegard Nossum if (ret < 0) 2342eca6f534SVegard Nossum goto out_data; 23431da177e4SLinus Torvalds 2344eca6f534SVegard Nossum ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags, 2345eca6f534SVegard Nossum (void *) data_page); 2346eca6f534SVegard Nossum 23471da177e4SLinus Torvalds free_page(data_page); 2348eca6f534SVegard Nossum out_data: 2349eca6f534SVegard Nossum kfree(kernel_dev); 2350eca6f534SVegard Nossum out_dev: 2351eca6f534SVegard Nossum putname(kernel_dir); 2352eca6f534SVegard Nossum out_dir: 2353eca6f534SVegard Nossum kfree(kernel_type); 2354eca6f534SVegard Nossum out_type: 2355eca6f534SVegard Nossum return ret; 23561da177e4SLinus Torvalds } 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds /* 2359afac7cbaSAl Viro * Return true if path is reachable from root 2360afac7cbaSAl Viro * 2361afac7cbaSAl Viro * namespace_sem or vfsmount_lock is held 2362afac7cbaSAl Viro */ 2363643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2364afac7cbaSAl Viro const struct path *root) 2365afac7cbaSAl Viro { 2366643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2367a73324daSAl Viro dentry = mnt->mnt_mountpoint; 23680714a533SAl Viro mnt = mnt->mnt_parent; 2369afac7cbaSAl Viro } 2370643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2371afac7cbaSAl Viro } 2372afac7cbaSAl Viro 2373afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2374afac7cbaSAl Viro { 2375afac7cbaSAl Viro int res; 2376afac7cbaSAl Viro br_read_lock(vfsmount_lock); 2377643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 2378afac7cbaSAl Viro br_read_unlock(vfsmount_lock); 2379afac7cbaSAl Viro return res; 2380afac7cbaSAl Viro } 2381afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2382afac7cbaSAl Viro 2383afac7cbaSAl Viro /* 23841da177e4SLinus Torvalds * pivot_root Semantics: 23851da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 23861da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 23871da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 23881da177e4SLinus Torvalds * 23891da177e4SLinus Torvalds * Restrictions: 23901da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 23911da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 23921da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 23931da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 23941da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 23951da177e4SLinus Torvalds * 23964a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 23974a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 23984a0d11faSNeil Brown * in this situation. 23994a0d11faSNeil Brown * 24001da177e4SLinus Torvalds * Notes: 24011da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 24021da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 24031da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 24041da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 24051da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 24061da177e4SLinus Torvalds * first. 24071da177e4SLinus Torvalds */ 24083480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 24093480b257SHeiko Carstens const char __user *, put_old) 24101da177e4SLinus Torvalds { 24112d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 2412419148daSAl Viro struct mount *new_mnt, *root_mnt; 24131da177e4SLinus Torvalds int error; 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 24161da177e4SLinus Torvalds return -EPERM; 24171da177e4SLinus Torvalds 24182d8f3038SAl Viro error = user_path_dir(new_root, &new); 24191da177e4SLinus Torvalds if (error) 24201da177e4SLinus Torvalds goto out0; 24211da177e4SLinus Torvalds 24222d8f3038SAl Viro error = user_path_dir(put_old, &old); 24231da177e4SLinus Torvalds if (error) 24241da177e4SLinus Torvalds goto out1; 24251da177e4SLinus Torvalds 24262d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2427b12cea91SAl Viro if (error) 2428b12cea91SAl Viro goto out2; 24291da177e4SLinus Torvalds 2430f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 2431b12cea91SAl Viro error = lock_mount(&old); 2432b12cea91SAl Viro if (error) 2433b12cea91SAl Viro goto out3; 2434b12cea91SAl Viro 24351da177e4SLinus Torvalds error = -EINVAL; 2436419148daSAl Viro new_mnt = real_mount(new.mnt); 2437419148daSAl Viro root_mnt = real_mount(root.mnt); 2438fc7be130SAl Viro if (IS_MNT_SHARED(real_mount(old.mnt)) || 2439fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2440fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2441b12cea91SAl Viro goto out4; 2442143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2443b12cea91SAl Viro goto out4; 24441da177e4SLinus Torvalds error = -ENOENT; 2445f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2446b12cea91SAl Viro goto out4; 2447f3da392eSAlexey Dobriyan if (d_unlinked(old.dentry)) 2448b12cea91SAl Viro goto out4; 24491da177e4SLinus Torvalds error = -EBUSY; 24502d8f3038SAl Viro if (new.mnt == root.mnt || 24512d8f3038SAl Viro old.mnt == root.mnt) 2452b12cea91SAl Viro goto out4; /* loop, on the same file system */ 24531da177e4SLinus Torvalds error = -EINVAL; 24548c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2455b12cea91SAl Viro goto out4; /* not a mountpoint */ 2456676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2457b12cea91SAl Viro goto out4; /* not attached */ 24582d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2459b12cea91SAl Viro goto out4; /* not a mountpoint */ 2460676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2461b12cea91SAl Viro goto out4; /* not attached */ 24624ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 2463643822b4SAl Viro if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new)) 2464b12cea91SAl Viro goto out4; 246527cb1572SAl Viro br_write_lock(vfsmount_lock); 2466419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2467419148daSAl Viro detach_mnt(root_mnt, &root_parent); 24684ac91378SJan Blunck /* mount old root on put_old */ 2469419148daSAl Viro attach_mnt(root_mnt, &old); 24704ac91378SJan Blunck /* mount new_root on / */ 2471419148daSAl Viro attach_mnt(new_mnt, &root_parent); 24726b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 247399b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 24742d8f3038SAl Viro chroot_fs_refs(&root, &new); 24751da177e4SLinus Torvalds error = 0; 2476b12cea91SAl Viro out4: 2477b12cea91SAl Viro unlock_mount(&old); 2478b12cea91SAl Viro if (!error) { 24791a390689SAl Viro path_put(&root_parent); 24801a390689SAl Viro path_put(&parent_path); 2481b12cea91SAl Viro } 2482b12cea91SAl Viro out3: 24838c3ee42eSAl Viro path_put(&root); 2484b12cea91SAl Viro out2: 24852d8f3038SAl Viro path_put(&old); 24861da177e4SLinus Torvalds out1: 24872d8f3038SAl Viro path_put(&new); 24881da177e4SLinus Torvalds out0: 24891da177e4SLinus Torvalds return error; 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds static void __init init_mount_tree(void) 24931da177e4SLinus Torvalds { 24941da177e4SLinus Torvalds struct vfsmount *mnt; 24956b3286edSKirill Korotaev struct mnt_namespace *ns; 2496ac748a09SJan Blunck struct path root; 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 24991da177e4SLinus Torvalds if (IS_ERR(mnt)) 25001da177e4SLinus Torvalds panic("Can't create rootfs"); 2501b3e19d92SNick Piggin 25023b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 25033b22edc5STrond Myklebust if (IS_ERR(ns)) 25041da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 25051da177e4SLinus Torvalds 25066b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 25076b3286edSKirill Korotaev get_mnt_ns(ns); 25081da177e4SLinus Torvalds 2509be08d6d2SAl Viro root.mnt = mnt; 2510be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2511ac748a09SJan Blunck 2512ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2513ac748a09SJan Blunck set_fs_root(current->fs, &root); 25141da177e4SLinus Torvalds } 25151da177e4SLinus Torvalds 251674bf17cfSDenis Cheng void __init mnt_init(void) 25171da177e4SLinus Torvalds { 251813f14b4dSEric Dumazet unsigned u; 251915a67dd8SRandy Dunlap int err; 25201da177e4SLinus Torvalds 2521390c6843SRam Pai init_rwsem(&namespace_sem); 2522390c6843SRam Pai 25237d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 252420c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 25251da177e4SLinus Torvalds 2526b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds if (!mount_hashtable) 25291da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 25301da177e4SLinus Torvalds 253180cdc6daSMandeep Singh Baines printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE); 25321da177e4SLinus Torvalds 253313f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 253413f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 25351da177e4SLinus Torvalds 253699b7db7bSNick Piggin br_lock_init(vfsmount_lock); 253799b7db7bSNick Piggin 253815a67dd8SRandy Dunlap err = sysfs_init(); 253915a67dd8SRandy Dunlap if (err) 254015a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 25418e24eea7SHarvey Harrison __func__, err); 254200d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 254300d26666SGreg Kroah-Hartman if (!fs_kobj) 25448e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 25451da177e4SLinus Torvalds init_rootfs(); 25461da177e4SLinus Torvalds init_mount_tree(); 25471da177e4SLinus Torvalds } 25481da177e4SLinus Torvalds 2549616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 25501da177e4SLinus Torvalds { 255170fbcdf4SRam Pai LIST_HEAD(umount_list); 2552616511d0STrond Myklebust 2553d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2554616511d0STrond Myklebust return; 2555390c6843SRam Pai down_write(&namespace_sem); 255699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 2557be08d6d2SAl Viro umount_tree(ns->root, 0, &umount_list); 255899b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 2559390c6843SRam Pai up_write(&namespace_sem); 256070fbcdf4SRam Pai release_mounts(&umount_list); 25616b3286edSKirill Korotaev kfree(ns); 25621da177e4SLinus Torvalds } 25639d412a43SAl Viro 25649d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 25659d412a43SAl Viro { 2566423e0ab0STim Chen struct vfsmount *mnt; 2567423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2568423e0ab0STim Chen if (!IS_ERR(mnt)) { 2569423e0ab0STim Chen /* 2570423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2571423e0ab0STim Chen * we unmount before file sys is unregistered 2572423e0ab0STim Chen */ 2573423e0ab0STim Chen mnt_make_longterm(mnt); 2574423e0ab0STim Chen } 2575423e0ab0STim Chen return mnt; 25769d412a43SAl Viro } 25779d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2578423e0ab0STim Chen 2579423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2580423e0ab0STim Chen { 2581423e0ab0STim Chen /* release long term mount so mount point can be released */ 2582423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2583423e0ab0STim Chen mnt_make_shortterm(mnt); 2584423e0ab0STim Chen mntput(mnt); 2585423e0ab0STim Chen } 2586423e0ab0STim Chen } 2587423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 258802125a82SAl Viro 258902125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 259002125a82SAl Viro { 2591143c8c91SAl Viro return check_mnt(real_mount(mnt)); 259202125a82SAl Viro } 2593