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 2764ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2774ed5e82fSMiklos Szeredi { 2784ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 2794ed5e82fSMiklos Szeredi return 1; 2804ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 2814ed5e82fSMiklos Szeredi smp_rmb(); 2824ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 2834ed5e82fSMiklos Szeredi } 2844ed5e82fSMiklos Szeredi 2853d733633SDave Hansen /* 2863d733633SDave Hansen * Most r/o checks on a fs are for operations that take 2873d733633SDave Hansen * discrete amounts of time, like a write() or unlink(). 2883d733633SDave Hansen * We must keep track of when those operations start 2893d733633SDave Hansen * (for permission checks) and when they end, so that 2903d733633SDave Hansen * we can determine when writes are able to occur to 2913d733633SDave Hansen * a filesystem. 2923d733633SDave Hansen */ 2938366025eSDave Hansen /** 2948366025eSDave Hansen * mnt_want_write - get write access to a mount 29583adc753SAl Viro * @m: the mount on which to take a write 2968366025eSDave Hansen * 2978366025eSDave Hansen * This tells the low-level filesystem that a write is 2988366025eSDave Hansen * about to be performed to it, and makes sure that 2998366025eSDave Hansen * writes are allowed before returning success. When 3008366025eSDave Hansen * the write operation is finished, mnt_drop_write() 3018366025eSDave Hansen * must be called. This is effectively a refcount. 3028366025eSDave Hansen */ 30383adc753SAl Viro int mnt_want_write(struct vfsmount *m) 3048366025eSDave Hansen { 30583adc753SAl Viro struct mount *mnt = real_mount(m); 3063d733633SDave Hansen int ret = 0; 3073d733633SDave Hansen 308d3ef3d73Snpiggin@suse.de preempt_disable(); 309c6653a83SNick Piggin mnt_inc_writers(mnt); 310d3ef3d73Snpiggin@suse.de /* 311c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 312d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 313d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 314d3ef3d73Snpiggin@suse.de */ 315d3ef3d73Snpiggin@suse.de smp_mb(); 31683adc753SAl Viro while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 317d3ef3d73Snpiggin@suse.de cpu_relax(); 318d3ef3d73Snpiggin@suse.de /* 319d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 320d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 321d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 322d3ef3d73Snpiggin@suse.de */ 323d3ef3d73Snpiggin@suse.de smp_rmb(); 3244ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 325c6653a83SNick Piggin mnt_dec_writers(mnt); 3263d733633SDave Hansen ret = -EROFS; 3273d733633SDave Hansen } 328d3ef3d73Snpiggin@suse.de preempt_enable(); 3293d733633SDave Hansen return ret; 3308366025eSDave Hansen } 3318366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3328366025eSDave Hansen 3338366025eSDave Hansen /** 33496029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 33596029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 33696029c4eSnpiggin@suse.de * 33796029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 33896029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 33996029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 34096029c4eSnpiggin@suse.de * on it. This allows some optimisation. 34196029c4eSnpiggin@suse.de * 34296029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 34396029c4eSnpiggin@suse.de * drop the reference. 34496029c4eSnpiggin@suse.de */ 34596029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 34696029c4eSnpiggin@suse.de { 34796029c4eSnpiggin@suse.de /* superblock may be r/o */ 34896029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 34996029c4eSnpiggin@suse.de return -EROFS; 35096029c4eSnpiggin@suse.de preempt_disable(); 35183adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 35296029c4eSnpiggin@suse.de preempt_enable(); 35396029c4eSnpiggin@suse.de return 0; 35496029c4eSnpiggin@suse.de } 35596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 35696029c4eSnpiggin@suse.de 35796029c4eSnpiggin@suse.de /** 35896029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 35996029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 36096029c4eSnpiggin@suse.de * 36196029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 36296029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 36396029c4eSnpiggin@suse.de */ 36496029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 36596029c4eSnpiggin@suse.de { 3662d8dd38aSOGAWA Hirofumi struct inode *inode = file->f_dentry->d_inode; 3672d8dd38aSOGAWA Hirofumi if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 36896029c4eSnpiggin@suse.de return mnt_want_write(file->f_path.mnt); 36996029c4eSnpiggin@suse.de else 37096029c4eSnpiggin@suse.de return mnt_clone_write(file->f_path.mnt); 37196029c4eSnpiggin@suse.de } 37296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 37396029c4eSnpiggin@suse.de 37496029c4eSnpiggin@suse.de /** 3758366025eSDave Hansen * mnt_drop_write - give up write access to a mount 3768366025eSDave Hansen * @mnt: the mount on which to give up write access 3778366025eSDave Hansen * 3788366025eSDave Hansen * Tells the low-level filesystem that we are done 3798366025eSDave Hansen * performing writes to it. Must be matched with 3808366025eSDave Hansen * mnt_want_write() call above. 3818366025eSDave Hansen */ 3828366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt) 3838366025eSDave Hansen { 384d3ef3d73Snpiggin@suse.de preempt_disable(); 38583adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 386d3ef3d73Snpiggin@suse.de preempt_enable(); 3878366025eSDave Hansen } 3888366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 3898366025eSDave Hansen 3902a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 3912a79f17eSAl Viro { 3922a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 3932a79f17eSAl Viro } 3942a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 3952a79f17eSAl Viro 39683adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 3978366025eSDave Hansen { 3983d733633SDave Hansen int ret = 0; 3993d733633SDave Hansen 400962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 40183adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 402d3ef3d73Snpiggin@suse.de /* 403d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 404d3ef3d73Snpiggin@suse.de * should be visible before we do. 405d3ef3d73Snpiggin@suse.de */ 406d3ef3d73Snpiggin@suse.de smp_mb(); 407d3ef3d73Snpiggin@suse.de 408d3ef3d73Snpiggin@suse.de /* 409d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 410d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 411d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 412d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 413d3ef3d73Snpiggin@suse.de * 414d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 415d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 416d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 417d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 418d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 419d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 420d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 421d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 422d3ef3d73Snpiggin@suse.de * we're counting up here. 423d3ef3d73Snpiggin@suse.de */ 424c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 425d3ef3d73Snpiggin@suse.de ret = -EBUSY; 426d3ef3d73Snpiggin@suse.de else 42783adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 428d3ef3d73Snpiggin@suse.de /* 429d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 430d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 431d3ef3d73Snpiggin@suse.de */ 432d3ef3d73Snpiggin@suse.de smp_wmb(); 43383adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 434962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 4353d733633SDave Hansen return ret; 4363d733633SDave Hansen } 4378366025eSDave Hansen 43883adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 4392e4b7fcdSDave Hansen { 440962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 44183adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 442962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 4432e4b7fcdSDave Hansen } 4442e4b7fcdSDave Hansen 4454ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 4464ed5e82fSMiklos Szeredi { 4474ed5e82fSMiklos Szeredi struct mount *mnt; 4484ed5e82fSMiklos Szeredi int err = 0; 4494ed5e82fSMiklos Szeredi 4508e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 4518e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 4528e8b8796SMiklos Szeredi return -EBUSY; 4538e8b8796SMiklos Szeredi 454962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 4554ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 4564ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 4574ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 4584ed5e82fSMiklos Szeredi smp_mb(); 4594ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 4604ed5e82fSMiklos Szeredi err = -EBUSY; 4614ed5e82fSMiklos Szeredi break; 4624ed5e82fSMiklos Szeredi } 4634ed5e82fSMiklos Szeredi } 4644ed5e82fSMiklos Szeredi } 4658e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 4668e8b8796SMiklos Szeredi err = -EBUSY; 4678e8b8796SMiklos Szeredi 4684ed5e82fSMiklos Szeredi if (!err) { 4694ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 4704ed5e82fSMiklos Szeredi smp_wmb(); 4714ed5e82fSMiklos Szeredi } 4724ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 4734ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 4744ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 4754ed5e82fSMiklos Szeredi } 476962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 4774ed5e82fSMiklos Szeredi 4784ed5e82fSMiklos Szeredi return err; 4794ed5e82fSMiklos Szeredi } 4804ed5e82fSMiklos Szeredi 481b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 4821da177e4SLinus Torvalds { 48352ba1621SAl Viro kfree(mnt->mnt_devname); 48473cd49ecSMiklos Szeredi mnt_free_id(mnt); 485d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 48668e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 487d3ef3d73Snpiggin@suse.de #endif 488b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* 492a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 493a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 49499b7db7bSNick Piggin * vfsmount_lock must be held for read or write. 4951da177e4SLinus Torvalds */ 496c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 497a05964f3SRam Pai int dir) 4981da177e4SLinus Torvalds { 4991da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 5001da177e4SLinus Torvalds struct list_head *tmp = head; 501c7105365SAl Viro struct mount *p, *found = NULL; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds for (;;) { 504a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 5051da177e4SLinus Torvalds p = NULL; 5061da177e4SLinus Torvalds if (tmp == head) 5071da177e4SLinus Torvalds break; 5081b8e5564SAl Viro p = list_entry(tmp, struct mount, mnt_hash); 509a73324daSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) { 510a05964f3SRam Pai found = p; 5111da177e4SLinus Torvalds break; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds return found; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 517a05964f3SRam Pai /* 518a05964f3SRam Pai * lookup_mnt increments the ref count before returning 519a05964f3SRam Pai * the vfsmount struct. 520a05964f3SRam Pai */ 5211c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 522a05964f3SRam Pai { 523c7105365SAl Viro struct mount *child_mnt; 52499b7db7bSNick Piggin 525962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 526c7105365SAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry, 1); 527c7105365SAl Viro if (child_mnt) { 528c7105365SAl Viro mnt_add_count(child_mnt, 1); 529962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 530c7105365SAl Viro return &child_mnt->mnt; 531c7105365SAl Viro } else { 532962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 533c7105365SAl Viro return NULL; 534c7105365SAl Viro } 535a05964f3SRam Pai } 536a05964f3SRam Pai 537143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 5381da177e4SLinus Torvalds { 5396b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 54299b7db7bSNick Piggin /* 54399b7db7bSNick Piggin * vfsmount lock must be held for write 54499b7db7bSNick Piggin */ 5456b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 5465addc5ddSAl Viro { 5475addc5ddSAl Viro if (ns) { 5485addc5ddSAl Viro ns->event = ++event; 5495addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5505addc5ddSAl Viro } 5515addc5ddSAl Viro } 5525addc5ddSAl Viro 55399b7db7bSNick Piggin /* 55499b7db7bSNick Piggin * vfsmount lock must be held for write 55599b7db7bSNick Piggin */ 5566b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 5575addc5ddSAl Viro { 5585addc5ddSAl Viro if (ns && ns->event != event) { 5595addc5ddSAl Viro ns->event = event; 5605addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5615addc5ddSAl Viro } 5625addc5ddSAl Viro } 5635addc5ddSAl Viro 56499b7db7bSNick Piggin /* 5655f57cbccSNick Piggin * Clear dentry's mounted state if it has no remaining mounts. 5665f57cbccSNick Piggin * vfsmount_lock must be held for write. 5675f57cbccSNick Piggin */ 568aa0a4cf0SAl Viro static void dentry_reset_mounted(struct dentry *dentry) 5695f57cbccSNick Piggin { 5705f57cbccSNick Piggin unsigned u; 5715f57cbccSNick Piggin 5725f57cbccSNick Piggin for (u = 0; u < HASH_SIZE; u++) { 573d5e50f74SAl Viro struct mount *p; 5745f57cbccSNick Piggin 5751b8e5564SAl Viro list_for_each_entry(p, &mount_hashtable[u], mnt_hash) { 576a73324daSAl Viro if (p->mnt_mountpoint == dentry) 5775f57cbccSNick Piggin return; 5785f57cbccSNick Piggin } 5795f57cbccSNick Piggin } 5805f57cbccSNick Piggin spin_lock(&dentry->d_lock); 5815f57cbccSNick Piggin dentry->d_flags &= ~DCACHE_MOUNTED; 5825f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 5835f57cbccSNick Piggin } 5845f57cbccSNick Piggin 5855f57cbccSNick Piggin /* 58699b7db7bSNick Piggin * vfsmount lock must be held for write 58799b7db7bSNick Piggin */ 588419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 5891da177e4SLinus Torvalds { 590a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 5910714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 5920714a533SAl Viro mnt->mnt_parent = mnt; 593a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 5946b41d536SAl Viro list_del_init(&mnt->mnt_child); 5951b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 596aa0a4cf0SAl Viro dentry_reset_mounted(old_path->dentry); 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 59999b7db7bSNick Piggin /* 60099b7db7bSNick Piggin * vfsmount lock must be held for write 60199b7db7bSNick Piggin */ 60214cf1fa8SAl Viro void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry, 60344d964d6SAl Viro struct mount *child_mnt) 604b90fa9aeSRam Pai { 6053a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 606a73324daSAl Viro child_mnt->mnt_mountpoint = dget(dentry); 6073a2393d7SAl Viro child_mnt->mnt_parent = mnt; 6085f57cbccSNick Piggin spin_lock(&dentry->d_lock); 6095f57cbccSNick Piggin dentry->d_flags |= DCACHE_MOUNTED; 6105f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 611b90fa9aeSRam Pai } 612b90fa9aeSRam Pai 61399b7db7bSNick Piggin /* 61499b7db7bSNick Piggin * vfsmount lock must be held for write 61599b7db7bSNick Piggin */ 616419148daSAl Viro static void attach_mnt(struct mount *mnt, struct path *path) 6171da177e4SLinus Torvalds { 61814cf1fa8SAl Viro mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt); 6191b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 6201a390689SAl Viro hash(path->mnt, path->dentry)); 6216b41d536SAl Viro list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts); 622b90fa9aeSRam Pai } 623b90fa9aeSRam Pai 62483adc753SAl Viro static inline void __mnt_make_longterm(struct mount *mnt) 6257e3d0eb0SAl Viro { 6267e3d0eb0SAl Viro #ifdef CONFIG_SMP 62768e8a9feSAl Viro atomic_inc(&mnt->mnt_longterm); 6287e3d0eb0SAl Viro #endif 6297e3d0eb0SAl Viro } 6307e3d0eb0SAl Viro 6317e3d0eb0SAl Viro /* needs vfsmount lock for write */ 63283adc753SAl Viro static inline void __mnt_make_shortterm(struct mount *mnt) 6337e3d0eb0SAl Viro { 6347e3d0eb0SAl Viro #ifdef CONFIG_SMP 63568e8a9feSAl Viro atomic_dec(&mnt->mnt_longterm); 6367e3d0eb0SAl Viro #endif 6377e3d0eb0SAl Viro } 6387e3d0eb0SAl Viro 639b90fa9aeSRam Pai /* 64099b7db7bSNick Piggin * vfsmount lock must be held for write 641b90fa9aeSRam Pai */ 6424b2619a5SAl Viro static void commit_tree(struct mount *mnt) 643b90fa9aeSRam Pai { 6440714a533SAl Viro struct mount *parent = mnt->mnt_parent; 64583adc753SAl Viro struct mount *m; 646b90fa9aeSRam Pai LIST_HEAD(head); 647143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 648b90fa9aeSRam Pai 6490714a533SAl Viro BUG_ON(parent == mnt); 650b90fa9aeSRam Pai 6511a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 6521a4eeaf2SAl Viro list_for_each_entry(m, &head, mnt_list) { 653143c8c91SAl Viro m->mnt_ns = n; 6547e3d0eb0SAl Viro __mnt_make_longterm(m); 655f03c6599SAl Viro } 656f03c6599SAl Viro 657b90fa9aeSRam Pai list_splice(&head, n->list.prev); 658b90fa9aeSRam Pai 6591b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 660a73324daSAl Viro hash(&parent->mnt, mnt->mnt_mountpoint)); 6616b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 6626b3286edSKirill Korotaev touch_mnt_namespace(n); 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 665909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 6661da177e4SLinus Torvalds { 6676b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 6686b41d536SAl Viro if (next == &p->mnt_mounts) { 6691da177e4SLinus Torvalds while (1) { 670909b0a88SAl Viro if (p == root) 6711da177e4SLinus Torvalds return NULL; 6726b41d536SAl Viro next = p->mnt_child.next; 6736b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 6741da177e4SLinus Torvalds break; 6750714a533SAl Viro p = p->mnt_parent; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds } 6786b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds 681315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 6829676f0c6SRam Pai { 6836b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 6846b41d536SAl Viro while (prev != &p->mnt_mounts) { 6856b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 6866b41d536SAl Viro prev = p->mnt_mounts.prev; 6879676f0c6SRam Pai } 6889676f0c6SRam Pai return p; 6899676f0c6SRam Pai } 6909676f0c6SRam Pai 6919d412a43SAl Viro struct vfsmount * 6929d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 6939d412a43SAl Viro { 694b105e270SAl Viro struct mount *mnt; 6959d412a43SAl Viro struct dentry *root; 6969d412a43SAl Viro 6979d412a43SAl Viro if (!type) 6989d412a43SAl Viro return ERR_PTR(-ENODEV); 6999d412a43SAl Viro 7009d412a43SAl Viro mnt = alloc_vfsmnt(name); 7019d412a43SAl Viro if (!mnt) 7029d412a43SAl Viro return ERR_PTR(-ENOMEM); 7039d412a43SAl Viro 7049d412a43SAl Viro if (flags & MS_KERNMOUNT) 705b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 7069d412a43SAl Viro 7079d412a43SAl Viro root = mount_fs(type, flags, name, data); 7089d412a43SAl Viro if (IS_ERR(root)) { 7099d412a43SAl Viro free_vfsmnt(mnt); 7109d412a43SAl Viro return ERR_CAST(root); 7119d412a43SAl Viro } 7129d412a43SAl Viro 713b105e270SAl Viro mnt->mnt.mnt_root = root; 714b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 715a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7160714a533SAl Viro mnt->mnt_parent = mnt; 717962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 71839f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 719962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 720b105e270SAl Viro return &mnt->mnt; 7219d412a43SAl Viro } 7229d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 7239d412a43SAl Viro 72487129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 72536341f64SRam Pai int flag) 7261da177e4SLinus Torvalds { 72787129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 72852ba1621SAl Viro struct mount *mnt = alloc_vfsmnt(old->mnt_devname); 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds if (mnt) { 731719f5d7fSMiklos Szeredi if (flag & (CL_SLAVE | CL_PRIVATE)) 73215169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 733719f5d7fSMiklos Szeredi else 73415169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 735719f5d7fSMiklos Szeredi 73615169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 737b105e270SAl Viro int err = mnt_alloc_group_id(mnt); 738719f5d7fSMiklos Szeredi if (err) 739719f5d7fSMiklos Szeredi goto out_free; 740719f5d7fSMiklos Szeredi } 741719f5d7fSMiklos Szeredi 74287129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 7431da177e4SLinus Torvalds atomic_inc(&sb->s_active); 744b105e270SAl Viro mnt->mnt.mnt_sb = sb; 745b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 746a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7470714a533SAl Viro mnt->mnt_parent = mnt; 748962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 74939f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 750962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 751b90fa9aeSRam Pai 7525afe0022SRam Pai if (flag & CL_SLAVE) { 7536776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 75432301920SAl Viro mnt->mnt_master = old; 755fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 7568aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 757fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 7586776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 759d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 7606776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 761d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 7625afe0022SRam Pai } 763b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 7640f0afb1dSAl Viro set_mnt_shared(mnt); 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 7671da177e4SLinus Torvalds * as the original if that was on one */ 76836341f64SRam Pai if (flag & CL_EXPIRE) { 7696776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 7706776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 7711da177e4SLinus Torvalds } 77236341f64SRam Pai } 773cb338d06SAl Viro return mnt; 774719f5d7fSMiklos Szeredi 775719f5d7fSMiklos Szeredi out_free: 776719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 777719f5d7fSMiklos Szeredi return NULL; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 78083adc753SAl Viro static inline void mntfree(struct mount *mnt) 7811da177e4SLinus Torvalds { 78283adc753SAl Viro struct vfsmount *m = &mnt->mnt; 78383adc753SAl Viro struct super_block *sb = m->mnt_sb; 784b3e19d92SNick Piggin 7853d733633SDave Hansen /* 7863d733633SDave Hansen * This probably indicates that somebody messed 7873d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 7883d733633SDave Hansen * happens, the filesystem was probably unable 7893d733633SDave Hansen * to make r/w->r/o transitions. 7903d733633SDave Hansen */ 791d3ef3d73Snpiggin@suse.de /* 792b3e19d92SNick Piggin * The locking used to deal with mnt_count decrement provides barriers, 793b3e19d92SNick Piggin * so mnt_get_writers() below is safe. 794d3ef3d73Snpiggin@suse.de */ 795c6653a83SNick Piggin WARN_ON(mnt_get_writers(mnt)); 79683adc753SAl Viro fsnotify_vfsmount_delete(m); 79783adc753SAl Viro dput(m->mnt_root); 79883adc753SAl Viro free_vfsmnt(mnt); 7991da177e4SLinus Torvalds deactivate_super(sb); 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 802900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 8037b7b1aceSAl Viro { 804b3e19d92SNick Piggin put_again: 805f03c6599SAl Viro #ifdef CONFIG_SMP 806962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 80768e8a9feSAl Viro if (likely(atomic_read(&mnt->mnt_longterm))) { 808aa9c0e07SAl Viro mnt_add_count(mnt, -1); 809962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 81099b7db7bSNick Piggin return; 811b3e19d92SNick Piggin } 812962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 813b3e19d92SNick Piggin 814962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 815aa9c0e07SAl Viro mnt_add_count(mnt, -1); 816b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 817962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 81899b7db7bSNick Piggin return; 81999b7db7bSNick Piggin } 820b3e19d92SNick Piggin #else 821aa9c0e07SAl Viro mnt_add_count(mnt, -1); 822b3e19d92SNick Piggin if (likely(mnt_get_count(mnt))) 823b3e19d92SNick Piggin return; 824962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 825f03c6599SAl Viro #endif 826863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 827863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 828863d684fSAl Viro mnt->mnt_pinned = 0; 829962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 830900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 831b3e19d92SNick Piggin goto put_again; 832b3e19d92SNick Piggin } 833962830dfSAndi Kleen 83439f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 835962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 836b3e19d92SNick Piggin mntfree(mnt); 837b3e19d92SNick Piggin } 838b3e19d92SNick Piggin 839b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 840b3e19d92SNick Piggin { 841b3e19d92SNick Piggin if (mnt) { 842863d684fSAl Viro struct mount *m = real_mount(mnt); 843b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 844863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 845863d684fSAl Viro m->mnt_expiry_mark = 0; 846863d684fSAl Viro mntput_no_expire(m); 847b3e19d92SNick Piggin } 848b3e19d92SNick Piggin } 849b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 850b3e19d92SNick Piggin 851b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 852b3e19d92SNick Piggin { 853b3e19d92SNick Piggin if (mnt) 85483adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 855b3e19d92SNick Piggin return mnt; 856b3e19d92SNick Piggin } 857b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 858b3e19d92SNick Piggin 8597b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 8607b7b1aceSAl Viro { 861962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 862863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 863962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 8647b7b1aceSAl Viro } 8657b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 8667b7b1aceSAl Viro 867863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 8687b7b1aceSAl Viro { 869863d684fSAl Viro struct mount *mnt = real_mount(m); 870962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 8717b7b1aceSAl Viro if (mnt->mnt_pinned) { 872863d684fSAl Viro mnt_add_count(mnt, 1); 8737b7b1aceSAl Viro mnt->mnt_pinned--; 8747b7b1aceSAl Viro } 875962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 8767b7b1aceSAl Viro } 8777b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 8781da177e4SLinus Torvalds 879b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 880b3b304a2SMiklos Szeredi { 881b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 882b3b304a2SMiklos Szeredi } 883b3b304a2SMiklos Szeredi 884b3b304a2SMiklos Szeredi /* 885b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 886b3b304a2SMiklos Szeredi * implement more complex mount option showing. 887b3b304a2SMiklos Szeredi * 888b3b304a2SMiklos Szeredi * See also save_mount_options(). 889b3b304a2SMiklos Szeredi */ 89034c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 891b3b304a2SMiklos Szeredi { 8922a32cebdSAl Viro const char *options; 8932a32cebdSAl Viro 8942a32cebdSAl Viro rcu_read_lock(); 89534c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 896b3b304a2SMiklos Szeredi 897b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 898b3b304a2SMiklos Szeredi seq_putc(m, ','); 899b3b304a2SMiklos Szeredi mangle(m, options); 900b3b304a2SMiklos Szeredi } 9012a32cebdSAl Viro rcu_read_unlock(); 902b3b304a2SMiklos Szeredi 903b3b304a2SMiklos Szeredi return 0; 904b3b304a2SMiklos Szeredi } 905b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 906b3b304a2SMiklos Szeredi 907b3b304a2SMiklos Szeredi /* 908b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 909b3b304a2SMiklos Szeredi * called from the fill_super() callback. 910b3b304a2SMiklos Szeredi * 911b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 912b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 913b3b304a2SMiklos Szeredi * remount fails. 914b3b304a2SMiklos Szeredi * 915b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 916b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 917b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 918b3b304a2SMiklos Szeredi * any more. 919b3b304a2SMiklos Szeredi */ 920b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 921b3b304a2SMiklos Szeredi { 9222a32cebdSAl Viro BUG_ON(sb->s_options); 9232a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 924b3b304a2SMiklos Szeredi } 925b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 926b3b304a2SMiklos Szeredi 9272a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 9282a32cebdSAl Viro { 9292a32cebdSAl Viro char *old = sb->s_options; 9302a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 9312a32cebdSAl Viro if (old) { 9322a32cebdSAl Viro synchronize_rcu(); 9332a32cebdSAl Viro kfree(old); 9342a32cebdSAl Viro } 9352a32cebdSAl Viro } 9362a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 9372a32cebdSAl Viro 938a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 9390226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 9401da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 9411da177e4SLinus Torvalds { 9420226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 9431da177e4SLinus Torvalds 944390c6843SRam Pai down_read(&namespace_sem); 945a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 9491da177e4SLinus Torvalds { 9500226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 951b0765fb8SPavel Emelianov 952a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 9561da177e4SLinus Torvalds { 957390c6843SRam Pai up_read(&namespace_sem); 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 9600226f492SAl Viro static int m_show(struct seq_file *m, void *v) 9619f5596afSAl Viro { 9620226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 9631a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 9640226f492SAl Viro return p->show(m, &r->mnt); 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 967a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 9681da177e4SLinus Torvalds .start = m_start, 9691da177e4SLinus Torvalds .next = m_next, 9701da177e4SLinus Torvalds .stop = m_stop, 9710226f492SAl Viro .show = m_show, 972b4629fe2SChuck Lever }; 973a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 974b4629fe2SChuck Lever 9751da177e4SLinus Torvalds /** 9761da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 9771da177e4SLinus Torvalds * @mnt: root of mount tree 9781da177e4SLinus Torvalds * 9791da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 9801da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 9811da177e4SLinus Torvalds * busy. 9821da177e4SLinus Torvalds */ 983909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 9841da177e4SLinus Torvalds { 985909b0a88SAl Viro struct mount *mnt = real_mount(m); 98636341f64SRam Pai int actual_refs = 0; 98736341f64SRam Pai int minimum_refs = 0; 988315fc83eSAl Viro struct mount *p; 989909b0a88SAl Viro BUG_ON(!m); 9901da177e4SLinus Torvalds 991b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 992962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 993909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 99483adc753SAl Viro actual_refs += mnt_get_count(p); 9951da177e4SLinus Torvalds minimum_refs += 2; 9961da177e4SLinus Torvalds } 997962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds if (actual_refs > minimum_refs) 10001da177e4SLinus Torvalds return 0; 1001e3474a8eSIan Kent 1002e3474a8eSIan Kent return 1; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds /** 10081da177e4SLinus Torvalds * may_umount - check if a mount point is busy 10091da177e4SLinus Torvalds * @mnt: root of mount 10101da177e4SLinus Torvalds * 10111da177e4SLinus Torvalds * This is called to check if a mount point has any 10121da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 10131da177e4SLinus Torvalds * mount has sub mounts this will return busy 10141da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 10151da177e4SLinus Torvalds * 10161da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 10171da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 10181da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 10191da177e4SLinus Torvalds */ 10201da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 10211da177e4SLinus Torvalds { 1022e3474a8eSIan Kent int ret = 1; 10238ad08d8aSAl Viro down_read(&namespace_sem); 1024962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 10251ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1026e3474a8eSIan Kent ret = 0; 1027962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 10288ad08d8aSAl Viro up_read(&namespace_sem); 1029a05964f3SRam Pai return ret; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 10331da177e4SLinus Torvalds 1034b90fa9aeSRam Pai void release_mounts(struct list_head *head) 10351da177e4SLinus Torvalds { 1036d5e50f74SAl Viro struct mount *mnt; 103770fbcdf4SRam Pai while (!list_empty(head)) { 10381b8e5564SAl Viro mnt = list_first_entry(head, struct mount, mnt_hash); 10391b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 1040676da58dSAl Viro if (mnt_has_parent(mnt)) { 104170fbcdf4SRam Pai struct dentry *dentry; 1042863d684fSAl Viro struct mount *m; 104399b7db7bSNick Piggin 1044962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1045a73324daSAl Viro dentry = mnt->mnt_mountpoint; 1046863d684fSAl Viro m = mnt->mnt_parent; 1047a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10480714a533SAl Viro mnt->mnt_parent = mnt; 10497c4b93d8SAl Viro m->mnt_ghosts--; 1050962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 105170fbcdf4SRam Pai dput(dentry); 1052863d684fSAl Viro mntput(&m->mnt); 10531da177e4SLinus Torvalds } 1054d5e50f74SAl Viro mntput(&mnt->mnt); 105570fbcdf4SRam Pai } 105670fbcdf4SRam Pai } 105770fbcdf4SRam Pai 105899b7db7bSNick Piggin /* 105999b7db7bSNick Piggin * vfsmount lock must be held for write 106099b7db7bSNick Piggin * namespace_sem must be held for write 106199b7db7bSNick Piggin */ 1062761d5c38SAl Viro void umount_tree(struct mount *mnt, int propagate, struct list_head *kill) 106370fbcdf4SRam Pai { 10647b8a53fdSAl Viro LIST_HEAD(tmp_list); 1065315fc83eSAl Viro struct mount *p; 106670fbcdf4SRam Pai 1067909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) 10681b8e5564SAl Viro list_move(&p->mnt_hash, &tmp_list); 106970fbcdf4SRam Pai 1070a05964f3SRam Pai if (propagate) 10717b8a53fdSAl Viro propagate_umount(&tmp_list); 1072a05964f3SRam Pai 10731b8e5564SAl Viro list_for_each_entry(p, &tmp_list, mnt_hash) { 10746776db3dSAl Viro list_del_init(&p->mnt_expire); 10751a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1076143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 1077143c8c91SAl Viro p->mnt_ns = NULL; 107883adc753SAl Viro __mnt_make_shortterm(p); 10796b41d536SAl Viro list_del_init(&p->mnt_child); 1080676da58dSAl Viro if (mnt_has_parent(p)) { 1081863d684fSAl Viro p->mnt_parent->mnt_ghosts++; 1082a73324daSAl Viro dentry_reset_mounted(p->mnt_mountpoint); 10837c4b93d8SAl Viro } 10840f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 10851da177e4SLinus Torvalds } 10867b8a53fdSAl Viro list_splice(&tmp_list, kill); 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 1089692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts); 1090c35038beSAl Viro 10911ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 10921da177e4SLinus Torvalds { 10931ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 10941da177e4SLinus Torvalds int retval; 109570fbcdf4SRam Pai LIST_HEAD(umount_list); 10961da177e4SLinus Torvalds 10971ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 10981da177e4SLinus Torvalds if (retval) 10991da177e4SLinus Torvalds return retval; 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds /* 11021da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 11031da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 11041da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 11051da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 11061da177e4SLinus Torvalds */ 11071da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 11081ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 11091da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 11101da177e4SLinus Torvalds return -EINVAL; 11111da177e4SLinus Torvalds 1112b3e19d92SNick Piggin /* 1113b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1114b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1115b3e19d92SNick Piggin */ 1116962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 111783adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1118962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11191da177e4SLinus Torvalds return -EBUSY; 1120b3e19d92SNick Piggin } 1121962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11221da177e4SLinus Torvalds 1123863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 11241da177e4SLinus Torvalds return -EAGAIN; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds /* 11281da177e4SLinus Torvalds * If we may have to abort operations to get out of this 11291da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 11301da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 11311da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 11321da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 11331da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 11341da177e4SLinus Torvalds * about for the moment. 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds 113742faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 113842faad99SAl Viro sb->s_op->umount_begin(sb); 113942faad99SAl Viro } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds /* 11421da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 11431da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 11441da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 11451da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 11461da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 11471da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 11481da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 11491da177e4SLinus Torvalds */ 11501ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 11511da177e4SLinus Torvalds /* 11521da177e4SLinus Torvalds * Special case for "unmounting" root ... 11531da177e4SLinus Torvalds * we just try to remount it readonly. 11541da177e4SLinus Torvalds */ 11551da177e4SLinus Torvalds down_write(&sb->s_umount); 11564aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 11571da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 11581da177e4SLinus Torvalds up_write(&sb->s_umount); 11591da177e4SLinus Torvalds return retval; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds 1162390c6843SRam Pai down_write(&namespace_sem); 1163962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 11645addc5ddSAl Viro event++; 11651da177e4SLinus Torvalds 1166c35038beSAl Viro if (!(flags & MNT_DETACH)) 11671ab59738SAl Viro shrink_submounts(mnt, &umount_list); 1168c35038beSAl Viro 11691da177e4SLinus Torvalds retval = -EBUSY; 1170a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 11711a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 11721ab59738SAl Viro umount_tree(mnt, 1, &umount_list); 11731da177e4SLinus Torvalds retval = 0; 11741da177e4SLinus Torvalds } 1175962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1176390c6843SRam Pai up_write(&namespace_sem); 117770fbcdf4SRam Pai release_mounts(&umount_list); 11781da177e4SLinus Torvalds return retval; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds /* 11821da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 11831da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 11841da177e4SLinus Torvalds * 11851da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 11861da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 11871da177e4SLinus Torvalds */ 11881da177e4SLinus Torvalds 1189bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 11901da177e4SLinus Torvalds { 11912d8f3038SAl Viro struct path path; 1192900148dcSAl Viro struct mount *mnt; 11931da177e4SLinus Torvalds int retval; 1194db1f05bbSMiklos Szeredi int lookup_flags = 0; 11951da177e4SLinus Torvalds 1196db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1197db1f05bbSMiklos Szeredi return -EINVAL; 1198db1f05bbSMiklos Szeredi 1199db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1200db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1201db1f05bbSMiklos Szeredi 1202db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 12031da177e4SLinus Torvalds if (retval) 12041da177e4SLinus Torvalds goto out; 1205900148dcSAl Viro mnt = real_mount(path.mnt); 12061da177e4SLinus Torvalds retval = -EINVAL; 12072d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 12081da177e4SLinus Torvalds goto dput_and_out; 1209143c8c91SAl Viro if (!check_mnt(mnt)) 12101da177e4SLinus Torvalds goto dput_and_out; 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds retval = -EPERM; 12131da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 12141da177e4SLinus Torvalds goto dput_and_out; 12151da177e4SLinus Torvalds 1216900148dcSAl Viro retval = do_umount(mnt, flags); 12171da177e4SLinus Torvalds dput_and_out: 1218429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 12192d8f3038SAl Viro dput(path.dentry); 1220900148dcSAl Viro mntput_no_expire(mnt); 12211da177e4SLinus Torvalds out: 12221da177e4SLinus Torvalds return retval; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds /* 12281da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 12291da177e4SLinus Torvalds */ 1230bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 12311da177e4SLinus Torvalds { 12321da177e4SLinus Torvalds return sys_umount(name, 0); 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds #endif 12361da177e4SLinus Torvalds 12372d92ab3cSAl Viro static int mount_is_safe(struct path *path) 12381da177e4SLinus Torvalds { 12391da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 12401da177e4SLinus Torvalds return 0; 12411da177e4SLinus Torvalds return -EPERM; 12421da177e4SLinus Torvalds #ifdef notyet 12432d92ab3cSAl Viro if (S_ISLNK(path->dentry->d_inode->i_mode)) 12441da177e4SLinus Torvalds return -EPERM; 12452d92ab3cSAl Viro if (path->dentry->d_inode->i_mode & S_ISVTX) { 1246da9592edSDavid Howells if (current_uid() != path->dentry->d_inode->i_uid) 12471da177e4SLinus Torvalds return -EPERM; 12481da177e4SLinus Torvalds } 12492d92ab3cSAl Viro if (inode_permission(path->dentry->d_inode, MAY_WRITE)) 12501da177e4SLinus Torvalds return -EPERM; 12511da177e4SLinus Torvalds return 0; 12521da177e4SLinus Torvalds #endif 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 125587129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 125636341f64SRam Pai int flag) 12571da177e4SLinus Torvalds { 1258a73324daSAl Viro struct mount *res, *p, *q, *r; 12591a390689SAl Viro struct path path; 12601da177e4SLinus Torvalds 1261fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 12629676f0c6SRam Pai return NULL; 12639676f0c6SRam Pai 126436341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 12651da177e4SLinus Torvalds if (!q) 12661da177e4SLinus Torvalds goto Enomem; 1267a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds p = mnt; 12706b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1271315fc83eSAl Viro struct mount *s; 12727ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 12731da177e4SLinus Torvalds continue; 12741da177e4SLinus Torvalds 1275909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 1276fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 12779676f0c6SRam Pai s = skip_mnt_tree(s); 12789676f0c6SRam Pai continue; 12799676f0c6SRam Pai } 12800714a533SAl Viro while (p != s->mnt_parent) { 12810714a533SAl Viro p = p->mnt_parent; 12820714a533SAl Viro q = q->mnt_parent; 12831da177e4SLinus Torvalds } 128487129cc0SAl Viro p = s; 1285cb338d06SAl Viro path.mnt = &q->mnt; 1286a73324daSAl Viro path.dentry = p->mnt_mountpoint; 128787129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 12881da177e4SLinus Torvalds if (!q) 12891da177e4SLinus Torvalds goto Enomem; 1290962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 12911a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 1292cb338d06SAl Viro attach_mnt(q, &path); 1293962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds return res; 12971da177e4SLinus Torvalds Enomem: 12981da177e4SLinus Torvalds if (res) { 129970fbcdf4SRam Pai LIST_HEAD(umount_list); 1300962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1301761d5c38SAl Viro umount_tree(res, 0, &umount_list); 1302962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 130370fbcdf4SRam Pai release_mounts(&umount_list); 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds return NULL; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 1308589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 13098aec0809SAl Viro { 1310cb338d06SAl Viro struct mount *tree; 13111a60a280SAl Viro down_write(&namespace_sem); 131287129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 131387129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 13141a60a280SAl Viro up_write(&namespace_sem); 1315cb338d06SAl Viro return tree ? &tree->mnt : NULL; 13168aec0809SAl Viro } 13178aec0809SAl Viro 13188aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 13198aec0809SAl Viro { 13208aec0809SAl Viro LIST_HEAD(umount_list); 13211a60a280SAl Viro down_write(&namespace_sem); 1322962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1323761d5c38SAl Viro umount_tree(real_mount(mnt), 0, &umount_list); 1324962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 13251a60a280SAl Viro up_write(&namespace_sem); 13268aec0809SAl Viro release_mounts(&umount_list); 13278aec0809SAl Viro } 13288aec0809SAl Viro 13291f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 13301f707137SAl Viro struct vfsmount *root) 13311f707137SAl Viro { 13321a4eeaf2SAl Viro struct mount *mnt; 13331f707137SAl Viro int res = f(root, arg); 13341f707137SAl Viro if (res) 13351f707137SAl Viro return res; 13361a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 13371a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 13381f707137SAl Viro if (res) 13391f707137SAl Viro return res; 13401f707137SAl Viro } 13411f707137SAl Viro return 0; 13421f707137SAl Viro } 13431f707137SAl Viro 13444b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1345719f5d7fSMiklos Szeredi { 1346315fc83eSAl Viro struct mount *p; 1347719f5d7fSMiklos Szeredi 1348909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1349fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 13504b8b21f4SAl Viro mnt_release_group_id(p); 1351719f5d7fSMiklos Szeredi } 1352719f5d7fSMiklos Szeredi } 1353719f5d7fSMiklos Szeredi 13544b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1355719f5d7fSMiklos Szeredi { 1356315fc83eSAl Viro struct mount *p; 1357719f5d7fSMiklos Szeredi 1358909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1359fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 13604b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1361719f5d7fSMiklos Szeredi if (err) { 13624b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1363719f5d7fSMiklos Szeredi return err; 1364719f5d7fSMiklos Szeredi } 1365719f5d7fSMiklos Szeredi } 1366719f5d7fSMiklos Szeredi } 1367719f5d7fSMiklos Szeredi 1368719f5d7fSMiklos Szeredi return 0; 1369719f5d7fSMiklos Szeredi } 1370719f5d7fSMiklos Szeredi 1371b90fa9aeSRam Pai /* 1372b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1373b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 137421444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 137521444403SRam Pai * store the parent mount and mountpoint dentry. 137621444403SRam Pai * (done when source_mnt is moved) 1377b90fa9aeSRam Pai * 1378b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1379b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 13809676f0c6SRam Pai * --------------------------------------------------------------------------- 1381b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 13829676f0c6SRam Pai * |************************************************************************** 13839676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13849676f0c6SRam Pai * | dest | | | | | 13859676f0c6SRam Pai * | | | | | | | 13869676f0c6SRam Pai * | v | | | | | 13879676f0c6SRam Pai * |************************************************************************** 13889676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 13895afe0022SRam Pai * | | | | | | 13909676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 13919676f0c6SRam Pai * *************************************************************************** 1392b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1393b90fa9aeSRam Pai * destination mount. 1394b90fa9aeSRam Pai * 1395b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1396b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1397b90fa9aeSRam Pai * the peer group of the source mount. 1398b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1399b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1400b90fa9aeSRam Pai * mount. 14015afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 14025afe0022SRam Pai * of the destination mount and the cloned mount is made slave 14035afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 14045afe0022SRam Pai * is marked as 'shared and slave'. 14055afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 14065afe0022SRam Pai * source mount. 14075afe0022SRam Pai * 14089676f0c6SRam Pai * --------------------------------------------------------------------------- 140921444403SRam Pai * | MOVE MOUNT OPERATION | 14109676f0c6SRam Pai * |************************************************************************** 14119676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 14129676f0c6SRam Pai * | dest | | | | | 14139676f0c6SRam Pai * | | | | | | | 14149676f0c6SRam Pai * | v | | | | | 14159676f0c6SRam Pai * |************************************************************************** 14169676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 14175afe0022SRam Pai * | | | | | | 14189676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 14199676f0c6SRam Pai * *************************************************************************** 14205afe0022SRam Pai * 14215afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 14225afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 142321444403SRam Pai * (+*) the mount is moved to the destination. 14245afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 14255afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 14265afe0022SRam Pai * the mount is marked as 'shared and slave'. 14275afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1428b90fa9aeSRam Pai * 1429b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1430b90fa9aeSRam Pai * applied to each mount in the tree. 1431b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1432b90fa9aeSRam Pai * in allocations. 1433b90fa9aeSRam Pai */ 14340fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 14351a390689SAl Viro struct path *path, struct path *parent_path) 1436b90fa9aeSRam Pai { 1437b90fa9aeSRam Pai LIST_HEAD(tree_list); 1438a8d56d8eSAl Viro struct mount *dest_mnt = real_mount(path->mnt); 14391a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1440315fc83eSAl Viro struct mount *child, *p; 1441719f5d7fSMiklos Szeredi int err; 1442b90fa9aeSRam Pai 1443fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 14440fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1445719f5d7fSMiklos Szeredi if (err) 1446719f5d7fSMiklos Szeredi goto out; 1447719f5d7fSMiklos Szeredi } 1448a8d56d8eSAl Viro err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); 1449719f5d7fSMiklos Szeredi if (err) 1450719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1451b90fa9aeSRam Pai 1452962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1453df1a1ad2SAl Viro 1454fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1455909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 14560f0afb1dSAl Viro set_mnt_shared(p); 1457b90fa9aeSRam Pai } 14581a390689SAl Viro if (parent_path) { 14590fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 14600fb54e50SAl Viro attach_mnt(source_mnt, path); 1461143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 146221444403SRam Pai } else { 146314cf1fa8SAl Viro mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 14640fb54e50SAl Viro commit_tree(source_mnt); 146521444403SRam Pai } 1466b90fa9aeSRam Pai 14671b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 14681b8e5564SAl Viro list_del_init(&child->mnt_hash); 14694b2619a5SAl Viro commit_tree(child); 1470b90fa9aeSRam Pai } 1471962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 147299b7db7bSNick Piggin 1473b90fa9aeSRam Pai return 0; 1474719f5d7fSMiklos Szeredi 1475719f5d7fSMiklos Szeredi out_cleanup_ids: 1476fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 14770fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1478719f5d7fSMiklos Szeredi out: 1479719f5d7fSMiklos Szeredi return err; 1480b90fa9aeSRam Pai } 1481b90fa9aeSRam Pai 1482b12cea91SAl Viro static int lock_mount(struct path *path) 1483b12cea91SAl Viro { 1484b12cea91SAl Viro struct vfsmount *mnt; 1485b12cea91SAl Viro retry: 1486b12cea91SAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1487b12cea91SAl Viro if (unlikely(cant_mount(path->dentry))) { 1488b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1489b12cea91SAl Viro return -ENOENT; 1490b12cea91SAl Viro } 1491b12cea91SAl Viro down_write(&namespace_sem); 1492b12cea91SAl Viro mnt = lookup_mnt(path); 1493b12cea91SAl Viro if (likely(!mnt)) 1494b12cea91SAl Viro return 0; 1495b12cea91SAl Viro up_write(&namespace_sem); 1496b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1497b12cea91SAl Viro path_put(path); 1498b12cea91SAl Viro path->mnt = mnt; 1499b12cea91SAl Viro path->dentry = dget(mnt->mnt_root); 1500b12cea91SAl Viro goto retry; 1501b12cea91SAl Viro } 1502b12cea91SAl Viro 1503b12cea91SAl Viro static void unlock_mount(struct path *path) 1504b12cea91SAl Viro { 1505b12cea91SAl Viro up_write(&namespace_sem); 1506b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1507b12cea91SAl Viro } 1508b12cea91SAl Viro 150995bc5f25SAl Viro static int graft_tree(struct mount *mnt, struct path *path) 15101da177e4SLinus Torvalds { 151195bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 15121da177e4SLinus Torvalds return -EINVAL; 15131da177e4SLinus Torvalds 15148c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 151595bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 15161da177e4SLinus Torvalds return -ENOTDIR; 15171da177e4SLinus Torvalds 1518b12cea91SAl Viro if (d_unlinked(path->dentry)) 1519b12cea91SAl Viro return -ENOENT; 15201da177e4SLinus Torvalds 152195bc5f25SAl Viro return attach_recursive_mnt(mnt, path, NULL); 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds /* 15257a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 15267a2e8a8fSValerie Aurora */ 15277a2e8a8fSValerie Aurora 15287a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 15297a2e8a8fSValerie Aurora { 15307c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 15317a2e8a8fSValerie Aurora 15327a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 15337a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 15347a2e8a8fSValerie Aurora return 0; 15357a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 15367a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 15377a2e8a8fSValerie Aurora return 0; 15387a2e8a8fSValerie Aurora return type; 15397a2e8a8fSValerie Aurora } 15407a2e8a8fSValerie Aurora 15417a2e8a8fSValerie Aurora /* 154207b20889SRam Pai * recursively change the type of the mountpoint. 154307b20889SRam Pai */ 15440a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 154507b20889SRam Pai { 1546315fc83eSAl Viro struct mount *m; 15474b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 154807b20889SRam Pai int recurse = flag & MS_REC; 15497a2e8a8fSValerie Aurora int type; 1550719f5d7fSMiklos Szeredi int err = 0; 155107b20889SRam Pai 1552ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 1553ee6f9582SMiklos Szeredi return -EPERM; 1554ee6f9582SMiklos Szeredi 15552d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 155607b20889SRam Pai return -EINVAL; 155707b20889SRam Pai 15587a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 15597a2e8a8fSValerie Aurora if (!type) 15607a2e8a8fSValerie Aurora return -EINVAL; 15617a2e8a8fSValerie Aurora 156207b20889SRam Pai down_write(&namespace_sem); 1563719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1564719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1565719f5d7fSMiklos Szeredi if (err) 1566719f5d7fSMiklos Szeredi goto out_unlock; 1567719f5d7fSMiklos Szeredi } 1568719f5d7fSMiklos Szeredi 1569962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1570909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 15710f0afb1dSAl Viro change_mnt_propagation(m, type); 1572962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1573719f5d7fSMiklos Szeredi 1574719f5d7fSMiklos Szeredi out_unlock: 157507b20889SRam Pai up_write(&namespace_sem); 1576719f5d7fSMiklos Szeredi return err; 157707b20889SRam Pai } 157807b20889SRam Pai 157907b20889SRam Pai /* 15801da177e4SLinus Torvalds * do loopback mount. 15811da177e4SLinus Torvalds */ 15820a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name, 15832dafe1c4SEric Sandeen int recurse) 15841da177e4SLinus Torvalds { 1585b12cea91SAl Viro LIST_HEAD(umount_list); 15862d92ab3cSAl Viro struct path old_path; 158787129cc0SAl Viro struct mount *mnt = NULL, *old; 15882d92ab3cSAl Viro int err = mount_is_safe(path); 15891da177e4SLinus Torvalds if (err) 15901da177e4SLinus Torvalds return err; 15911da177e4SLinus Torvalds if (!old_name || !*old_name) 15921da177e4SLinus Torvalds return -EINVAL; 1593815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 15941da177e4SLinus Torvalds if (err) 15951da177e4SLinus Torvalds return err; 15961da177e4SLinus Torvalds 1597b12cea91SAl Viro err = lock_mount(path); 1598b12cea91SAl Viro if (err) 15999676f0c6SRam Pai goto out; 16009676f0c6SRam Pai 160187129cc0SAl Viro old = real_mount(old_path.mnt); 160287129cc0SAl Viro 1603b12cea91SAl Viro err = -EINVAL; 1604fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1605b12cea91SAl Viro goto out2; 1606b12cea91SAl Viro 1607143c8c91SAl Viro if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old)) 1608b12cea91SAl Viro goto out2; 1609ccd48bc7SAl Viro 16101da177e4SLinus Torvalds err = -ENOMEM; 16111da177e4SLinus Torvalds if (recurse) 161287129cc0SAl Viro mnt = copy_tree(old, old_path.dentry, 0); 16131da177e4SLinus Torvalds else 161487129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 16151da177e4SLinus Torvalds 1616ccd48bc7SAl Viro if (!mnt) 1617b12cea91SAl Viro goto out2; 1618ccd48bc7SAl Viro 161995bc5f25SAl Viro err = graft_tree(mnt, path); 16201da177e4SLinus Torvalds if (err) { 1621962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1622761d5c38SAl Viro umount_tree(mnt, 0, &umount_list); 1623962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 16245b83d2c5SRam Pai } 1625b12cea91SAl Viro out2: 1626b12cea91SAl Viro unlock_mount(path); 1627b12cea91SAl Viro release_mounts(&umount_list); 1628ccd48bc7SAl Viro out: 16292d92ab3cSAl Viro path_put(&old_path); 16301da177e4SLinus Torvalds return err; 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds 16332e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 16342e4b7fcdSDave Hansen { 16352e4b7fcdSDave Hansen int error = 0; 16362e4b7fcdSDave Hansen int readonly_request = 0; 16372e4b7fcdSDave Hansen 16382e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 16392e4b7fcdSDave Hansen readonly_request = 1; 16402e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 16412e4b7fcdSDave Hansen return 0; 16422e4b7fcdSDave Hansen 16432e4b7fcdSDave Hansen if (readonly_request) 164483adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 16452e4b7fcdSDave Hansen else 164683adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 16472e4b7fcdSDave Hansen return error; 16482e4b7fcdSDave Hansen } 16492e4b7fcdSDave Hansen 16501da177e4SLinus Torvalds /* 16511da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 16521da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 16531da177e4SLinus Torvalds * on it - tough luck. 16541da177e4SLinus Torvalds */ 16550a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 16561da177e4SLinus Torvalds void *data) 16571da177e4SLinus Torvalds { 16581da177e4SLinus Torvalds int err; 16592d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1660143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16631da177e4SLinus Torvalds return -EPERM; 16641da177e4SLinus Torvalds 1665143c8c91SAl Viro if (!check_mnt(mnt)) 16661da177e4SLinus Torvalds return -EINVAL; 16671da177e4SLinus Torvalds 16682d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 16691da177e4SLinus Torvalds return -EINVAL; 16701da177e4SLinus Torvalds 1671ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1672ff36fe2cSEric Paris if (err) 1673ff36fe2cSEric Paris return err; 1674ff36fe2cSEric Paris 16751da177e4SLinus Torvalds down_write(&sb->s_umount); 16762e4b7fcdSDave Hansen if (flags & MS_BIND) 16772d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 16784aa98cf7SAl Viro else 16791da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 16807b43a79fSAl Viro if (!err) { 1681962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1682143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1683143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1684962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 16857b43a79fSAl Viro } 16861da177e4SLinus Torvalds up_write(&sb->s_umount); 16870e55a7ccSDan Williams if (!err) { 1688962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1689143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1690962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 16910e55a7ccSDan Williams } 16921da177e4SLinus Torvalds return err; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 1695cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 16969676f0c6SRam Pai { 1697315fc83eSAl Viro struct mount *p; 1698909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1699fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 17009676f0c6SRam Pai return 1; 17019676f0c6SRam Pai } 17029676f0c6SRam Pai return 0; 17039676f0c6SRam Pai } 17049676f0c6SRam Pai 17050a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name) 17061da177e4SLinus Torvalds { 17072d92ab3cSAl Viro struct path old_path, parent_path; 1708676da58dSAl Viro struct mount *p; 17090fb54e50SAl Viro struct mount *old; 17101da177e4SLinus Torvalds int err = 0; 17111da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17121da177e4SLinus Torvalds return -EPERM; 17131da177e4SLinus Torvalds if (!old_name || !*old_name) 17141da177e4SLinus Torvalds return -EINVAL; 17152d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 17161da177e4SLinus Torvalds if (err) 17171da177e4SLinus Torvalds return err; 17181da177e4SLinus Torvalds 1719b12cea91SAl Viro err = lock_mount(path); 1720cc53ce53SDavid Howells if (err < 0) 1721cc53ce53SDavid Howells goto out; 1722cc53ce53SDavid Howells 1723143c8c91SAl Viro old = real_mount(old_path.mnt); 1724fc7be130SAl Viro p = real_mount(path->mnt); 1725143c8c91SAl Viro 17261da177e4SLinus Torvalds err = -EINVAL; 1727fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 17281da177e4SLinus Torvalds goto out1; 17291da177e4SLinus Torvalds 1730f3da392eSAlexey Dobriyan if (d_unlinked(path->dentry)) 173121444403SRam Pai goto out1; 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds err = -EINVAL; 17342d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 173521444403SRam Pai goto out1; 17361da177e4SLinus Torvalds 1737676da58dSAl Viro if (!mnt_has_parent(old)) 173821444403SRam Pai goto out1; 17391da177e4SLinus Torvalds 17402d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 17412d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 174221444403SRam Pai goto out1; 174321444403SRam Pai /* 174421444403SRam Pai * Don't move a mount residing in a shared parent. 174521444403SRam Pai */ 1746fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 174721444403SRam Pai goto out1; 17489676f0c6SRam Pai /* 17499676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 17509676f0c6SRam Pai * mount which is shared. 17519676f0c6SRam Pai */ 1752fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 17539676f0c6SRam Pai goto out1; 17541da177e4SLinus Torvalds err = -ELOOP; 1755fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1756676da58dSAl Viro if (p == old) 175721444403SRam Pai goto out1; 17581da177e4SLinus Torvalds 17590fb54e50SAl Viro err = attach_recursive_mnt(old, path, &parent_path); 17604ac91378SJan Blunck if (err) 176121444403SRam Pai goto out1; 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 17641da177e4SLinus Torvalds * automatically */ 17656776db3dSAl Viro list_del_init(&old->mnt_expire); 17661da177e4SLinus Torvalds out1: 1767b12cea91SAl Viro unlock_mount(path); 17681da177e4SLinus Torvalds out: 17691da177e4SLinus Torvalds if (!err) 17701a390689SAl Viro path_put(&parent_path); 17712d92ab3cSAl Viro path_put(&old_path); 17721da177e4SLinus Torvalds return err; 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds 17759d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 17769d412a43SAl Viro { 17779d412a43SAl Viro int err; 17789d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 17799d412a43SAl Viro if (subtype) { 17809d412a43SAl Viro subtype++; 17819d412a43SAl Viro err = -EINVAL; 17829d412a43SAl Viro if (!subtype[0]) 17839d412a43SAl Viro goto err; 17849d412a43SAl Viro } else 17859d412a43SAl Viro subtype = ""; 17869d412a43SAl Viro 17879d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 17889d412a43SAl Viro err = -ENOMEM; 17899d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 17909d412a43SAl Viro goto err; 17919d412a43SAl Viro return mnt; 17929d412a43SAl Viro 17939d412a43SAl Viro err: 17949d412a43SAl Viro mntput(mnt); 17959d412a43SAl Viro return ERR_PTR(err); 17969d412a43SAl Viro } 17979d412a43SAl Viro 179879e801a9SAl Viro static struct vfsmount * 17999d412a43SAl Viro do_kern_mount(const char *fstype, int flags, const char *name, void *data) 18009d412a43SAl Viro { 18019d412a43SAl Viro struct file_system_type *type = get_fs_type(fstype); 18029d412a43SAl Viro struct vfsmount *mnt; 18039d412a43SAl Viro if (!type) 18049d412a43SAl Viro return ERR_PTR(-ENODEV); 18059d412a43SAl Viro mnt = vfs_kern_mount(type, flags, name, data); 18069d412a43SAl Viro if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 18079d412a43SAl Viro !mnt->mnt_sb->s_subtype) 18089d412a43SAl Viro mnt = fs_set_subtype(mnt, fstype); 18099d412a43SAl Viro put_filesystem(type); 18109d412a43SAl Viro return mnt; 18119d412a43SAl Viro } 18129d412a43SAl Viro 18139d412a43SAl Viro /* 18149d412a43SAl Viro * add a mount into a namespace's mount tree 18159d412a43SAl Viro */ 181695bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 18179d412a43SAl Viro { 18189d412a43SAl Viro int err; 18199d412a43SAl Viro 18209d412a43SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 18219d412a43SAl Viro 1822b12cea91SAl Viro err = lock_mount(path); 1823b12cea91SAl Viro if (err) 1824b12cea91SAl Viro return err; 18259d412a43SAl Viro 18269d412a43SAl Viro err = -EINVAL; 1827143c8c91SAl Viro if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt))) 18289d412a43SAl Viro goto unlock; 18299d412a43SAl Viro 18309d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 18319d412a43SAl Viro err = -EBUSY; 183295bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 18339d412a43SAl Viro path->mnt->mnt_root == path->dentry) 18349d412a43SAl Viro goto unlock; 18359d412a43SAl Viro 18369d412a43SAl Viro err = -EINVAL; 183795bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 18389d412a43SAl Viro goto unlock; 18399d412a43SAl Viro 184095bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 18419d412a43SAl Viro err = graft_tree(newmnt, path); 18429d412a43SAl Viro 18439d412a43SAl Viro unlock: 1844b12cea91SAl Viro unlock_mount(path); 18459d412a43SAl Viro return err; 18469d412a43SAl Viro } 1847b1e75df4SAl Viro 18481da177e4SLinus Torvalds /* 18491da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 18501da177e4SLinus Torvalds * namespace's tree 18511da177e4SLinus Torvalds */ 18520a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags, 18531da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 18541da177e4SLinus Torvalds { 18551da177e4SLinus Torvalds struct vfsmount *mnt; 185615f9a3f3SAl Viro int err; 18571da177e4SLinus Torvalds 1858eca6f534SVegard Nossum if (!type) 18591da177e4SLinus Torvalds return -EINVAL; 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds /* we need capabilities... */ 18621da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 18631da177e4SLinus Torvalds return -EPERM; 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 18661da177e4SLinus Torvalds if (IS_ERR(mnt)) 18671da177e4SLinus Torvalds return PTR_ERR(mnt); 18681da177e4SLinus Torvalds 186995bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 187015f9a3f3SAl Viro if (err) 187115f9a3f3SAl Viro mntput(mnt); 187215f9a3f3SAl Viro return err; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 187519a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 187619a167afSAl Viro { 18776776db3dSAl Viro struct mount *mnt = real_mount(m); 187819a167afSAl Viro int err; 187919a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 188019a167afSAl Viro * expired before we get a chance to add it 188119a167afSAl Viro */ 18826776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 188319a167afSAl Viro 188419a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 188519a167afSAl Viro m->mnt_root == path->dentry) { 1886b1e75df4SAl Viro err = -ELOOP; 1887b1e75df4SAl Viro goto fail; 188819a167afSAl Viro } 188919a167afSAl Viro 189095bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 1891b1e75df4SAl Viro if (!err) 1892b1e75df4SAl Viro return 0; 1893b1e75df4SAl Viro fail: 1894b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 18956776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 1896b1e75df4SAl Viro down_write(&namespace_sem); 1897962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 18986776db3dSAl Viro list_del_init(&mnt->mnt_expire); 1899962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1900b1e75df4SAl Viro up_write(&namespace_sem); 190119a167afSAl Viro } 1902b1e75df4SAl Viro mntput(m); 1903b1e75df4SAl Viro mntput(m); 190419a167afSAl Viro return err; 190519a167afSAl Viro } 190619a167afSAl Viro 1907ea5b778aSDavid Howells /** 1908ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 1909ea5b778aSDavid Howells * @mnt: The mount to list. 1910ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 1911ea5b778aSDavid Howells */ 1912ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 1913ea5b778aSDavid Howells { 1914ea5b778aSDavid Howells down_write(&namespace_sem); 1915962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1916ea5b778aSDavid Howells 19176776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 1918ea5b778aSDavid Howells 1919962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1920ea5b778aSDavid Howells up_write(&namespace_sem); 1921ea5b778aSDavid Howells } 1922ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 1923ea5b778aSDavid Howells 1924ea5b778aSDavid Howells /* 19251da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 19261da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 19271da177e4SLinus Torvalds * here 19281da177e4SLinus Torvalds */ 19291da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 19301da177e4SLinus Torvalds { 1931761d5c38SAl Viro struct mount *mnt, *next; 19321da177e4SLinus Torvalds LIST_HEAD(graveyard); 1933bcc5c7d2SAl Viro LIST_HEAD(umounts); 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds if (list_empty(mounts)) 19361da177e4SLinus Torvalds return; 19371da177e4SLinus Torvalds 1938bcc5c7d2SAl Viro down_write(&namespace_sem); 1939962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 19421da177e4SLinus Torvalds * following criteria: 19431da177e4SLinus Torvalds * - only referenced by its parent vfsmount 19441da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 19451da177e4SLinus Torvalds * cleared by mntput()) 19461da177e4SLinus Torvalds */ 19476776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 1948863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 19491ab59738SAl Viro propagate_mount_busy(mnt, 1)) 19501da177e4SLinus Torvalds continue; 19516776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 19521da177e4SLinus Torvalds } 1953bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 19546776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 1955143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1956bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 1957bcc5c7d2SAl Viro } 1958962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1959bcc5c7d2SAl Viro up_write(&namespace_sem); 1960bcc5c7d2SAl Viro 1961bcc5c7d2SAl Viro release_mounts(&umounts); 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds /* 19675528f911STrond Myklebust * Ripoff of 'select_parent()' 19685528f911STrond Myklebust * 19695528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 19705528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 19715528f911STrond Myklebust */ 1972692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 19735528f911STrond Myklebust { 1974692afc31SAl Viro struct mount *this_parent = parent; 19755528f911STrond Myklebust struct list_head *next; 19765528f911STrond Myklebust int found = 0; 19775528f911STrond Myklebust 19785528f911STrond Myklebust repeat: 19796b41d536SAl Viro next = this_parent->mnt_mounts.next; 19805528f911STrond Myklebust resume: 19816b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 19825528f911STrond Myklebust struct list_head *tmp = next; 19836b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 19845528f911STrond Myklebust 19855528f911STrond Myklebust next = tmp->next; 1986692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 19875528f911STrond Myklebust continue; 19885528f911STrond Myklebust /* 19895528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 19905528f911STrond Myklebust */ 19916b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 19925528f911STrond Myklebust this_parent = mnt; 19935528f911STrond Myklebust goto repeat; 19945528f911STrond Myklebust } 19955528f911STrond Myklebust 19961ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 19976776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 19985528f911STrond Myklebust found++; 19995528f911STrond Myklebust } 20005528f911STrond Myklebust } 20015528f911STrond Myklebust /* 20025528f911STrond Myklebust * All done at this level ... ascend and resume the search 20035528f911STrond Myklebust */ 20045528f911STrond Myklebust if (this_parent != parent) { 20056b41d536SAl Viro next = this_parent->mnt_child.next; 20060714a533SAl Viro this_parent = this_parent->mnt_parent; 20075528f911STrond Myklebust goto resume; 20085528f911STrond Myklebust } 20095528f911STrond Myklebust return found; 20105528f911STrond Myklebust } 20115528f911STrond Myklebust 20125528f911STrond Myklebust /* 20135528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 20145528f911STrond Myklebust * submounts of a specific parent mountpoint 201599b7db7bSNick Piggin * 201699b7db7bSNick Piggin * vfsmount_lock must be held for write 20175528f911STrond Myklebust */ 2018692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts) 20195528f911STrond Myklebust { 20205528f911STrond Myklebust LIST_HEAD(graveyard); 2021761d5c38SAl Viro struct mount *m; 20225528f911STrond Myklebust 20235528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2024c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2025bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2026761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 20276776db3dSAl Viro mnt_expire); 2028143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2029afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 2030bcc5c7d2SAl Viro } 2031bcc5c7d2SAl Viro } 20325528f911STrond Myklebust } 20335528f911STrond Myklebust 20345528f911STrond Myklebust /* 20351da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 20361da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 20371da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 20381da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 20391da177e4SLinus Torvalds */ 2040b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2041b58fed8bSRam Pai unsigned long n) 20421da177e4SLinus Torvalds { 20431da177e4SLinus Torvalds char *t = to; 20441da177e4SLinus Torvalds const char __user *f = from; 20451da177e4SLinus Torvalds char c; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 20481da177e4SLinus Torvalds return n; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds while (n) { 20511da177e4SLinus Torvalds if (__get_user(c, f)) { 20521da177e4SLinus Torvalds memset(t, 0, n); 20531da177e4SLinus Torvalds break; 20541da177e4SLinus Torvalds } 20551da177e4SLinus Torvalds *t++ = c; 20561da177e4SLinus Torvalds f++; 20571da177e4SLinus Torvalds n--; 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds return n; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 20631da177e4SLinus Torvalds { 20641da177e4SLinus Torvalds int i; 20651da177e4SLinus Torvalds unsigned long page; 20661da177e4SLinus Torvalds unsigned long size; 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds *where = 0; 20691da177e4SLinus Torvalds if (!data) 20701da177e4SLinus Torvalds return 0; 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 20731da177e4SLinus Torvalds return -ENOMEM; 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds /* We only care that *some* data at the address the user 20761da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 20771da177e4SLinus Torvalds * the remainder of the page. 20781da177e4SLinus Torvalds */ 20791da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 20801da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 20811da177e4SLinus Torvalds if (size > PAGE_SIZE) 20821da177e4SLinus Torvalds size = PAGE_SIZE; 20831da177e4SLinus Torvalds 20841da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 20851da177e4SLinus Torvalds if (!i) { 20861da177e4SLinus Torvalds free_page(page); 20871da177e4SLinus Torvalds return -EFAULT; 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds if (i != PAGE_SIZE) 20901da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 20911da177e4SLinus Torvalds *where = page; 20921da177e4SLinus Torvalds return 0; 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds 2095eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2096eca6f534SVegard Nossum { 2097eca6f534SVegard Nossum char *tmp; 2098eca6f534SVegard Nossum 2099eca6f534SVegard Nossum if (!data) { 2100eca6f534SVegard Nossum *where = NULL; 2101eca6f534SVegard Nossum return 0; 2102eca6f534SVegard Nossum } 2103eca6f534SVegard Nossum 2104eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2105eca6f534SVegard Nossum if (IS_ERR(tmp)) 2106eca6f534SVegard Nossum return PTR_ERR(tmp); 2107eca6f534SVegard Nossum 2108eca6f534SVegard Nossum *where = tmp; 2109eca6f534SVegard Nossum return 0; 2110eca6f534SVegard Nossum } 2111eca6f534SVegard Nossum 21121da177e4SLinus Torvalds /* 21131da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 21141da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 21151da177e4SLinus Torvalds * 21161da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 21171da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 21181da177e4SLinus Torvalds * information (or be NULL). 21191da177e4SLinus Torvalds * 21201da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 21211da177e4SLinus Torvalds * When the flags word was introduced its top half was required 21221da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 21231da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 21241da177e4SLinus Torvalds * and must be discarded. 21251da177e4SLinus Torvalds */ 21261da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 21271da177e4SLinus Torvalds unsigned long flags, void *data_page) 21281da177e4SLinus Torvalds { 21292d92ab3cSAl Viro struct path path; 21301da177e4SLinus Torvalds int retval = 0; 21311da177e4SLinus Torvalds int mnt_flags = 0; 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds /* Discard magic */ 21341da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 21351da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 21361da177e4SLinus Torvalds 21371da177e4SLinus Torvalds /* Basic sanity checks */ 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 21401da177e4SLinus Torvalds return -EINVAL; 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds if (data_page) 21431da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 21441da177e4SLinus Torvalds 2145a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2146a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2147a27ab9f2STetsuo Handa if (retval) 2148a27ab9f2STetsuo Handa return retval; 2149a27ab9f2STetsuo Handa 2150a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2151a27ab9f2STetsuo Handa type_page, flags, data_page); 2152a27ab9f2STetsuo Handa if (retval) 2153a27ab9f2STetsuo Handa goto dput_out; 2154a27ab9f2STetsuo Handa 2155613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2156613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 21570a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 21580a1c01c9SMatthew Garrett 21591da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 21601da177e4SLinus Torvalds if (flags & MS_NOSUID) 21611da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 21621da177e4SLinus Torvalds if (flags & MS_NODEV) 21631da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 21641da177e4SLinus Torvalds if (flags & MS_NOEXEC) 21651da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2166fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2167fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2168fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2169fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2170d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2171d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 21722e4b7fcdSDave Hansen if (flags & MS_RDONLY) 21732e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2174fc33a7bbSChristoph Hellwig 21757a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2176d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2177d0adde57SMatthew Garrett MS_STRICTATIME); 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds if (flags & MS_REMOUNT) 21802d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 21811da177e4SLinus Torvalds data_page); 21821da177e4SLinus Torvalds else if (flags & MS_BIND) 21832d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 21849676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21852d92ab3cSAl Viro retval = do_change_type(&path, flags); 21861da177e4SLinus Torvalds else if (flags & MS_MOVE) 21872d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 21881da177e4SLinus Torvalds else 21892d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 21901da177e4SLinus Torvalds dev_name, data_page); 21911da177e4SLinus Torvalds dput_out: 21922d92ab3cSAl Viro path_put(&path); 21931da177e4SLinus Torvalds return retval; 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds 2196cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void) 2197cf8d2c11STrond Myklebust { 2198cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2199cf8d2c11STrond Myklebust 2200cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2201cf8d2c11STrond Myklebust if (!new_ns) 2202cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2203cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2204cf8d2c11STrond Myklebust new_ns->root = NULL; 2205cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2206cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2207cf8d2c11STrond Myklebust new_ns->event = 0; 2208cf8d2c11STrond Myklebust return new_ns; 2209cf8d2c11STrond Myklebust } 2210cf8d2c11STrond Myklebust 2211f03c6599SAl Viro void mnt_make_longterm(struct vfsmount *mnt) 2212f03c6599SAl Viro { 221383adc753SAl Viro __mnt_make_longterm(real_mount(mnt)); 2214f03c6599SAl Viro } 2215f03c6599SAl Viro 221683adc753SAl Viro void mnt_make_shortterm(struct vfsmount *m) 2217f03c6599SAl Viro { 22187e3d0eb0SAl Viro #ifdef CONFIG_SMP 221983adc753SAl Viro struct mount *mnt = real_mount(m); 222068e8a9feSAl Viro if (atomic_add_unless(&mnt->mnt_longterm, -1, 1)) 2221f03c6599SAl Viro return; 2222962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 222368e8a9feSAl Viro atomic_dec(&mnt->mnt_longterm); 2224962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 22257e3d0eb0SAl Viro #endif 2226f03c6599SAl Viro } 2227f03c6599SAl Viro 2228741a2951SJANAK DESAI /* 2229741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2230741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2231741a2951SJANAK DESAI */ 2232e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 22336b3286edSKirill Korotaev struct fs_struct *fs) 22341da177e4SLinus Torvalds { 22356b3286edSKirill Korotaev struct mnt_namespace *new_ns; 22367f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2237315fc83eSAl Viro struct mount *p, *q; 2238be08d6d2SAl Viro struct mount *old = mnt_ns->root; 2239cb338d06SAl Viro struct mount *new; 22401da177e4SLinus Torvalds 2241cf8d2c11STrond Myklebust new_ns = alloc_mnt_ns(); 2242cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2243cf8d2c11STrond Myklebust return new_ns; 22441da177e4SLinus Torvalds 2245390c6843SRam Pai down_write(&namespace_sem); 22461da177e4SLinus Torvalds /* First pass: copy the tree topology */ 2247909b0a88SAl Viro new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE); 2248cb338d06SAl Viro if (!new) { 2249390c6843SRam Pai up_write(&namespace_sem); 22501da177e4SLinus Torvalds kfree(new_ns); 22515cc4a034SJulia Lawall return ERR_PTR(-ENOMEM); 22521da177e4SLinus Torvalds } 2253be08d6d2SAl Viro new_ns->root = new; 2254962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 22551a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 2256962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds /* 22591da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 22601da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 22611da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 22621da177e4SLinus Torvalds */ 2263909b0a88SAl Viro p = old; 2264cb338d06SAl Viro q = new; 22651da177e4SLinus Torvalds while (p) { 2266143c8c91SAl Viro q->mnt_ns = new_ns; 226783adc753SAl Viro __mnt_make_longterm(q); 22681da177e4SLinus Torvalds if (fs) { 2269315fc83eSAl Viro if (&p->mnt == fs->root.mnt) { 2270315fc83eSAl Viro fs->root.mnt = mntget(&q->mnt); 227183adc753SAl Viro __mnt_make_longterm(q); 2272315fc83eSAl Viro mnt_make_shortterm(&p->mnt); 2273315fc83eSAl Viro rootmnt = &p->mnt; 22741da177e4SLinus Torvalds } 2275315fc83eSAl Viro if (&p->mnt == fs->pwd.mnt) { 2276315fc83eSAl Viro fs->pwd.mnt = mntget(&q->mnt); 227783adc753SAl Viro __mnt_make_longterm(q); 2278315fc83eSAl Viro mnt_make_shortterm(&p->mnt); 2279315fc83eSAl Viro pwdmnt = &p->mnt; 22801da177e4SLinus Torvalds } 22811da177e4SLinus Torvalds } 2282909b0a88SAl Viro p = next_mnt(p, old); 2283909b0a88SAl Viro q = next_mnt(q, new); 22841da177e4SLinus Torvalds } 2285390c6843SRam Pai up_write(&namespace_sem); 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds if (rootmnt) 2288f03c6599SAl Viro mntput(rootmnt); 22891da177e4SLinus Torvalds if (pwdmnt) 2290f03c6599SAl Viro mntput(pwdmnt); 22911da177e4SLinus Torvalds 2292741a2951SJANAK DESAI return new_ns; 2293741a2951SJANAK DESAI } 2294741a2951SJANAK DESAI 2295213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2296e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 2297741a2951SJANAK DESAI { 22986b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2299741a2951SJANAK DESAI 2300e3222c4eSBadari Pulavarty BUG_ON(!ns); 23016b3286edSKirill Korotaev get_mnt_ns(ns); 2302741a2951SJANAK DESAI 2303741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2304e3222c4eSBadari Pulavarty return ns; 2305741a2951SJANAK DESAI 2306e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 2307741a2951SJANAK DESAI 23086b3286edSKirill Korotaev put_mnt_ns(ns); 2309e3222c4eSBadari Pulavarty return new_ns; 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 2312cf8d2c11STrond Myklebust /** 2313cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2314cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2315cf8d2c11STrond Myklebust */ 23161a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2317cf8d2c11STrond Myklebust { 23181a4eeaf2SAl Viro struct mnt_namespace *new_ns = alloc_mnt_ns(); 2319cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 23201a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 23211a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 23221a4eeaf2SAl Viro __mnt_make_longterm(mnt); 2323be08d6d2SAl Viro new_ns->root = mnt; 23241a4eeaf2SAl Viro list_add(&new_ns->list, &mnt->mnt_list); 2325c1334495SAl Viro } else { 23261a4eeaf2SAl Viro mntput(m); 2327cf8d2c11STrond Myklebust } 2328cf8d2c11STrond Myklebust return new_ns; 2329cf8d2c11STrond Myklebust } 2330cf8d2c11STrond Myklebust 2331ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2332ea441d11SAl Viro { 2333ea441d11SAl Viro struct mnt_namespace *ns; 2334d31da0f0SAl Viro struct super_block *s; 2335ea441d11SAl Viro struct path path; 2336ea441d11SAl Viro int err; 2337ea441d11SAl Viro 2338ea441d11SAl Viro ns = create_mnt_ns(mnt); 2339ea441d11SAl Viro if (IS_ERR(ns)) 2340ea441d11SAl Viro return ERR_CAST(ns); 2341ea441d11SAl Viro 2342ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2343ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2344ea441d11SAl Viro 2345ea441d11SAl Viro put_mnt_ns(ns); 2346ea441d11SAl Viro 2347ea441d11SAl Viro if (err) 2348ea441d11SAl Viro return ERR_PTR(err); 2349ea441d11SAl Viro 2350ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2351d31da0f0SAl Viro s = path.mnt->mnt_sb; 2352d31da0f0SAl Viro atomic_inc(&s->s_active); 2353ea441d11SAl Viro mntput(path.mnt); 2354ea441d11SAl Viro /* lock the sucker */ 2355d31da0f0SAl Viro down_write(&s->s_umount); 2356ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2357ea441d11SAl Viro return path.dentry; 2358ea441d11SAl Viro } 2359ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2360ea441d11SAl Viro 2361bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2362bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 23631da177e4SLinus Torvalds { 2364eca6f534SVegard Nossum int ret; 2365eca6f534SVegard Nossum char *kernel_type; 2366eca6f534SVegard Nossum char *kernel_dir; 2367eca6f534SVegard Nossum char *kernel_dev; 23681da177e4SLinus Torvalds unsigned long data_page; 23691da177e4SLinus Torvalds 2370eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2371eca6f534SVegard Nossum if (ret < 0) 2372eca6f534SVegard Nossum goto out_type; 23731da177e4SLinus Torvalds 2374eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2375eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2376eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2377eca6f534SVegard Nossum goto out_dir; 2378eca6f534SVegard Nossum } 23791da177e4SLinus Torvalds 2380eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2381eca6f534SVegard Nossum if (ret < 0) 2382eca6f534SVegard Nossum goto out_dev; 23831da177e4SLinus Torvalds 2384eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2385eca6f534SVegard Nossum if (ret < 0) 2386eca6f534SVegard Nossum goto out_data; 23871da177e4SLinus Torvalds 2388eca6f534SVegard Nossum ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags, 2389eca6f534SVegard Nossum (void *) data_page); 2390eca6f534SVegard Nossum 23911da177e4SLinus Torvalds free_page(data_page); 2392eca6f534SVegard Nossum out_data: 2393eca6f534SVegard Nossum kfree(kernel_dev); 2394eca6f534SVegard Nossum out_dev: 2395eca6f534SVegard Nossum putname(kernel_dir); 2396eca6f534SVegard Nossum out_dir: 2397eca6f534SVegard Nossum kfree(kernel_type); 2398eca6f534SVegard Nossum out_type: 2399eca6f534SVegard Nossum return ret; 24001da177e4SLinus Torvalds } 24011da177e4SLinus Torvalds 24021da177e4SLinus Torvalds /* 2403afac7cbaSAl Viro * Return true if path is reachable from root 2404afac7cbaSAl Viro * 2405afac7cbaSAl Viro * namespace_sem or vfsmount_lock is held 2406afac7cbaSAl Viro */ 2407643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2408afac7cbaSAl Viro const struct path *root) 2409afac7cbaSAl Viro { 2410643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2411a73324daSAl Viro dentry = mnt->mnt_mountpoint; 24120714a533SAl Viro mnt = mnt->mnt_parent; 2413afac7cbaSAl Viro } 2414643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2415afac7cbaSAl Viro } 2416afac7cbaSAl Viro 2417afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2418afac7cbaSAl Viro { 2419afac7cbaSAl Viro int res; 2420962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 2421643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 2422962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 2423afac7cbaSAl Viro return res; 2424afac7cbaSAl Viro } 2425afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2426afac7cbaSAl Viro 2427afac7cbaSAl Viro /* 24281da177e4SLinus Torvalds * pivot_root Semantics: 24291da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 24301da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 24311da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 24321da177e4SLinus Torvalds * 24331da177e4SLinus Torvalds * Restrictions: 24341da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 24351da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 24361da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 24371da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 24381da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 24391da177e4SLinus Torvalds * 24404a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 24414a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 24424a0d11faSNeil Brown * in this situation. 24434a0d11faSNeil Brown * 24441da177e4SLinus Torvalds * Notes: 24451da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 24461da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 24471da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 24481da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 24491da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 24501da177e4SLinus Torvalds * first. 24511da177e4SLinus Torvalds */ 24523480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 24533480b257SHeiko Carstens const char __user *, put_old) 24541da177e4SLinus Torvalds { 24552d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 2456419148daSAl Viro struct mount *new_mnt, *root_mnt; 24571da177e4SLinus Torvalds int error; 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 24601da177e4SLinus Torvalds return -EPERM; 24611da177e4SLinus Torvalds 24622d8f3038SAl Viro error = user_path_dir(new_root, &new); 24631da177e4SLinus Torvalds if (error) 24641da177e4SLinus Torvalds goto out0; 24651da177e4SLinus Torvalds 24662d8f3038SAl Viro error = user_path_dir(put_old, &old); 24671da177e4SLinus Torvalds if (error) 24681da177e4SLinus Torvalds goto out1; 24691da177e4SLinus Torvalds 24702d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2471b12cea91SAl Viro if (error) 2472b12cea91SAl Viro goto out2; 24731da177e4SLinus Torvalds 2474f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 2475b12cea91SAl Viro error = lock_mount(&old); 2476b12cea91SAl Viro if (error) 2477b12cea91SAl Viro goto out3; 2478b12cea91SAl Viro 24791da177e4SLinus Torvalds error = -EINVAL; 2480419148daSAl Viro new_mnt = real_mount(new.mnt); 2481419148daSAl Viro root_mnt = real_mount(root.mnt); 2482fc7be130SAl Viro if (IS_MNT_SHARED(real_mount(old.mnt)) || 2483fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2484fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2485b12cea91SAl Viro goto out4; 2486143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2487b12cea91SAl Viro goto out4; 24881da177e4SLinus Torvalds error = -ENOENT; 2489f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2490b12cea91SAl Viro goto out4; 2491f3da392eSAlexey Dobriyan if (d_unlinked(old.dentry)) 2492b12cea91SAl Viro goto out4; 24931da177e4SLinus Torvalds error = -EBUSY; 24942d8f3038SAl Viro if (new.mnt == root.mnt || 24952d8f3038SAl Viro old.mnt == root.mnt) 2496b12cea91SAl Viro goto out4; /* loop, on the same file system */ 24971da177e4SLinus Torvalds error = -EINVAL; 24988c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2499b12cea91SAl Viro goto out4; /* not a mountpoint */ 2500676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2501b12cea91SAl Viro goto out4; /* not attached */ 25022d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2503b12cea91SAl Viro goto out4; /* not a mountpoint */ 2504676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2505b12cea91SAl Viro goto out4; /* not attached */ 25064ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 2507643822b4SAl Viro if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new)) 2508b12cea91SAl Viro goto out4; 2509962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2510419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2511419148daSAl Viro detach_mnt(root_mnt, &root_parent); 25124ac91378SJan Blunck /* mount old root on put_old */ 2513419148daSAl Viro attach_mnt(root_mnt, &old); 25144ac91378SJan Blunck /* mount new_root on / */ 2515419148daSAl Viro attach_mnt(new_mnt, &root_parent); 25166b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2517962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 25182d8f3038SAl Viro chroot_fs_refs(&root, &new); 25191da177e4SLinus Torvalds error = 0; 2520b12cea91SAl Viro out4: 2521b12cea91SAl Viro unlock_mount(&old); 2522b12cea91SAl Viro if (!error) { 25231a390689SAl Viro path_put(&root_parent); 25241a390689SAl Viro path_put(&parent_path); 2525b12cea91SAl Viro } 2526b12cea91SAl Viro out3: 25278c3ee42eSAl Viro path_put(&root); 2528b12cea91SAl Viro out2: 25292d8f3038SAl Viro path_put(&old); 25301da177e4SLinus Torvalds out1: 25312d8f3038SAl Viro path_put(&new); 25321da177e4SLinus Torvalds out0: 25331da177e4SLinus Torvalds return error; 25341da177e4SLinus Torvalds } 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds static void __init init_mount_tree(void) 25371da177e4SLinus Torvalds { 25381da177e4SLinus Torvalds struct vfsmount *mnt; 25396b3286edSKirill Korotaev struct mnt_namespace *ns; 2540ac748a09SJan Blunck struct path root; 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 25431da177e4SLinus Torvalds if (IS_ERR(mnt)) 25441da177e4SLinus Torvalds panic("Can't create rootfs"); 2545b3e19d92SNick Piggin 25463b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 25473b22edc5STrond Myklebust if (IS_ERR(ns)) 25481da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 25491da177e4SLinus Torvalds 25506b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 25516b3286edSKirill Korotaev get_mnt_ns(ns); 25521da177e4SLinus Torvalds 2553be08d6d2SAl Viro root.mnt = mnt; 2554be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2555ac748a09SJan Blunck 2556ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2557ac748a09SJan Blunck set_fs_root(current->fs, &root); 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds 256074bf17cfSDenis Cheng void __init mnt_init(void) 25611da177e4SLinus Torvalds { 256213f14b4dSEric Dumazet unsigned u; 256315a67dd8SRandy Dunlap int err; 25641da177e4SLinus Torvalds 2565390c6843SRam Pai init_rwsem(&namespace_sem); 2566390c6843SRam Pai 25677d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 256820c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 25691da177e4SLinus Torvalds 2570b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 25711da177e4SLinus Torvalds 25721da177e4SLinus Torvalds if (!mount_hashtable) 25731da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 25741da177e4SLinus Torvalds 257580cdc6daSMandeep Singh Baines printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE); 25761da177e4SLinus Torvalds 257713f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 257813f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 25791da177e4SLinus Torvalds 2580962830dfSAndi Kleen br_lock_init(&vfsmount_lock); 258199b7db7bSNick Piggin 258215a67dd8SRandy Dunlap err = sysfs_init(); 258315a67dd8SRandy Dunlap if (err) 258415a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 25858e24eea7SHarvey Harrison __func__, err); 258600d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 258700d26666SGreg Kroah-Hartman if (!fs_kobj) 25888e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 25891da177e4SLinus Torvalds init_rootfs(); 25901da177e4SLinus Torvalds init_mount_tree(); 25911da177e4SLinus Torvalds } 25921da177e4SLinus Torvalds 2593616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 25941da177e4SLinus Torvalds { 259570fbcdf4SRam Pai LIST_HEAD(umount_list); 2596616511d0STrond Myklebust 2597d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2598616511d0STrond Myklebust return; 2599390c6843SRam Pai down_write(&namespace_sem); 2600962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2601be08d6d2SAl Viro umount_tree(ns->root, 0, &umount_list); 2602962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2603390c6843SRam Pai up_write(&namespace_sem); 260470fbcdf4SRam Pai release_mounts(&umount_list); 26056b3286edSKirill Korotaev kfree(ns); 26061da177e4SLinus Torvalds } 26079d412a43SAl Viro 26089d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 26099d412a43SAl Viro { 2610423e0ab0STim Chen struct vfsmount *mnt; 2611423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2612423e0ab0STim Chen if (!IS_ERR(mnt)) { 2613423e0ab0STim Chen /* 2614423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2615423e0ab0STim Chen * we unmount before file sys is unregistered 2616423e0ab0STim Chen */ 2617423e0ab0STim Chen mnt_make_longterm(mnt); 2618423e0ab0STim Chen } 2619423e0ab0STim Chen return mnt; 26209d412a43SAl Viro } 26219d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2622423e0ab0STim Chen 2623423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2624423e0ab0STim Chen { 2625423e0ab0STim Chen /* release long term mount so mount point can be released */ 2626423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2627423e0ab0STim Chen mnt_make_shortterm(mnt); 2628423e0ab0STim Chen mntput(mnt); 2629423e0ab0STim Chen } 2630423e0ab0STim Chen } 2631423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 263202125a82SAl Viro 263302125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 263402125a82SAl Viro { 2635143c8c91SAl Viro return check_mnt(real_mount(mnt)); 263602125a82SAl Viro } 2637