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> 185b825c3aSIngo Molnar #include <linux/cred.h> 1973cd49ecSMiklos Szeredi #include <linux/idr.h> 2057f150a5SRob Landley #include <linux/init.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> 240bb80f24SDavid Howells #include <linux/proc_ns.h> 2520b4fb48SLinus Torvalds #include <linux/magic.h> 2657c8a661SMike Rapoport #include <linux/memblock.h> 279ea459e1SAl Viro #include <linux/task_work.h> 289164bb4aSIngo Molnar #include <linux/sched/task.h> 299164bb4aSIngo Molnar 3007b20889SRam Pai #include "pnode.h" 31948730b0SAdrian Bunk #include "internal.h" 321da177e4SLinus Torvalds 33d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */ 34d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000; 35d2921684SEric W. Biederman 360818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 370818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 380818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 390818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 400818bf27SAl Viro 410818bf27SAl Viro static __initdata unsigned long mhash_entries; 420818bf27SAl Viro static int __init set_mhash_entries(char *str) 430818bf27SAl Viro { 440818bf27SAl Viro if (!str) 450818bf27SAl Viro return 0; 460818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 470818bf27SAl Viro return 1; 480818bf27SAl Viro } 490818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 500818bf27SAl Viro 510818bf27SAl Viro static __initdata unsigned long mphash_entries; 520818bf27SAl Viro static int __init set_mphash_entries(char *str) 530818bf27SAl Viro { 540818bf27SAl Viro if (!str) 550818bf27SAl Viro return 0; 560818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 570818bf27SAl Viro return 1; 580818bf27SAl Viro } 590818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 6013f14b4dSEric Dumazet 61c7999c36SAl Viro static u64 event; 6273cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 63719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 645addc5ddSAl Viro 6538129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 660818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 67e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 6859aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 691da177e4SLinus Torvalds 70f87fd4c2SMiklos Szeredi /* /sys/fs */ 7100d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 7200d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 73f87fd4c2SMiklos Szeredi 7499b7db7bSNick Piggin /* 7599b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 7699b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 7799b7db7bSNick Piggin * up the tree. 7899b7db7bSNick Piggin * 7999b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 8099b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 8199b7db7bSNick Piggin */ 8248a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 8399b7db7bSNick Piggin 8438129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 851da177e4SLinus Torvalds { 861da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 871da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 880818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 890818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 900818bf27SAl Viro } 910818bf27SAl Viro 920818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 930818bf27SAl Viro { 940818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 950818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 960818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 99b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 10073cd49ecSMiklos Szeredi { 101169b480eSMatthew Wilcox int res = ida_alloc(&mnt_id_ida, GFP_KERNEL); 10273cd49ecSMiklos Szeredi 103169b480eSMatthew Wilcox if (res < 0) 10473cd49ecSMiklos Szeredi return res; 105169b480eSMatthew Wilcox mnt->mnt_id = res; 106169b480eSMatthew Wilcox return 0; 10773cd49ecSMiklos Szeredi } 10873cd49ecSMiklos Szeredi 109b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 11073cd49ecSMiklos Szeredi { 111169b480eSMatthew Wilcox ida_free(&mnt_id_ida, mnt->mnt_id); 11273cd49ecSMiklos Szeredi } 11373cd49ecSMiklos Szeredi 114719f5d7fSMiklos Szeredi /* 115719f5d7fSMiklos Szeredi * Allocate a new peer group ID 116719f5d7fSMiklos Szeredi */ 1174b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 118719f5d7fSMiklos Szeredi { 119169b480eSMatthew Wilcox int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL); 120f21f6220SAl Viro 121169b480eSMatthew Wilcox if (res < 0) 122f21f6220SAl Viro return res; 123169b480eSMatthew Wilcox mnt->mnt_group_id = res; 124169b480eSMatthew Wilcox return 0; 125719f5d7fSMiklos Szeredi } 126719f5d7fSMiklos Szeredi 127719f5d7fSMiklos Szeredi /* 128719f5d7fSMiklos Szeredi * Release a peer group ID 129719f5d7fSMiklos Szeredi */ 1304b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 131719f5d7fSMiklos Szeredi { 132169b480eSMatthew Wilcox ida_free(&mnt_group_ida, mnt->mnt_group_id); 13315169fe7SAl Viro mnt->mnt_group_id = 0; 134719f5d7fSMiklos Szeredi } 135719f5d7fSMiklos Szeredi 136b3e19d92SNick Piggin /* 137b3e19d92SNick Piggin * vfsmount lock must be held for read 138b3e19d92SNick Piggin */ 13983adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 140b3e19d92SNick Piggin { 141b3e19d92SNick Piggin #ifdef CONFIG_SMP 14268e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 143b3e19d92SNick Piggin #else 144b3e19d92SNick Piggin preempt_disable(); 14568e8a9feSAl Viro mnt->mnt_count += n; 146b3e19d92SNick Piggin preempt_enable(); 147b3e19d92SNick Piggin #endif 148b3e19d92SNick Piggin } 149b3e19d92SNick Piggin 150b3e19d92SNick Piggin /* 151b3e19d92SNick Piggin * vfsmount lock must be held for write 152b3e19d92SNick Piggin */ 15383adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 154b3e19d92SNick Piggin { 155b3e19d92SNick Piggin #ifdef CONFIG_SMP 156f03c6599SAl Viro unsigned int count = 0; 157b3e19d92SNick Piggin int cpu; 158b3e19d92SNick Piggin 159b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 16068e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 161b3e19d92SNick Piggin } 162b3e19d92SNick Piggin 163b3e19d92SNick Piggin return count; 164b3e19d92SNick Piggin #else 16568e8a9feSAl Viro return mnt->mnt_count; 166b3e19d92SNick Piggin #endif 167b3e19d92SNick Piggin } 168b3e19d92SNick Piggin 16987b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p) 17087b95ce0SAl Viro { 17187b95ce0SAl Viro struct mount *m = container_of(p, struct mount, mnt_umount); 17287b95ce0SAl Viro dput(m->mnt_ex_mountpoint); 17387b95ce0SAl Viro pin_remove(p); 17487b95ce0SAl Viro mntput(&m->mnt); 17587b95ce0SAl Viro } 17687b95ce0SAl Viro 177b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1781da177e4SLinus Torvalds { 179c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 180c63181e6SAl Viro if (mnt) { 18173cd49ecSMiklos Szeredi int err; 18273cd49ecSMiklos Szeredi 183c63181e6SAl Viro err = mnt_alloc_id(mnt); 18488b38782SLi Zefan if (err) 18588b38782SLi Zefan goto out_free_cache; 18688b38782SLi Zefan 18788b38782SLi Zefan if (name) { 188fcc139aeSAndrzej Hajda mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL); 189c63181e6SAl Viro if (!mnt->mnt_devname) 19088b38782SLi Zefan goto out_free_id; 19173cd49ecSMiklos Szeredi } 19273cd49ecSMiklos Szeredi 193b3e19d92SNick Piggin #ifdef CONFIG_SMP 194c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 195c63181e6SAl Viro if (!mnt->mnt_pcp) 196b3e19d92SNick Piggin goto out_free_devname; 197b3e19d92SNick Piggin 198c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 199b3e19d92SNick Piggin #else 200c63181e6SAl Viro mnt->mnt_count = 1; 201c63181e6SAl Viro mnt->mnt_writers = 0; 202b3e19d92SNick Piggin #endif 203b3e19d92SNick Piggin 20438129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 205c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 206c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 207c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 208c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 209c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 210c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 211c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2120a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 21399b19d16SEric W. Biederman INIT_LIST_HEAD(&mnt->mnt_umounting); 21487b95ce0SAl Viro init_fs_pin(&mnt->mnt_umount, drop_mountpoint); 2151da177e4SLinus Torvalds } 216c63181e6SAl Viro return mnt; 21788b38782SLi Zefan 218d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 219d3ef3d73Snpiggin@suse.de out_free_devname: 220fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 221d3ef3d73Snpiggin@suse.de #endif 22288b38782SLi Zefan out_free_id: 223c63181e6SAl Viro mnt_free_id(mnt); 22488b38782SLi Zefan out_free_cache: 225c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 22688b38782SLi Zefan return NULL; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2298366025eSDave Hansen /* 2308366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2318366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2328366025eSDave Hansen * We must keep track of when those operations start 2338366025eSDave Hansen * (for permission checks) and when they end, so that 2348366025eSDave Hansen * we can determine when writes are able to occur to 2358366025eSDave Hansen * a filesystem. 2368366025eSDave Hansen */ 2373d733633SDave Hansen /* 2383d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2393d733633SDave Hansen * @mnt: the mount to check for its write status 2403d733633SDave Hansen * 2413d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2423d733633SDave Hansen * It does not guarantee that the filesystem will stay 2433d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2443d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2453d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2463d733633SDave Hansen * r/w. 2473d733633SDave Hansen */ 2483d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2493d733633SDave Hansen { 2502e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2512e4b7fcdSDave Hansen return 1; 252bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt_sb)) 2532e4b7fcdSDave Hansen return 1; 2542e4b7fcdSDave Hansen return 0; 2553d733633SDave Hansen } 2563d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2573d733633SDave Hansen 25883adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2593d733633SDave Hansen { 260d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 26168e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 262d3ef3d73Snpiggin@suse.de #else 26368e8a9feSAl Viro mnt->mnt_writers++; 264d3ef3d73Snpiggin@suse.de #endif 2653d733633SDave Hansen } 2663d733633SDave Hansen 26783adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2683d733633SDave Hansen { 269d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 27068e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 271d3ef3d73Snpiggin@suse.de #else 27268e8a9feSAl Viro mnt->mnt_writers--; 273d3ef3d73Snpiggin@suse.de #endif 274d3ef3d73Snpiggin@suse.de } 275d3ef3d73Snpiggin@suse.de 27683adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 277d3ef3d73Snpiggin@suse.de { 278d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 279d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2803d733633SDave Hansen int cpu; 2813d733633SDave Hansen 2823d733633SDave Hansen for_each_possible_cpu(cpu) { 28368e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2843d733633SDave Hansen } 2853d733633SDave Hansen 286d3ef3d73Snpiggin@suse.de return count; 287d3ef3d73Snpiggin@suse.de #else 288d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 289d3ef3d73Snpiggin@suse.de #endif 2903d733633SDave Hansen } 2913d733633SDave Hansen 2924ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2934ed5e82fSMiklos Szeredi { 2944ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 2954ed5e82fSMiklos Szeredi return 1; 2964ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 2974ed5e82fSMiklos Szeredi smp_rmb(); 2984ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 2994ed5e82fSMiklos Szeredi } 3004ed5e82fSMiklos Szeredi 3013d733633SDave Hansen /* 302eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 303eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 304eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 305eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3063d733633SDave Hansen */ 3078366025eSDave Hansen /** 308eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 30983adc753SAl Viro * @m: the mount on which to take a write 3108366025eSDave Hansen * 311eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 312eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 313eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 314eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 315eb04c282SJan Kara * called. This is effectively a refcount. 3168366025eSDave Hansen */ 317eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3188366025eSDave Hansen { 31983adc753SAl Viro struct mount *mnt = real_mount(m); 3203d733633SDave Hansen int ret = 0; 3213d733633SDave Hansen 322d3ef3d73Snpiggin@suse.de preempt_disable(); 323c6653a83SNick Piggin mnt_inc_writers(mnt); 324d3ef3d73Snpiggin@suse.de /* 325c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 326d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 327d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 328d3ef3d73Snpiggin@suse.de */ 329d3ef3d73Snpiggin@suse.de smp_mb(); 3306aa7de05SMark Rutland while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 331d3ef3d73Snpiggin@suse.de cpu_relax(); 332d3ef3d73Snpiggin@suse.de /* 333d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 334d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 335d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 336d3ef3d73Snpiggin@suse.de */ 337d3ef3d73Snpiggin@suse.de smp_rmb(); 3384ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 339c6653a83SNick Piggin mnt_dec_writers(mnt); 3403d733633SDave Hansen ret = -EROFS; 3413d733633SDave Hansen } 342d3ef3d73Snpiggin@suse.de preempt_enable(); 343eb04c282SJan Kara 344eb04c282SJan Kara return ret; 345eb04c282SJan Kara } 346eb04c282SJan Kara 347eb04c282SJan Kara /** 348eb04c282SJan Kara * mnt_want_write - get write access to a mount 349eb04c282SJan Kara * @m: the mount on which to take a write 350eb04c282SJan Kara * 351eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 352eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 353eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 354eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 355eb04c282SJan Kara */ 356eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 357eb04c282SJan Kara { 358eb04c282SJan Kara int ret; 359eb04c282SJan Kara 360eb04c282SJan Kara sb_start_write(m->mnt_sb); 361eb04c282SJan Kara ret = __mnt_want_write(m); 362eb04c282SJan Kara if (ret) 363eb04c282SJan Kara sb_end_write(m->mnt_sb); 3643d733633SDave Hansen return ret; 3658366025eSDave Hansen } 3668366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3678366025eSDave Hansen 3688366025eSDave Hansen /** 36996029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 37096029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 37196029c4eSnpiggin@suse.de * 37296029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 37396029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 37496029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 37596029c4eSnpiggin@suse.de * on it. This allows some optimisation. 37696029c4eSnpiggin@suse.de * 37796029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 37896029c4eSnpiggin@suse.de * drop the reference. 37996029c4eSnpiggin@suse.de */ 38096029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 38196029c4eSnpiggin@suse.de { 38296029c4eSnpiggin@suse.de /* superblock may be r/o */ 38396029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 38496029c4eSnpiggin@suse.de return -EROFS; 38596029c4eSnpiggin@suse.de preempt_disable(); 38683adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 38796029c4eSnpiggin@suse.de preempt_enable(); 38896029c4eSnpiggin@suse.de return 0; 38996029c4eSnpiggin@suse.de } 39096029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 39196029c4eSnpiggin@suse.de 39296029c4eSnpiggin@suse.de /** 393eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 394eb04c282SJan Kara * @file: the file who's mount on which to take a write 395eb04c282SJan Kara * 396eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 397eb04c282SJan Kara * do some optimisations if the file is open for write already 398eb04c282SJan Kara */ 399eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 400eb04c282SJan Kara { 40183f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 402eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 403eb04c282SJan Kara else 404eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 405eb04c282SJan Kara } 406eb04c282SJan Kara 407eb04c282SJan Kara /** 4087c6893e3SMiklos Szeredi * mnt_want_write_file - get write access to a file's mount 4097c6893e3SMiklos Szeredi * @file: the file who's mount on which to take a write 4107c6893e3SMiklos Szeredi * 4117c6893e3SMiklos Szeredi * This is like mnt_want_write, but it takes a file and can 4127c6893e3SMiklos Szeredi * do some optimisations if the file is open for write already 4137c6893e3SMiklos Szeredi */ 4147c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file) 4157c6893e3SMiklos Szeredi { 4167c6893e3SMiklos Szeredi int ret; 4177c6893e3SMiklos Szeredi 4187c6893e3SMiklos Szeredi sb_start_write(file_inode(file)->i_sb); 4197c6893e3SMiklos Szeredi ret = __mnt_want_write_file(file); 4207c6893e3SMiklos Szeredi if (ret) 4217c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4227c6893e3SMiklos Szeredi return ret; 4237c6893e3SMiklos Szeredi } 42496029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 42596029c4eSnpiggin@suse.de 42696029c4eSnpiggin@suse.de /** 427eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4288366025eSDave Hansen * @mnt: the mount on which to give up write access 4298366025eSDave Hansen * 4308366025eSDave Hansen * Tells the low-level filesystem that we are done 4318366025eSDave Hansen * performing writes to it. Must be matched with 432eb04c282SJan Kara * __mnt_want_write() call above. 4338366025eSDave Hansen */ 434eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4358366025eSDave Hansen { 436d3ef3d73Snpiggin@suse.de preempt_disable(); 43783adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 438d3ef3d73Snpiggin@suse.de preempt_enable(); 4398366025eSDave Hansen } 440eb04c282SJan Kara 441eb04c282SJan Kara /** 442eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 443eb04c282SJan Kara * @mnt: the mount on which to give up write access 444eb04c282SJan Kara * 445eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 446eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 447eb04c282SJan Kara * mnt_want_write() call above. 448eb04c282SJan Kara */ 449eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 450eb04c282SJan Kara { 451eb04c282SJan Kara __mnt_drop_write(mnt); 452eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 453eb04c282SJan Kara } 4548366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4558366025eSDave Hansen 456eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 457eb04c282SJan Kara { 458eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 459eb04c282SJan Kara } 460eb04c282SJan Kara 4617c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file) 4627c6893e3SMiklos Szeredi { 463a6795a58SMiklos Szeredi __mnt_drop_write_file(file); 4647c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4657c6893e3SMiklos Szeredi } 4662a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4672a79f17eSAl Viro 46883adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4698366025eSDave Hansen { 4703d733633SDave Hansen int ret = 0; 4713d733633SDave Hansen 472719ea2fbSAl Viro lock_mount_hash(); 47383adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 474d3ef3d73Snpiggin@suse.de /* 475d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 476d3ef3d73Snpiggin@suse.de * should be visible before we do. 477d3ef3d73Snpiggin@suse.de */ 478d3ef3d73Snpiggin@suse.de smp_mb(); 479d3ef3d73Snpiggin@suse.de 480d3ef3d73Snpiggin@suse.de /* 481d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 482d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 483d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 484d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 485d3ef3d73Snpiggin@suse.de * 486d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 487d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 488d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 489d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 490d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 491d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 492d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 493d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 494d3ef3d73Snpiggin@suse.de * we're counting up here. 495d3ef3d73Snpiggin@suse.de */ 496c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 497d3ef3d73Snpiggin@suse.de ret = -EBUSY; 498d3ef3d73Snpiggin@suse.de else 49983adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 500d3ef3d73Snpiggin@suse.de /* 501d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 502d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 503d3ef3d73Snpiggin@suse.de */ 504d3ef3d73Snpiggin@suse.de smp_wmb(); 50583adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 506719ea2fbSAl Viro unlock_mount_hash(); 5073d733633SDave Hansen return ret; 5083d733633SDave Hansen } 5098366025eSDave Hansen 51083adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 5112e4b7fcdSDave Hansen { 512719ea2fbSAl Viro lock_mount_hash(); 51383adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 514719ea2fbSAl Viro unlock_mount_hash(); 5152e4b7fcdSDave Hansen } 5162e4b7fcdSDave Hansen 5174ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5184ed5e82fSMiklos Szeredi { 5194ed5e82fSMiklos Szeredi struct mount *mnt; 5204ed5e82fSMiklos Szeredi int err = 0; 5214ed5e82fSMiklos Szeredi 5228e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5238e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5248e8b8796SMiklos Szeredi return -EBUSY; 5258e8b8796SMiklos Szeredi 526719ea2fbSAl Viro lock_mount_hash(); 5274ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5284ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5294ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5304ed5e82fSMiklos Szeredi smp_mb(); 5314ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5324ed5e82fSMiklos Szeredi err = -EBUSY; 5334ed5e82fSMiklos Szeredi break; 5344ed5e82fSMiklos Szeredi } 5354ed5e82fSMiklos Szeredi } 5364ed5e82fSMiklos Szeredi } 5378e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5388e8b8796SMiklos Szeredi err = -EBUSY; 5398e8b8796SMiklos Szeredi 5404ed5e82fSMiklos Szeredi if (!err) { 5414ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5424ed5e82fSMiklos Szeredi smp_wmb(); 5434ed5e82fSMiklos Szeredi } 5444ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5454ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5464ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5474ed5e82fSMiklos Szeredi } 548719ea2fbSAl Viro unlock_mount_hash(); 5494ed5e82fSMiklos Szeredi 5504ed5e82fSMiklos Szeredi return err; 5514ed5e82fSMiklos Szeredi } 5524ed5e82fSMiklos Szeredi 553b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5541da177e4SLinus Torvalds { 555fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 556d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 55768e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 558d3ef3d73Snpiggin@suse.de #endif 559b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5628ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5638ffcb32eSDavid Howells { 5648ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5658ffcb32eSDavid Howells } 5668ffcb32eSDavid Howells 56748a066e7SAl Viro /* call under rcu_read_lock */ 568294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 56948a066e7SAl Viro { 57048a066e7SAl Viro struct mount *mnt; 57148a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 572294d71ffSAl Viro return 1; 57348a066e7SAl Viro if (bastard == NULL) 574294d71ffSAl Viro return 0; 57548a066e7SAl Viro mnt = real_mount(bastard); 57648a066e7SAl Viro mnt_add_count(mnt, 1); 577119e1ef8SAl Viro smp_mb(); // see mntput_no_expire() 57848a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 579294d71ffSAl Viro return 0; 58048a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 58148a066e7SAl Viro mnt_add_count(mnt, -1); 582294d71ffSAl Viro return 1; 58348a066e7SAl Viro } 584119e1ef8SAl Viro lock_mount_hash(); 585119e1ef8SAl Viro if (unlikely(bastard->mnt_flags & MNT_DOOMED)) { 586119e1ef8SAl Viro mnt_add_count(mnt, -1); 587119e1ef8SAl Viro unlock_mount_hash(); 588119e1ef8SAl Viro return 1; 589119e1ef8SAl Viro } 590119e1ef8SAl Viro unlock_mount_hash(); 591119e1ef8SAl Viro /* caller will mntput() */ 592294d71ffSAl Viro return -1; 593294d71ffSAl Viro } 594294d71ffSAl Viro 595294d71ffSAl Viro /* call under rcu_read_lock */ 596294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 597294d71ffSAl Viro { 598294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 599294d71ffSAl Viro if (likely(!res)) 600294d71ffSAl Viro return true; 601294d71ffSAl Viro if (unlikely(res < 0)) { 60248a066e7SAl Viro rcu_read_unlock(); 60348a066e7SAl Viro mntput(bastard); 60448a066e7SAl Viro rcu_read_lock(); 605294d71ffSAl Viro } 60648a066e7SAl Viro return false; 60748a066e7SAl Viro } 60848a066e7SAl Viro 6091da177e4SLinus Torvalds /* 610474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 61148a066e7SAl Viro * call under rcu_read_lock() 6121da177e4SLinus Torvalds */ 613474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6141da177e4SLinus Torvalds { 61538129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 616474279dcSAl Viro struct mount *p; 6171da177e4SLinus Torvalds 61838129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 619474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 620474279dcSAl Viro return p; 621474279dcSAl Viro return NULL; 6221da177e4SLinus Torvalds } 623474279dcSAl Viro 624474279dcSAl Viro /* 625f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 626f015f126SDavid Howells * 627f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 628f015f126SDavid Howells * following mounts: 629f015f126SDavid Howells * 630f015f126SDavid Howells * mount /dev/sda1 /mnt 631f015f126SDavid Howells * mount /dev/sda2 /mnt 632f015f126SDavid Howells * mount /dev/sda3 /mnt 633f015f126SDavid Howells * 634f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 635f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 636f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 637f015f126SDavid Howells * 638f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 639a05964f3SRam Pai */ 640ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 641a05964f3SRam Pai { 642c7105365SAl Viro struct mount *child_mnt; 64348a066e7SAl Viro struct vfsmount *m; 64448a066e7SAl Viro unsigned seq; 64599b7db7bSNick Piggin 64648a066e7SAl Viro rcu_read_lock(); 64748a066e7SAl Viro do { 64848a066e7SAl Viro seq = read_seqbegin(&mount_lock); 649474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 65048a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 65148a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 65248a066e7SAl Viro rcu_read_unlock(); 65348a066e7SAl Viro return m; 654a05964f3SRam Pai } 655a05964f3SRam Pai 6567af1364fSEric W. Biederman /* 6577af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 6587af1364fSEric W. Biederman * current mount namespace. 6597af1364fSEric W. Biederman * 6607af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 6617af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 6627af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 6637af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 6647af1364fSEric W. Biederman * is a mountpoint. 6657af1364fSEric W. Biederman * 6667af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 6677af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 6687af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 6697af1364fSEric W. Biederman * parent mount. 6707af1364fSEric W. Biederman */ 6717af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 6727af1364fSEric W. Biederman { 6737af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 6747af1364fSEric W. Biederman struct mount *mnt; 6757af1364fSEric W. Biederman bool is_covered = false; 6767af1364fSEric W. Biederman 6777af1364fSEric W. Biederman if (!d_mountpoint(dentry)) 6787af1364fSEric W. Biederman goto out; 6797af1364fSEric W. Biederman 6807af1364fSEric W. Biederman down_read(&namespace_sem); 6817af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 6827af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 6837af1364fSEric W. Biederman if (is_covered) 6847af1364fSEric W. Biederman break; 6857af1364fSEric W. Biederman } 6867af1364fSEric W. Biederman up_read(&namespace_sem); 6877af1364fSEric W. Biederman out: 6887af1364fSEric W. Biederman return is_covered; 6897af1364fSEric W. Biederman } 6907af1364fSEric W. Biederman 691e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 69284d17192SAl Viro { 6930818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 69484d17192SAl Viro struct mountpoint *mp; 69584d17192SAl Viro 6960818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 69784d17192SAl Viro if (mp->m_dentry == dentry) { 69884d17192SAl Viro /* might be worth a WARN_ON() */ 69984d17192SAl Viro if (d_unlinked(dentry)) 70084d17192SAl Viro return ERR_PTR(-ENOENT); 70184d17192SAl Viro mp->m_count++; 70284d17192SAl Viro return mp; 70384d17192SAl Viro } 70484d17192SAl Viro } 705e2dfa935SEric W. Biederman return NULL; 706e2dfa935SEric W. Biederman } 707e2dfa935SEric W. Biederman 7083895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 709e2dfa935SEric W. Biederman { 7103895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 711e2dfa935SEric W. Biederman int ret; 71284d17192SAl Viro 7133895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7143895dbf8SEric W. Biederman mountpoint: 7153895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7163895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 7173895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7183895dbf8SEric W. Biederman if (mp) 7193895dbf8SEric W. Biederman goto done; 72084d17192SAl Viro } 721eed81007SMiklos Szeredi 7223895dbf8SEric W. Biederman if (!new) 7233895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 7243895dbf8SEric W. Biederman if (!new) 7253895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 7263895dbf8SEric W. Biederman 7273895dbf8SEric W. Biederman 7283895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 7293895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 7303895dbf8SEric W. Biederman 7313895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 7323895dbf8SEric W. Biederman if (ret == -EBUSY) 7333895dbf8SEric W. Biederman goto mountpoint; 7343895dbf8SEric W. Biederman 7353895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 7363895dbf8SEric W. Biederman mp = ERR_PTR(ret); 7373895dbf8SEric W. Biederman if (ret) 7383895dbf8SEric W. Biederman goto done; 7393895dbf8SEric W. Biederman 7403895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 7413895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7423895dbf8SEric W. Biederman new->m_dentry = dentry; 7433895dbf8SEric W. Biederman new->m_count = 1; 7443895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 7453895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 7463895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7473895dbf8SEric W. Biederman 7483895dbf8SEric W. Biederman mp = new; 7493895dbf8SEric W. Biederman new = NULL; 7503895dbf8SEric W. Biederman done: 7513895dbf8SEric W. Biederman kfree(new); 75284d17192SAl Viro return mp; 75384d17192SAl Viro } 75484d17192SAl Viro 75584d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 75684d17192SAl Viro { 75784d17192SAl Viro if (!--mp->m_count) { 75884d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7590a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 76084d17192SAl Viro spin_lock(&dentry->d_lock); 76184d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 76284d17192SAl Viro spin_unlock(&dentry->d_lock); 7630818bf27SAl Viro hlist_del(&mp->m_hash); 76484d17192SAl Viro kfree(mp); 76584d17192SAl Viro } 76684d17192SAl Viro } 76784d17192SAl Viro 768143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7691da177e4SLinus Torvalds { 7706b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 77399b7db7bSNick Piggin /* 77499b7db7bSNick Piggin * vfsmount lock must be held for write 77599b7db7bSNick Piggin */ 7766b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7775addc5ddSAl Viro { 7785addc5ddSAl Viro if (ns) { 7795addc5ddSAl Viro ns->event = ++event; 7805addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7815addc5ddSAl Viro } 7825addc5ddSAl Viro } 7835addc5ddSAl Viro 78499b7db7bSNick Piggin /* 78599b7db7bSNick Piggin * vfsmount lock must be held for write 78699b7db7bSNick Piggin */ 7876b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7885addc5ddSAl Viro { 7895addc5ddSAl Viro if (ns && ns->event != event) { 7905addc5ddSAl Viro ns->event = event; 7915addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7925addc5ddSAl Viro } 7935addc5ddSAl Viro } 7945addc5ddSAl Viro 79599b7db7bSNick Piggin /* 79699b7db7bSNick Piggin * vfsmount lock must be held for write 79799b7db7bSNick Piggin */ 7987bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt) 7991da177e4SLinus Torvalds { 8000714a533SAl Viro mnt->mnt_parent = mnt; 801a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8026b41d536SAl Viro list_del_init(&mnt->mnt_child); 80338129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8040a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 80584d17192SAl Viro put_mountpoint(mnt->mnt_mp); 80684d17192SAl Viro mnt->mnt_mp = NULL; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 80999b7db7bSNick Piggin /* 81099b7db7bSNick Piggin * vfsmount lock must be held for write 81199b7db7bSNick Piggin */ 8127bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path) 8137bdb11deSEric W. Biederman { 8147bdb11deSEric W. Biederman old_path->dentry = mnt->mnt_mountpoint; 8157bdb11deSEric W. Biederman old_path->mnt = &mnt->mnt_parent->mnt; 8167bdb11deSEric W. Biederman unhash_mnt(mnt); 8177bdb11deSEric W. Biederman } 8187bdb11deSEric W. Biederman 8197bdb11deSEric W. Biederman /* 8207bdb11deSEric W. Biederman * vfsmount lock must be held for write 8217bdb11deSEric W. Biederman */ 8226a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 8236a46c573SEric W. Biederman { 8246a46c573SEric W. Biederman /* old mountpoint will be dropped when we can do that */ 8256a46c573SEric W. Biederman mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint; 8266a46c573SEric W. Biederman unhash_mnt(mnt); 8276a46c573SEric W. Biederman } 8286a46c573SEric W. Biederman 8296a46c573SEric W. Biederman /* 8306a46c573SEric W. Biederman * vfsmount lock must be held for write 8316a46c573SEric W. Biederman */ 83284d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 83384d17192SAl Viro struct mountpoint *mp, 83444d964d6SAl Viro struct mount *child_mnt) 835b90fa9aeSRam Pai { 83684d17192SAl Viro mp->m_count++; 8373a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 83884d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 8393a2393d7SAl Viro child_mnt->mnt_parent = mnt; 84084d17192SAl Viro child_mnt->mnt_mp = mp; 8410a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 842b90fa9aeSRam Pai } 843b90fa9aeSRam Pai 8441064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 8451064f874SEric W. Biederman { 8461064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 8471064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8481064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8491064f874SEric W. Biederman } 8501064f874SEric W. Biederman 85199b7db7bSNick Piggin /* 85299b7db7bSNick Piggin * vfsmount lock must be held for write 85399b7db7bSNick Piggin */ 85484d17192SAl Viro static void attach_mnt(struct mount *mnt, 85584d17192SAl Viro struct mount *parent, 85684d17192SAl Viro struct mountpoint *mp) 8571da177e4SLinus Torvalds { 85884d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 8591064f874SEric W. Biederman __attach_mnt(mnt, parent); 860b90fa9aeSRam Pai } 861b90fa9aeSRam Pai 8621064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 86312a5b529SAl Viro { 8641064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 8651064f874SEric W. Biederman struct dentry *old_mountpoint = mnt->mnt_mountpoint; 8661064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 8671064f874SEric W. Biederman 8681064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 8691064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 8701064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 8711064f874SEric W. Biederman 8721064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 8731064f874SEric W. Biederman 8741064f874SEric W. Biederman put_mountpoint(old_mp); 8751064f874SEric W. Biederman 8761064f874SEric W. Biederman /* 8771064f874SEric W. Biederman * Safely avoid even the suggestion this code might sleep or 8781064f874SEric W. Biederman * lock the mount hash by taking advantage of the knowledge that 8791064f874SEric W. Biederman * mnt_change_mountpoint will not release the final reference 8801064f874SEric W. Biederman * to a mountpoint. 8811064f874SEric W. Biederman * 8821064f874SEric W. Biederman * During mounting, the mount passed in as the parent mount will 8831064f874SEric W. Biederman * continue to use the old mountpoint and during unmounting, the 8841064f874SEric W. Biederman * old mountpoint will continue to exist until namespace_unlock, 8851064f874SEric W. Biederman * which happens well after mnt_change_mountpoint. 8861064f874SEric W. Biederman */ 8871064f874SEric W. Biederman spin_lock(&old_mountpoint->d_lock); 8881064f874SEric W. Biederman old_mountpoint->d_lockref.count--; 8891064f874SEric W. Biederman spin_unlock(&old_mountpoint->d_lock); 8901064f874SEric W. Biederman 8911064f874SEric W. Biederman mnt_add_count(old_parent, -1); 89212a5b529SAl Viro } 89312a5b529SAl Viro 894b90fa9aeSRam Pai /* 89599b7db7bSNick Piggin * vfsmount lock must be held for write 896b90fa9aeSRam Pai */ 8971064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 898b90fa9aeSRam Pai { 8990714a533SAl Viro struct mount *parent = mnt->mnt_parent; 90083adc753SAl Viro struct mount *m; 901b90fa9aeSRam Pai LIST_HEAD(head); 902143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 903b90fa9aeSRam Pai 9040714a533SAl Viro BUG_ON(parent == mnt); 905b90fa9aeSRam Pai 9061a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 907f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 908143c8c91SAl Viro m->mnt_ns = n; 909f03c6599SAl Viro 910b90fa9aeSRam Pai list_splice(&head, n->list.prev); 911b90fa9aeSRam Pai 912d2921684SEric W. Biederman n->mounts += n->pending_mounts; 913d2921684SEric W. Biederman n->pending_mounts = 0; 914d2921684SEric W. Biederman 9151064f874SEric W. Biederman __attach_mnt(mnt, parent); 9166b3286edSKirill Korotaev touch_mnt_namespace(n); 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds 919909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 9201da177e4SLinus Torvalds { 9216b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 9226b41d536SAl Viro if (next == &p->mnt_mounts) { 9231da177e4SLinus Torvalds while (1) { 924909b0a88SAl Viro if (p == root) 9251da177e4SLinus Torvalds return NULL; 9266b41d536SAl Viro next = p->mnt_child.next; 9276b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 9281da177e4SLinus Torvalds break; 9290714a533SAl Viro p = p->mnt_parent; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds } 9326b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 9369676f0c6SRam Pai { 9376b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 9386b41d536SAl Viro while (prev != &p->mnt_mounts) { 9396b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 9406b41d536SAl Viro prev = p->mnt_mounts.prev; 9419676f0c6SRam Pai } 9429676f0c6SRam Pai return p; 9439676f0c6SRam Pai } 9449676f0c6SRam Pai 9459d412a43SAl Viro struct vfsmount * 9469d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 9479d412a43SAl Viro { 948b105e270SAl Viro struct mount *mnt; 9499d412a43SAl Viro struct dentry *root; 9509d412a43SAl Viro 9519d412a43SAl Viro if (!type) 9529d412a43SAl Viro return ERR_PTR(-ENODEV); 9539d412a43SAl Viro 9549d412a43SAl Viro mnt = alloc_vfsmnt(name); 9559d412a43SAl Viro if (!mnt) 9569d412a43SAl Viro return ERR_PTR(-ENOMEM); 9579d412a43SAl Viro 958e462ec50SDavid Howells if (flags & SB_KERNMOUNT) 959b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9609d412a43SAl Viro 9619d412a43SAl Viro root = mount_fs(type, flags, name, data); 9629d412a43SAl Viro if (IS_ERR(root)) { 9638ffcb32eSDavid Howells mnt_free_id(mnt); 9649d412a43SAl Viro free_vfsmnt(mnt); 9659d412a43SAl Viro return ERR_CAST(root); 9669d412a43SAl Viro } 9679d412a43SAl Viro 968b105e270SAl Viro mnt->mnt.mnt_root = root; 969b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 970a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9710714a533SAl Viro mnt->mnt_parent = mnt; 972719ea2fbSAl Viro lock_mount_hash(); 97339f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 974719ea2fbSAl Viro unlock_mount_hash(); 975b105e270SAl Viro return &mnt->mnt; 9769d412a43SAl Viro } 9779d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 9789d412a43SAl Viro 97993faccbbSEric W. Biederman struct vfsmount * 98093faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 98193faccbbSEric W. Biederman const char *name, void *data) 98293faccbbSEric W. Biederman { 98393faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 98493faccbbSEric W. Biederman * through from the parent mount to the submount don't support 98593faccbbSEric W. Biederman * unprivileged mounts with submounts. 98693faccbbSEric W. Biederman */ 98793faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 98893faccbbSEric W. Biederman return ERR_PTR(-EPERM); 98993faccbbSEric W. Biederman 990e462ec50SDavid Howells return vfs_kern_mount(type, SB_SUBMOUNT, name, data); 99193faccbbSEric W. Biederman } 99293faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 99393faccbbSEric W. Biederman 99487129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 99536341f64SRam Pai int flag) 9961da177e4SLinus Torvalds { 99787129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 998be34d1a3SDavid Howells struct mount *mnt; 999be34d1a3SDavid Howells int err; 10001da177e4SLinus Torvalds 1001be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1002be34d1a3SDavid Howells if (!mnt) 1003be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1004be34d1a3SDavid Howells 10057a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 100615169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1007719f5d7fSMiklos Szeredi else 100815169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1009719f5d7fSMiklos Szeredi 101015169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1011be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1012719f5d7fSMiklos Szeredi if (err) 1013719f5d7fSMiklos Szeredi goto out_free; 1014719f5d7fSMiklos Szeredi } 1015719f5d7fSMiklos Szeredi 101616a34adbSAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags; 101716a34adbSAl Viro mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL); 1018132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 10199566d674SEric W. Biederman if (flag & CL_UNPRIVILEGED) { 10209566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_ATIME; 10219566d674SEric W. Biederman 10229566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_READONLY) 1023132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 1024132c94e3SEric W. Biederman 10259566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NODEV) 10269566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NODEV; 10279566d674SEric W. Biederman 10289566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOSUID) 10299566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID; 10309566d674SEric W. Biederman 10319566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOEXEC) 10329566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC; 10339566d674SEric W. Biederman } 10349566d674SEric W. Biederman 10355ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 1036381cacb1SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && 1037381cacb1SEric W. Biederman (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire))) 10385ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 10395ff9d8a6SEric W. Biederman 10401da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1041b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1042b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1043a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10440714a533SAl Viro mnt->mnt_parent = mnt; 1045719ea2fbSAl Viro lock_mount_hash(); 104639f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1047719ea2fbSAl Viro unlock_mount_hash(); 1048b90fa9aeSRam Pai 10497a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 10507a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 10516776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 105232301920SAl Viro mnt->mnt_master = old; 1053fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 10548aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1055fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 10566776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1057d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 10586776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1059d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 10605235d448SAl Viro } else { 10615235d448SAl Viro CLEAR_MNT_SHARED(mnt); 10625afe0022SRam Pai } 1063b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 10640f0afb1dSAl Viro set_mnt_shared(mnt); 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 10671da177e4SLinus Torvalds * as the original if that was on one */ 106836341f64SRam Pai if (flag & CL_EXPIRE) { 10696776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 10706776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 10711da177e4SLinus Torvalds } 1072be34d1a3SDavid Howells 1073cb338d06SAl Viro return mnt; 1074719f5d7fSMiklos Szeredi 1075719f5d7fSMiklos Szeredi out_free: 10768ffcb32eSDavid Howells mnt_free_id(mnt); 1077719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1078be34d1a3SDavid Howells return ERR_PTR(err); 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10819ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 10829ea459e1SAl Viro { 10839ea459e1SAl Viro /* 10849ea459e1SAl Viro * This probably indicates that somebody messed 10859ea459e1SAl Viro * up a mnt_want/drop_write() pair. If this 10869ea459e1SAl Viro * happens, the filesystem was probably unable 10879ea459e1SAl Viro * to make r/w->r/o transitions. 10889ea459e1SAl Viro */ 10899ea459e1SAl Viro /* 10909ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 10919ea459e1SAl Viro * so mnt_get_writers() below is safe. 10929ea459e1SAl Viro */ 10939ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 10949ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 10959ea459e1SAl Viro mnt_pin_kill(mnt); 10969ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 10979ea459e1SAl Viro dput(mnt->mnt.mnt_root); 10989ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 10999ea459e1SAl Viro mnt_free_id(mnt); 11009ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11019ea459e1SAl Viro } 11029ea459e1SAl Viro 11039ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11049ea459e1SAl Viro { 11059ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11069ea459e1SAl Viro } 11079ea459e1SAl Viro 11089ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11099ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11109ea459e1SAl Viro { 11119ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 111229785735SByungchul Park struct mount *m, *t; 11139ea459e1SAl Viro 111429785735SByungchul Park llist_for_each_entry_safe(m, t, node, mnt_llist) 111529785735SByungchul Park cleanup_mnt(m); 11169ea459e1SAl Viro } 11179ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 11189ea459e1SAl Viro 1119900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 11207b7b1aceSAl Viro { 112148a066e7SAl Viro rcu_read_lock(); 11229ea0a46cSAl Viro if (likely(READ_ONCE(mnt->mnt_ns))) { 11239ea0a46cSAl Viro /* 11249ea0a46cSAl Viro * Since we don't do lock_mount_hash() here, 11259ea0a46cSAl Viro * ->mnt_ns can change under us. However, if it's 11269ea0a46cSAl Viro * non-NULL, then there's a reference that won't 11279ea0a46cSAl Viro * be dropped until after an RCU delay done after 11289ea0a46cSAl Viro * turning ->mnt_ns NULL. So if we observe it 11299ea0a46cSAl Viro * non-NULL under rcu_read_lock(), the reference 11309ea0a46cSAl Viro * we are dropping is not the final one. 11319ea0a46cSAl Viro */ 1132aa9c0e07SAl Viro mnt_add_count(mnt, -1); 113348a066e7SAl Viro rcu_read_unlock(); 113499b7db7bSNick Piggin return; 1135b3e19d92SNick Piggin } 1136719ea2fbSAl Viro lock_mount_hash(); 1137119e1ef8SAl Viro /* 1138119e1ef8SAl Viro * make sure that if __legitimize_mnt() has not seen us grab 1139119e1ef8SAl Viro * mount_lock, we'll see their refcount increment here. 1140119e1ef8SAl Viro */ 1141119e1ef8SAl Viro smp_mb(); 11429ea0a46cSAl Viro mnt_add_count(mnt, -1); 1143b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 114448a066e7SAl Viro rcu_read_unlock(); 1145719ea2fbSAl Viro unlock_mount_hash(); 114699b7db7bSNick Piggin return; 114799b7db7bSNick Piggin } 114848a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 114948a066e7SAl Viro rcu_read_unlock(); 115048a066e7SAl Viro unlock_mount_hash(); 115148a066e7SAl Viro return; 115248a066e7SAl Viro } 115348a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 115448a066e7SAl Viro rcu_read_unlock(); 1155962830dfSAndi Kleen 115639f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1157ce07d891SEric W. Biederman 1158ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1159ce07d891SEric W. Biederman struct mount *p, *tmp; 1160ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 1161ce07d891SEric W. Biederman umount_mnt(p); 1162ce07d891SEric W. Biederman } 1163ce07d891SEric W. Biederman } 1164719ea2fbSAl Viro unlock_mount_hash(); 1165649a795aSAl Viro 11669ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 11679ea459e1SAl Viro struct task_struct *task = current; 11689ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 11699ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 11709ea459e1SAl Viro if (!task_work_add(task, &mnt->mnt_rcu, true)) 11719ea459e1SAl Viro return; 11729ea459e1SAl Viro } 11739ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 11749ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 11759ea459e1SAl Viro return; 11769ea459e1SAl Viro } 11779ea459e1SAl Viro cleanup_mnt(mnt); 1178b3e19d92SNick Piggin } 1179b3e19d92SNick Piggin 1180b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1181b3e19d92SNick Piggin { 1182b3e19d92SNick Piggin if (mnt) { 1183863d684fSAl Viro struct mount *m = real_mount(mnt); 1184b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1185863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1186863d684fSAl Viro m->mnt_expiry_mark = 0; 1187863d684fSAl Viro mntput_no_expire(m); 1188b3e19d92SNick Piggin } 1189b3e19d92SNick Piggin } 1190b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1191b3e19d92SNick Piggin 1192b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1193b3e19d92SNick Piggin { 1194b3e19d92SNick Piggin if (mnt) 119583adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1196b3e19d92SNick Piggin return mnt; 1197b3e19d92SNick Piggin } 1198b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1199b3e19d92SNick Piggin 1200c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current 1201c6609c0aSIan Kent * namespace. 1202c6609c0aSIan Kent * 1203c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1204c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1205c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1206c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1207c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1208c6609c0aSIan Kent * alone. 1209c6609c0aSIan Kent */ 1210c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1211c6609c0aSIan Kent { 1212c6609c0aSIan Kent unsigned seq; 1213c6609c0aSIan Kent bool res; 1214c6609c0aSIan Kent 1215c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1216c6609c0aSIan Kent return false; 1217c6609c0aSIan Kent 1218c6609c0aSIan Kent rcu_read_lock(); 1219c6609c0aSIan Kent do { 1220c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1221c6609c0aSIan Kent res = __path_is_mountpoint(path); 1222c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1223c6609c0aSIan Kent rcu_read_unlock(); 1224c6609c0aSIan Kent 1225c6609c0aSIan Kent return res; 1226c6609c0aSIan Kent } 1227c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1228c6609c0aSIan Kent 1229ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 12307b7b1aceSAl Viro { 12313064c356SAl Viro struct mount *p; 12323064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 12333064c356SAl Viro if (IS_ERR(p)) 12343064c356SAl Viro return ERR_CAST(p); 12353064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 12363064c356SAl Viro return &p->mnt; 12377b7b1aceSAl Viro } 12381da177e4SLinus Torvalds 1239a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 12400226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 12411da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 12421da177e4SLinus Torvalds { 1243ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12441da177e4SLinus Torvalds 1245390c6843SRam Pai down_read(&namespace_sem); 1246c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1247c7999c36SAl Viro void *v = p->cached_mount; 1248c7999c36SAl Viro if (*pos == p->cached_index) 1249c7999c36SAl Viro return v; 1250c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1251c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1252c7999c36SAl Viro return p->cached_mount = v; 1253c7999c36SAl Viro } 1254c7999c36SAl Viro } 1255c7999c36SAl Viro 1256c7999c36SAl Viro p->cached_event = p->ns->event; 1257c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1258c7999c36SAl Viro p->cached_index = *pos; 1259c7999c36SAl Viro return p->cached_mount; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 12631da177e4SLinus Torvalds { 1264ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 1265b0765fb8SPavel Emelianov 1266c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1267c7999c36SAl Viro p->cached_index = *pos; 1268c7999c36SAl Viro return p->cached_mount; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 12721da177e4SLinus Torvalds { 1273390c6843SRam Pai up_read(&namespace_sem); 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12760226f492SAl Viro static int m_show(struct seq_file *m, void *v) 12779f5596afSAl Viro { 1278ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12791a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 12800226f492SAl Viro return p->show(m, &r->mnt); 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds 1283a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 12841da177e4SLinus Torvalds .start = m_start, 12851da177e4SLinus Torvalds .next = m_next, 12861da177e4SLinus Torvalds .stop = m_stop, 12870226f492SAl Viro .show = m_show, 1288b4629fe2SChuck Lever }; 1289a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1290b4629fe2SChuck Lever 12911da177e4SLinus Torvalds /** 12921da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 12931da177e4SLinus Torvalds * @mnt: root of mount tree 12941da177e4SLinus Torvalds * 12951da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 12961da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 12971da177e4SLinus Torvalds * busy. 12981da177e4SLinus Torvalds */ 1299909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13001da177e4SLinus Torvalds { 1301909b0a88SAl Viro struct mount *mnt = real_mount(m); 130236341f64SRam Pai int actual_refs = 0; 130336341f64SRam Pai int minimum_refs = 0; 1304315fc83eSAl Viro struct mount *p; 1305909b0a88SAl Viro BUG_ON(!m); 13061da177e4SLinus Torvalds 1307b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1308719ea2fbSAl Viro lock_mount_hash(); 1309909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 131083adc753SAl Viro actual_refs += mnt_get_count(p); 13111da177e4SLinus Torvalds minimum_refs += 2; 13121da177e4SLinus Torvalds } 1313719ea2fbSAl Viro unlock_mount_hash(); 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds if (actual_refs > minimum_refs) 13161da177e4SLinus Torvalds return 0; 1317e3474a8eSIan Kent 1318e3474a8eSIan Kent return 1; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds /** 13241da177e4SLinus Torvalds * may_umount - check if a mount point is busy 13251da177e4SLinus Torvalds * @mnt: root of mount 13261da177e4SLinus Torvalds * 13271da177e4SLinus Torvalds * This is called to check if a mount point has any 13281da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 13291da177e4SLinus Torvalds * mount has sub mounts this will return busy 13301da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 13311da177e4SLinus Torvalds * 13321da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 13331da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 13341da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 13351da177e4SLinus Torvalds */ 13361da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 13371da177e4SLinus Torvalds { 1338e3474a8eSIan Kent int ret = 1; 13398ad08d8aSAl Viro down_read(&namespace_sem); 1340719ea2fbSAl Viro lock_mount_hash(); 13411ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1342e3474a8eSIan Kent ret = 0; 1343719ea2fbSAl Viro unlock_mount_hash(); 13448ad08d8aSAl Viro up_read(&namespace_sem); 1345a05964f3SRam Pai return ret; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 13491da177e4SLinus Torvalds 135038129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1351e3197d83SAl Viro 135297216be0SAl Viro static void namespace_unlock(void) 13531da177e4SLinus Torvalds { 1354a3b3c562SEric W. Biederman struct hlist_head head; 135597216be0SAl Viro 1356a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 1357a3b3c562SEric W. Biederman 135897216be0SAl Viro up_write(&namespace_sem); 1359a3b3c562SEric W. Biederman 1360a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 136197216be0SAl Viro return; 136297216be0SAl Viro 136348a066e7SAl Viro synchronize_rcu(); 136448a066e7SAl Viro 136587b95ce0SAl Viro group_pin_kill(&head); 136670fbcdf4SRam Pai } 136770fbcdf4SRam Pai 136897216be0SAl Viro static inline void namespace_lock(void) 1369e3197d83SAl Viro { 137097216be0SAl Viro down_write(&namespace_sem); 1371e3197d83SAl Viro } 1372e3197d83SAl Viro 1373e819f152SEric W. Biederman enum umount_tree_flags { 1374e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1375e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1376e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1377e819f152SEric W. Biederman }; 1378f2d0a123SEric W. Biederman 1379f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1380f2d0a123SEric W. Biederman { 1381f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1382f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1383f2d0a123SEric W. Biederman return true; 1384f2d0a123SEric W. Biederman 1385f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1386f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1387f2d0a123SEric W. Biederman return true; 1388f2d0a123SEric W. Biederman 1389f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1390f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1391f2d0a123SEric W. Biederman * connected to mounted mounts. 1392f2d0a123SEric W. Biederman */ 1393f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1394f2d0a123SEric W. Biederman return true; 1395f2d0a123SEric W. Biederman 1396f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1397f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1398f2d0a123SEric W. Biederman return false; 1399f2d0a123SEric W. Biederman 1400f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1401f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1402f2d0a123SEric W. Biederman return false; 1403f2d0a123SEric W. Biederman 1404f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1405f2d0a123SEric W. Biederman return true; 1406f2d0a123SEric W. Biederman } 1407f2d0a123SEric W. Biederman 140899b7db7bSNick Piggin /* 140948a066e7SAl Viro * mount_lock must be held 141099b7db7bSNick Piggin * namespace_sem must be held for write 141199b7db7bSNick Piggin */ 1412e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 141370fbcdf4SRam Pai { 1414c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1415315fc83eSAl Viro struct mount *p; 141670fbcdf4SRam Pai 14175d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 14185d88457eSEric W. Biederman propagate_mount_unlock(mnt); 14195d88457eSEric W. Biederman 1420c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1421590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1422590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1423c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1424590ce4bcSEric W. Biederman } 1425c003b26fSEric W. Biederman 1426411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1427c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1428c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 142938129a13SAl Viro } 143070fbcdf4SRam Pai 1431c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1432e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 14337b8a53fdSAl Viro propagate_umount(&tmp_list); 1434a05964f3SRam Pai 1435c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1436d2921684SEric W. Biederman struct mnt_namespace *ns; 1437ce07d891SEric W. Biederman bool disconnect; 1438c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 14396776db3dSAl Viro list_del_init(&p->mnt_expire); 14401a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1441d2921684SEric W. Biederman ns = p->mnt_ns; 1442d2921684SEric W. Biederman if (ns) { 1443d2921684SEric W. Biederman ns->mounts--; 1444d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1445d2921684SEric W. Biederman } 144663d37a84SAl Viro p->mnt_ns = NULL; 1447e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 144848a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 144987b95ce0SAl Viro 1450f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1451ce07d891SEric W. Biederman 1452ce07d891SEric W. Biederman pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt, 1453ce07d891SEric W. Biederman disconnect ? &unmounted : NULL); 1454676da58dSAl Viro if (mnt_has_parent(p)) { 145581b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1456ce07d891SEric W. Biederman if (!disconnect) { 1457ce07d891SEric W. Biederman /* Don't forget about p */ 1458ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1459ce07d891SEric W. Biederman } else { 14606a46c573SEric W. Biederman umount_mnt(p); 14617c4b93d8SAl Viro } 1462ce07d891SEric W. Biederman } 14630f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 146438129a13SAl Viro } 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 1467b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1468c35038beSAl Viro 14691ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 14701da177e4SLinus Torvalds { 14711ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 14721da177e4SLinus Torvalds int retval; 14731da177e4SLinus Torvalds 14741ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 14751da177e4SLinus Torvalds if (retval) 14761da177e4SLinus Torvalds return retval; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /* 14791da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 14801da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 14811da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 14821da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 14831da177e4SLinus Torvalds */ 14841da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 14851ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 14861da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 14871da177e4SLinus Torvalds return -EINVAL; 14881da177e4SLinus Torvalds 1489b3e19d92SNick Piggin /* 1490b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1491b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1492b3e19d92SNick Piggin */ 1493719ea2fbSAl Viro lock_mount_hash(); 149483adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1495719ea2fbSAl Viro unlock_mount_hash(); 14961da177e4SLinus Torvalds return -EBUSY; 1497b3e19d92SNick Piggin } 1498719ea2fbSAl Viro unlock_mount_hash(); 14991da177e4SLinus Torvalds 1500863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 15011da177e4SLinus Torvalds return -EAGAIN; 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /* 15051da177e4SLinus Torvalds * If we may have to abort operations to get out of this 15061da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 15071da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 15081da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 15091da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 15101da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 15111da177e4SLinus Torvalds * about for the moment. 15121da177e4SLinus Torvalds */ 15131da177e4SLinus Torvalds 151442faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 151542faad99SAl Viro sb->s_op->umount_begin(sb); 151642faad99SAl Viro } 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds /* 15191da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 15201da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 15211da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 15221da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 15231da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 15241da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 15251da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 15261da177e4SLinus Torvalds */ 15271ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 15281da177e4SLinus Torvalds /* 15291da177e4SLinus Torvalds * Special case for "unmounting" root ... 15301da177e4SLinus Torvalds * we just try to remount it readonly. 15311da177e4SLinus Torvalds */ 1532bc6155d1SEric W. Biederman if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 1533a1480dccSAndy Lutomirski return -EPERM; 15341da177e4SLinus Torvalds down_write(&sb->s_umount); 1535bc98a42cSDavid Howells if (!sb_rdonly(sb)) 1536e462ec50SDavid Howells retval = do_remount_sb(sb, SB_RDONLY, NULL, 0); 15371da177e4SLinus Torvalds up_write(&sb->s_umount); 15381da177e4SLinus Torvalds return retval; 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 154197216be0SAl Viro namespace_lock(); 1542719ea2fbSAl Viro lock_mount_hash(); 15435addc5ddSAl Viro event++; 15441da177e4SLinus Torvalds 154548a066e7SAl Viro if (flags & MNT_DETACH) { 154648a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1547e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 154848a066e7SAl Viro retval = 0; 154948a066e7SAl Viro } else { 1550b54b9be7SAl Viro shrink_submounts(mnt); 15511da177e4SLinus Torvalds retval = -EBUSY; 155248a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 15531a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1554e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 15551da177e4SLinus Torvalds retval = 0; 15561da177e4SLinus Torvalds } 155748a066e7SAl Viro } 1558719ea2fbSAl Viro unlock_mount_hash(); 1559e3197d83SAl Viro namespace_unlock(); 15601da177e4SLinus Torvalds return retval; 15611da177e4SLinus Torvalds } 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* 156480b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 156580b5dce8SEric W. Biederman * 156680b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 156780b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 156880b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 156980b5dce8SEric W. Biederman * leaking them. 157080b5dce8SEric W. Biederman * 157180b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 157280b5dce8SEric W. Biederman */ 157380b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 157480b5dce8SEric W. Biederman { 157580b5dce8SEric W. Biederman struct mountpoint *mp; 157680b5dce8SEric W. Biederman struct mount *mnt; 157780b5dce8SEric W. Biederman 157880b5dce8SEric W. Biederman namespace_lock(); 15793895dbf8SEric W. Biederman lock_mount_hash(); 158080b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1581f53e5797SEric W. Biederman if (IS_ERR_OR_NULL(mp)) 158280b5dce8SEric W. Biederman goto out_unlock; 158380b5dce8SEric W. Biederman 1584e06b933eSAndrey Ulanov event++; 158580b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 158680b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1587ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1588fe78fcc8SEric W. Biederman hlist_add_head(&mnt->mnt_umount.s_list, &unmounted); 1589fe78fcc8SEric W. Biederman umount_mnt(mnt); 1590ce07d891SEric W. Biederman } 1591e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 159280b5dce8SEric W. Biederman } 159380b5dce8SEric W. Biederman put_mountpoint(mp); 159480b5dce8SEric W. Biederman out_unlock: 15953895dbf8SEric W. Biederman unlock_mount_hash(); 159680b5dce8SEric W. Biederman namespace_unlock(); 159780b5dce8SEric W. Biederman } 159880b5dce8SEric W. Biederman 159980b5dce8SEric W. Biederman /* 16009b40bc90SAl Viro * Is the caller allowed to modify his namespace? 16019b40bc90SAl Viro */ 16029b40bc90SAl Viro static inline bool may_mount(void) 16039b40bc90SAl Viro { 16049b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 16059b40bc90SAl Viro } 16069b40bc90SAl Viro 16079e8925b6SJeff Layton static inline bool may_mandlock(void) 16089e8925b6SJeff Layton { 16099e8925b6SJeff Layton #ifndef CONFIG_MANDATORY_FILE_LOCKING 16109e8925b6SJeff Layton return false; 16119e8925b6SJeff Layton #endif 161295ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 16139e8925b6SJeff Layton } 16149e8925b6SJeff Layton 16159b40bc90SAl Viro /* 16161da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 16171da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 16181da177e4SLinus Torvalds * 16191da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 16201da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 16211da177e4SLinus Torvalds */ 16221da177e4SLinus Torvalds 16233a18ef5cSDominik Brodowski int ksys_umount(char __user *name, int flags) 16241da177e4SLinus Torvalds { 16252d8f3038SAl Viro struct path path; 1626900148dcSAl Viro struct mount *mnt; 16271da177e4SLinus Torvalds int retval; 1628db1f05bbSMiklos Szeredi int lookup_flags = 0; 16291da177e4SLinus Torvalds 1630db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1631db1f05bbSMiklos Szeredi return -EINVAL; 1632db1f05bbSMiklos Szeredi 16339b40bc90SAl Viro if (!may_mount()) 16349b40bc90SAl Viro return -EPERM; 16359b40bc90SAl Viro 1636db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1637db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1638db1f05bbSMiklos Szeredi 1639197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 16401da177e4SLinus Torvalds if (retval) 16411da177e4SLinus Torvalds goto out; 1642900148dcSAl Viro mnt = real_mount(path.mnt); 16431da177e4SLinus Torvalds retval = -EINVAL; 16442d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 16451da177e4SLinus Torvalds goto dput_and_out; 1646143c8c91SAl Viro if (!check_mnt(mnt)) 16471da177e4SLinus Torvalds goto dput_and_out; 16485ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 16495ff9d8a6SEric W. Biederman goto dput_and_out; 1650b2f5d4dcSEric W. Biederman retval = -EPERM; 1651b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 1652b2f5d4dcSEric W. Biederman goto dput_and_out; 16531da177e4SLinus Torvalds 1654900148dcSAl Viro retval = do_umount(mnt, flags); 16551da177e4SLinus Torvalds dput_and_out: 1656429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 16572d8f3038SAl Viro dput(path.dentry); 1658900148dcSAl Viro mntput_no_expire(mnt); 16591da177e4SLinus Torvalds out: 16601da177e4SLinus Torvalds return retval; 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 16633a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 16643a18ef5cSDominik Brodowski { 16653a18ef5cSDominik Brodowski return ksys_umount(name, flags); 16663a18ef5cSDominik Brodowski } 16673a18ef5cSDominik Brodowski 16681da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* 16711da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 16721da177e4SLinus Torvalds */ 1673bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 16741da177e4SLinus Torvalds { 16753a18ef5cSDominik Brodowski return ksys_umount(name, 0); 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds #endif 16791da177e4SLinus Torvalds 16804ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 16818823c079SEric W. Biederman { 16824ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1683e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1684e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 16854ce5d2b1SEric W. Biederman } 16864ce5d2b1SEric W. Biederman 168758be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 168858be2825SAl Viro { 168958be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 169058be2825SAl Viro } 169158be2825SAl Viro 16924ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 16934ce5d2b1SEric W. Biederman { 16944ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 16954ce5d2b1SEric W. Biederman * mount namespace loop? 16964ce5d2b1SEric W. Biederman */ 16974ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 16984ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 16994ce5d2b1SEric W. Biederman return false; 17004ce5d2b1SEric W. Biederman 1701f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 17028823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 17038823c079SEric W. Biederman } 17048823c079SEric W. Biederman 170587129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 170636341f64SRam Pai int flag) 17071da177e4SLinus Torvalds { 170884d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 17091da177e4SLinus Torvalds 17104ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 17114ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 17124ce5d2b1SEric W. Biederman 17134ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1714be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 17159676f0c6SRam Pai 171636341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1717be34d1a3SDavid Howells if (IS_ERR(q)) 1718be34d1a3SDavid Howells return q; 1719be34d1a3SDavid Howells 1720a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds p = mnt; 17236b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1724315fc83eSAl Viro struct mount *s; 17257ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 17261da177e4SLinus Torvalds continue; 17271da177e4SLinus Torvalds 1728909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 17294ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 17304ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 17314ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 17324ce5d2b1SEric W. Biederman continue; 17334ce5d2b1SEric W. Biederman } 17344ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 17354ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 17369676f0c6SRam Pai s = skip_mnt_tree(s); 17379676f0c6SRam Pai continue; 17389676f0c6SRam Pai } 17390714a533SAl Viro while (p != s->mnt_parent) { 17400714a533SAl Viro p = p->mnt_parent; 17410714a533SAl Viro q = q->mnt_parent; 17421da177e4SLinus Torvalds } 174387129cc0SAl Viro p = s; 174484d17192SAl Viro parent = q; 174587129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1746be34d1a3SDavid Howells if (IS_ERR(q)) 1747be34d1a3SDavid Howells goto out; 1748719ea2fbSAl Viro lock_mount_hash(); 17491a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 17501064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1751719ea2fbSAl Viro unlock_mount_hash(); 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds return res; 1755be34d1a3SDavid Howells out: 17561da177e4SLinus Torvalds if (res) { 1757719ea2fbSAl Viro lock_mount_hash(); 1758e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1759719ea2fbSAl Viro unlock_mount_hash(); 17601da177e4SLinus Torvalds } 1761be34d1a3SDavid Howells return q; 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 1764be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1765be34d1a3SDavid Howells 1766ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 17678aec0809SAl Viro { 1768cb338d06SAl Viro struct mount *tree; 176997216be0SAl Viro namespace_lock(); 1770cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1771cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1772cd4a4017SEric W. Biederman else 177387129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 177487129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1775328e6d90SAl Viro namespace_unlock(); 1776be34d1a3SDavid Howells if (IS_ERR(tree)) 177752e220d3SDan Carpenter return ERR_CAST(tree); 1778be34d1a3SDavid Howells return &tree->mnt; 17798aec0809SAl Viro } 17808aec0809SAl Viro 17818aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 17828aec0809SAl Viro { 178397216be0SAl Viro namespace_lock(); 1784719ea2fbSAl Viro lock_mount_hash(); 1785e819f152SEric W. Biederman umount_tree(real_mount(mnt), UMOUNT_SYNC); 1786719ea2fbSAl Viro unlock_mount_hash(); 17873ab6abeeSAl Viro namespace_unlock(); 17888aec0809SAl Viro } 17898aec0809SAl Viro 1790c771d683SMiklos Szeredi /** 1791c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 1792c771d683SMiklos Szeredi * 1793c771d683SMiklos Szeredi * This creates a new vfsmount, which will be the clone of @path. The new will 1794c771d683SMiklos Szeredi * not be attached anywhere in the namespace and will be private (i.e. changes 1795c771d683SMiklos Szeredi * to the originating mount won't be propagated into this). 1796c771d683SMiklos Szeredi * 1797c771d683SMiklos Szeredi * Release with mntput(). 1798c771d683SMiklos Szeredi */ 1799ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1800c771d683SMiklos Szeredi { 1801c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1802c771d683SMiklos Szeredi struct mount *new_mnt; 1803c771d683SMiklos Szeredi 1804c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1805c771d683SMiklos Szeredi return ERR_PTR(-EINVAL); 1806c771d683SMiklos Szeredi 1807c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1808c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1809c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1810c771d683SMiklos Szeredi 1811c771d683SMiklos Szeredi return &new_mnt->mnt; 1812c771d683SMiklos Szeredi } 1813c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 1814c771d683SMiklos Szeredi 18151f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 18161f707137SAl Viro struct vfsmount *root) 18171f707137SAl Viro { 18181a4eeaf2SAl Viro struct mount *mnt; 18191f707137SAl Viro int res = f(root, arg); 18201f707137SAl Viro if (res) 18211f707137SAl Viro return res; 18221a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 18231a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 18241f707137SAl Viro if (res) 18251f707137SAl Viro return res; 18261f707137SAl Viro } 18271f707137SAl Viro return 0; 18281f707137SAl Viro } 18291f707137SAl Viro 18304b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1831719f5d7fSMiklos Szeredi { 1832315fc83eSAl Viro struct mount *p; 1833719f5d7fSMiklos Szeredi 1834909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1835fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 18364b8b21f4SAl Viro mnt_release_group_id(p); 1837719f5d7fSMiklos Szeredi } 1838719f5d7fSMiklos Szeredi } 1839719f5d7fSMiklos Szeredi 18404b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1841719f5d7fSMiklos Szeredi { 1842315fc83eSAl Viro struct mount *p; 1843719f5d7fSMiklos Szeredi 1844909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1845fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 18464b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1847719f5d7fSMiklos Szeredi if (err) { 18484b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1849719f5d7fSMiklos Szeredi return err; 1850719f5d7fSMiklos Szeredi } 1851719f5d7fSMiklos Szeredi } 1852719f5d7fSMiklos Szeredi } 1853719f5d7fSMiklos Szeredi 1854719f5d7fSMiklos Szeredi return 0; 1855719f5d7fSMiklos Szeredi } 1856719f5d7fSMiklos Szeredi 1857d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 1858d2921684SEric W. Biederman { 1859d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 1860d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 1861d2921684SEric W. Biederman struct mount *p; 1862d2921684SEric W. Biederman 1863d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 1864d2921684SEric W. Biederman mounts++; 1865d2921684SEric W. Biederman 1866d2921684SEric W. Biederman old = ns->mounts; 1867d2921684SEric W. Biederman pending = ns->pending_mounts; 1868d2921684SEric W. Biederman sum = old + pending; 1869d2921684SEric W. Biederman if ((old > sum) || 1870d2921684SEric W. Biederman (pending > sum) || 1871d2921684SEric W. Biederman (max < sum) || 1872d2921684SEric W. Biederman (mounts > (max - sum))) 1873d2921684SEric W. Biederman return -ENOSPC; 1874d2921684SEric W. Biederman 1875d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 1876d2921684SEric W. Biederman return 0; 1877d2921684SEric W. Biederman } 1878d2921684SEric W. Biederman 1879b90fa9aeSRam Pai /* 1880b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1881b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 188221444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 188321444403SRam Pai * store the parent mount and mountpoint dentry. 188421444403SRam Pai * (done when source_mnt is moved) 1885b90fa9aeSRam Pai * 1886b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1887b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 18889676f0c6SRam Pai * --------------------------------------------------------------------------- 1889b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 18909676f0c6SRam Pai * |************************************************************************** 18919676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 18929676f0c6SRam Pai * | dest | | | | | 18939676f0c6SRam Pai * | | | | | | | 18949676f0c6SRam Pai * | v | | | | | 18959676f0c6SRam Pai * |************************************************************************** 18969676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 18975afe0022SRam Pai * | | | | | | 18989676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 18999676f0c6SRam Pai * *************************************************************************** 1900b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1901b90fa9aeSRam Pai * destination mount. 1902b90fa9aeSRam Pai * 1903b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1904b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1905b90fa9aeSRam Pai * the peer group of the source mount. 1906b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1907b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1908b90fa9aeSRam Pai * mount. 19095afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 19105afe0022SRam Pai * of the destination mount and the cloned mount is made slave 19115afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 19125afe0022SRam Pai * is marked as 'shared and slave'. 19135afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 19145afe0022SRam Pai * source mount. 19155afe0022SRam Pai * 19169676f0c6SRam Pai * --------------------------------------------------------------------------- 191721444403SRam Pai * | MOVE MOUNT OPERATION | 19189676f0c6SRam Pai * |************************************************************************** 19199676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 19209676f0c6SRam Pai * | dest | | | | | 19219676f0c6SRam Pai * | | | | | | | 19229676f0c6SRam Pai * | v | | | | | 19239676f0c6SRam Pai * |************************************************************************** 19249676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 19255afe0022SRam Pai * | | | | | | 19269676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 19279676f0c6SRam Pai * *************************************************************************** 19285afe0022SRam Pai * 19295afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 19305afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 193121444403SRam Pai * (+*) the mount is moved to the destination. 19325afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 19335afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 19345afe0022SRam Pai * the mount is marked as 'shared and slave'. 19355afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1936b90fa9aeSRam Pai * 1937b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1938b90fa9aeSRam Pai * applied to each mount in the tree. 1939b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1940b90fa9aeSRam Pai * in allocations. 1941b90fa9aeSRam Pai */ 19420fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 194384d17192SAl Viro struct mount *dest_mnt, 194484d17192SAl Viro struct mountpoint *dest_mp, 194584d17192SAl Viro struct path *parent_path) 1946b90fa9aeSRam Pai { 194738129a13SAl Viro HLIST_HEAD(tree_list); 1948d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 19491064f874SEric W. Biederman struct mountpoint *smp; 1950315fc83eSAl Viro struct mount *child, *p; 195138129a13SAl Viro struct hlist_node *n; 1952719f5d7fSMiklos Szeredi int err; 1953b90fa9aeSRam Pai 19541064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 19551064f874SEric W. Biederman * to be tucked under other mounts. 19561064f874SEric W. Biederman */ 19571064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 19581064f874SEric W. Biederman if (IS_ERR(smp)) 19591064f874SEric W. Biederman return PTR_ERR(smp); 19601064f874SEric W. Biederman 1961d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 1962d2921684SEric W. Biederman if (!parent_path) { 1963d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 1964d2921684SEric W. Biederman if (err) 1965d2921684SEric W. Biederman goto out; 1966d2921684SEric W. Biederman } 1967d2921684SEric W. Biederman 1968fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 19690fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1970719f5d7fSMiklos Szeredi if (err) 1971719f5d7fSMiklos Szeredi goto out; 197284d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 1973f2ebb3a9SAl Viro lock_mount_hash(); 1974719f5d7fSMiklos Szeredi if (err) 1975719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1976909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 19770f0afb1dSAl Viro set_mnt_shared(p); 19780b1b901bSAl Viro } else { 19790b1b901bSAl Viro lock_mount_hash(); 1980b90fa9aeSRam Pai } 19811a390689SAl Viro if (parent_path) { 19820fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 198384d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 1984143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 198521444403SRam Pai } else { 198684d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 19871064f874SEric W. Biederman commit_tree(source_mnt); 198821444403SRam Pai } 1989b90fa9aeSRam Pai 199038129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 19911d6a32acSAl Viro struct mount *q; 199238129a13SAl Viro hlist_del_init(&child->mnt_hash); 19931064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 19941d6a32acSAl Viro child->mnt_mountpoint); 19951064f874SEric W. Biederman if (q) 19961064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 19971064f874SEric W. Biederman commit_tree(child); 1998b90fa9aeSRam Pai } 19991064f874SEric W. Biederman put_mountpoint(smp); 2000719ea2fbSAl Viro unlock_mount_hash(); 200199b7db7bSNick Piggin 2002b90fa9aeSRam Pai return 0; 2003719f5d7fSMiklos Szeredi 2004719f5d7fSMiklos Szeredi out_cleanup_ids: 2005f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2006f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2007d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2008e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2009f2ebb3a9SAl Viro } 2010f2ebb3a9SAl Viro unlock_mount_hash(); 20110fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2012719f5d7fSMiklos Szeredi out: 2013d2921684SEric W. Biederman ns->pending_mounts = 0; 20141064f874SEric W. Biederman 20151064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 20161064f874SEric W. Biederman put_mountpoint(smp); 20171064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 20181064f874SEric W. Biederman 2019719f5d7fSMiklos Szeredi return err; 2020b90fa9aeSRam Pai } 2021b90fa9aeSRam Pai 202284d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2023b12cea91SAl Viro { 2024b12cea91SAl Viro struct vfsmount *mnt; 202584d17192SAl Viro struct dentry *dentry = path->dentry; 2026b12cea91SAl Viro retry: 20275955102cSAl Viro inode_lock(dentry->d_inode); 202884d17192SAl Viro if (unlikely(cant_mount(dentry))) { 20295955102cSAl Viro inode_unlock(dentry->d_inode); 203084d17192SAl Viro return ERR_PTR(-ENOENT); 2031b12cea91SAl Viro } 203297216be0SAl Viro namespace_lock(); 2033b12cea91SAl Viro mnt = lookup_mnt(path); 203484d17192SAl Viro if (likely(!mnt)) { 20353895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 203684d17192SAl Viro if (IS_ERR(mp)) { 203797216be0SAl Viro namespace_unlock(); 20385955102cSAl Viro inode_unlock(dentry->d_inode); 203984d17192SAl Viro return mp; 204084d17192SAl Viro } 204184d17192SAl Viro return mp; 204284d17192SAl Viro } 204397216be0SAl Viro namespace_unlock(); 20445955102cSAl Viro inode_unlock(path->dentry->d_inode); 2045b12cea91SAl Viro path_put(path); 2046b12cea91SAl Viro path->mnt = mnt; 204784d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2048b12cea91SAl Viro goto retry; 2049b12cea91SAl Viro } 2050b12cea91SAl Viro 205184d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2052b12cea91SAl Viro { 205384d17192SAl Viro struct dentry *dentry = where->m_dentry; 20543895dbf8SEric W. Biederman 20553895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 205684d17192SAl Viro put_mountpoint(where); 20573895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 20583895dbf8SEric W. Biederman 2059328e6d90SAl Viro namespace_unlock(); 20605955102cSAl Viro inode_unlock(dentry->d_inode); 2061b12cea91SAl Viro } 2062b12cea91SAl Viro 206384d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 20641da177e4SLinus Torvalds { 2065e462ec50SDavid Howells if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER) 20661da177e4SLinus Torvalds return -EINVAL; 20671da177e4SLinus Torvalds 2068e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2069e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 20701da177e4SLinus Torvalds return -ENOTDIR; 20711da177e4SLinus Torvalds 207284d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds /* 20767a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 20777a2e8a8fSValerie Aurora */ 20787a2e8a8fSValerie Aurora 2079e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags) 20807a2e8a8fSValerie Aurora { 2081e462ec50SDavid Howells int type = ms_flags & ~(MS_REC | MS_SILENT); 20827a2e8a8fSValerie Aurora 20837a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 20847a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 20857a2e8a8fSValerie Aurora return 0; 20867a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 20877a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 20887a2e8a8fSValerie Aurora return 0; 20897a2e8a8fSValerie Aurora return type; 20907a2e8a8fSValerie Aurora } 20917a2e8a8fSValerie Aurora 20927a2e8a8fSValerie Aurora /* 209307b20889SRam Pai * recursively change the type of the mountpoint. 209407b20889SRam Pai */ 2095e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags) 209607b20889SRam Pai { 2097315fc83eSAl Viro struct mount *m; 20984b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 2099e462ec50SDavid Howells int recurse = ms_flags & MS_REC; 21007a2e8a8fSValerie Aurora int type; 2101719f5d7fSMiklos Szeredi int err = 0; 210207b20889SRam Pai 21032d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 210407b20889SRam Pai return -EINVAL; 210507b20889SRam Pai 2106e462ec50SDavid Howells type = flags_to_propagation_type(ms_flags); 21077a2e8a8fSValerie Aurora if (!type) 21087a2e8a8fSValerie Aurora return -EINVAL; 21097a2e8a8fSValerie Aurora 211097216be0SAl Viro namespace_lock(); 2111719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2112719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2113719f5d7fSMiklos Szeredi if (err) 2114719f5d7fSMiklos Szeredi goto out_unlock; 2115719f5d7fSMiklos Szeredi } 2116719f5d7fSMiklos Szeredi 2117719ea2fbSAl Viro lock_mount_hash(); 2118909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 21190f0afb1dSAl Viro change_mnt_propagation(m, type); 2120719ea2fbSAl Viro unlock_mount_hash(); 2121719f5d7fSMiklos Szeredi 2122719f5d7fSMiklos Szeredi out_unlock: 212397216be0SAl Viro namespace_unlock(); 2124719f5d7fSMiklos Szeredi return err; 212507b20889SRam Pai } 212607b20889SRam Pai 21275ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 21285ff9d8a6SEric W. Biederman { 21295ff9d8a6SEric W. Biederman struct mount *child; 21305ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 21315ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 21325ff9d8a6SEric W. Biederman continue; 21335ff9d8a6SEric W. Biederman 21345ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 21355ff9d8a6SEric W. Biederman return true; 21365ff9d8a6SEric W. Biederman } 21375ff9d8a6SEric W. Biederman return false; 21385ff9d8a6SEric W. Biederman } 21395ff9d8a6SEric W. Biederman 214007b20889SRam Pai /* 21411da177e4SLinus Torvalds * do loopback mount. 21421da177e4SLinus Torvalds */ 2143808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 21442dafe1c4SEric Sandeen int recurse) 21451da177e4SLinus Torvalds { 21462d92ab3cSAl Viro struct path old_path; 214784d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 214884d17192SAl Viro struct mountpoint *mp; 214957eccb83SAl Viro int err; 21501da177e4SLinus Torvalds if (!old_name || !*old_name) 21511da177e4SLinus Torvalds return -EINVAL; 2152815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 21531da177e4SLinus Torvalds if (err) 21541da177e4SLinus Torvalds return err; 21551da177e4SLinus Torvalds 21568823c079SEric W. Biederman err = -EINVAL; 21574ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 21588823c079SEric W. Biederman goto out; 21598823c079SEric W. Biederman 216084d17192SAl Viro mp = lock_mount(path); 216184d17192SAl Viro err = PTR_ERR(mp); 216284d17192SAl Viro if (IS_ERR(mp)) 21639676f0c6SRam Pai goto out; 21649676f0c6SRam Pai 216587129cc0SAl Viro old = real_mount(old_path.mnt); 216684d17192SAl Viro parent = real_mount(path->mnt); 216787129cc0SAl Viro 2168b12cea91SAl Viro err = -EINVAL; 2169fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 2170b12cea91SAl Viro goto out2; 2171b12cea91SAl Viro 2172e149ed2bSAl Viro if (!check_mnt(parent)) 2173e149ed2bSAl Viro goto out2; 2174e149ed2bSAl Viro 2175e149ed2bSAl Viro if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations) 2176b12cea91SAl Viro goto out2; 2177ccd48bc7SAl Viro 21785ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 21795ff9d8a6SEric W. Biederman goto out2; 21805ff9d8a6SEric W. Biederman 21811da177e4SLinus Torvalds if (recurse) 21824ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 21831da177e4SLinus Torvalds else 218487129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 21851da177e4SLinus Torvalds 2186be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2187be34d1a3SDavid Howells err = PTR_ERR(mnt); 2188e9c5d8a5SAndrey Vagin goto out2; 2189be34d1a3SDavid Howells } 2190ccd48bc7SAl Viro 21915ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 21925ff9d8a6SEric W. Biederman 219384d17192SAl Viro err = graft_tree(mnt, parent, mp); 21941da177e4SLinus Torvalds if (err) { 2195719ea2fbSAl Viro lock_mount_hash(); 2196e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2197719ea2fbSAl Viro unlock_mount_hash(); 21985b83d2c5SRam Pai } 2199b12cea91SAl Viro out2: 220084d17192SAl Viro unlock_mount(mp); 2201ccd48bc7SAl Viro out: 22022d92ab3cSAl Viro path_put(&old_path); 22031da177e4SLinus Torvalds return err; 22041da177e4SLinus Torvalds } 22051da177e4SLinus Torvalds 22062e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 22072e4b7fcdSDave Hansen { 22082e4b7fcdSDave Hansen int error = 0; 22092e4b7fcdSDave Hansen int readonly_request = 0; 22102e4b7fcdSDave Hansen 22112e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 22122e4b7fcdSDave Hansen readonly_request = 1; 22132e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 22142e4b7fcdSDave Hansen return 0; 22152e4b7fcdSDave Hansen 22162e4b7fcdSDave Hansen if (readonly_request) 221783adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 22182e4b7fcdSDave Hansen else 221983adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 22202e4b7fcdSDave Hansen return error; 22212e4b7fcdSDave Hansen } 22222e4b7fcdSDave Hansen 22231da177e4SLinus Torvalds /* 22241da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 22251da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 22261da177e4SLinus Torvalds * on it - tough luck. 22271da177e4SLinus Torvalds */ 2228e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags, 2229e462ec50SDavid Howells int mnt_flags, void *data) 22301da177e4SLinus Torvalds { 22311da177e4SLinus Torvalds int err; 22322d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2233143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 22341da177e4SLinus Torvalds 2235143c8c91SAl Viro if (!check_mnt(mnt)) 22361da177e4SLinus Torvalds return -EINVAL; 22371da177e4SLinus Torvalds 22382d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 22391da177e4SLinus Torvalds return -EINVAL; 22401da177e4SLinus Torvalds 224107b64558SEric W. Biederman /* Don't allow changing of locked mnt flags. 224207b64558SEric W. Biederman * 224307b64558SEric W. Biederman * No locks need to be held here while testing the various 224407b64558SEric W. Biederman * MNT_LOCK flags because those flags can never be cleared 224507b64558SEric W. Biederman * once they are set. 224607b64558SEric W. Biederman */ 224707b64558SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) && 224807b64558SEric W. Biederman !(mnt_flags & MNT_READONLY)) { 224907b64558SEric W. Biederman return -EPERM; 225007b64558SEric W. Biederman } 22519566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) && 22529566d674SEric W. Biederman !(mnt_flags & MNT_NODEV)) { 22539566d674SEric W. Biederman return -EPERM; 22549566d674SEric W. Biederman } 22559566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) && 22569566d674SEric W. Biederman !(mnt_flags & MNT_NOSUID)) { 22579566d674SEric W. Biederman return -EPERM; 22589566d674SEric W. Biederman } 22599566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) && 22609566d674SEric W. Biederman !(mnt_flags & MNT_NOEXEC)) { 22619566d674SEric W. Biederman return -EPERM; 22629566d674SEric W. Biederman } 22639566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) && 22649566d674SEric W. Biederman ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) { 22659566d674SEric W. Biederman return -EPERM; 22669566d674SEric W. Biederman } 22679566d674SEric W. Biederman 2268ff36fe2cSEric Paris err = security_sb_remount(sb, data); 2269ff36fe2cSEric Paris if (err) 2270ff36fe2cSEric Paris return err; 2271ff36fe2cSEric Paris 22721da177e4SLinus Torvalds down_write(&sb->s_umount); 2273e462ec50SDavid Howells if (ms_flags & MS_BIND) 2274e462ec50SDavid Howells err = change_mount_flags(path->mnt, ms_flags); 2275bc6155d1SEric W. Biederman else if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 227657eccb83SAl Viro err = -EPERM; 22774aa98cf7SAl Viro else 2278e462ec50SDavid Howells err = do_remount_sb(sb, sb_flags, data, 0); 22797b43a79fSAl Viro if (!err) { 2280719ea2fbSAl Viro lock_mount_hash(); 2281a6138db8SEric W. Biederman mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 2282143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 2283143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2284719ea2fbSAl Viro unlock_mount_hash(); 22850e55a7ccSDan Williams } 22866339dab8SAl Viro up_write(&sb->s_umount); 22871da177e4SLinus Torvalds return err; 22881da177e4SLinus Torvalds } 22891da177e4SLinus Torvalds 2290cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 22919676f0c6SRam Pai { 2292315fc83eSAl Viro struct mount *p; 2293909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2294fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 22959676f0c6SRam Pai return 1; 22969676f0c6SRam Pai } 22979676f0c6SRam Pai return 0; 22989676f0c6SRam Pai } 22999676f0c6SRam Pai 2300808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 23011da177e4SLinus Torvalds { 23022d92ab3cSAl Viro struct path old_path, parent_path; 2303676da58dSAl Viro struct mount *p; 23040fb54e50SAl Viro struct mount *old; 230584d17192SAl Viro struct mountpoint *mp; 230657eccb83SAl Viro int err; 23071da177e4SLinus Torvalds if (!old_name || !*old_name) 23081da177e4SLinus Torvalds return -EINVAL; 23092d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 23101da177e4SLinus Torvalds if (err) 23111da177e4SLinus Torvalds return err; 23121da177e4SLinus Torvalds 231384d17192SAl Viro mp = lock_mount(path); 231484d17192SAl Viro err = PTR_ERR(mp); 231584d17192SAl Viro if (IS_ERR(mp)) 2316cc53ce53SDavid Howells goto out; 2317cc53ce53SDavid Howells 2318143c8c91SAl Viro old = real_mount(old_path.mnt); 2319fc7be130SAl Viro p = real_mount(path->mnt); 2320143c8c91SAl Viro 23211da177e4SLinus Torvalds err = -EINVAL; 2322fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 23231da177e4SLinus Torvalds goto out1; 23241da177e4SLinus Torvalds 23255ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 23265ff9d8a6SEric W. Biederman goto out1; 23275ff9d8a6SEric W. Biederman 23281da177e4SLinus Torvalds err = -EINVAL; 23292d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 233021444403SRam Pai goto out1; 23311da177e4SLinus Torvalds 2332676da58dSAl Viro if (!mnt_has_parent(old)) 233321444403SRam Pai goto out1; 23341da177e4SLinus Torvalds 2335e36cb0b8SDavid Howells if (d_is_dir(path->dentry) != 2336e36cb0b8SDavid Howells d_is_dir(old_path.dentry)) 233721444403SRam Pai goto out1; 233821444403SRam Pai /* 233921444403SRam Pai * Don't move a mount residing in a shared parent. 234021444403SRam Pai */ 2341fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 234221444403SRam Pai goto out1; 23439676f0c6SRam Pai /* 23449676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 23459676f0c6SRam Pai * mount which is shared. 23469676f0c6SRam Pai */ 2347fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 23489676f0c6SRam Pai goto out1; 23491da177e4SLinus Torvalds err = -ELOOP; 2350fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2351676da58dSAl Viro if (p == old) 235221444403SRam Pai goto out1; 23531da177e4SLinus Torvalds 235484d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 23554ac91378SJan Blunck if (err) 235621444403SRam Pai goto out1; 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 23591da177e4SLinus Torvalds * automatically */ 23606776db3dSAl Viro list_del_init(&old->mnt_expire); 23611da177e4SLinus Torvalds out1: 236284d17192SAl Viro unlock_mount(mp); 23631da177e4SLinus Torvalds out: 23641da177e4SLinus Torvalds if (!err) 23651a390689SAl Viro path_put(&parent_path); 23662d92ab3cSAl Viro path_put(&old_path); 23671da177e4SLinus Torvalds return err; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds 23709d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 23719d412a43SAl Viro { 23729d412a43SAl Viro int err; 23739d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 23749d412a43SAl Viro if (subtype) { 23759d412a43SAl Viro subtype++; 23769d412a43SAl Viro err = -EINVAL; 23779d412a43SAl Viro if (!subtype[0]) 23789d412a43SAl Viro goto err; 23799d412a43SAl Viro } else 23809d412a43SAl Viro subtype = ""; 23819d412a43SAl Viro 23829d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 23839d412a43SAl Viro err = -ENOMEM; 23849d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 23859d412a43SAl Viro goto err; 23869d412a43SAl Viro return mnt; 23879d412a43SAl Viro 23889d412a43SAl Viro err: 23899d412a43SAl Viro mntput(mnt); 23909d412a43SAl Viro return ERR_PTR(err); 23919d412a43SAl Viro } 23929d412a43SAl Viro 23939d412a43SAl Viro /* 23949d412a43SAl Viro * add a mount into a namespace's mount tree 23959d412a43SAl Viro */ 239695bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 23979d412a43SAl Viro { 239884d17192SAl Viro struct mountpoint *mp; 239984d17192SAl Viro struct mount *parent; 24009d412a43SAl Viro int err; 24019d412a43SAl Viro 2402f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 24039d412a43SAl Viro 240484d17192SAl Viro mp = lock_mount(path); 240584d17192SAl Viro if (IS_ERR(mp)) 240684d17192SAl Viro return PTR_ERR(mp); 24079d412a43SAl Viro 240884d17192SAl Viro parent = real_mount(path->mnt); 24099d412a43SAl Viro err = -EINVAL; 241084d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2411156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2412156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 24139d412a43SAl Viro goto unlock; 2414156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 241584d17192SAl Viro if (!parent->mnt_ns) 2416156cacb1SAl Viro goto unlock; 2417156cacb1SAl Viro } 24189d412a43SAl Viro 24199d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 24209d412a43SAl Viro err = -EBUSY; 242195bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 24229d412a43SAl Viro path->mnt->mnt_root == path->dentry) 24239d412a43SAl Viro goto unlock; 24249d412a43SAl Viro 24259d412a43SAl Viro err = -EINVAL; 2426e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 24279d412a43SAl Viro goto unlock; 24289d412a43SAl Viro 242995bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 243084d17192SAl Viro err = graft_tree(newmnt, parent, mp); 24319d412a43SAl Viro 24329d412a43SAl Viro unlock: 243384d17192SAl Viro unlock_mount(mp); 24349d412a43SAl Viro return err; 24359d412a43SAl Viro } 2436b1e75df4SAl Viro 24378654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags); 24381b852bceSEric W. Biederman 24391da177e4SLinus Torvalds /* 24401da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 24411da177e4SLinus Torvalds * namespace's tree 24421da177e4SLinus Torvalds */ 2443e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags, 2444808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 24451da177e4SLinus Torvalds { 24460c55cfc4SEric W. Biederman struct file_system_type *type; 24471da177e4SLinus Torvalds struct vfsmount *mnt; 244815f9a3f3SAl Viro int err; 24491da177e4SLinus Torvalds 24500c55cfc4SEric W. Biederman if (!fstype) 24511da177e4SLinus Torvalds return -EINVAL; 24521da177e4SLinus Torvalds 24530c55cfc4SEric W. Biederman type = get_fs_type(fstype); 24540c55cfc4SEric W. Biederman if (!type) 24550c55cfc4SEric W. Biederman return -ENODEV; 24560c55cfc4SEric W. Biederman 2457e462ec50SDavid Howells mnt = vfs_kern_mount(type, sb_flags, name, data); 24580c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 24590c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 24600c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 24610c55cfc4SEric W. Biederman 24620c55cfc4SEric W. Biederman put_filesystem(type); 24631da177e4SLinus Torvalds if (IS_ERR(mnt)) 24641da177e4SLinus Torvalds return PTR_ERR(mnt); 24651da177e4SLinus Torvalds 24668654df4eSEric W. Biederman if (mount_too_revealing(mnt, &mnt_flags)) { 24678654df4eSEric W. Biederman mntput(mnt); 24688654df4eSEric W. Biederman return -EPERM; 24698654df4eSEric W. Biederman } 24708654df4eSEric W. Biederman 247195bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 247215f9a3f3SAl Viro if (err) 247315f9a3f3SAl Viro mntput(mnt); 247415f9a3f3SAl Viro return err; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 247719a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 247819a167afSAl Viro { 24796776db3dSAl Viro struct mount *mnt = real_mount(m); 248019a167afSAl Viro int err; 248119a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 248219a167afSAl Viro * expired before we get a chance to add it 248319a167afSAl Viro */ 24846776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 248519a167afSAl Viro 248619a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 248719a167afSAl Viro m->mnt_root == path->dentry) { 2488b1e75df4SAl Viro err = -ELOOP; 2489b1e75df4SAl Viro goto fail; 249019a167afSAl Viro } 249119a167afSAl Viro 249295bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2493b1e75df4SAl Viro if (!err) 2494b1e75df4SAl Viro return 0; 2495b1e75df4SAl Viro fail: 2496b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 24976776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 249897216be0SAl Viro namespace_lock(); 24996776db3dSAl Viro list_del_init(&mnt->mnt_expire); 250097216be0SAl Viro namespace_unlock(); 250119a167afSAl Viro } 2502b1e75df4SAl Viro mntput(m); 2503b1e75df4SAl Viro mntput(m); 250419a167afSAl Viro return err; 250519a167afSAl Viro } 250619a167afSAl Viro 2507ea5b778aSDavid Howells /** 2508ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2509ea5b778aSDavid Howells * @mnt: The mount to list. 2510ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2511ea5b778aSDavid Howells */ 2512ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2513ea5b778aSDavid Howells { 251497216be0SAl Viro namespace_lock(); 2515ea5b778aSDavid Howells 25166776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2517ea5b778aSDavid Howells 251897216be0SAl Viro namespace_unlock(); 2519ea5b778aSDavid Howells } 2520ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2521ea5b778aSDavid Howells 2522ea5b778aSDavid Howells /* 25231da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 25241da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 25251da177e4SLinus Torvalds * here 25261da177e4SLinus Torvalds */ 25271da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 25281da177e4SLinus Torvalds { 2529761d5c38SAl Viro struct mount *mnt, *next; 25301da177e4SLinus Torvalds LIST_HEAD(graveyard); 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds if (list_empty(mounts)) 25331da177e4SLinus Torvalds return; 25341da177e4SLinus Torvalds 253597216be0SAl Viro namespace_lock(); 2536719ea2fbSAl Viro lock_mount_hash(); 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 25391da177e4SLinus Torvalds * following criteria: 25401da177e4SLinus Torvalds * - only referenced by its parent vfsmount 25411da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 25421da177e4SLinus Torvalds * cleared by mntput()) 25431da177e4SLinus Torvalds */ 25446776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2545863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 25461ab59738SAl Viro propagate_mount_busy(mnt, 1)) 25471da177e4SLinus Torvalds continue; 25486776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 25491da177e4SLinus Torvalds } 2550bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 25516776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2552143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2553e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2554bcc5c7d2SAl Viro } 2555719ea2fbSAl Viro unlock_mount_hash(); 25563ab6abeeSAl Viro namespace_unlock(); 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds /* 25625528f911STrond Myklebust * Ripoff of 'select_parent()' 25635528f911STrond Myklebust * 25645528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 25655528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 25665528f911STrond Myklebust */ 2567692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 25685528f911STrond Myklebust { 2569692afc31SAl Viro struct mount *this_parent = parent; 25705528f911STrond Myklebust struct list_head *next; 25715528f911STrond Myklebust int found = 0; 25725528f911STrond Myklebust 25735528f911STrond Myklebust repeat: 25746b41d536SAl Viro next = this_parent->mnt_mounts.next; 25755528f911STrond Myklebust resume: 25766b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 25775528f911STrond Myklebust struct list_head *tmp = next; 25786b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 25795528f911STrond Myklebust 25805528f911STrond Myklebust next = tmp->next; 2581692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 25825528f911STrond Myklebust continue; 25835528f911STrond Myklebust /* 25845528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 25855528f911STrond Myklebust */ 25866b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 25875528f911STrond Myklebust this_parent = mnt; 25885528f911STrond Myklebust goto repeat; 25895528f911STrond Myklebust } 25905528f911STrond Myklebust 25911ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 25926776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 25935528f911STrond Myklebust found++; 25945528f911STrond Myklebust } 25955528f911STrond Myklebust } 25965528f911STrond Myklebust /* 25975528f911STrond Myklebust * All done at this level ... ascend and resume the search 25985528f911STrond Myklebust */ 25995528f911STrond Myklebust if (this_parent != parent) { 26006b41d536SAl Viro next = this_parent->mnt_child.next; 26010714a533SAl Viro this_parent = this_parent->mnt_parent; 26025528f911STrond Myklebust goto resume; 26035528f911STrond Myklebust } 26045528f911STrond Myklebust return found; 26055528f911STrond Myklebust } 26065528f911STrond Myklebust 26075528f911STrond Myklebust /* 26085528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 26095528f911STrond Myklebust * submounts of a specific parent mountpoint 261099b7db7bSNick Piggin * 261148a066e7SAl Viro * mount_lock must be held for write 26125528f911STrond Myklebust */ 2613b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 26145528f911STrond Myklebust { 26155528f911STrond Myklebust LIST_HEAD(graveyard); 2616761d5c38SAl Viro struct mount *m; 26175528f911STrond Myklebust 26185528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2619c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2620bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2621761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 26226776db3dSAl Viro mnt_expire); 2623143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2624e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2625bcc5c7d2SAl Viro } 2626bcc5c7d2SAl Viro } 26275528f911STrond Myklebust } 26285528f911STrond Myklebust 26295528f911STrond Myklebust /* 26301da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 26311da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 26321da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 26331da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 26341da177e4SLinus Torvalds */ 2635b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2636b58fed8bSRam Pai unsigned long n) 26371da177e4SLinus Torvalds { 26381da177e4SLinus Torvalds char *t = to; 26391da177e4SLinus Torvalds const char __user *f = from; 26401da177e4SLinus Torvalds char c; 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 26431da177e4SLinus Torvalds return n; 26441da177e4SLinus Torvalds 26459da3f2b7SJann Horn current->kernel_uaccess_faults_ok++; 26461da177e4SLinus Torvalds while (n) { 26471da177e4SLinus Torvalds if (__get_user(c, f)) { 26481da177e4SLinus Torvalds memset(t, 0, n); 26491da177e4SLinus Torvalds break; 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds *t++ = c; 26521da177e4SLinus Torvalds f++; 26531da177e4SLinus Torvalds n--; 26541da177e4SLinus Torvalds } 26559da3f2b7SJann Horn current->kernel_uaccess_faults_ok--; 26561da177e4SLinus Torvalds return n; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds 2659b40ef869SAl Viro void *copy_mount_options(const void __user * data) 26601da177e4SLinus Torvalds { 26611da177e4SLinus Torvalds int i; 26621da177e4SLinus Torvalds unsigned long size; 2663b40ef869SAl Viro char *copy; 26641da177e4SLinus Torvalds 26651da177e4SLinus Torvalds if (!data) 2666b40ef869SAl Viro return NULL; 26671da177e4SLinus Torvalds 2668b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 2669b40ef869SAl Viro if (!copy) 2670b40ef869SAl Viro return ERR_PTR(-ENOMEM); 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds /* We only care that *some* data at the address the user 26731da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 26741da177e4SLinus Torvalds * the remainder of the page. 26751da177e4SLinus Torvalds */ 26761da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 26771da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 26781da177e4SLinus Torvalds if (size > PAGE_SIZE) 26791da177e4SLinus Torvalds size = PAGE_SIZE; 26801da177e4SLinus Torvalds 2681b40ef869SAl Viro i = size - exact_copy_from_user(copy, data, size); 26821da177e4SLinus Torvalds if (!i) { 2683b40ef869SAl Viro kfree(copy); 2684b40ef869SAl Viro return ERR_PTR(-EFAULT); 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds if (i != PAGE_SIZE) 2687b40ef869SAl Viro memset(copy + i, 0, PAGE_SIZE - i); 2688b40ef869SAl Viro return copy; 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds 2691b8850d1fSTim Gardner char *copy_mount_string(const void __user *data) 2692eca6f534SVegard Nossum { 2693b8850d1fSTim Gardner return data ? strndup_user(data, PAGE_SIZE) : NULL; 2694eca6f534SVegard Nossum } 2695eca6f534SVegard Nossum 26961da177e4SLinus Torvalds /* 26971da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 26981da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 26991da177e4SLinus Torvalds * 27001da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 27011da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 27021da177e4SLinus Torvalds * information (or be NULL). 27031da177e4SLinus Torvalds * 27041da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 27051da177e4SLinus Torvalds * When the flags word was introduced its top half was required 27061da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 27071da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 27081da177e4SLinus Torvalds * and must be discarded. 27091da177e4SLinus Torvalds */ 27105e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name, 2711808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 27121da177e4SLinus Torvalds { 27132d92ab3cSAl Viro struct path path; 2714e462ec50SDavid Howells unsigned int mnt_flags = 0, sb_flags; 27151da177e4SLinus Torvalds int retval = 0; 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds /* Discard magic */ 27181da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 27191da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds /* Basic sanity checks */ 27221da177e4SLinus Torvalds if (data_page) 27231da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 27241da177e4SLinus Torvalds 2725e462ec50SDavid Howells if (flags & MS_NOUSER) 2726e462ec50SDavid Howells return -EINVAL; 2727e462ec50SDavid Howells 2728a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 27295e6123f3SSeunghun Lee retval = user_path(dir_name, &path); 2730a27ab9f2STetsuo Handa if (retval) 2731a27ab9f2STetsuo Handa return retval; 2732a27ab9f2STetsuo Handa 2733a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2734a27ab9f2STetsuo Handa type_page, flags, data_page); 27350d5cadb8SAl Viro if (!retval && !may_mount()) 27360d5cadb8SAl Viro retval = -EPERM; 2737e462ec50SDavid Howells if (!retval && (flags & SB_MANDLOCK) && !may_mandlock()) 27389e8925b6SJeff Layton retval = -EPERM; 2739a27ab9f2STetsuo Handa if (retval) 2740a27ab9f2STetsuo Handa goto dput_out; 2741a27ab9f2STetsuo Handa 2742613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2743613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 27440a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 27450a1c01c9SMatthew Garrett 27461da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 27471da177e4SLinus Torvalds if (flags & MS_NOSUID) 27481da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 27491da177e4SLinus Torvalds if (flags & MS_NODEV) 27501da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 27511da177e4SLinus Torvalds if (flags & MS_NOEXEC) 27521da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2753fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2754fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2755fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2756fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2757d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2758d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 2759a9e5b732SDavid Howells if (flags & MS_RDONLY) 27602e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2761fc33a7bbSChristoph Hellwig 2762ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 2763ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 2764ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 2765ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 2766ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 2767ffbc6f0eSEric W. Biederman mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; 2768ffbc6f0eSEric W. Biederman } 2769ffbc6f0eSEric W. Biederman 2770e462ec50SDavid Howells sb_flags = flags & (SB_RDONLY | 2771e462ec50SDavid Howells SB_SYNCHRONOUS | 2772e462ec50SDavid Howells SB_MANDLOCK | 2773e462ec50SDavid Howells SB_DIRSYNC | 2774e462ec50SDavid Howells SB_SILENT | 2775917086ffSMimi Zohar SB_POSIXACL | 2776d7ee9469SMarkus Trippelsdorf SB_LAZYTIME | 2777917086ffSMimi Zohar SB_I_VERSION); 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2780e462ec50SDavid Howells retval = do_remount(&path, flags, sb_flags, mnt_flags, 27811da177e4SLinus Torvalds data_page); 27821da177e4SLinus Torvalds else if (flags & MS_BIND) 27832d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 27849676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 27852d92ab3cSAl Viro retval = do_change_type(&path, flags); 27861da177e4SLinus Torvalds else if (flags & MS_MOVE) 27872d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 27881da177e4SLinus Torvalds else 2789e462ec50SDavid Howells retval = do_new_mount(&path, type_page, sb_flags, mnt_flags, 27901da177e4SLinus Torvalds dev_name, data_page); 27911da177e4SLinus Torvalds dput_out: 27922d92ab3cSAl Viro path_put(&path); 27931da177e4SLinus Torvalds return retval; 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds 2796537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 2797537f7ccbSEric W. Biederman { 2798537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 2799537f7ccbSEric W. Biederman } 2800537f7ccbSEric W. Biederman 2801537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 2802537f7ccbSEric W. Biederman { 2803537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 2804537f7ccbSEric W. Biederman } 2805537f7ccbSEric W. Biederman 2806771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2807771b1371SEric W. Biederman { 28086344c433SAl Viro ns_free_inum(&ns->ns); 2809537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 2810771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2811771b1371SEric W. Biederman kfree(ns); 2812771b1371SEric W. Biederman } 2813771b1371SEric W. Biederman 28148823c079SEric W. Biederman /* 28158823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 28168823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 28178823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 28188823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 28198823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 28208823c079SEric W. Biederman */ 28218823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 28228823c079SEric W. Biederman 2823771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2824cf8d2c11STrond Myklebust { 2825cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2826537f7ccbSEric W. Biederman struct ucounts *ucounts; 282798f842e6SEric W. Biederman int ret; 2828cf8d2c11STrond Myklebust 2829537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 2830537f7ccbSEric W. Biederman if (!ucounts) 2831df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 2832537f7ccbSEric W. Biederman 2833cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2834537f7ccbSEric W. Biederman if (!new_ns) { 2835537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 2836cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2837537f7ccbSEric W. Biederman } 28386344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 283998f842e6SEric W. Biederman if (ret) { 284098f842e6SEric W. Biederman kfree(new_ns); 2841537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 284298f842e6SEric W. Biederman return ERR_PTR(ret); 284398f842e6SEric W. Biederman } 284433c42940SAl Viro new_ns->ns.ops = &mntns_operations; 28458823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2846cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2847cf8d2c11STrond Myklebust new_ns->root = NULL; 2848cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2849cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2850cf8d2c11STrond Myklebust new_ns->event = 0; 2851771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2852537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 2853d2921684SEric W. Biederman new_ns->mounts = 0; 2854d2921684SEric W. Biederman new_ns->pending_mounts = 0; 2855cf8d2c11STrond Myklebust return new_ns; 2856cf8d2c11STrond Myklebust } 2857cf8d2c11STrond Myklebust 28580766f788SEmese Revfy __latent_entropy 28599559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 28609559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 28611da177e4SLinus Torvalds { 28626b3286edSKirill Korotaev struct mnt_namespace *new_ns; 28637f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2864315fc83eSAl Viro struct mount *p, *q; 28659559f689SAl Viro struct mount *old; 2866cb338d06SAl Viro struct mount *new; 28677a472ef4SEric W. Biederman int copy_flags; 28681da177e4SLinus Torvalds 28699559f689SAl Viro BUG_ON(!ns); 28709559f689SAl Viro 28719559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 28729559f689SAl Viro get_mnt_ns(ns); 28739559f689SAl Viro return ns; 28749559f689SAl Viro } 28759559f689SAl Viro 28769559f689SAl Viro old = ns->root; 28779559f689SAl Viro 2878771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2879cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2880cf8d2c11STrond Myklebust return new_ns; 28811da177e4SLinus Torvalds 288297216be0SAl Viro namespace_lock(); 28831da177e4SLinus Torvalds /* First pass: copy the tree topology */ 28844ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 28859559f689SAl Viro if (user_ns != ns->user_ns) 2886132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 28877a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2888be34d1a3SDavid Howells if (IS_ERR(new)) { 2889328e6d90SAl Viro namespace_unlock(); 2890771b1371SEric W. Biederman free_mnt_ns(new_ns); 2891be34d1a3SDavid Howells return ERR_CAST(new); 28921da177e4SLinus Torvalds } 2893be08d6d2SAl Viro new_ns->root = new; 28941a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds /* 28971da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 28981da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 28991da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 29001da177e4SLinus Torvalds */ 2901909b0a88SAl Viro p = old; 2902cb338d06SAl Viro q = new; 29031da177e4SLinus Torvalds while (p) { 2904143c8c91SAl Viro q->mnt_ns = new_ns; 2905d2921684SEric W. Biederman new_ns->mounts++; 29069559f689SAl Viro if (new_fs) { 29079559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 29089559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2909315fc83eSAl Viro rootmnt = &p->mnt; 29101da177e4SLinus Torvalds } 29119559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 29129559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2913315fc83eSAl Viro pwdmnt = &p->mnt; 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds } 2916909b0a88SAl Viro p = next_mnt(p, old); 2917909b0a88SAl Viro q = next_mnt(q, new); 29184ce5d2b1SEric W. Biederman if (!q) 29194ce5d2b1SEric W. Biederman break; 29204ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 29214ce5d2b1SEric W. Biederman p = next_mnt(p, old); 29221da177e4SLinus Torvalds } 2923328e6d90SAl Viro namespace_unlock(); 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds if (rootmnt) 2926f03c6599SAl Viro mntput(rootmnt); 29271da177e4SLinus Torvalds if (pwdmnt) 2928f03c6599SAl Viro mntput(pwdmnt); 29291da177e4SLinus Torvalds 2930741a2951SJANAK DESAI return new_ns; 2931741a2951SJANAK DESAI } 2932741a2951SJANAK DESAI 2933cf8d2c11STrond Myklebust /** 2934cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2935cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2936cf8d2c11STrond Myklebust */ 29371a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2938cf8d2c11STrond Myklebust { 2939771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2940cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 29411a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 29421a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2943be08d6d2SAl Viro new_ns->root = mnt; 2944d2921684SEric W. Biederman new_ns->mounts++; 2945b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 2946c1334495SAl Viro } else { 29471a4eeaf2SAl Viro mntput(m); 2948cf8d2c11STrond Myklebust } 2949cf8d2c11STrond Myklebust return new_ns; 2950cf8d2c11STrond Myklebust } 2951cf8d2c11STrond Myklebust 2952ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2953ea441d11SAl Viro { 2954ea441d11SAl Viro struct mnt_namespace *ns; 2955d31da0f0SAl Viro struct super_block *s; 2956ea441d11SAl Viro struct path path; 2957ea441d11SAl Viro int err; 2958ea441d11SAl Viro 2959ea441d11SAl Viro ns = create_mnt_ns(mnt); 2960ea441d11SAl Viro if (IS_ERR(ns)) 2961ea441d11SAl Viro return ERR_CAST(ns); 2962ea441d11SAl Viro 2963ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2964ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2965ea441d11SAl Viro 2966ea441d11SAl Viro put_mnt_ns(ns); 2967ea441d11SAl Viro 2968ea441d11SAl Viro if (err) 2969ea441d11SAl Viro return ERR_PTR(err); 2970ea441d11SAl Viro 2971ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2972d31da0f0SAl Viro s = path.mnt->mnt_sb; 2973d31da0f0SAl Viro atomic_inc(&s->s_active); 2974ea441d11SAl Viro mntput(path.mnt); 2975ea441d11SAl Viro /* lock the sucker */ 2976d31da0f0SAl Viro down_write(&s->s_umount); 2977ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2978ea441d11SAl Viro return path.dentry; 2979ea441d11SAl Viro } 2980ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2981ea441d11SAl Viro 2982312db1aaSDominik Brodowski int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type, 2983312db1aaSDominik Brodowski unsigned long flags, void __user *data) 29841da177e4SLinus Torvalds { 2985eca6f534SVegard Nossum int ret; 2986eca6f534SVegard Nossum char *kernel_type; 2987eca6f534SVegard Nossum char *kernel_dev; 2988b40ef869SAl Viro void *options; 29891da177e4SLinus Torvalds 2990b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 2991b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 2992b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 2993eca6f534SVegard Nossum goto out_type; 29941da177e4SLinus Torvalds 2995b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 2996b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 2997b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 2998eca6f534SVegard Nossum goto out_dev; 29991da177e4SLinus Torvalds 3000b40ef869SAl Viro options = copy_mount_options(data); 3001b40ef869SAl Viro ret = PTR_ERR(options); 3002b40ef869SAl Viro if (IS_ERR(options)) 3003eca6f534SVegard Nossum goto out_data; 30041da177e4SLinus Torvalds 3005b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 3006eca6f534SVegard Nossum 3007b40ef869SAl Viro kfree(options); 3008eca6f534SVegard Nossum out_data: 3009eca6f534SVegard Nossum kfree(kernel_dev); 3010eca6f534SVegard Nossum out_dev: 3011eca6f534SVegard Nossum kfree(kernel_type); 3012eca6f534SVegard Nossum out_type: 3013eca6f534SVegard Nossum return ret; 30141da177e4SLinus Torvalds } 30151da177e4SLinus Torvalds 3016312db1aaSDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 3017312db1aaSDominik Brodowski char __user *, type, unsigned long, flags, void __user *, data) 3018312db1aaSDominik Brodowski { 3019312db1aaSDominik Brodowski return ksys_mount(dev_name, dir_name, type, flags, data); 3020312db1aaSDominik Brodowski } 3021312db1aaSDominik Brodowski 30221da177e4SLinus Torvalds /* 3023afac7cbaSAl Viro * Return true if path is reachable from root 3024afac7cbaSAl Viro * 302548a066e7SAl Viro * namespace_sem or mount_lock is held 3026afac7cbaSAl Viro */ 3027643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3028afac7cbaSAl Viro const struct path *root) 3029afac7cbaSAl Viro { 3030643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3031a73324daSAl Viro dentry = mnt->mnt_mountpoint; 30320714a533SAl Viro mnt = mnt->mnt_parent; 3033afac7cbaSAl Viro } 3034643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3035afac7cbaSAl Viro } 3036afac7cbaSAl Viro 3037640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3038afac7cbaSAl Viro { 303925ab4c9bSYaowei Bai bool res; 304048a066e7SAl Viro read_seqlock_excl(&mount_lock); 3041643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 304248a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3043afac7cbaSAl Viro return res; 3044afac7cbaSAl Viro } 3045afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3046afac7cbaSAl Viro 3047afac7cbaSAl Viro /* 30481da177e4SLinus Torvalds * pivot_root Semantics: 30491da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 30501da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 30511da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 30521da177e4SLinus Torvalds * 30531da177e4SLinus Torvalds * Restrictions: 30541da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 30551da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 30561da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 30571da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 30581da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 30591da177e4SLinus Torvalds * 30604a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 30614a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 30624a0d11faSNeil Brown * in this situation. 30634a0d11faSNeil Brown * 30641da177e4SLinus Torvalds * Notes: 30651da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 30661da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 30671da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 30681da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 30691da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 30701da177e4SLinus Torvalds * first. 30711da177e4SLinus Torvalds */ 30723480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 30733480b257SHeiko Carstens const char __user *, put_old) 30741da177e4SLinus Torvalds { 30752d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 307684d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 307784d17192SAl Viro struct mountpoint *old_mp, *root_mp; 30781da177e4SLinus Torvalds int error; 30791da177e4SLinus Torvalds 30809b40bc90SAl Viro if (!may_mount()) 30811da177e4SLinus Torvalds return -EPERM; 30821da177e4SLinus Torvalds 30832d8f3038SAl Viro error = user_path_dir(new_root, &new); 30841da177e4SLinus Torvalds if (error) 30851da177e4SLinus Torvalds goto out0; 30861da177e4SLinus Torvalds 30872d8f3038SAl Viro error = user_path_dir(put_old, &old); 30881da177e4SLinus Torvalds if (error) 30891da177e4SLinus Torvalds goto out1; 30901da177e4SLinus Torvalds 30912d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3092b12cea91SAl Viro if (error) 3093b12cea91SAl Viro goto out2; 30941da177e4SLinus Torvalds 3095f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 309684d17192SAl Viro old_mp = lock_mount(&old); 309784d17192SAl Viro error = PTR_ERR(old_mp); 309884d17192SAl Viro if (IS_ERR(old_mp)) 3099b12cea91SAl Viro goto out3; 3100b12cea91SAl Viro 31011da177e4SLinus Torvalds error = -EINVAL; 3102419148daSAl Viro new_mnt = real_mount(new.mnt); 3103419148daSAl Viro root_mnt = real_mount(root.mnt); 310484d17192SAl Viro old_mnt = real_mount(old.mnt); 310584d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 3106fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 3107fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 3108b12cea91SAl Viro goto out4; 3109143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3110b12cea91SAl Viro goto out4; 31115ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 31125ff9d8a6SEric W. Biederman goto out4; 31131da177e4SLinus Torvalds error = -ENOENT; 3114f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3115b12cea91SAl Viro goto out4; 31161da177e4SLinus Torvalds error = -EBUSY; 311784d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3118b12cea91SAl Viro goto out4; /* loop, on the same file system */ 31191da177e4SLinus Torvalds error = -EINVAL; 31208c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3121b12cea91SAl Viro goto out4; /* not a mountpoint */ 3122676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3123b12cea91SAl Viro goto out4; /* not attached */ 312484d17192SAl Viro root_mp = root_mnt->mnt_mp; 31252d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3126b12cea91SAl Viro goto out4; /* not a mountpoint */ 3127676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3128b12cea91SAl Viro goto out4; /* not attached */ 31294ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 313084d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3131b12cea91SAl Viro goto out4; 31320d082601SEric W. Biederman /* make certain new is below the root */ 31330d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 31340d082601SEric W. Biederman goto out4; 313584d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 3136719ea2fbSAl Viro lock_mount_hash(); 3137419148daSAl Viro detach_mnt(new_mnt, &parent_path); 3138419148daSAl Viro detach_mnt(root_mnt, &root_parent); 31395ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 31405ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 31415ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 31425ff9d8a6SEric W. Biederman } 31434ac91378SJan Blunck /* mount old root on put_old */ 314484d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 31454ac91378SJan Blunck /* mount new_root on / */ 314684d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 31476b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 31484fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 31494fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 31503895dbf8SEric W. Biederman put_mountpoint(root_mp); 3151719ea2fbSAl Viro unlock_mount_hash(); 31522d8f3038SAl Viro chroot_fs_refs(&root, &new); 31531da177e4SLinus Torvalds error = 0; 3154b12cea91SAl Viro out4: 315584d17192SAl Viro unlock_mount(old_mp); 3156b12cea91SAl Viro if (!error) { 31571a390689SAl Viro path_put(&root_parent); 31581a390689SAl Viro path_put(&parent_path); 3159b12cea91SAl Viro } 3160b12cea91SAl Viro out3: 31618c3ee42eSAl Viro path_put(&root); 3162b12cea91SAl Viro out2: 31632d8f3038SAl Viro path_put(&old); 31641da177e4SLinus Torvalds out1: 31652d8f3038SAl Viro path_put(&new); 31661da177e4SLinus Torvalds out0: 31671da177e4SLinus Torvalds return error; 31681da177e4SLinus Torvalds } 31691da177e4SLinus Torvalds 31701da177e4SLinus Torvalds static void __init init_mount_tree(void) 31711da177e4SLinus Torvalds { 31721da177e4SLinus Torvalds struct vfsmount *mnt; 31736b3286edSKirill Korotaev struct mnt_namespace *ns; 3174ac748a09SJan Blunck struct path root; 31750c55cfc4SEric W. Biederman struct file_system_type *type; 31761da177e4SLinus Torvalds 31770c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 31780c55cfc4SEric W. Biederman if (!type) 31790c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 31800c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 31810c55cfc4SEric W. Biederman put_filesystem(type); 31821da177e4SLinus Torvalds if (IS_ERR(mnt)) 31831da177e4SLinus Torvalds panic("Can't create rootfs"); 3184b3e19d92SNick Piggin 31853b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 31863b22edc5STrond Myklebust if (IS_ERR(ns)) 31871da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 31881da177e4SLinus Torvalds 31896b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 31906b3286edSKirill Korotaev get_mnt_ns(ns); 31911da177e4SLinus Torvalds 3192be08d6d2SAl Viro root.mnt = mnt; 3193be08d6d2SAl Viro root.dentry = mnt->mnt_root; 3194da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 3195ac748a09SJan Blunck 3196ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 3197ac748a09SJan Blunck set_fs_root(current->fs, &root); 31981da177e4SLinus Torvalds } 31991da177e4SLinus Torvalds 320074bf17cfSDenis Cheng void __init mnt_init(void) 32011da177e4SLinus Torvalds { 320215a67dd8SRandy Dunlap int err; 32031da177e4SLinus Torvalds 32047d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 320520c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 32061da177e4SLinus Torvalds 32070818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 320838129a13SAl Viro sizeof(struct hlist_head), 32090818bf27SAl Viro mhash_entries, 19, 32103d375d78SPavel Tatashin HASH_ZERO, 32110818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 32120818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 32130818bf27SAl Viro sizeof(struct hlist_head), 32140818bf27SAl Viro mphash_entries, 19, 32153d375d78SPavel Tatashin HASH_ZERO, 32160818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 32171da177e4SLinus Torvalds 321884d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 32191da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 32201da177e4SLinus Torvalds 32214b93dc9bSTejun Heo kernfs_init(); 32224b93dc9bSTejun Heo 322315a67dd8SRandy Dunlap err = sysfs_init(); 322415a67dd8SRandy Dunlap if (err) 322515a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 32268e24eea7SHarvey Harrison __func__, err); 322700d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 322800d26666SGreg Kroah-Hartman if (!fs_kobj) 32298e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 32301da177e4SLinus Torvalds init_rootfs(); 32311da177e4SLinus Torvalds init_mount_tree(); 32321da177e4SLinus Torvalds } 32331da177e4SLinus Torvalds 3234616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 32351da177e4SLinus Torvalds { 3236d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 3237616511d0STrond Myklebust return; 32387b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3239771b1371SEric W. Biederman free_mnt_ns(ns); 32401da177e4SLinus Torvalds } 32419d412a43SAl Viro 32429d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 32439d412a43SAl Viro { 3244423e0ab0STim Chen struct vfsmount *mnt; 3245e462ec50SDavid Howells mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, data); 3246423e0ab0STim Chen if (!IS_ERR(mnt)) { 3247423e0ab0STim Chen /* 3248423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3249423e0ab0STim Chen * we unmount before file sys is unregistered 3250423e0ab0STim Chen */ 3251f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3252423e0ab0STim Chen } 3253423e0ab0STim Chen return mnt; 32549d412a43SAl Viro } 32559d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 3256423e0ab0STim Chen 3257423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3258423e0ab0STim Chen { 3259423e0ab0STim Chen /* release long term mount so mount point can be released */ 3260423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3261f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 326248a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3263423e0ab0STim Chen mntput(mnt); 3264423e0ab0STim Chen } 3265423e0ab0STim Chen } 3266423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 326702125a82SAl Viro 326802125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 326902125a82SAl Viro { 3270143c8c91SAl Viro return check_mnt(real_mount(mnt)); 327102125a82SAl Viro } 32728823c079SEric W. Biederman 32733151527eSEric W. Biederman bool current_chrooted(void) 32743151527eSEric W. Biederman { 32753151527eSEric W. Biederman /* Does the current process have a non-standard root */ 32763151527eSEric W. Biederman struct path ns_root; 32773151527eSEric W. Biederman struct path fs_root; 32783151527eSEric W. Biederman bool chrooted; 32793151527eSEric W. Biederman 32803151527eSEric W. Biederman /* Find the namespace root */ 32813151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 32823151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 32833151527eSEric W. Biederman path_get(&ns_root); 32843151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 32853151527eSEric W. Biederman ; 32863151527eSEric W. Biederman 32873151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 32883151527eSEric W. Biederman 32893151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 32903151527eSEric W. Biederman 32913151527eSEric W. Biederman path_put(&fs_root); 32923151527eSEric W. Biederman path_put(&ns_root); 32933151527eSEric W. Biederman 32943151527eSEric W. Biederman return chrooted; 32953151527eSEric W. Biederman } 32963151527eSEric W. Biederman 32978654df4eSEric W. Biederman static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new, 32988654df4eSEric W. Biederman int *new_mnt_flags) 329987a8ebd6SEric W. Biederman { 33008c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 330187a8ebd6SEric W. Biederman struct mount *mnt; 3302e51db735SEric W. Biederman bool visible = false; 330387a8ebd6SEric W. Biederman 330444bb4385SAl Viro down_read(&namespace_sem); 330587a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3306e51db735SEric W. Biederman struct mount *child; 330777b1a97dSEric W. Biederman int mnt_flags; 330877b1a97dSEric W. Biederman 33098654df4eSEric W. Biederman if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type) 3310e51db735SEric W. Biederman continue; 3311e51db735SEric W. Biederman 33127e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 33137e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 33147e96c1b0SEric W. Biederman */ 33157e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 33167e96c1b0SEric W. Biederman continue; 33177e96c1b0SEric W. Biederman 3318a1935c17SEric W. Biederman /* A local view of the mount flags */ 331977b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 332077b1a97dSEric W. Biederman 3321695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 3322bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt.mnt_sb)) 3323695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 3324695e9df0SEric W. Biederman 33258c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 33268c6cf9ccSEric W. Biederman * than the proposed new mount. 33278c6cf9ccSEric W. Biederman */ 332877b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 33298c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 33308c6cf9ccSEric W. Biederman continue; 333177b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 333277b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 33338c6cf9ccSEric W. Biederman continue; 33348c6cf9ccSEric W. Biederman 3335ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 3336ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 3337ceeb0e5dSEric W. Biederman * empty directories. 3338e51db735SEric W. Biederman */ 3339e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3340e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3341ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 3342d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 3343ceeb0e5dSEric W. Biederman continue; 33447236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 33457236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 3346e51db735SEric W. Biederman goto next; 334787a8ebd6SEric W. Biederman } 33488c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 334977b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 33508c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 3351e51db735SEric W. Biederman visible = true; 3352e51db735SEric W. Biederman goto found; 3353e51db735SEric W. Biederman next: ; 335487a8ebd6SEric W. Biederman } 3355e51db735SEric W. Biederman found: 335644bb4385SAl Viro up_read(&namespace_sem); 3357e51db735SEric W. Biederman return visible; 335887a8ebd6SEric W. Biederman } 335987a8ebd6SEric W. Biederman 33608654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags) 33618654df4eSEric W. Biederman { 3362a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 33638654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 33648654df4eSEric W. Biederman unsigned long s_iflags; 33658654df4eSEric W. Biederman 33668654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 33678654df4eSEric W. Biederman return false; 33688654df4eSEric W. Biederman 33698654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 33708654df4eSEric W. Biederman s_iflags = mnt->mnt_sb->s_iflags; 33718654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 33728654df4eSEric W. Biederman return false; 33738654df4eSEric W. Biederman 3374a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 3375a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 3376a1935c17SEric W. Biederman required_iflags); 3377a1935c17SEric W. Biederman return true; 3378a1935c17SEric W. Biederman } 3379a1935c17SEric W. Biederman 33808654df4eSEric W. Biederman return !mnt_already_visible(ns, mnt, new_mnt_flags); 33818654df4eSEric W. Biederman } 33828654df4eSEric W. Biederman 3383380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 3384380cf5baSAndy Lutomirski { 3385380cf5baSAndy Lutomirski /* 3386380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 3387380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 3388380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 3389380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 3390380cf5baSAndy Lutomirski * in other namespaces. 3391380cf5baSAndy Lutomirski */ 3392380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 3393380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 3394380cf5baSAndy Lutomirski } 3395380cf5baSAndy Lutomirski 339664964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 33978823c079SEric W. Biederman { 339858be2825SAl Viro struct ns_common *ns = NULL; 33998823c079SEric W. Biederman struct nsproxy *nsproxy; 34008823c079SEric W. Biederman 3401728dba3aSEric W. Biederman task_lock(task); 3402728dba3aSEric W. Biederman nsproxy = task->nsproxy; 34038823c079SEric W. Biederman if (nsproxy) { 340458be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 340558be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 34068823c079SEric W. Biederman } 3407728dba3aSEric W. Biederman task_unlock(task); 34088823c079SEric W. Biederman 34098823c079SEric W. Biederman return ns; 34108823c079SEric W. Biederman } 34118823c079SEric W. Biederman 341264964528SAl Viro static void mntns_put(struct ns_common *ns) 34138823c079SEric W. Biederman { 341458be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 34158823c079SEric W. Biederman } 34168823c079SEric W. Biederman 341764964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns) 34188823c079SEric W. Biederman { 34198823c079SEric W. Biederman struct fs_struct *fs = current->fs; 34204f757f3cSAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 34218823c079SEric W. Biederman struct path root; 34224f757f3cSAl Viro int err; 34238823c079SEric W. Biederman 34240c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3425c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3426c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3427ae11e0f1SZhao Hongjiang return -EPERM; 34288823c079SEric W. Biederman 34298823c079SEric W. Biederman if (fs->users != 1) 34308823c079SEric W. Biederman return -EINVAL; 34318823c079SEric W. Biederman 34328823c079SEric W. Biederman get_mnt_ns(mnt_ns); 34334f757f3cSAl Viro old_mnt_ns = nsproxy->mnt_ns; 34348823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 34358823c079SEric W. Biederman 34368823c079SEric W. Biederman /* Find the root */ 34374f757f3cSAl Viro err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 34384f757f3cSAl Viro "/", LOOKUP_DOWN, &root); 34394f757f3cSAl Viro if (err) { 34404f757f3cSAl Viro /* revert to old namespace */ 34414f757f3cSAl Viro nsproxy->mnt_ns = old_mnt_ns; 34424f757f3cSAl Viro put_mnt_ns(mnt_ns); 34434f757f3cSAl Viro return err; 34444f757f3cSAl Viro } 34458823c079SEric W. Biederman 34464068367cSAndrei Vagin put_mnt_ns(old_mnt_ns); 34474068367cSAndrei Vagin 34488823c079SEric W. Biederman /* Update the pwd and root */ 34498823c079SEric W. Biederman set_fs_pwd(fs, &root); 34508823c079SEric W. Biederman set_fs_root(fs, &root); 34518823c079SEric W. Biederman 34528823c079SEric W. Biederman path_put(&root); 34538823c079SEric W. Biederman return 0; 34548823c079SEric W. Biederman } 34558823c079SEric W. Biederman 3456bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 3457bcac25a5SAndrey Vagin { 3458bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 3459bcac25a5SAndrey Vagin } 3460bcac25a5SAndrey Vagin 34618823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 34628823c079SEric W. Biederman .name = "mnt", 34638823c079SEric W. Biederman .type = CLONE_NEWNS, 34648823c079SEric W. Biederman .get = mntns_get, 34658823c079SEric W. Biederman .put = mntns_put, 34668823c079SEric W. Biederman .install = mntns_install, 3467bcac25a5SAndrey Vagin .owner = mntns_owner, 34688823c079SEric W. Biederman }; 3469