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; 39e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 40390c6843SRam Pai static struct rw_semaphore namespace_sem; 411da177e4SLinus Torvalds 42f87fd4c2SMiklos Szeredi /* /sys/fs */ 4300d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 4400d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 45f87fd4c2SMiklos Szeredi 4699b7db7bSNick Piggin /* 4799b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 4899b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 4999b7db7bSNick Piggin * up the tree. 5099b7db7bSNick Piggin * 5199b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 5299b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 5399b7db7bSNick Piggin */ 5499b7db7bSNick Piggin DEFINE_BRLOCK(vfsmount_lock); 5599b7db7bSNick Piggin 561da177e4SLinus Torvalds static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) 571da177e4SLinus Torvalds { 581da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 591da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 6013f14b4dSEric Dumazet tmp = tmp + (tmp >> HASH_SHIFT); 6113f14b4dSEric Dumazet return tmp & (HASH_SIZE - 1); 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 643d733633SDave Hansen #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16) 653d733633SDave Hansen 6699b7db7bSNick Piggin /* 6799b7db7bSNick Piggin * allocation is serialized by namespace_sem, but we need the spinlock to 6899b7db7bSNick Piggin * serialize with freeing. 6999b7db7bSNick Piggin */ 70b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 7173cd49ecSMiklos Szeredi { 7273cd49ecSMiklos Szeredi int res; 7373cd49ecSMiklos Szeredi 7473cd49ecSMiklos Szeredi retry: 7573cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 7699b7db7bSNick Piggin spin_lock(&mnt_id_lock); 7715169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 78f21f6220SAl Viro if (!res) 7915169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 8099b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 8173cd49ecSMiklos Szeredi if (res == -EAGAIN) 8273cd49ecSMiklos Szeredi goto retry; 8373cd49ecSMiklos Szeredi 8473cd49ecSMiklos Szeredi return res; 8573cd49ecSMiklos Szeredi } 8673cd49ecSMiklos Szeredi 87b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 8873cd49ecSMiklos Szeredi { 8915169fe7SAl Viro int id = mnt->mnt_id; 9099b7db7bSNick Piggin spin_lock(&mnt_id_lock); 91f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 92f21f6220SAl Viro if (mnt_id_start > id) 93f21f6220SAl Viro mnt_id_start = id; 9499b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 9573cd49ecSMiklos Szeredi } 9673cd49ecSMiklos Szeredi 97719f5d7fSMiklos Szeredi /* 98719f5d7fSMiklos Szeredi * Allocate a new peer group ID 99719f5d7fSMiklos Szeredi * 100719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 101719f5d7fSMiklos Szeredi */ 1024b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 103719f5d7fSMiklos Szeredi { 104f21f6220SAl Viro int res; 105f21f6220SAl Viro 106719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 107719f5d7fSMiklos Szeredi return -ENOMEM; 108719f5d7fSMiklos Szeredi 109f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 110f21f6220SAl Viro mnt_group_start, 11115169fe7SAl Viro &mnt->mnt_group_id); 112f21f6220SAl Viro if (!res) 11315169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 114f21f6220SAl Viro 115f21f6220SAl Viro return res; 116719f5d7fSMiklos Szeredi } 117719f5d7fSMiklos Szeredi 118719f5d7fSMiklos Szeredi /* 119719f5d7fSMiklos Szeredi * Release a peer group ID 120719f5d7fSMiklos Szeredi */ 1214b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 122719f5d7fSMiklos Szeredi { 12315169fe7SAl Viro int id = mnt->mnt_group_id; 124f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 125f21f6220SAl Viro if (mnt_group_start > id) 126f21f6220SAl Viro mnt_group_start = id; 12715169fe7SAl Viro mnt->mnt_group_id = 0; 128719f5d7fSMiklos Szeredi } 129719f5d7fSMiklos Szeredi 130b3e19d92SNick Piggin /* 131b3e19d92SNick Piggin * vfsmount lock must be held for read 132b3e19d92SNick Piggin */ 13383adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 134b3e19d92SNick Piggin { 135b3e19d92SNick Piggin #ifdef CONFIG_SMP 13668e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 137b3e19d92SNick Piggin #else 138b3e19d92SNick Piggin preempt_disable(); 13968e8a9feSAl Viro mnt->mnt_count += n; 140b3e19d92SNick Piggin preempt_enable(); 141b3e19d92SNick Piggin #endif 142b3e19d92SNick Piggin } 143b3e19d92SNick Piggin 144b3e19d92SNick Piggin /* 145b3e19d92SNick Piggin * vfsmount lock must be held for write 146b3e19d92SNick Piggin */ 14783adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 148b3e19d92SNick Piggin { 149b3e19d92SNick Piggin #ifdef CONFIG_SMP 150f03c6599SAl Viro unsigned int count = 0; 151b3e19d92SNick Piggin int cpu; 152b3e19d92SNick Piggin 153b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 15468e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 155b3e19d92SNick Piggin } 156b3e19d92SNick Piggin 157b3e19d92SNick Piggin return count; 158b3e19d92SNick Piggin #else 15968e8a9feSAl Viro return mnt->mnt_count; 160b3e19d92SNick Piggin #endif 161b3e19d92SNick Piggin } 162b3e19d92SNick Piggin 163b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1641da177e4SLinus Torvalds { 165c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 166c63181e6SAl Viro if (mnt) { 16773cd49ecSMiklos Szeredi int err; 16873cd49ecSMiklos Szeredi 169c63181e6SAl Viro err = mnt_alloc_id(mnt); 17088b38782SLi Zefan if (err) 17188b38782SLi Zefan goto out_free_cache; 17288b38782SLi Zefan 17388b38782SLi Zefan if (name) { 174c63181e6SAl Viro mnt->mnt_devname = kstrdup(name, GFP_KERNEL); 175c63181e6SAl Viro if (!mnt->mnt_devname) 17688b38782SLi Zefan goto out_free_id; 17773cd49ecSMiklos Szeredi } 17873cd49ecSMiklos Szeredi 179b3e19d92SNick Piggin #ifdef CONFIG_SMP 180c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 181c63181e6SAl Viro if (!mnt->mnt_pcp) 182b3e19d92SNick Piggin goto out_free_devname; 183b3e19d92SNick Piggin 184c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 185b3e19d92SNick Piggin #else 186c63181e6SAl Viro mnt->mnt_count = 1; 187c63181e6SAl Viro mnt->mnt_writers = 0; 188b3e19d92SNick Piggin #endif 189b3e19d92SNick Piggin 190c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_hash); 191c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 192c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 193c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 194c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 195c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 196c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 197c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 1982504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 1992504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 2002504c5d6SAndreas Gruenbacher #endif 2011da177e4SLinus Torvalds } 202c63181e6SAl Viro return mnt; 20388b38782SLi Zefan 204d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 205d3ef3d73Snpiggin@suse.de out_free_devname: 206c63181e6SAl Viro kfree(mnt->mnt_devname); 207d3ef3d73Snpiggin@suse.de #endif 20888b38782SLi Zefan out_free_id: 209c63181e6SAl Viro mnt_free_id(mnt); 21088b38782SLi Zefan out_free_cache: 211c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 21288b38782SLi Zefan return NULL; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2158366025eSDave Hansen /* 2168366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2178366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2188366025eSDave Hansen * We must keep track of when those operations start 2198366025eSDave Hansen * (for permission checks) and when they end, so that 2208366025eSDave Hansen * we can determine when writes are able to occur to 2218366025eSDave Hansen * a filesystem. 2228366025eSDave Hansen */ 2233d733633SDave Hansen /* 2243d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2253d733633SDave Hansen * @mnt: the mount to check for its write status 2263d733633SDave Hansen * 2273d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2283d733633SDave Hansen * It does not guarantee that the filesystem will stay 2293d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2303d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2313d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2323d733633SDave Hansen * r/w. 2333d733633SDave Hansen */ 2343d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2353d733633SDave Hansen { 2362e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2372e4b7fcdSDave Hansen return 1; 2382e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2392e4b7fcdSDave Hansen return 1; 2402e4b7fcdSDave Hansen return 0; 2413d733633SDave Hansen } 2423d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2433d733633SDave Hansen 24483adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2453d733633SDave Hansen { 246d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 24768e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 248d3ef3d73Snpiggin@suse.de #else 24968e8a9feSAl Viro mnt->mnt_writers++; 250d3ef3d73Snpiggin@suse.de #endif 2513d733633SDave Hansen } 2523d733633SDave Hansen 25383adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2543d733633SDave Hansen { 255d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 25668e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 257d3ef3d73Snpiggin@suse.de #else 25868e8a9feSAl Viro mnt->mnt_writers--; 259d3ef3d73Snpiggin@suse.de #endif 260d3ef3d73Snpiggin@suse.de } 261d3ef3d73Snpiggin@suse.de 26283adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 263d3ef3d73Snpiggin@suse.de { 264d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 265d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2663d733633SDave Hansen int cpu; 2673d733633SDave Hansen 2683d733633SDave Hansen for_each_possible_cpu(cpu) { 26968e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2703d733633SDave Hansen } 2713d733633SDave Hansen 272d3ef3d73Snpiggin@suse.de return count; 273d3ef3d73Snpiggin@suse.de #else 274d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 275d3ef3d73Snpiggin@suse.de #endif 2763d733633SDave Hansen } 2773d733633SDave Hansen 2784ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2794ed5e82fSMiklos Szeredi { 2804ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 2814ed5e82fSMiklos Szeredi return 1; 2824ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 2834ed5e82fSMiklos Szeredi smp_rmb(); 2844ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 2854ed5e82fSMiklos Szeredi } 2864ed5e82fSMiklos Szeredi 2873d733633SDave Hansen /* 288eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 289eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 290eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 291eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 2923d733633SDave Hansen */ 2938366025eSDave Hansen /** 294eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 29583adc753SAl Viro * @m: the mount on which to take a write 2968366025eSDave Hansen * 297eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 298eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 299eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 300eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 301eb04c282SJan Kara * called. This is effectively a refcount. 3028366025eSDave Hansen */ 303eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3048366025eSDave Hansen { 30583adc753SAl Viro struct mount *mnt = real_mount(m); 3063d733633SDave Hansen int ret = 0; 3073d733633SDave Hansen 308d3ef3d73Snpiggin@suse.de preempt_disable(); 309c6653a83SNick Piggin mnt_inc_writers(mnt); 310d3ef3d73Snpiggin@suse.de /* 311c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 312d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 313d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 314d3ef3d73Snpiggin@suse.de */ 315d3ef3d73Snpiggin@suse.de smp_mb(); 31683adc753SAl Viro while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 317d3ef3d73Snpiggin@suse.de cpu_relax(); 318d3ef3d73Snpiggin@suse.de /* 319d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 320d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 321d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 322d3ef3d73Snpiggin@suse.de */ 323d3ef3d73Snpiggin@suse.de smp_rmb(); 3244ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 325c6653a83SNick Piggin mnt_dec_writers(mnt); 3263d733633SDave Hansen ret = -EROFS; 3273d733633SDave Hansen } 328d3ef3d73Snpiggin@suse.de preempt_enable(); 329eb04c282SJan Kara 330eb04c282SJan Kara return ret; 331eb04c282SJan Kara } 332eb04c282SJan Kara 333eb04c282SJan Kara /** 334eb04c282SJan Kara * mnt_want_write - get write access to a mount 335eb04c282SJan Kara * @m: the mount on which to take a write 336eb04c282SJan Kara * 337eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 338eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 339eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 340eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 341eb04c282SJan Kara */ 342eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 343eb04c282SJan Kara { 344eb04c282SJan Kara int ret; 345eb04c282SJan Kara 346eb04c282SJan Kara sb_start_write(m->mnt_sb); 347eb04c282SJan Kara ret = __mnt_want_write(m); 348eb04c282SJan Kara if (ret) 349eb04c282SJan Kara sb_end_write(m->mnt_sb); 3503d733633SDave Hansen return ret; 3518366025eSDave Hansen } 3528366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3538366025eSDave Hansen 3548366025eSDave Hansen /** 35596029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 35696029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 35796029c4eSnpiggin@suse.de * 35896029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 35996029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 36096029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 36196029c4eSnpiggin@suse.de * on it. This allows some optimisation. 36296029c4eSnpiggin@suse.de * 36396029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 36496029c4eSnpiggin@suse.de * drop the reference. 36596029c4eSnpiggin@suse.de */ 36696029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 36796029c4eSnpiggin@suse.de { 36896029c4eSnpiggin@suse.de /* superblock may be r/o */ 36996029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 37096029c4eSnpiggin@suse.de return -EROFS; 37196029c4eSnpiggin@suse.de preempt_disable(); 37283adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 37396029c4eSnpiggin@suse.de preempt_enable(); 37496029c4eSnpiggin@suse.de return 0; 37596029c4eSnpiggin@suse.de } 37696029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 37796029c4eSnpiggin@suse.de 37896029c4eSnpiggin@suse.de /** 379eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 380eb04c282SJan Kara * @file: the file who's mount on which to take a write 381eb04c282SJan Kara * 382eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 383eb04c282SJan Kara * do some optimisations if the file is open for write already 384eb04c282SJan Kara */ 385eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 386eb04c282SJan Kara { 387eb04c282SJan Kara struct inode *inode = file->f_dentry->d_inode; 388eb04c282SJan Kara 389eb04c282SJan Kara if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode)) 390eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 391eb04c282SJan Kara else 392eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 393eb04c282SJan Kara } 394eb04c282SJan Kara 395eb04c282SJan Kara /** 39696029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 39796029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 39896029c4eSnpiggin@suse.de * 39996029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 40096029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 40196029c4eSnpiggin@suse.de */ 40296029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 40396029c4eSnpiggin@suse.de { 404eb04c282SJan Kara int ret; 405eb04c282SJan Kara 406eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 407eb04c282SJan Kara ret = __mnt_want_write_file(file); 408eb04c282SJan Kara if (ret) 409eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 410eb04c282SJan Kara return ret; 41196029c4eSnpiggin@suse.de } 41296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 41396029c4eSnpiggin@suse.de 41496029c4eSnpiggin@suse.de /** 415eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4168366025eSDave Hansen * @mnt: the mount on which to give up write access 4178366025eSDave Hansen * 4188366025eSDave Hansen * Tells the low-level filesystem that we are done 4198366025eSDave Hansen * performing writes to it. Must be matched with 420eb04c282SJan Kara * __mnt_want_write() call above. 4218366025eSDave Hansen */ 422eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4238366025eSDave Hansen { 424d3ef3d73Snpiggin@suse.de preempt_disable(); 42583adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 426d3ef3d73Snpiggin@suse.de preempt_enable(); 4278366025eSDave Hansen } 428eb04c282SJan Kara 429eb04c282SJan Kara /** 430eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 431eb04c282SJan Kara * @mnt: the mount on which to give up write access 432eb04c282SJan Kara * 433eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 434eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 435eb04c282SJan Kara * mnt_want_write() call above. 436eb04c282SJan Kara */ 437eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 438eb04c282SJan Kara { 439eb04c282SJan Kara __mnt_drop_write(mnt); 440eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 441eb04c282SJan Kara } 4428366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4438366025eSDave Hansen 444eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 445eb04c282SJan Kara { 446eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 447eb04c282SJan Kara } 448eb04c282SJan Kara 4492a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4502a79f17eSAl Viro { 4512a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4522a79f17eSAl Viro } 4532a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4542a79f17eSAl Viro 45583adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4568366025eSDave Hansen { 4573d733633SDave Hansen int ret = 0; 4583d733633SDave Hansen 459962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 46083adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 461d3ef3d73Snpiggin@suse.de /* 462d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 463d3ef3d73Snpiggin@suse.de * should be visible before we do. 464d3ef3d73Snpiggin@suse.de */ 465d3ef3d73Snpiggin@suse.de smp_mb(); 466d3ef3d73Snpiggin@suse.de 467d3ef3d73Snpiggin@suse.de /* 468d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 469d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 470d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 471d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 472d3ef3d73Snpiggin@suse.de * 473d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 474d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 475d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 476d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 477d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 478d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 479d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 480d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 481d3ef3d73Snpiggin@suse.de * we're counting up here. 482d3ef3d73Snpiggin@suse.de */ 483c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 484d3ef3d73Snpiggin@suse.de ret = -EBUSY; 485d3ef3d73Snpiggin@suse.de else 48683adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 487d3ef3d73Snpiggin@suse.de /* 488d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 489d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 490d3ef3d73Snpiggin@suse.de */ 491d3ef3d73Snpiggin@suse.de smp_wmb(); 49283adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 493962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 4943d733633SDave Hansen return ret; 4953d733633SDave Hansen } 4968366025eSDave Hansen 49783adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 4982e4b7fcdSDave Hansen { 499962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 50083adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 501962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 5022e4b7fcdSDave Hansen } 5032e4b7fcdSDave Hansen 5044ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5054ed5e82fSMiklos Szeredi { 5064ed5e82fSMiklos Szeredi struct mount *mnt; 5074ed5e82fSMiklos Szeredi int err = 0; 5084ed5e82fSMiklos Szeredi 5098e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5108e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5118e8b8796SMiklos Szeredi return -EBUSY; 5128e8b8796SMiklos Szeredi 513962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 5144ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5154ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5164ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5174ed5e82fSMiklos Szeredi smp_mb(); 5184ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5194ed5e82fSMiklos Szeredi err = -EBUSY; 5204ed5e82fSMiklos Szeredi break; 5214ed5e82fSMiklos Szeredi } 5224ed5e82fSMiklos Szeredi } 5234ed5e82fSMiklos Szeredi } 5248e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5258e8b8796SMiklos Szeredi err = -EBUSY; 5268e8b8796SMiklos Szeredi 5274ed5e82fSMiklos Szeredi if (!err) { 5284ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5294ed5e82fSMiklos Szeredi smp_wmb(); 5304ed5e82fSMiklos Szeredi } 5314ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5324ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5334ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5344ed5e82fSMiklos Szeredi } 535962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 5364ed5e82fSMiklos Szeredi 5374ed5e82fSMiklos Szeredi return err; 5384ed5e82fSMiklos Szeredi } 5394ed5e82fSMiklos Szeredi 540b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5411da177e4SLinus Torvalds { 54252ba1621SAl Viro kfree(mnt->mnt_devname); 54373cd49ecSMiklos Szeredi mnt_free_id(mnt); 544d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 54568e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 546d3ef3d73Snpiggin@suse.de #endif 547b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds /* 551a05964f3SRam Pai * find the first or last mount at @dentry on vfsmount @mnt depending on 552a05964f3SRam Pai * @dir. If @dir is set return the first mount else return the last mount. 55399b7db7bSNick Piggin * vfsmount_lock must be held for read or write. 5541da177e4SLinus Torvalds */ 555c7105365SAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry, 556a05964f3SRam Pai int dir) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds struct list_head *head = mount_hashtable + hash(mnt, dentry); 5591da177e4SLinus Torvalds struct list_head *tmp = head; 560c7105365SAl Viro struct mount *p, *found = NULL; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds for (;;) { 563a05964f3SRam Pai tmp = dir ? tmp->next : tmp->prev; 5641da177e4SLinus Torvalds p = NULL; 5651da177e4SLinus Torvalds if (tmp == head) 5661da177e4SLinus Torvalds break; 5671b8e5564SAl Viro p = list_entry(tmp, struct mount, mnt_hash); 568a73324daSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) { 569a05964f3SRam Pai found = p; 5701da177e4SLinus Torvalds break; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds return found; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 576a05964f3SRam Pai /* 577f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 578f015f126SDavid Howells * 579f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 580f015f126SDavid Howells * following mounts: 581f015f126SDavid Howells * 582f015f126SDavid Howells * mount /dev/sda1 /mnt 583f015f126SDavid Howells * mount /dev/sda2 /mnt 584f015f126SDavid Howells * mount /dev/sda3 /mnt 585f015f126SDavid Howells * 586f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 587f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 588f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 589f015f126SDavid Howells * 590f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 591a05964f3SRam Pai */ 5921c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 593a05964f3SRam Pai { 594c7105365SAl Viro struct mount *child_mnt; 59599b7db7bSNick Piggin 596962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 597c7105365SAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry, 1); 598c7105365SAl Viro if (child_mnt) { 599c7105365SAl Viro mnt_add_count(child_mnt, 1); 600962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 601c7105365SAl Viro return &child_mnt->mnt; 602c7105365SAl Viro } else { 603962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 604c7105365SAl Viro return NULL; 605c7105365SAl Viro } 606a05964f3SRam Pai } 607a05964f3SRam Pai 608143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 6091da177e4SLinus Torvalds { 6106b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 61399b7db7bSNick Piggin /* 61499b7db7bSNick Piggin * vfsmount lock must be held for write 61599b7db7bSNick Piggin */ 6166b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 6175addc5ddSAl Viro { 6185addc5ddSAl Viro if (ns) { 6195addc5ddSAl Viro ns->event = ++event; 6205addc5ddSAl Viro wake_up_interruptible(&ns->poll); 6215addc5ddSAl Viro } 6225addc5ddSAl Viro } 6235addc5ddSAl Viro 62499b7db7bSNick Piggin /* 62599b7db7bSNick Piggin * vfsmount lock must be held for write 62699b7db7bSNick Piggin */ 6276b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 6285addc5ddSAl Viro { 6295addc5ddSAl Viro if (ns && ns->event != event) { 6305addc5ddSAl Viro ns->event = event; 6315addc5ddSAl Viro wake_up_interruptible(&ns->poll); 6325addc5ddSAl Viro } 6335addc5ddSAl Viro } 6345addc5ddSAl Viro 63599b7db7bSNick Piggin /* 6365f57cbccSNick Piggin * Clear dentry's mounted state if it has no remaining mounts. 6375f57cbccSNick Piggin * vfsmount_lock must be held for write. 6385f57cbccSNick Piggin */ 639aa0a4cf0SAl Viro static void dentry_reset_mounted(struct dentry *dentry) 6405f57cbccSNick Piggin { 6415f57cbccSNick Piggin unsigned u; 6425f57cbccSNick Piggin 6435f57cbccSNick Piggin for (u = 0; u < HASH_SIZE; u++) { 644d5e50f74SAl Viro struct mount *p; 6455f57cbccSNick Piggin 6461b8e5564SAl Viro list_for_each_entry(p, &mount_hashtable[u], mnt_hash) { 647a73324daSAl Viro if (p->mnt_mountpoint == dentry) 6485f57cbccSNick Piggin return; 6495f57cbccSNick Piggin } 6505f57cbccSNick Piggin } 6515f57cbccSNick Piggin spin_lock(&dentry->d_lock); 6525f57cbccSNick Piggin dentry->d_flags &= ~DCACHE_MOUNTED; 6535f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 6545f57cbccSNick Piggin } 6555f57cbccSNick Piggin 6565f57cbccSNick Piggin /* 65799b7db7bSNick Piggin * vfsmount lock must be held for write 65899b7db7bSNick Piggin */ 659419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 6601da177e4SLinus Torvalds { 661a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 6620714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 6630714a533SAl Viro mnt->mnt_parent = mnt; 664a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 6656b41d536SAl Viro list_del_init(&mnt->mnt_child); 6661b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 667aa0a4cf0SAl Viro dentry_reset_mounted(old_path->dentry); 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 67099b7db7bSNick Piggin /* 67199b7db7bSNick Piggin * vfsmount lock must be held for write 67299b7db7bSNick Piggin */ 67314cf1fa8SAl Viro void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry, 67444d964d6SAl Viro struct mount *child_mnt) 675b90fa9aeSRam Pai { 6763a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 677a73324daSAl Viro child_mnt->mnt_mountpoint = dget(dentry); 6783a2393d7SAl Viro child_mnt->mnt_parent = mnt; 6795f57cbccSNick Piggin spin_lock(&dentry->d_lock); 6805f57cbccSNick Piggin dentry->d_flags |= DCACHE_MOUNTED; 6815f57cbccSNick Piggin spin_unlock(&dentry->d_lock); 682b90fa9aeSRam Pai } 683b90fa9aeSRam Pai 68499b7db7bSNick Piggin /* 68599b7db7bSNick Piggin * vfsmount lock must be held for write 68699b7db7bSNick Piggin */ 687419148daSAl Viro static void attach_mnt(struct mount *mnt, struct path *path) 6881da177e4SLinus Torvalds { 68914cf1fa8SAl Viro mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt); 6901b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 6911a390689SAl Viro hash(path->mnt, path->dentry)); 6926b41d536SAl Viro list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts); 693b90fa9aeSRam Pai } 694b90fa9aeSRam Pai 695b90fa9aeSRam Pai /* 69699b7db7bSNick Piggin * vfsmount lock must be held for write 697b90fa9aeSRam Pai */ 6984b2619a5SAl Viro static void commit_tree(struct mount *mnt) 699b90fa9aeSRam Pai { 7000714a533SAl Viro struct mount *parent = mnt->mnt_parent; 70183adc753SAl Viro struct mount *m; 702b90fa9aeSRam Pai LIST_HEAD(head); 703143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 704b90fa9aeSRam Pai 7050714a533SAl Viro BUG_ON(parent == mnt); 706b90fa9aeSRam Pai 7071a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 708f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 709143c8c91SAl Viro m->mnt_ns = n; 710f03c6599SAl Viro 711b90fa9aeSRam Pai list_splice(&head, n->list.prev); 712b90fa9aeSRam Pai 7131b8e5564SAl Viro list_add_tail(&mnt->mnt_hash, mount_hashtable + 714a73324daSAl Viro hash(&parent->mnt, mnt->mnt_mountpoint)); 7156b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 7166b3286edSKirill Korotaev touch_mnt_namespace(n); 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 719909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 7201da177e4SLinus Torvalds { 7216b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 7226b41d536SAl Viro if (next == &p->mnt_mounts) { 7231da177e4SLinus Torvalds while (1) { 724909b0a88SAl Viro if (p == root) 7251da177e4SLinus Torvalds return NULL; 7266b41d536SAl Viro next = p->mnt_child.next; 7276b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 7281da177e4SLinus Torvalds break; 7290714a533SAl Viro p = p->mnt_parent; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds } 7326b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 735315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 7369676f0c6SRam Pai { 7376b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 7386b41d536SAl Viro while (prev != &p->mnt_mounts) { 7396b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 7406b41d536SAl Viro prev = p->mnt_mounts.prev; 7419676f0c6SRam Pai } 7429676f0c6SRam Pai return p; 7439676f0c6SRam Pai } 7449676f0c6SRam Pai 7459d412a43SAl Viro struct vfsmount * 7469d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 7479d412a43SAl Viro { 748b105e270SAl Viro struct mount *mnt; 7499d412a43SAl Viro struct dentry *root; 7509d412a43SAl Viro 7519d412a43SAl Viro if (!type) 7529d412a43SAl Viro return ERR_PTR(-ENODEV); 7539d412a43SAl Viro 7549d412a43SAl Viro mnt = alloc_vfsmnt(name); 7559d412a43SAl Viro if (!mnt) 7569d412a43SAl Viro return ERR_PTR(-ENOMEM); 7579d412a43SAl Viro 7589d412a43SAl Viro if (flags & MS_KERNMOUNT) 759b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 7609d412a43SAl Viro 7619d412a43SAl Viro root = mount_fs(type, flags, name, data); 7629d412a43SAl Viro if (IS_ERR(root)) { 7639d412a43SAl Viro free_vfsmnt(mnt); 7649d412a43SAl Viro return ERR_CAST(root); 7659d412a43SAl Viro } 7669d412a43SAl Viro 767b105e270SAl Viro mnt->mnt.mnt_root = root; 768b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 769a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7700714a533SAl Viro mnt->mnt_parent = mnt; 771962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 77239f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 773962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 774b105e270SAl Viro return &mnt->mnt; 7759d412a43SAl Viro } 7769d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 7779d412a43SAl Viro 77887129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 77936341f64SRam Pai int flag) 7801da177e4SLinus Torvalds { 78187129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 782be34d1a3SDavid Howells struct mount *mnt; 783be34d1a3SDavid Howells int err; 7841da177e4SLinus Torvalds 785be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 786be34d1a3SDavid Howells if (!mnt) 787be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 788be34d1a3SDavid Howells 7897a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 79015169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 791719f5d7fSMiklos Szeredi else 79215169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 793719f5d7fSMiklos Szeredi 79415169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 795be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 796719f5d7fSMiklos Szeredi if (err) 797719f5d7fSMiklos Szeredi goto out_free; 798719f5d7fSMiklos Szeredi } 799719f5d7fSMiklos Szeredi 80087129cc0SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD; 8011da177e4SLinus Torvalds atomic_inc(&sb->s_active); 802b105e270SAl Viro mnt->mnt.mnt_sb = sb; 803b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 804a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8050714a533SAl Viro mnt->mnt_parent = mnt; 806962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 80739f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 808962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 809b90fa9aeSRam Pai 8107a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 8117a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 8126776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 81332301920SAl Viro mnt->mnt_master = old; 814fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 8158aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 816fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 8176776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 818d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 8196776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 820d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 8215afe0022SRam Pai } 822b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 8230f0afb1dSAl Viro set_mnt_shared(mnt); 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 8261da177e4SLinus Torvalds * as the original if that was on one */ 82736341f64SRam Pai if (flag & CL_EXPIRE) { 8286776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 8296776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 8301da177e4SLinus Torvalds } 831be34d1a3SDavid Howells 832cb338d06SAl Viro return mnt; 833719f5d7fSMiklos Szeredi 834719f5d7fSMiklos Szeredi out_free: 835719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 836be34d1a3SDavid Howells return ERR_PTR(err); 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 83983adc753SAl Viro static inline void mntfree(struct mount *mnt) 8401da177e4SLinus Torvalds { 84183adc753SAl Viro struct vfsmount *m = &mnt->mnt; 84283adc753SAl Viro struct super_block *sb = m->mnt_sb; 843b3e19d92SNick Piggin 8443d733633SDave Hansen /* 8453d733633SDave Hansen * This probably indicates that somebody messed 8463d733633SDave Hansen * up a mnt_want/drop_write() pair. If this 8473d733633SDave Hansen * happens, the filesystem was probably unable 8483d733633SDave Hansen * to make r/w->r/o transitions. 8493d733633SDave Hansen */ 850d3ef3d73Snpiggin@suse.de /* 851b3e19d92SNick Piggin * The locking used to deal with mnt_count decrement provides barriers, 852b3e19d92SNick Piggin * so mnt_get_writers() below is safe. 853d3ef3d73Snpiggin@suse.de */ 854c6653a83SNick Piggin WARN_ON(mnt_get_writers(mnt)); 85583adc753SAl Viro fsnotify_vfsmount_delete(m); 85683adc753SAl Viro dput(m->mnt_root); 85783adc753SAl Viro free_vfsmnt(mnt); 8581da177e4SLinus Torvalds deactivate_super(sb); 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 861900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 8627b7b1aceSAl Viro { 863b3e19d92SNick Piggin put_again: 864f03c6599SAl Viro #ifdef CONFIG_SMP 865962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 866f7a99c5bSAl Viro if (likely(mnt->mnt_ns)) { 867f7a99c5bSAl Viro /* shouldn't be the last one */ 868aa9c0e07SAl Viro mnt_add_count(mnt, -1); 869962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 87099b7db7bSNick Piggin return; 871b3e19d92SNick Piggin } 872962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 873b3e19d92SNick Piggin 874962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 875aa9c0e07SAl Viro mnt_add_count(mnt, -1); 876b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 877962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 87899b7db7bSNick Piggin return; 87999b7db7bSNick Piggin } 880b3e19d92SNick Piggin #else 881aa9c0e07SAl Viro mnt_add_count(mnt, -1); 882b3e19d92SNick Piggin if (likely(mnt_get_count(mnt))) 883b3e19d92SNick Piggin return; 884962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 885f03c6599SAl Viro #endif 886863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 887863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 888863d684fSAl Viro mnt->mnt_pinned = 0; 889962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 890900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 891b3e19d92SNick Piggin goto put_again; 892b3e19d92SNick Piggin } 893962830dfSAndi Kleen 89439f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 895962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 896b3e19d92SNick Piggin mntfree(mnt); 897b3e19d92SNick Piggin } 898b3e19d92SNick Piggin 899b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 900b3e19d92SNick Piggin { 901b3e19d92SNick Piggin if (mnt) { 902863d684fSAl Viro struct mount *m = real_mount(mnt); 903b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 904863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 905863d684fSAl Viro m->mnt_expiry_mark = 0; 906863d684fSAl Viro mntput_no_expire(m); 907b3e19d92SNick Piggin } 908b3e19d92SNick Piggin } 909b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 910b3e19d92SNick Piggin 911b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 912b3e19d92SNick Piggin { 913b3e19d92SNick Piggin if (mnt) 91483adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 915b3e19d92SNick Piggin return mnt; 916b3e19d92SNick Piggin } 917b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 918b3e19d92SNick Piggin 9197b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 9207b7b1aceSAl Viro { 921962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 922863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 923962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 9247b7b1aceSAl Viro } 9257b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 9267b7b1aceSAl Viro 927863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 9287b7b1aceSAl Viro { 929863d684fSAl Viro struct mount *mnt = real_mount(m); 930962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 9317b7b1aceSAl Viro if (mnt->mnt_pinned) { 932863d684fSAl Viro mnt_add_count(mnt, 1); 9337b7b1aceSAl Viro mnt->mnt_pinned--; 9347b7b1aceSAl Viro } 935962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 9367b7b1aceSAl Viro } 9377b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 9381da177e4SLinus Torvalds 939b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 940b3b304a2SMiklos Szeredi { 941b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 942b3b304a2SMiklos Szeredi } 943b3b304a2SMiklos Szeredi 944b3b304a2SMiklos Szeredi /* 945b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 946b3b304a2SMiklos Szeredi * implement more complex mount option showing. 947b3b304a2SMiklos Szeredi * 948b3b304a2SMiklos Szeredi * See also save_mount_options(). 949b3b304a2SMiklos Szeredi */ 95034c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 951b3b304a2SMiklos Szeredi { 9522a32cebdSAl Viro const char *options; 9532a32cebdSAl Viro 9542a32cebdSAl Viro rcu_read_lock(); 95534c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 956b3b304a2SMiklos Szeredi 957b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 958b3b304a2SMiklos Szeredi seq_putc(m, ','); 959b3b304a2SMiklos Szeredi mangle(m, options); 960b3b304a2SMiklos Szeredi } 9612a32cebdSAl Viro rcu_read_unlock(); 962b3b304a2SMiklos Szeredi 963b3b304a2SMiklos Szeredi return 0; 964b3b304a2SMiklos Szeredi } 965b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 966b3b304a2SMiklos Szeredi 967b3b304a2SMiklos Szeredi /* 968b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 969b3b304a2SMiklos Szeredi * called from the fill_super() callback. 970b3b304a2SMiklos Szeredi * 971b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 972b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 973b3b304a2SMiklos Szeredi * remount fails. 974b3b304a2SMiklos Szeredi * 975b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 976b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 977b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 978b3b304a2SMiklos Szeredi * any more. 979b3b304a2SMiklos Szeredi */ 980b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 981b3b304a2SMiklos Szeredi { 9822a32cebdSAl Viro BUG_ON(sb->s_options); 9832a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 984b3b304a2SMiklos Szeredi } 985b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 986b3b304a2SMiklos Szeredi 9872a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 9882a32cebdSAl Viro { 9892a32cebdSAl Viro char *old = sb->s_options; 9902a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 9912a32cebdSAl Viro if (old) { 9922a32cebdSAl Viro synchronize_rcu(); 9932a32cebdSAl Viro kfree(old); 9942a32cebdSAl Viro } 9952a32cebdSAl Viro } 9962a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 9972a32cebdSAl Viro 998a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 9990226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 10001da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 10011da177e4SLinus Torvalds { 10026ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10031da177e4SLinus Torvalds 1004390c6843SRam Pai down_read(&namespace_sem); 1005a1a2c409SMiklos Szeredi return seq_list_start(&p->ns->list, *pos); 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 10091da177e4SLinus Torvalds { 10106ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 1011b0765fb8SPavel Emelianov 1012a1a2c409SMiklos Szeredi return seq_list_next(v, &p->ns->list, pos); 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 10161da177e4SLinus Torvalds { 1017390c6843SRam Pai up_read(&namespace_sem); 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10200226f492SAl Viro static int m_show(struct seq_file *m, void *v) 10219f5596afSAl Viro { 10226ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10231a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 10240226f492SAl Viro return p->show(m, &r->mnt); 10251da177e4SLinus Torvalds } 10261da177e4SLinus Torvalds 1027a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 10281da177e4SLinus Torvalds .start = m_start, 10291da177e4SLinus Torvalds .next = m_next, 10301da177e4SLinus Torvalds .stop = m_stop, 10310226f492SAl Viro .show = m_show, 1032b4629fe2SChuck Lever }; 1033a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1034b4629fe2SChuck Lever 10351da177e4SLinus Torvalds /** 10361da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 10371da177e4SLinus Torvalds * @mnt: root of mount tree 10381da177e4SLinus Torvalds * 10391da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 10401da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 10411da177e4SLinus Torvalds * busy. 10421da177e4SLinus Torvalds */ 1043909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 10441da177e4SLinus Torvalds { 1045909b0a88SAl Viro struct mount *mnt = real_mount(m); 104636341f64SRam Pai int actual_refs = 0; 104736341f64SRam Pai int minimum_refs = 0; 1048315fc83eSAl Viro struct mount *p; 1049909b0a88SAl Viro BUG_ON(!m); 10501da177e4SLinus Torvalds 1051b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1052962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1053909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 105483adc753SAl Viro actual_refs += mnt_get_count(p); 10551da177e4SLinus Torvalds minimum_refs += 2; 10561da177e4SLinus Torvalds } 1057962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds if (actual_refs > minimum_refs) 10601da177e4SLinus Torvalds return 0; 1061e3474a8eSIan Kent 1062e3474a8eSIan Kent return 1; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds /** 10681da177e4SLinus Torvalds * may_umount - check if a mount point is busy 10691da177e4SLinus Torvalds * @mnt: root of mount 10701da177e4SLinus Torvalds * 10711da177e4SLinus Torvalds * This is called to check if a mount point has any 10721da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 10731da177e4SLinus Torvalds * mount has sub mounts this will return busy 10741da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 10751da177e4SLinus Torvalds * 10761da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 10771da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 10781da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 10791da177e4SLinus Torvalds */ 10801da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 10811da177e4SLinus Torvalds { 1082e3474a8eSIan Kent int ret = 1; 10838ad08d8aSAl Viro down_read(&namespace_sem); 1084962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 10851ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1086e3474a8eSIan Kent ret = 0; 1087962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 10888ad08d8aSAl Viro up_read(&namespace_sem); 1089a05964f3SRam Pai return ret; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 10931da177e4SLinus Torvalds 1094b90fa9aeSRam Pai void release_mounts(struct list_head *head) 10951da177e4SLinus Torvalds { 1096d5e50f74SAl Viro struct mount *mnt; 109770fbcdf4SRam Pai while (!list_empty(head)) { 10981b8e5564SAl Viro mnt = list_first_entry(head, struct mount, mnt_hash); 10991b8e5564SAl Viro list_del_init(&mnt->mnt_hash); 1100676da58dSAl Viro if (mnt_has_parent(mnt)) { 110170fbcdf4SRam Pai struct dentry *dentry; 1102863d684fSAl Viro struct mount *m; 110399b7db7bSNick Piggin 1104962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1105a73324daSAl Viro dentry = mnt->mnt_mountpoint; 1106863d684fSAl Viro m = mnt->mnt_parent; 1107a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 11080714a533SAl Viro mnt->mnt_parent = mnt; 11097c4b93d8SAl Viro m->mnt_ghosts--; 1110962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 111170fbcdf4SRam Pai dput(dentry); 1112863d684fSAl Viro mntput(&m->mnt); 11131da177e4SLinus Torvalds } 1114d5e50f74SAl Viro mntput(&mnt->mnt); 111570fbcdf4SRam Pai } 111670fbcdf4SRam Pai } 111770fbcdf4SRam Pai 111899b7db7bSNick Piggin /* 111999b7db7bSNick Piggin * vfsmount lock must be held for write 112099b7db7bSNick Piggin * namespace_sem must be held for write 112199b7db7bSNick Piggin */ 1122761d5c38SAl Viro void umount_tree(struct mount *mnt, int propagate, struct list_head *kill) 112370fbcdf4SRam Pai { 11247b8a53fdSAl Viro LIST_HEAD(tmp_list); 1125315fc83eSAl Viro struct mount *p; 112670fbcdf4SRam Pai 1127909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) 11281b8e5564SAl Viro list_move(&p->mnt_hash, &tmp_list); 112970fbcdf4SRam Pai 1130a05964f3SRam Pai if (propagate) 11317b8a53fdSAl Viro propagate_umount(&tmp_list); 1132a05964f3SRam Pai 11331b8e5564SAl Viro list_for_each_entry(p, &tmp_list, mnt_hash) { 11346776db3dSAl Viro list_del_init(&p->mnt_expire); 11351a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1136143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 113763d37a84SAl Viro p->mnt_ns = NULL; 11386b41d536SAl Viro list_del_init(&p->mnt_child); 1139676da58dSAl Viro if (mnt_has_parent(p)) { 1140863d684fSAl Viro p->mnt_parent->mnt_ghosts++; 1141a73324daSAl Viro dentry_reset_mounted(p->mnt_mountpoint); 11427c4b93d8SAl Viro } 11430f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 11441da177e4SLinus Torvalds } 11457b8a53fdSAl Viro list_splice(&tmp_list, kill); 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 1148692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts); 1149c35038beSAl Viro 11501ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 11511da177e4SLinus Torvalds { 11521ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 11531da177e4SLinus Torvalds int retval; 115470fbcdf4SRam Pai LIST_HEAD(umount_list); 11551da177e4SLinus Torvalds 11561ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 11571da177e4SLinus Torvalds if (retval) 11581da177e4SLinus Torvalds return retval; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds /* 11611da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 11621da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 11631da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 11641da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 11651da177e4SLinus Torvalds */ 11661da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 11671ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 11681da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 11691da177e4SLinus Torvalds return -EINVAL; 11701da177e4SLinus Torvalds 1171b3e19d92SNick Piggin /* 1172b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1173b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1174b3e19d92SNick Piggin */ 1175962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 117683adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1177962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11781da177e4SLinus Torvalds return -EBUSY; 1179b3e19d92SNick Piggin } 1180962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 11811da177e4SLinus Torvalds 1182863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 11831da177e4SLinus Torvalds return -EAGAIN; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds /* 11871da177e4SLinus Torvalds * If we may have to abort operations to get out of this 11881da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 11891da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 11901da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 11911da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 11921da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 11931da177e4SLinus Torvalds * about for the moment. 11941da177e4SLinus Torvalds */ 11951da177e4SLinus Torvalds 119642faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 119742faad99SAl Viro sb->s_op->umount_begin(sb); 119842faad99SAl Viro } 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds /* 12011da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 12021da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 12031da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 12041da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 12051da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 12061da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 12071da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 12081da177e4SLinus Torvalds */ 12091ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 12101da177e4SLinus Torvalds /* 12111da177e4SLinus Torvalds * Special case for "unmounting" root ... 12121da177e4SLinus Torvalds * we just try to remount it readonly. 12131da177e4SLinus Torvalds */ 12141da177e4SLinus Torvalds down_write(&sb->s_umount); 12154aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 12161da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 12171da177e4SLinus Torvalds up_write(&sb->s_umount); 12181da177e4SLinus Torvalds return retval; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 1221390c6843SRam Pai down_write(&namespace_sem); 1222962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 12235addc5ddSAl Viro event++; 12241da177e4SLinus Torvalds 1225c35038beSAl Viro if (!(flags & MNT_DETACH)) 12261ab59738SAl Viro shrink_submounts(mnt, &umount_list); 1227c35038beSAl Viro 12281da177e4SLinus Torvalds retval = -EBUSY; 1229a05964f3SRam Pai if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) { 12301a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 12311ab59738SAl Viro umount_tree(mnt, 1, &umount_list); 12321da177e4SLinus Torvalds retval = 0; 12331da177e4SLinus Torvalds } 1234962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1235390c6843SRam Pai up_write(&namespace_sem); 123670fbcdf4SRam Pai release_mounts(&umount_list); 12371da177e4SLinus Torvalds return retval; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds /* 12411da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 12421da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 12431da177e4SLinus Torvalds * 12441da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 12451da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 12461da177e4SLinus Torvalds */ 12471da177e4SLinus Torvalds 1248bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 12491da177e4SLinus Torvalds { 12502d8f3038SAl Viro struct path path; 1251900148dcSAl Viro struct mount *mnt; 12521da177e4SLinus Torvalds int retval; 1253db1f05bbSMiklos Szeredi int lookup_flags = 0; 12541da177e4SLinus Torvalds 1255db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1256db1f05bbSMiklos Szeredi return -EINVAL; 1257db1f05bbSMiklos Szeredi 1258db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1259db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1260db1f05bbSMiklos Szeredi 1261db1f05bbSMiklos Szeredi retval = user_path_at(AT_FDCWD, name, lookup_flags, &path); 12621da177e4SLinus Torvalds if (retval) 12631da177e4SLinus Torvalds goto out; 1264900148dcSAl Viro mnt = real_mount(path.mnt); 12651da177e4SLinus Torvalds retval = -EINVAL; 12662d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 12671da177e4SLinus Torvalds goto dput_and_out; 1268143c8c91SAl Viro if (!check_mnt(mnt)) 12691da177e4SLinus Torvalds goto dput_and_out; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds retval = -EPERM; 12720c55cfc4SEric W. Biederman if (!ns_capable(mnt->mnt_ns->user_ns, CAP_SYS_ADMIN)) 12731da177e4SLinus Torvalds goto dput_and_out; 12741da177e4SLinus Torvalds 1275900148dcSAl Viro retval = do_umount(mnt, flags); 12761da177e4SLinus Torvalds dput_and_out: 1277429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 12782d8f3038SAl Viro dput(path.dentry); 1279900148dcSAl Viro mntput_no_expire(mnt); 12801da177e4SLinus Torvalds out: 12811da177e4SLinus Torvalds return retval; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds /* 12871da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 12881da177e4SLinus Torvalds */ 1289bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 12901da177e4SLinus Torvalds { 12911da177e4SLinus Torvalds return sys_umount(name, 0); 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds #endif 12951da177e4SLinus Torvalds 12962d92ab3cSAl Viro static int mount_is_safe(struct path *path) 12971da177e4SLinus Torvalds { 12980c55cfc4SEric W. Biederman if (ns_capable(real_mount(path->mnt)->mnt_ns->user_ns, CAP_SYS_ADMIN)) 12991da177e4SLinus Torvalds return 0; 13001da177e4SLinus Torvalds return -EPERM; 13011da177e4SLinus Torvalds #ifdef notyet 13022d92ab3cSAl Viro if (S_ISLNK(path->dentry->d_inode->i_mode)) 13031da177e4SLinus Torvalds return -EPERM; 13042d92ab3cSAl Viro if (path->dentry->d_inode->i_mode & S_ISVTX) { 1305da9592edSDavid Howells if (current_uid() != path->dentry->d_inode->i_uid) 13061da177e4SLinus Torvalds return -EPERM; 13071da177e4SLinus Torvalds } 13082d92ab3cSAl Viro if (inode_permission(path->dentry->d_inode, MAY_WRITE)) 13091da177e4SLinus Torvalds return -EPERM; 13101da177e4SLinus Torvalds return 0; 13111da177e4SLinus Torvalds #endif 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13148823c079SEric W. Biederman static bool mnt_ns_loop(struct path *path) 13158823c079SEric W. Biederman { 13168823c079SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 13178823c079SEric W. Biederman * mount namespace loop? 13188823c079SEric W. Biederman */ 13198823c079SEric W. Biederman struct inode *inode = path->dentry->d_inode; 13208823c079SEric W. Biederman struct proc_inode *ei; 13218823c079SEric W. Biederman struct mnt_namespace *mnt_ns; 13228823c079SEric W. Biederman 13238823c079SEric W. Biederman if (!proc_ns_inode(inode)) 13248823c079SEric W. Biederman return false; 13258823c079SEric W. Biederman 13268823c079SEric W. Biederman ei = PROC_I(inode); 13278823c079SEric W. Biederman if (ei->ns_ops != &mntns_operations) 13288823c079SEric W. Biederman return false; 13298823c079SEric W. Biederman 13308823c079SEric W. Biederman mnt_ns = ei->ns; 13318823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 13328823c079SEric W. Biederman } 13338823c079SEric W. Biederman 133487129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 133536341f64SRam Pai int flag) 13361da177e4SLinus Torvalds { 1337a73324daSAl Viro struct mount *res, *p, *q, *r; 13381a390689SAl Viro struct path path; 13391da177e4SLinus Torvalds 1340fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt)) 1341be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 13429676f0c6SRam Pai 134336341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1344be34d1a3SDavid Howells if (IS_ERR(q)) 1345be34d1a3SDavid Howells return q; 1346be34d1a3SDavid Howells 1347a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds p = mnt; 13506b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1351315fc83eSAl Viro struct mount *s; 13527ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 13531da177e4SLinus Torvalds continue; 13541da177e4SLinus Torvalds 1355909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 1356fc7be130SAl Viro if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) { 13579676f0c6SRam Pai s = skip_mnt_tree(s); 13589676f0c6SRam Pai continue; 13599676f0c6SRam Pai } 13600714a533SAl Viro while (p != s->mnt_parent) { 13610714a533SAl Viro p = p->mnt_parent; 13620714a533SAl Viro q = q->mnt_parent; 13631da177e4SLinus Torvalds } 136487129cc0SAl Viro p = s; 1365cb338d06SAl Viro path.mnt = &q->mnt; 1366a73324daSAl Viro path.dentry = p->mnt_mountpoint; 136787129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1368be34d1a3SDavid Howells if (IS_ERR(q)) 1369be34d1a3SDavid Howells goto out; 1370962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 13711a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 1372cb338d06SAl Viro attach_mnt(q, &path); 1373962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds return res; 1377be34d1a3SDavid Howells out: 13781da177e4SLinus Torvalds if (res) { 137970fbcdf4SRam Pai LIST_HEAD(umount_list); 1380962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1381761d5c38SAl Viro umount_tree(res, 0, &umount_list); 1382962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 138370fbcdf4SRam Pai release_mounts(&umount_list); 13841da177e4SLinus Torvalds } 1385be34d1a3SDavid Howells return q; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 1388be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1389be34d1a3SDavid Howells 1390589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 13918aec0809SAl Viro { 1392cb338d06SAl Viro struct mount *tree; 13931a60a280SAl Viro down_write(&namespace_sem); 139487129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 139587129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 13961a60a280SAl Viro up_write(&namespace_sem); 1397be34d1a3SDavid Howells if (IS_ERR(tree)) 1398be34d1a3SDavid Howells return NULL; 1399be34d1a3SDavid Howells return &tree->mnt; 14008aec0809SAl Viro } 14018aec0809SAl Viro 14028aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 14038aec0809SAl Viro { 14048aec0809SAl Viro LIST_HEAD(umount_list); 14051a60a280SAl Viro down_write(&namespace_sem); 1406962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1407761d5c38SAl Viro umount_tree(real_mount(mnt), 0, &umount_list); 1408962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 14091a60a280SAl Viro up_write(&namespace_sem); 14108aec0809SAl Viro release_mounts(&umount_list); 14118aec0809SAl Viro } 14128aec0809SAl Viro 14131f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 14141f707137SAl Viro struct vfsmount *root) 14151f707137SAl Viro { 14161a4eeaf2SAl Viro struct mount *mnt; 14171f707137SAl Viro int res = f(root, arg); 14181f707137SAl Viro if (res) 14191f707137SAl Viro return res; 14201a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 14211a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 14221f707137SAl Viro if (res) 14231f707137SAl Viro return res; 14241f707137SAl Viro } 14251f707137SAl Viro return 0; 14261f707137SAl Viro } 14271f707137SAl Viro 14284b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1429719f5d7fSMiklos Szeredi { 1430315fc83eSAl Viro struct mount *p; 1431719f5d7fSMiklos Szeredi 1432909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1433fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 14344b8b21f4SAl Viro mnt_release_group_id(p); 1435719f5d7fSMiklos Szeredi } 1436719f5d7fSMiklos Szeredi } 1437719f5d7fSMiklos Szeredi 14384b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1439719f5d7fSMiklos Szeredi { 1440315fc83eSAl Viro struct mount *p; 1441719f5d7fSMiklos Szeredi 1442909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1443fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 14444b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1445719f5d7fSMiklos Szeredi if (err) { 14464b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1447719f5d7fSMiklos Szeredi return err; 1448719f5d7fSMiklos Szeredi } 1449719f5d7fSMiklos Szeredi } 1450719f5d7fSMiklos Szeredi } 1451719f5d7fSMiklos Szeredi 1452719f5d7fSMiklos Szeredi return 0; 1453719f5d7fSMiklos Szeredi } 1454719f5d7fSMiklos Szeredi 1455b90fa9aeSRam Pai /* 1456b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1457b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 145821444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 145921444403SRam Pai * store the parent mount and mountpoint dentry. 146021444403SRam Pai * (done when source_mnt is moved) 1461b90fa9aeSRam Pai * 1462b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1463b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 14649676f0c6SRam Pai * --------------------------------------------------------------------------- 1465b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 14669676f0c6SRam Pai * |************************************************************************** 14679676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 14689676f0c6SRam Pai * | dest | | | | | 14699676f0c6SRam Pai * | | | | | | | 14709676f0c6SRam Pai * | v | | | | | 14719676f0c6SRam Pai * |************************************************************************** 14729676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 14735afe0022SRam Pai * | | | | | | 14749676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 14759676f0c6SRam Pai * *************************************************************************** 1476b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1477b90fa9aeSRam Pai * destination mount. 1478b90fa9aeSRam Pai * 1479b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1480b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1481b90fa9aeSRam Pai * the peer group of the source mount. 1482b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1483b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1484b90fa9aeSRam Pai * mount. 14855afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 14865afe0022SRam Pai * of the destination mount and the cloned mount is made slave 14875afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 14885afe0022SRam Pai * is marked as 'shared and slave'. 14895afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 14905afe0022SRam Pai * source mount. 14915afe0022SRam Pai * 14929676f0c6SRam Pai * --------------------------------------------------------------------------- 149321444403SRam Pai * | MOVE MOUNT OPERATION | 14949676f0c6SRam Pai * |************************************************************************** 14959676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 14969676f0c6SRam Pai * | dest | | | | | 14979676f0c6SRam Pai * | | | | | | | 14989676f0c6SRam Pai * | v | | | | | 14999676f0c6SRam Pai * |************************************************************************** 15009676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 15015afe0022SRam Pai * | | | | | | 15029676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 15039676f0c6SRam Pai * *************************************************************************** 15045afe0022SRam Pai * 15055afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 15065afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 150721444403SRam Pai * (+*) the mount is moved to the destination. 15085afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 15095afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 15105afe0022SRam Pai * the mount is marked as 'shared and slave'. 15115afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1512b90fa9aeSRam Pai * 1513b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1514b90fa9aeSRam Pai * applied to each mount in the tree. 1515b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1516b90fa9aeSRam Pai * in allocations. 1517b90fa9aeSRam Pai */ 15180fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 15191a390689SAl Viro struct path *path, struct path *parent_path) 1520b90fa9aeSRam Pai { 1521b90fa9aeSRam Pai LIST_HEAD(tree_list); 1522a8d56d8eSAl Viro struct mount *dest_mnt = real_mount(path->mnt); 15231a390689SAl Viro struct dentry *dest_dentry = path->dentry; 1524315fc83eSAl Viro struct mount *child, *p; 1525719f5d7fSMiklos Szeredi int err; 1526b90fa9aeSRam Pai 1527fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 15280fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1529719f5d7fSMiklos Szeredi if (err) 1530719f5d7fSMiklos Szeredi goto out; 1531719f5d7fSMiklos Szeredi } 1532a8d56d8eSAl Viro err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list); 1533719f5d7fSMiklos Szeredi if (err) 1534719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1535b90fa9aeSRam Pai 1536962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1537df1a1ad2SAl Viro 1538fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 1539909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 15400f0afb1dSAl Viro set_mnt_shared(p); 1541b90fa9aeSRam Pai } 15421a390689SAl Viro if (parent_path) { 15430fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 15440fb54e50SAl Viro attach_mnt(source_mnt, path); 1545143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 154621444403SRam Pai } else { 154714cf1fa8SAl Viro mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt); 15480fb54e50SAl Viro commit_tree(source_mnt); 154921444403SRam Pai } 1550b90fa9aeSRam Pai 15511b8e5564SAl Viro list_for_each_entry_safe(child, p, &tree_list, mnt_hash) { 15521b8e5564SAl Viro list_del_init(&child->mnt_hash); 15534b2619a5SAl Viro commit_tree(child); 1554b90fa9aeSRam Pai } 1555962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 155699b7db7bSNick Piggin 1557b90fa9aeSRam Pai return 0; 1558719f5d7fSMiklos Szeredi 1559719f5d7fSMiklos Szeredi out_cleanup_ids: 1560fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) 15610fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1562719f5d7fSMiklos Szeredi out: 1563719f5d7fSMiklos Szeredi return err; 1564b90fa9aeSRam Pai } 1565b90fa9aeSRam Pai 1566b12cea91SAl Viro static int lock_mount(struct path *path) 1567b12cea91SAl Viro { 1568b12cea91SAl Viro struct vfsmount *mnt; 1569b12cea91SAl Viro retry: 1570b12cea91SAl Viro mutex_lock(&path->dentry->d_inode->i_mutex); 1571b12cea91SAl Viro if (unlikely(cant_mount(path->dentry))) { 1572b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1573b12cea91SAl Viro return -ENOENT; 1574b12cea91SAl Viro } 1575b12cea91SAl Viro down_write(&namespace_sem); 1576b12cea91SAl Viro mnt = lookup_mnt(path); 1577b12cea91SAl Viro if (likely(!mnt)) 1578b12cea91SAl Viro return 0; 1579b12cea91SAl Viro up_write(&namespace_sem); 1580b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1581b12cea91SAl Viro path_put(path); 1582b12cea91SAl Viro path->mnt = mnt; 1583b12cea91SAl Viro path->dentry = dget(mnt->mnt_root); 1584b12cea91SAl Viro goto retry; 1585b12cea91SAl Viro } 1586b12cea91SAl Viro 1587b12cea91SAl Viro static void unlock_mount(struct path *path) 1588b12cea91SAl Viro { 1589b12cea91SAl Viro up_write(&namespace_sem); 1590b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1591b12cea91SAl Viro } 1592b12cea91SAl Viro 159395bc5f25SAl Viro static int graft_tree(struct mount *mnt, struct path *path) 15941da177e4SLinus Torvalds { 159595bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 15961da177e4SLinus Torvalds return -EINVAL; 15971da177e4SLinus Torvalds 15988c3ee42eSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 159995bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 16001da177e4SLinus Torvalds return -ENOTDIR; 16011da177e4SLinus Torvalds 1602b12cea91SAl Viro if (d_unlinked(path->dentry)) 1603b12cea91SAl Viro return -ENOENT; 16041da177e4SLinus Torvalds 160595bc5f25SAl Viro return attach_recursive_mnt(mnt, path, NULL); 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds /* 16097a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 16107a2e8a8fSValerie Aurora */ 16117a2e8a8fSValerie Aurora 16127a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 16137a2e8a8fSValerie Aurora { 16147c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 16157a2e8a8fSValerie Aurora 16167a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 16177a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 16187a2e8a8fSValerie Aurora return 0; 16197a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 16207a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 16217a2e8a8fSValerie Aurora return 0; 16227a2e8a8fSValerie Aurora return type; 16237a2e8a8fSValerie Aurora } 16247a2e8a8fSValerie Aurora 16257a2e8a8fSValerie Aurora /* 162607b20889SRam Pai * recursively change the type of the mountpoint. 162707b20889SRam Pai */ 16280a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 162907b20889SRam Pai { 1630315fc83eSAl Viro struct mount *m; 16314b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 163207b20889SRam Pai int recurse = flag & MS_REC; 16337a2e8a8fSValerie Aurora int type; 1634719f5d7fSMiklos Szeredi int err = 0; 163507b20889SRam Pai 16360c55cfc4SEric W. Biederman if (!ns_capable(mnt->mnt_ns->user_ns, CAP_SYS_ADMIN)) 1637ee6f9582SMiklos Szeredi return -EPERM; 1638ee6f9582SMiklos Szeredi 16392d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 164007b20889SRam Pai return -EINVAL; 164107b20889SRam Pai 16427a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 16437a2e8a8fSValerie Aurora if (!type) 16447a2e8a8fSValerie Aurora return -EINVAL; 16457a2e8a8fSValerie Aurora 164607b20889SRam Pai down_write(&namespace_sem); 1647719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1648719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1649719f5d7fSMiklos Szeredi if (err) 1650719f5d7fSMiklos Szeredi goto out_unlock; 1651719f5d7fSMiklos Szeredi } 1652719f5d7fSMiklos Szeredi 1653962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1654909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 16550f0afb1dSAl Viro change_mnt_propagation(m, type); 1656962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 1657719f5d7fSMiklos Szeredi 1658719f5d7fSMiklos Szeredi out_unlock: 165907b20889SRam Pai up_write(&namespace_sem); 1660719f5d7fSMiklos Szeredi return err; 166107b20889SRam Pai } 166207b20889SRam Pai 166307b20889SRam Pai /* 16641da177e4SLinus Torvalds * do loopback mount. 16651da177e4SLinus Torvalds */ 1666808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 16672dafe1c4SEric Sandeen int recurse) 16681da177e4SLinus Torvalds { 1669b12cea91SAl Viro LIST_HEAD(umount_list); 16702d92ab3cSAl Viro struct path old_path; 167187129cc0SAl Viro struct mount *mnt = NULL, *old; 16722d92ab3cSAl Viro int err = mount_is_safe(path); 16731da177e4SLinus Torvalds if (err) 16741da177e4SLinus Torvalds return err; 16751da177e4SLinus Torvalds if (!old_name || !*old_name) 16761da177e4SLinus Torvalds return -EINVAL; 1677815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 16781da177e4SLinus Torvalds if (err) 16791da177e4SLinus Torvalds return err; 16801da177e4SLinus Torvalds 16818823c079SEric W. Biederman err = -EINVAL; 16828823c079SEric W. Biederman if (mnt_ns_loop(&old_path)) 16838823c079SEric W. Biederman goto out; 16848823c079SEric W. Biederman 1685b12cea91SAl Viro err = lock_mount(path); 1686b12cea91SAl Viro if (err) 16879676f0c6SRam Pai goto out; 16889676f0c6SRam Pai 168987129cc0SAl Viro old = real_mount(old_path.mnt); 169087129cc0SAl Viro 1691b12cea91SAl Viro err = -EINVAL; 1692fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1693b12cea91SAl Viro goto out2; 1694b12cea91SAl Viro 1695143c8c91SAl Viro if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old)) 1696b12cea91SAl Viro goto out2; 1697ccd48bc7SAl Viro 16981da177e4SLinus Torvalds if (recurse) 169987129cc0SAl Viro mnt = copy_tree(old, old_path.dentry, 0); 17001da177e4SLinus Torvalds else 170187129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 17021da177e4SLinus Torvalds 1703be34d1a3SDavid Howells if (IS_ERR(mnt)) { 1704be34d1a3SDavid Howells err = PTR_ERR(mnt); 1705be34d1a3SDavid Howells goto out; 1706be34d1a3SDavid Howells } 1707ccd48bc7SAl Viro 170895bc5f25SAl Viro err = graft_tree(mnt, path); 17091da177e4SLinus Torvalds if (err) { 1710962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1711761d5c38SAl Viro umount_tree(mnt, 0, &umount_list); 1712962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 17135b83d2c5SRam Pai } 1714b12cea91SAl Viro out2: 1715b12cea91SAl Viro unlock_mount(path); 1716b12cea91SAl Viro release_mounts(&umount_list); 1717ccd48bc7SAl Viro out: 17182d92ab3cSAl Viro path_put(&old_path); 17191da177e4SLinus Torvalds return err; 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 17222e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 17232e4b7fcdSDave Hansen { 17242e4b7fcdSDave Hansen int error = 0; 17252e4b7fcdSDave Hansen int readonly_request = 0; 17262e4b7fcdSDave Hansen 17272e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 17282e4b7fcdSDave Hansen readonly_request = 1; 17292e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 17302e4b7fcdSDave Hansen return 0; 17312e4b7fcdSDave Hansen 17322e4b7fcdSDave Hansen if (readonly_request) 173383adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 17342e4b7fcdSDave Hansen else 173583adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 17362e4b7fcdSDave Hansen return error; 17372e4b7fcdSDave Hansen } 17382e4b7fcdSDave Hansen 17391da177e4SLinus Torvalds /* 17401da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 17411da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 17421da177e4SLinus Torvalds * on it - tough luck. 17431da177e4SLinus Torvalds */ 17440a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 17451da177e4SLinus Torvalds void *data) 17461da177e4SLinus Torvalds { 17471da177e4SLinus Torvalds int err; 17482d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1749143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17521da177e4SLinus Torvalds return -EPERM; 17531da177e4SLinus Torvalds 1754143c8c91SAl Viro if (!check_mnt(mnt)) 17551da177e4SLinus Torvalds return -EINVAL; 17561da177e4SLinus Torvalds 17572d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 17581da177e4SLinus Torvalds return -EINVAL; 17591da177e4SLinus Torvalds 1760ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1761ff36fe2cSEric Paris if (err) 1762ff36fe2cSEric Paris return err; 1763ff36fe2cSEric Paris 17641da177e4SLinus Torvalds down_write(&sb->s_umount); 17652e4b7fcdSDave Hansen if (flags & MS_BIND) 17662d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 17674aa98cf7SAl Viro else 17681da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 17697b43a79fSAl Viro if (!err) { 1770962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1771143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1772143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1773962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 17747b43a79fSAl Viro } 17751da177e4SLinus Torvalds up_write(&sb->s_umount); 17760e55a7ccSDan Williams if (!err) { 1777962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 1778143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1779962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 17800e55a7ccSDan Williams } 17811da177e4SLinus Torvalds return err; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 1784cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 17859676f0c6SRam Pai { 1786315fc83eSAl Viro struct mount *p; 1787909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1788fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 17899676f0c6SRam Pai return 1; 17909676f0c6SRam Pai } 17919676f0c6SRam Pai return 0; 17929676f0c6SRam Pai } 17939676f0c6SRam Pai 1794808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 17951da177e4SLinus Torvalds { 17962d92ab3cSAl Viro struct path old_path, parent_path; 1797676da58dSAl Viro struct mount *p; 17980fb54e50SAl Viro struct mount *old; 17991da177e4SLinus Torvalds int err = 0; 18000c55cfc4SEric W. Biederman if (!ns_capable(real_mount(path->mnt)->mnt_ns->user_ns, CAP_SYS_ADMIN)) 18011da177e4SLinus Torvalds return -EPERM; 18021da177e4SLinus Torvalds if (!old_name || !*old_name) 18031da177e4SLinus Torvalds return -EINVAL; 18042d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 18051da177e4SLinus Torvalds if (err) 18061da177e4SLinus Torvalds return err; 18071da177e4SLinus Torvalds 1808b12cea91SAl Viro err = lock_mount(path); 1809cc53ce53SDavid Howells if (err < 0) 1810cc53ce53SDavid Howells goto out; 1811cc53ce53SDavid Howells 1812143c8c91SAl Viro old = real_mount(old_path.mnt); 1813fc7be130SAl Viro p = real_mount(path->mnt); 1814143c8c91SAl Viro 18151da177e4SLinus Torvalds err = -EINVAL; 1816fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 18171da177e4SLinus Torvalds goto out1; 18181da177e4SLinus Torvalds 1819f3da392eSAlexey Dobriyan if (d_unlinked(path->dentry)) 182021444403SRam Pai goto out1; 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds err = -EINVAL; 18232d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 182421444403SRam Pai goto out1; 18251da177e4SLinus Torvalds 1826676da58dSAl Viro if (!mnt_has_parent(old)) 182721444403SRam Pai goto out1; 18281da177e4SLinus Torvalds 18292d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 18302d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 183121444403SRam Pai goto out1; 183221444403SRam Pai /* 183321444403SRam Pai * Don't move a mount residing in a shared parent. 183421444403SRam Pai */ 1835fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 183621444403SRam Pai goto out1; 18379676f0c6SRam Pai /* 18389676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 18399676f0c6SRam Pai * mount which is shared. 18409676f0c6SRam Pai */ 1841fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 18429676f0c6SRam Pai goto out1; 18431da177e4SLinus Torvalds err = -ELOOP; 1844fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 1845676da58dSAl Viro if (p == old) 184621444403SRam Pai goto out1; 18471da177e4SLinus Torvalds 18480fb54e50SAl Viro err = attach_recursive_mnt(old, path, &parent_path); 18494ac91378SJan Blunck if (err) 185021444403SRam Pai goto out1; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 18531da177e4SLinus Torvalds * automatically */ 18546776db3dSAl Viro list_del_init(&old->mnt_expire); 18551da177e4SLinus Torvalds out1: 1856b12cea91SAl Viro unlock_mount(path); 18571da177e4SLinus Torvalds out: 18581da177e4SLinus Torvalds if (!err) 18591a390689SAl Viro path_put(&parent_path); 18602d92ab3cSAl Viro path_put(&old_path); 18611da177e4SLinus Torvalds return err; 18621da177e4SLinus Torvalds } 18631da177e4SLinus Torvalds 18649d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 18659d412a43SAl Viro { 18669d412a43SAl Viro int err; 18679d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 18689d412a43SAl Viro if (subtype) { 18699d412a43SAl Viro subtype++; 18709d412a43SAl Viro err = -EINVAL; 18719d412a43SAl Viro if (!subtype[0]) 18729d412a43SAl Viro goto err; 18739d412a43SAl Viro } else 18749d412a43SAl Viro subtype = ""; 18759d412a43SAl Viro 18769d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 18779d412a43SAl Viro err = -ENOMEM; 18789d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 18799d412a43SAl Viro goto err; 18809d412a43SAl Viro return mnt; 18819d412a43SAl Viro 18829d412a43SAl Viro err: 18839d412a43SAl Viro mntput(mnt); 18849d412a43SAl Viro return ERR_PTR(err); 18859d412a43SAl Viro } 18869d412a43SAl Viro 18879d412a43SAl Viro /* 18889d412a43SAl Viro * add a mount into a namespace's mount tree 18899d412a43SAl Viro */ 189095bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 18919d412a43SAl Viro { 18929d412a43SAl Viro int err; 18939d412a43SAl Viro 18949d412a43SAl Viro mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL); 18959d412a43SAl Viro 1896b12cea91SAl Viro err = lock_mount(path); 1897b12cea91SAl Viro if (err) 1898b12cea91SAl Viro return err; 18999d412a43SAl Viro 19009d412a43SAl Viro err = -EINVAL; 1901156cacb1SAl Viro if (unlikely(!check_mnt(real_mount(path->mnt)))) { 1902156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 1903156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 19049d412a43SAl Viro goto unlock; 1905156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 1906156cacb1SAl Viro if (!real_mount(path->mnt)->mnt_ns) 1907156cacb1SAl Viro goto unlock; 1908156cacb1SAl Viro } 19099d412a43SAl Viro 19109d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 19119d412a43SAl Viro err = -EBUSY; 191295bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 19139d412a43SAl Viro path->mnt->mnt_root == path->dentry) 19149d412a43SAl Viro goto unlock; 19159d412a43SAl Viro 19169d412a43SAl Viro err = -EINVAL; 191795bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 19189d412a43SAl Viro goto unlock; 19199d412a43SAl Viro 192095bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 19219d412a43SAl Viro err = graft_tree(newmnt, path); 19229d412a43SAl Viro 19239d412a43SAl Viro unlock: 1924b12cea91SAl Viro unlock_mount(path); 19259d412a43SAl Viro return err; 19269d412a43SAl Viro } 1927b1e75df4SAl Viro 19281da177e4SLinus Torvalds /* 19291da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 19301da177e4SLinus Torvalds * namespace's tree 19311da177e4SLinus Torvalds */ 19320c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags, 1933808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 19341da177e4SLinus Torvalds { 19350c55cfc4SEric W. Biederman struct file_system_type *type; 19360c55cfc4SEric W. Biederman struct user_namespace *user_ns; 19371da177e4SLinus Torvalds struct vfsmount *mnt; 193815f9a3f3SAl Viro int err; 19391da177e4SLinus Torvalds 19400c55cfc4SEric W. Biederman if (!fstype) 19411da177e4SLinus Torvalds return -EINVAL; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds /* we need capabilities... */ 19440c55cfc4SEric W. Biederman user_ns = real_mount(path->mnt)->mnt_ns->user_ns; 19450c55cfc4SEric W. Biederman if (!ns_capable(user_ns, CAP_SYS_ADMIN)) 19461da177e4SLinus Torvalds return -EPERM; 19471da177e4SLinus Torvalds 19480c55cfc4SEric W. Biederman type = get_fs_type(fstype); 19490c55cfc4SEric W. Biederman if (!type) 19500c55cfc4SEric W. Biederman return -ENODEV; 19510c55cfc4SEric W. Biederman 19520c55cfc4SEric W. Biederman if (user_ns != &init_user_ns) { 19530c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_MOUNT)) { 19540c55cfc4SEric W. Biederman put_filesystem(type); 19550c55cfc4SEric W. Biederman return -EPERM; 19560c55cfc4SEric W. Biederman } 19570c55cfc4SEric W. Biederman /* Only in special cases allow devices from mounts 19580c55cfc4SEric W. Biederman * created outside the initial user namespace. 19590c55cfc4SEric W. Biederman */ 19600c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) { 19610c55cfc4SEric W. Biederman flags |= MS_NODEV; 19620c55cfc4SEric W. Biederman mnt_flags |= MNT_NODEV; 19630c55cfc4SEric W. Biederman } 19640c55cfc4SEric W. Biederman } 19650c55cfc4SEric W. Biederman 19660c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, flags, name, data); 19670c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 19680c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 19690c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 19700c55cfc4SEric W. Biederman 19710c55cfc4SEric W. Biederman put_filesystem(type); 19721da177e4SLinus Torvalds if (IS_ERR(mnt)) 19731da177e4SLinus Torvalds return PTR_ERR(mnt); 19741da177e4SLinus Torvalds 197595bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 197615f9a3f3SAl Viro if (err) 197715f9a3f3SAl Viro mntput(mnt); 197815f9a3f3SAl Viro return err; 19791da177e4SLinus Torvalds } 19801da177e4SLinus Torvalds 198119a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 198219a167afSAl Viro { 19836776db3dSAl Viro struct mount *mnt = real_mount(m); 198419a167afSAl Viro int err; 198519a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 198619a167afSAl Viro * expired before we get a chance to add it 198719a167afSAl Viro */ 19886776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 198919a167afSAl Viro 199019a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 199119a167afSAl Viro m->mnt_root == path->dentry) { 1992b1e75df4SAl Viro err = -ELOOP; 1993b1e75df4SAl Viro goto fail; 199419a167afSAl Viro } 199519a167afSAl Viro 199695bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 1997b1e75df4SAl Viro if (!err) 1998b1e75df4SAl Viro return 0; 1999b1e75df4SAl Viro fail: 2000b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 20016776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 2002b1e75df4SAl Viro down_write(&namespace_sem); 2003962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 20046776db3dSAl Viro list_del_init(&mnt->mnt_expire); 2005962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2006b1e75df4SAl Viro up_write(&namespace_sem); 200719a167afSAl Viro } 2008b1e75df4SAl Viro mntput(m); 2009b1e75df4SAl Viro mntput(m); 201019a167afSAl Viro return err; 201119a167afSAl Viro } 201219a167afSAl Viro 2013ea5b778aSDavid Howells /** 2014ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2015ea5b778aSDavid Howells * @mnt: The mount to list. 2016ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2017ea5b778aSDavid Howells */ 2018ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2019ea5b778aSDavid Howells { 2020ea5b778aSDavid Howells down_write(&namespace_sem); 2021962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2022ea5b778aSDavid Howells 20236776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2024ea5b778aSDavid Howells 2025962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2026ea5b778aSDavid Howells up_write(&namespace_sem); 2027ea5b778aSDavid Howells } 2028ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2029ea5b778aSDavid Howells 2030ea5b778aSDavid Howells /* 20311da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 20321da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 20331da177e4SLinus Torvalds * here 20341da177e4SLinus Torvalds */ 20351da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 20361da177e4SLinus Torvalds { 2037761d5c38SAl Viro struct mount *mnt, *next; 20381da177e4SLinus Torvalds LIST_HEAD(graveyard); 2039bcc5c7d2SAl Viro LIST_HEAD(umounts); 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds if (list_empty(mounts)) 20421da177e4SLinus Torvalds return; 20431da177e4SLinus Torvalds 2044bcc5c7d2SAl Viro down_write(&namespace_sem); 2045962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 20481da177e4SLinus Torvalds * following criteria: 20491da177e4SLinus Torvalds * - only referenced by its parent vfsmount 20501da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 20511da177e4SLinus Torvalds * cleared by mntput()) 20521da177e4SLinus Torvalds */ 20536776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2054863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 20551ab59738SAl Viro propagate_mount_busy(mnt, 1)) 20561da177e4SLinus Torvalds continue; 20576776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 20581da177e4SLinus Torvalds } 2059bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 20606776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2061143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2062bcc5c7d2SAl Viro umount_tree(mnt, 1, &umounts); 2063bcc5c7d2SAl Viro } 2064962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2065bcc5c7d2SAl Viro up_write(&namespace_sem); 2066bcc5c7d2SAl Viro 2067bcc5c7d2SAl Viro release_mounts(&umounts); 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds /* 20735528f911STrond Myklebust * Ripoff of 'select_parent()' 20745528f911STrond Myklebust * 20755528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 20765528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 20775528f911STrond Myklebust */ 2078692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 20795528f911STrond Myklebust { 2080692afc31SAl Viro struct mount *this_parent = parent; 20815528f911STrond Myklebust struct list_head *next; 20825528f911STrond Myklebust int found = 0; 20835528f911STrond Myklebust 20845528f911STrond Myklebust repeat: 20856b41d536SAl Viro next = this_parent->mnt_mounts.next; 20865528f911STrond Myklebust resume: 20876b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 20885528f911STrond Myklebust struct list_head *tmp = next; 20896b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 20905528f911STrond Myklebust 20915528f911STrond Myklebust next = tmp->next; 2092692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 20935528f911STrond Myklebust continue; 20945528f911STrond Myklebust /* 20955528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 20965528f911STrond Myklebust */ 20976b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 20985528f911STrond Myklebust this_parent = mnt; 20995528f911STrond Myklebust goto repeat; 21005528f911STrond Myklebust } 21015528f911STrond Myklebust 21021ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 21036776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 21045528f911STrond Myklebust found++; 21055528f911STrond Myklebust } 21065528f911STrond Myklebust } 21075528f911STrond Myklebust /* 21085528f911STrond Myklebust * All done at this level ... ascend and resume the search 21095528f911STrond Myklebust */ 21105528f911STrond Myklebust if (this_parent != parent) { 21116b41d536SAl Viro next = this_parent->mnt_child.next; 21120714a533SAl Viro this_parent = this_parent->mnt_parent; 21135528f911STrond Myklebust goto resume; 21145528f911STrond Myklebust } 21155528f911STrond Myklebust return found; 21165528f911STrond Myklebust } 21175528f911STrond Myklebust 21185528f911STrond Myklebust /* 21195528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 21205528f911STrond Myklebust * submounts of a specific parent mountpoint 212199b7db7bSNick Piggin * 212299b7db7bSNick Piggin * vfsmount_lock must be held for write 21235528f911STrond Myklebust */ 2124692afc31SAl Viro static void shrink_submounts(struct mount *mnt, struct list_head *umounts) 21255528f911STrond Myklebust { 21265528f911STrond Myklebust LIST_HEAD(graveyard); 2127761d5c38SAl Viro struct mount *m; 21285528f911STrond Myklebust 21295528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2130c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2131bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2132761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 21336776db3dSAl Viro mnt_expire); 2134143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2135afef80b3SEric W. Biederman umount_tree(m, 1, umounts); 2136bcc5c7d2SAl Viro } 2137bcc5c7d2SAl Viro } 21385528f911STrond Myklebust } 21395528f911STrond Myklebust 21405528f911STrond Myklebust /* 21411da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 21421da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 21431da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 21441da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 21451da177e4SLinus Torvalds */ 2146b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2147b58fed8bSRam Pai unsigned long n) 21481da177e4SLinus Torvalds { 21491da177e4SLinus Torvalds char *t = to; 21501da177e4SLinus Torvalds const char __user *f = from; 21511da177e4SLinus Torvalds char c; 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 21541da177e4SLinus Torvalds return n; 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds while (n) { 21571da177e4SLinus Torvalds if (__get_user(c, f)) { 21581da177e4SLinus Torvalds memset(t, 0, n); 21591da177e4SLinus Torvalds break; 21601da177e4SLinus Torvalds } 21611da177e4SLinus Torvalds *t++ = c; 21621da177e4SLinus Torvalds f++; 21631da177e4SLinus Torvalds n--; 21641da177e4SLinus Torvalds } 21651da177e4SLinus Torvalds return n; 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 21691da177e4SLinus Torvalds { 21701da177e4SLinus Torvalds int i; 21711da177e4SLinus Torvalds unsigned long page; 21721da177e4SLinus Torvalds unsigned long size; 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds *where = 0; 21751da177e4SLinus Torvalds if (!data) 21761da177e4SLinus Torvalds return 0; 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 21791da177e4SLinus Torvalds return -ENOMEM; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds /* We only care that *some* data at the address the user 21821da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 21831da177e4SLinus Torvalds * the remainder of the page. 21841da177e4SLinus Torvalds */ 21851da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 21861da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 21871da177e4SLinus Torvalds if (size > PAGE_SIZE) 21881da177e4SLinus Torvalds size = PAGE_SIZE; 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 21911da177e4SLinus Torvalds if (!i) { 21921da177e4SLinus Torvalds free_page(page); 21931da177e4SLinus Torvalds return -EFAULT; 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds if (i != PAGE_SIZE) 21961da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 21971da177e4SLinus Torvalds *where = page; 21981da177e4SLinus Torvalds return 0; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 2201eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2202eca6f534SVegard Nossum { 2203eca6f534SVegard Nossum char *tmp; 2204eca6f534SVegard Nossum 2205eca6f534SVegard Nossum if (!data) { 2206eca6f534SVegard Nossum *where = NULL; 2207eca6f534SVegard Nossum return 0; 2208eca6f534SVegard Nossum } 2209eca6f534SVegard Nossum 2210eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2211eca6f534SVegard Nossum if (IS_ERR(tmp)) 2212eca6f534SVegard Nossum return PTR_ERR(tmp); 2213eca6f534SVegard Nossum 2214eca6f534SVegard Nossum *where = tmp; 2215eca6f534SVegard Nossum return 0; 2216eca6f534SVegard Nossum } 2217eca6f534SVegard Nossum 22181da177e4SLinus Torvalds /* 22191da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 22201da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 22211da177e4SLinus Torvalds * 22221da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 22231da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 22241da177e4SLinus Torvalds * information (or be NULL). 22251da177e4SLinus Torvalds * 22261da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 22271da177e4SLinus Torvalds * When the flags word was introduced its top half was required 22281da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 22291da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 22301da177e4SLinus Torvalds * and must be discarded. 22311da177e4SLinus Torvalds */ 2232808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name, 2233808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 22341da177e4SLinus Torvalds { 22352d92ab3cSAl Viro struct path path; 22361da177e4SLinus Torvalds int retval = 0; 22371da177e4SLinus Torvalds int mnt_flags = 0; 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds /* Discard magic */ 22401da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 22411da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 22421da177e4SLinus Torvalds 22431da177e4SLinus Torvalds /* Basic sanity checks */ 22441da177e4SLinus Torvalds 22451da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 22461da177e4SLinus Torvalds return -EINVAL; 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds if (data_page) 22491da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 22501da177e4SLinus Torvalds 2251a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2252a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2253a27ab9f2STetsuo Handa if (retval) 2254a27ab9f2STetsuo Handa return retval; 2255a27ab9f2STetsuo Handa 2256a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2257a27ab9f2STetsuo Handa type_page, flags, data_page); 2258a27ab9f2STetsuo Handa if (retval) 2259a27ab9f2STetsuo Handa goto dput_out; 2260a27ab9f2STetsuo Handa 2261613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2262613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 22630a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 22640a1c01c9SMatthew Garrett 22651da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 22661da177e4SLinus Torvalds if (flags & MS_NOSUID) 22671da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 22681da177e4SLinus Torvalds if (flags & MS_NODEV) 22691da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 22701da177e4SLinus Torvalds if (flags & MS_NOEXEC) 22711da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2272fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2273fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2274fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2275fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2276d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2277d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 22782e4b7fcdSDave Hansen if (flags & MS_RDONLY) 22792e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2280fc33a7bbSChristoph Hellwig 22817a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2282d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2283d0adde57SMatthew Garrett MS_STRICTATIME); 22841da177e4SLinus Torvalds 22851da177e4SLinus Torvalds if (flags & MS_REMOUNT) 22862d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 22871da177e4SLinus Torvalds data_page); 22881da177e4SLinus Torvalds else if (flags & MS_BIND) 22892d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 22909676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 22912d92ab3cSAl Viro retval = do_change_type(&path, flags); 22921da177e4SLinus Torvalds else if (flags & MS_MOVE) 22932d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 22941da177e4SLinus Torvalds else 22952d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 22961da177e4SLinus Torvalds dev_name, data_page); 22971da177e4SLinus Torvalds dput_out: 22982d92ab3cSAl Viro path_put(&path); 22991da177e4SLinus Torvalds return retval; 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds 2302771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2303771b1371SEric W. Biederman { 2304771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2305771b1371SEric W. Biederman kfree(ns); 2306771b1371SEric W. Biederman } 2307771b1371SEric W. Biederman 23088823c079SEric W. Biederman /* 23098823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 23108823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 23118823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 23128823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 23138823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 23148823c079SEric W. Biederman */ 23158823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 23168823c079SEric W. Biederman 2317771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2318cf8d2c11STrond Myklebust { 2319cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2320cf8d2c11STrond Myklebust 2321cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2322cf8d2c11STrond Myklebust if (!new_ns) 2323cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 23248823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2325cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2326cf8d2c11STrond Myklebust new_ns->root = NULL; 2327cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2328cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2329cf8d2c11STrond Myklebust new_ns->event = 0; 2330771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2331cf8d2c11STrond Myklebust return new_ns; 2332cf8d2c11STrond Myklebust } 2333cf8d2c11STrond Myklebust 2334741a2951SJANAK DESAI /* 2335741a2951SJANAK DESAI * Allocate a new namespace structure and populate it with contents 2336741a2951SJANAK DESAI * copied from the namespace of the passed in task structure. 2337741a2951SJANAK DESAI */ 2338e3222c4eSBadari Pulavarty static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns, 2339771b1371SEric W. Biederman struct user_namespace *user_ns, struct fs_struct *fs) 23401da177e4SLinus Torvalds { 23416b3286edSKirill Korotaev struct mnt_namespace *new_ns; 23427f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2343315fc83eSAl Viro struct mount *p, *q; 2344be08d6d2SAl Viro struct mount *old = mnt_ns->root; 2345cb338d06SAl Viro struct mount *new; 23467a472ef4SEric W. Biederman int copy_flags; 23471da177e4SLinus Torvalds 2348771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2349cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2350cf8d2c11STrond Myklebust return new_ns; 23511da177e4SLinus Torvalds 2352390c6843SRam Pai down_write(&namespace_sem); 23531da177e4SLinus Torvalds /* First pass: copy the tree topology */ 23547a472ef4SEric W. Biederman copy_flags = CL_COPY_ALL | CL_EXPIRE; 23557a472ef4SEric W. Biederman if (user_ns != mnt_ns->user_ns) 23567a472ef4SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE; 23577a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2358be34d1a3SDavid Howells if (IS_ERR(new)) { 2359390c6843SRam Pai up_write(&namespace_sem); 2360771b1371SEric W. Biederman free_mnt_ns(new_ns); 2361be34d1a3SDavid Howells return ERR_CAST(new); 23621da177e4SLinus Torvalds } 2363be08d6d2SAl Viro new_ns->root = new; 2364962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 23651a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 2366962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds /* 23691da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 23701da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 23711da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 23721da177e4SLinus Torvalds */ 2373909b0a88SAl Viro p = old; 2374cb338d06SAl Viro q = new; 23751da177e4SLinus Torvalds while (p) { 2376143c8c91SAl Viro q->mnt_ns = new_ns; 23771da177e4SLinus Torvalds if (fs) { 2378315fc83eSAl Viro if (&p->mnt == fs->root.mnt) { 2379315fc83eSAl Viro fs->root.mnt = mntget(&q->mnt); 2380315fc83eSAl Viro rootmnt = &p->mnt; 23811da177e4SLinus Torvalds } 2382315fc83eSAl Viro if (&p->mnt == fs->pwd.mnt) { 2383315fc83eSAl Viro fs->pwd.mnt = mntget(&q->mnt); 2384315fc83eSAl Viro pwdmnt = &p->mnt; 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds } 2387909b0a88SAl Viro p = next_mnt(p, old); 2388909b0a88SAl Viro q = next_mnt(q, new); 23891da177e4SLinus Torvalds } 2390390c6843SRam Pai up_write(&namespace_sem); 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds if (rootmnt) 2393f03c6599SAl Viro mntput(rootmnt); 23941da177e4SLinus Torvalds if (pwdmnt) 2395f03c6599SAl Viro mntput(pwdmnt); 23961da177e4SLinus Torvalds 2397741a2951SJANAK DESAI return new_ns; 2398741a2951SJANAK DESAI } 2399741a2951SJANAK DESAI 2400213dd266SEric W. Biederman struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 2401771b1371SEric W. Biederman struct user_namespace *user_ns, struct fs_struct *new_fs) 2402741a2951SJANAK DESAI { 24036b3286edSKirill Korotaev struct mnt_namespace *new_ns; 2404741a2951SJANAK DESAI 2405e3222c4eSBadari Pulavarty BUG_ON(!ns); 24066b3286edSKirill Korotaev get_mnt_ns(ns); 2407741a2951SJANAK DESAI 2408741a2951SJANAK DESAI if (!(flags & CLONE_NEWNS)) 2409e3222c4eSBadari Pulavarty return ns; 2410741a2951SJANAK DESAI 2411771b1371SEric W. Biederman new_ns = dup_mnt_ns(ns, user_ns, new_fs); 2412741a2951SJANAK DESAI 24136b3286edSKirill Korotaev put_mnt_ns(ns); 2414e3222c4eSBadari Pulavarty return new_ns; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 2417cf8d2c11STrond Myklebust /** 2418cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2419cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2420cf8d2c11STrond Myklebust */ 24211a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2422cf8d2c11STrond Myklebust { 2423771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2424cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 24251a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 24261a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2427be08d6d2SAl Viro new_ns->root = mnt; 24281a4eeaf2SAl Viro list_add(&new_ns->list, &mnt->mnt_list); 2429c1334495SAl Viro } else { 24301a4eeaf2SAl Viro mntput(m); 2431cf8d2c11STrond Myklebust } 2432cf8d2c11STrond Myklebust return new_ns; 2433cf8d2c11STrond Myklebust } 2434cf8d2c11STrond Myklebust 2435ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2436ea441d11SAl Viro { 2437ea441d11SAl Viro struct mnt_namespace *ns; 2438d31da0f0SAl Viro struct super_block *s; 2439ea441d11SAl Viro struct path path; 2440ea441d11SAl Viro int err; 2441ea441d11SAl Viro 2442ea441d11SAl Viro ns = create_mnt_ns(mnt); 2443ea441d11SAl Viro if (IS_ERR(ns)) 2444ea441d11SAl Viro return ERR_CAST(ns); 2445ea441d11SAl Viro 2446ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2447ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2448ea441d11SAl Viro 2449ea441d11SAl Viro put_mnt_ns(ns); 2450ea441d11SAl Viro 2451ea441d11SAl Viro if (err) 2452ea441d11SAl Viro return ERR_PTR(err); 2453ea441d11SAl Viro 2454ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2455d31da0f0SAl Viro s = path.mnt->mnt_sb; 2456d31da0f0SAl Viro atomic_inc(&s->s_active); 2457ea441d11SAl Viro mntput(path.mnt); 2458ea441d11SAl Viro /* lock the sucker */ 2459d31da0f0SAl Viro down_write(&s->s_umount); 2460ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2461ea441d11SAl Viro return path.dentry; 2462ea441d11SAl Viro } 2463ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2464ea441d11SAl Viro 2465bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2466bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 24671da177e4SLinus Torvalds { 2468eca6f534SVegard Nossum int ret; 2469eca6f534SVegard Nossum char *kernel_type; 247091a27b2aSJeff Layton struct filename *kernel_dir; 2471eca6f534SVegard Nossum char *kernel_dev; 24721da177e4SLinus Torvalds unsigned long data_page; 24731da177e4SLinus Torvalds 2474eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2475eca6f534SVegard Nossum if (ret < 0) 2476eca6f534SVegard Nossum goto out_type; 24771da177e4SLinus Torvalds 2478eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2479eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2480eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2481eca6f534SVegard Nossum goto out_dir; 2482eca6f534SVegard Nossum } 24831da177e4SLinus Torvalds 2484eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2485eca6f534SVegard Nossum if (ret < 0) 2486eca6f534SVegard Nossum goto out_dev; 24871da177e4SLinus Torvalds 2488eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2489eca6f534SVegard Nossum if (ret < 0) 2490eca6f534SVegard Nossum goto out_data; 24911da177e4SLinus Torvalds 249291a27b2aSJeff Layton ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags, 2493eca6f534SVegard Nossum (void *) data_page); 2494eca6f534SVegard Nossum 24951da177e4SLinus Torvalds free_page(data_page); 2496eca6f534SVegard Nossum out_data: 2497eca6f534SVegard Nossum kfree(kernel_dev); 2498eca6f534SVegard Nossum out_dev: 2499eca6f534SVegard Nossum putname(kernel_dir); 2500eca6f534SVegard Nossum out_dir: 2501eca6f534SVegard Nossum kfree(kernel_type); 2502eca6f534SVegard Nossum out_type: 2503eca6f534SVegard Nossum return ret; 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds /* 2507afac7cbaSAl Viro * Return true if path is reachable from root 2508afac7cbaSAl Viro * 2509afac7cbaSAl Viro * namespace_sem or vfsmount_lock is held 2510afac7cbaSAl Viro */ 2511643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2512afac7cbaSAl Viro const struct path *root) 2513afac7cbaSAl Viro { 2514643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2515a73324daSAl Viro dentry = mnt->mnt_mountpoint; 25160714a533SAl Viro mnt = mnt->mnt_parent; 2517afac7cbaSAl Viro } 2518643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2519afac7cbaSAl Viro } 2520afac7cbaSAl Viro 2521afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2522afac7cbaSAl Viro { 2523afac7cbaSAl Viro int res; 2524962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 2525643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 2526962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 2527afac7cbaSAl Viro return res; 2528afac7cbaSAl Viro } 2529afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2530afac7cbaSAl Viro 2531afac7cbaSAl Viro /* 25321da177e4SLinus Torvalds * pivot_root Semantics: 25331da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 25341da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 25351da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 25361da177e4SLinus Torvalds * 25371da177e4SLinus Torvalds * Restrictions: 25381da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 25391da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 25401da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 25411da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 25421da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 25431da177e4SLinus Torvalds * 25444a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 25454a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 25464a0d11faSNeil Brown * in this situation. 25474a0d11faSNeil Brown * 25481da177e4SLinus Torvalds * Notes: 25491da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 25501da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 25511da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 25521da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 25531da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 25541da177e4SLinus Torvalds * first. 25551da177e4SLinus Torvalds */ 25563480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 25573480b257SHeiko Carstens const char __user *, put_old) 25581da177e4SLinus Torvalds { 25592d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 2560419148daSAl Viro struct mount *new_mnt, *root_mnt; 25611da177e4SLinus Torvalds int error; 25621da177e4SLinus Torvalds 25630c55cfc4SEric W. Biederman if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN)) 25641da177e4SLinus Torvalds return -EPERM; 25651da177e4SLinus Torvalds 25662d8f3038SAl Viro error = user_path_dir(new_root, &new); 25671da177e4SLinus Torvalds if (error) 25681da177e4SLinus Torvalds goto out0; 25691da177e4SLinus Torvalds 25702d8f3038SAl Viro error = user_path_dir(put_old, &old); 25711da177e4SLinus Torvalds if (error) 25721da177e4SLinus Torvalds goto out1; 25731da177e4SLinus Torvalds 25742d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2575b12cea91SAl Viro if (error) 2576b12cea91SAl Viro goto out2; 25771da177e4SLinus Torvalds 2578f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 2579b12cea91SAl Viro error = lock_mount(&old); 2580b12cea91SAl Viro if (error) 2581b12cea91SAl Viro goto out3; 2582b12cea91SAl Viro 25831da177e4SLinus Torvalds error = -EINVAL; 2584419148daSAl Viro new_mnt = real_mount(new.mnt); 2585419148daSAl Viro root_mnt = real_mount(root.mnt); 2586fc7be130SAl Viro if (IS_MNT_SHARED(real_mount(old.mnt)) || 2587fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2588fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2589b12cea91SAl Viro goto out4; 2590143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2591b12cea91SAl Viro goto out4; 25921da177e4SLinus Torvalds error = -ENOENT; 2593f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2594b12cea91SAl Viro goto out4; 2595f3da392eSAlexey Dobriyan if (d_unlinked(old.dentry)) 2596b12cea91SAl Viro goto out4; 25971da177e4SLinus Torvalds error = -EBUSY; 25982d8f3038SAl Viro if (new.mnt == root.mnt || 25992d8f3038SAl Viro old.mnt == root.mnt) 2600b12cea91SAl Viro goto out4; /* loop, on the same file system */ 26011da177e4SLinus Torvalds error = -EINVAL; 26028c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2603b12cea91SAl Viro goto out4; /* not a mountpoint */ 2604676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2605b12cea91SAl Viro goto out4; /* not attached */ 26062d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2607b12cea91SAl Viro goto out4; /* not a mountpoint */ 2608676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2609b12cea91SAl Viro goto out4; /* not attached */ 26104ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 2611643822b4SAl Viro if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new)) 2612b12cea91SAl Viro goto out4; 2613962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2614419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2615419148daSAl Viro detach_mnt(root_mnt, &root_parent); 26164ac91378SJan Blunck /* mount old root on put_old */ 2617419148daSAl Viro attach_mnt(root_mnt, &old); 26184ac91378SJan Blunck /* mount new_root on / */ 2619419148daSAl Viro attach_mnt(new_mnt, &root_parent); 26206b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2621962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 26222d8f3038SAl Viro chroot_fs_refs(&root, &new); 26231da177e4SLinus Torvalds error = 0; 2624b12cea91SAl Viro out4: 2625b12cea91SAl Viro unlock_mount(&old); 2626b12cea91SAl Viro if (!error) { 26271a390689SAl Viro path_put(&root_parent); 26281a390689SAl Viro path_put(&parent_path); 2629b12cea91SAl Viro } 2630b12cea91SAl Viro out3: 26318c3ee42eSAl Viro path_put(&root); 2632b12cea91SAl Viro out2: 26332d8f3038SAl Viro path_put(&old); 26341da177e4SLinus Torvalds out1: 26352d8f3038SAl Viro path_put(&new); 26361da177e4SLinus Torvalds out0: 26371da177e4SLinus Torvalds return error; 26381da177e4SLinus Torvalds } 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds static void __init init_mount_tree(void) 26411da177e4SLinus Torvalds { 26421da177e4SLinus Torvalds struct vfsmount *mnt; 26436b3286edSKirill Korotaev struct mnt_namespace *ns; 2644ac748a09SJan Blunck struct path root; 26450c55cfc4SEric W. Biederman struct file_system_type *type; 26461da177e4SLinus Torvalds 26470c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 26480c55cfc4SEric W. Biederman if (!type) 26490c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 26500c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 26510c55cfc4SEric W. Biederman put_filesystem(type); 26521da177e4SLinus Torvalds if (IS_ERR(mnt)) 26531da177e4SLinus Torvalds panic("Can't create rootfs"); 2654b3e19d92SNick Piggin 26553b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 26563b22edc5STrond Myklebust if (IS_ERR(ns)) 26571da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 26581da177e4SLinus Torvalds 26596b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 26606b3286edSKirill Korotaev get_mnt_ns(ns); 26611da177e4SLinus Torvalds 2662be08d6d2SAl Viro root.mnt = mnt; 2663be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2664ac748a09SJan Blunck 2665ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2666ac748a09SJan Blunck set_fs_root(current->fs, &root); 26671da177e4SLinus Torvalds } 26681da177e4SLinus Torvalds 266974bf17cfSDenis Cheng void __init mnt_init(void) 26701da177e4SLinus Torvalds { 267113f14b4dSEric Dumazet unsigned u; 267215a67dd8SRandy Dunlap int err; 26731da177e4SLinus Torvalds 2674390c6843SRam Pai init_rwsem(&namespace_sem); 2675390c6843SRam Pai 26767d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 267720c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 26781da177e4SLinus Torvalds 2679b58fed8bSRam Pai mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds if (!mount_hashtable) 26821da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 26831da177e4SLinus Torvalds 268480cdc6daSMandeep Singh Baines printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE); 26851da177e4SLinus Torvalds 268613f14b4dSEric Dumazet for (u = 0; u < HASH_SIZE; u++) 268713f14b4dSEric Dumazet INIT_LIST_HEAD(&mount_hashtable[u]); 26881da177e4SLinus Torvalds 2689962830dfSAndi Kleen br_lock_init(&vfsmount_lock); 269099b7db7bSNick Piggin 269115a67dd8SRandy Dunlap err = sysfs_init(); 269215a67dd8SRandy Dunlap if (err) 269315a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 26948e24eea7SHarvey Harrison __func__, err); 269500d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 269600d26666SGreg Kroah-Hartman if (!fs_kobj) 26978e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 26981da177e4SLinus Torvalds init_rootfs(); 26991da177e4SLinus Torvalds init_mount_tree(); 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds 2702616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 27031da177e4SLinus Torvalds { 270470fbcdf4SRam Pai LIST_HEAD(umount_list); 2705616511d0STrond Myklebust 2706d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2707616511d0STrond Myklebust return; 2708390c6843SRam Pai down_write(&namespace_sem); 2709962830dfSAndi Kleen br_write_lock(&vfsmount_lock); 2710be08d6d2SAl Viro umount_tree(ns->root, 0, &umount_list); 2711962830dfSAndi Kleen br_write_unlock(&vfsmount_lock); 2712390c6843SRam Pai up_write(&namespace_sem); 271370fbcdf4SRam Pai release_mounts(&umount_list); 2714771b1371SEric W. Biederman free_mnt_ns(ns); 27151da177e4SLinus Torvalds } 27169d412a43SAl Viro 27179d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 27189d412a43SAl Viro { 2719423e0ab0STim Chen struct vfsmount *mnt; 2720423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2721423e0ab0STim Chen if (!IS_ERR(mnt)) { 2722423e0ab0STim Chen /* 2723423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2724423e0ab0STim Chen * we unmount before file sys is unregistered 2725423e0ab0STim Chen */ 2726f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 2727423e0ab0STim Chen } 2728423e0ab0STim Chen return mnt; 27299d412a43SAl Viro } 27309d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2731423e0ab0STim Chen 2732423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2733423e0ab0STim Chen { 2734423e0ab0STim Chen /* release long term mount so mount point can be released */ 2735423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2736f7a99c5bSAl Viro br_write_lock(&vfsmount_lock); 2737f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 2738f7a99c5bSAl Viro br_write_unlock(&vfsmount_lock); 2739423e0ab0STim Chen mntput(mnt); 2740423e0ab0STim Chen } 2741423e0ab0STim Chen } 2742423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 274302125a82SAl Viro 274402125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 274502125a82SAl Viro { 2746143c8c91SAl Viro return check_mnt(real_mount(mnt)); 274702125a82SAl Viro } 27488823c079SEric W. Biederman 27498823c079SEric W. Biederman static void *mntns_get(struct task_struct *task) 27508823c079SEric W. Biederman { 27518823c079SEric W. Biederman struct mnt_namespace *ns = NULL; 27528823c079SEric W. Biederman struct nsproxy *nsproxy; 27538823c079SEric W. Biederman 27548823c079SEric W. Biederman rcu_read_lock(); 27558823c079SEric W. Biederman nsproxy = task_nsproxy(task); 27568823c079SEric W. Biederman if (nsproxy) { 27578823c079SEric W. Biederman ns = nsproxy->mnt_ns; 27588823c079SEric W. Biederman get_mnt_ns(ns); 27598823c079SEric W. Biederman } 27608823c079SEric W. Biederman rcu_read_unlock(); 27618823c079SEric W. Biederman 27628823c079SEric W. Biederman return ns; 27638823c079SEric W. Biederman } 27648823c079SEric W. Biederman 27658823c079SEric W. Biederman static void mntns_put(void *ns) 27668823c079SEric W. Biederman { 27678823c079SEric W. Biederman put_mnt_ns(ns); 27688823c079SEric W. Biederman } 27698823c079SEric W. Biederman 27708823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns) 27718823c079SEric W. Biederman { 27728823c079SEric W. Biederman struct fs_struct *fs = current->fs; 27738823c079SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 27748823c079SEric W. Biederman struct path root; 27758823c079SEric W. Biederman 27760c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 27770c55cfc4SEric W. Biederman !nsown_capable(CAP_SYS_CHROOT)) 27788823c079SEric W. Biederman return -EINVAL; 27798823c079SEric W. Biederman 27808823c079SEric W. Biederman if (fs->users != 1) 27818823c079SEric W. Biederman return -EINVAL; 27828823c079SEric W. Biederman 27838823c079SEric W. Biederman get_mnt_ns(mnt_ns); 27848823c079SEric W. Biederman put_mnt_ns(nsproxy->mnt_ns); 27858823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 27868823c079SEric W. Biederman 27878823c079SEric W. Biederman /* Find the root */ 27888823c079SEric W. Biederman root.mnt = &mnt_ns->root->mnt; 27898823c079SEric W. Biederman root.dentry = mnt_ns->root->mnt.mnt_root; 27908823c079SEric W. Biederman path_get(&root); 27918823c079SEric W. Biederman while(d_mountpoint(root.dentry) && follow_down_one(&root)) 27928823c079SEric W. Biederman ; 27938823c079SEric W. Biederman 27948823c079SEric W. Biederman /* Update the pwd and root */ 27958823c079SEric W. Biederman set_fs_pwd(fs, &root); 27968823c079SEric W. Biederman set_fs_root(fs, &root); 27978823c079SEric W. Biederman 27988823c079SEric W. Biederman path_put(&root); 27998823c079SEric W. Biederman return 0; 28008823c079SEric W. Biederman } 28018823c079SEric W. Biederman 28028823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 28038823c079SEric W. Biederman .name = "mnt", 28048823c079SEric W. Biederman .type = CLONE_NEWNS, 28058823c079SEric W. Biederman .get = mntns_get, 28068823c079SEric W. Biederman .put = mntns_put, 28078823c079SEric W. Biederman .install = mntns_install, 28088823c079SEric W. Biederman }; 2809