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 goto out; 3283d733633SDave Hansen } 3293d733633SDave Hansen out: 330d3ef3d73Snpiggin@suse.de preempt_enable(); 3313d733633SDave Hansen return ret; 3328366025eSDave Hansen } 3338366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3348366025eSDave Hansen 3358366025eSDave Hansen /** 33696029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 33796029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 33896029c4eSnpiggin@suse.de * 33996029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 34096029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 34196029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 34296029c4eSnpiggin@suse.de * on it. This allows some optimisation. 34396029c4eSnpiggin@suse.de * 34496029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 34596029c4eSnpiggin@suse.de * drop the reference. 34696029c4eSnpiggin@suse.de */ 34796029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 34896029c4eSnpiggin@suse.de { 34996029c4eSnpiggin@suse.de /* superblock may be r/o */ 35096029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 35196029c4eSnpiggin@suse.de return -EROFS; 35296029c4eSnpiggin@suse.de preempt_disable(); 35383adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 35496029c4eSnpiggin@suse.de preempt_enable(); 35596029c4eSnpiggin@suse.de return 0; 35696029c4eSnpiggin@suse.de } 35796029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 35896029c4eSnpiggin@suse.de 35996029c4eSnpiggin@suse.de /** 36096029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 36196029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 36296029c4eSnpiggin@suse.de * 36396029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 36496029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 36596029c4eSnpiggin@suse.de */ 36696029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 36796029c4eSnpiggin@suse.de { 3682d8dd38aSOGAWA Hirofumi struct inode *inode = file->f_dentry->d_inode; 3692d8dd38aSOGAWA Hirofumi if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 37096029c4eSnpiggin@suse.de return mnt_want_write(file->f_path.mnt); 37196029c4eSnpiggin@suse.de else 37296029c4eSnpiggin@suse.de return mnt_clone_write(file->f_path.mnt); 37396029c4eSnpiggin@suse.de } 37496029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 37596029c4eSnpiggin@suse.de 37696029c4eSnpiggin@suse.de /** 3778366025eSDave Hansen * mnt_drop_write - give up write access to a mount 3788366025eSDave Hansen * @mnt: the mount on which to give up write access 3798366025eSDave Hansen * 3808366025eSDave Hansen * Tells the low-level filesystem that we are done 3818366025eSDave Hansen * performing writes to it. Must be matched with 3828366025eSDave Hansen * mnt_want_write() call above. 3838366025eSDave Hansen */ 3848366025eSDave Hansen void mnt_drop_write(struct vfsmount *mnt) 3858366025eSDave Hansen { 386d3ef3d73Snpiggin@suse.de preempt_disable(); 38783adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 388d3ef3d73Snpiggin@suse.de preempt_enable(); 3898366025eSDave Hansen } 3908366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 3918366025eSDave Hansen 3922a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 3932a79f17eSAl Viro { 3942a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 3952a79f17eSAl Viro } 3962a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 3972a79f17eSAl Viro 39883adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 3998366025eSDave Hansen { 4003d733633SDave Hansen int ret = 0; 4013d733633SDave Hansen 40299b7db7bSNick Piggin br_write_lock(vfsmount_lock); 40383adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 404d3ef3d73Snpiggin@suse.de /* 405d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 406d3ef3d73Snpiggin@suse.de * should be visible before we do. 407d3ef3d73Snpiggin@suse.de */ 408d3ef3d73Snpiggin@suse.de smp_mb(); 409d3ef3d73Snpiggin@suse.de 410d3ef3d73Snpiggin@suse.de /* 411d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 412d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 413d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 414d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 415d3ef3d73Snpiggin@suse.de * 416d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 417d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 418d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 419d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 420d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 421d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 422d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 423d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 424d3ef3d73Snpiggin@suse.de * we're counting up here. 425d3ef3d73Snpiggin@suse.de */ 426c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 427d3ef3d73Snpiggin@suse.de ret = -EBUSY; 428d3ef3d73Snpiggin@suse.de else 42983adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 430d3ef3d73Snpiggin@suse.de /* 431d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 432d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 433d3ef3d73Snpiggin@suse.de */ 434d3ef3d73Snpiggin@suse.de smp_wmb(); 43583adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 43699b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 4373d733633SDave Hansen return ret; 4383d733633SDave Hansen } 4398366025eSDave Hansen 44083adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 4412e4b7fcdSDave Hansen { 44299b7db7bSNick Piggin br_write_lock(vfsmount_lock); 44383adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 44499b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 4452e4b7fcdSDave Hansen } 4462e4b7fcdSDave Hansen 4474ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 4484ed5e82fSMiklos Szeredi { 4494ed5e82fSMiklos Szeredi struct mount *mnt; 4504ed5e82fSMiklos Szeredi int err = 0; 4514ed5e82fSMiklos Szeredi 4524ed5e82fSMiklos Szeredi br_write_lock(vfsmount_lock); 4534ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 4544ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 4554ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 4564ed5e82fSMiklos Szeredi smp_mb(); 4574ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 4584ed5e82fSMiklos Szeredi err = -EBUSY; 4594ed5e82fSMiklos Szeredi break; 4604ed5e82fSMiklos Szeredi } 4614ed5e82fSMiklos Szeredi } 4624ed5e82fSMiklos Szeredi } 4634ed5e82fSMiklos Szeredi if (!err) { 4644ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 4654ed5e82fSMiklos Szeredi smp_wmb(); 4664ed5e82fSMiklos Szeredi } 4674ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 4684ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 4694ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 4704ed5e82fSMiklos Szeredi } 4714ed5e82fSMiklos Szeredi br_write_unlock(vfsmount_lock); 4724ed5e82fSMiklos Szeredi 4734ed5e82fSMiklos Szeredi return err; 4744ed5e82fSMiklos Szeredi } 4754ed5e82fSMiklos Szeredi 476b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 4771da177e4SLinus Torvalds { 47852ba1621SAl Viro kfree(mnt->mnt_devname); 47973cd49ecSMiklos Szeredi mnt_free_id(mnt); 480d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 48168e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 482d3ef3d73Snpiggin@suse.de #endif 483b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds /* 487a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 488a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 48999b7db7bSNick Piggin * vfsmount_lock must be held for read or write. 4901da177e4SLinus Torvalds */ 491c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 492a05964f3SRam Pai int dir) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 4951da177e4SLinus Torvalds struct list_head *tmp = head; 496c7105365SAl Viro struct mount *p, *found = NULL; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds for (;;) { 499a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 5001da177e4SLinus Torvalds p = NULL; 5011da177e4SLinus Torvalds if (tmp == head) 5021da177e4SLinus Torvalds break; 5031b8e5564SAl Viro p = list_entry(tmp, struct mount, mnt_hash); 504a73324daSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) { 505a05964f3SRam Pai found = p; 5061da177e4SLinus Torvalds break; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds return found; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 512a05964f3SRam Pai /* 513a05964f3SRam Pai * lookup_mnt increments the ref count before returning 514a05964f3SRam Pai * the vfsmount struct. 515a05964f3SRam Pai */ 5161c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 517a05964f3SRam Pai { 518c7105365SAl Viro struct mount *child_mnt; 51999b7db7bSNick Piggin 52099b7db7bSNick Piggin br_read_lock(vfsmount_lock); 521c7105365SAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry, 1); 522c7105365SAl Viro if (child_mnt) { 523c7105365SAl Viro mnt_add_count(child_mnt, 1); 52499b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 525c7105365SAl Viro return &child_mnt->mnt; 526c7105365SAl Viro } else { 527c7105365SAl Viro br_read_unlock(vfsmount_lock); 528c7105365SAl Viro return NULL; 529c7105365SAl Viro } 530a05964f3SRam Pai } 531a05964f3SRam Pai 532143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 5331da177e4SLinus Torvalds { 5346b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 53799b7db7bSNick Piggin /* 53899b7db7bSNick Piggin * vfsmount lock must be held for write 53999b7db7bSNick Piggin */ 5406b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 5415addc5ddSAl Viro { 5425addc5ddSAl Viro if (ns) { 5435addc5ddSAl Viro ns->event = ++event; 5445addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5455addc5ddSAl Viro } 5465addc5ddSAl Viro } 5475addc5ddSAl Viro 54899b7db7bSNick Piggin /* 54999b7db7bSNick Piggin * vfsmount lock must be held for write 55099b7db7bSNick Piggin */ 5516b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 5525addc5ddSAl Viro { 5535addc5ddSAl Viro if (ns && ns->event != event) { 5545addc5ddSAl Viro ns->event = event; 5555addc5ddSAl Viro wake_up_interruptible(&ns->poll); 5565addc5ddSAl Viro } 5575addc5ddSAl Viro } 5585addc5ddSAl Viro 55999b7db7bSNick Piggin /* 5605f57cbccSNick Piggin * Clear dentry's mounted state if it has no remaining mounts. 5615f57cbccSNick Piggin * vfsmount_lock must be held for write. 5625f57cbccSNick Piggin */ 563aa0a4cf0SAl Viro static void dentry_reset_mounted(struct dentry *dentry) 5645f57cbccSNick Piggin { 5655f57cbccSNick Piggin unsigned u; 5665f57cbccSNick Piggin 5675f57cbccSNick Piggin for (u = 0; u < HASH_SIZE; u++) { 568d5e50f74SAl Viro struct mount *p; 5695f57cbccSNick Piggin 5701b8e5564SAl Viro list_for_each_entry(p, &mount_hashtable[u], mnt_hash) { 571a73324daSAl Viro if (p->mnt_mountpoint == dentry) 5725f57cbccSNick Piggin return; 5735f57cbccSNick Piggin } 5745f57cbccSNick Piggin } 5755f57cbccSNick Piggin spin_lock(&dentry->d_lock); 5765f57cbccSNick Piggin dentry->d_flags &= ~DCACHE_MOUNTED; 5775f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 5785f57cbccSNick Piggin } 5795f57cbccSNick Piggin 5805f57cbccSNick Piggin /* 58199b7db7bSNick Piggin * vfsmount lock must be held for write 58299b7db7bSNick Piggin */ 583419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 5841da177e4SLinus Torvalds { 585a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 5860714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 5870714a533SAl Viro mnt->mnt_parent = mnt; 588a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 5896b41d536SAl Viro list_del_init(&mnt->mnt_child); 5901b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 591aa0a4cf0SAl Viro dentry_reset_mounted(old_path->dentry); 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds 59499b7db7bSNick Piggin /* 59599b7db7bSNick Piggin * vfsmount lock must be held for write 59699b7db7bSNick Piggin */ 59714cf1fa8SAl Viro void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry, 59844d964d6SAl Viro struct mount *child_mnt) 599b90fa9aeSRam Pai { 6003a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 601a73324daSAl Viro child_mnt->mnt_mountpoint = dget(dentry); 6023a2393d7SAl Viro child_mnt->mnt_parent = mnt; 6035f57cbccSNick Piggin spin_lock(&dentry->d_lock); 6045f57cbccSNick Piggin dentry->d_flags |= DCACHE_MOUNTED; 6055f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 606b90fa9aeSRam Pai } 607b90fa9aeSRam Pai 60899b7db7bSNick Piggin /* 60999b7db7bSNick Piggin * vfsmount lock must be held for write 61099b7db7bSNick Piggin */ 611419148daSAl Viro static void attach_mnt(struct mount *mnt, struct path *path) 6121da177e4SLinus Torvalds { 61314cf1fa8SAl Viro mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt); 6141b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 6151a390689SAl Viro hash(path->mnt, path->dentry)); 6166b41d536SAl Viro list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts); 617b90fa9aeSRam Pai } 618b90fa9aeSRam Pai 61983adc753SAl Viro static inline void __mnt_make_longterm(struct mount *mnt) 6207e3d0eb0SAl Viro { 6217e3d0eb0SAl Viro #ifdef CONFIG_SMP 62268e8a9feSAl Viro atomic_inc(&mnt->mnt_longterm); 6237e3d0eb0SAl Viro #endif 6247e3d0eb0SAl Viro } 6257e3d0eb0SAl Viro 6267e3d0eb0SAl Viro /* needs vfsmount lock for write */ 62783adc753SAl Viro static inline void __mnt_make_shortterm(struct mount *mnt) 6287e3d0eb0SAl Viro { 6297e3d0eb0SAl Viro #ifdef CONFIG_SMP 63068e8a9feSAl Viro atomic_dec(&mnt->mnt_longterm); 6317e3d0eb0SAl Viro #endif 6327e3d0eb0SAl Viro } 6337e3d0eb0SAl Viro 634b90fa9aeSRam Pai /* 63599b7db7bSNick Piggin * vfsmount lock must be held for write 636b90fa9aeSRam Pai */ 6374b2619a5SAl Viro static void commit_tree(struct mount *mnt) 638b90fa9aeSRam Pai { 6390714a533SAl Viro struct mount *parent = mnt->mnt_parent; 64083adc753SAl Viro struct mount *m; 641b90fa9aeSRam Pai LIST_HEAD(head); 642143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 643b90fa9aeSRam Pai 6440714a533SAl Viro BUG_ON(parent == mnt); 645b90fa9aeSRam Pai 6461a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 6471a4eeaf2SAl Viro list_for_each_entry(m, &head, mnt_list) { 648143c8c91SAl Viro m->mnt_ns = n; 6497e3d0eb0SAl Viro __mnt_make_longterm(m); 650f03c6599SAl Viro } 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; 71239f7c4dbSMiklos Szeredi br_write_lock(vfsmount_lock); 71339f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 71439f7c4dbSMiklos Szeredi 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; 72352ba1621SAl Viro struct mount *mnt = alloc_vfsmnt(old->mnt_devname); 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds if (mnt) { 726719f5d7fSMiklos Szeredi if (flag & (CL_SLAVE | CL_PRIVATE)) 72715169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 728719f5d7fSMiklos Szeredi else 72915169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 730719f5d7fSMiklos Szeredi 73115169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 732b105e270SAl Viro int err = mnt_alloc_group_id(mnt); 733719f5d7fSMiklos Szeredi if (err) 734719f5d7fSMiklos Szeredi goto out_free; 735719f5d7fSMiklos Szeredi } 736719f5d7fSMiklos Szeredi 73787129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 7381da177e4SLinus Torvalds atomic_inc(&sb->s_active); 739b105e270SAl Viro mnt->mnt.mnt_sb = sb; 740b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 741a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7420714a533SAl Viro mnt->mnt_parent = mnt; 74339f7c4dbSMiklos Szeredi br_write_lock(vfsmount_lock); 74439f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 74539f7c4dbSMiklos Szeredi br_write_unlock(vfsmount_lock); 746b90fa9aeSRam Pai 7475afe0022SRam Pai if (flag & CL_SLAVE) { 7486776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 74932301920SAl Viro mnt->mnt_master = old; 750fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 7518aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 752fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 7536776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 754d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 7556776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 756d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 7575afe0022SRam Pai } 758b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 7590f0afb1dSAl Viro set_mnt_shared(mnt); 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 7621da177e4SLinus Torvalds * as the original if that was on one */ 76336341f64SRam Pai if (flag & CL_EXPIRE) { 7646776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 7656776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 7661da177e4SLinus Torvalds } 76736341f64SRam Pai } 768cb338d06SAl Viro return mnt; 769719f5d7fSMiklos Szeredi 770719f5d7fSMiklos Szeredi out_free: 771719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 772719f5d7fSMiklos Szeredi return NULL; 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 77583adc753SAl Viro static inline void mntfree(struct mount *mnt) 7761da177e4SLinus Torvalds { 77783adc753SAl Viro struct vfsmount *m = &mnt->mnt; 77883adc753SAl Viro struct super_block *sb = m->mnt_sb; 779b3e19d92SNick Piggin 7803d733633SDave Hansen /* 7813d733633SDave Hansen * This probably indicates that somebody messed 7823d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 7833d733633SDave Hansen * happens, the filesystem was probably unable 7843d733633SDave Hansen * to make r/w->r/o transitions. 7853d733633SDave Hansen */ 786d3ef3d73Snpiggin@suse.de /* 787b3e19d92SNick Piggin * The locking used to deal with mnt_count decrement provides barriers, 788b3e19d92SNick Piggin * so mnt_get_writers() below is safe. 789d3ef3d73Snpiggin@suse.de */ 790c6653a83SNick Piggin WARN_ON(mnt_get_writers(mnt)); 79183adc753SAl Viro fsnotify_vfsmount_delete(m); 79283adc753SAl Viro dput(m->mnt_root); 79383adc753SAl Viro free_vfsmnt(mnt); 7941da177e4SLinus Torvalds deactivate_super(sb); 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 797900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 7987b7b1aceSAl Viro { 799b3e19d92SNick Piggin put_again: 800f03c6599SAl Viro #ifdef CONFIG_SMP 801b3e19d92SNick Piggin br_read_lock(vfsmount_lock); 80268e8a9feSAl Viro if (likely(atomic_read(&mnt->mnt_longterm))) { 803aa9c0e07SAl Viro mnt_add_count(mnt, -1); 804b3e19d92SNick Piggin br_read_unlock(vfsmount_lock); 80599b7db7bSNick Piggin return; 806b3e19d92SNick Piggin } 807b3e19d92SNick Piggin br_read_unlock(vfsmount_lock); 808b3e19d92SNick Piggin 80999b7db7bSNick Piggin br_write_lock(vfsmount_lock); 810aa9c0e07SAl Viro mnt_add_count(mnt, -1); 811b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 81299b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 81399b7db7bSNick Piggin return; 81499b7db7bSNick Piggin } 815b3e19d92SNick Piggin #else 816aa9c0e07SAl Viro mnt_add_count(mnt, -1); 817b3e19d92SNick Piggin if (likely(mnt_get_count(mnt))) 818b3e19d92SNick Piggin return; 819b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 820f03c6599SAl Viro #endif 821863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 822863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 823863d684fSAl Viro mnt->mnt_pinned = 0; 824b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 825900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 826b3e19d92SNick Piggin goto put_again; 827b3e19d92SNick Piggin } 82839f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 829b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 830b3e19d92SNick Piggin mntfree(mnt); 831b3e19d92SNick Piggin } 832b3e19d92SNick Piggin 833b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 834b3e19d92SNick Piggin { 835b3e19d92SNick Piggin if (mnt) { 836863d684fSAl Viro struct mount *m = real_mount(mnt); 837b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 838863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 839863d684fSAl Viro m->mnt_expiry_mark = 0; 840863d684fSAl Viro mntput_no_expire(m); 841b3e19d92SNick Piggin } 842b3e19d92SNick Piggin } 843b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 844b3e19d92SNick Piggin 845b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 846b3e19d92SNick Piggin { 847b3e19d92SNick Piggin if (mnt) 84883adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 849b3e19d92SNick Piggin return mnt; 850b3e19d92SNick Piggin } 851b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 852b3e19d92SNick Piggin 8537b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 8547b7b1aceSAl Viro { 85599b7db7bSNick Piggin br_write_lock(vfsmount_lock); 856863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 85799b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 8587b7b1aceSAl Viro } 8597b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 8607b7b1aceSAl Viro 861863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 8627b7b1aceSAl Viro { 863863d684fSAl Viro struct mount *mnt = real_mount(m); 86499b7db7bSNick Piggin br_write_lock(vfsmount_lock); 8657b7b1aceSAl Viro if (mnt->mnt_pinned) { 866863d684fSAl Viro mnt_add_count(mnt, 1); 8677b7b1aceSAl Viro mnt->mnt_pinned--; 8687b7b1aceSAl Viro } 86999b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 8707b7b1aceSAl Viro } 8717b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 8721da177e4SLinus Torvalds 873b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 874b3b304a2SMiklos Szeredi { 875b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 876b3b304a2SMiklos Szeredi } 877b3b304a2SMiklos Szeredi 878b3b304a2SMiklos Szeredi /* 879b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 880b3b304a2SMiklos Szeredi * implement more complex mount option showing. 881b3b304a2SMiklos Szeredi * 882b3b304a2SMiklos Szeredi * See also save_mount_options(). 883b3b304a2SMiklos Szeredi */ 88434c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 885b3b304a2SMiklos Szeredi { 8862a32cebdSAl Viro const char *options; 8872a32cebdSAl Viro 8882a32cebdSAl Viro rcu_read_lock(); 88934c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 890b3b304a2SMiklos Szeredi 891b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 892b3b304a2SMiklos Szeredi seq_putc(m, ','); 893b3b304a2SMiklos Szeredi mangle(m, options); 894b3b304a2SMiklos Szeredi } 8952a32cebdSAl Viro rcu_read_unlock(); 896b3b304a2SMiklos Szeredi 897b3b304a2SMiklos Szeredi return 0; 898b3b304a2SMiklos Szeredi } 899b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 900b3b304a2SMiklos Szeredi 901b3b304a2SMiklos Szeredi /* 902b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 903b3b304a2SMiklos Szeredi * called from the fill_super() callback. 904b3b304a2SMiklos Szeredi * 905b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 906b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 907b3b304a2SMiklos Szeredi * remount fails. 908b3b304a2SMiklos Szeredi * 909b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 910b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 911b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 912b3b304a2SMiklos Szeredi * any more. 913b3b304a2SMiklos Szeredi */ 914b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 915b3b304a2SMiklos Szeredi { 9162a32cebdSAl Viro BUG_ON(sb->s_options); 9172a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 918b3b304a2SMiklos Szeredi } 919b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 920b3b304a2SMiklos Szeredi 9212a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 9222a32cebdSAl Viro { 9232a32cebdSAl Viro char *old = sb->s_options; 9242a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 9252a32cebdSAl Viro if (old) { 9262a32cebdSAl Viro synchronize_rcu(); 9272a32cebdSAl Viro kfree(old); 9282a32cebdSAl Viro } 9292a32cebdSAl Viro } 9302a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 9312a32cebdSAl Viro 932a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 9330226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 9341da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 9351da177e4SLinus Torvalds { 9360226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 9371da177e4SLinus Torvalds 938390c6843SRam Pai down_read(&namespace_sem); 939a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 9431da177e4SLinus Torvalds { 9440226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 945b0765fb8SPavel Emelianov 946a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 9501da177e4SLinus Torvalds { 951390c6843SRam Pai up_read(&namespace_sem); 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 9540226f492SAl Viro static int m_show(struct seq_file *m, void *v) 9559f5596afSAl Viro { 9560226f492SAl Viro struct proc_mounts *p = container_of(m, struct proc_mounts, m); 9571a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 9580226f492SAl Viro return p->show(m, &r->mnt); 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 961a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 9621da177e4SLinus Torvalds .start = m_start, 9631da177e4SLinus Torvalds .next = m_next, 9641da177e4SLinus Torvalds .stop = m_stop, 9650226f492SAl Viro .show = m_show, 966b4629fe2SChuck Lever }; 967a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 968b4629fe2SChuck Lever 9691da177e4SLinus Torvalds /** 9701da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 9711da177e4SLinus Torvalds * @mnt: root of mount tree 9721da177e4SLinus Torvalds * 9731da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 9741da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 9751da177e4SLinus Torvalds * busy. 9761da177e4SLinus Torvalds */ 977909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 9781da177e4SLinus Torvalds { 979909b0a88SAl Viro struct mount *mnt = real_mount(m); 98036341f64SRam Pai int actual_refs = 0; 98136341f64SRam Pai int minimum_refs = 0; 982315fc83eSAl Viro struct mount *p; 983909b0a88SAl Viro BUG_ON(!m); 9841da177e4SLinus Torvalds 985b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 986b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 987909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 98883adc753SAl Viro actual_refs += mnt_get_count(p); 9891da177e4SLinus Torvalds minimum_refs += 2; 9901da177e4SLinus Torvalds } 991b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds if (actual_refs > minimum_refs) 9941da177e4SLinus Torvalds return 0; 995e3474a8eSIan Kent 996e3474a8eSIan Kent return 1; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /** 10021da177e4SLinus Torvalds * may_umount - check if a mount point is busy 10031da177e4SLinus Torvalds * @mnt: root of mount 10041da177e4SLinus Torvalds * 10051da177e4SLinus Torvalds * This is called to check if a mount point has any 10061da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 10071da177e4SLinus Torvalds * mount has sub mounts this will return busy 10081da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 10091da177e4SLinus Torvalds * 10101da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 10111da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 10121da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 10131da177e4SLinus Torvalds */ 10141da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 10151da177e4SLinus Torvalds { 1016e3474a8eSIan Kent int ret = 1; 10178ad08d8aSAl Viro down_read(&namespace_sem); 1018b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 10191ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1020e3474a8eSIan Kent ret = 0; 1021b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 10228ad08d8aSAl Viro up_read(&namespace_sem); 1023a05964f3SRam Pai return ret; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 10271da177e4SLinus Torvalds 1028b90fa9aeSRam Pai void release_mounts(struct list_head *head) 10291da177e4SLinus Torvalds { 1030d5e50f74SAl Viro struct mount *mnt; 103170fbcdf4SRam Pai while (!list_empty(head)) { 10321b8e5564SAl Viro mnt = list_first_entry(head, struct mount, mnt_hash); 10331b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 1034676da58dSAl Viro if (mnt_has_parent(mnt)) { 103570fbcdf4SRam Pai struct dentry *dentry; 1036863d684fSAl Viro struct mount *m; 103799b7db7bSNick Piggin 103899b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1039a73324daSAl Viro dentry = mnt->mnt_mountpoint; 1040863d684fSAl Viro m = mnt->mnt_parent; 1041a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10420714a533SAl Viro mnt->mnt_parent = mnt; 10437c4b93d8SAl Viro m->mnt_ghosts--; 104499b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 104570fbcdf4SRam Pai dput(dentry); 1046863d684fSAl Viro mntput(&m->mnt); 10471da177e4SLinus Torvalds } 1048d5e50f74SAl Viro mntput(&mnt->mnt); 104970fbcdf4SRam Pai } 105070fbcdf4SRam Pai } 105170fbcdf4SRam Pai 105299b7db7bSNick Piggin /* 105399b7db7bSNick Piggin * vfsmount lock must be held for write 105499b7db7bSNick Piggin * namespace_sem must be held for write 105599b7db7bSNick Piggin */ 1056761d5c38SAl Viro void umount_tree(struct mount *mnt, int propagate, struct list_head *kill) 105770fbcdf4SRam Pai { 10587b8a53fdSAl Viro LIST_HEAD(tmp_list); 1059315fc83eSAl Viro struct mount *p; 106070fbcdf4SRam Pai 1061909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) 10621b8e5564SAl Viro list_move(&p->mnt_hash, &tmp_list); 106370fbcdf4SRam Pai 1064a05964f3SRam Pai if (propagate) 10657b8a53fdSAl Viro propagate_umount(&tmp_list); 1066a05964f3SRam Pai 10671b8e5564SAl Viro list_for_each_entry(p, &tmp_list, mnt_hash) { 10686776db3dSAl Viro list_del_init(&p->mnt_expire); 10691a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1070143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 1071143c8c91SAl Viro p->mnt_ns = NULL; 107283adc753SAl Viro __mnt_make_shortterm(p); 10736b41d536SAl Viro list_del_init(&p->mnt_child); 1074676da58dSAl Viro if (mnt_has_parent(p)) { 1075863d684fSAl Viro p->mnt_parent->mnt_ghosts++; 1076a73324daSAl Viro dentry_reset_mounted(p->mnt_mountpoint); 10777c4b93d8SAl Viro } 10780f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 10791da177e4SLinus Torvalds } 10807b8a53fdSAl Viro list_splice(&tmp_list, kill); 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 1083692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts); 1084c35038beSAl Viro 10851ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 10861da177e4SLinus Torvalds { 10871ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 10881da177e4SLinus Torvalds int retval; 108970fbcdf4SRam Pai LIST_HEAD(umount_list); 10901da177e4SLinus Torvalds 10911ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 10921da177e4SLinus Torvalds if (retval) 10931da177e4SLinus Torvalds return retval; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds /* 10961da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 10971da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 10981da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 10991da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 11001da177e4SLinus Torvalds */ 11011da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 11021ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 11031da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 11041da177e4SLinus Torvalds return -EINVAL; 11051da177e4SLinus Torvalds 1106b3e19d92SNick Piggin /* 1107b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1108b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1109b3e19d92SNick Piggin */ 1110b3e19d92SNick Piggin br_write_lock(vfsmount_lock); 111183adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1112bf9faa2aSJ. R. Okajima br_write_unlock(vfsmount_lock); 11131da177e4SLinus Torvalds return -EBUSY; 1114b3e19d92SNick Piggin } 1115b3e19d92SNick Piggin br_write_unlock(vfsmount_lock); 11161da177e4SLinus Torvalds 1117863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 11181da177e4SLinus Torvalds return -EAGAIN; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds /* 11221da177e4SLinus Torvalds * If we may have to abort operations to get out of this 11231da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 11241da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 11251da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 11261da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 11271da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 11281da177e4SLinus Torvalds * about for the moment. 11291da177e4SLinus Torvalds */ 11301da177e4SLinus Torvalds 113142faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 113242faad99SAl Viro sb->s_op->umount_begin(sb); 113342faad99SAl Viro } 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds /* 11361da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 11371da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 11381da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 11391da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 11401da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 11411da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 11421da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 11431da177e4SLinus Torvalds */ 11441ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 11451da177e4SLinus Torvalds /* 11461da177e4SLinus Torvalds * Special case for "unmounting" root ... 11471da177e4SLinus Torvalds * we just try to remount it readonly. 11481da177e4SLinus Torvalds */ 11491da177e4SLinus Torvalds down_write(&sb->s_umount); 11504aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 11511da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 11521da177e4SLinus Torvalds up_write(&sb->s_umount); 11531da177e4SLinus Torvalds return retval; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds 1156390c6843SRam Pai down_write(&namespace_sem); 115799b7db7bSNick Piggin br_write_lock(vfsmount_lock); 11585addc5ddSAl Viro event++; 11591da177e4SLinus Torvalds 1160c35038beSAl Viro if (!(flags & MNT_DETACH)) 11611ab59738SAl Viro shrink_submounts(mnt, &umount_list); 1162c35038beSAl Viro 11631da177e4SLinus Torvalds retval = -EBUSY; 1164a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 11651a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 11661ab59738SAl Viro umount_tree(mnt, 1, &umount_list); 11671da177e4SLinus Torvalds retval = 0; 11681da177e4SLinus Torvalds } 116999b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1170390c6843SRam Pai up_write(&namespace_sem); 117170fbcdf4SRam Pai release_mounts(&umount_list); 11721da177e4SLinus Torvalds return retval; 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds /* 11761da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 11771da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 11781da177e4SLinus Torvalds * 11791da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 11801da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 11811da177e4SLinus Torvalds */ 11821da177e4SLinus Torvalds 1183bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 11841da177e4SLinus Torvalds { 11852d8f3038SAl Viro struct path path; 1186900148dcSAl Viro struct mount *mnt; 11871da177e4SLinus Torvalds int retval; 1188db1f05bbSMiklos Szeredi int lookup_flags = 0; 11891da177e4SLinus Torvalds 1190db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1191db1f05bbSMiklos Szeredi return -EINVAL; 1192db1f05bbSMiklos Szeredi 1193db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1194db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1195db1f05bbSMiklos Szeredi 1196db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 11971da177e4SLinus Torvalds if (retval) 11981da177e4SLinus Torvalds goto out; 1199900148dcSAl Viro mnt = real_mount(path.mnt); 12001da177e4SLinus Torvalds retval = -EINVAL; 12012d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 12021da177e4SLinus Torvalds goto dput_and_out; 1203143c8c91SAl Viro if (!check_mnt(mnt)) 12041da177e4SLinus Torvalds goto dput_and_out; 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds retval = -EPERM; 12071da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 12081da177e4SLinus Torvalds goto dput_and_out; 12091da177e4SLinus Torvalds 1210900148dcSAl Viro retval = do_umount(mnt, flags); 12111da177e4SLinus Torvalds dput_and_out: 1212429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 12132d8f3038SAl Viro dput(path.dentry); 1214900148dcSAl Viro mntput_no_expire(mnt); 12151da177e4SLinus Torvalds out: 12161da177e4SLinus Torvalds return retval; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* 12221da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 12231da177e4SLinus Torvalds */ 1224bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 12251da177e4SLinus Torvalds { 12261da177e4SLinus Torvalds return sys_umount(name, 0); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds #endif 12301da177e4SLinus Torvalds 12312d92ab3cSAl Viro static int mount_is_safe(struct path *path) 12321da177e4SLinus Torvalds { 12331da177e4SLinus Torvalds if (capable(CAP_SYS_ADMIN)) 12341da177e4SLinus Torvalds return 0; 12351da177e4SLinus Torvalds return -EPERM; 12361da177e4SLinus Torvalds #ifdef notyet 12372d92ab3cSAl Viro if (S_ISLNK(path->dentry->d_inode->i_mode)) 12381da177e4SLinus Torvalds return -EPERM; 12392d92ab3cSAl Viro if (path->dentry->d_inode->i_mode & S_ISVTX) { 1240da9592edSDavid Howells if (current_uid() != path->dentry->d_inode->i_uid) 12411da177e4SLinus Torvalds return -EPERM; 12421da177e4SLinus Torvalds } 12432d92ab3cSAl Viro if (inode_permission(path->dentry->d_inode, MAY_WRITE)) 12441da177e4SLinus Torvalds return -EPERM; 12451da177e4SLinus Torvalds return 0; 12461da177e4SLinus Torvalds #endif 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 124987129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 125036341f64SRam Pai int flag) 12511da177e4SLinus Torvalds { 1252a73324daSAl Viro struct mount *res, *p, *q, *r; 12531a390689SAl Viro struct path path; 12541da177e4SLinus Torvalds 1255fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 12569676f0c6SRam Pai return NULL; 12579676f0c6SRam Pai 125836341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 12591da177e4SLinus Torvalds if (!q) 12601da177e4SLinus Torvalds goto Enomem; 1261a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds p = mnt; 12646b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1265315fc83eSAl Viro struct mount *s; 12667ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 12671da177e4SLinus Torvalds continue; 12681da177e4SLinus Torvalds 1269909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 1270fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 12719676f0c6SRam Pai s = skip_mnt_tree(s); 12729676f0c6SRam Pai continue; 12739676f0c6SRam Pai } 12740714a533SAl Viro while (p != s->mnt_parent) { 12750714a533SAl Viro p = p->mnt_parent; 12760714a533SAl Viro q = q->mnt_parent; 12771da177e4SLinus Torvalds } 127887129cc0SAl Viro p = s; 1279cb338d06SAl Viro path.mnt = &q->mnt; 1280a73324daSAl Viro path.dentry = p->mnt_mountpoint; 128187129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 12821da177e4SLinus Torvalds if (!q) 12831da177e4SLinus Torvalds goto Enomem; 128499b7db7bSNick Piggin br_write_lock(vfsmount_lock); 12851a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 1286cb338d06SAl Viro attach_mnt(q, &path); 128799b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds } 12901da177e4SLinus Torvalds return res; 12911da177e4SLinus Torvalds Enomem: 12921da177e4SLinus Torvalds if (res) { 129370fbcdf4SRam Pai LIST_HEAD(umount_list); 129499b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1295761d5c38SAl Viro umount_tree(res, 0, &umount_list); 129699b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 129770fbcdf4SRam Pai release_mounts(&umount_list); 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds return NULL; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 1302589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 13038aec0809SAl Viro { 1304cb338d06SAl Viro struct mount *tree; 13051a60a280SAl Viro down_write(&namespace_sem); 130687129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 130787129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 13081a60a280SAl Viro up_write(&namespace_sem); 1309cb338d06SAl Viro return tree ? &tree->mnt : NULL; 13108aec0809SAl Viro } 13118aec0809SAl Viro 13128aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 13138aec0809SAl Viro { 13148aec0809SAl Viro LIST_HEAD(umount_list); 13151a60a280SAl Viro down_write(&namespace_sem); 131699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1317761d5c38SAl Viro umount_tree(real_mount(mnt), 0, &umount_list); 131899b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 13191a60a280SAl Viro up_write(&namespace_sem); 13208aec0809SAl Viro release_mounts(&umount_list); 13218aec0809SAl Viro } 13228aec0809SAl Viro 13231f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 13241f707137SAl Viro struct vfsmount *root) 13251f707137SAl Viro { 13261a4eeaf2SAl Viro struct mount *mnt; 13271f707137SAl Viro int res = f(root, arg); 13281f707137SAl Viro if (res) 13291f707137SAl Viro return res; 13301a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 13311a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 13321f707137SAl Viro if (res) 13331f707137SAl Viro return res; 13341f707137SAl Viro } 13351f707137SAl Viro return 0; 13361f707137SAl Viro } 13371f707137SAl Viro 13384b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1339719f5d7fSMiklos Szeredi { 1340315fc83eSAl Viro struct mount *p; 1341719f5d7fSMiklos Szeredi 1342909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1343fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 13444b8b21f4SAl Viro mnt_release_group_id(p); 1345719f5d7fSMiklos Szeredi } 1346719f5d7fSMiklos Szeredi } 1347719f5d7fSMiklos Szeredi 13484b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1349719f5d7fSMiklos Szeredi { 1350315fc83eSAl Viro struct mount *p; 1351719f5d7fSMiklos Szeredi 1352909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1353fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 13544b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1355719f5d7fSMiklos Szeredi if (err) { 13564b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1357719f5d7fSMiklos Szeredi return err; 1358719f5d7fSMiklos Szeredi } 1359719f5d7fSMiklos Szeredi } 1360719f5d7fSMiklos Szeredi } 1361719f5d7fSMiklos Szeredi 1362719f5d7fSMiklos Szeredi return 0; 1363719f5d7fSMiklos Szeredi } 1364719f5d7fSMiklos Szeredi 1365b90fa9aeSRam Pai /* 1366b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1367b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 136821444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 136921444403SRam Pai * store the parent mount and mountpoint dentry. 137021444403SRam Pai * (done when source_mnt is moved) 1371b90fa9aeSRam Pai * 1372b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1373b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 13749676f0c6SRam Pai * --------------------------------------------------------------------------- 1375b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 13769676f0c6SRam Pai * |************************************************************************** 13779676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 13789676f0c6SRam Pai * | dest | | | | | 13799676f0c6SRam Pai * | | | | | | | 13809676f0c6SRam Pai * | v | | | | | 13819676f0c6SRam Pai * |************************************************************************** 13829676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 13835afe0022SRam Pai * | | | | | | 13849676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 13859676f0c6SRam Pai * *************************************************************************** 1386b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1387b90fa9aeSRam Pai * destination mount. 1388b90fa9aeSRam Pai * 1389b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1390b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1391b90fa9aeSRam Pai * the peer group of the source mount. 1392b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1393b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1394b90fa9aeSRam Pai * mount. 13955afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 13965afe0022SRam Pai * of the destination mount and the cloned mount is made slave 13975afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 13985afe0022SRam Pai * is marked as 'shared and slave'. 13995afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 14005afe0022SRam Pai * source mount. 14015afe0022SRam Pai * 14029676f0c6SRam Pai * --------------------------------------------------------------------------- 140321444403SRam Pai * | MOVE MOUNT OPERATION | 14049676f0c6SRam Pai * |************************************************************************** 14059676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 14069676f0c6SRam Pai * | dest | | | | | 14079676f0c6SRam Pai * | | | | | | | 14089676f0c6SRam Pai * | v | | | | | 14099676f0c6SRam Pai * |************************************************************************** 14109676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 14115afe0022SRam Pai * | | | | | | 14129676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 14139676f0c6SRam Pai * *************************************************************************** 14145afe0022SRam Pai * 14155afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 14165afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 141721444403SRam Pai * (+*) the mount is moved to the destination. 14185afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 14195afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 14205afe0022SRam Pai * the mount is marked as 'shared and slave'. 14215afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1422b90fa9aeSRam Pai * 1423b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1424b90fa9aeSRam Pai * applied to each mount in the tree. 1425b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1426b90fa9aeSRam Pai * in allocations. 1427b90fa9aeSRam Pai */ 14280fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 14291a390689SAl Viro struct path *path, struct path *parent_path) 1430b90fa9aeSRam Pai { 1431b90fa9aeSRam Pai LIST_HEAD(tree_list); 1432a8d56d8eSAl Viro struct mount *dest_mnt = real_mount(path->mnt); 14331a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1434315fc83eSAl Viro struct mount *child, *p; 1435719f5d7fSMiklos Szeredi int err; 1436b90fa9aeSRam Pai 1437fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 14380fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1439719f5d7fSMiklos Szeredi if (err) 1440719f5d7fSMiklos Szeredi goto out; 1441719f5d7fSMiklos Szeredi } 1442a8d56d8eSAl Viro err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); 1443719f5d7fSMiklos Szeredi if (err) 1444719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1445b90fa9aeSRam Pai 144699b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1447df1a1ad2SAl Viro 1448fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1449909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 14500f0afb1dSAl Viro set_mnt_shared(p); 1451b90fa9aeSRam Pai } 14521a390689SAl Viro if (parent_path) { 14530fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 14540fb54e50SAl Viro attach_mnt(source_mnt, path); 1455143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 145621444403SRam Pai } else { 145714cf1fa8SAl Viro mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 14580fb54e50SAl Viro commit_tree(source_mnt); 145921444403SRam Pai } 1460b90fa9aeSRam Pai 14611b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 14621b8e5564SAl Viro list_del_init(&child->mnt_hash); 14634b2619a5SAl Viro commit_tree(child); 1464b90fa9aeSRam Pai } 146599b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 146699b7db7bSNick Piggin 1467b90fa9aeSRam Pai return 0; 1468719f5d7fSMiklos Szeredi 1469719f5d7fSMiklos Szeredi out_cleanup_ids: 1470fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 14710fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1472719f5d7fSMiklos Szeredi out: 1473719f5d7fSMiklos Szeredi return err; 1474b90fa9aeSRam Pai } 1475b90fa9aeSRam Pai 1476b12cea91SAl Viro static int lock_mount(struct path *path) 1477b12cea91SAl Viro { 1478b12cea91SAl Viro struct vfsmount *mnt; 1479b12cea91SAl Viro retry: 1480b12cea91SAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1481b12cea91SAl Viro if (unlikely(cant_mount(path->dentry))) { 1482b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1483b12cea91SAl Viro return -ENOENT; 1484b12cea91SAl Viro } 1485b12cea91SAl Viro down_write(&namespace_sem); 1486b12cea91SAl Viro mnt = lookup_mnt(path); 1487b12cea91SAl Viro if (likely(!mnt)) 1488b12cea91SAl Viro return 0; 1489b12cea91SAl Viro up_write(&namespace_sem); 1490b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1491b12cea91SAl Viro path_put(path); 1492b12cea91SAl Viro path->mnt = mnt; 1493b12cea91SAl Viro path->dentry = dget(mnt->mnt_root); 1494b12cea91SAl Viro goto retry; 1495b12cea91SAl Viro } 1496b12cea91SAl Viro 1497b12cea91SAl Viro static void unlock_mount(struct path *path) 1498b12cea91SAl Viro { 1499b12cea91SAl Viro up_write(&namespace_sem); 1500b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1501b12cea91SAl Viro } 1502b12cea91SAl Viro 150395bc5f25SAl Viro static int graft_tree(struct mount *mnt, struct path *path) 15041da177e4SLinus Torvalds { 150595bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 15061da177e4SLinus Torvalds return -EINVAL; 15071da177e4SLinus Torvalds 15088c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 150995bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 15101da177e4SLinus Torvalds return -ENOTDIR; 15111da177e4SLinus Torvalds 1512b12cea91SAl Viro if (d_unlinked(path->dentry)) 1513b12cea91SAl Viro return -ENOENT; 15141da177e4SLinus Torvalds 151595bc5f25SAl Viro return attach_recursive_mnt(mnt, path, NULL); 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds /* 15197a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 15207a2e8a8fSValerie Aurora */ 15217a2e8a8fSValerie Aurora 15227a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 15237a2e8a8fSValerie Aurora { 15247c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 15257a2e8a8fSValerie Aurora 15267a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 15277a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 15287a2e8a8fSValerie Aurora return 0; 15297a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 15307a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 15317a2e8a8fSValerie Aurora return 0; 15327a2e8a8fSValerie Aurora return type; 15337a2e8a8fSValerie Aurora } 15347a2e8a8fSValerie Aurora 15357a2e8a8fSValerie Aurora /* 153607b20889SRam Pai * recursively change the type of the mountpoint. 153707b20889SRam Pai */ 15380a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 153907b20889SRam Pai { 1540315fc83eSAl Viro struct mount *m; 15414b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 154207b20889SRam Pai int recurse = flag & MS_REC; 15437a2e8a8fSValerie Aurora int type; 1544719f5d7fSMiklos Szeredi int err = 0; 154507b20889SRam Pai 1546ee6f9582SMiklos Szeredi if (!capable(CAP_SYS_ADMIN)) 1547ee6f9582SMiklos Szeredi return -EPERM; 1548ee6f9582SMiklos Szeredi 15492d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 155007b20889SRam Pai return -EINVAL; 155107b20889SRam Pai 15527a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 15537a2e8a8fSValerie Aurora if (!type) 15547a2e8a8fSValerie Aurora return -EINVAL; 15557a2e8a8fSValerie Aurora 155607b20889SRam Pai down_write(&namespace_sem); 1557719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1558719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1559719f5d7fSMiklos Szeredi if (err) 1560719f5d7fSMiklos Szeredi goto out_unlock; 1561719f5d7fSMiklos Szeredi } 1562719f5d7fSMiklos Szeredi 156399b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1564909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 15650f0afb1dSAl Viro change_mnt_propagation(m, type); 156699b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1567719f5d7fSMiklos Szeredi 1568719f5d7fSMiklos Szeredi out_unlock: 156907b20889SRam Pai up_write(&namespace_sem); 1570719f5d7fSMiklos Szeredi return err; 157107b20889SRam Pai } 157207b20889SRam Pai 157307b20889SRam Pai /* 15741da177e4SLinus Torvalds * do loopback mount. 15751da177e4SLinus Torvalds */ 15760a0d8a46SAl Viro static int do_loopback(struct path *path, char *old_name, 15772dafe1c4SEric Sandeen int recurse) 15781da177e4SLinus Torvalds { 1579b12cea91SAl Viro LIST_HEAD(umount_list); 15802d92ab3cSAl Viro struct path old_path; 158187129cc0SAl Viro struct mount *mnt = NULL, *old; 15822d92ab3cSAl Viro int err = mount_is_safe(path); 15831da177e4SLinus Torvalds if (err) 15841da177e4SLinus Torvalds return err; 15851da177e4SLinus Torvalds if (!old_name || !*old_name) 15861da177e4SLinus Torvalds return -EINVAL; 1587815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 15881da177e4SLinus Torvalds if (err) 15891da177e4SLinus Torvalds return err; 15901da177e4SLinus Torvalds 1591b12cea91SAl Viro err = lock_mount(path); 1592b12cea91SAl Viro if (err) 15939676f0c6SRam Pai goto out; 15949676f0c6SRam Pai 159587129cc0SAl Viro old = real_mount(old_path.mnt); 159687129cc0SAl Viro 1597b12cea91SAl Viro err = -EINVAL; 1598fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1599b12cea91SAl Viro goto out2; 1600b12cea91SAl Viro 1601143c8c91SAl Viro if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old)) 1602b12cea91SAl Viro goto out2; 1603ccd48bc7SAl Viro 16041da177e4SLinus Torvalds err = -ENOMEM; 16051da177e4SLinus Torvalds if (recurse) 160687129cc0SAl Viro mnt = copy_tree(old, old_path.dentry, 0); 16071da177e4SLinus Torvalds else 160887129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 16091da177e4SLinus Torvalds 1610ccd48bc7SAl Viro if (!mnt) 1611b12cea91SAl Viro goto out2; 1612ccd48bc7SAl Viro 161395bc5f25SAl Viro err = graft_tree(mnt, path); 16141da177e4SLinus Torvalds if (err) { 161599b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1616761d5c38SAl Viro umount_tree(mnt, 0, &umount_list); 161799b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 16185b83d2c5SRam Pai } 1619b12cea91SAl Viro out2: 1620b12cea91SAl Viro unlock_mount(path); 1621b12cea91SAl Viro release_mounts(&umount_list); 1622ccd48bc7SAl Viro out: 16232d92ab3cSAl Viro path_put(&old_path); 16241da177e4SLinus Torvalds return err; 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds 16272e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 16282e4b7fcdSDave Hansen { 16292e4b7fcdSDave Hansen int error = 0; 16302e4b7fcdSDave Hansen int readonly_request = 0; 16312e4b7fcdSDave Hansen 16322e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 16332e4b7fcdSDave Hansen readonly_request = 1; 16342e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 16352e4b7fcdSDave Hansen return 0; 16362e4b7fcdSDave Hansen 16372e4b7fcdSDave Hansen if (readonly_request) 163883adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 16392e4b7fcdSDave Hansen else 164083adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 16412e4b7fcdSDave Hansen return error; 16422e4b7fcdSDave Hansen } 16432e4b7fcdSDave Hansen 16441da177e4SLinus Torvalds /* 16451da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 16461da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 16471da177e4SLinus Torvalds * on it - tough luck. 16481da177e4SLinus Torvalds */ 16490a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 16501da177e4SLinus Torvalds void *data) 16511da177e4SLinus Torvalds { 16521da177e4SLinus Torvalds int err; 16532d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1654143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16571da177e4SLinus Torvalds return -EPERM; 16581da177e4SLinus Torvalds 1659143c8c91SAl Viro if (!check_mnt(mnt)) 16601da177e4SLinus Torvalds return -EINVAL; 16611da177e4SLinus Torvalds 16622d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 16631da177e4SLinus Torvalds return -EINVAL; 16641da177e4SLinus Torvalds 1665ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1666ff36fe2cSEric Paris if (err) 1667ff36fe2cSEric Paris return err; 1668ff36fe2cSEric Paris 16691da177e4SLinus Torvalds down_write(&sb->s_umount); 16702e4b7fcdSDave Hansen if (flags & MS_BIND) 16712d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 16724aa98cf7SAl Viro else 16731da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 16747b43a79fSAl Viro if (!err) { 167599b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1676143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1677143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 167899b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 16797b43a79fSAl Viro } 16801da177e4SLinus Torvalds up_write(&sb->s_umount); 16810e55a7ccSDan Williams if (!err) { 168299b7db7bSNick Piggin br_write_lock(vfsmount_lock); 1683143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 168499b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 16850e55a7ccSDan Williams } 16861da177e4SLinus Torvalds return err; 16871da177e4SLinus Torvalds } 16881da177e4SLinus Torvalds 1689cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 16909676f0c6SRam Pai { 1691315fc83eSAl Viro struct mount *p; 1692909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1693fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 16949676f0c6SRam Pai return 1; 16959676f0c6SRam Pai } 16969676f0c6SRam Pai return 0; 16979676f0c6SRam Pai } 16989676f0c6SRam Pai 16990a0d8a46SAl Viro static int do_move_mount(struct path *path, char *old_name) 17001da177e4SLinus Torvalds { 17012d92ab3cSAl Viro struct path old_path, parent_path; 1702676da58dSAl Viro struct mount *p; 17030fb54e50SAl Viro struct mount *old; 17041da177e4SLinus Torvalds int err = 0; 17051da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17061da177e4SLinus Torvalds return -EPERM; 17071da177e4SLinus Torvalds if (!old_name || !*old_name) 17081da177e4SLinus Torvalds return -EINVAL; 17092d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 17101da177e4SLinus Torvalds if (err) 17111da177e4SLinus Torvalds return err; 17121da177e4SLinus Torvalds 1713b12cea91SAl Viro err = lock_mount(path); 1714cc53ce53SDavid Howells if (err < 0) 1715cc53ce53SDavid Howells goto out; 1716cc53ce53SDavid Howells 1717143c8c91SAl Viro old = real_mount(old_path.mnt); 1718fc7be130SAl Viro p = real_mount(path->mnt); 1719143c8c91SAl Viro 17201da177e4SLinus Torvalds err = -EINVAL; 1721fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 17221da177e4SLinus Torvalds goto out1; 17231da177e4SLinus Torvalds 1724f3da392eSAlexey Dobriyan if (d_unlinked(path->dentry)) 172521444403SRam Pai goto out1; 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds err = -EINVAL; 17282d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 172921444403SRam Pai goto out1; 17301da177e4SLinus Torvalds 1731676da58dSAl Viro if (!mnt_has_parent(old)) 173221444403SRam Pai goto out1; 17331da177e4SLinus Torvalds 17342d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 17352d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 173621444403SRam Pai goto out1; 173721444403SRam Pai /* 173821444403SRam Pai * Don't move a mount residing in a shared parent. 173921444403SRam Pai */ 1740fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 174121444403SRam Pai goto out1; 17429676f0c6SRam Pai /* 17439676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 17449676f0c6SRam Pai * mount which is shared. 17459676f0c6SRam Pai */ 1746fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 17479676f0c6SRam Pai goto out1; 17481da177e4SLinus Torvalds err = -ELOOP; 1749fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1750676da58dSAl Viro if (p == old) 175121444403SRam Pai goto out1; 17521da177e4SLinus Torvalds 17530fb54e50SAl Viro err = attach_recursive_mnt(old, path, &parent_path); 17544ac91378SJan Blunck if (err) 175521444403SRam Pai goto out1; 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 17581da177e4SLinus Torvalds * automatically */ 17596776db3dSAl Viro list_del_init(&old->mnt_expire); 17601da177e4SLinus Torvalds out1: 1761b12cea91SAl Viro unlock_mount(path); 17621da177e4SLinus Torvalds out: 17631da177e4SLinus Torvalds if (!err) 17641a390689SAl Viro path_put(&parent_path); 17652d92ab3cSAl Viro path_put(&old_path); 17661da177e4SLinus Torvalds return err; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds 17699d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 17709d412a43SAl Viro { 17719d412a43SAl Viro int err; 17729d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 17739d412a43SAl Viro if (subtype) { 17749d412a43SAl Viro subtype++; 17759d412a43SAl Viro err = -EINVAL; 17769d412a43SAl Viro if (!subtype[0]) 17779d412a43SAl Viro goto err; 17789d412a43SAl Viro } else 17799d412a43SAl Viro subtype = ""; 17809d412a43SAl Viro 17819d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 17829d412a43SAl Viro err = -ENOMEM; 17839d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 17849d412a43SAl Viro goto err; 17859d412a43SAl Viro return mnt; 17869d412a43SAl Viro 17879d412a43SAl Viro err: 17889d412a43SAl Viro mntput(mnt); 17899d412a43SAl Viro return ERR_PTR(err); 17909d412a43SAl Viro } 17919d412a43SAl Viro 179279e801a9SAl Viro static struct vfsmount * 17939d412a43SAl Viro do_kern_mount(const char *fstype, int flags, const char *name, void *data) 17949d412a43SAl Viro { 17959d412a43SAl Viro struct file_system_type *type = get_fs_type(fstype); 17969d412a43SAl Viro struct vfsmount *mnt; 17979d412a43SAl Viro if (!type) 17989d412a43SAl Viro return ERR_PTR(-ENODEV); 17999d412a43SAl Viro mnt = vfs_kern_mount(type, flags, name, data); 18009d412a43SAl Viro if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 18019d412a43SAl Viro !mnt->mnt_sb->s_subtype) 18029d412a43SAl Viro mnt = fs_set_subtype(mnt, fstype); 18039d412a43SAl Viro put_filesystem(type); 18049d412a43SAl Viro return mnt; 18059d412a43SAl Viro } 18069d412a43SAl Viro 18079d412a43SAl Viro /* 18089d412a43SAl Viro * add a mount into a namespace's mount tree 18099d412a43SAl Viro */ 181095bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 18119d412a43SAl Viro { 18129d412a43SAl Viro int err; 18139d412a43SAl Viro 18149d412a43SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 18159d412a43SAl Viro 1816b12cea91SAl Viro err = lock_mount(path); 1817b12cea91SAl Viro if (err) 1818b12cea91SAl Viro return err; 18199d412a43SAl Viro 18209d412a43SAl Viro err = -EINVAL; 1821143c8c91SAl Viro if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt))) 18229d412a43SAl Viro goto unlock; 18239d412a43SAl Viro 18249d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 18259d412a43SAl Viro err = -EBUSY; 182695bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 18279d412a43SAl Viro path->mnt->mnt_root == path->dentry) 18289d412a43SAl Viro goto unlock; 18299d412a43SAl Viro 18309d412a43SAl Viro err = -EINVAL; 183195bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 18329d412a43SAl Viro goto unlock; 18339d412a43SAl Viro 183495bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 18359d412a43SAl Viro err = graft_tree(newmnt, path); 18369d412a43SAl Viro 18379d412a43SAl Viro unlock: 1838b12cea91SAl Viro unlock_mount(path); 18399d412a43SAl Viro return err; 18409d412a43SAl Viro } 1841b1e75df4SAl Viro 18421da177e4SLinus Torvalds /* 18431da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 18441da177e4SLinus Torvalds * namespace's tree 18451da177e4SLinus Torvalds */ 18460a0d8a46SAl Viro static int do_new_mount(struct path *path, char *type, int flags, 18471da177e4SLinus Torvalds int mnt_flags, char *name, void *data) 18481da177e4SLinus Torvalds { 18491da177e4SLinus Torvalds struct vfsmount *mnt; 185015f9a3f3SAl Viro int err; 18511da177e4SLinus Torvalds 1852eca6f534SVegard Nossum if (!type) 18531da177e4SLinus Torvalds return -EINVAL; 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds /* we need capabilities... */ 18561da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 18571da177e4SLinus Torvalds return -EPERM; 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds mnt = do_kern_mount(type, flags, name, data); 18601da177e4SLinus Torvalds if (IS_ERR(mnt)) 18611da177e4SLinus Torvalds return PTR_ERR(mnt); 18621da177e4SLinus Torvalds 186395bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 186415f9a3f3SAl Viro if (err) 186515f9a3f3SAl Viro mntput(mnt); 186615f9a3f3SAl Viro return err; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 186919a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 187019a167afSAl Viro { 18716776db3dSAl Viro struct mount *mnt = real_mount(m); 187219a167afSAl Viro int err; 187319a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 187419a167afSAl Viro * expired before we get a chance to add it 187519a167afSAl Viro */ 18766776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 187719a167afSAl Viro 187819a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 187919a167afSAl Viro m->mnt_root == path->dentry) { 1880b1e75df4SAl Viro err = -ELOOP; 1881b1e75df4SAl Viro goto fail; 188219a167afSAl Viro } 188319a167afSAl Viro 188495bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 1885b1e75df4SAl Viro if (!err) 1886b1e75df4SAl Viro return 0; 1887b1e75df4SAl Viro fail: 1888b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 18896776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 1890b1e75df4SAl Viro down_write(&namespace_sem); 1891b1e75df4SAl Viro br_write_lock(vfsmount_lock); 18926776db3dSAl Viro list_del_init(&mnt->mnt_expire); 1893b1e75df4SAl Viro br_write_unlock(vfsmount_lock); 1894b1e75df4SAl Viro up_write(&namespace_sem); 189519a167afSAl Viro } 1896b1e75df4SAl Viro mntput(m); 1897b1e75df4SAl Viro mntput(m); 189819a167afSAl Viro return err; 189919a167afSAl Viro } 190019a167afSAl Viro 1901ea5b778aSDavid Howells /** 1902ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 1903ea5b778aSDavid Howells * @mnt: The mount to list. 1904ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 1905ea5b778aSDavid Howells */ 1906ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 1907ea5b778aSDavid Howells { 1908ea5b778aSDavid Howells down_write(&namespace_sem); 1909ea5b778aSDavid Howells br_write_lock(vfsmount_lock); 1910ea5b778aSDavid Howells 19116776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 1912ea5b778aSDavid Howells 1913ea5b778aSDavid Howells br_write_unlock(vfsmount_lock); 1914ea5b778aSDavid Howells up_write(&namespace_sem); 1915ea5b778aSDavid Howells } 1916ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 1917ea5b778aSDavid Howells 1918ea5b778aSDavid Howells /* 19191da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 19201da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 19211da177e4SLinus Torvalds * here 19221da177e4SLinus Torvalds */ 19231da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 19241da177e4SLinus Torvalds { 1925761d5c38SAl Viro struct mount *mnt, *next; 19261da177e4SLinus Torvalds LIST_HEAD(graveyard); 1927bcc5c7d2SAl Viro LIST_HEAD(umounts); 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds if (list_empty(mounts)) 19301da177e4SLinus Torvalds return; 19311da177e4SLinus Torvalds 1932bcc5c7d2SAl Viro down_write(&namespace_sem); 193399b7db7bSNick Piggin br_write_lock(vfsmount_lock); 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 19361da177e4SLinus Torvalds * following criteria: 19371da177e4SLinus Torvalds * - only referenced by its parent vfsmount 19381da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 19391da177e4SLinus Torvalds * cleared by mntput()) 19401da177e4SLinus Torvalds */ 19416776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 1942863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 19431ab59738SAl Viro propagate_mount_busy(mnt, 1)) 19441da177e4SLinus Torvalds continue; 19456776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 19461da177e4SLinus Torvalds } 1947bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 19486776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 1949143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1950bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 1951bcc5c7d2SAl Viro } 195299b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 1953bcc5c7d2SAl Viro up_write(&namespace_sem); 1954bcc5c7d2SAl Viro 1955bcc5c7d2SAl Viro release_mounts(&umounts); 19561da177e4SLinus Torvalds } 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds /* 19615528f911STrond Myklebust * Ripoff of 'select_parent()' 19625528f911STrond Myklebust * 19635528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 19645528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 19655528f911STrond Myklebust */ 1966692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 19675528f911STrond Myklebust { 1968692afc31SAl Viro struct mount *this_parent = parent; 19695528f911STrond Myklebust struct list_head *next; 19705528f911STrond Myklebust int found = 0; 19715528f911STrond Myklebust 19725528f911STrond Myklebust repeat: 19736b41d536SAl Viro next = this_parent->mnt_mounts.next; 19745528f911STrond Myklebust resume: 19756b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 19765528f911STrond Myklebust struct list_head *tmp = next; 19776b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 19785528f911STrond Myklebust 19795528f911STrond Myklebust next = tmp->next; 1980692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 19815528f911STrond Myklebust continue; 19825528f911STrond Myklebust /* 19835528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 19845528f911STrond Myklebust */ 19856b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 19865528f911STrond Myklebust this_parent = mnt; 19875528f911STrond Myklebust goto repeat; 19885528f911STrond Myklebust } 19895528f911STrond Myklebust 19901ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 19916776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 19925528f911STrond Myklebust found++; 19935528f911STrond Myklebust } 19945528f911STrond Myklebust } 19955528f911STrond Myklebust /* 19965528f911STrond Myklebust * All done at this level ... ascend and resume the search 19975528f911STrond Myklebust */ 19985528f911STrond Myklebust if (this_parent != parent) { 19996b41d536SAl Viro next = this_parent->mnt_child.next; 20000714a533SAl Viro this_parent = this_parent->mnt_parent; 20015528f911STrond Myklebust goto resume; 20025528f911STrond Myklebust } 20035528f911STrond Myklebust return found; 20045528f911STrond Myklebust } 20055528f911STrond Myklebust 20065528f911STrond Myklebust /* 20075528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 20085528f911STrond Myklebust * submounts of a specific parent mountpoint 200999b7db7bSNick Piggin * 201099b7db7bSNick Piggin * vfsmount_lock must be held for write 20115528f911STrond Myklebust */ 2012692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts) 20135528f911STrond Myklebust { 20145528f911STrond Myklebust LIST_HEAD(graveyard); 2015761d5c38SAl Viro struct mount *m; 20165528f911STrond Myklebust 20175528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2018c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2019bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2020761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 20216776db3dSAl Viro mnt_expire); 2022143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2023afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 2024bcc5c7d2SAl Viro } 2025bcc5c7d2SAl Viro } 20265528f911STrond Myklebust } 20275528f911STrond Myklebust 20285528f911STrond Myklebust /* 20291da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 20301da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 20311da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 20321da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 20331da177e4SLinus Torvalds */ 2034b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2035b58fed8bSRam Pai unsigned long n) 20361da177e4SLinus Torvalds { 20371da177e4SLinus Torvalds char *t = to; 20381da177e4SLinus Torvalds const char __user *f = from; 20391da177e4SLinus Torvalds char c; 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 20421da177e4SLinus Torvalds return n; 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds while (n) { 20451da177e4SLinus Torvalds if (__get_user(c, f)) { 20461da177e4SLinus Torvalds memset(t, 0, n); 20471da177e4SLinus Torvalds break; 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds *t++ = c; 20501da177e4SLinus Torvalds f++; 20511da177e4SLinus Torvalds n--; 20521da177e4SLinus Torvalds } 20531da177e4SLinus Torvalds return n; 20541da177e4SLinus Torvalds } 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 20571da177e4SLinus Torvalds { 20581da177e4SLinus Torvalds int i; 20591da177e4SLinus Torvalds unsigned long page; 20601da177e4SLinus Torvalds unsigned long size; 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds *where = 0; 20631da177e4SLinus Torvalds if (!data) 20641da177e4SLinus Torvalds return 0; 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 20671da177e4SLinus Torvalds return -ENOMEM; 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds /* We only care that *some* data at the address the user 20701da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 20711da177e4SLinus Torvalds * the remainder of the page. 20721da177e4SLinus Torvalds */ 20731da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 20741da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 20751da177e4SLinus Torvalds if (size > PAGE_SIZE) 20761da177e4SLinus Torvalds size = PAGE_SIZE; 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 20791da177e4SLinus Torvalds if (!i) { 20801da177e4SLinus Torvalds free_page(page); 20811da177e4SLinus Torvalds return -EFAULT; 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds if (i != PAGE_SIZE) 20841da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 20851da177e4SLinus Torvalds *where = page; 20861da177e4SLinus Torvalds return 0; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 2089eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2090eca6f534SVegard Nossum { 2091eca6f534SVegard Nossum char *tmp; 2092eca6f534SVegard Nossum 2093eca6f534SVegard Nossum if (!data) { 2094eca6f534SVegard Nossum *where = NULL; 2095eca6f534SVegard Nossum return 0; 2096eca6f534SVegard Nossum } 2097eca6f534SVegard Nossum 2098eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2099eca6f534SVegard Nossum if (IS_ERR(tmp)) 2100eca6f534SVegard Nossum return PTR_ERR(tmp); 2101eca6f534SVegard Nossum 2102eca6f534SVegard Nossum *where = tmp; 2103eca6f534SVegard Nossum return 0; 2104eca6f534SVegard Nossum } 2105eca6f534SVegard Nossum 21061da177e4SLinus Torvalds /* 21071da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 21081da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 21091da177e4SLinus Torvalds * 21101da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 21111da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 21121da177e4SLinus Torvalds * information (or be NULL). 21131da177e4SLinus Torvalds * 21141da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 21151da177e4SLinus Torvalds * When the flags word was introduced its top half was required 21161da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 21171da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 21181da177e4SLinus Torvalds * and must be discarded. 21191da177e4SLinus Torvalds */ 21201da177e4SLinus Torvalds long do_mount(char *dev_name, char *dir_name, char *type_page, 21211da177e4SLinus Torvalds unsigned long flags, void *data_page) 21221da177e4SLinus Torvalds { 21232d92ab3cSAl Viro struct path path; 21241da177e4SLinus Torvalds int retval = 0; 21251da177e4SLinus Torvalds int mnt_flags = 0; 21261da177e4SLinus Torvalds 21271da177e4SLinus Torvalds /* Discard magic */ 21281da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 21291da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 21301da177e4SLinus Torvalds 21311da177e4SLinus Torvalds /* Basic sanity checks */ 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 21341da177e4SLinus Torvalds return -EINVAL; 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds if (data_page) 21371da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 21381da177e4SLinus Torvalds 2139a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2140a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2141a27ab9f2STetsuo Handa if (retval) 2142a27ab9f2STetsuo Handa return retval; 2143a27ab9f2STetsuo Handa 2144a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2145a27ab9f2STetsuo Handa type_page, flags, data_page); 2146a27ab9f2STetsuo Handa if (retval) 2147a27ab9f2STetsuo Handa goto dput_out; 2148a27ab9f2STetsuo Handa 2149613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2150613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 21510a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 21520a1c01c9SMatthew Garrett 21531da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 21541da177e4SLinus Torvalds if (flags & MS_NOSUID) 21551da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 21561da177e4SLinus Torvalds if (flags & MS_NODEV) 21571da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 21581da177e4SLinus Torvalds if (flags & MS_NOEXEC) 21591da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2160fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2161fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2162fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2163fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2164d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2165d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 21662e4b7fcdSDave Hansen if (flags & MS_RDONLY) 21672e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2168fc33a7bbSChristoph Hellwig 21697a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2170d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2171d0adde57SMatthew Garrett MS_STRICTATIME); 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds if (flags & MS_REMOUNT) 21742d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 21751da177e4SLinus Torvalds data_page); 21761da177e4SLinus Torvalds else if (flags & MS_BIND) 21772d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 21789676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21792d92ab3cSAl Viro retval = do_change_type(&path, flags); 21801da177e4SLinus Torvalds else if (flags & MS_MOVE) 21812d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 21821da177e4SLinus Torvalds else 21832d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 21841da177e4SLinus Torvalds dev_name, data_page); 21851da177e4SLinus Torvalds dput_out: 21862d92ab3cSAl Viro path_put(&path); 21871da177e4SLinus Torvalds return retval; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 2190cf8d2c11STrond Myklebust static struct mnt_namespace *alloc_mnt_ns(void) 2191cf8d2c11STrond Myklebust { 2192cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2193cf8d2c11STrond Myklebust 2194cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2195cf8d2c11STrond Myklebust if (!new_ns) 2196cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2197cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2198cf8d2c11STrond Myklebust new_ns->root = NULL; 2199cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2200cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2201cf8d2c11STrond Myklebust new_ns->event = 0; 2202cf8d2c11STrond Myklebust return new_ns; 2203cf8d2c11STrond Myklebust } 2204cf8d2c11STrond Myklebust 2205f03c6599SAl Viro void mnt_make_longterm(struct vfsmount *mnt) 2206f03c6599SAl Viro { 220783adc753SAl Viro __mnt_make_longterm(real_mount(mnt)); 2208f03c6599SAl Viro } 2209f03c6599SAl Viro 221083adc753SAl Viro void mnt_make_shortterm(struct vfsmount *m) 2211f03c6599SAl Viro { 22127e3d0eb0SAl Viro #ifdef CONFIG_SMP 221383adc753SAl Viro struct mount *mnt = real_mount(m); 221468e8a9feSAl Viro if (atomic_add_unless(&mnt->mnt_longterm, -1, 1)) 2215f03c6599SAl Viro return; 2216f03c6599SAl Viro br_write_lock(vfsmount_lock); 221768e8a9feSAl Viro atomic_dec(&mnt->mnt_longterm); 2218f03c6599SAl Viro br_write_unlock(vfsmount_lock); 22197e3d0eb0SAl Viro #endif 2220f03c6599SAl Viro } 2221f03c6599SAl Viro 2222741a2951SJANAK DESAI /* 2223741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2224741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2225741a2951SJANAK DESAI */ 2226e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 22276b3286edSKirill Korotaev struct fs_struct *fs) 22281da177e4SLinus Torvalds { 22296b3286edSKirill Korotaev struct mnt_namespace *new_ns; 22307f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2231315fc83eSAl Viro struct mount *p, *q; 2232be08d6d2SAl Viro struct mount *old = mnt_ns->root; 2233cb338d06SAl Viro struct mount *new; 22341da177e4SLinus Torvalds 2235cf8d2c11STrond Myklebust new_ns = alloc_mnt_ns(); 2236cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2237cf8d2c11STrond Myklebust return new_ns; 22381da177e4SLinus Torvalds 2239390c6843SRam Pai down_write(&namespace_sem); 22401da177e4SLinus Torvalds /* First pass: copy the tree topology */ 2241909b0a88SAl Viro new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE); 2242cb338d06SAl Viro if (!new) { 2243390c6843SRam Pai up_write(&namespace_sem); 22441da177e4SLinus Torvalds kfree(new_ns); 22455cc4a034SJulia Lawall return ERR_PTR(-ENOMEM); 22461da177e4SLinus Torvalds } 2247be08d6d2SAl Viro new_ns->root = new; 224899b7db7bSNick Piggin br_write_lock(vfsmount_lock); 22491a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 225099b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds /* 22531da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 22541da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 22551da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 22561da177e4SLinus Torvalds */ 2257909b0a88SAl Viro p = old; 2258cb338d06SAl Viro q = new; 22591da177e4SLinus Torvalds while (p) { 2260143c8c91SAl Viro q->mnt_ns = new_ns; 226183adc753SAl Viro __mnt_make_longterm(q); 22621da177e4SLinus Torvalds if (fs) { 2263315fc83eSAl Viro if (&p->mnt == fs->root.mnt) { 2264315fc83eSAl Viro fs->root.mnt = mntget(&q->mnt); 226583adc753SAl Viro __mnt_make_longterm(q); 2266315fc83eSAl Viro mnt_make_shortterm(&p->mnt); 2267315fc83eSAl Viro rootmnt = &p->mnt; 22681da177e4SLinus Torvalds } 2269315fc83eSAl Viro if (&p->mnt == fs->pwd.mnt) { 2270315fc83eSAl Viro fs->pwd.mnt = mntget(&q->mnt); 227183adc753SAl Viro __mnt_make_longterm(q); 2272315fc83eSAl Viro mnt_make_shortterm(&p->mnt); 2273315fc83eSAl Viro pwdmnt = &p->mnt; 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds } 2276909b0a88SAl Viro p = next_mnt(p, old); 2277909b0a88SAl Viro q = next_mnt(q, new); 22781da177e4SLinus Torvalds } 2279390c6843SRam Pai up_write(&namespace_sem); 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds if (rootmnt) 2282f03c6599SAl Viro mntput(rootmnt); 22831da177e4SLinus Torvalds if (pwdmnt) 2284f03c6599SAl Viro mntput(pwdmnt); 22851da177e4SLinus Torvalds 2286741a2951SJANAK DESAI return new_ns; 2287741a2951SJANAK DESAI } 2288741a2951SJANAK DESAI 2289213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2290e3222c4eSBadari Pulavarty struct fs_struct *new_fs) 2291741a2951SJANAK DESAI { 22926b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2293741a2951SJANAK DESAI 2294e3222c4eSBadari Pulavarty BUG_ON(!ns); 22956b3286edSKirill Korotaev get_mnt_ns(ns); 2296741a2951SJANAK DESAI 2297741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2298e3222c4eSBadari Pulavarty return ns; 2299741a2951SJANAK DESAI 2300e3222c4eSBadari Pulavarty new_ns = dup_mnt_ns(ns, new_fs); 2301741a2951SJANAK DESAI 23026b3286edSKirill Korotaev put_mnt_ns(ns); 2303e3222c4eSBadari Pulavarty return new_ns; 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 2306cf8d2c11STrond Myklebust /** 2307cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2308cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2309cf8d2c11STrond Myklebust */ 23101a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2311cf8d2c11STrond Myklebust { 23121a4eeaf2SAl Viro struct mnt_namespace *new_ns = alloc_mnt_ns(); 2313cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 23141a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 23151a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 23161a4eeaf2SAl Viro __mnt_make_longterm(mnt); 2317be08d6d2SAl Viro new_ns->root = mnt; 23181a4eeaf2SAl Viro list_add(&new_ns->list, &mnt->mnt_list); 2319c1334495SAl Viro } else { 23201a4eeaf2SAl Viro mntput(m); 2321cf8d2c11STrond Myklebust } 2322cf8d2c11STrond Myklebust return new_ns; 2323cf8d2c11STrond Myklebust } 2324cf8d2c11STrond Myklebust 2325ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2326ea441d11SAl Viro { 2327ea441d11SAl Viro struct mnt_namespace *ns; 2328d31da0f0SAl Viro struct super_block *s; 2329ea441d11SAl Viro struct path path; 2330ea441d11SAl Viro int err; 2331ea441d11SAl Viro 2332ea441d11SAl Viro ns = create_mnt_ns(mnt); 2333ea441d11SAl Viro if (IS_ERR(ns)) 2334ea441d11SAl Viro return ERR_CAST(ns); 2335ea441d11SAl Viro 2336ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2337ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2338ea441d11SAl Viro 2339ea441d11SAl Viro put_mnt_ns(ns); 2340ea441d11SAl Viro 2341ea441d11SAl Viro if (err) 2342ea441d11SAl Viro return ERR_PTR(err); 2343ea441d11SAl Viro 2344ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2345d31da0f0SAl Viro s = path.mnt->mnt_sb; 2346d31da0f0SAl Viro atomic_inc(&s->s_active); 2347ea441d11SAl Viro mntput(path.mnt); 2348ea441d11SAl Viro /* lock the sucker */ 2349d31da0f0SAl Viro down_write(&s->s_umount); 2350ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2351ea441d11SAl Viro return path.dentry; 2352ea441d11SAl Viro } 2353ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2354ea441d11SAl Viro 2355bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2356bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 23571da177e4SLinus Torvalds { 2358eca6f534SVegard Nossum int ret; 2359eca6f534SVegard Nossum char *kernel_type; 2360eca6f534SVegard Nossum char *kernel_dir; 2361eca6f534SVegard Nossum char *kernel_dev; 23621da177e4SLinus Torvalds unsigned long data_page; 23631da177e4SLinus Torvalds 2364eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2365eca6f534SVegard Nossum if (ret < 0) 2366eca6f534SVegard Nossum goto out_type; 23671da177e4SLinus Torvalds 2368eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2369eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2370eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2371eca6f534SVegard Nossum goto out_dir; 2372eca6f534SVegard Nossum } 23731da177e4SLinus Torvalds 2374eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2375eca6f534SVegard Nossum if (ret < 0) 2376eca6f534SVegard Nossum goto out_dev; 23771da177e4SLinus Torvalds 2378eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2379eca6f534SVegard Nossum if (ret < 0) 2380eca6f534SVegard Nossum goto out_data; 23811da177e4SLinus Torvalds 2382eca6f534SVegard Nossum ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags, 2383eca6f534SVegard Nossum (void *) data_page); 2384eca6f534SVegard Nossum 23851da177e4SLinus Torvalds free_page(data_page); 2386eca6f534SVegard Nossum out_data: 2387eca6f534SVegard Nossum kfree(kernel_dev); 2388eca6f534SVegard Nossum out_dev: 2389eca6f534SVegard Nossum putname(kernel_dir); 2390eca6f534SVegard Nossum out_dir: 2391eca6f534SVegard Nossum kfree(kernel_type); 2392eca6f534SVegard Nossum out_type: 2393eca6f534SVegard Nossum return ret; 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds /* 2397afac7cbaSAl Viro * Return true if path is reachable from root 2398afac7cbaSAl Viro * 2399afac7cbaSAl Viro * namespace_sem or vfsmount_lock is held 2400afac7cbaSAl Viro */ 2401643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2402afac7cbaSAl Viro const struct path *root) 2403afac7cbaSAl Viro { 2404643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2405a73324daSAl Viro dentry = mnt->mnt_mountpoint; 24060714a533SAl Viro mnt = mnt->mnt_parent; 2407afac7cbaSAl Viro } 2408643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2409afac7cbaSAl Viro } 2410afac7cbaSAl Viro 2411afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2412afac7cbaSAl Viro { 2413afac7cbaSAl Viro int res; 2414afac7cbaSAl Viro br_read_lock(vfsmount_lock); 2415643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 2416afac7cbaSAl Viro br_read_unlock(vfsmount_lock); 2417afac7cbaSAl Viro return res; 2418afac7cbaSAl Viro } 2419afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2420afac7cbaSAl Viro 2421afac7cbaSAl Viro /* 24221da177e4SLinus Torvalds * pivot_root Semantics: 24231da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 24241da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 24251da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 24261da177e4SLinus Torvalds * 24271da177e4SLinus Torvalds * Restrictions: 24281da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 24291da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 24301da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 24311da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 24321da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 24331da177e4SLinus Torvalds * 24344a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 24354a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 24364a0d11faSNeil Brown * in this situation. 24374a0d11faSNeil Brown * 24381da177e4SLinus Torvalds * Notes: 24391da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 24401da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 24411da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 24421da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 24431da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 24441da177e4SLinus Torvalds * first. 24451da177e4SLinus Torvalds */ 24463480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 24473480b257SHeiko Carstens const char __user *, put_old) 24481da177e4SLinus Torvalds { 24492d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 2450419148daSAl Viro struct mount *new_mnt, *root_mnt; 24511da177e4SLinus Torvalds int error; 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 24541da177e4SLinus Torvalds return -EPERM; 24551da177e4SLinus Torvalds 24562d8f3038SAl Viro error = user_path_dir(new_root, &new); 24571da177e4SLinus Torvalds if (error) 24581da177e4SLinus Torvalds goto out0; 24591da177e4SLinus Torvalds 24602d8f3038SAl Viro error = user_path_dir(put_old, &old); 24611da177e4SLinus Torvalds if (error) 24621da177e4SLinus Torvalds goto out1; 24631da177e4SLinus Torvalds 24642d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2465b12cea91SAl Viro if (error) 2466b12cea91SAl Viro goto out2; 24671da177e4SLinus Torvalds 2468f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 2469b12cea91SAl Viro error = lock_mount(&old); 2470b12cea91SAl Viro if (error) 2471b12cea91SAl Viro goto out3; 2472b12cea91SAl Viro 24731da177e4SLinus Torvalds error = -EINVAL; 2474419148daSAl Viro new_mnt = real_mount(new.mnt); 2475419148daSAl Viro root_mnt = real_mount(root.mnt); 2476fc7be130SAl Viro if (IS_MNT_SHARED(real_mount(old.mnt)) || 2477fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2478fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2479b12cea91SAl Viro goto out4; 2480143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2481b12cea91SAl Viro goto out4; 24821da177e4SLinus Torvalds error = -ENOENT; 2483f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2484b12cea91SAl Viro goto out4; 2485f3da392eSAlexey Dobriyan if (d_unlinked(old.dentry)) 2486b12cea91SAl Viro goto out4; 24871da177e4SLinus Torvalds error = -EBUSY; 24882d8f3038SAl Viro if (new.mnt == root.mnt || 24892d8f3038SAl Viro old.mnt == root.mnt) 2490b12cea91SAl Viro goto out4; /* loop, on the same file system */ 24911da177e4SLinus Torvalds error = -EINVAL; 24928c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2493b12cea91SAl Viro goto out4; /* not a mountpoint */ 2494676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2495b12cea91SAl Viro goto out4; /* not attached */ 24962d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2497b12cea91SAl Viro goto out4; /* not a mountpoint */ 2498676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2499b12cea91SAl Viro goto out4; /* not attached */ 25004ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 2501643822b4SAl Viro if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new)) 2502b12cea91SAl Viro goto out4; 250327cb1572SAl Viro br_write_lock(vfsmount_lock); 2504419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2505419148daSAl Viro detach_mnt(root_mnt, &root_parent); 25064ac91378SJan Blunck /* mount old root on put_old */ 2507419148daSAl Viro attach_mnt(root_mnt, &old); 25084ac91378SJan Blunck /* mount new_root on / */ 2509419148daSAl Viro attach_mnt(new_mnt, &root_parent); 25106b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 251199b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 25122d8f3038SAl Viro chroot_fs_refs(&root, &new); 25131da177e4SLinus Torvalds error = 0; 2514b12cea91SAl Viro out4: 2515b12cea91SAl Viro unlock_mount(&old); 2516b12cea91SAl Viro if (!error) { 25171a390689SAl Viro path_put(&root_parent); 25181a390689SAl Viro path_put(&parent_path); 2519b12cea91SAl Viro } 2520b12cea91SAl Viro out3: 25218c3ee42eSAl Viro path_put(&root); 2522b12cea91SAl Viro out2: 25232d8f3038SAl Viro path_put(&old); 25241da177e4SLinus Torvalds out1: 25252d8f3038SAl Viro path_put(&new); 25261da177e4SLinus Torvalds out0: 25271da177e4SLinus Torvalds return error; 25281da177e4SLinus Torvalds } 25291da177e4SLinus Torvalds 25301da177e4SLinus Torvalds static void __init init_mount_tree(void) 25311da177e4SLinus Torvalds { 25321da177e4SLinus Torvalds struct vfsmount *mnt; 25336b3286edSKirill Korotaev struct mnt_namespace *ns; 2534ac748a09SJan Blunck struct path root; 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); 25371da177e4SLinus Torvalds if (IS_ERR(mnt)) 25381da177e4SLinus Torvalds panic("Can't create rootfs"); 2539b3e19d92SNick Piggin 25403b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 25413b22edc5STrond Myklebust if (IS_ERR(ns)) 25421da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 25431da177e4SLinus Torvalds 25446b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 25456b3286edSKirill Korotaev get_mnt_ns(ns); 25461da177e4SLinus Torvalds 2547be08d6d2SAl Viro root.mnt = mnt; 2548be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2549ac748a09SJan Blunck 2550ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2551ac748a09SJan Blunck set_fs_root(current->fs, &root); 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds 255474bf17cfSDenis Cheng void __init mnt_init(void) 25551da177e4SLinus Torvalds { 255613f14b4dSEric Dumazet unsigned u; 255715a67dd8SRandy Dunlap int err; 25581da177e4SLinus Torvalds 2559390c6843SRam Pai init_rwsem(&namespace_sem); 2560390c6843SRam Pai 25617d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 256220c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 25631da177e4SLinus Torvalds 2564b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 25651da177e4SLinus Torvalds 25661da177e4SLinus Torvalds if (!mount_hashtable) 25671da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 25681da177e4SLinus Torvalds 256980cdc6daSMandeep Singh Baines printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE); 25701da177e4SLinus Torvalds 257113f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 257213f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 25731da177e4SLinus Torvalds 257499b7db7bSNick Piggin br_lock_init(vfsmount_lock); 257599b7db7bSNick Piggin 257615a67dd8SRandy Dunlap err = sysfs_init(); 257715a67dd8SRandy Dunlap if (err) 257815a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 25798e24eea7SHarvey Harrison __func__, err); 258000d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 258100d26666SGreg Kroah-Hartman if (!fs_kobj) 25828e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 25831da177e4SLinus Torvalds init_rootfs(); 25841da177e4SLinus Torvalds init_mount_tree(); 25851da177e4SLinus Torvalds } 25861da177e4SLinus Torvalds 2587616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 25881da177e4SLinus Torvalds { 258970fbcdf4SRam Pai LIST_HEAD(umount_list); 2590616511d0STrond Myklebust 2591d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2592616511d0STrond Myklebust return; 2593390c6843SRam Pai down_write(&namespace_sem); 259499b7db7bSNick Piggin br_write_lock(vfsmount_lock); 2595be08d6d2SAl Viro umount_tree(ns->root, 0, &umount_list); 259699b7db7bSNick Piggin br_write_unlock(vfsmount_lock); 2597390c6843SRam Pai up_write(&namespace_sem); 259870fbcdf4SRam Pai release_mounts(&umount_list); 25996b3286edSKirill Korotaev kfree(ns); 26001da177e4SLinus Torvalds } 26019d412a43SAl Viro 26029d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 26039d412a43SAl Viro { 2604423e0ab0STim Chen struct vfsmount *mnt; 2605423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2606423e0ab0STim Chen if (!IS_ERR(mnt)) { 2607423e0ab0STim Chen /* 2608423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2609423e0ab0STim Chen * we unmount before file sys is unregistered 2610423e0ab0STim Chen */ 2611423e0ab0STim Chen mnt_make_longterm(mnt); 2612423e0ab0STim Chen } 2613423e0ab0STim Chen return mnt; 26149d412a43SAl Viro } 26159d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2616423e0ab0STim Chen 2617423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2618423e0ab0STim Chen { 2619423e0ab0STim Chen /* release long term mount so mount point can be released */ 2620423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2621423e0ab0STim Chen mnt_make_shortterm(mnt); 2622423e0ab0STim Chen mntput(mnt); 2623423e0ab0STim Chen } 2624423e0ab0STim Chen } 2625423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 262602125a82SAl Viro 262702125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 262802125a82SAl Viro { 2629143c8c91SAl Viro return check_mnt(real_mount(mnt)); 263002125a82SAl Viro } 2631