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> 15771b1371SEric W. Biederman #include <linux/user_namespace.h> 161da177e4SLinus Torvalds #include <linux/namei.h> 171da177e4SLinus Torvalds #include <linux/security.h> 1873cd49ecSMiklos Szeredi #include <linux/idr.h> 19d10577a8SAl Viro #include <linux/acct.h> /* acct_auto_close_mnt */ 20d10577a8SAl Viro #include <linux/ramfs.h> /* init_rootfs */ 21d10577a8SAl Viro #include <linux/fs_struct.h> /* get_fs_root et.al. */ 22d10577a8SAl Viro #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */ 23d10577a8SAl Viro #include <linux/uaccess.h> 248823c079SEric W. Biederman #include <linux/proc_fs.h> 2507b20889SRam Pai #include "pnode.h" 26948730b0SAdrian Bunk #include "internal.h" 271da177e4SLinus Torvalds 2813f14b4dSEric Dumazet #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head)) 2913f14b4dSEric Dumazet #define HASH_SIZE (1UL << HASH_SHIFT) 3013f14b4dSEric Dumazet 315addc5ddSAl Viro static int event; 3273cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 33719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 3499b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock); 35f21f6220SAl Viro static int mnt_id_start = 0; 36f21f6220SAl Viro static int mnt_group_start = 1; 375addc5ddSAl Viro 38fa3536ccSEric Dumazet static struct list_head *mount_hashtable __read_mostly; 3984d17192SAl Viro static struct list_head *mountpoint_hashtable __read_mostly; 40e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 41390c6843SRam Pai static struct rw_semaphore namespace_sem; 421da177e4SLinus Torvalds 43f87fd4c2SMiklos Szeredi /* /sys/fs */ 4400d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 4500d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 46f87fd4c2SMiklos Szeredi 4799b7db7bSNick Piggin /* 4899b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 4999b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 5099b7db7bSNick Piggin * up the tree. 5199b7db7bSNick Piggin * 5299b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 5399b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 5499b7db7bSNick Piggin */ 5599b7db7bSNick Piggin DEFINE_BRLOCK(vfsmount_lock); 5699b7db7bSNick Piggin 571da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 601da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 6113f14b4dSEric Dumazet tmp = tmp + (tmp >> HASH_SHIFT); 6213f14b4dSEric Dumazet return tmp & (HASH_SIZE - 1); 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds 653d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16) 663d733633SDave Hansen 6799b7db7bSNick Piggin /* 6899b7db7bSNick Piggin * allocation is serialized by namespace_sem, but we need the spinlock to 6999b7db7bSNick Piggin * serialize with freeing. 7099b7db7bSNick Piggin */ 71b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 7273cd49ecSMiklos Szeredi { 7373cd49ecSMiklos Szeredi int res; 7473cd49ecSMiklos Szeredi 7573cd49ecSMiklos Szeredi retry: 7673cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 7799b7db7bSNick Piggin spin_lock(&mnt_id_lock); 7815169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 79f21f6220SAl Viro if (!res) 8015169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 8199b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 8273cd49ecSMiklos Szeredi if (res == -EAGAIN) 8373cd49ecSMiklos Szeredi goto retry; 8473cd49ecSMiklos Szeredi 8573cd49ecSMiklos Szeredi return res; 8673cd49ecSMiklos Szeredi } 8773cd49ecSMiklos Szeredi 88b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 8973cd49ecSMiklos Szeredi { 9015169fe7SAl Viro int id = mnt->mnt_id; 9199b7db7bSNick Piggin spin_lock(&mnt_id_lock); 92f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 93f21f6220SAl Viro if (mnt_id_start > id) 94f21f6220SAl Viro mnt_id_start = id; 9599b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 9673cd49ecSMiklos Szeredi } 9773cd49ecSMiklos Szeredi 98719f5d7fSMiklos Szeredi /* 99719f5d7fSMiklos Szeredi * Allocate a new peer group ID 100719f5d7fSMiklos Szeredi * 101719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 102719f5d7fSMiklos Szeredi */ 1034b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 104719f5d7fSMiklos Szeredi { 105f21f6220SAl Viro int res; 106f21f6220SAl Viro 107719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 108719f5d7fSMiklos Szeredi return -ENOMEM; 109719f5d7fSMiklos Szeredi 110f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 111f21f6220SAl Viro mnt_group_start, 11215169fe7SAl Viro &mnt->mnt_group_id); 113f21f6220SAl Viro if (!res) 11415169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 115f21f6220SAl Viro 116f21f6220SAl Viro return res; 117719f5d7fSMiklos Szeredi } 118719f5d7fSMiklos Szeredi 119719f5d7fSMiklos Szeredi /* 120719f5d7fSMiklos Szeredi * Release a peer group ID 121719f5d7fSMiklos Szeredi */ 1224b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 123719f5d7fSMiklos Szeredi { 12415169fe7SAl Viro int id = mnt->mnt_group_id; 125f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 126f21f6220SAl Viro if (mnt_group_start > id) 127f21f6220SAl Viro mnt_group_start = id; 12815169fe7SAl Viro mnt->mnt_group_id = 0; 129719f5d7fSMiklos Szeredi } 130719f5d7fSMiklos Szeredi 131b3e19d92SNick Piggin /* 132b3e19d92SNick Piggin * vfsmount lock must be held for read 133b3e19d92SNick Piggin */ 13483adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 135b3e19d92SNick Piggin { 136b3e19d92SNick Piggin #ifdef CONFIG_SMP 13768e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 138b3e19d92SNick Piggin #else 139b3e19d92SNick Piggin preempt_disable(); 14068e8a9feSAl Viro mnt->mnt_count += n; 141b3e19d92SNick Piggin preempt_enable(); 142b3e19d92SNick Piggin #endif 143b3e19d92SNick Piggin } 144b3e19d92SNick Piggin 145b3e19d92SNick Piggin /* 146b3e19d92SNick Piggin * vfsmount lock must be held for write 147b3e19d92SNick Piggin */ 14883adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 149b3e19d92SNick Piggin { 150b3e19d92SNick Piggin #ifdef CONFIG_SMP 151f03c6599SAl Viro unsigned int count = 0; 152b3e19d92SNick Piggin int cpu; 153b3e19d92SNick Piggin 154b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 15568e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 156b3e19d92SNick Piggin } 157b3e19d92SNick Piggin 158b3e19d92SNick Piggin return count; 159b3e19d92SNick Piggin #else 16068e8a9feSAl Viro return mnt->mnt_count; 161b3e19d92SNick Piggin #endif 162b3e19d92SNick Piggin } 163b3e19d92SNick Piggin 164b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1651da177e4SLinus Torvalds { 166c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 167c63181e6SAl Viro if (mnt) { 16873cd49ecSMiklos Szeredi int err; 16973cd49ecSMiklos Szeredi 170c63181e6SAl Viro err = mnt_alloc_id(mnt); 17188b38782SLi Zefan if (err) 17288b38782SLi Zefan goto out_free_cache; 17388b38782SLi Zefan 17488b38782SLi Zefan if (name) { 175c63181e6SAl Viro mnt->mnt_devname = kstrdup(name, GFP_KERNEL); 176c63181e6SAl Viro if (!mnt->mnt_devname) 17788b38782SLi Zefan goto out_free_id; 17873cd49ecSMiklos Szeredi } 17973cd49ecSMiklos Szeredi 180b3e19d92SNick Piggin #ifdef CONFIG_SMP 181c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 182c63181e6SAl Viro if (!mnt->mnt_pcp) 183b3e19d92SNick Piggin goto out_free_devname; 184b3e19d92SNick Piggin 185c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 186b3e19d92SNick Piggin #else 187c63181e6SAl Viro mnt->mnt_count = 1; 188c63181e6SAl Viro mnt->mnt_writers = 0; 189b3e19d92SNick Piggin #endif 190b3e19d92SNick Piggin 191c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_hash); 192c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 193c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 194c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 195c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 196c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 197c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 198c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 1992504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 2002504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 2012504c5d6SAndreas Gruenbacher #endif 2021da177e4SLinus Torvalds } 203c63181e6SAl Viro return mnt; 20488b38782SLi Zefan 205d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 206d3ef3d73Snpiggin@suse.de out_free_devname: 207c63181e6SAl Viro kfree(mnt->mnt_devname); 208d3ef3d73Snpiggin@suse.de #endif 20988b38782SLi Zefan out_free_id: 210c63181e6SAl Viro mnt_free_id(mnt); 21188b38782SLi Zefan out_free_cache: 212c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 21388b38782SLi Zefan return NULL; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2168366025eSDave Hansen /* 2178366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2188366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2198366025eSDave Hansen * We must keep track of when those operations start 2208366025eSDave Hansen * (for permission checks) and when they end, so that 2218366025eSDave Hansen * we can determine when writes are able to occur to 2228366025eSDave Hansen * a filesystem. 2238366025eSDave Hansen */ 2243d733633SDave Hansen /* 2253d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2263d733633SDave Hansen * @mnt: the mount to check for its write status 2273d733633SDave Hansen * 2283d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2293d733633SDave Hansen * It does not guarantee that the filesystem will stay 2303d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2313d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2323d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2333d733633SDave Hansen * r/w. 2343d733633SDave Hansen */ 2353d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2363d733633SDave Hansen { 2372e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2382e4b7fcdSDave Hansen return 1; 2392e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2402e4b7fcdSDave Hansen return 1; 2412e4b7fcdSDave Hansen return 0; 2423d733633SDave Hansen } 2433d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2443d733633SDave Hansen 24583adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2463d733633SDave Hansen { 247d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 24868e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 249d3ef3d73Snpiggin@suse.de #else 25068e8a9feSAl Viro mnt->mnt_writers++; 251d3ef3d73Snpiggin@suse.de #endif 2523d733633SDave Hansen } 2533d733633SDave Hansen 25483adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2553d733633SDave Hansen { 256d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 25768e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 258d3ef3d73Snpiggin@suse.de #else 25968e8a9feSAl Viro mnt->mnt_writers--; 260d3ef3d73Snpiggin@suse.de #endif 261d3ef3d73Snpiggin@suse.de } 262d3ef3d73Snpiggin@suse.de 26383adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 264d3ef3d73Snpiggin@suse.de { 265d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 266d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2673d733633SDave Hansen int cpu; 2683d733633SDave Hansen 2693d733633SDave Hansen for_each_possible_cpu(cpu) { 27068e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2713d733633SDave Hansen } 2723d733633SDave Hansen 273d3ef3d73Snpiggin@suse.de return count; 274d3ef3d73Snpiggin@suse.de #else 275d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 276d3ef3d73Snpiggin@suse.de #endif 2773d733633SDave Hansen } 2783d733633SDave Hansen 2794ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2804ed5e82fSMiklos Szeredi { 2814ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 2824ed5e82fSMiklos Szeredi return 1; 2834ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 2844ed5e82fSMiklos Szeredi smp_rmb(); 2854ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 2864ed5e82fSMiklos Szeredi } 2874ed5e82fSMiklos Szeredi 2883d733633SDave Hansen /* 289eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 290eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 291eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 292eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 2933d733633SDave Hansen */ 2948366025eSDave Hansen /** 295eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 29683adc753SAl Viro * @m: the mount on which to take a write 2978366025eSDave Hansen * 298eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 299eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 300eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 301eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 302eb04c282SJan Kara * called. This is effectively a refcount. 3038366025eSDave Hansen */ 304eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3058366025eSDave Hansen { 30683adc753SAl Viro struct mount *mnt = real_mount(m); 3073d733633SDave Hansen int ret = 0; 3083d733633SDave Hansen 309d3ef3d73Snpiggin@suse.de preempt_disable(); 310c6653a83SNick Piggin mnt_inc_writers(mnt); 311d3ef3d73Snpiggin@suse.de /* 312c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 313d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 314d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 315d3ef3d73Snpiggin@suse.de */ 316d3ef3d73Snpiggin@suse.de smp_mb(); 3171e75529eSMiao Xie while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 318d3ef3d73Snpiggin@suse.de cpu_relax(); 319d3ef3d73Snpiggin@suse.de /* 320d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 321d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 322d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 323d3ef3d73Snpiggin@suse.de */ 324d3ef3d73Snpiggin@suse.de smp_rmb(); 3254ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 326c6653a83SNick Piggin mnt_dec_writers(mnt); 3273d733633SDave Hansen ret = -EROFS; 3283d733633SDave Hansen } 329d3ef3d73Snpiggin@suse.de preempt_enable(); 330eb04c282SJan Kara 331eb04c282SJan Kara return ret; 332eb04c282SJan Kara } 333eb04c282SJan Kara 334eb04c282SJan Kara /** 335eb04c282SJan Kara * mnt_want_write - get write access to a mount 336eb04c282SJan Kara * @m: the mount on which to take a write 337eb04c282SJan Kara * 338eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 339eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 340eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 341eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 342eb04c282SJan Kara */ 343eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 344eb04c282SJan Kara { 345eb04c282SJan Kara int ret; 346eb04c282SJan Kara 347eb04c282SJan Kara sb_start_write(m->mnt_sb); 348eb04c282SJan Kara ret = __mnt_want_write(m); 349eb04c282SJan Kara if (ret) 350eb04c282SJan Kara sb_end_write(m->mnt_sb); 3513d733633SDave Hansen return ret; 3528366025eSDave Hansen } 3538366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3548366025eSDave Hansen 3558366025eSDave Hansen /** 35696029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 35796029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 35896029c4eSnpiggin@suse.de * 35996029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 36096029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 36196029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 36296029c4eSnpiggin@suse.de * on it. This allows some optimisation. 36396029c4eSnpiggin@suse.de * 36496029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 36596029c4eSnpiggin@suse.de * drop the reference. 36696029c4eSnpiggin@suse.de */ 36796029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 36896029c4eSnpiggin@suse.de { 36996029c4eSnpiggin@suse.de /* superblock may be r/o */ 37096029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 37196029c4eSnpiggin@suse.de return -EROFS; 37296029c4eSnpiggin@suse.de preempt_disable(); 37383adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 37496029c4eSnpiggin@suse.de preempt_enable(); 37596029c4eSnpiggin@suse.de return 0; 37696029c4eSnpiggin@suse.de } 37796029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 37896029c4eSnpiggin@suse.de 37996029c4eSnpiggin@suse.de /** 380eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 381eb04c282SJan Kara * @file: the file who's mount on which to take a write 382eb04c282SJan Kara * 383eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 384eb04c282SJan Kara * do some optimisations if the file is open for write already 385eb04c282SJan Kara */ 386eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 387eb04c282SJan Kara { 388496ad9aaSAl Viro struct inode *inode = file_inode(file); 389eb04c282SJan Kara 390eb04c282SJan Kara if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 391eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 392eb04c282SJan Kara else 393eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 394eb04c282SJan Kara } 395eb04c282SJan Kara 396eb04c282SJan Kara /** 39796029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 39896029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 39996029c4eSnpiggin@suse.de * 40096029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 40196029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 40296029c4eSnpiggin@suse.de */ 40396029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 40496029c4eSnpiggin@suse.de { 405eb04c282SJan Kara int ret; 406eb04c282SJan Kara 407eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 408eb04c282SJan Kara ret = __mnt_want_write_file(file); 409eb04c282SJan Kara if (ret) 410eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 411eb04c282SJan Kara return ret; 41296029c4eSnpiggin@suse.de } 41396029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 41496029c4eSnpiggin@suse.de 41596029c4eSnpiggin@suse.de /** 416eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4178366025eSDave Hansen * @mnt: the mount on which to give up write access 4188366025eSDave Hansen * 4198366025eSDave Hansen * Tells the low-level filesystem that we are done 4208366025eSDave Hansen * performing writes to it. Must be matched with 421eb04c282SJan Kara * __mnt_want_write() call above. 4228366025eSDave Hansen */ 423eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4248366025eSDave Hansen { 425d3ef3d73Snpiggin@suse.de preempt_disable(); 42683adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 427d3ef3d73Snpiggin@suse.de preempt_enable(); 4288366025eSDave Hansen } 429eb04c282SJan Kara 430eb04c282SJan Kara /** 431eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 432eb04c282SJan Kara * @mnt: the mount on which to give up write access 433eb04c282SJan Kara * 434eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 435eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 436eb04c282SJan Kara * mnt_want_write() call above. 437eb04c282SJan Kara */ 438eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 439eb04c282SJan Kara { 440eb04c282SJan Kara __mnt_drop_write(mnt); 441eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 442eb04c282SJan Kara } 4438366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4448366025eSDave Hansen 445eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 446eb04c282SJan Kara { 447eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 448eb04c282SJan Kara } 449eb04c282SJan Kara 4502a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4512a79f17eSAl Viro { 4522a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4532a79f17eSAl Viro } 4542a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4552a79f17eSAl Viro 45683adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4578366025eSDave Hansen { 4583d733633SDave Hansen int ret = 0; 4593d733633SDave Hansen 460962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 46183adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 462d3ef3d73Snpiggin@suse.de /* 463d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 464d3ef3d73Snpiggin@suse.de * should be visible before we do. 465d3ef3d73Snpiggin@suse.de */ 466d3ef3d73Snpiggin@suse.de smp_mb(); 467d3ef3d73Snpiggin@suse.de 468d3ef3d73Snpiggin@suse.de /* 469d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 470d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 471d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 472d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 473d3ef3d73Snpiggin@suse.de * 474d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 475d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 476d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 477d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 478d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 479d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 480d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 481d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 482d3ef3d73Snpiggin@suse.de * we're counting up here. 483d3ef3d73Snpiggin@suse.de */ 484c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 485d3ef3d73Snpiggin@suse.de ret = -EBUSY; 486d3ef3d73Snpiggin@suse.de else 48783adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 488d3ef3d73Snpiggin@suse.de /* 489d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 490d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 491d3ef3d73Snpiggin@suse.de */ 492d3ef3d73Snpiggin@suse.de smp_wmb(); 49383adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 494962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 4953d733633SDave Hansen return ret; 4963d733633SDave Hansen } 4978366025eSDave Hansen 49883adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 4992e4b7fcdSDave Hansen { 500962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 50183adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 502962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 5032e4b7fcdSDave Hansen } 5042e4b7fcdSDave Hansen 5054ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5064ed5e82fSMiklos Szeredi { 5074ed5e82fSMiklos Szeredi struct mount *mnt; 5084ed5e82fSMiklos Szeredi int err = 0; 5094ed5e82fSMiklos Szeredi 5108e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5118e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5128e8b8796SMiklos Szeredi return -EBUSY; 5138e8b8796SMiklos Szeredi 514962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 5154ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5164ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5174ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5184ed5e82fSMiklos Szeredi smp_mb(); 5194ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5204ed5e82fSMiklos Szeredi err = -EBUSY; 5214ed5e82fSMiklos Szeredi break; 5224ed5e82fSMiklos Szeredi } 5234ed5e82fSMiklos Szeredi } 5244ed5e82fSMiklos Szeredi } 5258e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5268e8b8796SMiklos Szeredi err = -EBUSY; 5278e8b8796SMiklos Szeredi 5284ed5e82fSMiklos Szeredi if (!err) { 5294ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5304ed5e82fSMiklos Szeredi smp_wmb(); 5314ed5e82fSMiklos Szeredi } 5324ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5334ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5344ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5354ed5e82fSMiklos Szeredi } 536962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 5374ed5e82fSMiklos Szeredi 5384ed5e82fSMiklos Szeredi return err; 5394ed5e82fSMiklos Szeredi } 5404ed5e82fSMiklos Szeredi 541b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5421da177e4SLinus Torvalds { 54352ba1621SAl Viro kfree(mnt->mnt_devname); 54473cd49ecSMiklos Szeredi mnt_free_id(mnt); 545d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 54668e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 547d3ef3d73Snpiggin@suse.de #endif 548b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds /* 552a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 553a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 55499b7db7bSNick Piggin * vfsmount_lock must be held for read or write. 5551da177e4SLinus Torvalds */ 556c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 557a05964f3SRam Pai int dir) 5581da177e4SLinus Torvalds { 5591da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 5601da177e4SLinus Torvalds struct list_head *tmp = head; 561c7105365SAl Viro struct mount *p, *found = NULL; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds for (;;) { 564a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 5651da177e4SLinus Torvalds p = NULL; 5661da177e4SLinus Torvalds if (tmp == head) 5671da177e4SLinus Torvalds break; 5681b8e5564SAl Viro p = list_entry(tmp, struct mount, mnt_hash); 569a73324daSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) { 570a05964f3SRam Pai found = p; 5711da177e4SLinus Torvalds break; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds return found; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 577a05964f3SRam Pai /* 578f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 579f015f126SDavid Howells * 580f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 581f015f126SDavid Howells * following mounts: 582f015f126SDavid Howells * 583f015f126SDavid Howells * mount /dev/sda1 /mnt 584f015f126SDavid Howells * mount /dev/sda2 /mnt 585f015f126SDavid Howells * mount /dev/sda3 /mnt 586f015f126SDavid Howells * 587f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 588f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 589f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 590f015f126SDavid Howells * 591f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 592a05964f3SRam Pai */ 5931c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 594a05964f3SRam Pai { 595c7105365SAl Viro struct mount *child_mnt; 59699b7db7bSNick Piggin 597962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 598c7105365SAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry, 1); 599c7105365SAl Viro if (child_mnt) { 600c7105365SAl Viro mnt_add_count(child_mnt, 1); 601962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 602c7105365SAl Viro return &child_mnt->mnt; 603c7105365SAl Viro } else { 604962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 605c7105365SAl Viro return NULL; 606c7105365SAl Viro } 607a05964f3SRam Pai } 608a05964f3SRam Pai 60984d17192SAl Viro static struct mountpoint *new_mountpoint(struct dentry *dentry) 61084d17192SAl Viro { 61184d17192SAl Viro struct list_head *chain = mountpoint_hashtable + hash(NULL, dentry); 61284d17192SAl Viro struct mountpoint *mp; 61384d17192SAl Viro 61484d17192SAl Viro list_for_each_entry(mp, chain, m_hash) { 61584d17192SAl Viro if (mp->m_dentry == dentry) { 61684d17192SAl Viro /* might be worth a WARN_ON() */ 61784d17192SAl Viro if (d_unlinked(dentry)) 61884d17192SAl Viro return ERR_PTR(-ENOENT); 61984d17192SAl Viro mp->m_count++; 62084d17192SAl Viro return mp; 62184d17192SAl Viro } 62284d17192SAl Viro } 62384d17192SAl Viro 62484d17192SAl Viro mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 62584d17192SAl Viro if (!mp) 62684d17192SAl Viro return ERR_PTR(-ENOMEM); 62784d17192SAl Viro 62884d17192SAl Viro spin_lock(&dentry->d_lock); 62984d17192SAl Viro if (d_unlinked(dentry)) { 63084d17192SAl Viro spin_unlock(&dentry->d_lock); 63184d17192SAl Viro kfree(mp); 63284d17192SAl Viro return ERR_PTR(-ENOENT); 63384d17192SAl Viro } 63484d17192SAl Viro dentry->d_flags |= DCACHE_MOUNTED; 63584d17192SAl Viro spin_unlock(&dentry->d_lock); 63684d17192SAl Viro mp->m_dentry = dentry; 63784d17192SAl Viro mp->m_count = 1; 63884d17192SAl Viro list_add(&mp->m_hash, chain); 63984d17192SAl Viro return mp; 64084d17192SAl Viro } 64184d17192SAl Viro 64284d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 64384d17192SAl Viro { 64484d17192SAl Viro if (!--mp->m_count) { 64584d17192SAl Viro struct dentry *dentry = mp->m_dentry; 64684d17192SAl Viro spin_lock(&dentry->d_lock); 64784d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 64884d17192SAl Viro spin_unlock(&dentry->d_lock); 64984d17192SAl Viro list_del(&mp->m_hash); 65084d17192SAl Viro kfree(mp); 65184d17192SAl Viro } 65284d17192SAl Viro } 65384d17192SAl Viro 654143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 6551da177e4SLinus Torvalds { 6566b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds 65999b7db7bSNick Piggin /* 66099b7db7bSNick Piggin * vfsmount lock must be held for write 66199b7db7bSNick Piggin */ 6626b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 6635addc5ddSAl Viro { 6645addc5ddSAl Viro if (ns) { 6655addc5ddSAl Viro ns->event = ++event; 6665addc5ddSAl Viro wake_up_interruptible(&ns->poll); 6675addc5ddSAl Viro } 6685addc5ddSAl Viro } 6695addc5ddSAl Viro 67099b7db7bSNick Piggin /* 67199b7db7bSNick Piggin * vfsmount lock must be held for write 67299b7db7bSNick Piggin */ 6736b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 6745addc5ddSAl Viro { 6755addc5ddSAl Viro if (ns && ns->event != event) { 6765addc5ddSAl Viro ns->event = event; 6775addc5ddSAl Viro wake_up_interruptible(&ns->poll); 6785addc5ddSAl Viro } 6795addc5ddSAl Viro } 6805addc5ddSAl Viro 68199b7db7bSNick Piggin /* 68299b7db7bSNick Piggin * vfsmount lock must be held for write 68399b7db7bSNick Piggin */ 684419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 6851da177e4SLinus Torvalds { 686a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 6870714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 6880714a533SAl Viro mnt->mnt_parent = mnt; 689a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 6906b41d536SAl Viro list_del_init(&mnt->mnt_child); 6911b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 69284d17192SAl Viro put_mountpoint(mnt->mnt_mp); 69384d17192SAl Viro mnt->mnt_mp = NULL; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 69699b7db7bSNick Piggin /* 69799b7db7bSNick Piggin * vfsmount lock must be held for write 69899b7db7bSNick Piggin */ 69984d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 70084d17192SAl Viro struct mountpoint *mp, 70144d964d6SAl Viro struct mount *child_mnt) 702b90fa9aeSRam Pai { 70384d17192SAl Viro mp->m_count++; 7043a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 70584d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 7063a2393d7SAl Viro child_mnt->mnt_parent = mnt; 70784d17192SAl Viro child_mnt->mnt_mp = mp; 708b90fa9aeSRam Pai } 709b90fa9aeSRam Pai 71099b7db7bSNick Piggin /* 71199b7db7bSNick Piggin * vfsmount lock must be held for write 71299b7db7bSNick Piggin */ 71384d17192SAl Viro static void attach_mnt(struct mount *mnt, 71484d17192SAl Viro struct mount *parent, 71584d17192SAl Viro struct mountpoint *mp) 7161da177e4SLinus Torvalds { 71784d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 7181b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 71984d17192SAl Viro hash(&parent->mnt, mp->m_dentry)); 72084d17192SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 721b90fa9aeSRam Pai } 722b90fa9aeSRam Pai 723b90fa9aeSRam Pai /* 72499b7db7bSNick Piggin * vfsmount lock must be held for write 725b90fa9aeSRam Pai */ 7264b2619a5SAl Viro static void commit_tree(struct mount *mnt) 727b90fa9aeSRam Pai { 7280714a533SAl Viro struct mount *parent = mnt->mnt_parent; 72983adc753SAl Viro struct mount *m; 730b90fa9aeSRam Pai LIST_HEAD(head); 731143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 732b90fa9aeSRam Pai 7330714a533SAl Viro BUG_ON(parent == mnt); 734b90fa9aeSRam Pai 7351a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 736f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 737143c8c91SAl Viro m->mnt_ns = n; 738f03c6599SAl Viro 739b90fa9aeSRam Pai list_splice(&head, n->list.prev); 740b90fa9aeSRam Pai 7411b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 742a73324daSAl Viro hash(&parent->mnt, mnt->mnt_mountpoint)); 7436b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 7446b3286edSKirill Korotaev touch_mnt_namespace(n); 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 747909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 7481da177e4SLinus Torvalds { 7496b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 7506b41d536SAl Viro if (next == &p->mnt_mounts) { 7511da177e4SLinus Torvalds while (1) { 752909b0a88SAl Viro if (p == root) 7531da177e4SLinus Torvalds return NULL; 7546b41d536SAl Viro next = p->mnt_child.next; 7556b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 7561da177e4SLinus Torvalds break; 7570714a533SAl Viro p = p->mnt_parent; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds } 7606b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 763315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 7649676f0c6SRam Pai { 7656b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 7666b41d536SAl Viro while (prev != &p->mnt_mounts) { 7676b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 7686b41d536SAl Viro prev = p->mnt_mounts.prev; 7699676f0c6SRam Pai } 7709676f0c6SRam Pai return p; 7719676f0c6SRam Pai } 7729676f0c6SRam Pai 7739d412a43SAl Viro struct vfsmount * 7749d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 7759d412a43SAl Viro { 776b105e270SAl Viro struct mount *mnt; 7779d412a43SAl Viro struct dentry *root; 7789d412a43SAl Viro 7799d412a43SAl Viro if (!type) 7809d412a43SAl Viro return ERR_PTR(-ENODEV); 7819d412a43SAl Viro 7829d412a43SAl Viro mnt = alloc_vfsmnt(name); 7839d412a43SAl Viro if (!mnt) 7849d412a43SAl Viro return ERR_PTR(-ENOMEM); 7859d412a43SAl Viro 7869d412a43SAl Viro if (flags & MS_KERNMOUNT) 787b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 7889d412a43SAl Viro 7899d412a43SAl Viro root = mount_fs(type, flags, name, data); 7909d412a43SAl Viro if (IS_ERR(root)) { 7919d412a43SAl Viro free_vfsmnt(mnt); 7929d412a43SAl Viro return ERR_CAST(root); 7939d412a43SAl Viro } 7949d412a43SAl Viro 795b105e270SAl Viro mnt->mnt.mnt_root = root; 796b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 797a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7980714a533SAl Viro mnt->mnt_parent = mnt; 799962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 80039f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 801962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 802b105e270SAl Viro return &mnt->mnt; 8039d412a43SAl Viro } 8049d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 8059d412a43SAl Viro 80687129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 80736341f64SRam Pai int flag) 8081da177e4SLinus Torvalds { 80987129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 810be34d1a3SDavid Howells struct mount *mnt; 811be34d1a3SDavid Howells int err; 8121da177e4SLinus Torvalds 813be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 814be34d1a3SDavid Howells if (!mnt) 815be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 816be34d1a3SDavid Howells 8177a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 81815169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 819719f5d7fSMiklos Szeredi else 82015169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 821719f5d7fSMiklos Szeredi 82215169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 823be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 824719f5d7fSMiklos Szeredi if (err) 825719f5d7fSMiklos Szeredi goto out_free; 826719f5d7fSMiklos Szeredi } 827719f5d7fSMiklos Szeredi 82887129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 8291da177e4SLinus Torvalds atomic_inc(&sb->s_active); 830b105e270SAl Viro mnt->mnt.mnt_sb = sb; 831b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 832a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8330714a533SAl Viro mnt->mnt_parent = mnt; 834962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 83539f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 836962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 837b90fa9aeSRam Pai 8387a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 8397a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 8406776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 84132301920SAl Viro mnt->mnt_master = old; 842fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 8438aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 844fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 8456776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 846d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 8476776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 848d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 8495afe0022SRam Pai } 850b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 8510f0afb1dSAl Viro set_mnt_shared(mnt); 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 8541da177e4SLinus Torvalds * as the original if that was on one */ 85536341f64SRam Pai if (flag & CL_EXPIRE) { 8566776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 8576776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 8581da177e4SLinus Torvalds } 859be34d1a3SDavid Howells 860cb338d06SAl Viro return mnt; 861719f5d7fSMiklos Szeredi 862719f5d7fSMiklos Szeredi out_free: 863719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 864be34d1a3SDavid Howells return ERR_PTR(err); 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 86783adc753SAl Viro static inline void mntfree(struct mount *mnt) 8681da177e4SLinus Torvalds { 86983adc753SAl Viro struct vfsmount *m = &mnt->mnt; 87083adc753SAl Viro struct super_block *sb = m->mnt_sb; 871b3e19d92SNick Piggin 8723d733633SDave Hansen /* 8733d733633SDave Hansen * This probably indicates that somebody messed 8743d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 8753d733633SDave Hansen * happens, the filesystem was probably unable 8763d733633SDave Hansen * to make r/w->r/o transitions. 8773d733633SDave Hansen */ 878d3ef3d73Snpiggin@suse.de /* 879b3e19d92SNick Piggin * The locking used to deal with mnt_count decrement provides barriers, 880b3e19d92SNick Piggin * so mnt_get_writers() below is safe. 881d3ef3d73Snpiggin@suse.de */ 882c6653a83SNick Piggin WARN_ON(mnt_get_writers(mnt)); 88383adc753SAl Viro fsnotify_vfsmount_delete(m); 88483adc753SAl Viro dput(m->mnt_root); 88583adc753SAl Viro free_vfsmnt(mnt); 8861da177e4SLinus Torvalds deactivate_super(sb); 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 889900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 8907b7b1aceSAl Viro { 891b3e19d92SNick Piggin put_again: 892f03c6599SAl Viro #ifdef CONFIG_SMP 893962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 894f7a99c5bSAl Viro if (likely(mnt->mnt_ns)) { 895f7a99c5bSAl Viro /* shouldn't be the last one */ 896aa9c0e07SAl Viro mnt_add_count(mnt, -1); 897962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 89899b7db7bSNick Piggin return; 899b3e19d92SNick Piggin } 900962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 901b3e19d92SNick Piggin 902962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 903aa9c0e07SAl Viro mnt_add_count(mnt, -1); 904b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 905962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 90699b7db7bSNick Piggin return; 90799b7db7bSNick Piggin } 908b3e19d92SNick Piggin #else 909aa9c0e07SAl Viro mnt_add_count(mnt, -1); 910b3e19d92SNick Piggin if (likely(mnt_get_count(mnt))) 911b3e19d92SNick Piggin return; 912962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 913f03c6599SAl Viro #endif 914863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 915863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 916863d684fSAl Viro mnt->mnt_pinned = 0; 917962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 918900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 919b3e19d92SNick Piggin goto put_again; 920b3e19d92SNick Piggin } 921962830dfSAndi Kleen 92239f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 923962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 924b3e19d92SNick Piggin mntfree(mnt); 925b3e19d92SNick Piggin } 926b3e19d92SNick Piggin 927b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 928b3e19d92SNick Piggin { 929b3e19d92SNick Piggin if (mnt) { 930863d684fSAl Viro struct mount *m = real_mount(mnt); 931b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 932863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 933863d684fSAl Viro m->mnt_expiry_mark = 0; 934863d684fSAl Viro mntput_no_expire(m); 935b3e19d92SNick Piggin } 936b3e19d92SNick Piggin } 937b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 938b3e19d92SNick Piggin 939b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 940b3e19d92SNick Piggin { 941b3e19d92SNick Piggin if (mnt) 94283adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 943b3e19d92SNick Piggin return mnt; 944b3e19d92SNick Piggin } 945b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 946b3e19d92SNick Piggin 9477b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 9487b7b1aceSAl Viro { 949962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 950863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 951962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 9527b7b1aceSAl Viro } 9537b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 9547b7b1aceSAl Viro 955863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 9567b7b1aceSAl Viro { 957863d684fSAl Viro struct mount *mnt = real_mount(m); 958962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 9597b7b1aceSAl Viro if (mnt->mnt_pinned) { 960863d684fSAl Viro mnt_add_count(mnt, 1); 9617b7b1aceSAl Viro mnt->mnt_pinned--; 9627b7b1aceSAl Viro } 963962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 9647b7b1aceSAl Viro } 9657b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 9661da177e4SLinus Torvalds 967b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 968b3b304a2SMiklos Szeredi { 969b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 970b3b304a2SMiklos Szeredi } 971b3b304a2SMiklos Szeredi 972b3b304a2SMiklos Szeredi /* 973b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 974b3b304a2SMiklos Szeredi * implement more complex mount option showing. 975b3b304a2SMiklos Szeredi * 976b3b304a2SMiklos Szeredi * See also save_mount_options(). 977b3b304a2SMiklos Szeredi */ 97834c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 979b3b304a2SMiklos Szeredi { 9802a32cebdSAl Viro const char *options; 9812a32cebdSAl Viro 9822a32cebdSAl Viro rcu_read_lock(); 98334c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 984b3b304a2SMiklos Szeredi 985b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 986b3b304a2SMiklos Szeredi seq_putc(m, ','); 987b3b304a2SMiklos Szeredi mangle(m, options); 988b3b304a2SMiklos Szeredi } 9892a32cebdSAl Viro rcu_read_unlock(); 990b3b304a2SMiklos Szeredi 991b3b304a2SMiklos Szeredi return 0; 992b3b304a2SMiklos Szeredi } 993b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 994b3b304a2SMiklos Szeredi 995b3b304a2SMiklos Szeredi /* 996b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 997b3b304a2SMiklos Szeredi * called from the fill_super() callback. 998b3b304a2SMiklos Szeredi * 999b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 1000b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 1001b3b304a2SMiklos Szeredi * remount fails. 1002b3b304a2SMiklos Szeredi * 1003b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 1004b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 1005b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 1006b3b304a2SMiklos Szeredi * any more. 1007b3b304a2SMiklos Szeredi */ 1008b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 1009b3b304a2SMiklos Szeredi { 10102a32cebdSAl Viro BUG_ON(sb->s_options); 10112a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 1012b3b304a2SMiklos Szeredi } 1013b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 1014b3b304a2SMiklos Szeredi 10152a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 10162a32cebdSAl Viro { 10172a32cebdSAl Viro char *old = sb->s_options; 10182a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 10192a32cebdSAl Viro if (old) { 10202a32cebdSAl Viro synchronize_rcu(); 10212a32cebdSAl Viro kfree(old); 10222a32cebdSAl Viro } 10232a32cebdSAl Viro } 10242a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 10252a32cebdSAl Viro 1026a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 10270226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 10281da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 10291da177e4SLinus Torvalds { 10306ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10311da177e4SLinus Torvalds 1032390c6843SRam Pai down_read(&namespace_sem); 1033a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 10371da177e4SLinus Torvalds { 10386ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 1039b0765fb8SPavel Emelianov 1040a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 10441da177e4SLinus Torvalds { 1045390c6843SRam Pai up_read(&namespace_sem); 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10480226f492SAl Viro static int m_show(struct seq_file *m, void *v) 10499f5596afSAl Viro { 10506ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10511a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 10520226f492SAl Viro return p->show(m, &r->mnt); 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 1055a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 10561da177e4SLinus Torvalds .start = m_start, 10571da177e4SLinus Torvalds .next = m_next, 10581da177e4SLinus Torvalds .stop = m_stop, 10590226f492SAl Viro .show = m_show, 1060b4629fe2SChuck Lever }; 1061a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1062b4629fe2SChuck Lever 10631da177e4SLinus Torvalds /** 10641da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 10651da177e4SLinus Torvalds * @mnt: root of mount tree 10661da177e4SLinus Torvalds * 10671da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 10681da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 10691da177e4SLinus Torvalds * busy. 10701da177e4SLinus Torvalds */ 1071909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 10721da177e4SLinus Torvalds { 1073909b0a88SAl Viro struct mount *mnt = real_mount(m); 107436341f64SRam Pai int actual_refs = 0; 107536341f64SRam Pai int minimum_refs = 0; 1076315fc83eSAl Viro struct mount *p; 1077909b0a88SAl Viro BUG_ON(!m); 10781da177e4SLinus Torvalds 1079b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1080962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1081909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 108283adc753SAl Viro actual_refs += mnt_get_count(p); 10831da177e4SLinus Torvalds minimum_refs += 2; 10841da177e4SLinus Torvalds } 1085962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds if (actual_refs > minimum_refs) 10881da177e4SLinus Torvalds return 0; 1089e3474a8eSIan Kent 1090e3474a8eSIan Kent return 1; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds /** 10961da177e4SLinus Torvalds * may_umount - check if a mount point is busy 10971da177e4SLinus Torvalds * @mnt: root of mount 10981da177e4SLinus Torvalds * 10991da177e4SLinus Torvalds * This is called to check if a mount point has any 11001da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 11011da177e4SLinus Torvalds * mount has sub mounts this will return busy 11021da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 11031da177e4SLinus Torvalds * 11041da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 11051da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 11061da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 11071da177e4SLinus Torvalds */ 11081da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 11091da177e4SLinus Torvalds { 1110e3474a8eSIan Kent int ret = 1; 11118ad08d8aSAl Viro down_read(&namespace_sem); 1112962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 11131ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1114e3474a8eSIan Kent ret = 0; 1115962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11168ad08d8aSAl Viro up_read(&namespace_sem); 1117a05964f3SRam Pai return ret; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 11211da177e4SLinus Torvalds 1122e3197d83SAl Viro static LIST_HEAD(unmounted); /* protected by namespace_sem */ 1123e3197d83SAl Viro 1124b90fa9aeSRam Pai void release_mounts(struct list_head *head) 11251da177e4SLinus Torvalds { 1126d5e50f74SAl Viro struct mount *mnt; 112770fbcdf4SRam Pai while (!list_empty(head)) { 11281b8e5564SAl Viro mnt = list_first_entry(head, struct mount, mnt_hash); 11291b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 1130676da58dSAl Viro if (mnt_has_parent(mnt)) { 113170fbcdf4SRam Pai struct dentry *dentry; 1132863d684fSAl Viro struct mount *m; 113399b7db7bSNick Piggin 1134962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1135a73324daSAl Viro dentry = mnt->mnt_mountpoint; 1136863d684fSAl Viro m = mnt->mnt_parent; 1137a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 11380714a533SAl Viro mnt->mnt_parent = mnt; 11397c4b93d8SAl Viro m->mnt_ghosts--; 1140962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 114170fbcdf4SRam Pai dput(dentry); 1142863d684fSAl Viro mntput(&m->mnt); 11431da177e4SLinus Torvalds } 1144d5e50f74SAl Viro mntput(&mnt->mnt); 114570fbcdf4SRam Pai } 114670fbcdf4SRam Pai } 114770fbcdf4SRam Pai 1148e3197d83SAl Viro static void namespace_unlock(void) 1149e3197d83SAl Viro { 1150e3197d83SAl Viro LIST_HEAD(head); 1151e3197d83SAl Viro list_splice_init(&unmounted, &head); 1152e3197d83SAl Viro up_write(&namespace_sem); 1153e3197d83SAl Viro release_mounts(&head); 1154e3197d83SAl Viro } 1155e3197d83SAl Viro 115699b7db7bSNick Piggin /* 115799b7db7bSNick Piggin * vfsmount lock must be held for write 115899b7db7bSNick Piggin * namespace_sem must be held for write 115999b7db7bSNick Piggin */ 1160761d5c38SAl Viro void umount_tree(struct mount *mnt, int propagate, struct list_head *kill) 116170fbcdf4SRam Pai { 11627b8a53fdSAl Viro LIST_HEAD(tmp_list); 1163315fc83eSAl Viro struct mount *p; 116470fbcdf4SRam Pai 1165909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) 11661b8e5564SAl Viro list_move(&p->mnt_hash, &tmp_list); 116770fbcdf4SRam Pai 1168a05964f3SRam Pai if (propagate) 11697b8a53fdSAl Viro propagate_umount(&tmp_list); 1170a05964f3SRam Pai 11711b8e5564SAl Viro list_for_each_entry(p, &tmp_list, mnt_hash) { 11726776db3dSAl Viro list_del_init(&p->mnt_expire); 11731a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1174143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 117563d37a84SAl Viro p->mnt_ns = NULL; 11766b41d536SAl Viro list_del_init(&p->mnt_child); 1177676da58dSAl Viro if (mnt_has_parent(p)) { 1178863d684fSAl Viro p->mnt_parent->mnt_ghosts++; 117984d17192SAl Viro put_mountpoint(p->mnt_mp); 118084d17192SAl Viro p->mnt_mp = NULL; 11817c4b93d8SAl Viro } 11820f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 11831da177e4SLinus Torvalds } 11847b8a53fdSAl Viro list_splice(&tmp_list, kill); 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 1187692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts); 1188c35038beSAl Viro 11891ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 11901da177e4SLinus Torvalds { 11911ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 11921da177e4SLinus Torvalds int retval; 119370fbcdf4SRam Pai LIST_HEAD(umount_list); 11941da177e4SLinus Torvalds 11951ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 11961da177e4SLinus Torvalds if (retval) 11971da177e4SLinus Torvalds return retval; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds /* 12001da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 12011da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 12021da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 12031da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 12041da177e4SLinus Torvalds */ 12051da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 12061ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 12071da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 12081da177e4SLinus Torvalds return -EINVAL; 12091da177e4SLinus Torvalds 1210b3e19d92SNick Piggin /* 1211b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1212b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1213b3e19d92SNick Piggin */ 1214962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 121583adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1216962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 12171da177e4SLinus Torvalds return -EBUSY; 1218b3e19d92SNick Piggin } 1219962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 12201da177e4SLinus Torvalds 1221863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 12221da177e4SLinus Torvalds return -EAGAIN; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds /* 12261da177e4SLinus Torvalds * If we may have to abort operations to get out of this 12271da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 12281da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 12291da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 12301da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 12311da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 12321da177e4SLinus Torvalds * about for the moment. 12331da177e4SLinus Torvalds */ 12341da177e4SLinus Torvalds 123542faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 123642faad99SAl Viro sb->s_op->umount_begin(sb); 123742faad99SAl Viro } 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds /* 12401da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 12411da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 12421da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 12431da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 12441da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 12451da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 12461da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 12471da177e4SLinus Torvalds */ 12481ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 12491da177e4SLinus Torvalds /* 12501da177e4SLinus Torvalds * Special case for "unmounting" root ... 12511da177e4SLinus Torvalds * we just try to remount it readonly. 12521da177e4SLinus Torvalds */ 12531da177e4SLinus Torvalds down_write(&sb->s_umount); 12544aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 12551da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 12561da177e4SLinus Torvalds up_write(&sb->s_umount); 12571da177e4SLinus Torvalds return retval; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 1260390c6843SRam Pai down_write(&namespace_sem); 1261962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 12625addc5ddSAl Viro event++; 12631da177e4SLinus Torvalds 1264c35038beSAl Viro if (!(flags & MNT_DETACH)) 1265e3197d83SAl Viro shrink_submounts(mnt, &unmounted); 1266c35038beSAl Viro 12671da177e4SLinus Torvalds retval = -EBUSY; 1268a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 12691a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1270e3197d83SAl Viro umount_tree(mnt, 1, &unmounted); 12711da177e4SLinus Torvalds retval = 0; 12721da177e4SLinus Torvalds } 1273962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1274e3197d83SAl Viro namespace_unlock(); 12751da177e4SLinus Torvalds return retval; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds /* 12799b40bc90SAl Viro * Is the caller allowed to modify his namespace? 12809b40bc90SAl Viro */ 12819b40bc90SAl Viro static inline bool may_mount(void) 12829b40bc90SAl Viro { 12839b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 12849b40bc90SAl Viro } 12859b40bc90SAl Viro 12869b40bc90SAl Viro /* 12871da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 12881da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 12891da177e4SLinus Torvalds * 12901da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 12911da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 12921da177e4SLinus Torvalds */ 12931da177e4SLinus Torvalds 1294bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 12951da177e4SLinus Torvalds { 12962d8f3038SAl Viro struct path path; 1297900148dcSAl Viro struct mount *mnt; 12981da177e4SLinus Torvalds int retval; 1299db1f05bbSMiklos Szeredi int lookup_flags = 0; 13001da177e4SLinus Torvalds 1301db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1302db1f05bbSMiklos Szeredi return -EINVAL; 1303db1f05bbSMiklos Szeredi 13049b40bc90SAl Viro if (!may_mount()) 13059b40bc90SAl Viro return -EPERM; 13069b40bc90SAl Viro 1307db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1308db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1309db1f05bbSMiklos Szeredi 1310db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 13111da177e4SLinus Torvalds if (retval) 13121da177e4SLinus Torvalds goto out; 1313900148dcSAl Viro mnt = real_mount(path.mnt); 13141da177e4SLinus Torvalds retval = -EINVAL; 13152d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 13161da177e4SLinus Torvalds goto dput_and_out; 1317143c8c91SAl Viro if (!check_mnt(mnt)) 13181da177e4SLinus Torvalds goto dput_and_out; 13191da177e4SLinus Torvalds 1320900148dcSAl Viro retval = do_umount(mnt, flags); 13211da177e4SLinus Torvalds dput_and_out: 1322429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 13232d8f3038SAl Viro dput(path.dentry); 1324900148dcSAl Viro mntput_no_expire(mnt); 13251da177e4SLinus Torvalds out: 13261da177e4SLinus Torvalds return retval; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds /* 13321da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 13331da177e4SLinus Torvalds */ 1334bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 13351da177e4SLinus Torvalds { 13361da177e4SLinus Torvalds return sys_umount(name, 0); 13371da177e4SLinus Torvalds } 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds #endif 13401da177e4SLinus Torvalds 13418823c079SEric W. Biederman static bool mnt_ns_loop(struct path *path) 13428823c079SEric W. Biederman { 13438823c079SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 13448823c079SEric W. Biederman * mount namespace loop? 13458823c079SEric W. Biederman */ 13468823c079SEric W. Biederman struct inode *inode = path->dentry->d_inode; 13478823c079SEric W. Biederman struct proc_inode *ei; 13488823c079SEric W. Biederman struct mnt_namespace *mnt_ns; 13498823c079SEric W. Biederman 13508823c079SEric W. Biederman if (!proc_ns_inode(inode)) 13518823c079SEric W. Biederman return false; 13528823c079SEric W. Biederman 13538823c079SEric W. Biederman ei = PROC_I(inode); 13548823c079SEric W. Biederman if (ei->ns_ops != &mntns_operations) 13558823c079SEric W. Biederman return false; 13568823c079SEric W. Biederman 13578823c079SEric W. Biederman mnt_ns = ei->ns; 13588823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 13598823c079SEric W. Biederman } 13608823c079SEric W. Biederman 136187129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 136236341f64SRam Pai int flag) 13631da177e4SLinus Torvalds { 136484d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 13651da177e4SLinus Torvalds 1366fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 1367be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 13689676f0c6SRam Pai 136936341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1370be34d1a3SDavid Howells if (IS_ERR(q)) 1371be34d1a3SDavid Howells return q; 1372be34d1a3SDavid Howells 1373a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds p = mnt; 13766b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1377315fc83eSAl Viro struct mount *s; 13787ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 13791da177e4SLinus Torvalds continue; 13801da177e4SLinus Torvalds 1381909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 1382fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 13839676f0c6SRam Pai s = skip_mnt_tree(s); 13849676f0c6SRam Pai continue; 13859676f0c6SRam Pai } 13860714a533SAl Viro while (p != s->mnt_parent) { 13870714a533SAl Viro p = p->mnt_parent; 13880714a533SAl Viro q = q->mnt_parent; 13891da177e4SLinus Torvalds } 139087129cc0SAl Viro p = s; 139184d17192SAl Viro parent = q; 139287129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1393be34d1a3SDavid Howells if (IS_ERR(q)) 1394be34d1a3SDavid Howells goto out; 1395962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 13961a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 139784d17192SAl Viro attach_mnt(q, parent, p->mnt_mp); 1398962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds return res; 1402be34d1a3SDavid Howells out: 14031da177e4SLinus Torvalds if (res) { 140470fbcdf4SRam Pai LIST_HEAD(umount_list); 1405962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1406761d5c38SAl Viro umount_tree(res, 0, &umount_list); 1407962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 140870fbcdf4SRam Pai release_mounts(&umount_list); 14091da177e4SLinus Torvalds } 1410be34d1a3SDavid Howells return q; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 1413be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1414be34d1a3SDavid Howells 1415589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 14168aec0809SAl Viro { 1417cb338d06SAl Viro struct mount *tree; 14181a60a280SAl Viro down_write(&namespace_sem); 141987129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 142087129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 14211a60a280SAl Viro up_write(&namespace_sem); 1422be34d1a3SDavid Howells if (IS_ERR(tree)) 1423be34d1a3SDavid Howells return NULL; 1424be34d1a3SDavid Howells return &tree->mnt; 14258aec0809SAl Viro } 14268aec0809SAl Viro 14278aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 14288aec0809SAl Viro { 14298aec0809SAl Viro LIST_HEAD(umount_list); 14301a60a280SAl Viro down_write(&namespace_sem); 1431962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1432761d5c38SAl Viro umount_tree(real_mount(mnt), 0, &umount_list); 1433962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 14341a60a280SAl Viro up_write(&namespace_sem); 14358aec0809SAl Viro release_mounts(&umount_list); 14368aec0809SAl Viro } 14378aec0809SAl Viro 14381f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 14391f707137SAl Viro struct vfsmount *root) 14401f707137SAl Viro { 14411a4eeaf2SAl Viro struct mount *mnt; 14421f707137SAl Viro int res = f(root, arg); 14431f707137SAl Viro if (res) 14441f707137SAl Viro return res; 14451a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 14461a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 14471f707137SAl Viro if (res) 14481f707137SAl Viro return res; 14491f707137SAl Viro } 14501f707137SAl Viro return 0; 14511f707137SAl Viro } 14521f707137SAl Viro 14534b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1454719f5d7fSMiklos Szeredi { 1455315fc83eSAl Viro struct mount *p; 1456719f5d7fSMiklos Szeredi 1457909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1458fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 14594b8b21f4SAl Viro mnt_release_group_id(p); 1460719f5d7fSMiklos Szeredi } 1461719f5d7fSMiklos Szeredi } 1462719f5d7fSMiklos Szeredi 14634b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1464719f5d7fSMiklos Szeredi { 1465315fc83eSAl Viro struct mount *p; 1466719f5d7fSMiklos Szeredi 1467909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1468fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 14694b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1470719f5d7fSMiklos Szeredi if (err) { 14714b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1472719f5d7fSMiklos Szeredi return err; 1473719f5d7fSMiklos Szeredi } 1474719f5d7fSMiklos Szeredi } 1475719f5d7fSMiklos Szeredi } 1476719f5d7fSMiklos Szeredi 1477719f5d7fSMiklos Szeredi return 0; 1478719f5d7fSMiklos Szeredi } 1479719f5d7fSMiklos Szeredi 1480b90fa9aeSRam Pai /* 1481b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1482b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 148321444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 148421444403SRam Pai * store the parent mount and mountpoint dentry. 148521444403SRam Pai * (done when source_mnt is moved) 1486b90fa9aeSRam Pai * 1487b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1488b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 14899676f0c6SRam Pai * --------------------------------------------------------------------------- 1490b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 14919676f0c6SRam Pai * |************************************************************************** 14929676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 14939676f0c6SRam Pai * | dest | | | | | 14949676f0c6SRam Pai * | | | | | | | 14959676f0c6SRam Pai * | v | | | | | 14969676f0c6SRam Pai * |************************************************************************** 14979676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 14985afe0022SRam Pai * | | | | | | 14999676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 15009676f0c6SRam Pai * *************************************************************************** 1501b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1502b90fa9aeSRam Pai * destination mount. 1503b90fa9aeSRam Pai * 1504b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1505b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1506b90fa9aeSRam Pai * the peer group of the source mount. 1507b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1508b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1509b90fa9aeSRam Pai * mount. 15105afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 15115afe0022SRam Pai * of the destination mount and the cloned mount is made slave 15125afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 15135afe0022SRam Pai * is marked as 'shared and slave'. 15145afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 15155afe0022SRam Pai * source mount. 15165afe0022SRam Pai * 15179676f0c6SRam Pai * --------------------------------------------------------------------------- 151821444403SRam Pai * | MOVE MOUNT OPERATION | 15199676f0c6SRam Pai * |************************************************************************** 15209676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 15219676f0c6SRam Pai * | dest | | | | | 15229676f0c6SRam Pai * | | | | | | | 15239676f0c6SRam Pai * | v | | | | | 15249676f0c6SRam Pai * |************************************************************************** 15259676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 15265afe0022SRam Pai * | | | | | | 15279676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 15289676f0c6SRam Pai * *************************************************************************** 15295afe0022SRam Pai * 15305afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 15315afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 153221444403SRam Pai * (+*) the mount is moved to the destination. 15335afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 15345afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 15355afe0022SRam Pai * the mount is marked as 'shared and slave'. 15365afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1537b90fa9aeSRam Pai * 1538b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1539b90fa9aeSRam Pai * applied to each mount in the tree. 1540b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1541b90fa9aeSRam Pai * in allocations. 1542b90fa9aeSRam Pai */ 15430fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 154484d17192SAl Viro struct mount *dest_mnt, 154584d17192SAl Viro struct mountpoint *dest_mp, 154684d17192SAl Viro struct path *parent_path) 1547b90fa9aeSRam Pai { 1548b90fa9aeSRam Pai LIST_HEAD(tree_list); 1549315fc83eSAl Viro struct mount *child, *p; 1550719f5d7fSMiklos Szeredi int err; 1551b90fa9aeSRam Pai 1552fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 15530fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1554719f5d7fSMiklos Szeredi if (err) 1555719f5d7fSMiklos Szeredi goto out; 1556719f5d7fSMiklos Szeredi } 155784d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 1558719f5d7fSMiklos Szeredi if (err) 1559719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1560b90fa9aeSRam Pai 1561962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1562df1a1ad2SAl Viro 1563fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1564909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 15650f0afb1dSAl Viro set_mnt_shared(p); 1566b90fa9aeSRam Pai } 15671a390689SAl Viro if (parent_path) { 15680fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 156984d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 1570143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 157121444403SRam Pai } else { 157284d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 15730fb54e50SAl Viro commit_tree(source_mnt); 157421444403SRam Pai } 1575b90fa9aeSRam Pai 15761b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 15771b8e5564SAl Viro list_del_init(&child->mnt_hash); 15784b2619a5SAl Viro commit_tree(child); 1579b90fa9aeSRam Pai } 1580962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 158199b7db7bSNick Piggin 1582b90fa9aeSRam Pai return 0; 1583719f5d7fSMiklos Szeredi 1584719f5d7fSMiklos Szeredi out_cleanup_ids: 1585fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 15860fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1587719f5d7fSMiklos Szeredi out: 1588719f5d7fSMiklos Szeredi return err; 1589b90fa9aeSRam Pai } 1590b90fa9aeSRam Pai 159184d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 1592b12cea91SAl Viro { 1593b12cea91SAl Viro struct vfsmount *mnt; 159484d17192SAl Viro struct dentry *dentry = path->dentry; 1595b12cea91SAl Viro retry: 159684d17192SAl Viro mutex_lock(&dentry->d_inode->i_mutex); 159784d17192SAl Viro if (unlikely(cant_mount(dentry))) { 159884d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 159984d17192SAl Viro return ERR_PTR(-ENOENT); 1600b12cea91SAl Viro } 1601b12cea91SAl Viro down_write(&namespace_sem); 1602b12cea91SAl Viro mnt = lookup_mnt(path); 160384d17192SAl Viro if (likely(!mnt)) { 160484d17192SAl Viro struct mountpoint *mp = new_mountpoint(dentry); 160584d17192SAl Viro if (IS_ERR(mp)) { 160684d17192SAl Viro up_write(&namespace_sem); 160784d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 160884d17192SAl Viro return mp; 160984d17192SAl Viro } 161084d17192SAl Viro return mp; 161184d17192SAl Viro } 1612b12cea91SAl Viro up_write(&namespace_sem); 1613b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1614b12cea91SAl Viro path_put(path); 1615b12cea91SAl Viro path->mnt = mnt; 161684d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 1617b12cea91SAl Viro goto retry; 1618b12cea91SAl Viro } 1619b12cea91SAl Viro 162084d17192SAl Viro static void unlock_mount(struct mountpoint *where) 1621b12cea91SAl Viro { 162284d17192SAl Viro struct dentry *dentry = where->m_dentry; 162384d17192SAl Viro put_mountpoint(where); 1624b12cea91SAl Viro up_write(&namespace_sem); 162584d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 1626b12cea91SAl Viro } 1627b12cea91SAl Viro 162884d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 16291da177e4SLinus Torvalds { 163095bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 16311da177e4SLinus Torvalds return -EINVAL; 16321da177e4SLinus Torvalds 163384d17192SAl Viro if (S_ISDIR(mp->m_dentry->d_inode->i_mode) != 163495bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 16351da177e4SLinus Torvalds return -ENOTDIR; 16361da177e4SLinus Torvalds 163784d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds /* 16417a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 16427a2e8a8fSValerie Aurora */ 16437a2e8a8fSValerie Aurora 16447a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 16457a2e8a8fSValerie Aurora { 16467c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 16477a2e8a8fSValerie Aurora 16487a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 16497a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 16507a2e8a8fSValerie Aurora return 0; 16517a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 16527a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 16537a2e8a8fSValerie Aurora return 0; 16547a2e8a8fSValerie Aurora return type; 16557a2e8a8fSValerie Aurora } 16567a2e8a8fSValerie Aurora 16577a2e8a8fSValerie Aurora /* 165807b20889SRam Pai * recursively change the type of the mountpoint. 165907b20889SRam Pai */ 16600a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 166107b20889SRam Pai { 1662315fc83eSAl Viro struct mount *m; 16634b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 166407b20889SRam Pai int recurse = flag & MS_REC; 16657a2e8a8fSValerie Aurora int type; 1666719f5d7fSMiklos Szeredi int err = 0; 166707b20889SRam Pai 16682d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 166907b20889SRam Pai return -EINVAL; 167007b20889SRam Pai 16717a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 16727a2e8a8fSValerie Aurora if (!type) 16737a2e8a8fSValerie Aurora return -EINVAL; 16747a2e8a8fSValerie Aurora 167507b20889SRam Pai down_write(&namespace_sem); 1676719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1677719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1678719f5d7fSMiklos Szeredi if (err) 1679719f5d7fSMiklos Szeredi goto out_unlock; 1680719f5d7fSMiklos Szeredi } 1681719f5d7fSMiklos Szeredi 1682962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1683909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 16840f0afb1dSAl Viro change_mnt_propagation(m, type); 1685962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1686719f5d7fSMiklos Szeredi 1687719f5d7fSMiklos Szeredi out_unlock: 168807b20889SRam Pai up_write(&namespace_sem); 1689719f5d7fSMiklos Szeredi return err; 169007b20889SRam Pai } 169107b20889SRam Pai 169207b20889SRam Pai /* 16931da177e4SLinus Torvalds * do loopback mount. 16941da177e4SLinus Torvalds */ 1695808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 16962dafe1c4SEric Sandeen int recurse) 16971da177e4SLinus Torvalds { 1698b12cea91SAl Viro LIST_HEAD(umount_list); 16992d92ab3cSAl Viro struct path old_path; 170084d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 170184d17192SAl Viro struct mountpoint *mp; 170257eccb83SAl Viro int err; 17031da177e4SLinus Torvalds if (!old_name || !*old_name) 17041da177e4SLinus Torvalds return -EINVAL; 1705815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 17061da177e4SLinus Torvalds if (err) 17071da177e4SLinus Torvalds return err; 17081da177e4SLinus Torvalds 17098823c079SEric W. Biederman err = -EINVAL; 17108823c079SEric W. Biederman if (mnt_ns_loop(&old_path)) 17118823c079SEric W. Biederman goto out; 17128823c079SEric W. Biederman 171384d17192SAl Viro mp = lock_mount(path); 171484d17192SAl Viro err = PTR_ERR(mp); 171584d17192SAl Viro if (IS_ERR(mp)) 17169676f0c6SRam Pai goto out; 17179676f0c6SRam Pai 171887129cc0SAl Viro old = real_mount(old_path.mnt); 171984d17192SAl Viro parent = real_mount(path->mnt); 172087129cc0SAl Viro 1721b12cea91SAl Viro err = -EINVAL; 1722fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1723b12cea91SAl Viro goto out2; 1724b12cea91SAl Viro 172584d17192SAl Viro if (!check_mnt(parent) || !check_mnt(old)) 1726b12cea91SAl Viro goto out2; 1727ccd48bc7SAl Viro 17281da177e4SLinus Torvalds if (recurse) 172987129cc0SAl Viro mnt = copy_tree(old, old_path.dentry, 0); 17301da177e4SLinus Torvalds else 173187129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 17321da177e4SLinus Torvalds 1733be34d1a3SDavid Howells if (IS_ERR(mnt)) { 1734be34d1a3SDavid Howells err = PTR_ERR(mnt); 1735e9c5d8a5SAndrey Vagin goto out2; 1736be34d1a3SDavid Howells } 1737ccd48bc7SAl Viro 173884d17192SAl Viro err = graft_tree(mnt, parent, mp); 17391da177e4SLinus Torvalds if (err) { 1740962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1741761d5c38SAl Viro umount_tree(mnt, 0, &umount_list); 1742962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 17435b83d2c5SRam Pai } 1744b12cea91SAl Viro out2: 174584d17192SAl Viro unlock_mount(mp); 1746b12cea91SAl Viro release_mounts(&umount_list); 1747ccd48bc7SAl Viro out: 17482d92ab3cSAl Viro path_put(&old_path); 17491da177e4SLinus Torvalds return err; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 17522e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 17532e4b7fcdSDave Hansen { 17542e4b7fcdSDave Hansen int error = 0; 17552e4b7fcdSDave Hansen int readonly_request = 0; 17562e4b7fcdSDave Hansen 17572e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 17582e4b7fcdSDave Hansen readonly_request = 1; 17592e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 17602e4b7fcdSDave Hansen return 0; 17612e4b7fcdSDave Hansen 17622e4b7fcdSDave Hansen if (readonly_request) 176383adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 17642e4b7fcdSDave Hansen else 176583adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 17662e4b7fcdSDave Hansen return error; 17672e4b7fcdSDave Hansen } 17682e4b7fcdSDave Hansen 17691da177e4SLinus Torvalds /* 17701da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 17711da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 17721da177e4SLinus Torvalds * on it - tough luck. 17731da177e4SLinus Torvalds */ 17740a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 17751da177e4SLinus Torvalds void *data) 17761da177e4SLinus Torvalds { 17771da177e4SLinus Torvalds int err; 17782d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1779143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 17801da177e4SLinus Torvalds 1781143c8c91SAl Viro if (!check_mnt(mnt)) 17821da177e4SLinus Torvalds return -EINVAL; 17831da177e4SLinus Torvalds 17842d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 17851da177e4SLinus Torvalds return -EINVAL; 17861da177e4SLinus Torvalds 1787ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1788ff36fe2cSEric Paris if (err) 1789ff36fe2cSEric Paris return err; 1790ff36fe2cSEric Paris 17911da177e4SLinus Torvalds down_write(&sb->s_umount); 17922e4b7fcdSDave Hansen if (flags & MS_BIND) 17932d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 179457eccb83SAl Viro else if (!capable(CAP_SYS_ADMIN)) 179557eccb83SAl Viro err = -EPERM; 17964aa98cf7SAl Viro else 17971da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 17987b43a79fSAl Viro if (!err) { 1799962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1800143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1801143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1802962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 18037b43a79fSAl Viro } 18041da177e4SLinus Torvalds up_write(&sb->s_umount); 18050e55a7ccSDan Williams if (!err) { 1806962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1807143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1808962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 18090e55a7ccSDan Williams } 18101da177e4SLinus Torvalds return err; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 1813cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 18149676f0c6SRam Pai { 1815315fc83eSAl Viro struct mount *p; 1816909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1817fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 18189676f0c6SRam Pai return 1; 18199676f0c6SRam Pai } 18209676f0c6SRam Pai return 0; 18219676f0c6SRam Pai } 18229676f0c6SRam Pai 1823808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 18241da177e4SLinus Torvalds { 18252d92ab3cSAl Viro struct path old_path, parent_path; 1826676da58dSAl Viro struct mount *p; 18270fb54e50SAl Viro struct mount *old; 182884d17192SAl Viro struct mountpoint *mp; 182957eccb83SAl Viro int err; 18301da177e4SLinus Torvalds if (!old_name || !*old_name) 18311da177e4SLinus Torvalds return -EINVAL; 18322d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 18331da177e4SLinus Torvalds if (err) 18341da177e4SLinus Torvalds return err; 18351da177e4SLinus Torvalds 183684d17192SAl Viro mp = lock_mount(path); 183784d17192SAl Viro err = PTR_ERR(mp); 183884d17192SAl Viro if (IS_ERR(mp)) 1839cc53ce53SDavid Howells goto out; 1840cc53ce53SDavid Howells 1841143c8c91SAl Viro old = real_mount(old_path.mnt); 1842fc7be130SAl Viro p = real_mount(path->mnt); 1843143c8c91SAl Viro 18441da177e4SLinus Torvalds err = -EINVAL; 1845fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 18461da177e4SLinus Torvalds goto out1; 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds err = -EINVAL; 18492d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 185021444403SRam Pai goto out1; 18511da177e4SLinus Torvalds 1852676da58dSAl Viro if (!mnt_has_parent(old)) 185321444403SRam Pai goto out1; 18541da177e4SLinus Torvalds 18552d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 18562d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 185721444403SRam Pai goto out1; 185821444403SRam Pai /* 185921444403SRam Pai * Don't move a mount residing in a shared parent. 186021444403SRam Pai */ 1861fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 186221444403SRam Pai goto out1; 18639676f0c6SRam Pai /* 18649676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 18659676f0c6SRam Pai * mount which is shared. 18669676f0c6SRam Pai */ 1867fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 18689676f0c6SRam Pai goto out1; 18691da177e4SLinus Torvalds err = -ELOOP; 1870fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1871676da58dSAl Viro if (p == old) 187221444403SRam Pai goto out1; 18731da177e4SLinus Torvalds 187484d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 18754ac91378SJan Blunck if (err) 187621444403SRam Pai goto out1; 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 18791da177e4SLinus Torvalds * automatically */ 18806776db3dSAl Viro list_del_init(&old->mnt_expire); 18811da177e4SLinus Torvalds out1: 188284d17192SAl Viro unlock_mount(mp); 18831da177e4SLinus Torvalds out: 18841da177e4SLinus Torvalds if (!err) 18851a390689SAl Viro path_put(&parent_path); 18862d92ab3cSAl Viro path_put(&old_path); 18871da177e4SLinus Torvalds return err; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18909d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 18919d412a43SAl Viro { 18929d412a43SAl Viro int err; 18939d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 18949d412a43SAl Viro if (subtype) { 18959d412a43SAl Viro subtype++; 18969d412a43SAl Viro err = -EINVAL; 18979d412a43SAl Viro if (!subtype[0]) 18989d412a43SAl Viro goto err; 18999d412a43SAl Viro } else 19009d412a43SAl Viro subtype = ""; 19019d412a43SAl Viro 19029d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 19039d412a43SAl Viro err = -ENOMEM; 19049d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 19059d412a43SAl Viro goto err; 19069d412a43SAl Viro return mnt; 19079d412a43SAl Viro 19089d412a43SAl Viro err: 19099d412a43SAl Viro mntput(mnt); 19109d412a43SAl Viro return ERR_PTR(err); 19119d412a43SAl Viro } 19129d412a43SAl Viro 19139d412a43SAl Viro /* 19149d412a43SAl Viro * add a mount into a namespace's mount tree 19159d412a43SAl Viro */ 191695bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 19179d412a43SAl Viro { 191884d17192SAl Viro struct mountpoint *mp; 191984d17192SAl Viro struct mount *parent; 19209d412a43SAl Viro int err; 19219d412a43SAl Viro 19229d412a43SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 19239d412a43SAl Viro 192484d17192SAl Viro mp = lock_mount(path); 192584d17192SAl Viro if (IS_ERR(mp)) 192684d17192SAl Viro return PTR_ERR(mp); 19279d412a43SAl Viro 192884d17192SAl Viro parent = real_mount(path->mnt); 19299d412a43SAl Viro err = -EINVAL; 193084d17192SAl Viro if (unlikely(!check_mnt(parent))) { 1931156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 1932156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 19339d412a43SAl Viro goto unlock; 1934156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 193584d17192SAl Viro if (!parent->mnt_ns) 1936156cacb1SAl Viro goto unlock; 1937156cacb1SAl Viro } 19389d412a43SAl Viro 19399d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 19409d412a43SAl Viro err = -EBUSY; 194195bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 19429d412a43SAl Viro path->mnt->mnt_root == path->dentry) 19439d412a43SAl Viro goto unlock; 19449d412a43SAl Viro 19459d412a43SAl Viro err = -EINVAL; 194695bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 19479d412a43SAl Viro goto unlock; 19489d412a43SAl Viro 194995bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 195084d17192SAl Viro err = graft_tree(newmnt, parent, mp); 19519d412a43SAl Viro 19529d412a43SAl Viro unlock: 195384d17192SAl Viro unlock_mount(mp); 19549d412a43SAl Viro return err; 19559d412a43SAl Viro } 1956b1e75df4SAl Viro 19571da177e4SLinus Torvalds /* 19581da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 19591da177e4SLinus Torvalds * namespace's tree 19601da177e4SLinus Torvalds */ 19610c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags, 1962808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 19631da177e4SLinus Torvalds { 19640c55cfc4SEric W. Biederman struct file_system_type *type; 19659b40bc90SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 19661da177e4SLinus Torvalds struct vfsmount *mnt; 196715f9a3f3SAl Viro int err; 19681da177e4SLinus Torvalds 19690c55cfc4SEric W. Biederman if (!fstype) 19701da177e4SLinus Torvalds return -EINVAL; 19711da177e4SLinus Torvalds 19720c55cfc4SEric W. Biederman type = get_fs_type(fstype); 19730c55cfc4SEric W. Biederman if (!type) 19740c55cfc4SEric W. Biederman return -ENODEV; 19750c55cfc4SEric W. Biederman 19760c55cfc4SEric W. Biederman if (user_ns != &init_user_ns) { 19770c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_MOUNT)) { 19780c55cfc4SEric W. Biederman put_filesystem(type); 19790c55cfc4SEric W. Biederman return -EPERM; 19800c55cfc4SEric W. Biederman } 19810c55cfc4SEric W. Biederman /* Only in special cases allow devices from mounts 19820c55cfc4SEric W. Biederman * created outside the initial user namespace. 19830c55cfc4SEric W. Biederman */ 19840c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) { 19850c55cfc4SEric W. Biederman flags |= MS_NODEV; 19860c55cfc4SEric W. Biederman mnt_flags |= MNT_NODEV; 19870c55cfc4SEric W. Biederman } 19880c55cfc4SEric W. Biederman } 19890c55cfc4SEric W. Biederman 19900c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, flags, name, data); 19910c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 19920c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 19930c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 19940c55cfc4SEric W. Biederman 19950c55cfc4SEric W. Biederman put_filesystem(type); 19961da177e4SLinus Torvalds if (IS_ERR(mnt)) 19971da177e4SLinus Torvalds return PTR_ERR(mnt); 19981da177e4SLinus Torvalds 199995bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 200015f9a3f3SAl Viro if (err) 200115f9a3f3SAl Viro mntput(mnt); 200215f9a3f3SAl Viro return err; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 200519a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 200619a167afSAl Viro { 20076776db3dSAl Viro struct mount *mnt = real_mount(m); 200819a167afSAl Viro int err; 200919a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 201019a167afSAl Viro * expired before we get a chance to add it 201119a167afSAl Viro */ 20126776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 201319a167afSAl Viro 201419a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 201519a167afSAl Viro m->mnt_root == path->dentry) { 2016b1e75df4SAl Viro err = -ELOOP; 2017b1e75df4SAl Viro goto fail; 201819a167afSAl Viro } 201919a167afSAl Viro 202095bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2021b1e75df4SAl Viro if (!err) 2022b1e75df4SAl Viro return 0; 2023b1e75df4SAl Viro fail: 2024b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 20256776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 2026b1e75df4SAl Viro down_write(&namespace_sem); 2027962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 20286776db3dSAl Viro list_del_init(&mnt->mnt_expire); 2029962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2030b1e75df4SAl Viro up_write(&namespace_sem); 203119a167afSAl Viro } 2032b1e75df4SAl Viro mntput(m); 2033b1e75df4SAl Viro mntput(m); 203419a167afSAl Viro return err; 203519a167afSAl Viro } 203619a167afSAl Viro 2037ea5b778aSDavid Howells /** 2038ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2039ea5b778aSDavid Howells * @mnt: The mount to list. 2040ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2041ea5b778aSDavid Howells */ 2042ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2043ea5b778aSDavid Howells { 2044ea5b778aSDavid Howells down_write(&namespace_sem); 2045962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2046ea5b778aSDavid Howells 20476776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2048ea5b778aSDavid Howells 2049962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2050ea5b778aSDavid Howells up_write(&namespace_sem); 2051ea5b778aSDavid Howells } 2052ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2053ea5b778aSDavid Howells 2054ea5b778aSDavid Howells /* 20551da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 20561da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 20571da177e4SLinus Torvalds * here 20581da177e4SLinus Torvalds */ 20591da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 20601da177e4SLinus Torvalds { 2061761d5c38SAl Viro struct mount *mnt, *next; 20621da177e4SLinus Torvalds LIST_HEAD(graveyard); 2063bcc5c7d2SAl Viro LIST_HEAD(umounts); 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds if (list_empty(mounts)) 20661da177e4SLinus Torvalds return; 20671da177e4SLinus Torvalds 2068bcc5c7d2SAl Viro down_write(&namespace_sem); 2069962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 20721da177e4SLinus Torvalds * following criteria: 20731da177e4SLinus Torvalds * - only referenced by its parent vfsmount 20741da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 20751da177e4SLinus Torvalds * cleared by mntput()) 20761da177e4SLinus Torvalds */ 20776776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2078863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 20791ab59738SAl Viro propagate_mount_busy(mnt, 1)) 20801da177e4SLinus Torvalds continue; 20816776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 20821da177e4SLinus Torvalds } 2083bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 20846776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2085143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2086bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 2087bcc5c7d2SAl Viro } 2088962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2089bcc5c7d2SAl Viro up_write(&namespace_sem); 2090bcc5c7d2SAl Viro 2091bcc5c7d2SAl Viro release_mounts(&umounts); 20921da177e4SLinus Torvalds } 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds /* 20975528f911STrond Myklebust * Ripoff of 'select_parent()' 20985528f911STrond Myklebust * 20995528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 21005528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 21015528f911STrond Myklebust */ 2102692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 21035528f911STrond Myklebust { 2104692afc31SAl Viro struct mount *this_parent = parent; 21055528f911STrond Myklebust struct list_head *next; 21065528f911STrond Myklebust int found = 0; 21075528f911STrond Myklebust 21085528f911STrond Myklebust repeat: 21096b41d536SAl Viro next = this_parent->mnt_mounts.next; 21105528f911STrond Myklebust resume: 21116b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 21125528f911STrond Myklebust struct list_head *tmp = next; 21136b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 21145528f911STrond Myklebust 21155528f911STrond Myklebust next = tmp->next; 2116692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 21175528f911STrond Myklebust continue; 21185528f911STrond Myklebust /* 21195528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 21205528f911STrond Myklebust */ 21216b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 21225528f911STrond Myklebust this_parent = mnt; 21235528f911STrond Myklebust goto repeat; 21245528f911STrond Myklebust } 21255528f911STrond Myklebust 21261ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 21276776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 21285528f911STrond Myklebust found++; 21295528f911STrond Myklebust } 21305528f911STrond Myklebust } 21315528f911STrond Myklebust /* 21325528f911STrond Myklebust * All done at this level ... ascend and resume the search 21335528f911STrond Myklebust */ 21345528f911STrond Myklebust if (this_parent != parent) { 21356b41d536SAl Viro next = this_parent->mnt_child.next; 21360714a533SAl Viro this_parent = this_parent->mnt_parent; 21375528f911STrond Myklebust goto resume; 21385528f911STrond Myklebust } 21395528f911STrond Myklebust return found; 21405528f911STrond Myklebust } 21415528f911STrond Myklebust 21425528f911STrond Myklebust /* 21435528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 21445528f911STrond Myklebust * submounts of a specific parent mountpoint 214599b7db7bSNick Piggin * 214699b7db7bSNick Piggin * vfsmount_lock must be held for write 21475528f911STrond Myklebust */ 2148692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts) 21495528f911STrond Myklebust { 21505528f911STrond Myklebust LIST_HEAD(graveyard); 2151761d5c38SAl Viro struct mount *m; 21525528f911STrond Myklebust 21535528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2154c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2155bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2156761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 21576776db3dSAl Viro mnt_expire); 2158143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2159afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 2160bcc5c7d2SAl Viro } 2161bcc5c7d2SAl Viro } 21625528f911STrond Myklebust } 21635528f911STrond Myklebust 21645528f911STrond Myklebust /* 21651da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 21661da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 21671da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 21681da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 21691da177e4SLinus Torvalds */ 2170b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2171b58fed8bSRam Pai unsigned long n) 21721da177e4SLinus Torvalds { 21731da177e4SLinus Torvalds char *t = to; 21741da177e4SLinus Torvalds const char __user *f = from; 21751da177e4SLinus Torvalds char c; 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 21781da177e4SLinus Torvalds return n; 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds while (n) { 21811da177e4SLinus Torvalds if (__get_user(c, f)) { 21821da177e4SLinus Torvalds memset(t, 0, n); 21831da177e4SLinus Torvalds break; 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds *t++ = c; 21861da177e4SLinus Torvalds f++; 21871da177e4SLinus Torvalds n--; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds return n; 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 21931da177e4SLinus Torvalds { 21941da177e4SLinus Torvalds int i; 21951da177e4SLinus Torvalds unsigned long page; 21961da177e4SLinus Torvalds unsigned long size; 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds *where = 0; 21991da177e4SLinus Torvalds if (!data) 22001da177e4SLinus Torvalds return 0; 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 22031da177e4SLinus Torvalds return -ENOMEM; 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds /* We only care that *some* data at the address the user 22061da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 22071da177e4SLinus Torvalds * the remainder of the page. 22081da177e4SLinus Torvalds */ 22091da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 22101da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 22111da177e4SLinus Torvalds if (size > PAGE_SIZE) 22121da177e4SLinus Torvalds size = PAGE_SIZE; 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 22151da177e4SLinus Torvalds if (!i) { 22161da177e4SLinus Torvalds free_page(page); 22171da177e4SLinus Torvalds return -EFAULT; 22181da177e4SLinus Torvalds } 22191da177e4SLinus Torvalds if (i != PAGE_SIZE) 22201da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 22211da177e4SLinus Torvalds *where = page; 22221da177e4SLinus Torvalds return 0; 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 2225eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2226eca6f534SVegard Nossum { 2227eca6f534SVegard Nossum char *tmp; 2228eca6f534SVegard Nossum 2229eca6f534SVegard Nossum if (!data) { 2230eca6f534SVegard Nossum *where = NULL; 2231eca6f534SVegard Nossum return 0; 2232eca6f534SVegard Nossum } 2233eca6f534SVegard Nossum 2234eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2235eca6f534SVegard Nossum if (IS_ERR(tmp)) 2236eca6f534SVegard Nossum return PTR_ERR(tmp); 2237eca6f534SVegard Nossum 2238eca6f534SVegard Nossum *where = tmp; 2239eca6f534SVegard Nossum return 0; 2240eca6f534SVegard Nossum } 2241eca6f534SVegard Nossum 22421da177e4SLinus Torvalds /* 22431da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 22441da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 22451da177e4SLinus Torvalds * 22461da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 22471da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 22481da177e4SLinus Torvalds * information (or be NULL). 22491da177e4SLinus Torvalds * 22501da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 22511da177e4SLinus Torvalds * When the flags word was introduced its top half was required 22521da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 22531da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 22541da177e4SLinus Torvalds * and must be discarded. 22551da177e4SLinus Torvalds */ 2256808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name, 2257808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 22581da177e4SLinus Torvalds { 22592d92ab3cSAl Viro struct path path; 22601da177e4SLinus Torvalds int retval = 0; 22611da177e4SLinus Torvalds int mnt_flags = 0; 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds /* Discard magic */ 22641da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 22651da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds /* Basic sanity checks */ 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 22701da177e4SLinus Torvalds return -EINVAL; 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds if (data_page) 22731da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 22741da177e4SLinus Torvalds 2275a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2276a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2277a27ab9f2STetsuo Handa if (retval) 2278a27ab9f2STetsuo Handa return retval; 2279a27ab9f2STetsuo Handa 2280a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2281a27ab9f2STetsuo Handa type_page, flags, data_page); 2282a27ab9f2STetsuo Handa if (retval) 2283a27ab9f2STetsuo Handa goto dput_out; 2284a27ab9f2STetsuo Handa 228557eccb83SAl Viro if (!may_mount()) 228657eccb83SAl Viro return -EPERM; 228757eccb83SAl Viro 2288613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2289613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 22900a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 22910a1c01c9SMatthew Garrett 22921da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 22931da177e4SLinus Torvalds if (flags & MS_NOSUID) 22941da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 22951da177e4SLinus Torvalds if (flags & MS_NODEV) 22961da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 22971da177e4SLinus Torvalds if (flags & MS_NOEXEC) 22981da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2299fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2300fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2301fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2302fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2303d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2304d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 23052e4b7fcdSDave Hansen if (flags & MS_RDONLY) 23062e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2307fc33a7bbSChristoph Hellwig 23087a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2309d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2310d0adde57SMatthew Garrett MS_STRICTATIME); 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds if (flags & MS_REMOUNT) 23132d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 23141da177e4SLinus Torvalds data_page); 23151da177e4SLinus Torvalds else if (flags & MS_BIND) 23162d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 23179676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 23182d92ab3cSAl Viro retval = do_change_type(&path, flags); 23191da177e4SLinus Torvalds else if (flags & MS_MOVE) 23202d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 23211da177e4SLinus Torvalds else 23222d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 23231da177e4SLinus Torvalds dev_name, data_page); 23241da177e4SLinus Torvalds dput_out: 23252d92ab3cSAl Viro path_put(&path); 23261da177e4SLinus Torvalds return retval; 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds 2329771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2330771b1371SEric W. Biederman { 233198f842e6SEric W. Biederman proc_free_inum(ns->proc_inum); 2332771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2333771b1371SEric W. Biederman kfree(ns); 2334771b1371SEric W. Biederman } 2335771b1371SEric W. Biederman 23368823c079SEric W. Biederman /* 23378823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 23388823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 23398823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 23408823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 23418823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 23428823c079SEric W. Biederman */ 23438823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 23448823c079SEric W. Biederman 2345771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2346cf8d2c11STrond Myklebust { 2347cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 234898f842e6SEric W. Biederman int ret; 2349cf8d2c11STrond Myklebust 2350cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2351cf8d2c11STrond Myklebust if (!new_ns) 2352cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 235398f842e6SEric W. Biederman ret = proc_alloc_inum(&new_ns->proc_inum); 235498f842e6SEric W. Biederman if (ret) { 235598f842e6SEric W. Biederman kfree(new_ns); 235698f842e6SEric W. Biederman return ERR_PTR(ret); 235798f842e6SEric W. Biederman } 23588823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2359cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2360cf8d2c11STrond Myklebust new_ns->root = NULL; 2361cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2362cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2363cf8d2c11STrond Myklebust new_ns->event = 0; 2364771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2365cf8d2c11STrond Myklebust return new_ns; 2366cf8d2c11STrond Myklebust } 2367cf8d2c11STrond Myklebust 2368741a2951SJANAK DESAI /* 2369741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2370741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2371741a2951SJANAK DESAI */ 2372e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 2373771b1371SEric W. Biederman struct user_namespace *user_ns, struct fs_struct *fs) 23741da177e4SLinus Torvalds { 23756b3286edSKirill Korotaev struct mnt_namespace *new_ns; 23767f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2377315fc83eSAl Viro struct mount *p, *q; 2378be08d6d2SAl Viro struct mount *old = mnt_ns->root; 2379cb338d06SAl Viro struct mount *new; 23807a472ef4SEric W. Biederman int copy_flags; 23811da177e4SLinus Torvalds 2382771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2383cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2384cf8d2c11STrond Myklebust return new_ns; 23851da177e4SLinus Torvalds 2386390c6843SRam Pai down_write(&namespace_sem); 23871da177e4SLinus Torvalds /* First pass: copy the tree topology */ 23887a472ef4SEric W. Biederman copy_flags = CL_COPY_ALL | CL_EXPIRE; 23897a472ef4SEric W. Biederman if (user_ns != mnt_ns->user_ns) 23907a472ef4SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE; 23917a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2392be34d1a3SDavid Howells if (IS_ERR(new)) { 2393390c6843SRam Pai up_write(&namespace_sem); 2394771b1371SEric W. Biederman free_mnt_ns(new_ns); 2395be34d1a3SDavid Howells return ERR_CAST(new); 23961da177e4SLinus Torvalds } 2397be08d6d2SAl Viro new_ns->root = new; 2398962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 23991a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 2400962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 24011da177e4SLinus Torvalds 24021da177e4SLinus Torvalds /* 24031da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 24041da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 24051da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 24061da177e4SLinus Torvalds */ 2407909b0a88SAl Viro p = old; 2408cb338d06SAl Viro q = new; 24091da177e4SLinus Torvalds while (p) { 2410143c8c91SAl Viro q->mnt_ns = new_ns; 24111da177e4SLinus Torvalds if (fs) { 2412315fc83eSAl Viro if (&p->mnt == fs->root.mnt) { 2413315fc83eSAl Viro fs->root.mnt = mntget(&q->mnt); 2414315fc83eSAl Viro rootmnt = &p->mnt; 24151da177e4SLinus Torvalds } 2416315fc83eSAl Viro if (&p->mnt == fs->pwd.mnt) { 2417315fc83eSAl Viro fs->pwd.mnt = mntget(&q->mnt); 2418315fc83eSAl Viro pwdmnt = &p->mnt; 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds } 2421909b0a88SAl Viro p = next_mnt(p, old); 2422909b0a88SAl Viro q = next_mnt(q, new); 24231da177e4SLinus Torvalds } 2424390c6843SRam Pai up_write(&namespace_sem); 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds if (rootmnt) 2427f03c6599SAl Viro mntput(rootmnt); 24281da177e4SLinus Torvalds if (pwdmnt) 2429f03c6599SAl Viro mntput(pwdmnt); 24301da177e4SLinus Torvalds 2431741a2951SJANAK DESAI return new_ns; 2432741a2951SJANAK DESAI } 2433741a2951SJANAK DESAI 2434213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2435771b1371SEric W. Biederman struct user_namespace *user_ns, struct fs_struct *new_fs) 2436741a2951SJANAK DESAI { 24376b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2438741a2951SJANAK DESAI 2439e3222c4eSBadari Pulavarty BUG_ON(!ns); 24406b3286edSKirill Korotaev get_mnt_ns(ns); 2441741a2951SJANAK DESAI 2442741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2443e3222c4eSBadari Pulavarty return ns; 2444741a2951SJANAK DESAI 2445771b1371SEric W. Biederman new_ns = dup_mnt_ns(ns, user_ns, new_fs); 2446741a2951SJANAK DESAI 24476b3286edSKirill Korotaev put_mnt_ns(ns); 2448e3222c4eSBadari Pulavarty return new_ns; 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 2451cf8d2c11STrond Myklebust /** 2452cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2453cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2454cf8d2c11STrond Myklebust */ 24551a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2456cf8d2c11STrond Myklebust { 2457771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2458cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 24591a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 24601a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2461be08d6d2SAl Viro new_ns->root = mnt; 24621a4eeaf2SAl Viro list_add(&new_ns->list, &mnt->mnt_list); 2463c1334495SAl Viro } else { 24641a4eeaf2SAl Viro mntput(m); 2465cf8d2c11STrond Myklebust } 2466cf8d2c11STrond Myklebust return new_ns; 2467cf8d2c11STrond Myklebust } 2468cf8d2c11STrond Myklebust 2469ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2470ea441d11SAl Viro { 2471ea441d11SAl Viro struct mnt_namespace *ns; 2472d31da0f0SAl Viro struct super_block *s; 2473ea441d11SAl Viro struct path path; 2474ea441d11SAl Viro int err; 2475ea441d11SAl Viro 2476ea441d11SAl Viro ns = create_mnt_ns(mnt); 2477ea441d11SAl Viro if (IS_ERR(ns)) 2478ea441d11SAl Viro return ERR_CAST(ns); 2479ea441d11SAl Viro 2480ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2481ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2482ea441d11SAl Viro 2483ea441d11SAl Viro put_mnt_ns(ns); 2484ea441d11SAl Viro 2485ea441d11SAl Viro if (err) 2486ea441d11SAl Viro return ERR_PTR(err); 2487ea441d11SAl Viro 2488ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2489d31da0f0SAl Viro s = path.mnt->mnt_sb; 2490d31da0f0SAl Viro atomic_inc(&s->s_active); 2491ea441d11SAl Viro mntput(path.mnt); 2492ea441d11SAl Viro /* lock the sucker */ 2493d31da0f0SAl Viro down_write(&s->s_umount); 2494ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2495ea441d11SAl Viro return path.dentry; 2496ea441d11SAl Viro } 2497ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2498ea441d11SAl Viro 2499bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2500bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 25011da177e4SLinus Torvalds { 2502eca6f534SVegard Nossum int ret; 2503eca6f534SVegard Nossum char *kernel_type; 250491a27b2aSJeff Layton struct filename *kernel_dir; 2505eca6f534SVegard Nossum char *kernel_dev; 25061da177e4SLinus Torvalds unsigned long data_page; 25071da177e4SLinus Torvalds 2508eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2509eca6f534SVegard Nossum if (ret < 0) 2510eca6f534SVegard Nossum goto out_type; 25111da177e4SLinus Torvalds 2512eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2513eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2514eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2515eca6f534SVegard Nossum goto out_dir; 2516eca6f534SVegard Nossum } 25171da177e4SLinus Torvalds 2518eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2519eca6f534SVegard Nossum if (ret < 0) 2520eca6f534SVegard Nossum goto out_dev; 25211da177e4SLinus Torvalds 2522eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2523eca6f534SVegard Nossum if (ret < 0) 2524eca6f534SVegard Nossum goto out_data; 25251da177e4SLinus Torvalds 252691a27b2aSJeff Layton ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags, 2527eca6f534SVegard Nossum (void *) data_page); 2528eca6f534SVegard Nossum 25291da177e4SLinus Torvalds free_page(data_page); 2530eca6f534SVegard Nossum out_data: 2531eca6f534SVegard Nossum kfree(kernel_dev); 2532eca6f534SVegard Nossum out_dev: 2533eca6f534SVegard Nossum putname(kernel_dir); 2534eca6f534SVegard Nossum out_dir: 2535eca6f534SVegard Nossum kfree(kernel_type); 2536eca6f534SVegard Nossum out_type: 2537eca6f534SVegard Nossum return ret; 25381da177e4SLinus Torvalds } 25391da177e4SLinus Torvalds 25401da177e4SLinus Torvalds /* 2541afac7cbaSAl Viro * Return true if path is reachable from root 2542afac7cbaSAl Viro * 2543afac7cbaSAl Viro * namespace_sem or vfsmount_lock is held 2544afac7cbaSAl Viro */ 2545643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2546afac7cbaSAl Viro const struct path *root) 2547afac7cbaSAl Viro { 2548643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2549a73324daSAl Viro dentry = mnt->mnt_mountpoint; 25500714a533SAl Viro mnt = mnt->mnt_parent; 2551afac7cbaSAl Viro } 2552643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2553afac7cbaSAl Viro } 2554afac7cbaSAl Viro 2555afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2556afac7cbaSAl Viro { 2557afac7cbaSAl Viro int res; 2558962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 2559643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 2560962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 2561afac7cbaSAl Viro return res; 2562afac7cbaSAl Viro } 2563afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2564afac7cbaSAl Viro 2565afac7cbaSAl Viro /* 25661da177e4SLinus Torvalds * pivot_root Semantics: 25671da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 25681da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 25691da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 25701da177e4SLinus Torvalds * 25711da177e4SLinus Torvalds * Restrictions: 25721da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 25731da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 25741da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 25751da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 25761da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 25771da177e4SLinus Torvalds * 25784a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 25794a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 25804a0d11faSNeil Brown * in this situation. 25814a0d11faSNeil Brown * 25821da177e4SLinus Torvalds * Notes: 25831da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 25841da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 25851da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 25861da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 25871da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 25881da177e4SLinus Torvalds * first. 25891da177e4SLinus Torvalds */ 25903480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 25913480b257SHeiko Carstens const char __user *, put_old) 25921da177e4SLinus Torvalds { 25932d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 259484d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 259584d17192SAl Viro struct mountpoint *old_mp, *root_mp; 25961da177e4SLinus Torvalds int error; 25971da177e4SLinus Torvalds 25989b40bc90SAl Viro if (!may_mount()) 25991da177e4SLinus Torvalds return -EPERM; 26001da177e4SLinus Torvalds 26012d8f3038SAl Viro error = user_path_dir(new_root, &new); 26021da177e4SLinus Torvalds if (error) 26031da177e4SLinus Torvalds goto out0; 26041da177e4SLinus Torvalds 26052d8f3038SAl Viro error = user_path_dir(put_old, &old); 26061da177e4SLinus Torvalds if (error) 26071da177e4SLinus Torvalds goto out1; 26081da177e4SLinus Torvalds 26092d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2610b12cea91SAl Viro if (error) 2611b12cea91SAl Viro goto out2; 26121da177e4SLinus Torvalds 2613f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 261484d17192SAl Viro old_mp = lock_mount(&old); 261584d17192SAl Viro error = PTR_ERR(old_mp); 261684d17192SAl Viro if (IS_ERR(old_mp)) 2617b12cea91SAl Viro goto out3; 2618b12cea91SAl Viro 26191da177e4SLinus Torvalds error = -EINVAL; 2620419148daSAl Viro new_mnt = real_mount(new.mnt); 2621419148daSAl Viro root_mnt = real_mount(root.mnt); 262284d17192SAl Viro old_mnt = real_mount(old.mnt); 262384d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 2624fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2625fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2626b12cea91SAl Viro goto out4; 2627143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2628b12cea91SAl Viro goto out4; 26291da177e4SLinus Torvalds error = -ENOENT; 2630f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2631b12cea91SAl Viro goto out4; 26321da177e4SLinus Torvalds error = -EBUSY; 263384d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 2634b12cea91SAl Viro goto out4; /* loop, on the same file system */ 26351da177e4SLinus Torvalds error = -EINVAL; 26368c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2637b12cea91SAl Viro goto out4; /* not a mountpoint */ 2638676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2639b12cea91SAl Viro goto out4; /* not attached */ 264084d17192SAl Viro root_mp = root_mnt->mnt_mp; 26412d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2642b12cea91SAl Viro goto out4; /* not a mountpoint */ 2643676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2644b12cea91SAl Viro goto out4; /* not attached */ 26454ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 264684d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 2647b12cea91SAl Viro goto out4; 264884d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 2649962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2650419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2651419148daSAl Viro detach_mnt(root_mnt, &root_parent); 26524ac91378SJan Blunck /* mount old root on put_old */ 265384d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 26544ac91378SJan Blunck /* mount new_root on / */ 265584d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 26566b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2657962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 26582d8f3038SAl Viro chroot_fs_refs(&root, &new); 265984d17192SAl Viro put_mountpoint(root_mp); 26601da177e4SLinus Torvalds error = 0; 2661b12cea91SAl Viro out4: 266284d17192SAl Viro unlock_mount(old_mp); 2663b12cea91SAl Viro if (!error) { 26641a390689SAl Viro path_put(&root_parent); 26651a390689SAl Viro path_put(&parent_path); 2666b12cea91SAl Viro } 2667b12cea91SAl Viro out3: 26688c3ee42eSAl Viro path_put(&root); 2669b12cea91SAl Viro out2: 26702d8f3038SAl Viro path_put(&old); 26711da177e4SLinus Torvalds out1: 26722d8f3038SAl Viro path_put(&new); 26731da177e4SLinus Torvalds out0: 26741da177e4SLinus Torvalds return error; 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds static void __init init_mount_tree(void) 26781da177e4SLinus Torvalds { 26791da177e4SLinus Torvalds struct vfsmount *mnt; 26806b3286edSKirill Korotaev struct mnt_namespace *ns; 2681ac748a09SJan Blunck struct path root; 26820c55cfc4SEric W. Biederman struct file_system_type *type; 26831da177e4SLinus Torvalds 26840c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 26850c55cfc4SEric W. Biederman if (!type) 26860c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 26870c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 26880c55cfc4SEric W. Biederman put_filesystem(type); 26891da177e4SLinus Torvalds if (IS_ERR(mnt)) 26901da177e4SLinus Torvalds panic("Can't create rootfs"); 2691b3e19d92SNick Piggin 26923b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 26933b22edc5STrond Myklebust if (IS_ERR(ns)) 26941da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 26951da177e4SLinus Torvalds 26966b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 26976b3286edSKirill Korotaev get_mnt_ns(ns); 26981da177e4SLinus Torvalds 2699be08d6d2SAl Viro root.mnt = mnt; 2700be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2701ac748a09SJan Blunck 2702ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2703ac748a09SJan Blunck set_fs_root(current->fs, &root); 27041da177e4SLinus Torvalds } 27051da177e4SLinus Torvalds 270674bf17cfSDenis Cheng void __init mnt_init(void) 27071da177e4SLinus Torvalds { 270813f14b4dSEric Dumazet unsigned u; 270915a67dd8SRandy Dunlap int err; 27101da177e4SLinus Torvalds 2711390c6843SRam Pai init_rwsem(&namespace_sem); 2712390c6843SRam Pai 27137d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 271420c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 27151da177e4SLinus Torvalds 2716b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 271784d17192SAl Viro mountpoint_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 27181da177e4SLinus Torvalds 271984d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 27201da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 27211da177e4SLinus Torvalds 272280cdc6daSMandeep Singh Baines printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE); 27231da177e4SLinus Torvalds 272413f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 272513f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 272684d17192SAl Viro for (u = 0; u < HASH_SIZE; u++) 272784d17192SAl Viro INIT_LIST_HEAD(&mountpoint_hashtable[u]); 27281da177e4SLinus Torvalds 2729962830dfSAndi Kleen br_lock_init(&vfsmount_lock); 273099b7db7bSNick Piggin 273115a67dd8SRandy Dunlap err = sysfs_init(); 273215a67dd8SRandy Dunlap if (err) 273315a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 27348e24eea7SHarvey Harrison __func__, err); 273500d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 273600d26666SGreg Kroah-Hartman if (!fs_kobj) 27378e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 27381da177e4SLinus Torvalds init_rootfs(); 27391da177e4SLinus Torvalds init_mount_tree(); 27401da177e4SLinus Torvalds } 27411da177e4SLinus Torvalds 2742616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 27431da177e4SLinus Torvalds { 274470fbcdf4SRam Pai LIST_HEAD(umount_list); 2745616511d0STrond Myklebust 2746d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2747616511d0STrond Myklebust return; 2748390c6843SRam Pai down_write(&namespace_sem); 2749962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2750be08d6d2SAl Viro umount_tree(ns->root, 0, &umount_list); 2751962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2752390c6843SRam Pai up_write(&namespace_sem); 275370fbcdf4SRam Pai release_mounts(&umount_list); 2754771b1371SEric W. Biederman free_mnt_ns(ns); 27551da177e4SLinus Torvalds } 27569d412a43SAl Viro 27579d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 27589d412a43SAl Viro { 2759423e0ab0STim Chen struct vfsmount *mnt; 2760423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2761423e0ab0STim Chen if (!IS_ERR(mnt)) { 2762423e0ab0STim Chen /* 2763423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2764423e0ab0STim Chen * we unmount before file sys is unregistered 2765423e0ab0STim Chen */ 2766f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 2767423e0ab0STim Chen } 2768423e0ab0STim Chen return mnt; 27699d412a43SAl Viro } 27709d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2771423e0ab0STim Chen 2772423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2773423e0ab0STim Chen { 2774423e0ab0STim Chen /* release long term mount so mount point can be released */ 2775423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2776f7a99c5bSAl Viro br_write_lock(&vfsmount_lock); 2777f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 2778f7a99c5bSAl Viro br_write_unlock(&vfsmount_lock); 2779423e0ab0STim Chen mntput(mnt); 2780423e0ab0STim Chen } 2781423e0ab0STim Chen } 2782423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 278302125a82SAl Viro 278402125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 278502125a82SAl Viro { 2786143c8c91SAl Viro return check_mnt(real_mount(mnt)); 278702125a82SAl Viro } 27888823c079SEric W. Biederman 27898823c079SEric W. Biederman static void *mntns_get(struct task_struct *task) 27908823c079SEric W. Biederman { 27918823c079SEric W. Biederman struct mnt_namespace *ns = NULL; 27928823c079SEric W. Biederman struct nsproxy *nsproxy; 27938823c079SEric W. Biederman 27948823c079SEric W. Biederman rcu_read_lock(); 27958823c079SEric W. Biederman nsproxy = task_nsproxy(task); 27968823c079SEric W. Biederman if (nsproxy) { 27978823c079SEric W. Biederman ns = nsproxy->mnt_ns; 27988823c079SEric W. Biederman get_mnt_ns(ns); 27998823c079SEric W. Biederman } 28008823c079SEric W. Biederman rcu_read_unlock(); 28018823c079SEric W. Biederman 28028823c079SEric W. Biederman return ns; 28038823c079SEric W. Biederman } 28048823c079SEric W. Biederman 28058823c079SEric W. Biederman static void mntns_put(void *ns) 28068823c079SEric W. Biederman { 28078823c079SEric W. Biederman put_mnt_ns(ns); 28088823c079SEric W. Biederman } 28098823c079SEric W. Biederman 28108823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns) 28118823c079SEric W. Biederman { 28128823c079SEric W. Biederman struct fs_struct *fs = current->fs; 28138823c079SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 28148823c079SEric W. Biederman struct path root; 28158823c079SEric W. Biederman 28160c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 28175e4a0847SEric W. Biederman !nsown_capable(CAP_SYS_CHROOT) || 28185e4a0847SEric W. Biederman !nsown_capable(CAP_SYS_ADMIN)) 2819ae11e0f1SZhao Hongjiang return -EPERM; 28208823c079SEric W. Biederman 28218823c079SEric W. Biederman if (fs->users != 1) 28228823c079SEric W. Biederman return -EINVAL; 28238823c079SEric W. Biederman 28248823c079SEric W. Biederman get_mnt_ns(mnt_ns); 28258823c079SEric W. Biederman put_mnt_ns(nsproxy->mnt_ns); 28268823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 28278823c079SEric W. Biederman 28288823c079SEric W. Biederman /* Find the root */ 28298823c079SEric W. Biederman root.mnt = &mnt_ns->root->mnt; 28308823c079SEric W. Biederman root.dentry = mnt_ns->root->mnt.mnt_root; 28318823c079SEric W. Biederman path_get(&root); 28328823c079SEric W. Biederman while(d_mountpoint(root.dentry) && follow_down_one(&root)) 28338823c079SEric W. Biederman ; 28348823c079SEric W. Biederman 28358823c079SEric W. Biederman /* Update the pwd and root */ 28368823c079SEric W. Biederman set_fs_pwd(fs, &root); 28378823c079SEric W. Biederman set_fs_root(fs, &root); 28388823c079SEric W. Biederman 28398823c079SEric W. Biederman path_put(&root); 28408823c079SEric W. Biederman return 0; 28418823c079SEric W. Biederman } 28428823c079SEric W. Biederman 284398f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns) 284498f842e6SEric W. Biederman { 284598f842e6SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 284698f842e6SEric W. Biederman return mnt_ns->proc_inum; 284798f842e6SEric W. Biederman } 284898f842e6SEric W. Biederman 28498823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 28508823c079SEric W. Biederman .name = "mnt", 28518823c079SEric W. Biederman .type = CLONE_NEWNS, 28528823c079SEric W. Biederman .get = mntns_get, 28538823c079SEric W. Biederman .put = mntns_put, 28548823c079SEric W. Biederman .install = mntns_install, 285598f842e6SEric W. Biederman .inum = mntns_inum, 28568823c079SEric W. Biederman }; 2857