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 /* 518f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 519f015f126SDavid Howells * 520f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 521f015f126SDavid Howells * following mounts: 522f015f126SDavid Howells * 523f015f126SDavid Howells * mount /dev/sda1 /mnt 524f015f126SDavid Howells * mount /dev/sda2 /mnt 525f015f126SDavid Howells * mount /dev/sda3 /mnt 526f015f126SDavid Howells * 527f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 528f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 529f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 530f015f126SDavid Howells * 531f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 532a05964f3SRam Pai */ 5331c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 534a05964f3SRam Pai { 535c7105365SAl Viro struct mount *child_mnt; 53699b7db7bSNick Piggin 537962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 538c7105365SAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry, 1); 539c7105365SAl Viro if (child_mnt) { 540c7105365SAl Viro mnt_add_count(child_mnt, 1); 541962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 542c7105365SAl Viro return &child_mnt->mnt; 543c7105365SAl Viro } else { 544962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 545c7105365SAl Viro return NULL; 546c7105365SAl Viro } 547a05964f3SRam Pai } 548a05964f3SRam Pai 549143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 5501da177e4SLinus Torvalds { 5516b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 55499b7db7bSNick Piggin /* 55599b7db7bSNick Piggin * vfsmount lock must be held for write 55699b7db7bSNick Piggin */ 5576b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 5585addc5ddSAl Viro { 5595addc5ddSAl Viro if (ns) { 5605addc5ddSAl Viro ns->event = ++event; 5615addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5625addc5ddSAl Viro } 5635addc5ddSAl Viro } 5645addc5ddSAl Viro 56599b7db7bSNick Piggin /* 56699b7db7bSNick Piggin * vfsmount lock must be held for write 56799b7db7bSNick Piggin */ 5686b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 5695addc5ddSAl Viro { 5705addc5ddSAl Viro if (ns && ns->event != event) { 5715addc5ddSAl Viro ns->event = event; 5725addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5735addc5ddSAl Viro } 5745addc5ddSAl Viro } 5755addc5ddSAl Viro 57699b7db7bSNick Piggin /* 5775f57cbccSNick Piggin * Clear dentry's mounted state if it has no remaining mounts. 5785f57cbccSNick Piggin * vfsmount_lock must be held for write. 5795f57cbccSNick Piggin */ 580aa0a4cf0SAl Viro static void dentry_reset_mounted(struct dentry *dentry) 5815f57cbccSNick Piggin { 5825f57cbccSNick Piggin unsigned u; 5835f57cbccSNick Piggin 5845f57cbccSNick Piggin for (u = 0; u < HASH_SIZE; u++) { 585d5e50f74SAl Viro struct mount *p; 5865f57cbccSNick Piggin 5871b8e5564SAl Viro list_for_each_entry(p, &mount_hashtable[u], mnt_hash) { 588a73324daSAl Viro if (p->mnt_mountpoint == dentry) 5895f57cbccSNick Piggin return; 5905f57cbccSNick Piggin } 5915f57cbccSNick Piggin } 5925f57cbccSNick Piggin spin_lock(&dentry->d_lock); 5935f57cbccSNick Piggin dentry->d_flags &= ~DCACHE_MOUNTED; 5945f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 5955f57cbccSNick Piggin } 5965f57cbccSNick Piggin 5975f57cbccSNick Piggin /* 59899b7db7bSNick Piggin * vfsmount lock must be held for write 59999b7db7bSNick Piggin */ 600419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 6011da177e4SLinus Torvalds { 602a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 6030714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 6040714a533SAl Viro mnt->mnt_parent = mnt; 605a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 6066b41d536SAl Viro list_del_init(&mnt->mnt_child); 6071b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 608aa0a4cf0SAl Viro dentry_reset_mounted(old_path->dentry); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 61199b7db7bSNick Piggin /* 61299b7db7bSNick Piggin * vfsmount lock must be held for write 61399b7db7bSNick Piggin */ 61414cf1fa8SAl Viro void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry, 61544d964d6SAl Viro struct mount *child_mnt) 616b90fa9aeSRam Pai { 6173a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 618a73324daSAl Viro child_mnt->mnt_mountpoint = dget(dentry); 6193a2393d7SAl Viro child_mnt->mnt_parent = mnt; 6205f57cbccSNick Piggin spin_lock(&dentry->d_lock); 6215f57cbccSNick Piggin dentry->d_flags |= DCACHE_MOUNTED; 6225f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 623b90fa9aeSRam Pai } 624b90fa9aeSRam Pai 62599b7db7bSNick Piggin /* 62699b7db7bSNick Piggin * vfsmount lock must be held for write 62799b7db7bSNick Piggin */ 628419148daSAl Viro static void attach_mnt(struct mount *mnt, struct path *path) 6291da177e4SLinus Torvalds { 63014cf1fa8SAl Viro mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt); 6311b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 6321a390689SAl Viro hash(path->mnt, path->dentry)); 6336b41d536SAl Viro list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts); 634b90fa9aeSRam Pai } 635b90fa9aeSRam Pai 636b90fa9aeSRam Pai /* 63799b7db7bSNick Piggin * vfsmount lock must be held for write 638b90fa9aeSRam Pai */ 6394b2619a5SAl Viro static void commit_tree(struct mount *mnt) 640b90fa9aeSRam Pai { 6410714a533SAl Viro struct mount *parent = mnt->mnt_parent; 64283adc753SAl Viro struct mount *m; 643b90fa9aeSRam Pai LIST_HEAD(head); 644143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 645b90fa9aeSRam Pai 6460714a533SAl Viro BUG_ON(parent == mnt); 647b90fa9aeSRam Pai 6481a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 649f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 650143c8c91SAl Viro m->mnt_ns = n; 651f03c6599SAl Viro 652b90fa9aeSRam Pai list_splice(&head, n->list.prev); 653b90fa9aeSRam Pai 6541b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 655a73324daSAl Viro hash(&parent->mnt, mnt->mnt_mountpoint)); 6566b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 6576b3286edSKirill Korotaev touch_mnt_namespace(n); 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 660909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 6611da177e4SLinus Torvalds { 6626b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 6636b41d536SAl Viro if (next == &p->mnt_mounts) { 6641da177e4SLinus Torvalds while (1) { 665909b0a88SAl Viro if (p == root) 6661da177e4SLinus Torvalds return NULL; 6676b41d536SAl Viro next = p->mnt_child.next; 6686b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 6691da177e4SLinus Torvalds break; 6700714a533SAl Viro p = p->mnt_parent; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds } 6736b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 676315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 6779676f0c6SRam Pai { 6786b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 6796b41d536SAl Viro while (prev != &p->mnt_mounts) { 6806b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 6816b41d536SAl Viro prev = p->mnt_mounts.prev; 6829676f0c6SRam Pai } 6839676f0c6SRam Pai return p; 6849676f0c6SRam Pai } 6859676f0c6SRam Pai 6869d412a43SAl Viro struct vfsmount * 6879d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 6889d412a43SAl Viro { 689b105e270SAl Viro struct mount *mnt; 6909d412a43SAl Viro struct dentry *root; 6919d412a43SAl Viro 6929d412a43SAl Viro if (!type) 6939d412a43SAl Viro return ERR_PTR(-ENODEV); 6949d412a43SAl Viro 6959d412a43SAl Viro mnt = alloc_vfsmnt(name); 6969d412a43SAl Viro if (!mnt) 6979d412a43SAl Viro return ERR_PTR(-ENOMEM); 6989d412a43SAl Viro 6999d412a43SAl Viro if (flags & MS_KERNMOUNT) 700b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 7019d412a43SAl Viro 7029d412a43SAl Viro root = mount_fs(type, flags, name, data); 7039d412a43SAl Viro if (IS_ERR(root)) { 7049d412a43SAl Viro free_vfsmnt(mnt); 7059d412a43SAl Viro return ERR_CAST(root); 7069d412a43SAl Viro } 7079d412a43SAl Viro 708b105e270SAl Viro mnt->mnt.mnt_root = root; 709b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 710a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7110714a533SAl Viro mnt->mnt_parent = mnt; 712962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 71339f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 714962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 715b105e270SAl Viro return &mnt->mnt; 7169d412a43SAl Viro } 7179d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 7189d412a43SAl Viro 71987129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 72036341f64SRam Pai int flag) 7211da177e4SLinus Torvalds { 72287129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 723be34d1a3SDavid Howells struct mount *mnt; 724be34d1a3SDavid Howells int err; 7251da177e4SLinus Torvalds 726be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 727be34d1a3SDavid Howells if (!mnt) 728be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 729be34d1a3SDavid Howells 730719f5d7fSMiklos Szeredi if (flag & (CL_SLAVE | CL_PRIVATE)) 73115169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 732719f5d7fSMiklos Szeredi else 73315169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 734719f5d7fSMiklos Szeredi 73515169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 736be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 737719f5d7fSMiklos Szeredi if (err) 738719f5d7fSMiklos Szeredi goto out_free; 739719f5d7fSMiklos Szeredi } 740719f5d7fSMiklos Szeredi 74187129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 7421da177e4SLinus Torvalds atomic_inc(&sb->s_active); 743b105e270SAl Viro mnt->mnt.mnt_sb = sb; 744b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 745a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7460714a533SAl Viro mnt->mnt_parent = mnt; 747962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 74839f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 749962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 750b90fa9aeSRam Pai 7515afe0022SRam Pai if (flag & CL_SLAVE) { 7526776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 75332301920SAl Viro mnt->mnt_master = old; 754fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 7558aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 756fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 7576776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 758d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 7596776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 760d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 7615afe0022SRam Pai } 762b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 7630f0afb1dSAl Viro set_mnt_shared(mnt); 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 7661da177e4SLinus Torvalds * as the original if that was on one */ 76736341f64SRam Pai if (flag & CL_EXPIRE) { 7686776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 7696776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 7701da177e4SLinus Torvalds } 771be34d1a3SDavid Howells 772cb338d06SAl Viro return mnt; 773719f5d7fSMiklos Szeredi 774719f5d7fSMiklos Szeredi out_free: 775719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 776be34d1a3SDavid Howells return ERR_PTR(err); 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 77983adc753SAl Viro static inline void mntfree(struct mount *mnt) 7801da177e4SLinus Torvalds { 78183adc753SAl Viro struct vfsmount *m = &mnt->mnt; 78283adc753SAl Viro struct super_block *sb = m->mnt_sb; 783b3e19d92SNick Piggin 7843d733633SDave Hansen /* 7853d733633SDave Hansen * This probably indicates that somebody messed 7863d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 7873d733633SDave Hansen * happens, the filesystem was probably unable 7883d733633SDave Hansen * to make r/w->r/o transitions. 7893d733633SDave Hansen */ 790d3ef3d73Snpiggin@suse.de /* 791b3e19d92SNick Piggin * The locking used to deal with mnt_count decrement provides barriers, 792b3e19d92SNick Piggin * so mnt_get_writers() below is safe. 793d3ef3d73Snpiggin@suse.de */ 794c6653a83SNick Piggin WARN_ON(mnt_get_writers(mnt)); 79583adc753SAl Viro fsnotify_vfsmount_delete(m); 79683adc753SAl Viro dput(m->mnt_root); 79783adc753SAl Viro free_vfsmnt(mnt); 7981da177e4SLinus Torvalds deactivate_super(sb); 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 801900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 8027b7b1aceSAl Viro { 803b3e19d92SNick Piggin put_again: 804f03c6599SAl Viro #ifdef CONFIG_SMP 805962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 806f7a99c5bSAl Viro if (likely(mnt->mnt_ns)) { 807f7a99c5bSAl Viro /* shouldn't be the last one */ 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 { 9426ce6e24eSAl Viro struct proc_mounts *p = 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 { 9506ce6e24eSAl Viro struct proc_mounts *p = 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 { 9626ce6e24eSAl Viro struct proc_mounts *p = 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); 107763d37a84SAl Viro p->mnt_ns = NULL; 10786b41d536SAl Viro list_del_init(&p->mnt_child); 1079676da58dSAl Viro if (mnt_has_parent(p)) { 1080863d684fSAl Viro p->mnt_parent->mnt_ghosts++; 1081a73324daSAl Viro dentry_reset_mounted(p->mnt_mountpoint); 10827c4b93d8SAl Viro } 10830f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 10841da177e4SLinus Torvalds } 10857b8a53fdSAl Viro list_splice(&tmp_list, kill); 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 1088692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts); 1089c35038beSAl Viro 10901ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 10911da177e4SLinus Torvalds { 10921ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 10931da177e4SLinus Torvalds int retval; 109470fbcdf4SRam Pai LIST_HEAD(umount_list); 10951da177e4SLinus Torvalds 10961ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 10971da177e4SLinus Torvalds if (retval) 10981da177e4SLinus Torvalds return retval; 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* 11011da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 11021da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 11031da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 11041da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 11051da177e4SLinus Torvalds */ 11061da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 11071ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 11081da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 11091da177e4SLinus Torvalds return -EINVAL; 11101da177e4SLinus Torvalds 1111b3e19d92SNick Piggin /* 1112b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1113b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1114b3e19d92SNick Piggin */ 1115962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 111683adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1117962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11181da177e4SLinus Torvalds return -EBUSY; 1119b3e19d92SNick Piggin } 1120962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11211da177e4SLinus Torvalds 1122863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 11231da177e4SLinus Torvalds return -EAGAIN; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds /* 11271da177e4SLinus Torvalds * If we may have to abort operations to get out of this 11281da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 11291da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 11301da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 11311da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 11321da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 11331da177e4SLinus Torvalds * about for the moment. 11341da177e4SLinus Torvalds */ 11351da177e4SLinus Torvalds 113642faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 113742faad99SAl Viro sb->s_op->umount_begin(sb); 113842faad99SAl Viro } 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds /* 11411da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 11421da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 11431da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 11441da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 11451da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 11461da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 11471da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 11481da177e4SLinus Torvalds */ 11491ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 11501da177e4SLinus Torvalds /* 11511da177e4SLinus Torvalds * Special case for "unmounting" root ... 11521da177e4SLinus Torvalds * we just try to remount it readonly. 11531da177e4SLinus Torvalds */ 11541da177e4SLinus Torvalds down_write(&sb->s_umount); 11554aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 11561da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 11571da177e4SLinus Torvalds up_write(&sb->s_umount); 11581da177e4SLinus Torvalds return retval; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds 1161390c6843SRam Pai down_write(&namespace_sem); 1162962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 11635addc5ddSAl Viro event++; 11641da177e4SLinus Torvalds 1165c35038beSAl Viro if (!(flags & MNT_DETACH)) 11661ab59738SAl Viro shrink_submounts(mnt, &umount_list); 1167c35038beSAl Viro 11681da177e4SLinus Torvalds retval = -EBUSY; 1169a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 11701a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 11711ab59738SAl Viro umount_tree(mnt, 1, &umount_list); 11721da177e4SLinus Torvalds retval = 0; 11731da177e4SLinus Torvalds } 1174962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1175390c6843SRam Pai up_write(&namespace_sem); 117670fbcdf4SRam Pai release_mounts(&umount_list); 11771da177e4SLinus Torvalds return retval; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds /* 11811da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 11821da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 11831da177e4SLinus Torvalds * 11841da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 11851da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 11861da177e4SLinus Torvalds */ 11871da177e4SLinus Torvalds 1188bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 11891da177e4SLinus Torvalds { 11902d8f3038SAl Viro struct path path; 1191900148dcSAl Viro struct mount *mnt; 11921da177e4SLinus Torvalds int retval; 1193db1f05bbSMiklos Szeredi int lookup_flags = 0; 11941da177e4SLinus Torvalds 1195db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1196db1f05bbSMiklos Szeredi return -EINVAL; 1197db1f05bbSMiklos Szeredi 1198db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1199db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1200db1f05bbSMiklos Szeredi 1201db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 12021da177e4SLinus Torvalds if (retval) 12031da177e4SLinus Torvalds goto out; 1204900148dcSAl Viro mnt = real_mount(path.mnt); 12051da177e4SLinus Torvalds retval = -EINVAL; 12062d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 12071da177e4SLinus Torvalds goto dput_and_out; 1208143c8c91SAl Viro if (!check_mnt(mnt)) 12091da177e4SLinus Torvalds goto dput_and_out; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds retval = -EPERM; 12121da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 12131da177e4SLinus Torvalds goto dput_and_out; 12141da177e4SLinus Torvalds 1215900148dcSAl Viro retval = do_umount(mnt, flags); 12161da177e4SLinus Torvalds dput_and_out: 1217429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 12182d8f3038SAl Viro dput(path.dentry); 1219900148dcSAl Viro mntput_no_expire(mnt); 12201da177e4SLinus Torvalds out: 12211da177e4SLinus Torvalds return retval; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds /* 12271da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 12281da177e4SLinus Torvalds */ 1229bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds return sys_umount(name, 0); 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds #endif 12351da177e4SLinus Torvalds 12362d92ab3cSAl Viro static int mount_is_safe(struct path *path) 12371da177e4SLinus Torvalds { 12381da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 12391da177e4SLinus Torvalds return 0; 12401da177e4SLinus Torvalds return -EPERM; 12411da177e4SLinus Torvalds #ifdef notyet 12422d92ab3cSAl Viro if (S_ISLNK(path->dentry->d_inode->i_mode)) 12431da177e4SLinus Torvalds return -EPERM; 12442d92ab3cSAl Viro if (path->dentry->d_inode->i_mode & S_ISVTX) { 1245da9592edSDavid Howells if (current_uid() != path->dentry->d_inode->i_uid) 12461da177e4SLinus Torvalds return -EPERM; 12471da177e4SLinus Torvalds } 12482d92ab3cSAl Viro if (inode_permission(path->dentry->d_inode, MAY_WRITE)) 12491da177e4SLinus Torvalds return -EPERM; 12501da177e4SLinus Torvalds return 0; 12511da177e4SLinus Torvalds #endif 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 125487129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 125536341f64SRam Pai int flag) 12561da177e4SLinus Torvalds { 1257a73324daSAl Viro struct mount *res, *p, *q, *r; 12581a390689SAl Viro struct path path; 12591da177e4SLinus Torvalds 1260fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 1261be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 12629676f0c6SRam Pai 126336341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1264be34d1a3SDavid Howells if (IS_ERR(q)) 1265be34d1a3SDavid Howells return q; 1266be34d1a3SDavid Howells 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); 1288be34d1a3SDavid Howells if (IS_ERR(q)) 1289be34d1a3SDavid Howells goto out; 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; 1297be34d1a3SDavid Howells out: 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 } 1305be34d1a3SDavid Howells return q; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 1308be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1309be34d1a3SDavid Howells 1310589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 13118aec0809SAl Viro { 1312cb338d06SAl Viro struct mount *tree; 13131a60a280SAl Viro down_write(&namespace_sem); 131487129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 131587129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 13161a60a280SAl Viro up_write(&namespace_sem); 1317be34d1a3SDavid Howells if (IS_ERR(tree)) 1318be34d1a3SDavid Howells return NULL; 1319be34d1a3SDavid Howells return &tree->mnt; 13208aec0809SAl Viro } 13218aec0809SAl Viro 13228aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 13238aec0809SAl Viro { 13248aec0809SAl Viro LIST_HEAD(umount_list); 13251a60a280SAl Viro down_write(&namespace_sem); 1326962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1327761d5c38SAl Viro umount_tree(real_mount(mnt), 0, &umount_list); 1328962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 13291a60a280SAl Viro up_write(&namespace_sem); 13308aec0809SAl Viro release_mounts(&umount_list); 13318aec0809SAl Viro } 13328aec0809SAl Viro 13331f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 13341f707137SAl Viro struct vfsmount *root) 13351f707137SAl Viro { 13361a4eeaf2SAl Viro struct mount *mnt; 13371f707137SAl Viro int res = f(root, arg); 13381f707137SAl Viro if (res) 13391f707137SAl Viro return res; 13401a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 13411a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 13421f707137SAl Viro if (res) 13431f707137SAl Viro return res; 13441f707137SAl Viro } 13451f707137SAl Viro return 0; 13461f707137SAl Viro } 13471f707137SAl Viro 13484b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1349719f5d7fSMiklos Szeredi { 1350315fc83eSAl Viro struct mount *p; 1351719f5d7fSMiklos Szeredi 1352909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1353fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 13544b8b21f4SAl Viro mnt_release_group_id(p); 1355719f5d7fSMiklos Szeredi } 1356719f5d7fSMiklos Szeredi } 1357719f5d7fSMiklos Szeredi 13584b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1359719f5d7fSMiklos Szeredi { 1360315fc83eSAl Viro struct mount *p; 1361719f5d7fSMiklos Szeredi 1362909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1363fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 13644b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1365719f5d7fSMiklos Szeredi if (err) { 13664b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1367719f5d7fSMiklos Szeredi return err; 1368719f5d7fSMiklos Szeredi } 1369719f5d7fSMiklos Szeredi } 1370719f5d7fSMiklos Szeredi } 1371719f5d7fSMiklos Szeredi 1372719f5d7fSMiklos Szeredi return 0; 1373719f5d7fSMiklos Szeredi } 1374719f5d7fSMiklos Szeredi 1375b90fa9aeSRam Pai /* 1376b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1377b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 137821444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 137921444403SRam Pai * store the parent mount and mountpoint dentry. 138021444403SRam Pai * (done when source_mnt is moved) 1381b90fa9aeSRam Pai * 1382b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1383b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 13849676f0c6SRam Pai * --------------------------------------------------------------------------- 1385b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 13869676f0c6SRam Pai * |************************************************************************** 13879676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13889676f0c6SRam Pai * | dest | | | | | 13899676f0c6SRam Pai * | | | | | | | 13909676f0c6SRam Pai * | v | | | | | 13919676f0c6SRam Pai * |************************************************************************** 13929676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 13935afe0022SRam Pai * | | | | | | 13949676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 13959676f0c6SRam Pai * *************************************************************************** 1396b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1397b90fa9aeSRam Pai * destination mount. 1398b90fa9aeSRam Pai * 1399b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1400b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1401b90fa9aeSRam Pai * the peer group of the source mount. 1402b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1403b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1404b90fa9aeSRam Pai * mount. 14055afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 14065afe0022SRam Pai * of the destination mount and the cloned mount is made slave 14075afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 14085afe0022SRam Pai * is marked as 'shared and slave'. 14095afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 14105afe0022SRam Pai * source mount. 14115afe0022SRam Pai * 14129676f0c6SRam Pai * --------------------------------------------------------------------------- 141321444403SRam Pai * | MOVE MOUNT OPERATION | 14149676f0c6SRam Pai * |************************************************************************** 14159676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 14169676f0c6SRam Pai * | dest | | | | | 14179676f0c6SRam Pai * | | | | | | | 14189676f0c6SRam Pai * | v | | | | | 14199676f0c6SRam Pai * |************************************************************************** 14209676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 14215afe0022SRam Pai * | | | | | | 14229676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 14239676f0c6SRam Pai * *************************************************************************** 14245afe0022SRam Pai * 14255afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 14265afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 142721444403SRam Pai * (+*) the mount is moved to the destination. 14285afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 14295afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 14305afe0022SRam Pai * the mount is marked as 'shared and slave'. 14315afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1432b90fa9aeSRam Pai * 1433b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1434b90fa9aeSRam Pai * applied to each mount in the tree. 1435b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1436b90fa9aeSRam Pai * in allocations. 1437b90fa9aeSRam Pai */ 14380fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 14391a390689SAl Viro struct path *path, struct path *parent_path) 1440b90fa9aeSRam Pai { 1441b90fa9aeSRam Pai LIST_HEAD(tree_list); 1442a8d56d8eSAl Viro struct mount *dest_mnt = real_mount(path->mnt); 14431a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1444315fc83eSAl Viro struct mount *child, *p; 1445719f5d7fSMiklos Szeredi int err; 1446b90fa9aeSRam Pai 1447fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 14480fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1449719f5d7fSMiklos Szeredi if (err) 1450719f5d7fSMiklos Szeredi goto out; 1451719f5d7fSMiklos Szeredi } 1452a8d56d8eSAl Viro err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); 1453719f5d7fSMiklos Szeredi if (err) 1454719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1455b90fa9aeSRam Pai 1456962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1457df1a1ad2SAl Viro 1458fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1459909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 14600f0afb1dSAl Viro set_mnt_shared(p); 1461b90fa9aeSRam Pai } 14621a390689SAl Viro if (parent_path) { 14630fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 14640fb54e50SAl Viro attach_mnt(source_mnt, path); 1465143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 146621444403SRam Pai } else { 146714cf1fa8SAl Viro mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 14680fb54e50SAl Viro commit_tree(source_mnt); 146921444403SRam Pai } 1470b90fa9aeSRam Pai 14711b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 14721b8e5564SAl Viro list_del_init(&child->mnt_hash); 14734b2619a5SAl Viro commit_tree(child); 1474b90fa9aeSRam Pai } 1475962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 147699b7db7bSNick Piggin 1477b90fa9aeSRam Pai return 0; 1478719f5d7fSMiklos Szeredi 1479719f5d7fSMiklos Szeredi out_cleanup_ids: 1480fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 14810fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1482719f5d7fSMiklos Szeredi out: 1483719f5d7fSMiklos Szeredi return err; 1484b90fa9aeSRam Pai } 1485b90fa9aeSRam Pai 1486b12cea91SAl Viro static int lock_mount(struct path *path) 1487b12cea91SAl Viro { 1488b12cea91SAl Viro struct vfsmount *mnt; 1489b12cea91SAl Viro retry: 1490b12cea91SAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1491b12cea91SAl Viro if (unlikely(cant_mount(path->dentry))) { 1492b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1493b12cea91SAl Viro return -ENOENT; 1494b12cea91SAl Viro } 1495b12cea91SAl Viro down_write(&namespace_sem); 1496b12cea91SAl Viro mnt = lookup_mnt(path); 1497b12cea91SAl Viro if (likely(!mnt)) 1498b12cea91SAl Viro return 0; 1499b12cea91SAl Viro up_write(&namespace_sem); 1500b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1501b12cea91SAl Viro path_put(path); 1502b12cea91SAl Viro path->mnt = mnt; 1503b12cea91SAl Viro path->dentry = dget(mnt->mnt_root); 1504b12cea91SAl Viro goto retry; 1505b12cea91SAl Viro } 1506b12cea91SAl Viro 1507b12cea91SAl Viro static void unlock_mount(struct path *path) 1508b12cea91SAl Viro { 1509b12cea91SAl Viro up_write(&namespace_sem); 1510b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1511b12cea91SAl Viro } 1512b12cea91SAl Viro 151395bc5f25SAl Viro static int graft_tree(struct mount *mnt, struct path *path) 15141da177e4SLinus Torvalds { 151595bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 15161da177e4SLinus Torvalds return -EINVAL; 15171da177e4SLinus Torvalds 15188c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 151995bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 15201da177e4SLinus Torvalds return -ENOTDIR; 15211da177e4SLinus Torvalds 1522b12cea91SAl Viro if (d_unlinked(path->dentry)) 1523b12cea91SAl Viro return -ENOENT; 15241da177e4SLinus Torvalds 152595bc5f25SAl Viro return attach_recursive_mnt(mnt, path, NULL); 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds /* 15297a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 15307a2e8a8fSValerie Aurora */ 15317a2e8a8fSValerie Aurora 15327a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 15337a2e8a8fSValerie Aurora { 15347c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 15357a2e8a8fSValerie Aurora 15367a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 15377a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 15387a2e8a8fSValerie Aurora return 0; 15397a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 15407a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 15417a2e8a8fSValerie Aurora return 0; 15427a2e8a8fSValerie Aurora return type; 15437a2e8a8fSValerie Aurora } 15447a2e8a8fSValerie Aurora 15457a2e8a8fSValerie Aurora /* 154607b20889SRam Pai * recursively change the type of the mountpoint. 154707b20889SRam Pai */ 15480a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 154907b20889SRam Pai { 1550315fc83eSAl Viro struct mount *m; 15514b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 155207b20889SRam Pai int recurse = flag & MS_REC; 15537a2e8a8fSValerie Aurora int type; 1554719f5d7fSMiklos Szeredi int err = 0; 155507b20889SRam Pai 1556ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 1557ee6f9582SMiklos Szeredi return -EPERM; 1558ee6f9582SMiklos Szeredi 15592d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 156007b20889SRam Pai return -EINVAL; 156107b20889SRam Pai 15627a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 15637a2e8a8fSValerie Aurora if (!type) 15647a2e8a8fSValerie Aurora return -EINVAL; 15657a2e8a8fSValerie Aurora 156607b20889SRam Pai down_write(&namespace_sem); 1567719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1568719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1569719f5d7fSMiklos Szeredi if (err) 1570719f5d7fSMiklos Szeredi goto out_unlock; 1571719f5d7fSMiklos Szeredi } 1572719f5d7fSMiklos Szeredi 1573962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1574909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 15750f0afb1dSAl Viro change_mnt_propagation(m, type); 1576962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1577719f5d7fSMiklos Szeredi 1578719f5d7fSMiklos Szeredi out_unlock: 157907b20889SRam Pai up_write(&namespace_sem); 1580719f5d7fSMiklos Szeredi return err; 158107b20889SRam Pai } 158207b20889SRam Pai 158307b20889SRam Pai /* 15841da177e4SLinus Torvalds * do loopback mount. 15851da177e4SLinus Torvalds */ 15860a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name, 15872dafe1c4SEric Sandeen int recurse) 15881da177e4SLinus Torvalds { 1589b12cea91SAl Viro LIST_HEAD(umount_list); 15902d92ab3cSAl Viro struct path old_path; 159187129cc0SAl Viro struct mount *mnt = NULL, *old; 15922d92ab3cSAl Viro int err = mount_is_safe(path); 15931da177e4SLinus Torvalds if (err) 15941da177e4SLinus Torvalds return err; 15951da177e4SLinus Torvalds if (!old_name || !*old_name) 15961da177e4SLinus Torvalds return -EINVAL; 1597815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 15981da177e4SLinus Torvalds if (err) 15991da177e4SLinus Torvalds return err; 16001da177e4SLinus Torvalds 1601b12cea91SAl Viro err = lock_mount(path); 1602b12cea91SAl Viro if (err) 16039676f0c6SRam Pai goto out; 16049676f0c6SRam Pai 160587129cc0SAl Viro old = real_mount(old_path.mnt); 160687129cc0SAl Viro 1607b12cea91SAl Viro err = -EINVAL; 1608fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1609b12cea91SAl Viro goto out2; 1610b12cea91SAl Viro 1611143c8c91SAl Viro if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old)) 1612b12cea91SAl Viro goto out2; 1613ccd48bc7SAl Viro 16141da177e4SLinus Torvalds if (recurse) 161587129cc0SAl Viro mnt = copy_tree(old, old_path.dentry, 0); 16161da177e4SLinus Torvalds else 161787129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 16181da177e4SLinus Torvalds 1619be34d1a3SDavid Howells if (IS_ERR(mnt)) { 1620be34d1a3SDavid Howells err = PTR_ERR(mnt); 1621be34d1a3SDavid Howells goto out; 1622be34d1a3SDavid Howells } 1623ccd48bc7SAl Viro 162495bc5f25SAl Viro err = graft_tree(mnt, path); 16251da177e4SLinus Torvalds if (err) { 1626962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1627761d5c38SAl Viro umount_tree(mnt, 0, &umount_list); 1628962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 16295b83d2c5SRam Pai } 1630b12cea91SAl Viro out2: 1631b12cea91SAl Viro unlock_mount(path); 1632b12cea91SAl Viro release_mounts(&umount_list); 1633ccd48bc7SAl Viro out: 16342d92ab3cSAl Viro path_put(&old_path); 16351da177e4SLinus Torvalds return err; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16382e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 16392e4b7fcdSDave Hansen { 16402e4b7fcdSDave Hansen int error = 0; 16412e4b7fcdSDave Hansen int readonly_request = 0; 16422e4b7fcdSDave Hansen 16432e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 16442e4b7fcdSDave Hansen readonly_request = 1; 16452e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 16462e4b7fcdSDave Hansen return 0; 16472e4b7fcdSDave Hansen 16482e4b7fcdSDave Hansen if (readonly_request) 164983adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 16502e4b7fcdSDave Hansen else 165183adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 16522e4b7fcdSDave Hansen return error; 16532e4b7fcdSDave Hansen } 16542e4b7fcdSDave Hansen 16551da177e4SLinus Torvalds /* 16561da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 16571da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 16581da177e4SLinus Torvalds * on it - tough luck. 16591da177e4SLinus Torvalds */ 16600a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 16611da177e4SLinus Torvalds void *data) 16621da177e4SLinus Torvalds { 16631da177e4SLinus Torvalds int err; 16642d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1665143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16681da177e4SLinus Torvalds return -EPERM; 16691da177e4SLinus Torvalds 1670143c8c91SAl Viro if (!check_mnt(mnt)) 16711da177e4SLinus Torvalds return -EINVAL; 16721da177e4SLinus Torvalds 16732d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 16741da177e4SLinus Torvalds return -EINVAL; 16751da177e4SLinus Torvalds 1676ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1677ff36fe2cSEric Paris if (err) 1678ff36fe2cSEric Paris return err; 1679ff36fe2cSEric Paris 16801da177e4SLinus Torvalds down_write(&sb->s_umount); 16812e4b7fcdSDave Hansen if (flags & MS_BIND) 16822d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 16834aa98cf7SAl Viro else 16841da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 16857b43a79fSAl Viro if (!err) { 1686962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1687143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1688143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1689962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 16907b43a79fSAl Viro } 16911da177e4SLinus Torvalds up_write(&sb->s_umount); 16920e55a7ccSDan Williams if (!err) { 1693962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1694143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1695962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 16960e55a7ccSDan Williams } 16971da177e4SLinus Torvalds return err; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 1700cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 17019676f0c6SRam Pai { 1702315fc83eSAl Viro struct mount *p; 1703909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1704fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 17059676f0c6SRam Pai return 1; 17069676f0c6SRam Pai } 17079676f0c6SRam Pai return 0; 17089676f0c6SRam Pai } 17099676f0c6SRam Pai 17100a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name) 17111da177e4SLinus Torvalds { 17122d92ab3cSAl Viro struct path old_path, parent_path; 1713676da58dSAl Viro struct mount *p; 17140fb54e50SAl Viro struct mount *old; 17151da177e4SLinus Torvalds int err = 0; 17161da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17171da177e4SLinus Torvalds return -EPERM; 17181da177e4SLinus Torvalds if (!old_name || !*old_name) 17191da177e4SLinus Torvalds return -EINVAL; 17202d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 17211da177e4SLinus Torvalds if (err) 17221da177e4SLinus Torvalds return err; 17231da177e4SLinus Torvalds 1724b12cea91SAl Viro err = lock_mount(path); 1725cc53ce53SDavid Howells if (err < 0) 1726cc53ce53SDavid Howells goto out; 1727cc53ce53SDavid Howells 1728143c8c91SAl Viro old = real_mount(old_path.mnt); 1729fc7be130SAl Viro p = real_mount(path->mnt); 1730143c8c91SAl Viro 17311da177e4SLinus Torvalds err = -EINVAL; 1732fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 17331da177e4SLinus Torvalds goto out1; 17341da177e4SLinus Torvalds 1735f3da392eSAlexey Dobriyan if (d_unlinked(path->dentry)) 173621444403SRam Pai goto out1; 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds err = -EINVAL; 17392d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 174021444403SRam Pai goto out1; 17411da177e4SLinus Torvalds 1742676da58dSAl Viro if (!mnt_has_parent(old)) 174321444403SRam Pai goto out1; 17441da177e4SLinus Torvalds 17452d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 17462d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 174721444403SRam Pai goto out1; 174821444403SRam Pai /* 174921444403SRam Pai * Don't move a mount residing in a shared parent. 175021444403SRam Pai */ 1751fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 175221444403SRam Pai goto out1; 17539676f0c6SRam Pai /* 17549676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 17559676f0c6SRam Pai * mount which is shared. 17569676f0c6SRam Pai */ 1757fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 17589676f0c6SRam Pai goto out1; 17591da177e4SLinus Torvalds err = -ELOOP; 1760fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1761676da58dSAl Viro if (p == old) 176221444403SRam Pai goto out1; 17631da177e4SLinus Torvalds 17640fb54e50SAl Viro err = attach_recursive_mnt(old, path, &parent_path); 17654ac91378SJan Blunck if (err) 176621444403SRam Pai goto out1; 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 17691da177e4SLinus Torvalds * automatically */ 17706776db3dSAl Viro list_del_init(&old->mnt_expire); 17711da177e4SLinus Torvalds out1: 1772b12cea91SAl Viro unlock_mount(path); 17731da177e4SLinus Torvalds out: 17741da177e4SLinus Torvalds if (!err) 17751a390689SAl Viro path_put(&parent_path); 17762d92ab3cSAl Viro path_put(&old_path); 17771da177e4SLinus Torvalds return err; 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 17809d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 17819d412a43SAl Viro { 17829d412a43SAl Viro int err; 17839d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 17849d412a43SAl Viro if (subtype) { 17859d412a43SAl Viro subtype++; 17869d412a43SAl Viro err = -EINVAL; 17879d412a43SAl Viro if (!subtype[0]) 17889d412a43SAl Viro goto err; 17899d412a43SAl Viro } else 17909d412a43SAl Viro subtype = ""; 17919d412a43SAl Viro 17929d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 17939d412a43SAl Viro err = -ENOMEM; 17949d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 17959d412a43SAl Viro goto err; 17969d412a43SAl Viro return mnt; 17979d412a43SAl Viro 17989d412a43SAl Viro err: 17999d412a43SAl Viro mntput(mnt); 18009d412a43SAl Viro return ERR_PTR(err); 18019d412a43SAl Viro } 18029d412a43SAl Viro 180379e801a9SAl Viro static struct vfsmount * 18049d412a43SAl Viro do_kern_mount(const char *fstype, int flags, const char *name, void *data) 18059d412a43SAl Viro { 18069d412a43SAl Viro struct file_system_type *type = get_fs_type(fstype); 18079d412a43SAl Viro struct vfsmount *mnt; 18089d412a43SAl Viro if (!type) 18099d412a43SAl Viro return ERR_PTR(-ENODEV); 18109d412a43SAl Viro mnt = vfs_kern_mount(type, flags, name, data); 18119d412a43SAl Viro if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 18129d412a43SAl Viro !mnt->mnt_sb->s_subtype) 18139d412a43SAl Viro mnt = fs_set_subtype(mnt, fstype); 18149d412a43SAl Viro put_filesystem(type); 18159d412a43SAl Viro return mnt; 18169d412a43SAl Viro } 18179d412a43SAl Viro 18189d412a43SAl Viro /* 18199d412a43SAl Viro * add a mount into a namespace's mount tree 18209d412a43SAl Viro */ 182195bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 18229d412a43SAl Viro { 18239d412a43SAl Viro int err; 18249d412a43SAl Viro 18259d412a43SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 18269d412a43SAl Viro 1827b12cea91SAl Viro err = lock_mount(path); 1828b12cea91SAl Viro if (err) 1829b12cea91SAl Viro return err; 18309d412a43SAl Viro 18319d412a43SAl Viro err = -EINVAL; 1832143c8c91SAl Viro if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt))) 18339d412a43SAl Viro goto unlock; 18349d412a43SAl Viro 18359d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 18369d412a43SAl Viro err = -EBUSY; 183795bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 18389d412a43SAl Viro path->mnt->mnt_root == path->dentry) 18399d412a43SAl Viro goto unlock; 18409d412a43SAl Viro 18419d412a43SAl Viro err = -EINVAL; 184295bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 18439d412a43SAl Viro goto unlock; 18449d412a43SAl Viro 184595bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 18469d412a43SAl Viro err = graft_tree(newmnt, path); 18479d412a43SAl Viro 18489d412a43SAl Viro unlock: 1849b12cea91SAl Viro unlock_mount(path); 18509d412a43SAl Viro return err; 18519d412a43SAl Viro } 1852b1e75df4SAl Viro 18531da177e4SLinus Torvalds /* 18541da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 18551da177e4SLinus Torvalds * namespace's tree 18561da177e4SLinus Torvalds */ 18570a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags, 18581da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds struct vfsmount *mnt; 186115f9a3f3SAl Viro int err; 18621da177e4SLinus Torvalds 1863eca6f534SVegard Nossum if (!type) 18641da177e4SLinus Torvalds return -EINVAL; 18651da177e4SLinus Torvalds 18661da177e4SLinus Torvalds /* we need capabilities... */ 18671da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 18681da177e4SLinus Torvalds return -EPERM; 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 18711da177e4SLinus Torvalds if (IS_ERR(mnt)) 18721da177e4SLinus Torvalds return PTR_ERR(mnt); 18731da177e4SLinus Torvalds 187495bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 187515f9a3f3SAl Viro if (err) 187615f9a3f3SAl Viro mntput(mnt); 187715f9a3f3SAl Viro return err; 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 188019a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 188119a167afSAl Viro { 18826776db3dSAl Viro struct mount *mnt = real_mount(m); 188319a167afSAl Viro int err; 188419a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 188519a167afSAl Viro * expired before we get a chance to add it 188619a167afSAl Viro */ 18876776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 188819a167afSAl Viro 188919a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 189019a167afSAl Viro m->mnt_root == path->dentry) { 1891b1e75df4SAl Viro err = -ELOOP; 1892b1e75df4SAl Viro goto fail; 189319a167afSAl Viro } 189419a167afSAl Viro 189595bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 1896b1e75df4SAl Viro if (!err) 1897b1e75df4SAl Viro return 0; 1898b1e75df4SAl Viro fail: 1899b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 19006776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 1901b1e75df4SAl Viro down_write(&namespace_sem); 1902962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 19036776db3dSAl Viro list_del_init(&mnt->mnt_expire); 1904962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1905b1e75df4SAl Viro up_write(&namespace_sem); 190619a167afSAl Viro } 1907b1e75df4SAl Viro mntput(m); 1908b1e75df4SAl Viro mntput(m); 190919a167afSAl Viro return err; 191019a167afSAl Viro } 191119a167afSAl Viro 1912ea5b778aSDavid Howells /** 1913ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 1914ea5b778aSDavid Howells * @mnt: The mount to list. 1915ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 1916ea5b778aSDavid Howells */ 1917ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 1918ea5b778aSDavid Howells { 1919ea5b778aSDavid Howells down_write(&namespace_sem); 1920962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1921ea5b778aSDavid Howells 19226776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 1923ea5b778aSDavid Howells 1924962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1925ea5b778aSDavid Howells up_write(&namespace_sem); 1926ea5b778aSDavid Howells } 1927ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 1928ea5b778aSDavid Howells 1929ea5b778aSDavid Howells /* 19301da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 19311da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 19321da177e4SLinus Torvalds * here 19331da177e4SLinus Torvalds */ 19341da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 19351da177e4SLinus Torvalds { 1936761d5c38SAl Viro struct mount *mnt, *next; 19371da177e4SLinus Torvalds LIST_HEAD(graveyard); 1938bcc5c7d2SAl Viro LIST_HEAD(umounts); 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds if (list_empty(mounts)) 19411da177e4SLinus Torvalds return; 19421da177e4SLinus Torvalds 1943bcc5c7d2SAl Viro down_write(&namespace_sem); 1944962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 19451da177e4SLinus Torvalds 19461da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 19471da177e4SLinus Torvalds * following criteria: 19481da177e4SLinus Torvalds * - only referenced by its parent vfsmount 19491da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 19501da177e4SLinus Torvalds * cleared by mntput()) 19511da177e4SLinus Torvalds */ 19526776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 1953863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 19541ab59738SAl Viro propagate_mount_busy(mnt, 1)) 19551da177e4SLinus Torvalds continue; 19566776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 19571da177e4SLinus Torvalds } 1958bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 19596776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 1960143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1961bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 1962bcc5c7d2SAl Viro } 1963962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1964bcc5c7d2SAl Viro up_write(&namespace_sem); 1965bcc5c7d2SAl Viro 1966bcc5c7d2SAl Viro release_mounts(&umounts); 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds /* 19725528f911STrond Myklebust * Ripoff of 'select_parent()' 19735528f911STrond Myklebust * 19745528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 19755528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 19765528f911STrond Myklebust */ 1977692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 19785528f911STrond Myklebust { 1979692afc31SAl Viro struct mount *this_parent = parent; 19805528f911STrond Myklebust struct list_head *next; 19815528f911STrond Myklebust int found = 0; 19825528f911STrond Myklebust 19835528f911STrond Myklebust repeat: 19846b41d536SAl Viro next = this_parent->mnt_mounts.next; 19855528f911STrond Myklebust resume: 19866b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 19875528f911STrond Myklebust struct list_head *tmp = next; 19886b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 19895528f911STrond Myklebust 19905528f911STrond Myklebust next = tmp->next; 1991692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 19925528f911STrond Myklebust continue; 19935528f911STrond Myklebust /* 19945528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 19955528f911STrond Myklebust */ 19966b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 19975528f911STrond Myklebust this_parent = mnt; 19985528f911STrond Myklebust goto repeat; 19995528f911STrond Myklebust } 20005528f911STrond Myklebust 20011ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 20026776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 20035528f911STrond Myklebust found++; 20045528f911STrond Myklebust } 20055528f911STrond Myklebust } 20065528f911STrond Myklebust /* 20075528f911STrond Myklebust * All done at this level ... ascend and resume the search 20085528f911STrond Myklebust */ 20095528f911STrond Myklebust if (this_parent != parent) { 20106b41d536SAl Viro next = this_parent->mnt_child.next; 20110714a533SAl Viro this_parent = this_parent->mnt_parent; 20125528f911STrond Myklebust goto resume; 20135528f911STrond Myklebust } 20145528f911STrond Myklebust return found; 20155528f911STrond Myklebust } 20165528f911STrond Myklebust 20175528f911STrond Myklebust /* 20185528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 20195528f911STrond Myklebust * submounts of a specific parent mountpoint 202099b7db7bSNick Piggin * 202199b7db7bSNick Piggin * vfsmount_lock must be held for write 20225528f911STrond Myklebust */ 2023692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts) 20245528f911STrond Myklebust { 20255528f911STrond Myklebust LIST_HEAD(graveyard); 2026761d5c38SAl Viro struct mount *m; 20275528f911STrond Myklebust 20285528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2029c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2030bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2031761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 20326776db3dSAl Viro mnt_expire); 2033143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2034afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 2035bcc5c7d2SAl Viro } 2036bcc5c7d2SAl Viro } 20375528f911STrond Myklebust } 20385528f911STrond Myklebust 20395528f911STrond Myklebust /* 20401da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 20411da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 20421da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 20431da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 20441da177e4SLinus Torvalds */ 2045b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2046b58fed8bSRam Pai unsigned long n) 20471da177e4SLinus Torvalds { 20481da177e4SLinus Torvalds char *t = to; 20491da177e4SLinus Torvalds const char __user *f = from; 20501da177e4SLinus Torvalds char c; 20511da177e4SLinus Torvalds 20521da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 20531da177e4SLinus Torvalds return n; 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds while (n) { 20561da177e4SLinus Torvalds if (__get_user(c, f)) { 20571da177e4SLinus Torvalds memset(t, 0, n); 20581da177e4SLinus Torvalds break; 20591da177e4SLinus Torvalds } 20601da177e4SLinus Torvalds *t++ = c; 20611da177e4SLinus Torvalds f++; 20621da177e4SLinus Torvalds n--; 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds return n; 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 20681da177e4SLinus Torvalds { 20691da177e4SLinus Torvalds int i; 20701da177e4SLinus Torvalds unsigned long page; 20711da177e4SLinus Torvalds unsigned long size; 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds *where = 0; 20741da177e4SLinus Torvalds if (!data) 20751da177e4SLinus Torvalds return 0; 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 20781da177e4SLinus Torvalds return -ENOMEM; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds /* We only care that *some* data at the address the user 20811da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 20821da177e4SLinus Torvalds * the remainder of the page. 20831da177e4SLinus Torvalds */ 20841da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 20851da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 20861da177e4SLinus Torvalds if (size > PAGE_SIZE) 20871da177e4SLinus Torvalds size = PAGE_SIZE; 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 20901da177e4SLinus Torvalds if (!i) { 20911da177e4SLinus Torvalds free_page(page); 20921da177e4SLinus Torvalds return -EFAULT; 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds if (i != PAGE_SIZE) 20951da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 20961da177e4SLinus Torvalds *where = page; 20971da177e4SLinus Torvalds return 0; 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds 2100eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2101eca6f534SVegard Nossum { 2102eca6f534SVegard Nossum char *tmp; 2103eca6f534SVegard Nossum 2104eca6f534SVegard Nossum if (!data) { 2105eca6f534SVegard Nossum *where = NULL; 2106eca6f534SVegard Nossum return 0; 2107eca6f534SVegard Nossum } 2108eca6f534SVegard Nossum 2109eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2110eca6f534SVegard Nossum if (IS_ERR(tmp)) 2111eca6f534SVegard Nossum return PTR_ERR(tmp); 2112eca6f534SVegard Nossum 2113eca6f534SVegard Nossum *where = tmp; 2114eca6f534SVegard Nossum return 0; 2115eca6f534SVegard Nossum } 2116eca6f534SVegard Nossum 21171da177e4SLinus Torvalds /* 21181da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 21191da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 21201da177e4SLinus Torvalds * 21211da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 21221da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 21231da177e4SLinus Torvalds * information (or be NULL). 21241da177e4SLinus Torvalds * 21251da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 21261da177e4SLinus Torvalds * When the flags word was introduced its top half was required 21271da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 21281da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 21291da177e4SLinus Torvalds * and must be discarded. 21301da177e4SLinus Torvalds */ 21311da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 21321da177e4SLinus Torvalds unsigned long flags, void *data_page) 21331da177e4SLinus Torvalds { 21342d92ab3cSAl Viro struct path path; 21351da177e4SLinus Torvalds int retval = 0; 21361da177e4SLinus Torvalds int mnt_flags = 0; 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds /* Discard magic */ 21391da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 21401da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds /* Basic sanity checks */ 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 21451da177e4SLinus Torvalds return -EINVAL; 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds if (data_page) 21481da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 21491da177e4SLinus Torvalds 2150a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2151a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2152a27ab9f2STetsuo Handa if (retval) 2153a27ab9f2STetsuo Handa return retval; 2154a27ab9f2STetsuo Handa 2155a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2156a27ab9f2STetsuo Handa type_page, flags, data_page); 2157a27ab9f2STetsuo Handa if (retval) 2158a27ab9f2STetsuo Handa goto dput_out; 2159a27ab9f2STetsuo Handa 2160613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2161613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 21620a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 21630a1c01c9SMatthew Garrett 21641da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 21651da177e4SLinus Torvalds if (flags & MS_NOSUID) 21661da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 21671da177e4SLinus Torvalds if (flags & MS_NODEV) 21681da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 21691da177e4SLinus Torvalds if (flags & MS_NOEXEC) 21701da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2171fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2172fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2173fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2174fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2175d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2176d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 21772e4b7fcdSDave Hansen if (flags & MS_RDONLY) 21782e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2179fc33a7bbSChristoph Hellwig 21807a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2181d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2182d0adde57SMatthew Garrett MS_STRICTATIME); 21831da177e4SLinus Torvalds 21841da177e4SLinus Torvalds if (flags & MS_REMOUNT) 21852d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 21861da177e4SLinus Torvalds data_page); 21871da177e4SLinus Torvalds else if (flags & MS_BIND) 21882d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 21899676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21902d92ab3cSAl Viro retval = do_change_type(&path, flags); 21911da177e4SLinus Torvalds else if (flags & MS_MOVE) 21922d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 21931da177e4SLinus Torvalds else 21942d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 21951da177e4SLinus Torvalds dev_name, data_page); 21961da177e4SLinus Torvalds dput_out: 21972d92ab3cSAl Viro path_put(&path); 21981da177e4SLinus Torvalds return retval; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 2201cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void) 2202cf8d2c11STrond Myklebust { 2203cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2204cf8d2c11STrond Myklebust 2205cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2206cf8d2c11STrond Myklebust if (!new_ns) 2207cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2208cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2209cf8d2c11STrond Myklebust new_ns->root = NULL; 2210cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2211cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2212cf8d2c11STrond Myklebust new_ns->event = 0; 2213cf8d2c11STrond Myklebust return new_ns; 2214cf8d2c11STrond Myklebust } 2215cf8d2c11STrond Myklebust 2216741a2951SJANAK DESAI /* 2217741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2218741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2219741a2951SJANAK DESAI */ 2220e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 22216b3286edSKirill Korotaev struct fs_struct *fs) 22221da177e4SLinus Torvalds { 22236b3286edSKirill Korotaev struct mnt_namespace *new_ns; 22247f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2225315fc83eSAl Viro struct mount *p, *q; 2226be08d6d2SAl Viro struct mount *old = mnt_ns->root; 2227cb338d06SAl Viro struct mount *new; 22281da177e4SLinus Torvalds 2229cf8d2c11STrond Myklebust new_ns = alloc_mnt_ns(); 2230cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2231cf8d2c11STrond Myklebust return new_ns; 22321da177e4SLinus Torvalds 2233390c6843SRam Pai down_write(&namespace_sem); 22341da177e4SLinus Torvalds /* First pass: copy the tree topology */ 2235909b0a88SAl Viro new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE); 2236be34d1a3SDavid Howells if (IS_ERR(new)) { 2237390c6843SRam Pai up_write(&namespace_sem); 22381da177e4SLinus Torvalds kfree(new_ns); 2239be34d1a3SDavid Howells return ERR_CAST(new); 22401da177e4SLinus Torvalds } 2241be08d6d2SAl Viro new_ns->root = new; 2242962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 22431a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 2244962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds /* 22471da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 22481da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 22491da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 22501da177e4SLinus Torvalds */ 2251909b0a88SAl Viro p = old; 2252cb338d06SAl Viro q = new; 22531da177e4SLinus Torvalds while (p) { 2254143c8c91SAl Viro q->mnt_ns = new_ns; 22551da177e4SLinus Torvalds if (fs) { 2256315fc83eSAl Viro if (&p->mnt == fs->root.mnt) { 2257315fc83eSAl Viro fs->root.mnt = mntget(&q->mnt); 2258315fc83eSAl Viro rootmnt = &p->mnt; 22591da177e4SLinus Torvalds } 2260315fc83eSAl Viro if (&p->mnt == fs->pwd.mnt) { 2261315fc83eSAl Viro fs->pwd.mnt = mntget(&q->mnt); 2262315fc83eSAl Viro pwdmnt = &p->mnt; 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds } 2265909b0a88SAl Viro p = next_mnt(p, old); 2266909b0a88SAl Viro q = next_mnt(q, new); 22671da177e4SLinus Torvalds } 2268390c6843SRam Pai up_write(&namespace_sem); 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds if (rootmnt) 2271f03c6599SAl Viro mntput(rootmnt); 22721da177e4SLinus Torvalds if (pwdmnt) 2273f03c6599SAl Viro mntput(pwdmnt); 22741da177e4SLinus Torvalds 2275741a2951SJANAK DESAI return new_ns; 2276741a2951SJANAK DESAI } 2277741a2951SJANAK DESAI 2278213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2279e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 2280741a2951SJANAK DESAI { 22816b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2282741a2951SJANAK DESAI 2283e3222c4eSBadari Pulavarty BUG_ON(!ns); 22846b3286edSKirill Korotaev get_mnt_ns(ns); 2285741a2951SJANAK DESAI 2286741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2287e3222c4eSBadari Pulavarty return ns; 2288741a2951SJANAK DESAI 2289e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 2290741a2951SJANAK DESAI 22916b3286edSKirill Korotaev put_mnt_ns(ns); 2292e3222c4eSBadari Pulavarty return new_ns; 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds 2295cf8d2c11STrond Myklebust /** 2296cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2297cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2298cf8d2c11STrond Myklebust */ 22991a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2300cf8d2c11STrond Myklebust { 23011a4eeaf2SAl Viro struct mnt_namespace *new_ns = alloc_mnt_ns(); 2302cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 23031a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 23041a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2305be08d6d2SAl Viro new_ns->root = mnt; 23061a4eeaf2SAl Viro list_add(&new_ns->list, &mnt->mnt_list); 2307c1334495SAl Viro } else { 23081a4eeaf2SAl Viro mntput(m); 2309cf8d2c11STrond Myklebust } 2310cf8d2c11STrond Myklebust return new_ns; 2311cf8d2c11STrond Myklebust } 2312cf8d2c11STrond Myklebust 2313ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2314ea441d11SAl Viro { 2315ea441d11SAl Viro struct mnt_namespace *ns; 2316d31da0f0SAl Viro struct super_block *s; 2317ea441d11SAl Viro struct path path; 2318ea441d11SAl Viro int err; 2319ea441d11SAl Viro 2320ea441d11SAl Viro ns = create_mnt_ns(mnt); 2321ea441d11SAl Viro if (IS_ERR(ns)) 2322ea441d11SAl Viro return ERR_CAST(ns); 2323ea441d11SAl Viro 2324ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2325ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2326ea441d11SAl Viro 2327ea441d11SAl Viro put_mnt_ns(ns); 2328ea441d11SAl Viro 2329ea441d11SAl Viro if (err) 2330ea441d11SAl Viro return ERR_PTR(err); 2331ea441d11SAl Viro 2332ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2333d31da0f0SAl Viro s = path.mnt->mnt_sb; 2334d31da0f0SAl Viro atomic_inc(&s->s_active); 2335ea441d11SAl Viro mntput(path.mnt); 2336ea441d11SAl Viro /* lock the sucker */ 2337d31da0f0SAl Viro down_write(&s->s_umount); 2338ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2339ea441d11SAl Viro return path.dentry; 2340ea441d11SAl Viro } 2341ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2342ea441d11SAl Viro 2343bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2344bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 23451da177e4SLinus Torvalds { 2346eca6f534SVegard Nossum int ret; 2347eca6f534SVegard Nossum char *kernel_type; 2348eca6f534SVegard Nossum char *kernel_dir; 2349eca6f534SVegard Nossum char *kernel_dev; 23501da177e4SLinus Torvalds unsigned long data_page; 23511da177e4SLinus Torvalds 2352eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2353eca6f534SVegard Nossum if (ret < 0) 2354eca6f534SVegard Nossum goto out_type; 23551da177e4SLinus Torvalds 2356eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2357eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2358eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2359eca6f534SVegard Nossum goto out_dir; 2360eca6f534SVegard Nossum } 23611da177e4SLinus Torvalds 2362eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2363eca6f534SVegard Nossum if (ret < 0) 2364eca6f534SVegard Nossum goto out_dev; 23651da177e4SLinus Torvalds 2366eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2367eca6f534SVegard Nossum if (ret < 0) 2368eca6f534SVegard Nossum goto out_data; 23691da177e4SLinus Torvalds 2370eca6f534SVegard Nossum ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags, 2371eca6f534SVegard Nossum (void *) data_page); 2372eca6f534SVegard Nossum 23731da177e4SLinus Torvalds free_page(data_page); 2374eca6f534SVegard Nossum out_data: 2375eca6f534SVegard Nossum kfree(kernel_dev); 2376eca6f534SVegard Nossum out_dev: 2377eca6f534SVegard Nossum putname(kernel_dir); 2378eca6f534SVegard Nossum out_dir: 2379eca6f534SVegard Nossum kfree(kernel_type); 2380eca6f534SVegard Nossum out_type: 2381eca6f534SVegard Nossum return ret; 23821da177e4SLinus Torvalds } 23831da177e4SLinus Torvalds 23841da177e4SLinus Torvalds /* 2385afac7cbaSAl Viro * Return true if path is reachable from root 2386afac7cbaSAl Viro * 2387afac7cbaSAl Viro * namespace_sem or vfsmount_lock is held 2388afac7cbaSAl Viro */ 2389643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2390afac7cbaSAl Viro const struct path *root) 2391afac7cbaSAl Viro { 2392643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2393a73324daSAl Viro dentry = mnt->mnt_mountpoint; 23940714a533SAl Viro mnt = mnt->mnt_parent; 2395afac7cbaSAl Viro } 2396643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2397afac7cbaSAl Viro } 2398afac7cbaSAl Viro 2399afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2400afac7cbaSAl Viro { 2401afac7cbaSAl Viro int res; 2402962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 2403643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 2404962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 2405afac7cbaSAl Viro return res; 2406afac7cbaSAl Viro } 2407afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2408afac7cbaSAl Viro 2409afac7cbaSAl Viro /* 24101da177e4SLinus Torvalds * pivot_root Semantics: 24111da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 24121da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 24131da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 24141da177e4SLinus Torvalds * 24151da177e4SLinus Torvalds * Restrictions: 24161da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 24171da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 24181da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 24191da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 24201da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 24211da177e4SLinus Torvalds * 24224a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 24234a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 24244a0d11faSNeil Brown * in this situation. 24254a0d11faSNeil Brown * 24261da177e4SLinus Torvalds * Notes: 24271da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 24281da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 24291da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 24301da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 24311da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 24321da177e4SLinus Torvalds * first. 24331da177e4SLinus Torvalds */ 24343480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 24353480b257SHeiko Carstens const char __user *, put_old) 24361da177e4SLinus Torvalds { 24372d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 2438419148daSAl Viro struct mount *new_mnt, *root_mnt; 24391da177e4SLinus Torvalds int error; 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 24421da177e4SLinus Torvalds return -EPERM; 24431da177e4SLinus Torvalds 24442d8f3038SAl Viro error = user_path_dir(new_root, &new); 24451da177e4SLinus Torvalds if (error) 24461da177e4SLinus Torvalds goto out0; 24471da177e4SLinus Torvalds 24482d8f3038SAl Viro error = user_path_dir(put_old, &old); 24491da177e4SLinus Torvalds if (error) 24501da177e4SLinus Torvalds goto out1; 24511da177e4SLinus Torvalds 24522d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2453b12cea91SAl Viro if (error) 2454b12cea91SAl Viro goto out2; 24551da177e4SLinus Torvalds 2456f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 2457b12cea91SAl Viro error = lock_mount(&old); 2458b12cea91SAl Viro if (error) 2459b12cea91SAl Viro goto out3; 2460b12cea91SAl Viro 24611da177e4SLinus Torvalds error = -EINVAL; 2462419148daSAl Viro new_mnt = real_mount(new.mnt); 2463419148daSAl Viro root_mnt = real_mount(root.mnt); 2464fc7be130SAl Viro if (IS_MNT_SHARED(real_mount(old.mnt)) || 2465fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2466fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2467b12cea91SAl Viro goto out4; 2468143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2469b12cea91SAl Viro goto out4; 24701da177e4SLinus Torvalds error = -ENOENT; 2471f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2472b12cea91SAl Viro goto out4; 2473f3da392eSAlexey Dobriyan if (d_unlinked(old.dentry)) 2474b12cea91SAl Viro goto out4; 24751da177e4SLinus Torvalds error = -EBUSY; 24762d8f3038SAl Viro if (new.mnt == root.mnt || 24772d8f3038SAl Viro old.mnt == root.mnt) 2478b12cea91SAl Viro goto out4; /* loop, on the same file system */ 24791da177e4SLinus Torvalds error = -EINVAL; 24808c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2481b12cea91SAl Viro goto out4; /* not a mountpoint */ 2482676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2483b12cea91SAl Viro goto out4; /* not attached */ 24842d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2485b12cea91SAl Viro goto out4; /* not a mountpoint */ 2486676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2487b12cea91SAl Viro goto out4; /* not attached */ 24884ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 2489643822b4SAl Viro if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new)) 2490b12cea91SAl Viro goto out4; 2491962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2492419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2493419148daSAl Viro detach_mnt(root_mnt, &root_parent); 24944ac91378SJan Blunck /* mount old root on put_old */ 2495419148daSAl Viro attach_mnt(root_mnt, &old); 24964ac91378SJan Blunck /* mount new_root on / */ 2497419148daSAl Viro attach_mnt(new_mnt, &root_parent); 24986b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2499962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 25002d8f3038SAl Viro chroot_fs_refs(&root, &new); 25011da177e4SLinus Torvalds error = 0; 2502b12cea91SAl Viro out4: 2503b12cea91SAl Viro unlock_mount(&old); 2504b12cea91SAl Viro if (!error) { 25051a390689SAl Viro path_put(&root_parent); 25061a390689SAl Viro path_put(&parent_path); 2507b12cea91SAl Viro } 2508b12cea91SAl Viro out3: 25098c3ee42eSAl Viro path_put(&root); 2510b12cea91SAl Viro out2: 25112d8f3038SAl Viro path_put(&old); 25121da177e4SLinus Torvalds out1: 25132d8f3038SAl Viro path_put(&new); 25141da177e4SLinus Torvalds out0: 25151da177e4SLinus Torvalds return error; 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds static void __init init_mount_tree(void) 25191da177e4SLinus Torvalds { 25201da177e4SLinus Torvalds struct vfsmount *mnt; 25216b3286edSKirill Korotaev struct mnt_namespace *ns; 2522ac748a09SJan Blunck struct path root; 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 25251da177e4SLinus Torvalds if (IS_ERR(mnt)) 25261da177e4SLinus Torvalds panic("Can't create rootfs"); 2527b3e19d92SNick Piggin 25283b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 25293b22edc5STrond Myklebust if (IS_ERR(ns)) 25301da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 25311da177e4SLinus Torvalds 25326b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 25336b3286edSKirill Korotaev get_mnt_ns(ns); 25341da177e4SLinus Torvalds 2535be08d6d2SAl Viro root.mnt = mnt; 2536be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2537ac748a09SJan Blunck 2538ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2539ac748a09SJan Blunck set_fs_root(current->fs, &root); 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 254274bf17cfSDenis Cheng void __init mnt_init(void) 25431da177e4SLinus Torvalds { 254413f14b4dSEric Dumazet unsigned u; 254515a67dd8SRandy Dunlap int err; 25461da177e4SLinus Torvalds 2547390c6843SRam Pai init_rwsem(&namespace_sem); 2548390c6843SRam Pai 25497d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 255020c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 25511da177e4SLinus Torvalds 2552b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds if (!mount_hashtable) 25551da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 25561da177e4SLinus Torvalds 255780cdc6daSMandeep Singh Baines printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE); 25581da177e4SLinus Torvalds 255913f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 256013f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 25611da177e4SLinus Torvalds 2562962830dfSAndi Kleen br_lock_init(&vfsmount_lock); 256399b7db7bSNick Piggin 256415a67dd8SRandy Dunlap err = sysfs_init(); 256515a67dd8SRandy Dunlap if (err) 256615a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 25678e24eea7SHarvey Harrison __func__, err); 256800d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 256900d26666SGreg Kroah-Hartman if (!fs_kobj) 25708e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 25711da177e4SLinus Torvalds init_rootfs(); 25721da177e4SLinus Torvalds init_mount_tree(); 25731da177e4SLinus Torvalds } 25741da177e4SLinus Torvalds 2575616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 25761da177e4SLinus Torvalds { 257770fbcdf4SRam Pai LIST_HEAD(umount_list); 2578616511d0STrond Myklebust 2579d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2580616511d0STrond Myklebust return; 2581390c6843SRam Pai down_write(&namespace_sem); 2582962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2583be08d6d2SAl Viro umount_tree(ns->root, 0, &umount_list); 2584962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2585390c6843SRam Pai up_write(&namespace_sem); 258670fbcdf4SRam Pai release_mounts(&umount_list); 25876b3286edSKirill Korotaev kfree(ns); 25881da177e4SLinus Torvalds } 25899d412a43SAl Viro 25909d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 25919d412a43SAl Viro { 2592423e0ab0STim Chen struct vfsmount *mnt; 2593423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2594423e0ab0STim Chen if (!IS_ERR(mnt)) { 2595423e0ab0STim Chen /* 2596423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2597423e0ab0STim Chen * we unmount before file sys is unregistered 2598423e0ab0STim Chen */ 2599f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 2600423e0ab0STim Chen } 2601423e0ab0STim Chen return mnt; 26029d412a43SAl Viro } 26039d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2604423e0ab0STim Chen 2605423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2606423e0ab0STim Chen { 2607423e0ab0STim Chen /* release long term mount so mount point can be released */ 2608423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2609f7a99c5bSAl Viro br_write_lock(&vfsmount_lock); 2610f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 2611f7a99c5bSAl Viro br_write_unlock(&vfsmount_lock); 2612423e0ab0STim Chen mntput(mnt); 2613423e0ab0STim Chen } 2614423e0ab0STim Chen } 2615423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 261602125a82SAl Viro 261702125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 261802125a82SAl Viro { 2619143c8c91SAl Viro return check_mnt(real_mount(mnt)); 262002125a82SAl Viro } 2621