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> 29e262e32dSDavid Howells #include <uapi/linux/mount.h> 309bc61ab1SDavid Howells #include <linux/fs_context.h> 319164bb4aSIngo Molnar 3207b20889SRam Pai #include "pnode.h" 33948730b0SAdrian Bunk #include "internal.h" 341da177e4SLinus Torvalds 35d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */ 36d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000; 37d2921684SEric W. Biederman 380818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 390818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 400818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 410818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 420818bf27SAl Viro 430818bf27SAl Viro static __initdata unsigned long mhash_entries; 440818bf27SAl Viro static int __init set_mhash_entries(char *str) 450818bf27SAl Viro { 460818bf27SAl Viro if (!str) 470818bf27SAl Viro return 0; 480818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 490818bf27SAl Viro return 1; 500818bf27SAl Viro } 510818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 520818bf27SAl Viro 530818bf27SAl Viro static __initdata unsigned long mphash_entries; 540818bf27SAl Viro static int __init set_mphash_entries(char *str) 550818bf27SAl Viro { 560818bf27SAl Viro if (!str) 570818bf27SAl Viro return 0; 580818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 590818bf27SAl Viro return 1; 600818bf27SAl Viro } 610818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 6213f14b4dSEric Dumazet 63c7999c36SAl Viro static u64 event; 6473cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 65719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 665addc5ddSAl Viro 6738129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 680818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 69e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 7059aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 711da177e4SLinus Torvalds 72f87fd4c2SMiklos Szeredi /* /sys/fs */ 7300d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 7400d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 75f87fd4c2SMiklos Szeredi 7699b7db7bSNick Piggin /* 7799b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 7899b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 7999b7db7bSNick Piggin * up the tree. 8099b7db7bSNick Piggin * 8199b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 8299b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 8399b7db7bSNick Piggin */ 8448a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 8599b7db7bSNick Piggin 8638129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 871da177e4SLinus Torvalds { 881da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 891da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 900818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 910818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 920818bf27SAl Viro } 930818bf27SAl Viro 940818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 950818bf27SAl Viro { 960818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 970818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 980818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds 101b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 10273cd49ecSMiklos Szeredi { 103169b480eSMatthew Wilcox int res = ida_alloc(&mnt_id_ida, GFP_KERNEL); 10473cd49ecSMiklos Szeredi 105169b480eSMatthew Wilcox if (res < 0) 10673cd49ecSMiklos Szeredi return res; 107169b480eSMatthew Wilcox mnt->mnt_id = res; 108169b480eSMatthew Wilcox return 0; 10973cd49ecSMiklos Szeredi } 11073cd49ecSMiklos Szeredi 111b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 11273cd49ecSMiklos Szeredi { 113169b480eSMatthew Wilcox ida_free(&mnt_id_ida, mnt->mnt_id); 11473cd49ecSMiklos Szeredi } 11573cd49ecSMiklos Szeredi 116719f5d7fSMiklos Szeredi /* 117719f5d7fSMiklos Szeredi * Allocate a new peer group ID 118719f5d7fSMiklos Szeredi */ 1194b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 120719f5d7fSMiklos Szeredi { 121169b480eSMatthew Wilcox int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL); 122f21f6220SAl Viro 123169b480eSMatthew Wilcox if (res < 0) 124f21f6220SAl Viro return res; 125169b480eSMatthew Wilcox mnt->mnt_group_id = res; 126169b480eSMatthew Wilcox return 0; 127719f5d7fSMiklos Szeredi } 128719f5d7fSMiklos Szeredi 129719f5d7fSMiklos Szeredi /* 130719f5d7fSMiklos Szeredi * Release a peer group ID 131719f5d7fSMiklos Szeredi */ 1324b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 133719f5d7fSMiklos Szeredi { 134169b480eSMatthew Wilcox ida_free(&mnt_group_ida, mnt->mnt_group_id); 13515169fe7SAl Viro mnt->mnt_group_id = 0; 136719f5d7fSMiklos Szeredi } 137719f5d7fSMiklos Szeredi 138b3e19d92SNick Piggin /* 139b3e19d92SNick Piggin * vfsmount lock must be held for read 140b3e19d92SNick Piggin */ 14183adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 142b3e19d92SNick Piggin { 143b3e19d92SNick Piggin #ifdef CONFIG_SMP 14468e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 145b3e19d92SNick Piggin #else 146b3e19d92SNick Piggin preempt_disable(); 14768e8a9feSAl Viro mnt->mnt_count += n; 148b3e19d92SNick Piggin preempt_enable(); 149b3e19d92SNick Piggin #endif 150b3e19d92SNick Piggin } 151b3e19d92SNick Piggin 152b3e19d92SNick Piggin /* 153b3e19d92SNick Piggin * vfsmount lock must be held for write 154b3e19d92SNick Piggin */ 15583adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 156b3e19d92SNick Piggin { 157b3e19d92SNick Piggin #ifdef CONFIG_SMP 158f03c6599SAl Viro unsigned int count = 0; 159b3e19d92SNick Piggin int cpu; 160b3e19d92SNick Piggin 161b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 16268e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 163b3e19d92SNick Piggin } 164b3e19d92SNick Piggin 165b3e19d92SNick Piggin return count; 166b3e19d92SNick Piggin #else 16768e8a9feSAl Viro return mnt->mnt_count; 168b3e19d92SNick Piggin #endif 169b3e19d92SNick Piggin } 170b3e19d92SNick Piggin 17187b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p) 17287b95ce0SAl Viro { 17387b95ce0SAl Viro struct mount *m = container_of(p, struct mount, mnt_umount); 17487b95ce0SAl Viro dput(m->mnt_ex_mountpoint); 17587b95ce0SAl Viro pin_remove(p); 17687b95ce0SAl Viro mntput(&m->mnt); 17787b95ce0SAl Viro } 17887b95ce0SAl Viro 179b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1801da177e4SLinus Torvalds { 181c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 182c63181e6SAl Viro if (mnt) { 18373cd49ecSMiklos Szeredi int err; 18473cd49ecSMiklos Szeredi 185c63181e6SAl Viro err = mnt_alloc_id(mnt); 18688b38782SLi Zefan if (err) 18788b38782SLi Zefan goto out_free_cache; 18888b38782SLi Zefan 18988b38782SLi Zefan if (name) { 190fcc139aeSAndrzej Hajda mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL); 191c63181e6SAl Viro if (!mnt->mnt_devname) 19288b38782SLi Zefan goto out_free_id; 19373cd49ecSMiklos Szeredi } 19473cd49ecSMiklos Szeredi 195b3e19d92SNick Piggin #ifdef CONFIG_SMP 196c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 197c63181e6SAl Viro if (!mnt->mnt_pcp) 198b3e19d92SNick Piggin goto out_free_devname; 199b3e19d92SNick Piggin 200c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 201b3e19d92SNick Piggin #else 202c63181e6SAl Viro mnt->mnt_count = 1; 203c63181e6SAl Viro mnt->mnt_writers = 0; 204b3e19d92SNick Piggin #endif 205b3e19d92SNick Piggin 20638129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 207c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 208c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 209c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 210c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 211c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 212c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 213c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2140a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 21599b19d16SEric W. Biederman INIT_LIST_HEAD(&mnt->mnt_umounting); 21687b95ce0SAl Viro init_fs_pin(&mnt->mnt_umount, drop_mountpoint); 2171da177e4SLinus Torvalds } 218c63181e6SAl Viro return mnt; 21988b38782SLi Zefan 220d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 221d3ef3d73Snpiggin@suse.de out_free_devname: 222fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 223d3ef3d73Snpiggin@suse.de #endif 22488b38782SLi Zefan out_free_id: 225c63181e6SAl Viro mnt_free_id(mnt); 22688b38782SLi Zefan out_free_cache: 227c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 22888b38782SLi Zefan return NULL; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2318366025eSDave Hansen /* 2328366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2338366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2348366025eSDave Hansen * We must keep track of when those operations start 2358366025eSDave Hansen * (for permission checks) and when they end, so that 2368366025eSDave Hansen * we can determine when writes are able to occur to 2378366025eSDave Hansen * a filesystem. 2388366025eSDave Hansen */ 2393d733633SDave Hansen /* 2403d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2413d733633SDave Hansen * @mnt: the mount to check for its write status 2423d733633SDave Hansen * 2433d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2443d733633SDave Hansen * It does not guarantee that the filesystem will stay 2453d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2463d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2473d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2483d733633SDave Hansen * r/w. 2493d733633SDave Hansen */ 25043f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt) 2513d733633SDave Hansen { 25243f5e655SDavid Howells return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb); 2533d733633SDave Hansen } 2543d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2553d733633SDave Hansen 25683adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2573d733633SDave Hansen { 258d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 25968e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 260d3ef3d73Snpiggin@suse.de #else 26168e8a9feSAl Viro mnt->mnt_writers++; 262d3ef3d73Snpiggin@suse.de #endif 2633d733633SDave Hansen } 2643d733633SDave Hansen 26583adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2663d733633SDave Hansen { 267d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 26868e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 269d3ef3d73Snpiggin@suse.de #else 27068e8a9feSAl Viro mnt->mnt_writers--; 271d3ef3d73Snpiggin@suse.de #endif 272d3ef3d73Snpiggin@suse.de } 273d3ef3d73Snpiggin@suse.de 27483adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 275d3ef3d73Snpiggin@suse.de { 276d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 277d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2783d733633SDave Hansen int cpu; 2793d733633SDave Hansen 2803d733633SDave Hansen for_each_possible_cpu(cpu) { 28168e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2823d733633SDave Hansen } 2833d733633SDave Hansen 284d3ef3d73Snpiggin@suse.de return count; 285d3ef3d73Snpiggin@suse.de #else 286d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 287d3ef3d73Snpiggin@suse.de #endif 2883d733633SDave Hansen } 2893d733633SDave Hansen 2904ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2914ed5e82fSMiklos Szeredi { 2924ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 2934ed5e82fSMiklos Szeredi return 1; 2944ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 2954ed5e82fSMiklos Szeredi smp_rmb(); 2964ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 2974ed5e82fSMiklos Szeredi } 2984ed5e82fSMiklos Szeredi 2993d733633SDave Hansen /* 300eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 301eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 302eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 303eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3043d733633SDave Hansen */ 3058366025eSDave Hansen /** 306eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 30783adc753SAl Viro * @m: the mount on which to take a write 3088366025eSDave Hansen * 309eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 310eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 311eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 312eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 313eb04c282SJan Kara * called. This is effectively a refcount. 3148366025eSDave Hansen */ 315eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3168366025eSDave Hansen { 31783adc753SAl Viro struct mount *mnt = real_mount(m); 3183d733633SDave Hansen int ret = 0; 3193d733633SDave Hansen 320d3ef3d73Snpiggin@suse.de preempt_disable(); 321c6653a83SNick Piggin mnt_inc_writers(mnt); 322d3ef3d73Snpiggin@suse.de /* 323c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 324d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 325d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 326d3ef3d73Snpiggin@suse.de */ 327d3ef3d73Snpiggin@suse.de smp_mb(); 3286aa7de05SMark Rutland while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 329d3ef3d73Snpiggin@suse.de cpu_relax(); 330d3ef3d73Snpiggin@suse.de /* 331d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 332d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 333d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 334d3ef3d73Snpiggin@suse.de */ 335d3ef3d73Snpiggin@suse.de smp_rmb(); 3364ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 337c6653a83SNick Piggin mnt_dec_writers(mnt); 3383d733633SDave Hansen ret = -EROFS; 3393d733633SDave Hansen } 340d3ef3d73Snpiggin@suse.de preempt_enable(); 341eb04c282SJan Kara 342eb04c282SJan Kara return ret; 343eb04c282SJan Kara } 344eb04c282SJan Kara 345eb04c282SJan Kara /** 346eb04c282SJan Kara * mnt_want_write - get write access to a mount 347eb04c282SJan Kara * @m: the mount on which to take a write 348eb04c282SJan Kara * 349eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 350eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 351eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 352eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 353eb04c282SJan Kara */ 354eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 355eb04c282SJan Kara { 356eb04c282SJan Kara int ret; 357eb04c282SJan Kara 358eb04c282SJan Kara sb_start_write(m->mnt_sb); 359eb04c282SJan Kara ret = __mnt_want_write(m); 360eb04c282SJan Kara if (ret) 361eb04c282SJan Kara sb_end_write(m->mnt_sb); 3623d733633SDave Hansen return ret; 3638366025eSDave Hansen } 3648366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3658366025eSDave Hansen 3668366025eSDave Hansen /** 36796029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 36896029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 36996029c4eSnpiggin@suse.de * 37096029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 37196029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 37296029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 37396029c4eSnpiggin@suse.de * on it. This allows some optimisation. 37496029c4eSnpiggin@suse.de * 37596029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 37696029c4eSnpiggin@suse.de * drop the reference. 37796029c4eSnpiggin@suse.de */ 37896029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 37996029c4eSnpiggin@suse.de { 38096029c4eSnpiggin@suse.de /* superblock may be r/o */ 38196029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 38296029c4eSnpiggin@suse.de return -EROFS; 38396029c4eSnpiggin@suse.de preempt_disable(); 38483adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 38596029c4eSnpiggin@suse.de preempt_enable(); 38696029c4eSnpiggin@suse.de return 0; 38796029c4eSnpiggin@suse.de } 38896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 38996029c4eSnpiggin@suse.de 39096029c4eSnpiggin@suse.de /** 391eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 392eb04c282SJan Kara * @file: the file who's mount on which to take a write 393eb04c282SJan Kara * 394eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 395eb04c282SJan Kara * do some optimisations if the file is open for write already 396eb04c282SJan Kara */ 397eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 398eb04c282SJan Kara { 39983f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 400eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 401eb04c282SJan Kara else 402eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 403eb04c282SJan Kara } 404eb04c282SJan Kara 405eb04c282SJan Kara /** 4067c6893e3SMiklos Szeredi * mnt_want_write_file - get write access to a file's mount 4077c6893e3SMiklos Szeredi * @file: the file who's mount on which to take a write 4087c6893e3SMiklos Szeredi * 4097c6893e3SMiklos Szeredi * This is like mnt_want_write, but it takes a file and can 4107c6893e3SMiklos Szeredi * do some optimisations if the file is open for write already 4117c6893e3SMiklos Szeredi */ 4127c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file) 4137c6893e3SMiklos Szeredi { 4147c6893e3SMiklos Szeredi int ret; 4157c6893e3SMiklos Szeredi 4167c6893e3SMiklos Szeredi sb_start_write(file_inode(file)->i_sb); 4177c6893e3SMiklos Szeredi ret = __mnt_want_write_file(file); 4187c6893e3SMiklos Szeredi if (ret) 4197c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4207c6893e3SMiklos Szeredi return ret; 4217c6893e3SMiklos Szeredi } 42296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 42396029c4eSnpiggin@suse.de 42496029c4eSnpiggin@suse.de /** 425eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4268366025eSDave Hansen * @mnt: the mount on which to give up write access 4278366025eSDave Hansen * 4288366025eSDave Hansen * Tells the low-level filesystem that we are done 4298366025eSDave Hansen * performing writes to it. Must be matched with 430eb04c282SJan Kara * __mnt_want_write() call above. 4318366025eSDave Hansen */ 432eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4338366025eSDave Hansen { 434d3ef3d73Snpiggin@suse.de preempt_disable(); 43583adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 436d3ef3d73Snpiggin@suse.de preempt_enable(); 4378366025eSDave Hansen } 438eb04c282SJan Kara 439eb04c282SJan Kara /** 440eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 441eb04c282SJan Kara * @mnt: the mount on which to give up write access 442eb04c282SJan Kara * 443eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 444eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 445eb04c282SJan Kara * mnt_want_write() call above. 446eb04c282SJan Kara */ 447eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 448eb04c282SJan Kara { 449eb04c282SJan Kara __mnt_drop_write(mnt); 450eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 451eb04c282SJan Kara } 4528366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4538366025eSDave Hansen 454eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 455eb04c282SJan Kara { 456eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 457eb04c282SJan Kara } 458eb04c282SJan Kara 4597c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file) 4607c6893e3SMiklos Szeredi { 461a6795a58SMiklos Szeredi __mnt_drop_write_file(file); 4627c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4637c6893e3SMiklos Szeredi } 4642a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4652a79f17eSAl Viro 46683adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4678366025eSDave Hansen { 4683d733633SDave Hansen int ret = 0; 4693d733633SDave Hansen 470719ea2fbSAl Viro lock_mount_hash(); 47183adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 472d3ef3d73Snpiggin@suse.de /* 473d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 474d3ef3d73Snpiggin@suse.de * should be visible before we do. 475d3ef3d73Snpiggin@suse.de */ 476d3ef3d73Snpiggin@suse.de smp_mb(); 477d3ef3d73Snpiggin@suse.de 478d3ef3d73Snpiggin@suse.de /* 479d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 480d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 481d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 482d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 483d3ef3d73Snpiggin@suse.de * 484d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 485d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 486d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 487d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 488d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 489d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 490d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 491d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 492d3ef3d73Snpiggin@suse.de * we're counting up here. 493d3ef3d73Snpiggin@suse.de */ 494c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 495d3ef3d73Snpiggin@suse.de ret = -EBUSY; 496d3ef3d73Snpiggin@suse.de else 49783adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 498d3ef3d73Snpiggin@suse.de /* 499d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 500d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 501d3ef3d73Snpiggin@suse.de */ 502d3ef3d73Snpiggin@suse.de smp_wmb(); 50383adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 504719ea2fbSAl Viro unlock_mount_hash(); 5053d733633SDave Hansen return ret; 5063d733633SDave Hansen } 5078366025eSDave Hansen 50843f5e655SDavid Howells static int __mnt_unmake_readonly(struct mount *mnt) 5092e4b7fcdSDave Hansen { 510719ea2fbSAl Viro lock_mount_hash(); 51183adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 512719ea2fbSAl Viro unlock_mount_hash(); 51343f5e655SDavid Howells return 0; 5142e4b7fcdSDave Hansen } 5152e4b7fcdSDave Hansen 5164ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5174ed5e82fSMiklos Szeredi { 5184ed5e82fSMiklos Szeredi struct mount *mnt; 5194ed5e82fSMiklos Szeredi int err = 0; 5204ed5e82fSMiklos Szeredi 5218e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5228e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5238e8b8796SMiklos Szeredi return -EBUSY; 5248e8b8796SMiklos Szeredi 525719ea2fbSAl Viro lock_mount_hash(); 5264ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5274ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5284ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5294ed5e82fSMiklos Szeredi smp_mb(); 5304ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5314ed5e82fSMiklos Szeredi err = -EBUSY; 5324ed5e82fSMiklos Szeredi break; 5334ed5e82fSMiklos Szeredi } 5344ed5e82fSMiklos Szeredi } 5354ed5e82fSMiklos Szeredi } 5368e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5378e8b8796SMiklos Szeredi err = -EBUSY; 5388e8b8796SMiklos Szeredi 5394ed5e82fSMiklos Szeredi if (!err) { 5404ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5414ed5e82fSMiklos Szeredi smp_wmb(); 5424ed5e82fSMiklos Szeredi } 5434ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5444ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5454ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5464ed5e82fSMiklos Szeredi } 547719ea2fbSAl Viro unlock_mount_hash(); 5484ed5e82fSMiklos Szeredi 5494ed5e82fSMiklos Szeredi return err; 5504ed5e82fSMiklos Szeredi } 5514ed5e82fSMiklos Szeredi 552b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5531da177e4SLinus Torvalds { 554fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 555d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 55668e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 557d3ef3d73Snpiggin@suse.de #endif 558b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5618ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5628ffcb32eSDavid Howells { 5638ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5648ffcb32eSDavid Howells } 5658ffcb32eSDavid Howells 56648a066e7SAl Viro /* call under rcu_read_lock */ 567294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 56848a066e7SAl Viro { 56948a066e7SAl Viro struct mount *mnt; 57048a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 571294d71ffSAl Viro return 1; 57248a066e7SAl Viro if (bastard == NULL) 573294d71ffSAl Viro return 0; 57448a066e7SAl Viro mnt = real_mount(bastard); 57548a066e7SAl Viro mnt_add_count(mnt, 1); 576119e1ef8SAl Viro smp_mb(); // see mntput_no_expire() 57748a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 578294d71ffSAl Viro return 0; 57948a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 58048a066e7SAl Viro mnt_add_count(mnt, -1); 581294d71ffSAl Viro return 1; 58248a066e7SAl Viro } 583119e1ef8SAl Viro lock_mount_hash(); 584119e1ef8SAl Viro if (unlikely(bastard->mnt_flags & MNT_DOOMED)) { 585119e1ef8SAl Viro mnt_add_count(mnt, -1); 586119e1ef8SAl Viro unlock_mount_hash(); 587119e1ef8SAl Viro return 1; 588119e1ef8SAl Viro } 589119e1ef8SAl Viro unlock_mount_hash(); 590119e1ef8SAl Viro /* caller will mntput() */ 591294d71ffSAl Viro return -1; 592294d71ffSAl Viro } 593294d71ffSAl Viro 594294d71ffSAl Viro /* call under rcu_read_lock */ 595294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 596294d71ffSAl Viro { 597294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 598294d71ffSAl Viro if (likely(!res)) 599294d71ffSAl Viro return true; 600294d71ffSAl Viro if (unlikely(res < 0)) { 60148a066e7SAl Viro rcu_read_unlock(); 60248a066e7SAl Viro mntput(bastard); 60348a066e7SAl Viro rcu_read_lock(); 604294d71ffSAl Viro } 60548a066e7SAl Viro return false; 60648a066e7SAl Viro } 60748a066e7SAl Viro 6081da177e4SLinus Torvalds /* 609474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 61048a066e7SAl Viro * call under rcu_read_lock() 6111da177e4SLinus Torvalds */ 612474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6131da177e4SLinus Torvalds { 61438129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 615474279dcSAl Viro struct mount *p; 6161da177e4SLinus Torvalds 61738129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 618474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 619474279dcSAl Viro return p; 620474279dcSAl Viro return NULL; 6211da177e4SLinus Torvalds } 622474279dcSAl Viro 623474279dcSAl Viro /* 624f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 625f015f126SDavid Howells * 626f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 627f015f126SDavid Howells * following mounts: 628f015f126SDavid Howells * 629f015f126SDavid Howells * mount /dev/sda1 /mnt 630f015f126SDavid Howells * mount /dev/sda2 /mnt 631f015f126SDavid Howells * mount /dev/sda3 /mnt 632f015f126SDavid Howells * 633f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 634f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 635f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 636f015f126SDavid Howells * 637f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 638a05964f3SRam Pai */ 639ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 640a05964f3SRam Pai { 641c7105365SAl Viro struct mount *child_mnt; 64248a066e7SAl Viro struct vfsmount *m; 64348a066e7SAl Viro unsigned seq; 64499b7db7bSNick Piggin 64548a066e7SAl Viro rcu_read_lock(); 64648a066e7SAl Viro do { 64748a066e7SAl Viro seq = read_seqbegin(&mount_lock); 648474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 64948a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 65048a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 65148a066e7SAl Viro rcu_read_unlock(); 65248a066e7SAl Viro return m; 653a05964f3SRam Pai } 654a05964f3SRam Pai 6557af1364fSEric W. Biederman /* 6567af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 6577af1364fSEric W. Biederman * current mount namespace. 6587af1364fSEric W. Biederman * 6597af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 6607af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 6617af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 6627af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 6637af1364fSEric W. Biederman * is a mountpoint. 6647af1364fSEric W. Biederman * 6657af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 6667af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 6677af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 6687af1364fSEric W. Biederman * parent mount. 6697af1364fSEric W. Biederman */ 6707af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 6717af1364fSEric W. Biederman { 6727af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 6737af1364fSEric W. Biederman struct mount *mnt; 6747af1364fSEric W. Biederman bool is_covered = false; 6757af1364fSEric W. Biederman 6767af1364fSEric W. Biederman if (!d_mountpoint(dentry)) 6777af1364fSEric W. Biederman goto out; 6787af1364fSEric W. Biederman 6797af1364fSEric W. Biederman down_read(&namespace_sem); 6807af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 6817af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 6827af1364fSEric W. Biederman if (is_covered) 6837af1364fSEric W. Biederman break; 6847af1364fSEric W. Biederman } 6857af1364fSEric W. Biederman up_read(&namespace_sem); 6867af1364fSEric W. Biederman out: 6877af1364fSEric W. Biederman return is_covered; 6887af1364fSEric W. Biederman } 6897af1364fSEric W. Biederman 690e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 69184d17192SAl Viro { 6920818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 69384d17192SAl Viro struct mountpoint *mp; 69484d17192SAl Viro 6950818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 69684d17192SAl Viro if (mp->m_dentry == dentry) { 69784d17192SAl Viro mp->m_count++; 69884d17192SAl Viro return mp; 69984d17192SAl Viro } 70084d17192SAl Viro } 701e2dfa935SEric W. Biederman return NULL; 702e2dfa935SEric W. Biederman } 703e2dfa935SEric W. Biederman 7043895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 705e2dfa935SEric W. Biederman { 7063895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 707e2dfa935SEric W. Biederman int ret; 70884d17192SAl Viro 7093895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7101e9c75fbSBenjamin Coddington /* might be worth a WARN_ON() */ 7111e9c75fbSBenjamin Coddington if (d_unlinked(dentry)) 7121e9c75fbSBenjamin Coddington return ERR_PTR(-ENOENT); 7133895dbf8SEric W. Biederman mountpoint: 7143895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7153895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 7163895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7173895dbf8SEric W. Biederman if (mp) 7183895dbf8SEric W. Biederman goto done; 71984d17192SAl Viro } 720eed81007SMiklos Szeredi 7213895dbf8SEric W. Biederman if (!new) 7223895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 7233895dbf8SEric W. Biederman if (!new) 7243895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 7253895dbf8SEric W. Biederman 7263895dbf8SEric W. Biederman 7273895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 7283895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 7293895dbf8SEric W. Biederman 7303895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 7313895dbf8SEric W. Biederman if (ret == -EBUSY) 7323895dbf8SEric W. Biederman goto mountpoint; 7333895dbf8SEric W. Biederman 7343895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 7353895dbf8SEric W. Biederman mp = ERR_PTR(ret); 7363895dbf8SEric W. Biederman if (ret) 7373895dbf8SEric W. Biederman goto done; 7383895dbf8SEric W. Biederman 7393895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 7403895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7413895dbf8SEric W. Biederman new->m_dentry = dentry; 7423895dbf8SEric W. Biederman new->m_count = 1; 7433895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 7443895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 7453895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7463895dbf8SEric W. Biederman 7473895dbf8SEric W. Biederman mp = new; 7483895dbf8SEric W. Biederman new = NULL; 7493895dbf8SEric W. Biederman done: 7503895dbf8SEric W. Biederman kfree(new); 75184d17192SAl Viro return mp; 75284d17192SAl Viro } 75384d17192SAl Viro 75484d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 75584d17192SAl Viro { 75684d17192SAl Viro if (!--mp->m_count) { 75784d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7580a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 75984d17192SAl Viro spin_lock(&dentry->d_lock); 76084d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 76184d17192SAl Viro spin_unlock(&dentry->d_lock); 7620818bf27SAl Viro hlist_del(&mp->m_hash); 76384d17192SAl Viro kfree(mp); 76484d17192SAl Viro } 76584d17192SAl Viro } 76684d17192SAl Viro 767143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7681da177e4SLinus Torvalds { 7696b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 77299b7db7bSNick Piggin /* 77399b7db7bSNick Piggin * vfsmount lock must be held for write 77499b7db7bSNick Piggin */ 7756b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7765addc5ddSAl Viro { 7775addc5ddSAl Viro if (ns) { 7785addc5ddSAl Viro ns->event = ++event; 7795addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7805addc5ddSAl Viro } 7815addc5ddSAl Viro } 7825addc5ddSAl Viro 78399b7db7bSNick Piggin /* 78499b7db7bSNick Piggin * vfsmount lock must be held for write 78599b7db7bSNick Piggin */ 7866b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7875addc5ddSAl Viro { 7885addc5ddSAl Viro if (ns && ns->event != event) { 7895addc5ddSAl Viro ns->event = event; 7905addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7915addc5ddSAl Viro } 7925addc5ddSAl Viro } 7935addc5ddSAl Viro 79499b7db7bSNick Piggin /* 79599b7db7bSNick Piggin * vfsmount lock must be held for write 79699b7db7bSNick Piggin */ 7977bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt) 7981da177e4SLinus Torvalds { 7990714a533SAl Viro mnt->mnt_parent = mnt; 800a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8016b41d536SAl Viro list_del_init(&mnt->mnt_child); 80238129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8030a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 80484d17192SAl Viro put_mountpoint(mnt->mnt_mp); 80584d17192SAl Viro mnt->mnt_mp = NULL; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 80899b7db7bSNick Piggin /* 80999b7db7bSNick Piggin * vfsmount lock must be held for write 81099b7db7bSNick Piggin */ 8117bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path) 8127bdb11deSEric W. Biederman { 8137bdb11deSEric W. Biederman old_path->dentry = mnt->mnt_mountpoint; 8147bdb11deSEric W. Biederman old_path->mnt = &mnt->mnt_parent->mnt; 8157bdb11deSEric W. Biederman unhash_mnt(mnt); 8167bdb11deSEric W. Biederman } 8177bdb11deSEric W. Biederman 8187bdb11deSEric W. Biederman /* 8197bdb11deSEric W. Biederman * vfsmount lock must be held for write 8207bdb11deSEric W. Biederman */ 8216a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 8226a46c573SEric W. Biederman { 8236a46c573SEric W. Biederman /* old mountpoint will be dropped when we can do that */ 8246a46c573SEric W. Biederman mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint; 8256a46c573SEric W. Biederman unhash_mnt(mnt); 8266a46c573SEric W. Biederman } 8276a46c573SEric W. Biederman 8286a46c573SEric W. Biederman /* 8296a46c573SEric W. Biederman * vfsmount lock must be held for write 8306a46c573SEric W. Biederman */ 83184d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 83284d17192SAl Viro struct mountpoint *mp, 83344d964d6SAl Viro struct mount *child_mnt) 834b90fa9aeSRam Pai { 83584d17192SAl Viro mp->m_count++; 8363a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 83784d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 8383a2393d7SAl Viro child_mnt->mnt_parent = mnt; 83984d17192SAl Viro child_mnt->mnt_mp = mp; 8400a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 841b90fa9aeSRam Pai } 842b90fa9aeSRam Pai 8431064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 8441064f874SEric W. Biederman { 8451064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 8461064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8471064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8481064f874SEric W. Biederman } 8491064f874SEric W. Biederman 85099b7db7bSNick Piggin /* 85199b7db7bSNick Piggin * vfsmount lock must be held for write 85299b7db7bSNick Piggin */ 85384d17192SAl Viro static void attach_mnt(struct mount *mnt, 85484d17192SAl Viro struct mount *parent, 85584d17192SAl Viro struct mountpoint *mp) 8561da177e4SLinus Torvalds { 85784d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 8581064f874SEric W. Biederman __attach_mnt(mnt, parent); 859b90fa9aeSRam Pai } 860b90fa9aeSRam Pai 8611064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 86212a5b529SAl Viro { 8631064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 8641064f874SEric W. Biederman struct dentry *old_mountpoint = mnt->mnt_mountpoint; 8651064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 8661064f874SEric W. Biederman 8671064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 8681064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 8691064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 8701064f874SEric W. Biederman 8711064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 8721064f874SEric W. Biederman 8731064f874SEric W. Biederman put_mountpoint(old_mp); 8741064f874SEric W. Biederman 8751064f874SEric W. Biederman /* 8761064f874SEric W. Biederman * Safely avoid even the suggestion this code might sleep or 8771064f874SEric W. Biederman * lock the mount hash by taking advantage of the knowledge that 8781064f874SEric W. Biederman * mnt_change_mountpoint will not release the final reference 8791064f874SEric W. Biederman * to a mountpoint. 8801064f874SEric W. Biederman * 8811064f874SEric W. Biederman * During mounting, the mount passed in as the parent mount will 8821064f874SEric W. Biederman * continue to use the old mountpoint and during unmounting, the 8831064f874SEric W. Biederman * old mountpoint will continue to exist until namespace_unlock, 8841064f874SEric W. Biederman * which happens well after mnt_change_mountpoint. 8851064f874SEric W. Biederman */ 8861064f874SEric W. Biederman spin_lock(&old_mountpoint->d_lock); 8871064f874SEric W. Biederman old_mountpoint->d_lockref.count--; 8881064f874SEric W. Biederman spin_unlock(&old_mountpoint->d_lock); 8891064f874SEric W. Biederman 8901064f874SEric W. Biederman mnt_add_count(old_parent, -1); 89112a5b529SAl Viro } 89212a5b529SAl Viro 893b90fa9aeSRam Pai /* 89499b7db7bSNick Piggin * vfsmount lock must be held for write 895b90fa9aeSRam Pai */ 8961064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 897b90fa9aeSRam Pai { 8980714a533SAl Viro struct mount *parent = mnt->mnt_parent; 89983adc753SAl Viro struct mount *m; 900b90fa9aeSRam Pai LIST_HEAD(head); 901143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 902b90fa9aeSRam Pai 9030714a533SAl Viro BUG_ON(parent == mnt); 904b90fa9aeSRam Pai 9051a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 906f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 907143c8c91SAl Viro m->mnt_ns = n; 908f03c6599SAl Viro 909b90fa9aeSRam Pai list_splice(&head, n->list.prev); 910b90fa9aeSRam Pai 911d2921684SEric W. Biederman n->mounts += n->pending_mounts; 912d2921684SEric W. Biederman n->pending_mounts = 0; 913d2921684SEric W. Biederman 9141064f874SEric W. Biederman __attach_mnt(mnt, parent); 9156b3286edSKirill Korotaev touch_mnt_namespace(n); 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 918909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 9191da177e4SLinus Torvalds { 9206b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 9216b41d536SAl Viro if (next == &p->mnt_mounts) { 9221da177e4SLinus Torvalds while (1) { 923909b0a88SAl Viro if (p == root) 9241da177e4SLinus Torvalds return NULL; 9256b41d536SAl Viro next = p->mnt_child.next; 9266b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 9271da177e4SLinus Torvalds break; 9280714a533SAl Viro p = p->mnt_parent; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds } 9316b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 934315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 9359676f0c6SRam Pai { 9366b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 9376b41d536SAl Viro while (prev != &p->mnt_mounts) { 9386b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 9396b41d536SAl Viro prev = p->mnt_mounts.prev; 9409676f0c6SRam Pai } 9419676f0c6SRam Pai return p; 9429676f0c6SRam Pai } 9439676f0c6SRam Pai 9448f291889SAl Viro /** 9458f291889SAl Viro * vfs_create_mount - Create a mount for a configured superblock 9468f291889SAl Viro * @fc: The configuration context with the superblock attached 9478f291889SAl Viro * 9488f291889SAl Viro * Create a mount to an already configured superblock. If necessary, the 9498f291889SAl Viro * caller should invoke vfs_get_tree() before calling this. 9508f291889SAl Viro * 9518f291889SAl Viro * Note that this does not attach the mount to anything. 9528f291889SAl Viro */ 9538f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc) 9548f291889SAl Viro { 9558f291889SAl Viro struct mount *mnt; 9568f291889SAl Viro 9578f291889SAl Viro if (!fc->root) 9588f291889SAl Viro return ERR_PTR(-EINVAL); 9598f291889SAl Viro 9608f291889SAl Viro mnt = alloc_vfsmnt(fc->source ?: "none"); 9618f291889SAl Viro if (!mnt) 9628f291889SAl Viro return ERR_PTR(-ENOMEM); 9638f291889SAl Viro 9648f291889SAl Viro if (fc->sb_flags & SB_KERNMOUNT) 9658f291889SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9668f291889SAl Viro 9678f291889SAl Viro atomic_inc(&fc->root->d_sb->s_active); 9688f291889SAl Viro mnt->mnt.mnt_sb = fc->root->d_sb; 9698f291889SAl Viro mnt->mnt.mnt_root = dget(fc->root); 9708f291889SAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9718f291889SAl Viro mnt->mnt_parent = mnt; 9728f291889SAl Viro 9738f291889SAl Viro lock_mount_hash(); 9748f291889SAl Viro list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts); 9758f291889SAl Viro unlock_mount_hash(); 9768f291889SAl Viro return &mnt->mnt; 9778f291889SAl Viro } 9788f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount); 9798f291889SAl Viro 9808f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc) 9818f291889SAl Viro { 9828f291889SAl Viro int err = vfs_get_tree(fc); 9838f291889SAl Viro if (!err) { 9848f291889SAl Viro up_write(&fc->root->d_sb->s_umount); 9858f291889SAl Viro return vfs_create_mount(fc); 9868f291889SAl Viro } 9878f291889SAl Viro return ERR_PTR(err); 9888f291889SAl Viro } 9898f291889SAl Viro EXPORT_SYMBOL(fc_mount); 9908f291889SAl Viro 9919bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type, 9929bc61ab1SDavid Howells int flags, const char *name, 9939bc61ab1SDavid Howells void *data) 9949d412a43SAl Viro { 9959bc61ab1SDavid Howells struct fs_context *fc; 9968f291889SAl Viro struct vfsmount *mnt; 9979bc61ab1SDavid Howells int ret = 0; 9989d412a43SAl Viro 9999d412a43SAl Viro if (!type) 10003e1aeb00SDavid Howells return ERR_PTR(-EINVAL); 10019d412a43SAl Viro 10029bc61ab1SDavid Howells fc = fs_context_for_mount(type, flags); 10039bc61ab1SDavid Howells if (IS_ERR(fc)) 10049bc61ab1SDavid Howells return ERR_CAST(fc); 10059bc61ab1SDavid Howells 10063e1aeb00SDavid Howells if (name) 10073e1aeb00SDavid Howells ret = vfs_parse_fs_string(fc, "source", 10083e1aeb00SDavid Howells name, strlen(name)); 10099bc61ab1SDavid Howells if (!ret) 10109bc61ab1SDavid Howells ret = parse_monolithic_mount_data(fc, data); 10119bc61ab1SDavid Howells if (!ret) 10128f291889SAl Viro mnt = fc_mount(fc); 10138f291889SAl Viro else 10148f291889SAl Viro mnt = ERR_PTR(ret); 10159d412a43SAl Viro 10169bc61ab1SDavid Howells put_fs_context(fc); 10178f291889SAl Viro return mnt; 10189d412a43SAl Viro } 10199d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 10209d412a43SAl Viro 102193faccbbSEric W. Biederman struct vfsmount * 102293faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 102393faccbbSEric W. Biederman const char *name, void *data) 102493faccbbSEric W. Biederman { 102593faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 102693faccbbSEric W. Biederman * through from the parent mount to the submount don't support 102793faccbbSEric W. Biederman * unprivileged mounts with submounts. 102893faccbbSEric W. Biederman */ 102993faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 103093faccbbSEric W. Biederman return ERR_PTR(-EPERM); 103193faccbbSEric W. Biederman 1032e462ec50SDavid Howells return vfs_kern_mount(type, SB_SUBMOUNT, name, data); 103393faccbbSEric W. Biederman } 103493faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 103593faccbbSEric W. Biederman 103687129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 103736341f64SRam Pai int flag) 10381da177e4SLinus Torvalds { 103987129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 1040be34d1a3SDavid Howells struct mount *mnt; 1041be34d1a3SDavid Howells int err; 10421da177e4SLinus Torvalds 1043be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1044be34d1a3SDavid Howells if (!mnt) 1045be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1046be34d1a3SDavid Howells 10477a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 104815169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1049719f5d7fSMiklos Szeredi else 105015169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1051719f5d7fSMiklos Szeredi 105215169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1053be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1054719f5d7fSMiklos Szeredi if (err) 1055719f5d7fSMiklos Szeredi goto out_free; 1056719f5d7fSMiklos Szeredi } 1057719f5d7fSMiklos Szeredi 105816a34adbSAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags; 105916a34adbSAl Viro mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL); 10605ff9d8a6SEric W. Biederman 10611da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1062b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1063b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1064a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10650714a533SAl Viro mnt->mnt_parent = mnt; 1066719ea2fbSAl Viro lock_mount_hash(); 106739f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1068719ea2fbSAl Viro unlock_mount_hash(); 1069b90fa9aeSRam Pai 10707a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 10717a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 10726776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 107332301920SAl Viro mnt->mnt_master = old; 1074fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 10758aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1076fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 10776776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1078d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 10796776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1080d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 10815235d448SAl Viro } else { 10825235d448SAl Viro CLEAR_MNT_SHARED(mnt); 10835afe0022SRam Pai } 1084b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 10850f0afb1dSAl Viro set_mnt_shared(mnt); 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 10881da177e4SLinus Torvalds * as the original if that was on one */ 108936341f64SRam Pai if (flag & CL_EXPIRE) { 10906776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 10916776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 10921da177e4SLinus Torvalds } 1093be34d1a3SDavid Howells 1094cb338d06SAl Viro return mnt; 1095719f5d7fSMiklos Szeredi 1096719f5d7fSMiklos Szeredi out_free: 10978ffcb32eSDavid Howells mnt_free_id(mnt); 1098719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1099be34d1a3SDavid Howells return ERR_PTR(err); 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11029ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 11039ea459e1SAl Viro { 11049ea459e1SAl Viro /* 11059ea459e1SAl Viro * This probably indicates that somebody messed 11069ea459e1SAl Viro * up a mnt_want/drop_write() pair. If this 11079ea459e1SAl Viro * happens, the filesystem was probably unable 11089ea459e1SAl Viro * to make r/w->r/o transitions. 11099ea459e1SAl Viro */ 11109ea459e1SAl Viro /* 11119ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 11129ea459e1SAl Viro * so mnt_get_writers() below is safe. 11139ea459e1SAl Viro */ 11149ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 11159ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 11169ea459e1SAl Viro mnt_pin_kill(mnt); 11179ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 11189ea459e1SAl Viro dput(mnt->mnt.mnt_root); 11199ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 11209ea459e1SAl Viro mnt_free_id(mnt); 11219ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11229ea459e1SAl Viro } 11239ea459e1SAl Viro 11249ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11259ea459e1SAl Viro { 11269ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11279ea459e1SAl Viro } 11289ea459e1SAl Viro 11299ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11309ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11319ea459e1SAl Viro { 11329ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 113329785735SByungchul Park struct mount *m, *t; 11349ea459e1SAl Viro 113529785735SByungchul Park llist_for_each_entry_safe(m, t, node, mnt_llist) 113629785735SByungchul Park cleanup_mnt(m); 11379ea459e1SAl Viro } 11389ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 11399ea459e1SAl Viro 1140900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 11417b7b1aceSAl Viro { 114248a066e7SAl Viro rcu_read_lock(); 11439ea0a46cSAl Viro if (likely(READ_ONCE(mnt->mnt_ns))) { 11449ea0a46cSAl Viro /* 11459ea0a46cSAl Viro * Since we don't do lock_mount_hash() here, 11469ea0a46cSAl Viro * ->mnt_ns can change under us. However, if it's 11479ea0a46cSAl Viro * non-NULL, then there's a reference that won't 11489ea0a46cSAl Viro * be dropped until after an RCU delay done after 11499ea0a46cSAl Viro * turning ->mnt_ns NULL. So if we observe it 11509ea0a46cSAl Viro * non-NULL under rcu_read_lock(), the reference 11519ea0a46cSAl Viro * we are dropping is not the final one. 11529ea0a46cSAl Viro */ 1153aa9c0e07SAl Viro mnt_add_count(mnt, -1); 115448a066e7SAl Viro rcu_read_unlock(); 115599b7db7bSNick Piggin return; 1156b3e19d92SNick Piggin } 1157719ea2fbSAl Viro lock_mount_hash(); 1158119e1ef8SAl Viro /* 1159119e1ef8SAl Viro * make sure that if __legitimize_mnt() has not seen us grab 1160119e1ef8SAl Viro * mount_lock, we'll see their refcount increment here. 1161119e1ef8SAl Viro */ 1162119e1ef8SAl Viro smp_mb(); 11639ea0a46cSAl Viro mnt_add_count(mnt, -1); 1164b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 116548a066e7SAl Viro rcu_read_unlock(); 1166719ea2fbSAl Viro unlock_mount_hash(); 116799b7db7bSNick Piggin return; 116899b7db7bSNick Piggin } 116948a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 117048a066e7SAl Viro rcu_read_unlock(); 117148a066e7SAl Viro unlock_mount_hash(); 117248a066e7SAl Viro return; 117348a066e7SAl Viro } 117448a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 117548a066e7SAl Viro rcu_read_unlock(); 1176962830dfSAndi Kleen 117739f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1178ce07d891SEric W. Biederman 1179ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1180ce07d891SEric W. Biederman struct mount *p, *tmp; 1181ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 1182ce07d891SEric W. Biederman umount_mnt(p); 1183ce07d891SEric W. Biederman } 1184ce07d891SEric W. Biederman } 1185719ea2fbSAl Viro unlock_mount_hash(); 1186649a795aSAl Viro 11879ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 11889ea459e1SAl Viro struct task_struct *task = current; 11899ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 11909ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 11919ea459e1SAl Viro if (!task_work_add(task, &mnt->mnt_rcu, true)) 11929ea459e1SAl Viro return; 11939ea459e1SAl Viro } 11949ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 11959ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 11969ea459e1SAl Viro return; 11979ea459e1SAl Viro } 11989ea459e1SAl Viro cleanup_mnt(mnt); 1199b3e19d92SNick Piggin } 1200b3e19d92SNick Piggin 1201b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1202b3e19d92SNick Piggin { 1203b3e19d92SNick Piggin if (mnt) { 1204863d684fSAl Viro struct mount *m = real_mount(mnt); 1205b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1206863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1207863d684fSAl Viro m->mnt_expiry_mark = 0; 1208863d684fSAl Viro mntput_no_expire(m); 1209b3e19d92SNick Piggin } 1210b3e19d92SNick Piggin } 1211b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1212b3e19d92SNick Piggin 1213b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1214b3e19d92SNick Piggin { 1215b3e19d92SNick Piggin if (mnt) 121683adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1217b3e19d92SNick Piggin return mnt; 1218b3e19d92SNick Piggin } 1219b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1220b3e19d92SNick Piggin 1221c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current 1222c6609c0aSIan Kent * namespace. 1223c6609c0aSIan Kent * 1224c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1225c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1226c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1227c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1228c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1229c6609c0aSIan Kent * alone. 1230c6609c0aSIan Kent */ 1231c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1232c6609c0aSIan Kent { 1233c6609c0aSIan Kent unsigned seq; 1234c6609c0aSIan Kent bool res; 1235c6609c0aSIan Kent 1236c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1237c6609c0aSIan Kent return false; 1238c6609c0aSIan Kent 1239c6609c0aSIan Kent rcu_read_lock(); 1240c6609c0aSIan Kent do { 1241c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1242c6609c0aSIan Kent res = __path_is_mountpoint(path); 1243c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1244c6609c0aSIan Kent rcu_read_unlock(); 1245c6609c0aSIan Kent 1246c6609c0aSIan Kent return res; 1247c6609c0aSIan Kent } 1248c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1249c6609c0aSIan Kent 1250ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 12517b7b1aceSAl Viro { 12523064c356SAl Viro struct mount *p; 12533064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 12543064c356SAl Viro if (IS_ERR(p)) 12553064c356SAl Viro return ERR_CAST(p); 12563064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 12573064c356SAl Viro return &p->mnt; 12587b7b1aceSAl Viro } 12591da177e4SLinus Torvalds 1260a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 12610226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 12621da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 12631da177e4SLinus Torvalds { 1264ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12651da177e4SLinus Torvalds 1266390c6843SRam Pai down_read(&namespace_sem); 1267c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1268c7999c36SAl Viro void *v = p->cached_mount; 1269c7999c36SAl Viro if (*pos == p->cached_index) 1270c7999c36SAl Viro return v; 1271c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1272c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1273c7999c36SAl Viro return p->cached_mount = v; 1274c7999c36SAl Viro } 1275c7999c36SAl Viro } 1276c7999c36SAl Viro 1277c7999c36SAl Viro p->cached_event = p->ns->event; 1278c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1279c7999c36SAl Viro p->cached_index = *pos; 1280c7999c36SAl Viro return p->cached_mount; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 12841da177e4SLinus Torvalds { 1285ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 1286b0765fb8SPavel Emelianov 1287c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1288c7999c36SAl Viro p->cached_index = *pos; 1289c7999c36SAl Viro return p->cached_mount; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 12931da177e4SLinus Torvalds { 1294390c6843SRam Pai up_read(&namespace_sem); 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 12970226f492SAl Viro static int m_show(struct seq_file *m, void *v) 12989f5596afSAl Viro { 1299ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13001a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 13010226f492SAl Viro return p->show(m, &r->mnt); 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 1304a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 13051da177e4SLinus Torvalds .start = m_start, 13061da177e4SLinus Torvalds .next = m_next, 13071da177e4SLinus Torvalds .stop = m_stop, 13080226f492SAl Viro .show = m_show, 1309b4629fe2SChuck Lever }; 1310a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1311b4629fe2SChuck Lever 13121da177e4SLinus Torvalds /** 13131da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 13141da177e4SLinus Torvalds * @mnt: root of mount tree 13151da177e4SLinus Torvalds * 13161da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 13171da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 13181da177e4SLinus Torvalds * busy. 13191da177e4SLinus Torvalds */ 1320909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13211da177e4SLinus Torvalds { 1322909b0a88SAl Viro struct mount *mnt = real_mount(m); 132336341f64SRam Pai int actual_refs = 0; 132436341f64SRam Pai int minimum_refs = 0; 1325315fc83eSAl Viro struct mount *p; 1326909b0a88SAl Viro BUG_ON(!m); 13271da177e4SLinus Torvalds 1328b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1329719ea2fbSAl Viro lock_mount_hash(); 1330909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 133183adc753SAl Viro actual_refs += mnt_get_count(p); 13321da177e4SLinus Torvalds minimum_refs += 2; 13331da177e4SLinus Torvalds } 1334719ea2fbSAl Viro unlock_mount_hash(); 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds if (actual_refs > minimum_refs) 13371da177e4SLinus Torvalds return 0; 1338e3474a8eSIan Kent 1339e3474a8eSIan Kent return 1; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds /** 13451da177e4SLinus Torvalds * may_umount - check if a mount point is busy 13461da177e4SLinus Torvalds * @mnt: root of mount 13471da177e4SLinus Torvalds * 13481da177e4SLinus Torvalds * This is called to check if a mount point has any 13491da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 13501da177e4SLinus Torvalds * mount has sub mounts this will return busy 13511da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 13521da177e4SLinus Torvalds * 13531da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 13541da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 13551da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 13561da177e4SLinus Torvalds */ 13571da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 13581da177e4SLinus Torvalds { 1359e3474a8eSIan Kent int ret = 1; 13608ad08d8aSAl Viro down_read(&namespace_sem); 1361719ea2fbSAl Viro lock_mount_hash(); 13621ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1363e3474a8eSIan Kent ret = 0; 1364719ea2fbSAl Viro unlock_mount_hash(); 13658ad08d8aSAl Viro up_read(&namespace_sem); 1366a05964f3SRam Pai return ret; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 13701da177e4SLinus Torvalds 137138129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1372e3197d83SAl Viro 137397216be0SAl Viro static void namespace_unlock(void) 13741da177e4SLinus Torvalds { 1375a3b3c562SEric W. Biederman struct hlist_head head; 137697216be0SAl Viro 1377a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 1378a3b3c562SEric W. Biederman 137997216be0SAl Viro up_write(&namespace_sem); 1380a3b3c562SEric W. Biederman 1381a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 138297216be0SAl Viro return; 138397216be0SAl Viro 138422cb7405SNeilBrown synchronize_rcu_expedited(); 138548a066e7SAl Viro 138687b95ce0SAl Viro group_pin_kill(&head); 138770fbcdf4SRam Pai } 138870fbcdf4SRam Pai 138997216be0SAl Viro static inline void namespace_lock(void) 1390e3197d83SAl Viro { 139197216be0SAl Viro down_write(&namespace_sem); 1392e3197d83SAl Viro } 1393e3197d83SAl Viro 1394e819f152SEric W. Biederman enum umount_tree_flags { 1395e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1396e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1397e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1398e819f152SEric W. Biederman }; 1399f2d0a123SEric W. Biederman 1400f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1401f2d0a123SEric W. Biederman { 1402f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1403f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1404f2d0a123SEric W. Biederman return true; 1405f2d0a123SEric W. Biederman 1406f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1407f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1408f2d0a123SEric W. Biederman return true; 1409f2d0a123SEric W. Biederman 1410f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1411f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1412f2d0a123SEric W. Biederman * connected to mounted mounts. 1413f2d0a123SEric W. Biederman */ 1414f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1415f2d0a123SEric W. Biederman return true; 1416f2d0a123SEric W. Biederman 1417f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1418f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1419f2d0a123SEric W. Biederman return false; 1420f2d0a123SEric W. Biederman 1421f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1422f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1423f2d0a123SEric W. Biederman return false; 1424f2d0a123SEric W. Biederman 1425f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1426f2d0a123SEric W. Biederman return true; 1427f2d0a123SEric W. Biederman } 1428f2d0a123SEric W. Biederman 142999b7db7bSNick Piggin /* 143048a066e7SAl Viro * mount_lock must be held 143199b7db7bSNick Piggin * namespace_sem must be held for write 143299b7db7bSNick Piggin */ 1433e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 143470fbcdf4SRam Pai { 1435c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1436315fc83eSAl Viro struct mount *p; 143770fbcdf4SRam Pai 14385d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 14395d88457eSEric W. Biederman propagate_mount_unlock(mnt); 14405d88457eSEric W. Biederman 1441c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1442590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1443590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1444c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1445590ce4bcSEric W. Biederman } 1446c003b26fSEric W. Biederman 1447411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1448c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1449c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 145038129a13SAl Viro } 145170fbcdf4SRam Pai 1452c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1453e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 14547b8a53fdSAl Viro propagate_umount(&tmp_list); 1455a05964f3SRam Pai 1456c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1457d2921684SEric W. Biederman struct mnt_namespace *ns; 1458ce07d891SEric W. Biederman bool disconnect; 1459c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 14606776db3dSAl Viro list_del_init(&p->mnt_expire); 14611a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1462d2921684SEric W. Biederman ns = p->mnt_ns; 1463d2921684SEric W. Biederman if (ns) { 1464d2921684SEric W. Biederman ns->mounts--; 1465d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1466d2921684SEric W. Biederman } 146763d37a84SAl Viro p->mnt_ns = NULL; 1468e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 146948a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 147087b95ce0SAl Viro 1471f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1472ce07d891SEric W. Biederman 1473ce07d891SEric W. Biederman pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt, 1474ce07d891SEric W. Biederman disconnect ? &unmounted : NULL); 1475676da58dSAl Viro if (mnt_has_parent(p)) { 147681b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1477ce07d891SEric W. Biederman if (!disconnect) { 1478ce07d891SEric W. Biederman /* Don't forget about p */ 1479ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1480ce07d891SEric W. Biederman } else { 14816a46c573SEric W. Biederman umount_mnt(p); 14827c4b93d8SAl Viro } 1483ce07d891SEric W. Biederman } 14840f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 148538129a13SAl Viro } 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 1488b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1489c35038beSAl Viro 14908d0347f6SDavid Howells static int do_umount_root(struct super_block *sb) 14918d0347f6SDavid Howells { 14928d0347f6SDavid Howells int ret = 0; 14938d0347f6SDavid Howells 14948d0347f6SDavid Howells down_write(&sb->s_umount); 14958d0347f6SDavid Howells if (!sb_rdonly(sb)) { 14968d0347f6SDavid Howells struct fs_context *fc; 14978d0347f6SDavid Howells 14988d0347f6SDavid Howells fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY, 14998d0347f6SDavid Howells SB_RDONLY); 15008d0347f6SDavid Howells if (IS_ERR(fc)) { 15018d0347f6SDavid Howells ret = PTR_ERR(fc); 15028d0347f6SDavid Howells } else { 15038d0347f6SDavid Howells ret = parse_monolithic_mount_data(fc, NULL); 15048d0347f6SDavid Howells if (!ret) 15058d0347f6SDavid Howells ret = reconfigure_super(fc); 15068d0347f6SDavid Howells put_fs_context(fc); 15078d0347f6SDavid Howells } 15088d0347f6SDavid Howells } 15098d0347f6SDavid Howells up_write(&sb->s_umount); 15108d0347f6SDavid Howells return ret; 15118d0347f6SDavid Howells } 15128d0347f6SDavid Howells 15131ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 15141da177e4SLinus Torvalds { 15151ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 15161da177e4SLinus Torvalds int retval; 15171da177e4SLinus Torvalds 15181ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 15191da177e4SLinus Torvalds if (retval) 15201da177e4SLinus Torvalds return retval; 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /* 15231da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 15241da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 15251da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 15261da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 15271da177e4SLinus Torvalds */ 15281da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 15291ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 15301da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 15311da177e4SLinus Torvalds return -EINVAL; 15321da177e4SLinus Torvalds 1533b3e19d92SNick Piggin /* 1534b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1535b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1536b3e19d92SNick Piggin */ 1537719ea2fbSAl Viro lock_mount_hash(); 153883adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1539719ea2fbSAl Viro unlock_mount_hash(); 15401da177e4SLinus Torvalds return -EBUSY; 1541b3e19d92SNick Piggin } 1542719ea2fbSAl Viro unlock_mount_hash(); 15431da177e4SLinus Torvalds 1544863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 15451da177e4SLinus Torvalds return -EAGAIN; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* 15491da177e4SLinus Torvalds * If we may have to abort operations to get out of this 15501da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 15511da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 15521da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 15531da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 15541da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 15551da177e4SLinus Torvalds * about for the moment. 15561da177e4SLinus Torvalds */ 15571da177e4SLinus Torvalds 155842faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 155942faad99SAl Viro sb->s_op->umount_begin(sb); 156042faad99SAl Viro } 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds /* 15631da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 15641da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 15651da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 15661da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 15671da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 15681da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 15691da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 15701da177e4SLinus Torvalds */ 15711ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 15721da177e4SLinus Torvalds /* 15731da177e4SLinus Torvalds * Special case for "unmounting" root ... 15741da177e4SLinus Torvalds * we just try to remount it readonly. 15751da177e4SLinus Torvalds */ 1576bc6155d1SEric W. Biederman if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 1577a1480dccSAndy Lutomirski return -EPERM; 15788d0347f6SDavid Howells return do_umount_root(sb); 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds 158197216be0SAl Viro namespace_lock(); 1582719ea2fbSAl Viro lock_mount_hash(); 15831da177e4SLinus Torvalds 158425d202edSEric W. Biederman /* Recheck MNT_LOCKED with the locks held */ 158525d202edSEric W. Biederman retval = -EINVAL; 158625d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 158725d202edSEric W. Biederman goto out; 158825d202edSEric W. Biederman 158925d202edSEric W. Biederman event++; 159048a066e7SAl Viro if (flags & MNT_DETACH) { 159148a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1592e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 159348a066e7SAl Viro retval = 0; 159448a066e7SAl Viro } else { 1595b54b9be7SAl Viro shrink_submounts(mnt); 15961da177e4SLinus Torvalds retval = -EBUSY; 159748a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 15981a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1599e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 16001da177e4SLinus Torvalds retval = 0; 16011da177e4SLinus Torvalds } 160248a066e7SAl Viro } 160325d202edSEric W. Biederman out: 1604719ea2fbSAl Viro unlock_mount_hash(); 1605e3197d83SAl Viro namespace_unlock(); 16061da177e4SLinus Torvalds return retval; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds /* 161080b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 161180b5dce8SEric W. Biederman * 161280b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 161380b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 161480b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 161580b5dce8SEric W. Biederman * leaking them. 161680b5dce8SEric W. Biederman * 161780b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 161880b5dce8SEric W. Biederman */ 161980b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 162080b5dce8SEric W. Biederman { 162180b5dce8SEric W. Biederman struct mountpoint *mp; 162280b5dce8SEric W. Biederman struct mount *mnt; 162380b5dce8SEric W. Biederman 162480b5dce8SEric W. Biederman namespace_lock(); 16253895dbf8SEric W. Biederman lock_mount_hash(); 162680b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1627f53e5797SEric W. Biederman if (IS_ERR_OR_NULL(mp)) 162880b5dce8SEric W. Biederman goto out_unlock; 162980b5dce8SEric W. Biederman 1630e06b933eSAndrey Ulanov event++; 163180b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 163280b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1633ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1634fe78fcc8SEric W. Biederman hlist_add_head(&mnt->mnt_umount.s_list, &unmounted); 1635fe78fcc8SEric W. Biederman umount_mnt(mnt); 1636ce07d891SEric W. Biederman } 1637e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 163880b5dce8SEric W. Biederman } 163980b5dce8SEric W. Biederman put_mountpoint(mp); 164080b5dce8SEric W. Biederman out_unlock: 16413895dbf8SEric W. Biederman unlock_mount_hash(); 164280b5dce8SEric W. Biederman namespace_unlock(); 164380b5dce8SEric W. Biederman } 164480b5dce8SEric W. Biederman 164580b5dce8SEric W. Biederman /* 16469b40bc90SAl Viro * Is the caller allowed to modify his namespace? 16479b40bc90SAl Viro */ 16489b40bc90SAl Viro static inline bool may_mount(void) 16499b40bc90SAl Viro { 16509b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 16519b40bc90SAl Viro } 16529b40bc90SAl Viro 16539e8925b6SJeff Layton static inline bool may_mandlock(void) 16549e8925b6SJeff Layton { 16559e8925b6SJeff Layton #ifndef CONFIG_MANDATORY_FILE_LOCKING 16569e8925b6SJeff Layton return false; 16579e8925b6SJeff Layton #endif 165895ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 16599e8925b6SJeff Layton } 16609e8925b6SJeff Layton 16619b40bc90SAl Viro /* 16621da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 16631da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 16641da177e4SLinus Torvalds * 16651da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 16661da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 16671da177e4SLinus Torvalds */ 16681da177e4SLinus Torvalds 16693a18ef5cSDominik Brodowski int ksys_umount(char __user *name, int flags) 16701da177e4SLinus Torvalds { 16712d8f3038SAl Viro struct path path; 1672900148dcSAl Viro struct mount *mnt; 16731da177e4SLinus Torvalds int retval; 1674db1f05bbSMiklos Szeredi int lookup_flags = 0; 16751da177e4SLinus Torvalds 1676db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1677db1f05bbSMiklos Szeredi return -EINVAL; 1678db1f05bbSMiklos Szeredi 16799b40bc90SAl Viro if (!may_mount()) 16809b40bc90SAl Viro return -EPERM; 16819b40bc90SAl Viro 1682db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1683db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1684db1f05bbSMiklos Szeredi 1685197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 16861da177e4SLinus Torvalds if (retval) 16871da177e4SLinus Torvalds goto out; 1688900148dcSAl Viro mnt = real_mount(path.mnt); 16891da177e4SLinus Torvalds retval = -EINVAL; 16902d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 16911da177e4SLinus Torvalds goto dput_and_out; 1692143c8c91SAl Viro if (!check_mnt(mnt)) 16931da177e4SLinus Torvalds goto dput_and_out; 169425d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */ 16955ff9d8a6SEric W. Biederman goto dput_and_out; 1696b2f5d4dcSEric W. Biederman retval = -EPERM; 1697b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 1698b2f5d4dcSEric W. Biederman goto dput_and_out; 16991da177e4SLinus Torvalds 1700900148dcSAl Viro retval = do_umount(mnt, flags); 17011da177e4SLinus Torvalds dput_and_out: 1702429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 17032d8f3038SAl Viro dput(path.dentry); 1704900148dcSAl Viro mntput_no_expire(mnt); 17051da177e4SLinus Torvalds out: 17061da177e4SLinus Torvalds return retval; 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds 17093a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 17103a18ef5cSDominik Brodowski { 17113a18ef5cSDominik Brodowski return ksys_umount(name, flags); 17123a18ef5cSDominik Brodowski } 17133a18ef5cSDominik Brodowski 17141da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds /* 17171da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 17181da177e4SLinus Torvalds */ 1719bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 17201da177e4SLinus Torvalds { 17213a18ef5cSDominik Brodowski return ksys_umount(name, 0); 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds #endif 17251da177e4SLinus Torvalds 17264ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 17278823c079SEric W. Biederman { 17284ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1729e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1730e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 17314ce5d2b1SEric W. Biederman } 17324ce5d2b1SEric W. Biederman 173358be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 173458be2825SAl Viro { 173558be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 173658be2825SAl Viro } 173758be2825SAl Viro 17384ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 17394ce5d2b1SEric W. Biederman { 17404ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 17414ce5d2b1SEric W. Biederman * mount namespace loop? 17424ce5d2b1SEric W. Biederman */ 17434ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 17444ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 17454ce5d2b1SEric W. Biederman return false; 17464ce5d2b1SEric W. Biederman 1747f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 17488823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 17498823c079SEric W. Biederman } 17508823c079SEric W. Biederman 175187129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 175236341f64SRam Pai int flag) 17531da177e4SLinus Torvalds { 175484d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 17551da177e4SLinus Torvalds 17564ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 17574ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 17584ce5d2b1SEric W. Biederman 17594ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1760be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 17619676f0c6SRam Pai 176236341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1763be34d1a3SDavid Howells if (IS_ERR(q)) 1764be34d1a3SDavid Howells return q; 1765be34d1a3SDavid Howells 1766a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds p = mnt; 17696b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1770315fc83eSAl Viro struct mount *s; 17717ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 17721da177e4SLinus Torvalds continue; 17731da177e4SLinus Torvalds 1774909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 17754ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 17764ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 1777df7342b2SEric W. Biederman if (s->mnt.mnt_flags & MNT_LOCKED) { 1778df7342b2SEric W. Biederman /* Both unbindable and locked. */ 1779df7342b2SEric W. Biederman q = ERR_PTR(-EPERM); 1780df7342b2SEric W. Biederman goto out; 1781df7342b2SEric W. Biederman } else { 17824ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 17834ce5d2b1SEric W. Biederman continue; 17844ce5d2b1SEric W. Biederman } 1785df7342b2SEric W. Biederman } 17864ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 17874ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 17889676f0c6SRam Pai s = skip_mnt_tree(s); 17899676f0c6SRam Pai continue; 17909676f0c6SRam Pai } 17910714a533SAl Viro while (p != s->mnt_parent) { 17920714a533SAl Viro p = p->mnt_parent; 17930714a533SAl Viro q = q->mnt_parent; 17941da177e4SLinus Torvalds } 179587129cc0SAl Viro p = s; 179684d17192SAl Viro parent = q; 179787129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1798be34d1a3SDavid Howells if (IS_ERR(q)) 1799be34d1a3SDavid Howells goto out; 1800719ea2fbSAl Viro lock_mount_hash(); 18011a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 18021064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1803719ea2fbSAl Viro unlock_mount_hash(); 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds return res; 1807be34d1a3SDavid Howells out: 18081da177e4SLinus Torvalds if (res) { 1809719ea2fbSAl Viro lock_mount_hash(); 1810e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1811719ea2fbSAl Viro unlock_mount_hash(); 18121da177e4SLinus Torvalds } 1813be34d1a3SDavid Howells return q; 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 1816be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1817be34d1a3SDavid Howells 1818ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 18198aec0809SAl Viro { 1820cb338d06SAl Viro struct mount *tree; 182197216be0SAl Viro namespace_lock(); 1822cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1823cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1824cd4a4017SEric W. Biederman else 182587129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 182687129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1827328e6d90SAl Viro namespace_unlock(); 1828be34d1a3SDavid Howells if (IS_ERR(tree)) 182952e220d3SDan Carpenter return ERR_CAST(tree); 1830be34d1a3SDavid Howells return &tree->mnt; 18318aec0809SAl Viro } 18328aec0809SAl Viro 18338aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 18348aec0809SAl Viro { 183597216be0SAl Viro namespace_lock(); 1836719ea2fbSAl Viro lock_mount_hash(); 18379c8e0a1bSEric W. Biederman umount_tree(real_mount(mnt), 0); 1838719ea2fbSAl Viro unlock_mount_hash(); 18393ab6abeeSAl Viro namespace_unlock(); 18408aec0809SAl Viro } 18418aec0809SAl Viro 1842c771d683SMiklos Szeredi /** 1843c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 1844c771d683SMiklos Szeredi * 1845c771d683SMiklos Szeredi * This creates a new vfsmount, which will be the clone of @path. The new will 1846c771d683SMiklos Szeredi * not be attached anywhere in the namespace and will be private (i.e. changes 1847c771d683SMiklos Szeredi * to the originating mount won't be propagated into this). 1848c771d683SMiklos Szeredi * 1849c771d683SMiklos Szeredi * Release with mntput(). 1850c771d683SMiklos Szeredi */ 1851ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1852c771d683SMiklos Szeredi { 1853c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1854c771d683SMiklos Szeredi struct mount *new_mnt; 1855c771d683SMiklos Szeredi 1856c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1857c771d683SMiklos Szeredi return ERR_PTR(-EINVAL); 1858c771d683SMiklos Szeredi 1859c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1860c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1861c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1862c771d683SMiklos Szeredi 1863c771d683SMiklos Szeredi return &new_mnt->mnt; 1864c771d683SMiklos Szeredi } 1865c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 1866c771d683SMiklos Szeredi 18671f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 18681f707137SAl Viro struct vfsmount *root) 18691f707137SAl Viro { 18701a4eeaf2SAl Viro struct mount *mnt; 18711f707137SAl Viro int res = f(root, arg); 18721f707137SAl Viro if (res) 18731f707137SAl Viro return res; 18741a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 18751a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 18761f707137SAl Viro if (res) 18771f707137SAl Viro return res; 18781f707137SAl Viro } 18791f707137SAl Viro return 0; 18801f707137SAl Viro } 18811f707137SAl Viro 18823bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt) 18833bd045ccSAl Viro { 18843bd045ccSAl Viro struct mount *p; 18853bd045ccSAl Viro 18863bd045ccSAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 18873bd045ccSAl Viro int flags = p->mnt.mnt_flags; 18883bd045ccSAl Viro /* Don't allow unprivileged users to change mount flags */ 18893bd045ccSAl Viro flags |= MNT_LOCK_ATIME; 18903bd045ccSAl Viro 18913bd045ccSAl Viro if (flags & MNT_READONLY) 18923bd045ccSAl Viro flags |= MNT_LOCK_READONLY; 18933bd045ccSAl Viro 18943bd045ccSAl Viro if (flags & MNT_NODEV) 18953bd045ccSAl Viro flags |= MNT_LOCK_NODEV; 18963bd045ccSAl Viro 18973bd045ccSAl Viro if (flags & MNT_NOSUID) 18983bd045ccSAl Viro flags |= MNT_LOCK_NOSUID; 18993bd045ccSAl Viro 19003bd045ccSAl Viro if (flags & MNT_NOEXEC) 19013bd045ccSAl Viro flags |= MNT_LOCK_NOEXEC; 19023bd045ccSAl Viro /* Don't allow unprivileged users to reveal what is under a mount */ 19033bd045ccSAl Viro if (list_empty(&p->mnt_expire)) 19043bd045ccSAl Viro flags |= MNT_LOCKED; 19053bd045ccSAl Viro p->mnt.mnt_flags = flags; 19063bd045ccSAl Viro } 19073bd045ccSAl Viro } 19083bd045ccSAl Viro 19094b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1910719f5d7fSMiklos Szeredi { 1911315fc83eSAl Viro struct mount *p; 1912719f5d7fSMiklos Szeredi 1913909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1914fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 19154b8b21f4SAl Viro mnt_release_group_id(p); 1916719f5d7fSMiklos Szeredi } 1917719f5d7fSMiklos Szeredi } 1918719f5d7fSMiklos Szeredi 19194b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1920719f5d7fSMiklos Szeredi { 1921315fc83eSAl Viro struct mount *p; 1922719f5d7fSMiklos Szeredi 1923909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1924fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 19254b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1926719f5d7fSMiklos Szeredi if (err) { 19274b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1928719f5d7fSMiklos Szeredi return err; 1929719f5d7fSMiklos Szeredi } 1930719f5d7fSMiklos Szeredi } 1931719f5d7fSMiklos Szeredi } 1932719f5d7fSMiklos Szeredi 1933719f5d7fSMiklos Szeredi return 0; 1934719f5d7fSMiklos Szeredi } 1935719f5d7fSMiklos Szeredi 1936d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 1937d2921684SEric W. Biederman { 1938d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 1939d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 1940d2921684SEric W. Biederman struct mount *p; 1941d2921684SEric W. Biederman 1942d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 1943d2921684SEric W. Biederman mounts++; 1944d2921684SEric W. Biederman 1945d2921684SEric W. Biederman old = ns->mounts; 1946d2921684SEric W. Biederman pending = ns->pending_mounts; 1947d2921684SEric W. Biederman sum = old + pending; 1948d2921684SEric W. Biederman if ((old > sum) || 1949d2921684SEric W. Biederman (pending > sum) || 1950d2921684SEric W. Biederman (max < sum) || 1951d2921684SEric W. Biederman (mounts > (max - sum))) 1952d2921684SEric W. Biederman return -ENOSPC; 1953d2921684SEric W. Biederman 1954d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 1955d2921684SEric W. Biederman return 0; 1956d2921684SEric W. Biederman } 1957d2921684SEric W. Biederman 1958b90fa9aeSRam Pai /* 1959b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1960b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 196121444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 196221444403SRam Pai * store the parent mount and mountpoint dentry. 196321444403SRam Pai * (done when source_mnt is moved) 1964b90fa9aeSRam Pai * 1965b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1966b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 19679676f0c6SRam Pai * --------------------------------------------------------------------------- 1968b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 19699676f0c6SRam Pai * |************************************************************************** 19709676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 19719676f0c6SRam Pai * | dest | | | | | 19729676f0c6SRam Pai * | | | | | | | 19739676f0c6SRam Pai * | v | | | | | 19749676f0c6SRam Pai * |************************************************************************** 19759676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 19765afe0022SRam Pai * | | | | | | 19779676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 19789676f0c6SRam Pai * *************************************************************************** 1979b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1980b90fa9aeSRam Pai * destination mount. 1981b90fa9aeSRam Pai * 1982b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1983b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1984b90fa9aeSRam Pai * the peer group of the source mount. 1985b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1986b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1987b90fa9aeSRam Pai * mount. 19885afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 19895afe0022SRam Pai * of the destination mount and the cloned mount is made slave 19905afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 19915afe0022SRam Pai * is marked as 'shared and slave'. 19925afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 19935afe0022SRam Pai * source mount. 19945afe0022SRam Pai * 19959676f0c6SRam Pai * --------------------------------------------------------------------------- 199621444403SRam Pai * | MOVE MOUNT OPERATION | 19979676f0c6SRam Pai * |************************************************************************** 19989676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 19999676f0c6SRam Pai * | dest | | | | | 20009676f0c6SRam Pai * | | | | | | | 20019676f0c6SRam Pai * | v | | | | | 20029676f0c6SRam Pai * |************************************************************************** 20039676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 20045afe0022SRam Pai * | | | | | | 20059676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 20069676f0c6SRam Pai * *************************************************************************** 20075afe0022SRam Pai * 20085afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 20095afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 201021444403SRam Pai * (+*) the mount is moved to the destination. 20115afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 20125afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 20135afe0022SRam Pai * the mount is marked as 'shared and slave'. 20145afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 2015b90fa9aeSRam Pai * 2016b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 2017b90fa9aeSRam Pai * applied to each mount in the tree. 2018b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 2019b90fa9aeSRam Pai * in allocations. 2020b90fa9aeSRam Pai */ 20210fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 202284d17192SAl Viro struct mount *dest_mnt, 202384d17192SAl Viro struct mountpoint *dest_mp, 202484d17192SAl Viro struct path *parent_path) 2025b90fa9aeSRam Pai { 20263bd045ccSAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 202738129a13SAl Viro HLIST_HEAD(tree_list); 2028d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 20291064f874SEric W. Biederman struct mountpoint *smp; 2030315fc83eSAl Viro struct mount *child, *p; 203138129a13SAl Viro struct hlist_node *n; 2032719f5d7fSMiklos Szeredi int err; 2033b90fa9aeSRam Pai 20341064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 20351064f874SEric W. Biederman * to be tucked under other mounts. 20361064f874SEric W. Biederman */ 20371064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 20381064f874SEric W. Biederman if (IS_ERR(smp)) 20391064f874SEric W. Biederman return PTR_ERR(smp); 20401064f874SEric W. Biederman 2041d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 2042d2921684SEric W. Biederman if (!parent_path) { 2043d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 2044d2921684SEric W. Biederman if (err) 2045d2921684SEric W. Biederman goto out; 2046d2921684SEric W. Biederman } 2047d2921684SEric W. Biederman 2048fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 20490fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 2050719f5d7fSMiklos Szeredi if (err) 2051719f5d7fSMiklos Szeredi goto out; 205284d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 2053f2ebb3a9SAl Viro lock_mount_hash(); 2054719f5d7fSMiklos Szeredi if (err) 2055719f5d7fSMiklos Szeredi goto out_cleanup_ids; 2056909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 20570f0afb1dSAl Viro set_mnt_shared(p); 20580b1b901bSAl Viro } else { 20590b1b901bSAl Viro lock_mount_hash(); 2060b90fa9aeSRam Pai } 20611a390689SAl Viro if (parent_path) { 20620fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 206384d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 2064143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 206521444403SRam Pai } else { 206684d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 20671064f874SEric W. Biederman commit_tree(source_mnt); 206821444403SRam Pai } 2069b90fa9aeSRam Pai 207038129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 20711d6a32acSAl Viro struct mount *q; 207238129a13SAl Viro hlist_del_init(&child->mnt_hash); 20731064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 20741d6a32acSAl Viro child->mnt_mountpoint); 20751064f874SEric W. Biederman if (q) 20761064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 20773bd045ccSAl Viro /* Notice when we are propagating across user namespaces */ 20783bd045ccSAl Viro if (child->mnt_parent->mnt_ns->user_ns != user_ns) 20793bd045ccSAl Viro lock_mnt_tree(child); 20801064f874SEric W. Biederman commit_tree(child); 2081b90fa9aeSRam Pai } 20821064f874SEric W. Biederman put_mountpoint(smp); 2083719ea2fbSAl Viro unlock_mount_hash(); 208499b7db7bSNick Piggin 2085b90fa9aeSRam Pai return 0; 2086719f5d7fSMiklos Szeredi 2087719f5d7fSMiklos Szeredi out_cleanup_ids: 2088f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2089f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2090d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2091e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2092f2ebb3a9SAl Viro } 2093f2ebb3a9SAl Viro unlock_mount_hash(); 20940fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2095719f5d7fSMiklos Szeredi out: 2096d2921684SEric W. Biederman ns->pending_mounts = 0; 20971064f874SEric W. Biederman 20981064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 20991064f874SEric W. Biederman put_mountpoint(smp); 21001064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 21011064f874SEric W. Biederman 2102719f5d7fSMiklos Szeredi return err; 2103b90fa9aeSRam Pai } 2104b90fa9aeSRam Pai 210584d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2106b12cea91SAl Viro { 2107b12cea91SAl Viro struct vfsmount *mnt; 210884d17192SAl Viro struct dentry *dentry = path->dentry; 2109b12cea91SAl Viro retry: 21105955102cSAl Viro inode_lock(dentry->d_inode); 211184d17192SAl Viro if (unlikely(cant_mount(dentry))) { 21125955102cSAl Viro inode_unlock(dentry->d_inode); 211384d17192SAl Viro return ERR_PTR(-ENOENT); 2114b12cea91SAl Viro } 211597216be0SAl Viro namespace_lock(); 2116b12cea91SAl Viro mnt = lookup_mnt(path); 211784d17192SAl Viro if (likely(!mnt)) { 21183895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 211984d17192SAl Viro if (IS_ERR(mp)) { 212097216be0SAl Viro namespace_unlock(); 21215955102cSAl Viro inode_unlock(dentry->d_inode); 212284d17192SAl Viro return mp; 212384d17192SAl Viro } 212484d17192SAl Viro return mp; 212584d17192SAl Viro } 212697216be0SAl Viro namespace_unlock(); 21275955102cSAl Viro inode_unlock(path->dentry->d_inode); 2128b12cea91SAl Viro path_put(path); 2129b12cea91SAl Viro path->mnt = mnt; 213084d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2131b12cea91SAl Viro goto retry; 2132b12cea91SAl Viro } 2133b12cea91SAl Viro 213484d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2135b12cea91SAl Viro { 213684d17192SAl Viro struct dentry *dentry = where->m_dentry; 21373895dbf8SEric W. Biederman 21383895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 213984d17192SAl Viro put_mountpoint(where); 21403895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 21413895dbf8SEric W. Biederman 2142328e6d90SAl Viro namespace_unlock(); 21435955102cSAl Viro inode_unlock(dentry->d_inode); 2144b12cea91SAl Viro } 2145b12cea91SAl Viro 214684d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 21471da177e4SLinus Torvalds { 2148e462ec50SDavid Howells if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER) 21491da177e4SLinus Torvalds return -EINVAL; 21501da177e4SLinus Torvalds 2151e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2152e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 21531da177e4SLinus Torvalds return -ENOTDIR; 21541da177e4SLinus Torvalds 215584d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds 21581da177e4SLinus Torvalds /* 21597a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 21607a2e8a8fSValerie Aurora */ 21617a2e8a8fSValerie Aurora 2162e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags) 21637a2e8a8fSValerie Aurora { 2164e462ec50SDavid Howells int type = ms_flags & ~(MS_REC | MS_SILENT); 21657a2e8a8fSValerie Aurora 21667a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 21677a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21687a2e8a8fSValerie Aurora return 0; 21697a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 21707a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 21717a2e8a8fSValerie Aurora return 0; 21727a2e8a8fSValerie Aurora return type; 21737a2e8a8fSValerie Aurora } 21747a2e8a8fSValerie Aurora 21757a2e8a8fSValerie Aurora /* 217607b20889SRam Pai * recursively change the type of the mountpoint. 217707b20889SRam Pai */ 2178e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags) 217907b20889SRam Pai { 2180315fc83eSAl Viro struct mount *m; 21814b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 2182e462ec50SDavid Howells int recurse = ms_flags & MS_REC; 21837a2e8a8fSValerie Aurora int type; 2184719f5d7fSMiklos Szeredi int err = 0; 218507b20889SRam Pai 21862d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 218707b20889SRam Pai return -EINVAL; 218807b20889SRam Pai 2189e462ec50SDavid Howells type = flags_to_propagation_type(ms_flags); 21907a2e8a8fSValerie Aurora if (!type) 21917a2e8a8fSValerie Aurora return -EINVAL; 21927a2e8a8fSValerie Aurora 219397216be0SAl Viro namespace_lock(); 2194719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2195719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2196719f5d7fSMiklos Szeredi if (err) 2197719f5d7fSMiklos Szeredi goto out_unlock; 2198719f5d7fSMiklos Szeredi } 2199719f5d7fSMiklos Szeredi 2200719ea2fbSAl Viro lock_mount_hash(); 2201909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 22020f0afb1dSAl Viro change_mnt_propagation(m, type); 2203719ea2fbSAl Viro unlock_mount_hash(); 2204719f5d7fSMiklos Szeredi 2205719f5d7fSMiklos Szeredi out_unlock: 220697216be0SAl Viro namespace_unlock(); 2207719f5d7fSMiklos Szeredi return err; 220807b20889SRam Pai } 220907b20889SRam Pai 22105ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 22115ff9d8a6SEric W. Biederman { 22125ff9d8a6SEric W. Biederman struct mount *child; 22135ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 22145ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 22155ff9d8a6SEric W. Biederman continue; 22165ff9d8a6SEric W. Biederman 22175ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 22185ff9d8a6SEric W. Biederman return true; 22195ff9d8a6SEric W. Biederman } 22205ff9d8a6SEric W. Biederman return false; 22215ff9d8a6SEric W. Biederman } 22225ff9d8a6SEric W. Biederman 222307b20889SRam Pai /* 22241da177e4SLinus Torvalds * do loopback mount. 22251da177e4SLinus Torvalds */ 2226808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 22272dafe1c4SEric Sandeen int recurse) 22281da177e4SLinus Torvalds { 22292d92ab3cSAl Viro struct path old_path; 223084d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 223184d17192SAl Viro struct mountpoint *mp; 223257eccb83SAl Viro int err; 22331da177e4SLinus Torvalds if (!old_name || !*old_name) 22341da177e4SLinus Torvalds return -EINVAL; 2235815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 22361da177e4SLinus Torvalds if (err) 22371da177e4SLinus Torvalds return err; 22381da177e4SLinus Torvalds 22398823c079SEric W. Biederman err = -EINVAL; 22404ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 22418823c079SEric W. Biederman goto out; 22428823c079SEric W. Biederman 224384d17192SAl Viro mp = lock_mount(path); 224484d17192SAl Viro err = PTR_ERR(mp); 224584d17192SAl Viro if (IS_ERR(mp)) 22469676f0c6SRam Pai goto out; 22479676f0c6SRam Pai 224887129cc0SAl Viro old = real_mount(old_path.mnt); 224984d17192SAl Viro parent = real_mount(path->mnt); 225087129cc0SAl Viro 2251b12cea91SAl Viro err = -EINVAL; 2252fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 2253b12cea91SAl Viro goto out2; 2254b12cea91SAl Viro 2255e149ed2bSAl Viro if (!check_mnt(parent)) 2256e149ed2bSAl Viro goto out2; 2257e149ed2bSAl Viro 2258e149ed2bSAl Viro if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations) 2259b12cea91SAl Viro goto out2; 2260ccd48bc7SAl Viro 22615ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 22625ff9d8a6SEric W. Biederman goto out2; 22635ff9d8a6SEric W. Biederman 22641da177e4SLinus Torvalds if (recurse) 22654ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 22661da177e4SLinus Torvalds else 226787129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 22681da177e4SLinus Torvalds 2269be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2270be34d1a3SDavid Howells err = PTR_ERR(mnt); 2271e9c5d8a5SAndrey Vagin goto out2; 2272be34d1a3SDavid Howells } 2273ccd48bc7SAl Viro 22745ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 22755ff9d8a6SEric W. Biederman 227684d17192SAl Viro err = graft_tree(mnt, parent, mp); 22771da177e4SLinus Torvalds if (err) { 2278719ea2fbSAl Viro lock_mount_hash(); 2279e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2280719ea2fbSAl Viro unlock_mount_hash(); 22815b83d2c5SRam Pai } 2282b12cea91SAl Viro out2: 228384d17192SAl Viro unlock_mount(mp); 2284ccd48bc7SAl Viro out: 22852d92ab3cSAl Viro path_put(&old_path); 22861da177e4SLinus Torvalds return err; 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 228943f5e655SDavid Howells /* 229043f5e655SDavid Howells * Don't allow locked mount flags to be cleared. 229143f5e655SDavid Howells * 229243f5e655SDavid Howells * No locks need to be held here while testing the various MNT_LOCK 229343f5e655SDavid Howells * flags because those flags can never be cleared once they are set. 229443f5e655SDavid Howells */ 229543f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags) 22962e4b7fcdSDave Hansen { 229743f5e655SDavid Howells unsigned int fl = mnt->mnt.mnt_flags; 22982e4b7fcdSDave Hansen 229943f5e655SDavid Howells if ((fl & MNT_LOCK_READONLY) && 230043f5e655SDavid Howells !(mnt_flags & MNT_READONLY)) 230143f5e655SDavid Howells return false; 230243f5e655SDavid Howells 230343f5e655SDavid Howells if ((fl & MNT_LOCK_NODEV) && 230443f5e655SDavid Howells !(mnt_flags & MNT_NODEV)) 230543f5e655SDavid Howells return false; 230643f5e655SDavid Howells 230743f5e655SDavid Howells if ((fl & MNT_LOCK_NOSUID) && 230843f5e655SDavid Howells !(mnt_flags & MNT_NOSUID)) 230943f5e655SDavid Howells return false; 231043f5e655SDavid Howells 231143f5e655SDavid Howells if ((fl & MNT_LOCK_NOEXEC) && 231243f5e655SDavid Howells !(mnt_flags & MNT_NOEXEC)) 231343f5e655SDavid Howells return false; 231443f5e655SDavid Howells 231543f5e655SDavid Howells if ((fl & MNT_LOCK_ATIME) && 231643f5e655SDavid Howells ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) 231743f5e655SDavid Howells return false; 231843f5e655SDavid Howells 231943f5e655SDavid Howells return true; 232043f5e655SDavid Howells } 232143f5e655SDavid Howells 232243f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags) 232343f5e655SDavid Howells { 232443f5e655SDavid Howells bool readonly_request = (mnt_flags & MNT_READONLY); 232543f5e655SDavid Howells 232643f5e655SDavid Howells if (readonly_request == __mnt_is_readonly(&mnt->mnt)) 23272e4b7fcdSDave Hansen return 0; 23282e4b7fcdSDave Hansen 23292e4b7fcdSDave Hansen if (readonly_request) 233043f5e655SDavid Howells return mnt_make_readonly(mnt); 233143f5e655SDavid Howells 233243f5e655SDavid Howells return __mnt_unmake_readonly(mnt); 233343f5e655SDavid Howells } 233443f5e655SDavid Howells 233543f5e655SDavid Howells /* 233643f5e655SDavid Howells * Update the user-settable attributes on a mount. The caller must hold 233743f5e655SDavid Howells * sb->s_umount for writing. 233843f5e655SDavid Howells */ 233943f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags) 234043f5e655SDavid Howells { 234143f5e655SDavid Howells lock_mount_hash(); 234243f5e655SDavid Howells mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 234343f5e655SDavid Howells mnt->mnt.mnt_flags = mnt_flags; 234443f5e655SDavid Howells touch_mnt_namespace(mnt->mnt_ns); 234543f5e655SDavid Howells unlock_mount_hash(); 234643f5e655SDavid Howells } 234743f5e655SDavid Howells 234843f5e655SDavid Howells /* 234943f5e655SDavid Howells * Handle reconfiguration of the mountpoint only without alteration of the 235043f5e655SDavid Howells * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND 235143f5e655SDavid Howells * to mount(2). 235243f5e655SDavid Howells */ 235343f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags) 235443f5e655SDavid Howells { 235543f5e655SDavid Howells struct super_block *sb = path->mnt->mnt_sb; 235643f5e655SDavid Howells struct mount *mnt = real_mount(path->mnt); 235743f5e655SDavid Howells int ret; 235843f5e655SDavid Howells 235943f5e655SDavid Howells if (!check_mnt(mnt)) 236043f5e655SDavid Howells return -EINVAL; 236143f5e655SDavid Howells 236243f5e655SDavid Howells if (path->dentry != mnt->mnt.mnt_root) 236343f5e655SDavid Howells return -EINVAL; 236443f5e655SDavid Howells 236543f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 236643f5e655SDavid Howells return -EPERM; 236743f5e655SDavid Howells 236843f5e655SDavid Howells down_write(&sb->s_umount); 236943f5e655SDavid Howells ret = change_mount_ro_state(mnt, mnt_flags); 237043f5e655SDavid Howells if (ret == 0) 237143f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 237243f5e655SDavid Howells up_write(&sb->s_umount); 237343f5e655SDavid Howells return ret; 23742e4b7fcdSDave Hansen } 23752e4b7fcdSDave Hansen 23761da177e4SLinus Torvalds /* 23771da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 23781da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 23791da177e4SLinus Torvalds * on it - tough luck. 23801da177e4SLinus Torvalds */ 2381e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags, 2382e462ec50SDavid Howells int mnt_flags, void *data) 23831da177e4SLinus Torvalds { 23841da177e4SLinus Torvalds int err; 23852d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2386143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 23878d0347f6SDavid Howells struct fs_context *fc; 23881da177e4SLinus Torvalds 2389143c8c91SAl Viro if (!check_mnt(mnt)) 23901da177e4SLinus Torvalds return -EINVAL; 23911da177e4SLinus Torvalds 23922d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 23931da177e4SLinus Torvalds return -EINVAL; 23941da177e4SLinus Torvalds 239543f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 239607b64558SEric W. Biederman return -EPERM; 23979566d674SEric W. Biederman 23988d0347f6SDavid Howells fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK); 23998d0347f6SDavid Howells if (IS_ERR(fc)) 24008d0347f6SDavid Howells return PTR_ERR(fc); 2401ff36fe2cSEric Paris 24028d0347f6SDavid Howells err = parse_monolithic_mount_data(fc, data); 24038d0347f6SDavid Howells if (!err) { 24041da177e4SLinus Torvalds down_write(&sb->s_umount); 240557eccb83SAl Viro err = -EPERM; 240643f5e655SDavid Howells if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) { 24078d0347f6SDavid Howells err = reconfigure_super(fc); 240843f5e655SDavid Howells if (!err) 240943f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 24100e55a7ccSDan Williams } 24116339dab8SAl Viro up_write(&sb->s_umount); 24128d0347f6SDavid Howells } 24138d0347f6SDavid Howells put_fs_context(fc); 24141da177e4SLinus Torvalds return err; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 2417cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 24189676f0c6SRam Pai { 2419315fc83eSAl Viro struct mount *p; 2420909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2421fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 24229676f0c6SRam Pai return 1; 24239676f0c6SRam Pai } 24249676f0c6SRam Pai return 0; 24259676f0c6SRam Pai } 24269676f0c6SRam Pai 2427808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 24281da177e4SLinus Torvalds { 24292d92ab3cSAl Viro struct path old_path, parent_path; 2430676da58dSAl Viro struct mount *p; 24310fb54e50SAl Viro struct mount *old; 243284d17192SAl Viro struct mountpoint *mp; 243357eccb83SAl Viro int err; 24341da177e4SLinus Torvalds if (!old_name || !*old_name) 24351da177e4SLinus Torvalds return -EINVAL; 24362d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 24371da177e4SLinus Torvalds if (err) 24381da177e4SLinus Torvalds return err; 24391da177e4SLinus Torvalds 244084d17192SAl Viro mp = lock_mount(path); 244184d17192SAl Viro err = PTR_ERR(mp); 244284d17192SAl Viro if (IS_ERR(mp)) 2443cc53ce53SDavid Howells goto out; 2444cc53ce53SDavid Howells 2445143c8c91SAl Viro old = real_mount(old_path.mnt); 2446fc7be130SAl Viro p = real_mount(path->mnt); 2447143c8c91SAl Viro 24481da177e4SLinus Torvalds err = -EINVAL; 2449fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 24501da177e4SLinus Torvalds goto out1; 24511da177e4SLinus Torvalds 24525ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 24535ff9d8a6SEric W. Biederman goto out1; 24545ff9d8a6SEric W. Biederman 24551da177e4SLinus Torvalds err = -EINVAL; 24562d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 245721444403SRam Pai goto out1; 24581da177e4SLinus Torvalds 2459676da58dSAl Viro if (!mnt_has_parent(old)) 246021444403SRam Pai goto out1; 24611da177e4SLinus Torvalds 2462e36cb0b8SDavid Howells if (d_is_dir(path->dentry) != 2463e36cb0b8SDavid Howells d_is_dir(old_path.dentry)) 246421444403SRam Pai goto out1; 246521444403SRam Pai /* 246621444403SRam Pai * Don't move a mount residing in a shared parent. 246721444403SRam Pai */ 2468fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 246921444403SRam Pai goto out1; 24709676f0c6SRam Pai /* 24719676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 24729676f0c6SRam Pai * mount which is shared. 24739676f0c6SRam Pai */ 2474fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 24759676f0c6SRam Pai goto out1; 24761da177e4SLinus Torvalds err = -ELOOP; 2477fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2478676da58dSAl Viro if (p == old) 247921444403SRam Pai goto out1; 24801da177e4SLinus Torvalds 248184d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 24824ac91378SJan Blunck if (err) 248321444403SRam Pai goto out1; 24841da177e4SLinus Torvalds 24851da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 24861da177e4SLinus Torvalds * automatically */ 24876776db3dSAl Viro list_del_init(&old->mnt_expire); 24881da177e4SLinus Torvalds out1: 248984d17192SAl Viro unlock_mount(mp); 24901da177e4SLinus Torvalds out: 24911da177e4SLinus Torvalds if (!err) 24921a390689SAl Viro path_put(&parent_path); 24932d92ab3cSAl Viro path_put(&old_path); 24941da177e4SLinus Torvalds return err; 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds 24979d412a43SAl Viro /* 24989d412a43SAl Viro * add a mount into a namespace's mount tree 24999d412a43SAl Viro */ 250095bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 25019d412a43SAl Viro { 250284d17192SAl Viro struct mountpoint *mp; 250384d17192SAl Viro struct mount *parent; 25049d412a43SAl Viro int err; 25059d412a43SAl Viro 2506f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 25079d412a43SAl Viro 250884d17192SAl Viro mp = lock_mount(path); 250984d17192SAl Viro if (IS_ERR(mp)) 251084d17192SAl Viro return PTR_ERR(mp); 25119d412a43SAl Viro 251284d17192SAl Viro parent = real_mount(path->mnt); 25139d412a43SAl Viro err = -EINVAL; 251484d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2515156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2516156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 25179d412a43SAl Viro goto unlock; 2518156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 251984d17192SAl Viro if (!parent->mnt_ns) 2520156cacb1SAl Viro goto unlock; 2521156cacb1SAl Viro } 25229d412a43SAl Viro 25239d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 25249d412a43SAl Viro err = -EBUSY; 252595bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 25269d412a43SAl Viro path->mnt->mnt_root == path->dentry) 25279d412a43SAl Viro goto unlock; 25289d412a43SAl Viro 25299d412a43SAl Viro err = -EINVAL; 2530e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 25319d412a43SAl Viro goto unlock; 25329d412a43SAl Viro 253395bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 253484d17192SAl Viro err = graft_tree(newmnt, parent, mp); 25359d412a43SAl Viro 25369d412a43SAl Viro unlock: 253784d17192SAl Viro unlock_mount(mp); 25389d412a43SAl Viro return err; 25399d412a43SAl Viro } 2540b1e75df4SAl Viro 2541132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags); 2542132e4608SDavid Howells 2543132e4608SDavid Howells /* 2544132e4608SDavid Howells * Create a new mount using a superblock configuration and request it 2545132e4608SDavid Howells * be added to the namespace tree. 2546132e4608SDavid Howells */ 2547132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint, 2548132e4608SDavid Howells unsigned int mnt_flags) 2549132e4608SDavid Howells { 2550132e4608SDavid Howells struct vfsmount *mnt; 2551132e4608SDavid Howells struct super_block *sb = fc->root->d_sb; 2552132e4608SDavid Howells int error; 2553132e4608SDavid Howells 2554c9ce29edSAl Viro error = security_sb_kern_mount(sb); 2555c9ce29edSAl Viro if (!error && mount_too_revealing(sb, &mnt_flags)) 2556c9ce29edSAl Viro error = -EPERM; 2557c9ce29edSAl Viro 2558c9ce29edSAl Viro if (unlikely(error)) { 2559c9ce29edSAl Viro fc_drop_locked(fc); 2560c9ce29edSAl Viro return error; 2561132e4608SDavid Howells } 2562132e4608SDavid Howells 2563132e4608SDavid Howells up_write(&sb->s_umount); 2564132e4608SDavid Howells 2565132e4608SDavid Howells mnt = vfs_create_mount(fc); 2566132e4608SDavid Howells if (IS_ERR(mnt)) 2567132e4608SDavid Howells return PTR_ERR(mnt); 2568132e4608SDavid Howells 2569132e4608SDavid Howells error = do_add_mount(real_mount(mnt), mountpoint, mnt_flags); 2570132e4608SDavid Howells if (error < 0) 2571132e4608SDavid Howells mntput(mnt); 2572132e4608SDavid Howells return error; 2573132e4608SDavid Howells } 25741b852bceSEric W. Biederman 25751da177e4SLinus Torvalds /* 25761da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 25771da177e4SLinus Torvalds * namespace's tree 25781da177e4SLinus Torvalds */ 2579e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags, 2580808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 25811da177e4SLinus Torvalds { 25820c55cfc4SEric W. Biederman struct file_system_type *type; 2583a0c9a8b8SAl Viro struct fs_context *fc; 2584a0c9a8b8SAl Viro const char *subtype = NULL; 2585a0c9a8b8SAl Viro int err = 0; 25861da177e4SLinus Torvalds 25870c55cfc4SEric W. Biederman if (!fstype) 25881da177e4SLinus Torvalds return -EINVAL; 25891da177e4SLinus Torvalds 25900c55cfc4SEric W. Biederman type = get_fs_type(fstype); 25910c55cfc4SEric W. Biederman if (!type) 25920c55cfc4SEric W. Biederman return -ENODEV; 25930c55cfc4SEric W. Biederman 2594a0c9a8b8SAl Viro if (type->fs_flags & FS_HAS_SUBTYPE) { 2595a0c9a8b8SAl Viro subtype = strchr(fstype, '.'); 2596a0c9a8b8SAl Viro if (subtype) { 2597a0c9a8b8SAl Viro subtype++; 2598a0c9a8b8SAl Viro if (!*subtype) { 25990c55cfc4SEric W. Biederman put_filesystem(type); 2600a0c9a8b8SAl Viro return -EINVAL; 2601a0c9a8b8SAl Viro } 2602a0c9a8b8SAl Viro } else { 2603a0c9a8b8SAl Viro subtype = ""; 2604a0c9a8b8SAl Viro } 2605a0c9a8b8SAl Viro } 2606a0c9a8b8SAl Viro 2607a0c9a8b8SAl Viro fc = fs_context_for_mount(type, sb_flags); 2608a0c9a8b8SAl Viro put_filesystem(type); 2609a0c9a8b8SAl Viro if (IS_ERR(fc)) 2610a0c9a8b8SAl Viro return PTR_ERR(fc); 2611a0c9a8b8SAl Viro 26123e1aeb00SDavid Howells if (subtype) 26133e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "subtype", 26143e1aeb00SDavid Howells subtype, strlen(subtype)); 26153e1aeb00SDavid Howells if (!err && name) 26163e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "source", name, strlen(name)); 2617a0c9a8b8SAl Viro if (!err) 2618a0c9a8b8SAl Viro err = parse_monolithic_mount_data(fc, data); 2619a0c9a8b8SAl Viro if (!err) 2620a0c9a8b8SAl Viro err = vfs_get_tree(fc); 2621132e4608SDavid Howells if (!err) 2622132e4608SDavid Howells err = do_new_mount_fc(fc, path, mnt_flags); 2623a0c9a8b8SAl Viro 2624a0c9a8b8SAl Viro put_fs_context(fc); 262515f9a3f3SAl Viro return err; 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds 262819a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 262919a167afSAl Viro { 26306776db3dSAl Viro struct mount *mnt = real_mount(m); 263119a167afSAl Viro int err; 263219a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 263319a167afSAl Viro * expired before we get a chance to add it 263419a167afSAl Viro */ 26356776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 263619a167afSAl Viro 263719a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 263819a167afSAl Viro m->mnt_root == path->dentry) { 2639b1e75df4SAl Viro err = -ELOOP; 2640b1e75df4SAl Viro goto fail; 264119a167afSAl Viro } 264219a167afSAl Viro 264395bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2644b1e75df4SAl Viro if (!err) 2645b1e75df4SAl Viro return 0; 2646b1e75df4SAl Viro fail: 2647b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 26486776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 264997216be0SAl Viro namespace_lock(); 26506776db3dSAl Viro list_del_init(&mnt->mnt_expire); 265197216be0SAl Viro namespace_unlock(); 265219a167afSAl Viro } 2653b1e75df4SAl Viro mntput(m); 2654b1e75df4SAl Viro mntput(m); 265519a167afSAl Viro return err; 265619a167afSAl Viro } 265719a167afSAl Viro 2658ea5b778aSDavid Howells /** 2659ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2660ea5b778aSDavid Howells * @mnt: The mount to list. 2661ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2662ea5b778aSDavid Howells */ 2663ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2664ea5b778aSDavid Howells { 266597216be0SAl Viro namespace_lock(); 2666ea5b778aSDavid Howells 26676776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2668ea5b778aSDavid Howells 266997216be0SAl Viro namespace_unlock(); 2670ea5b778aSDavid Howells } 2671ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2672ea5b778aSDavid Howells 2673ea5b778aSDavid Howells /* 26741da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 26751da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 26761da177e4SLinus Torvalds * here 26771da177e4SLinus Torvalds */ 26781da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 26791da177e4SLinus Torvalds { 2680761d5c38SAl Viro struct mount *mnt, *next; 26811da177e4SLinus Torvalds LIST_HEAD(graveyard); 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds if (list_empty(mounts)) 26841da177e4SLinus Torvalds return; 26851da177e4SLinus Torvalds 268697216be0SAl Viro namespace_lock(); 2687719ea2fbSAl Viro lock_mount_hash(); 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 26901da177e4SLinus Torvalds * following criteria: 26911da177e4SLinus Torvalds * - only referenced by its parent vfsmount 26921da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 26931da177e4SLinus Torvalds * cleared by mntput()) 26941da177e4SLinus Torvalds */ 26956776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2696863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 26971ab59738SAl Viro propagate_mount_busy(mnt, 1)) 26981da177e4SLinus Torvalds continue; 26996776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 27001da177e4SLinus Torvalds } 2701bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 27026776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2703143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2704e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2705bcc5c7d2SAl Viro } 2706719ea2fbSAl Viro unlock_mount_hash(); 27073ab6abeeSAl Viro namespace_unlock(); 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds /* 27135528f911STrond Myklebust * Ripoff of 'select_parent()' 27145528f911STrond Myklebust * 27155528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 27165528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 27175528f911STrond Myklebust */ 2718692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 27195528f911STrond Myklebust { 2720692afc31SAl Viro struct mount *this_parent = parent; 27215528f911STrond Myklebust struct list_head *next; 27225528f911STrond Myklebust int found = 0; 27235528f911STrond Myklebust 27245528f911STrond Myklebust repeat: 27256b41d536SAl Viro next = this_parent->mnt_mounts.next; 27265528f911STrond Myklebust resume: 27276b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 27285528f911STrond Myklebust struct list_head *tmp = next; 27296b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 27305528f911STrond Myklebust 27315528f911STrond Myklebust next = tmp->next; 2732692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 27335528f911STrond Myklebust continue; 27345528f911STrond Myklebust /* 27355528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 27365528f911STrond Myklebust */ 27376b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 27385528f911STrond Myklebust this_parent = mnt; 27395528f911STrond Myklebust goto repeat; 27405528f911STrond Myklebust } 27415528f911STrond Myklebust 27421ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 27436776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 27445528f911STrond Myklebust found++; 27455528f911STrond Myklebust } 27465528f911STrond Myklebust } 27475528f911STrond Myklebust /* 27485528f911STrond Myklebust * All done at this level ... ascend and resume the search 27495528f911STrond Myklebust */ 27505528f911STrond Myklebust if (this_parent != parent) { 27516b41d536SAl Viro next = this_parent->mnt_child.next; 27520714a533SAl Viro this_parent = this_parent->mnt_parent; 27535528f911STrond Myklebust goto resume; 27545528f911STrond Myklebust } 27555528f911STrond Myklebust return found; 27565528f911STrond Myklebust } 27575528f911STrond Myklebust 27585528f911STrond Myklebust /* 27595528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 27605528f911STrond Myklebust * submounts of a specific parent mountpoint 276199b7db7bSNick Piggin * 276248a066e7SAl Viro * mount_lock must be held for write 27635528f911STrond Myklebust */ 2764b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 27655528f911STrond Myklebust { 27665528f911STrond Myklebust LIST_HEAD(graveyard); 2767761d5c38SAl Viro struct mount *m; 27685528f911STrond Myklebust 27695528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2770c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2771bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2772761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 27736776db3dSAl Viro mnt_expire); 2774143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2775e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2776bcc5c7d2SAl Viro } 2777bcc5c7d2SAl Viro } 27785528f911STrond Myklebust } 27795528f911STrond Myklebust 27805528f911STrond Myklebust /* 27811da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 27821da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 27831da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 27841da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 27851da177e4SLinus Torvalds */ 2786b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2787b58fed8bSRam Pai unsigned long n) 27881da177e4SLinus Torvalds { 27891da177e4SLinus Torvalds char *t = to; 27901da177e4SLinus Torvalds const char __user *f = from; 27911da177e4SLinus Torvalds char c; 27921da177e4SLinus Torvalds 279396d4f267SLinus Torvalds if (!access_ok(from, n)) 27941da177e4SLinus Torvalds return n; 27951da177e4SLinus Torvalds 27969da3f2b7SJann Horn current->kernel_uaccess_faults_ok++; 27971da177e4SLinus Torvalds while (n) { 27981da177e4SLinus Torvalds if (__get_user(c, f)) { 27991da177e4SLinus Torvalds memset(t, 0, n); 28001da177e4SLinus Torvalds break; 28011da177e4SLinus Torvalds } 28021da177e4SLinus Torvalds *t++ = c; 28031da177e4SLinus Torvalds f++; 28041da177e4SLinus Torvalds n--; 28051da177e4SLinus Torvalds } 28069da3f2b7SJann Horn current->kernel_uaccess_faults_ok--; 28071da177e4SLinus Torvalds return n; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 2810b40ef869SAl Viro void *copy_mount_options(const void __user * data) 28111da177e4SLinus Torvalds { 28121da177e4SLinus Torvalds int i; 28131da177e4SLinus Torvalds unsigned long size; 2814b40ef869SAl Viro char *copy; 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds if (!data) 2817b40ef869SAl Viro return NULL; 28181da177e4SLinus Torvalds 2819b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 2820b40ef869SAl Viro if (!copy) 2821b40ef869SAl Viro return ERR_PTR(-ENOMEM); 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds /* We only care that *some* data at the address the user 28241da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 28251da177e4SLinus Torvalds * the remainder of the page. 28261da177e4SLinus Torvalds */ 28271da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 28281da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 28291da177e4SLinus Torvalds if (size > PAGE_SIZE) 28301da177e4SLinus Torvalds size = PAGE_SIZE; 28311da177e4SLinus Torvalds 2832b40ef869SAl Viro i = size - exact_copy_from_user(copy, data, size); 28331da177e4SLinus Torvalds if (!i) { 2834b40ef869SAl Viro kfree(copy); 2835b40ef869SAl Viro return ERR_PTR(-EFAULT); 28361da177e4SLinus Torvalds } 28371da177e4SLinus Torvalds if (i != PAGE_SIZE) 2838b40ef869SAl Viro memset(copy + i, 0, PAGE_SIZE - i); 2839b40ef869SAl Viro return copy; 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds 2842b8850d1fSTim Gardner char *copy_mount_string(const void __user *data) 2843eca6f534SVegard Nossum { 2844b8850d1fSTim Gardner return data ? strndup_user(data, PAGE_SIZE) : NULL; 2845eca6f534SVegard Nossum } 2846eca6f534SVegard Nossum 28471da177e4SLinus Torvalds /* 28481da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 28491da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 28501da177e4SLinus Torvalds * 28511da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 28521da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 28531da177e4SLinus Torvalds * information (or be NULL). 28541da177e4SLinus Torvalds * 28551da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 28561da177e4SLinus Torvalds * When the flags word was introduced its top half was required 28571da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 28581da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 28591da177e4SLinus Torvalds * and must be discarded. 28601da177e4SLinus Torvalds */ 28615e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name, 2862808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 28631da177e4SLinus Torvalds { 28642d92ab3cSAl Viro struct path path; 2865e462ec50SDavid Howells unsigned int mnt_flags = 0, sb_flags; 28661da177e4SLinus Torvalds int retval = 0; 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds /* Discard magic */ 28691da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 28701da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 28711da177e4SLinus Torvalds 28721da177e4SLinus Torvalds /* Basic sanity checks */ 28731da177e4SLinus Torvalds if (data_page) 28741da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 28751da177e4SLinus Torvalds 2876e462ec50SDavid Howells if (flags & MS_NOUSER) 2877e462ec50SDavid Howells return -EINVAL; 2878e462ec50SDavid Howells 2879a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 28805e6123f3SSeunghun Lee retval = user_path(dir_name, &path); 2881a27ab9f2STetsuo Handa if (retval) 2882a27ab9f2STetsuo Handa return retval; 2883a27ab9f2STetsuo Handa 2884a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2885a27ab9f2STetsuo Handa type_page, flags, data_page); 28860d5cadb8SAl Viro if (!retval && !may_mount()) 28870d5cadb8SAl Viro retval = -EPERM; 2888e462ec50SDavid Howells if (!retval && (flags & SB_MANDLOCK) && !may_mandlock()) 28899e8925b6SJeff Layton retval = -EPERM; 2890a27ab9f2STetsuo Handa if (retval) 2891a27ab9f2STetsuo Handa goto dput_out; 2892a27ab9f2STetsuo Handa 2893613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2894613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 28950a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 28960a1c01c9SMatthew Garrett 28971da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 28981da177e4SLinus Torvalds if (flags & MS_NOSUID) 28991da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 29001da177e4SLinus Torvalds if (flags & MS_NODEV) 29011da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 29021da177e4SLinus Torvalds if (flags & MS_NOEXEC) 29031da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2904fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2905fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2906fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2907fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2908d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2909d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 2910a9e5b732SDavid Howells if (flags & MS_RDONLY) 29112e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2912fc33a7bbSChristoph Hellwig 2913ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 2914ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 2915ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 2916ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 2917ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 2918ffbc6f0eSEric W. Biederman mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; 2919ffbc6f0eSEric W. Biederman } 2920ffbc6f0eSEric W. Biederman 2921e462ec50SDavid Howells sb_flags = flags & (SB_RDONLY | 2922e462ec50SDavid Howells SB_SYNCHRONOUS | 2923e462ec50SDavid Howells SB_MANDLOCK | 2924e462ec50SDavid Howells SB_DIRSYNC | 2925e462ec50SDavid Howells SB_SILENT | 2926917086ffSMimi Zohar SB_POSIXACL | 2927d7ee9469SMarkus Trippelsdorf SB_LAZYTIME | 2928917086ffSMimi Zohar SB_I_VERSION); 29291da177e4SLinus Torvalds 293043f5e655SDavid Howells if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND)) 293143f5e655SDavid Howells retval = do_reconfigure_mnt(&path, mnt_flags); 293243f5e655SDavid Howells else if (flags & MS_REMOUNT) 2933e462ec50SDavid Howells retval = do_remount(&path, flags, sb_flags, mnt_flags, 29341da177e4SLinus Torvalds data_page); 29351da177e4SLinus Torvalds else if (flags & MS_BIND) 29362d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 29379676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 29382d92ab3cSAl Viro retval = do_change_type(&path, flags); 29391da177e4SLinus Torvalds else if (flags & MS_MOVE) 29402d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 29411da177e4SLinus Torvalds else 2942e462ec50SDavid Howells retval = do_new_mount(&path, type_page, sb_flags, mnt_flags, 29431da177e4SLinus Torvalds dev_name, data_page); 29441da177e4SLinus Torvalds dput_out: 29452d92ab3cSAl Viro path_put(&path); 29461da177e4SLinus Torvalds return retval; 29471da177e4SLinus Torvalds } 29481da177e4SLinus Torvalds 2949537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 2950537f7ccbSEric W. Biederman { 2951537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 2952537f7ccbSEric W. Biederman } 2953537f7ccbSEric W. Biederman 2954537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 2955537f7ccbSEric W. Biederman { 2956537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 2957537f7ccbSEric W. Biederman } 2958537f7ccbSEric W. Biederman 2959771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2960771b1371SEric W. Biederman { 296174e83122SAl Viro if (!is_anon_ns(ns)) 29626344c433SAl Viro ns_free_inum(&ns->ns); 2963537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 2964771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2965771b1371SEric W. Biederman kfree(ns); 2966771b1371SEric W. Biederman } 2967771b1371SEric W. Biederman 29688823c079SEric W. Biederman /* 29698823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 29708823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 29718823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 29728823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 29738823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 29748823c079SEric W. Biederman */ 29758823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 29768823c079SEric W. Biederman 297774e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon) 2978cf8d2c11STrond Myklebust { 2979cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2980537f7ccbSEric W. Biederman struct ucounts *ucounts; 298198f842e6SEric W. Biederman int ret; 2982cf8d2c11STrond Myklebust 2983537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 2984537f7ccbSEric W. Biederman if (!ucounts) 2985df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 2986537f7ccbSEric W. Biederman 298774e83122SAl Viro new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2988537f7ccbSEric W. Biederman if (!new_ns) { 2989537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 2990cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2991537f7ccbSEric W. Biederman } 299274e83122SAl Viro if (!anon) { 29936344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 299498f842e6SEric W. Biederman if (ret) { 299598f842e6SEric W. Biederman kfree(new_ns); 2996537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 299798f842e6SEric W. Biederman return ERR_PTR(ret); 299898f842e6SEric W. Biederman } 299974e83122SAl Viro } 300033c42940SAl Viro new_ns->ns.ops = &mntns_operations; 300174e83122SAl Viro if (!anon) 30028823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 3003cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 3004cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 3005cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 3006771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 3007537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 3008cf8d2c11STrond Myklebust return new_ns; 3009cf8d2c11STrond Myklebust } 3010cf8d2c11STrond Myklebust 30110766f788SEmese Revfy __latent_entropy 30129559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 30139559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 30141da177e4SLinus Torvalds { 30156b3286edSKirill Korotaev struct mnt_namespace *new_ns; 30167f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 3017315fc83eSAl Viro struct mount *p, *q; 30189559f689SAl Viro struct mount *old; 3019cb338d06SAl Viro struct mount *new; 30207a472ef4SEric W. Biederman int copy_flags; 30211da177e4SLinus Torvalds 30229559f689SAl Viro BUG_ON(!ns); 30239559f689SAl Viro 30249559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 30259559f689SAl Viro get_mnt_ns(ns); 30269559f689SAl Viro return ns; 30279559f689SAl Viro } 30289559f689SAl Viro 30299559f689SAl Viro old = ns->root; 30309559f689SAl Viro 303174e83122SAl Viro new_ns = alloc_mnt_ns(user_ns, false); 3032cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 3033cf8d2c11STrond Myklebust return new_ns; 30341da177e4SLinus Torvalds 303597216be0SAl Viro namespace_lock(); 30361da177e4SLinus Torvalds /* First pass: copy the tree topology */ 30374ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 30389559f689SAl Viro if (user_ns != ns->user_ns) 30393bd045ccSAl Viro copy_flags |= CL_SHARED_TO_SLAVE; 30407a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 3041be34d1a3SDavid Howells if (IS_ERR(new)) { 3042328e6d90SAl Viro namespace_unlock(); 3043771b1371SEric W. Biederman free_mnt_ns(new_ns); 3044be34d1a3SDavid Howells return ERR_CAST(new); 30451da177e4SLinus Torvalds } 30463bd045ccSAl Viro if (user_ns != ns->user_ns) { 30473bd045ccSAl Viro lock_mount_hash(); 30483bd045ccSAl Viro lock_mnt_tree(new); 30493bd045ccSAl Viro unlock_mount_hash(); 30503bd045ccSAl Viro } 3051be08d6d2SAl Viro new_ns->root = new; 30521a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 30531da177e4SLinus Torvalds 30541da177e4SLinus Torvalds /* 30551da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 30561da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 30571da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 30581da177e4SLinus Torvalds */ 3059909b0a88SAl Viro p = old; 3060cb338d06SAl Viro q = new; 30611da177e4SLinus Torvalds while (p) { 3062143c8c91SAl Viro q->mnt_ns = new_ns; 3063d2921684SEric W. Biederman new_ns->mounts++; 30649559f689SAl Viro if (new_fs) { 30659559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 30669559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 3067315fc83eSAl Viro rootmnt = &p->mnt; 30681da177e4SLinus Torvalds } 30699559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 30709559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 3071315fc83eSAl Viro pwdmnt = &p->mnt; 30721da177e4SLinus Torvalds } 30731da177e4SLinus Torvalds } 3074909b0a88SAl Viro p = next_mnt(p, old); 3075909b0a88SAl Viro q = next_mnt(q, new); 30764ce5d2b1SEric W. Biederman if (!q) 30774ce5d2b1SEric W. Biederman break; 30784ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 30794ce5d2b1SEric W. Biederman p = next_mnt(p, old); 30801da177e4SLinus Torvalds } 3081328e6d90SAl Viro namespace_unlock(); 30821da177e4SLinus Torvalds 30831da177e4SLinus Torvalds if (rootmnt) 3084f03c6599SAl Viro mntput(rootmnt); 30851da177e4SLinus Torvalds if (pwdmnt) 3086f03c6599SAl Viro mntput(pwdmnt); 30871da177e4SLinus Torvalds 3088741a2951SJANAK DESAI return new_ns; 3089741a2951SJANAK DESAI } 3090741a2951SJANAK DESAI 309174e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name) 3092cf8d2c11STrond Myklebust { 30931a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 3094ea441d11SAl Viro struct mnt_namespace *ns; 3095d31da0f0SAl Viro struct super_block *s; 3096ea441d11SAl Viro struct path path; 3097ea441d11SAl Viro int err; 3098ea441d11SAl Viro 309974e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, true); 310074e83122SAl Viro if (IS_ERR(ns)) { 310174e83122SAl Viro mntput(m); 3102ea441d11SAl Viro return ERR_CAST(ns); 310374e83122SAl Viro } 310474e83122SAl Viro mnt->mnt_ns = ns; 310574e83122SAl Viro ns->root = mnt; 310674e83122SAl Viro ns->mounts++; 310774e83122SAl Viro list_add(&mnt->mnt_list, &ns->list); 3108ea441d11SAl Viro 310974e83122SAl Viro err = vfs_path_lookup(m->mnt_root, m, 3110ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 3111ea441d11SAl Viro 3112ea441d11SAl Viro put_mnt_ns(ns); 3113ea441d11SAl Viro 3114ea441d11SAl Viro if (err) 3115ea441d11SAl Viro return ERR_PTR(err); 3116ea441d11SAl Viro 3117ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 3118d31da0f0SAl Viro s = path.mnt->mnt_sb; 3119d31da0f0SAl Viro atomic_inc(&s->s_active); 3120ea441d11SAl Viro mntput(path.mnt); 3121ea441d11SAl Viro /* lock the sucker */ 3122d31da0f0SAl Viro down_write(&s->s_umount); 3123ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 3124ea441d11SAl Viro return path.dentry; 3125ea441d11SAl Viro } 3126ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 3127ea441d11SAl Viro 3128312db1aaSDominik Brodowski int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type, 3129312db1aaSDominik Brodowski unsigned long flags, void __user *data) 31301da177e4SLinus Torvalds { 3131eca6f534SVegard Nossum int ret; 3132eca6f534SVegard Nossum char *kernel_type; 3133eca6f534SVegard Nossum char *kernel_dev; 3134b40ef869SAl Viro void *options; 31351da177e4SLinus Torvalds 3136b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 3137b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 3138b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 3139eca6f534SVegard Nossum goto out_type; 31401da177e4SLinus Torvalds 3141b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 3142b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 3143b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 3144eca6f534SVegard Nossum goto out_dev; 31451da177e4SLinus Torvalds 3146b40ef869SAl Viro options = copy_mount_options(data); 3147b40ef869SAl Viro ret = PTR_ERR(options); 3148b40ef869SAl Viro if (IS_ERR(options)) 3149eca6f534SVegard Nossum goto out_data; 31501da177e4SLinus Torvalds 3151b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 3152eca6f534SVegard Nossum 3153b40ef869SAl Viro kfree(options); 3154eca6f534SVegard Nossum out_data: 3155eca6f534SVegard Nossum kfree(kernel_dev); 3156eca6f534SVegard Nossum out_dev: 3157eca6f534SVegard Nossum kfree(kernel_type); 3158eca6f534SVegard Nossum out_type: 3159eca6f534SVegard Nossum return ret; 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 3162312db1aaSDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 3163312db1aaSDominik Brodowski char __user *, type, unsigned long, flags, void __user *, data) 3164312db1aaSDominik Brodowski { 3165312db1aaSDominik Brodowski return ksys_mount(dev_name, dir_name, type, flags, data); 3166312db1aaSDominik Brodowski } 3167312db1aaSDominik Brodowski 31681da177e4SLinus Torvalds /* 3169afac7cbaSAl Viro * Return true if path is reachable from root 3170afac7cbaSAl Viro * 317148a066e7SAl Viro * namespace_sem or mount_lock is held 3172afac7cbaSAl Viro */ 3173643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3174afac7cbaSAl Viro const struct path *root) 3175afac7cbaSAl Viro { 3176643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3177a73324daSAl Viro dentry = mnt->mnt_mountpoint; 31780714a533SAl Viro mnt = mnt->mnt_parent; 3179afac7cbaSAl Viro } 3180643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3181afac7cbaSAl Viro } 3182afac7cbaSAl Viro 3183640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3184afac7cbaSAl Viro { 318525ab4c9bSYaowei Bai bool res; 318648a066e7SAl Viro read_seqlock_excl(&mount_lock); 3187643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 318848a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3189afac7cbaSAl Viro return res; 3190afac7cbaSAl Viro } 3191afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3192afac7cbaSAl Viro 3193afac7cbaSAl Viro /* 31941da177e4SLinus Torvalds * pivot_root Semantics: 31951da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 31961da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 31971da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 31981da177e4SLinus Torvalds * 31991da177e4SLinus Torvalds * Restrictions: 32001da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 32011da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 32021da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 32031da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 32041da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 32051da177e4SLinus Torvalds * 32064a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 32074a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 32084a0d11faSNeil Brown * in this situation. 32094a0d11faSNeil Brown * 32101da177e4SLinus Torvalds * Notes: 32111da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 32121da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 32131da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 32141da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 32151da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 32161da177e4SLinus Torvalds * first. 32171da177e4SLinus Torvalds */ 32183480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 32193480b257SHeiko Carstens const char __user *, put_old) 32201da177e4SLinus Torvalds { 32212d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 322284d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 322384d17192SAl Viro struct mountpoint *old_mp, *root_mp; 32241da177e4SLinus Torvalds int error; 32251da177e4SLinus Torvalds 32269b40bc90SAl Viro if (!may_mount()) 32271da177e4SLinus Torvalds return -EPERM; 32281da177e4SLinus Torvalds 32292d8f3038SAl Viro error = user_path_dir(new_root, &new); 32301da177e4SLinus Torvalds if (error) 32311da177e4SLinus Torvalds goto out0; 32321da177e4SLinus Torvalds 32332d8f3038SAl Viro error = user_path_dir(put_old, &old); 32341da177e4SLinus Torvalds if (error) 32351da177e4SLinus Torvalds goto out1; 32361da177e4SLinus Torvalds 32372d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3238b12cea91SAl Viro if (error) 3239b12cea91SAl Viro goto out2; 32401da177e4SLinus Torvalds 3241f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 324284d17192SAl Viro old_mp = lock_mount(&old); 324384d17192SAl Viro error = PTR_ERR(old_mp); 324484d17192SAl Viro if (IS_ERR(old_mp)) 3245b12cea91SAl Viro goto out3; 3246b12cea91SAl Viro 32471da177e4SLinus Torvalds error = -EINVAL; 3248419148daSAl Viro new_mnt = real_mount(new.mnt); 3249419148daSAl Viro root_mnt = real_mount(root.mnt); 325084d17192SAl Viro old_mnt = real_mount(old.mnt); 325184d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 3252fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 3253fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 3254b12cea91SAl Viro goto out4; 3255143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3256b12cea91SAl Viro goto out4; 32575ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 32585ff9d8a6SEric W. Biederman goto out4; 32591da177e4SLinus Torvalds error = -ENOENT; 3260f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3261b12cea91SAl Viro goto out4; 32621da177e4SLinus Torvalds error = -EBUSY; 326384d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3264b12cea91SAl Viro goto out4; /* loop, on the same file system */ 32651da177e4SLinus Torvalds error = -EINVAL; 32668c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3267b12cea91SAl Viro goto out4; /* not a mountpoint */ 3268676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3269b12cea91SAl Viro goto out4; /* not attached */ 327084d17192SAl Viro root_mp = root_mnt->mnt_mp; 32712d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3272b12cea91SAl Viro goto out4; /* not a mountpoint */ 3273676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3274b12cea91SAl Viro goto out4; /* not attached */ 32754ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 327684d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3277b12cea91SAl Viro goto out4; 32780d082601SEric W. Biederman /* make certain new is below the root */ 32790d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 32800d082601SEric W. Biederman goto out4; 328184d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 3282719ea2fbSAl Viro lock_mount_hash(); 3283419148daSAl Viro detach_mnt(new_mnt, &parent_path); 3284419148daSAl Viro detach_mnt(root_mnt, &root_parent); 32855ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 32865ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 32875ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 32885ff9d8a6SEric W. Biederman } 32894ac91378SJan Blunck /* mount old root on put_old */ 329084d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 32914ac91378SJan Blunck /* mount new_root on / */ 329284d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 32936b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 32944fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 32954fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 32963895dbf8SEric W. Biederman put_mountpoint(root_mp); 3297719ea2fbSAl Viro unlock_mount_hash(); 32982d8f3038SAl Viro chroot_fs_refs(&root, &new); 32991da177e4SLinus Torvalds error = 0; 3300b12cea91SAl Viro out4: 330184d17192SAl Viro unlock_mount(old_mp); 3302b12cea91SAl Viro if (!error) { 33031a390689SAl Viro path_put(&root_parent); 33041a390689SAl Viro path_put(&parent_path); 3305b12cea91SAl Viro } 3306b12cea91SAl Viro out3: 33078c3ee42eSAl Viro path_put(&root); 3308b12cea91SAl Viro out2: 33092d8f3038SAl Viro path_put(&old); 33101da177e4SLinus Torvalds out1: 33112d8f3038SAl Viro path_put(&new); 33121da177e4SLinus Torvalds out0: 33131da177e4SLinus Torvalds return error; 33141da177e4SLinus Torvalds } 33151da177e4SLinus Torvalds 33161da177e4SLinus Torvalds static void __init init_mount_tree(void) 33171da177e4SLinus Torvalds { 33181da177e4SLinus Torvalds struct vfsmount *mnt; 331974e83122SAl Viro struct mount *m; 33206b3286edSKirill Korotaev struct mnt_namespace *ns; 3321ac748a09SJan Blunck struct path root; 33220c55cfc4SEric W. Biederman struct file_system_type *type; 33231da177e4SLinus Torvalds 33240c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 33250c55cfc4SEric W. Biederman if (!type) 33260c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 33270c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 33280c55cfc4SEric W. Biederman put_filesystem(type); 33291da177e4SLinus Torvalds if (IS_ERR(mnt)) 33301da177e4SLinus Torvalds panic("Can't create rootfs"); 3331b3e19d92SNick Piggin 333274e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, false); 33333b22edc5STrond Myklebust if (IS_ERR(ns)) 33341da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 333574e83122SAl Viro m = real_mount(mnt); 333674e83122SAl Viro m->mnt_ns = ns; 333774e83122SAl Viro ns->root = m; 333874e83122SAl Viro ns->mounts = 1; 333974e83122SAl Viro list_add(&m->mnt_list, &ns->list); 33406b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 33416b3286edSKirill Korotaev get_mnt_ns(ns); 33421da177e4SLinus Torvalds 3343be08d6d2SAl Viro root.mnt = mnt; 3344be08d6d2SAl Viro root.dentry = mnt->mnt_root; 3345da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 3346ac748a09SJan Blunck 3347ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 3348ac748a09SJan Blunck set_fs_root(current->fs, &root); 33491da177e4SLinus Torvalds } 33501da177e4SLinus Torvalds 335174bf17cfSDenis Cheng void __init mnt_init(void) 33521da177e4SLinus Torvalds { 335315a67dd8SRandy Dunlap int err; 33541da177e4SLinus Torvalds 33557d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 335620c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 33571da177e4SLinus Torvalds 33580818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 335938129a13SAl Viro sizeof(struct hlist_head), 33600818bf27SAl Viro mhash_entries, 19, 33613d375d78SPavel Tatashin HASH_ZERO, 33620818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 33630818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 33640818bf27SAl Viro sizeof(struct hlist_head), 33650818bf27SAl Viro mphash_entries, 19, 33663d375d78SPavel Tatashin HASH_ZERO, 33670818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 33681da177e4SLinus Torvalds 336984d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 33701da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 33711da177e4SLinus Torvalds 33724b93dc9bSTejun Heo kernfs_init(); 33734b93dc9bSTejun Heo 337415a67dd8SRandy Dunlap err = sysfs_init(); 337515a67dd8SRandy Dunlap if (err) 337615a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 33778e24eea7SHarvey Harrison __func__, err); 337800d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 337900d26666SGreg Kroah-Hartman if (!fs_kobj) 33808e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 33811da177e4SLinus Torvalds init_rootfs(); 33821da177e4SLinus Torvalds init_mount_tree(); 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds 3385616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 33861da177e4SLinus Torvalds { 3387d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 3388616511d0STrond Myklebust return; 33897b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3390771b1371SEric W. Biederman free_mnt_ns(ns); 33911da177e4SLinus Torvalds } 33929d412a43SAl Viro 3393d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type) 33949d412a43SAl Viro { 3395423e0ab0STim Chen struct vfsmount *mnt; 3396d911b458SDavid Howells mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 3397423e0ab0STim Chen if (!IS_ERR(mnt)) { 3398423e0ab0STim Chen /* 3399423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3400423e0ab0STim Chen * we unmount before file sys is unregistered 3401423e0ab0STim Chen */ 3402f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3403423e0ab0STim Chen } 3404423e0ab0STim Chen return mnt; 34059d412a43SAl Viro } 3406d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount); 3407423e0ab0STim Chen 3408423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3409423e0ab0STim Chen { 3410423e0ab0STim Chen /* release long term mount so mount point can be released */ 3411423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3412f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 341348a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3414423e0ab0STim Chen mntput(mnt); 3415423e0ab0STim Chen } 3416423e0ab0STim Chen } 3417423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 341802125a82SAl Viro 341902125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 342002125a82SAl Viro { 3421143c8c91SAl Viro return check_mnt(real_mount(mnt)); 342202125a82SAl Viro } 34238823c079SEric W. Biederman 34243151527eSEric W. Biederman bool current_chrooted(void) 34253151527eSEric W. Biederman { 34263151527eSEric W. Biederman /* Does the current process have a non-standard root */ 34273151527eSEric W. Biederman struct path ns_root; 34283151527eSEric W. Biederman struct path fs_root; 34293151527eSEric W. Biederman bool chrooted; 34303151527eSEric W. Biederman 34313151527eSEric W. Biederman /* Find the namespace root */ 34323151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 34333151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 34343151527eSEric W. Biederman path_get(&ns_root); 34353151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 34363151527eSEric W. Biederman ; 34373151527eSEric W. Biederman 34383151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 34393151527eSEric W. Biederman 34403151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 34413151527eSEric W. Biederman 34423151527eSEric W. Biederman path_put(&fs_root); 34433151527eSEric W. Biederman path_put(&ns_root); 34443151527eSEric W. Biederman 34453151527eSEric W. Biederman return chrooted; 34463151527eSEric W. Biederman } 34473151527eSEric W. Biederman 3448132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns, 3449132e4608SDavid Howells const struct super_block *sb, 34508654df4eSEric W. Biederman int *new_mnt_flags) 345187a8ebd6SEric W. Biederman { 34528c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 345387a8ebd6SEric W. Biederman struct mount *mnt; 3454e51db735SEric W. Biederman bool visible = false; 345587a8ebd6SEric W. Biederman 345644bb4385SAl Viro down_read(&namespace_sem); 345787a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3458e51db735SEric W. Biederman struct mount *child; 345977b1a97dSEric W. Biederman int mnt_flags; 346077b1a97dSEric W. Biederman 3461132e4608SDavid Howells if (mnt->mnt.mnt_sb->s_type != sb->s_type) 3462e51db735SEric W. Biederman continue; 3463e51db735SEric W. Biederman 34647e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 34657e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 34667e96c1b0SEric W. Biederman */ 34677e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 34687e96c1b0SEric W. Biederman continue; 34697e96c1b0SEric W. Biederman 3470a1935c17SEric W. Biederman /* A local view of the mount flags */ 347177b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 347277b1a97dSEric W. Biederman 3473695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 3474bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt.mnt_sb)) 3475695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 3476695e9df0SEric W. Biederman 34778c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 34788c6cf9ccSEric W. Biederman * than the proposed new mount. 34798c6cf9ccSEric W. Biederman */ 348077b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 34818c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 34828c6cf9ccSEric W. Biederman continue; 348377b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 348477b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 34858c6cf9ccSEric W. Biederman continue; 34868c6cf9ccSEric W. Biederman 3487ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 3488ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 3489ceeb0e5dSEric W. Biederman * empty directories. 3490e51db735SEric W. Biederman */ 3491e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3492e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3493ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 3494d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 3495ceeb0e5dSEric W. Biederman continue; 34967236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 34977236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 3498e51db735SEric W. Biederman goto next; 349987a8ebd6SEric W. Biederman } 35008c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 350177b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 35028c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 3503e51db735SEric W. Biederman visible = true; 3504e51db735SEric W. Biederman goto found; 3505e51db735SEric W. Biederman next: ; 350687a8ebd6SEric W. Biederman } 3507e51db735SEric W. Biederman found: 350844bb4385SAl Viro up_read(&namespace_sem); 3509e51db735SEric W. Biederman return visible; 351087a8ebd6SEric W. Biederman } 351187a8ebd6SEric W. Biederman 3512132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags) 35138654df4eSEric W. Biederman { 3514a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 35158654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 35168654df4eSEric W. Biederman unsigned long s_iflags; 35178654df4eSEric W. Biederman 35188654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 35198654df4eSEric W. Biederman return false; 35208654df4eSEric W. Biederman 35218654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 3522132e4608SDavid Howells s_iflags = sb->s_iflags; 35238654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 35248654df4eSEric W. Biederman return false; 35258654df4eSEric W. Biederman 3526a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 3527a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 3528a1935c17SEric W. Biederman required_iflags); 3529a1935c17SEric W. Biederman return true; 3530a1935c17SEric W. Biederman } 3531a1935c17SEric W. Biederman 3532132e4608SDavid Howells return !mnt_already_visible(ns, sb, new_mnt_flags); 35338654df4eSEric W. Biederman } 35348654df4eSEric W. Biederman 3535380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 3536380cf5baSAndy Lutomirski { 3537380cf5baSAndy Lutomirski /* 3538380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 3539380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 3540380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 3541380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 3542380cf5baSAndy Lutomirski * in other namespaces. 3543380cf5baSAndy Lutomirski */ 3544380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 3545380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 3546380cf5baSAndy Lutomirski } 3547380cf5baSAndy Lutomirski 354864964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 35498823c079SEric W. Biederman { 355058be2825SAl Viro struct ns_common *ns = NULL; 35518823c079SEric W. Biederman struct nsproxy *nsproxy; 35528823c079SEric W. Biederman 3553728dba3aSEric W. Biederman task_lock(task); 3554728dba3aSEric W. Biederman nsproxy = task->nsproxy; 35558823c079SEric W. Biederman if (nsproxy) { 355658be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 355758be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 35588823c079SEric W. Biederman } 3559728dba3aSEric W. Biederman task_unlock(task); 35608823c079SEric W. Biederman 35618823c079SEric W. Biederman return ns; 35628823c079SEric W. Biederman } 35638823c079SEric W. Biederman 356464964528SAl Viro static void mntns_put(struct ns_common *ns) 35658823c079SEric W. Biederman { 356658be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 35678823c079SEric W. Biederman } 35688823c079SEric W. Biederman 356964964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns) 35708823c079SEric W. Biederman { 35718823c079SEric W. Biederman struct fs_struct *fs = current->fs; 35724f757f3cSAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 35738823c079SEric W. Biederman struct path root; 35744f757f3cSAl Viro int err; 35758823c079SEric W. Biederman 35760c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3577c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3578c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3579ae11e0f1SZhao Hongjiang return -EPERM; 35808823c079SEric W. Biederman 358174e83122SAl Viro if (is_anon_ns(mnt_ns)) 358274e83122SAl Viro return -EINVAL; 358374e83122SAl Viro 35848823c079SEric W. Biederman if (fs->users != 1) 35858823c079SEric W. Biederman return -EINVAL; 35868823c079SEric W. Biederman 35878823c079SEric W. Biederman get_mnt_ns(mnt_ns); 35884f757f3cSAl Viro old_mnt_ns = nsproxy->mnt_ns; 35898823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 35908823c079SEric W. Biederman 35918823c079SEric W. Biederman /* Find the root */ 35924f757f3cSAl Viro err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 35934f757f3cSAl Viro "/", LOOKUP_DOWN, &root); 35944f757f3cSAl Viro if (err) { 35954f757f3cSAl Viro /* revert to old namespace */ 35964f757f3cSAl Viro nsproxy->mnt_ns = old_mnt_ns; 35974f757f3cSAl Viro put_mnt_ns(mnt_ns); 35984f757f3cSAl Viro return err; 35994f757f3cSAl Viro } 36008823c079SEric W. Biederman 36014068367cSAndrei Vagin put_mnt_ns(old_mnt_ns); 36024068367cSAndrei Vagin 36038823c079SEric W. Biederman /* Update the pwd and root */ 36048823c079SEric W. Biederman set_fs_pwd(fs, &root); 36058823c079SEric W. Biederman set_fs_root(fs, &root); 36068823c079SEric W. Biederman 36078823c079SEric W. Biederman path_put(&root); 36088823c079SEric W. Biederman return 0; 36098823c079SEric W. Biederman } 36108823c079SEric W. Biederman 3611bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 3612bcac25a5SAndrey Vagin { 3613bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 3614bcac25a5SAndrey Vagin } 3615bcac25a5SAndrey Vagin 36168823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 36178823c079SEric W. Biederman .name = "mnt", 36188823c079SEric W. Biederman .type = CLONE_NEWNS, 36198823c079SEric W. Biederman .get = mntns_get, 36208823c079SEric W. Biederman .put = mntns_put, 36218823c079SEric W. Biederman .install = mntns_install, 3622bcac25a5SAndrey Vagin .owner = mntns_owner, 36238823c079SEric W. Biederman }; 3624