159bd9dedSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/namespace.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * (C) Copyright Al Viro 2000, 2001 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 */ 23a07b2000SAl Viro #include <linux/file.h> 24d10577a8SAl Viro #include <linux/uaccess.h> 250bb80f24SDavid Howells #include <linux/proc_ns.h> 2620b4fb48SLinus Torvalds #include <linux/magic.h> 2757c8a661SMike Rapoport #include <linux/memblock.h> 289ea459e1SAl Viro #include <linux/task_work.h> 299164bb4aSIngo Molnar #include <linux/sched/task.h> 30e262e32dSDavid Howells #include <uapi/linux/mount.h> 319bc61ab1SDavid Howells #include <linux/fs_context.h> 32037f11b4SAl Viro #include <linux/shmem_fs.h> 339164bb4aSIngo Molnar 3407b20889SRam Pai #include "pnode.h" 35948730b0SAdrian Bunk #include "internal.h" 361da177e4SLinus Torvalds 37d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */ 38d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000; 39d2921684SEric W. Biederman 400818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 410818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 420818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 430818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 440818bf27SAl Viro 450818bf27SAl Viro static __initdata unsigned long mhash_entries; 460818bf27SAl Viro static int __init set_mhash_entries(char *str) 470818bf27SAl Viro { 480818bf27SAl Viro if (!str) 490818bf27SAl Viro return 0; 500818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 510818bf27SAl Viro return 1; 520818bf27SAl Viro } 530818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 540818bf27SAl Viro 550818bf27SAl Viro static __initdata unsigned long mphash_entries; 560818bf27SAl Viro static int __init set_mphash_entries(char *str) 570818bf27SAl Viro { 580818bf27SAl Viro if (!str) 590818bf27SAl Viro return 0; 600818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 610818bf27SAl Viro return 1; 620818bf27SAl Viro } 630818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 6413f14b4dSEric Dumazet 65c7999c36SAl Viro static u64 event; 6673cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 67719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 685addc5ddSAl Viro 6938129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 700818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 71e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 7259aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 734edbe133SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 744edbe133SAl Viro static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */ 751da177e4SLinus Torvalds 76f87fd4c2SMiklos Szeredi /* /sys/fs */ 7700d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 7800d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 79f87fd4c2SMiklos Szeredi 8099b7db7bSNick Piggin /* 8199b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 8299b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 8399b7db7bSNick Piggin * up the tree. 8499b7db7bSNick Piggin * 8599b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 8699b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 8799b7db7bSNick Piggin */ 8848a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 8999b7db7bSNick Piggin 90d033cb67SChristian Brauner static inline void lock_mount_hash(void) 91d033cb67SChristian Brauner { 92d033cb67SChristian Brauner write_seqlock(&mount_lock); 93d033cb67SChristian Brauner } 94d033cb67SChristian Brauner 95d033cb67SChristian Brauner static inline void unlock_mount_hash(void) 96d033cb67SChristian Brauner { 97d033cb67SChristian Brauner write_sequnlock(&mount_lock); 98d033cb67SChristian Brauner } 99d033cb67SChristian Brauner 10038129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 1011da177e4SLinus Torvalds { 1021da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 1031da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 1040818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 1050818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 1060818bf27SAl Viro } 1070818bf27SAl Viro 1080818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 1090818bf27SAl Viro { 1100818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 1110818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 1120818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 115b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 11673cd49ecSMiklos Szeredi { 117169b480eSMatthew Wilcox int res = ida_alloc(&mnt_id_ida, GFP_KERNEL); 11873cd49ecSMiklos Szeredi 119169b480eSMatthew Wilcox if (res < 0) 12073cd49ecSMiklos Szeredi return res; 121169b480eSMatthew Wilcox mnt->mnt_id = res; 122169b480eSMatthew Wilcox return 0; 12373cd49ecSMiklos Szeredi } 12473cd49ecSMiklos Szeredi 125b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 12673cd49ecSMiklos Szeredi { 127169b480eSMatthew Wilcox ida_free(&mnt_id_ida, mnt->mnt_id); 12873cd49ecSMiklos Szeredi } 12973cd49ecSMiklos Szeredi 130719f5d7fSMiklos Szeredi /* 131719f5d7fSMiklos Szeredi * Allocate a new peer group ID 132719f5d7fSMiklos Szeredi */ 1334b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 134719f5d7fSMiklos Szeredi { 135169b480eSMatthew Wilcox int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL); 136f21f6220SAl Viro 137169b480eSMatthew Wilcox if (res < 0) 138f21f6220SAl Viro return res; 139169b480eSMatthew Wilcox mnt->mnt_group_id = res; 140169b480eSMatthew Wilcox return 0; 141719f5d7fSMiklos Szeredi } 142719f5d7fSMiklos Szeredi 143719f5d7fSMiklos Szeredi /* 144719f5d7fSMiklos Szeredi * Release a peer group ID 145719f5d7fSMiklos Szeredi */ 1464b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 147719f5d7fSMiklos Szeredi { 148169b480eSMatthew Wilcox ida_free(&mnt_group_ida, mnt->mnt_group_id); 14915169fe7SAl Viro mnt->mnt_group_id = 0; 150719f5d7fSMiklos Szeredi } 151719f5d7fSMiklos Szeredi 152b3e19d92SNick Piggin /* 153b3e19d92SNick Piggin * vfsmount lock must be held for read 154b3e19d92SNick Piggin */ 15583adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 156b3e19d92SNick Piggin { 157b3e19d92SNick Piggin #ifdef CONFIG_SMP 15868e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 159b3e19d92SNick Piggin #else 160b3e19d92SNick Piggin preempt_disable(); 16168e8a9feSAl Viro mnt->mnt_count += n; 162b3e19d92SNick Piggin preempt_enable(); 163b3e19d92SNick Piggin #endif 164b3e19d92SNick Piggin } 165b3e19d92SNick Piggin 166b3e19d92SNick Piggin /* 167b3e19d92SNick Piggin * vfsmount lock must be held for write 168b3e19d92SNick Piggin */ 169edf7ddbfSEric Biggers int mnt_get_count(struct mount *mnt) 170b3e19d92SNick Piggin { 171b3e19d92SNick Piggin #ifdef CONFIG_SMP 172edf7ddbfSEric Biggers int count = 0; 173b3e19d92SNick Piggin int cpu; 174b3e19d92SNick Piggin 175b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 17668e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 177b3e19d92SNick Piggin } 178b3e19d92SNick Piggin 179b3e19d92SNick Piggin return count; 180b3e19d92SNick Piggin #else 18168e8a9feSAl Viro return mnt->mnt_count; 182b3e19d92SNick Piggin #endif 183b3e19d92SNick Piggin } 184b3e19d92SNick Piggin 185b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1861da177e4SLinus Torvalds { 187c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 188c63181e6SAl Viro if (mnt) { 18973cd49ecSMiklos Szeredi int err; 19073cd49ecSMiklos Szeredi 191c63181e6SAl Viro err = mnt_alloc_id(mnt); 19288b38782SLi Zefan if (err) 19388b38782SLi Zefan goto out_free_cache; 19488b38782SLi Zefan 19588b38782SLi Zefan if (name) { 196fcc139aeSAndrzej Hajda mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL); 197c63181e6SAl Viro if (!mnt->mnt_devname) 19888b38782SLi Zefan goto out_free_id; 19973cd49ecSMiklos Szeredi } 20073cd49ecSMiklos Szeredi 201b3e19d92SNick Piggin #ifdef CONFIG_SMP 202c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 203c63181e6SAl Viro if (!mnt->mnt_pcp) 204b3e19d92SNick Piggin goto out_free_devname; 205b3e19d92SNick Piggin 206c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 207b3e19d92SNick Piggin #else 208c63181e6SAl Viro mnt->mnt_count = 1; 209c63181e6SAl Viro mnt->mnt_writers = 0; 210b3e19d92SNick Piggin #endif 211b3e19d92SNick Piggin 21238129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 213c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 214c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 215c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 216c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 217c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 218c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 219c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2200a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 22199b19d16SEric W. Biederman INIT_LIST_HEAD(&mnt->mnt_umounting); 22256cbb429SAl Viro INIT_HLIST_HEAD(&mnt->mnt_stuck_children); 223a6435940SChristian Brauner mnt->mnt.mnt_userns = &init_user_ns; 2241da177e4SLinus Torvalds } 225c63181e6SAl Viro return mnt; 22688b38782SLi Zefan 227d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 228d3ef3d73Snpiggin@suse.de out_free_devname: 229fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 230d3ef3d73Snpiggin@suse.de #endif 23188b38782SLi Zefan out_free_id: 232c63181e6SAl Viro mnt_free_id(mnt); 23388b38782SLi Zefan out_free_cache: 234c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 23588b38782SLi Zefan return NULL; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2388366025eSDave Hansen /* 2398366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2408366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2418366025eSDave Hansen * We must keep track of when those operations start 2428366025eSDave Hansen * (for permission checks) and when they end, so that 2438366025eSDave Hansen * we can determine when writes are able to occur to 2448366025eSDave Hansen * a filesystem. 2458366025eSDave Hansen */ 2463d733633SDave Hansen /* 2473d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2483d733633SDave Hansen * @mnt: the mount to check for its write status 2493d733633SDave Hansen * 2503d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2513d733633SDave Hansen * It does not guarantee that the filesystem will stay 2523d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2533d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2543d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2553d733633SDave Hansen * r/w. 2563d733633SDave Hansen */ 25743f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt) 2583d733633SDave Hansen { 25943f5e655SDavid Howells return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb); 2603d733633SDave Hansen } 2613d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2623d733633SDave Hansen 26383adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2643d733633SDave Hansen { 265d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 26668e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 267d3ef3d73Snpiggin@suse.de #else 26868e8a9feSAl Viro mnt->mnt_writers++; 269d3ef3d73Snpiggin@suse.de #endif 2703d733633SDave Hansen } 2713d733633SDave Hansen 27283adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2733d733633SDave Hansen { 274d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 27568e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 276d3ef3d73Snpiggin@suse.de #else 27768e8a9feSAl Viro mnt->mnt_writers--; 278d3ef3d73Snpiggin@suse.de #endif 279d3ef3d73Snpiggin@suse.de } 280d3ef3d73Snpiggin@suse.de 28183adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 282d3ef3d73Snpiggin@suse.de { 283d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 284d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2853d733633SDave Hansen int cpu; 2863d733633SDave Hansen 2873d733633SDave Hansen for_each_possible_cpu(cpu) { 28868e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2893d733633SDave Hansen } 2903d733633SDave Hansen 291d3ef3d73Snpiggin@suse.de return count; 292d3ef3d73Snpiggin@suse.de #else 293d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 294d3ef3d73Snpiggin@suse.de #endif 2953d733633SDave Hansen } 2963d733633SDave Hansen 2974ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2984ed5e82fSMiklos Szeredi { 2994ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 3004ed5e82fSMiklos Szeredi return 1; 3014ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 3024ed5e82fSMiklos Szeredi smp_rmb(); 3034ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 3044ed5e82fSMiklos Szeredi } 3054ed5e82fSMiklos Szeredi 3063d733633SDave Hansen /* 307eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 308eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 309eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 310eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3113d733633SDave Hansen */ 3128366025eSDave Hansen /** 313eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 31483adc753SAl Viro * @m: the mount on which to take a write 3158366025eSDave Hansen * 316eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 317eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 318eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 319eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 320eb04c282SJan Kara * called. This is effectively a refcount. 3218366025eSDave Hansen */ 322eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3238366025eSDave Hansen { 32483adc753SAl Viro struct mount *mnt = real_mount(m); 3253d733633SDave Hansen int ret = 0; 3263d733633SDave Hansen 327d3ef3d73Snpiggin@suse.de preempt_disable(); 328c6653a83SNick Piggin mnt_inc_writers(mnt); 329d3ef3d73Snpiggin@suse.de /* 330c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 331d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 332d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 333d3ef3d73Snpiggin@suse.de */ 334d3ef3d73Snpiggin@suse.de smp_mb(); 3356aa7de05SMark Rutland while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 336d3ef3d73Snpiggin@suse.de cpu_relax(); 337d3ef3d73Snpiggin@suse.de /* 338d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 339d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 340d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 341d3ef3d73Snpiggin@suse.de */ 342d3ef3d73Snpiggin@suse.de smp_rmb(); 3434ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 344c6653a83SNick Piggin mnt_dec_writers(mnt); 3453d733633SDave Hansen ret = -EROFS; 3463d733633SDave Hansen } 347d3ef3d73Snpiggin@suse.de preempt_enable(); 348eb04c282SJan Kara 349eb04c282SJan Kara return ret; 350eb04c282SJan Kara } 351eb04c282SJan Kara 352eb04c282SJan Kara /** 353eb04c282SJan Kara * mnt_want_write - get write access to a mount 354eb04c282SJan Kara * @m: the mount on which to take a write 355eb04c282SJan Kara * 356eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 357eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 358eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 359eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 360eb04c282SJan Kara */ 361eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 362eb04c282SJan Kara { 363eb04c282SJan Kara int ret; 364eb04c282SJan Kara 365eb04c282SJan Kara sb_start_write(m->mnt_sb); 366eb04c282SJan Kara ret = __mnt_want_write(m); 367eb04c282SJan Kara if (ret) 368eb04c282SJan Kara sb_end_write(m->mnt_sb); 3693d733633SDave Hansen return ret; 3708366025eSDave Hansen } 3718366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3728366025eSDave Hansen 3738366025eSDave Hansen /** 37496029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 37596029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 37696029c4eSnpiggin@suse.de * 37796029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 37896029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 37996029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 38096029c4eSnpiggin@suse.de * on it. This allows some optimisation. 38196029c4eSnpiggin@suse.de * 38296029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 38396029c4eSnpiggin@suse.de * drop the reference. 38496029c4eSnpiggin@suse.de */ 38596029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 38696029c4eSnpiggin@suse.de { 38796029c4eSnpiggin@suse.de /* superblock may be r/o */ 38896029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 38996029c4eSnpiggin@suse.de return -EROFS; 39096029c4eSnpiggin@suse.de preempt_disable(); 39183adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 39296029c4eSnpiggin@suse.de preempt_enable(); 39396029c4eSnpiggin@suse.de return 0; 39496029c4eSnpiggin@suse.de } 39596029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 39696029c4eSnpiggin@suse.de 39796029c4eSnpiggin@suse.de /** 398eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 399eb04c282SJan Kara * @file: the file who's mount on which to take a write 400eb04c282SJan Kara * 401eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 402eb04c282SJan Kara * do some optimisations if the file is open for write already 403eb04c282SJan Kara */ 404eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 405eb04c282SJan Kara { 40683f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 407eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 408eb04c282SJan Kara else 409eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 410eb04c282SJan Kara } 411eb04c282SJan Kara 412eb04c282SJan Kara /** 4137c6893e3SMiklos Szeredi * mnt_want_write_file - get write access to a file's mount 4147c6893e3SMiklos Szeredi * @file: the file who's mount on which to take a write 4157c6893e3SMiklos Szeredi * 4167c6893e3SMiklos Szeredi * This is like mnt_want_write, but it takes a file and can 4177c6893e3SMiklos Szeredi * do some optimisations if the file is open for write already 4187c6893e3SMiklos Szeredi */ 4197c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file) 4207c6893e3SMiklos Szeredi { 4217c6893e3SMiklos Szeredi int ret; 4227c6893e3SMiklos Szeredi 4237c6893e3SMiklos Szeredi sb_start_write(file_inode(file)->i_sb); 4247c6893e3SMiklos Szeredi ret = __mnt_want_write_file(file); 4257c6893e3SMiklos Szeredi if (ret) 4267c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4277c6893e3SMiklos Szeredi return ret; 4287c6893e3SMiklos Szeredi } 42996029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 43096029c4eSnpiggin@suse.de 43196029c4eSnpiggin@suse.de /** 432eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4338366025eSDave Hansen * @mnt: the mount on which to give up write access 4348366025eSDave Hansen * 4358366025eSDave Hansen * Tells the low-level filesystem that we are done 4368366025eSDave Hansen * performing writes to it. Must be matched with 437eb04c282SJan Kara * __mnt_want_write() call above. 4388366025eSDave Hansen */ 439eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4408366025eSDave Hansen { 441d3ef3d73Snpiggin@suse.de preempt_disable(); 44283adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 443d3ef3d73Snpiggin@suse.de preempt_enable(); 4448366025eSDave Hansen } 445eb04c282SJan Kara 446eb04c282SJan Kara /** 447eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 448eb04c282SJan Kara * @mnt: the mount on which to give up write access 449eb04c282SJan Kara * 450eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 451eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 452eb04c282SJan Kara * mnt_want_write() call above. 453eb04c282SJan Kara */ 454eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 455eb04c282SJan Kara { 456eb04c282SJan Kara __mnt_drop_write(mnt); 457eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 458eb04c282SJan Kara } 4598366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4608366025eSDave Hansen 461eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 462eb04c282SJan Kara { 463eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 464eb04c282SJan Kara } 465eb04c282SJan Kara 4667c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file) 4677c6893e3SMiklos Szeredi { 468a6795a58SMiklos Szeredi __mnt_drop_write_file(file); 4697c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4707c6893e3SMiklos Szeredi } 4712a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4722a79f17eSAl Viro 47383adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4748366025eSDave Hansen { 4753d733633SDave Hansen int ret = 0; 4763d733633SDave Hansen 47783adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 478d3ef3d73Snpiggin@suse.de /* 479d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 480d3ef3d73Snpiggin@suse.de * should be visible before we do. 481d3ef3d73Snpiggin@suse.de */ 482d3ef3d73Snpiggin@suse.de smp_mb(); 483d3ef3d73Snpiggin@suse.de 484d3ef3d73Snpiggin@suse.de /* 485d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 486d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 487d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 488d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 489d3ef3d73Snpiggin@suse.de * 490d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 491d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 492d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 493d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 494d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 495d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 496d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 497d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 498d3ef3d73Snpiggin@suse.de * we're counting up here. 499d3ef3d73Snpiggin@suse.de */ 500c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 501d3ef3d73Snpiggin@suse.de ret = -EBUSY; 502d3ef3d73Snpiggin@suse.de else 50383adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 504d3ef3d73Snpiggin@suse.de /* 505d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 506d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 507d3ef3d73Snpiggin@suse.de */ 508d3ef3d73Snpiggin@suse.de smp_wmb(); 50983adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5103d733633SDave Hansen return ret; 5113d733633SDave Hansen } 5128366025eSDave Hansen 5134ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5144ed5e82fSMiklos Szeredi { 5154ed5e82fSMiklos Szeredi struct mount *mnt; 5164ed5e82fSMiklos Szeredi int err = 0; 5174ed5e82fSMiklos Szeredi 5188e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5198e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5208e8b8796SMiklos Szeredi return -EBUSY; 5218e8b8796SMiklos Szeredi 522719ea2fbSAl Viro lock_mount_hash(); 5234ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5244ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5254ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5264ed5e82fSMiklos Szeredi smp_mb(); 5274ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5284ed5e82fSMiklos Szeredi err = -EBUSY; 5294ed5e82fSMiklos Szeredi break; 5304ed5e82fSMiklos Szeredi } 5314ed5e82fSMiklos Szeredi } 5324ed5e82fSMiklos Szeredi } 5338e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5348e8b8796SMiklos Szeredi err = -EBUSY; 5358e8b8796SMiklos Szeredi 5364ed5e82fSMiklos Szeredi if (!err) { 5374ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5384ed5e82fSMiklos Szeredi smp_wmb(); 5394ed5e82fSMiklos Szeredi } 5404ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5414ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5424ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5434ed5e82fSMiklos Szeredi } 544719ea2fbSAl Viro unlock_mount_hash(); 5454ed5e82fSMiklos Szeredi 5464ed5e82fSMiklos Szeredi return err; 5474ed5e82fSMiklos Szeredi } 5484ed5e82fSMiklos Szeredi 549b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5501da177e4SLinus Torvalds { 551a6435940SChristian Brauner struct user_namespace *mnt_userns; 552a6435940SChristian Brauner 553a6435940SChristian Brauner mnt_userns = mnt_user_ns(&mnt->mnt); 554a6435940SChristian Brauner if (mnt_userns != &init_user_ns) 555a6435940SChristian Brauner put_user_ns(mnt_userns); 556fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 557d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 55868e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 559d3ef3d73Snpiggin@suse.de #endif 560b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5638ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5648ffcb32eSDavid Howells { 5658ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5668ffcb32eSDavid Howells } 5678ffcb32eSDavid Howells 56848a066e7SAl Viro /* call under rcu_read_lock */ 569294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 57048a066e7SAl Viro { 57148a066e7SAl Viro struct mount *mnt; 57248a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 573294d71ffSAl Viro return 1; 57448a066e7SAl Viro if (bastard == NULL) 575294d71ffSAl Viro return 0; 57648a066e7SAl Viro mnt = real_mount(bastard); 57748a066e7SAl Viro mnt_add_count(mnt, 1); 578119e1ef8SAl Viro smp_mb(); // see mntput_no_expire() 57948a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 580294d71ffSAl Viro return 0; 58148a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 58248a066e7SAl Viro mnt_add_count(mnt, -1); 583294d71ffSAl Viro return 1; 58448a066e7SAl Viro } 585119e1ef8SAl Viro lock_mount_hash(); 586119e1ef8SAl Viro if (unlikely(bastard->mnt_flags & MNT_DOOMED)) { 587119e1ef8SAl Viro mnt_add_count(mnt, -1); 588119e1ef8SAl Viro unlock_mount_hash(); 589119e1ef8SAl Viro return 1; 590119e1ef8SAl Viro } 591119e1ef8SAl Viro unlock_mount_hash(); 592119e1ef8SAl Viro /* caller will mntput() */ 593294d71ffSAl Viro return -1; 594294d71ffSAl Viro } 595294d71ffSAl Viro 596294d71ffSAl Viro /* call under rcu_read_lock */ 597294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 598294d71ffSAl Viro { 599294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 600294d71ffSAl Viro if (likely(!res)) 601294d71ffSAl Viro return true; 602294d71ffSAl Viro if (unlikely(res < 0)) { 60348a066e7SAl Viro rcu_read_unlock(); 60448a066e7SAl Viro mntput(bastard); 60548a066e7SAl Viro rcu_read_lock(); 606294d71ffSAl Viro } 60748a066e7SAl Viro return false; 60848a066e7SAl Viro } 60948a066e7SAl Viro 6101da177e4SLinus Torvalds /* 611474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 61248a066e7SAl Viro * call under rcu_read_lock() 6131da177e4SLinus Torvalds */ 614474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6151da177e4SLinus Torvalds { 61638129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 617474279dcSAl Viro struct mount *p; 6181da177e4SLinus Torvalds 61938129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 620474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 621474279dcSAl Viro return p; 622474279dcSAl Viro return NULL; 6231da177e4SLinus Torvalds } 624474279dcSAl Viro 625474279dcSAl Viro /* 626f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 627f015f126SDavid Howells * 628f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 629f015f126SDavid Howells * following mounts: 630f015f126SDavid Howells * 631f015f126SDavid Howells * mount /dev/sda1 /mnt 632f015f126SDavid Howells * mount /dev/sda2 /mnt 633f015f126SDavid Howells * mount /dev/sda3 /mnt 634f015f126SDavid Howells * 635f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 636f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 637f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 638f015f126SDavid Howells * 639f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 640a05964f3SRam Pai */ 641ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 642a05964f3SRam Pai { 643c7105365SAl Viro struct mount *child_mnt; 64448a066e7SAl Viro struct vfsmount *m; 64548a066e7SAl Viro unsigned seq; 64699b7db7bSNick Piggin 64748a066e7SAl Viro rcu_read_lock(); 64848a066e7SAl Viro do { 64948a066e7SAl Viro seq = read_seqbegin(&mount_lock); 650474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 65148a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 65248a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 65348a066e7SAl Viro rcu_read_unlock(); 65448a066e7SAl Viro return m; 655a05964f3SRam Pai } 656a05964f3SRam Pai 6579f6c61f9SMiklos Szeredi static inline void lock_ns_list(struct mnt_namespace *ns) 6589f6c61f9SMiklos Szeredi { 6599f6c61f9SMiklos Szeredi spin_lock(&ns->ns_lock); 6609f6c61f9SMiklos Szeredi } 6619f6c61f9SMiklos Szeredi 6629f6c61f9SMiklos Szeredi static inline void unlock_ns_list(struct mnt_namespace *ns) 6639f6c61f9SMiklos Szeredi { 6649f6c61f9SMiklos Szeredi spin_unlock(&ns->ns_lock); 6659f6c61f9SMiklos Szeredi } 6669f6c61f9SMiklos Szeredi 6679f6c61f9SMiklos Szeredi static inline bool mnt_is_cursor(struct mount *mnt) 6689f6c61f9SMiklos Szeredi { 6699f6c61f9SMiklos Szeredi return mnt->mnt.mnt_flags & MNT_CURSOR; 6709f6c61f9SMiklos Szeredi } 6719f6c61f9SMiklos Szeredi 6727af1364fSEric W. Biederman /* 6737af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 6747af1364fSEric W. Biederman * current mount namespace. 6757af1364fSEric W. Biederman * 6767af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 6777af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 6787af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 6797af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 6807af1364fSEric W. Biederman * is a mountpoint. 6817af1364fSEric W. Biederman * 6827af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 6837af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 6847af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 6857af1364fSEric W. Biederman * parent mount. 6867af1364fSEric W. Biederman */ 6877af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 6887af1364fSEric W. Biederman { 6897af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 6907af1364fSEric W. Biederman struct mount *mnt; 6917af1364fSEric W. Biederman bool is_covered = false; 6927af1364fSEric W. Biederman 6937af1364fSEric W. Biederman down_read(&namespace_sem); 6949f6c61f9SMiklos Szeredi lock_ns_list(ns); 6957af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 6969f6c61f9SMiklos Szeredi if (mnt_is_cursor(mnt)) 6979f6c61f9SMiklos Szeredi continue; 6987af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 6997af1364fSEric W. Biederman if (is_covered) 7007af1364fSEric W. Biederman break; 7017af1364fSEric W. Biederman } 7029f6c61f9SMiklos Szeredi unlock_ns_list(ns); 7037af1364fSEric W. Biederman up_read(&namespace_sem); 7045ad05cc8SNikolay Borisov 7057af1364fSEric W. Biederman return is_covered; 7067af1364fSEric W. Biederman } 7077af1364fSEric W. Biederman 708e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 70984d17192SAl Viro { 7100818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 71184d17192SAl Viro struct mountpoint *mp; 71284d17192SAl Viro 7130818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 71484d17192SAl Viro if (mp->m_dentry == dentry) { 71584d17192SAl Viro mp->m_count++; 71684d17192SAl Viro return mp; 71784d17192SAl Viro } 71884d17192SAl Viro } 719e2dfa935SEric W. Biederman return NULL; 720e2dfa935SEric W. Biederman } 721e2dfa935SEric W. Biederman 7223895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 723e2dfa935SEric W. Biederman { 7243895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 725e2dfa935SEric W. Biederman int ret; 72684d17192SAl Viro 7273895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7281e9c75fbSBenjamin Coddington /* might be worth a WARN_ON() */ 7291e9c75fbSBenjamin Coddington if (d_unlinked(dentry)) 7301e9c75fbSBenjamin Coddington return ERR_PTR(-ENOENT); 7313895dbf8SEric W. Biederman mountpoint: 7323895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7333895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 7343895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7353895dbf8SEric W. Biederman if (mp) 7363895dbf8SEric W. Biederman goto done; 73784d17192SAl Viro } 738eed81007SMiklos Szeredi 7393895dbf8SEric W. Biederman if (!new) 7403895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 7413895dbf8SEric W. Biederman if (!new) 7423895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 7433895dbf8SEric W. Biederman 7443895dbf8SEric W. Biederman 7453895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 7463895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 7473895dbf8SEric W. Biederman 7483895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 7493895dbf8SEric W. Biederman if (ret == -EBUSY) 7503895dbf8SEric W. Biederman goto mountpoint; 7513895dbf8SEric W. Biederman 7523895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 7533895dbf8SEric W. Biederman mp = ERR_PTR(ret); 7543895dbf8SEric W. Biederman if (ret) 7553895dbf8SEric W. Biederman goto done; 7563895dbf8SEric W. Biederman 7573895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 7583895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7594edbe133SAl Viro new->m_dentry = dget(dentry); 7603895dbf8SEric W. Biederman new->m_count = 1; 7613895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 7623895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 7633895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7643895dbf8SEric W. Biederman 7653895dbf8SEric W. Biederman mp = new; 7663895dbf8SEric W. Biederman new = NULL; 7673895dbf8SEric W. Biederman done: 7683895dbf8SEric W. Biederman kfree(new); 76984d17192SAl Viro return mp; 77084d17192SAl Viro } 77184d17192SAl Viro 7724edbe133SAl Viro /* 7734edbe133SAl Viro * vfsmount lock must be held. Additionally, the caller is responsible 7744edbe133SAl Viro * for serializing calls for given disposal list. 7754edbe133SAl Viro */ 7764edbe133SAl Viro static void __put_mountpoint(struct mountpoint *mp, struct list_head *list) 77784d17192SAl Viro { 77884d17192SAl Viro if (!--mp->m_count) { 77984d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7800a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 78184d17192SAl Viro spin_lock(&dentry->d_lock); 78284d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 78384d17192SAl Viro spin_unlock(&dentry->d_lock); 7844edbe133SAl Viro dput_to_list(dentry, list); 7850818bf27SAl Viro hlist_del(&mp->m_hash); 78684d17192SAl Viro kfree(mp); 78784d17192SAl Viro } 78884d17192SAl Viro } 78984d17192SAl Viro 7904edbe133SAl Viro /* called with namespace_lock and vfsmount lock */ 7914edbe133SAl Viro static void put_mountpoint(struct mountpoint *mp) 7924edbe133SAl Viro { 7934edbe133SAl Viro __put_mountpoint(mp, &ex_mountpoints); 7944edbe133SAl Viro } 7954edbe133SAl Viro 796143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7971da177e4SLinus Torvalds { 7986b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 80199b7db7bSNick Piggin /* 80299b7db7bSNick Piggin * vfsmount lock must be held for write 80399b7db7bSNick Piggin */ 8046b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 8055addc5ddSAl Viro { 8065addc5ddSAl Viro if (ns) { 8075addc5ddSAl Viro ns->event = ++event; 8085addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8095addc5ddSAl Viro } 8105addc5ddSAl Viro } 8115addc5ddSAl Viro 81299b7db7bSNick Piggin /* 81399b7db7bSNick Piggin * vfsmount lock must be held for write 81499b7db7bSNick Piggin */ 8156b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 8165addc5ddSAl Viro { 8175addc5ddSAl Viro if (ns && ns->event != event) { 8185addc5ddSAl Viro ns->event = event; 8195addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8205addc5ddSAl Viro } 8215addc5ddSAl Viro } 8225addc5ddSAl Viro 82399b7db7bSNick Piggin /* 82499b7db7bSNick Piggin * vfsmount lock must be held for write 82599b7db7bSNick Piggin */ 826e4e59906SAl Viro static struct mountpoint *unhash_mnt(struct mount *mnt) 8271da177e4SLinus Torvalds { 828e4e59906SAl Viro struct mountpoint *mp; 8290714a533SAl Viro mnt->mnt_parent = mnt; 830a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8316b41d536SAl Viro list_del_init(&mnt->mnt_child); 83238129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8330a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 834e4e59906SAl Viro mp = mnt->mnt_mp; 83584d17192SAl Viro mnt->mnt_mp = NULL; 836e4e59906SAl Viro return mp; 8377bdb11deSEric W. Biederman } 8387bdb11deSEric W. Biederman 8397bdb11deSEric W. Biederman /* 8407bdb11deSEric W. Biederman * vfsmount lock must be held for write 8417bdb11deSEric W. Biederman */ 8426a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 8436a46c573SEric W. Biederman { 844e4e59906SAl Viro put_mountpoint(unhash_mnt(mnt)); 8456a46c573SEric W. Biederman } 8466a46c573SEric W. Biederman 8476a46c573SEric W. Biederman /* 8486a46c573SEric W. Biederman * vfsmount lock must be held for write 8496a46c573SEric W. Biederman */ 85084d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 85184d17192SAl Viro struct mountpoint *mp, 85244d964d6SAl Viro struct mount *child_mnt) 853b90fa9aeSRam Pai { 85484d17192SAl Viro mp->m_count++; 8553a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 8564edbe133SAl Viro child_mnt->mnt_mountpoint = mp->m_dentry; 8573a2393d7SAl Viro child_mnt->mnt_parent = mnt; 85884d17192SAl Viro child_mnt->mnt_mp = mp; 8590a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 860b90fa9aeSRam Pai } 861b90fa9aeSRam Pai 8621064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 8631064f874SEric W. Biederman { 8641064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 8651064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8661064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8671064f874SEric W. Biederman } 8681064f874SEric W. Biederman 86999b7db7bSNick Piggin /* 87099b7db7bSNick Piggin * vfsmount lock must be held for write 87199b7db7bSNick Piggin */ 87284d17192SAl Viro static void attach_mnt(struct mount *mnt, 87384d17192SAl Viro struct mount *parent, 87484d17192SAl Viro struct mountpoint *mp) 8751da177e4SLinus Torvalds { 87684d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 8771064f874SEric W. Biederman __attach_mnt(mnt, parent); 878b90fa9aeSRam Pai } 879b90fa9aeSRam Pai 8801064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 88112a5b529SAl Viro { 8821064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 8831064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 8841064f874SEric W. Biederman 8851064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 8861064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 8871064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 8881064f874SEric W. Biederman 8891064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 8901064f874SEric W. Biederman 8911064f874SEric W. Biederman put_mountpoint(old_mp); 8921064f874SEric W. Biederman mnt_add_count(old_parent, -1); 89312a5b529SAl Viro } 89412a5b529SAl Viro 895b90fa9aeSRam Pai /* 89699b7db7bSNick Piggin * vfsmount lock must be held for write 897b90fa9aeSRam Pai */ 8981064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 899b90fa9aeSRam Pai { 9000714a533SAl Viro struct mount *parent = mnt->mnt_parent; 90183adc753SAl Viro struct mount *m; 902b90fa9aeSRam Pai LIST_HEAD(head); 903143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 904b90fa9aeSRam Pai 9050714a533SAl Viro BUG_ON(parent == mnt); 906b90fa9aeSRam Pai 9071a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 908f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 909143c8c91SAl Viro m->mnt_ns = n; 910f03c6599SAl Viro 911b90fa9aeSRam Pai list_splice(&head, n->list.prev); 912b90fa9aeSRam Pai 913d2921684SEric W. Biederman n->mounts += n->pending_mounts; 914d2921684SEric W. Biederman n->pending_mounts = 0; 915d2921684SEric W. Biederman 9161064f874SEric W. Biederman __attach_mnt(mnt, parent); 9176b3286edSKirill Korotaev touch_mnt_namespace(n); 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 920909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 9211da177e4SLinus Torvalds { 9226b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 9236b41d536SAl Viro if (next == &p->mnt_mounts) { 9241da177e4SLinus Torvalds while (1) { 925909b0a88SAl Viro if (p == root) 9261da177e4SLinus Torvalds return NULL; 9276b41d536SAl Viro next = p->mnt_child.next; 9286b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 9291da177e4SLinus Torvalds break; 9300714a533SAl Viro p = p->mnt_parent; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds } 9336b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 936315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 9379676f0c6SRam Pai { 9386b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 9396b41d536SAl Viro while (prev != &p->mnt_mounts) { 9406b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 9416b41d536SAl Viro prev = p->mnt_mounts.prev; 9429676f0c6SRam Pai } 9439676f0c6SRam Pai return p; 9449676f0c6SRam Pai } 9459676f0c6SRam Pai 9468f291889SAl Viro /** 9478f291889SAl Viro * vfs_create_mount - Create a mount for a configured superblock 9488f291889SAl Viro * @fc: The configuration context with the superblock attached 9498f291889SAl Viro * 9508f291889SAl Viro * Create a mount to an already configured superblock. If necessary, the 9518f291889SAl Viro * caller should invoke vfs_get_tree() before calling this. 9528f291889SAl Viro * 9538f291889SAl Viro * Note that this does not attach the mount to anything. 9548f291889SAl Viro */ 9558f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc) 9569d412a43SAl Viro { 957b105e270SAl Viro struct mount *mnt; 9589d412a43SAl Viro 9598f291889SAl Viro if (!fc->root) 9608f291889SAl Viro return ERR_PTR(-EINVAL); 9619d412a43SAl Viro 9628f291889SAl Viro mnt = alloc_vfsmnt(fc->source ?: "none"); 9639d412a43SAl Viro if (!mnt) 9649d412a43SAl Viro return ERR_PTR(-ENOMEM); 9659d412a43SAl Viro 9668f291889SAl Viro if (fc->sb_flags & SB_KERNMOUNT) 967b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9689d412a43SAl Viro 9698f291889SAl Viro atomic_inc(&fc->root->d_sb->s_active); 9708f291889SAl Viro mnt->mnt.mnt_sb = fc->root->d_sb; 9718f291889SAl Viro mnt->mnt.mnt_root = dget(fc->root); 972a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9730714a533SAl Viro mnt->mnt_parent = mnt; 9748f291889SAl Viro 975719ea2fbSAl Viro lock_mount_hash(); 9768f291889SAl Viro list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts); 977719ea2fbSAl Viro unlock_mount_hash(); 978b105e270SAl Viro return &mnt->mnt; 9799d412a43SAl Viro } 9808f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount); 9818f291889SAl Viro 9828f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc) 9838f291889SAl Viro { 9848f291889SAl Viro int err = vfs_get_tree(fc); 9858f291889SAl Viro if (!err) { 9868f291889SAl Viro up_write(&fc->root->d_sb->s_umount); 9878f291889SAl Viro return vfs_create_mount(fc); 9888f291889SAl Viro } 9898f291889SAl Viro return ERR_PTR(err); 9908f291889SAl Viro } 9918f291889SAl Viro EXPORT_SYMBOL(fc_mount); 9928f291889SAl Viro 9939bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type, 9949bc61ab1SDavid Howells int flags, const char *name, 9959bc61ab1SDavid Howells void *data) 9961da177e4SLinus Torvalds { 9979bc61ab1SDavid Howells struct fs_context *fc; 9988f291889SAl Viro struct vfsmount *mnt; 9999bc61ab1SDavid Howells int ret = 0; 10009d412a43SAl Viro 10019d412a43SAl Viro if (!type) 10023e1aeb00SDavid Howells return ERR_PTR(-EINVAL); 10039d412a43SAl Viro 10049bc61ab1SDavid Howells fc = fs_context_for_mount(type, flags); 10059bc61ab1SDavid Howells if (IS_ERR(fc)) 10069bc61ab1SDavid Howells return ERR_CAST(fc); 10079bc61ab1SDavid Howells 10083e1aeb00SDavid Howells if (name) 10093e1aeb00SDavid Howells ret = vfs_parse_fs_string(fc, "source", 10103e1aeb00SDavid Howells name, strlen(name)); 10119bc61ab1SDavid Howells if (!ret) 10129bc61ab1SDavid Howells ret = parse_monolithic_mount_data(fc, data); 10139bc61ab1SDavid Howells if (!ret) 10148f291889SAl Viro mnt = fc_mount(fc); 10158f291889SAl Viro else 10168f291889SAl Viro mnt = ERR_PTR(ret); 10179d412a43SAl Viro 10189bc61ab1SDavid Howells put_fs_context(fc); 10198f291889SAl Viro return mnt; 10209d412a43SAl Viro } 10219d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 10229d412a43SAl Viro 102393faccbbSEric W. Biederman struct vfsmount * 102493faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 102593faccbbSEric W. Biederman const char *name, void *data) 102693faccbbSEric W. Biederman { 102793faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 102893faccbbSEric W. Biederman * through from the parent mount to the submount don't support 102993faccbbSEric W. Biederman * unprivileged mounts with submounts. 103093faccbbSEric W. Biederman */ 103193faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 103293faccbbSEric W. Biederman return ERR_PTR(-EPERM); 103393faccbbSEric W. Biederman 1034e462ec50SDavid Howells return vfs_kern_mount(type, SB_SUBMOUNT, name, data); 103593faccbbSEric W. Biederman } 103693faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 103793faccbbSEric W. Biederman 103887129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 103936341f64SRam Pai int flag) 10401da177e4SLinus Torvalds { 104187129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 1042be34d1a3SDavid Howells struct mount *mnt; 1043be34d1a3SDavid Howells int err; 10441da177e4SLinus Torvalds 1045be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1046be34d1a3SDavid Howells if (!mnt) 1047be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1048be34d1a3SDavid Howells 10497a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 105015169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1051719f5d7fSMiklos Szeredi else 105215169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1053719f5d7fSMiklos Szeredi 105415169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1055be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1056719f5d7fSMiklos Szeredi if (err) 1057719f5d7fSMiklos Szeredi goto out_free; 1058719f5d7fSMiklos Szeredi } 1059719f5d7fSMiklos Szeredi 106016a34adbSAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags; 106116a34adbSAl Viro mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL); 10625ff9d8a6SEric W. Biederman 10631da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1064a6435940SChristian Brauner mnt->mnt.mnt_userns = mnt_user_ns(&old->mnt); 1065a6435940SChristian Brauner if (mnt->mnt.mnt_userns != &init_user_ns) 1066a6435940SChristian Brauner mnt->mnt.mnt_userns = get_user_ns(mnt->mnt.mnt_userns); 1067b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1068b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1069a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10700714a533SAl Viro mnt->mnt_parent = mnt; 1071719ea2fbSAl Viro lock_mount_hash(); 107239f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1073719ea2fbSAl Viro unlock_mount_hash(); 1074b90fa9aeSRam Pai 10757a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 10767a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 10776776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 107832301920SAl Viro mnt->mnt_master = old; 1079fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 10808aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1081fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 10826776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1083d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 10846776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1085d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 10865235d448SAl Viro } else { 10875235d448SAl Viro CLEAR_MNT_SHARED(mnt); 10885afe0022SRam Pai } 1089b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 10900f0afb1dSAl Viro set_mnt_shared(mnt); 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 10931da177e4SLinus Torvalds * as the original if that was on one */ 109436341f64SRam Pai if (flag & CL_EXPIRE) { 10956776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 10966776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 10971da177e4SLinus Torvalds } 1098be34d1a3SDavid Howells 1099cb338d06SAl Viro return mnt; 1100719f5d7fSMiklos Szeredi 1101719f5d7fSMiklos Szeredi out_free: 11028ffcb32eSDavid Howells mnt_free_id(mnt); 1103719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1104be34d1a3SDavid Howells return ERR_PTR(err); 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 11079ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 11089ea459e1SAl Viro { 110956cbb429SAl Viro struct hlist_node *p; 111056cbb429SAl Viro struct mount *m; 11119ea459e1SAl Viro /* 111256cbb429SAl Viro * The warning here probably indicates that somebody messed 111356cbb429SAl Viro * up a mnt_want/drop_write() pair. If this happens, the 111456cbb429SAl Viro * filesystem was probably unable to make r/w->r/o transitions. 11159ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 11169ea459e1SAl Viro * so mnt_get_writers() below is safe. 11179ea459e1SAl Viro */ 11189ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 11199ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 11209ea459e1SAl Viro mnt_pin_kill(mnt); 112156cbb429SAl Viro hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) { 112256cbb429SAl Viro hlist_del(&m->mnt_umount); 112356cbb429SAl Viro mntput(&m->mnt); 112456cbb429SAl Viro } 11259ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 11269ea459e1SAl Viro dput(mnt->mnt.mnt_root); 11279ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 11289ea459e1SAl Viro mnt_free_id(mnt); 11299ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11309ea459e1SAl Viro } 11319ea459e1SAl Viro 11329ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11339ea459e1SAl Viro { 11349ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11359ea459e1SAl Viro } 11369ea459e1SAl Viro 11379ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11389ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11399ea459e1SAl Viro { 11409ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 114129785735SByungchul Park struct mount *m, *t; 11429ea459e1SAl Viro 114329785735SByungchul Park llist_for_each_entry_safe(m, t, node, mnt_llist) 114429785735SByungchul Park cleanup_mnt(m); 11459ea459e1SAl Viro } 11469ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 11479ea459e1SAl Viro 1148900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 11497b7b1aceSAl Viro { 11504edbe133SAl Viro LIST_HEAD(list); 1151edf7ddbfSEric Biggers int count; 11524edbe133SAl Viro 115348a066e7SAl Viro rcu_read_lock(); 11549ea0a46cSAl Viro if (likely(READ_ONCE(mnt->mnt_ns))) { 11559ea0a46cSAl Viro /* 11569ea0a46cSAl Viro * Since we don't do lock_mount_hash() here, 11579ea0a46cSAl Viro * ->mnt_ns can change under us. However, if it's 11589ea0a46cSAl Viro * non-NULL, then there's a reference that won't 11599ea0a46cSAl Viro * be dropped until after an RCU delay done after 11609ea0a46cSAl Viro * turning ->mnt_ns NULL. So if we observe it 11619ea0a46cSAl Viro * non-NULL under rcu_read_lock(), the reference 11629ea0a46cSAl Viro * we are dropping is not the final one. 11639ea0a46cSAl Viro */ 1164aa9c0e07SAl Viro mnt_add_count(mnt, -1); 116548a066e7SAl Viro rcu_read_unlock(); 116699b7db7bSNick Piggin return; 1167b3e19d92SNick Piggin } 1168719ea2fbSAl Viro lock_mount_hash(); 1169119e1ef8SAl Viro /* 1170119e1ef8SAl Viro * make sure that if __legitimize_mnt() has not seen us grab 1171119e1ef8SAl Viro * mount_lock, we'll see their refcount increment here. 1172119e1ef8SAl Viro */ 1173119e1ef8SAl Viro smp_mb(); 11749ea0a46cSAl Viro mnt_add_count(mnt, -1); 1175edf7ddbfSEric Biggers count = mnt_get_count(mnt); 1176edf7ddbfSEric Biggers if (count != 0) { 1177edf7ddbfSEric Biggers WARN_ON(count < 0); 117848a066e7SAl Viro rcu_read_unlock(); 1179719ea2fbSAl Viro unlock_mount_hash(); 118099b7db7bSNick Piggin return; 118199b7db7bSNick Piggin } 118248a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 118348a066e7SAl Viro rcu_read_unlock(); 118448a066e7SAl Viro unlock_mount_hash(); 118548a066e7SAl Viro return; 118648a066e7SAl Viro } 118748a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 118848a066e7SAl Viro rcu_read_unlock(); 1189962830dfSAndi Kleen 119039f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1191ce07d891SEric W. Biederman 1192ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1193ce07d891SEric W. Biederman struct mount *p, *tmp; 1194ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 11954edbe133SAl Viro __put_mountpoint(unhash_mnt(p), &list); 119656cbb429SAl Viro hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children); 1197ce07d891SEric W. Biederman } 1198ce07d891SEric W. Biederman } 1199719ea2fbSAl Viro unlock_mount_hash(); 12004edbe133SAl Viro shrink_dentry_list(&list); 1201649a795aSAl Viro 12029ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 12039ea459e1SAl Viro struct task_struct *task = current; 12049ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 12059ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 120691989c70SJens Axboe if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME)) 12079ea459e1SAl Viro return; 12089ea459e1SAl Viro } 12099ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 12109ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 12119ea459e1SAl Viro return; 12129ea459e1SAl Viro } 12139ea459e1SAl Viro cleanup_mnt(mnt); 1214b3e19d92SNick Piggin } 1215b3e19d92SNick Piggin 1216b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1217b3e19d92SNick Piggin { 1218b3e19d92SNick Piggin if (mnt) { 1219863d684fSAl Viro struct mount *m = real_mount(mnt); 1220b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1221863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1222863d684fSAl Viro m->mnt_expiry_mark = 0; 1223863d684fSAl Viro mntput_no_expire(m); 1224b3e19d92SNick Piggin } 1225b3e19d92SNick Piggin } 1226b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1227b3e19d92SNick Piggin 1228b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1229b3e19d92SNick Piggin { 1230b3e19d92SNick Piggin if (mnt) 123183adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1232b3e19d92SNick Piggin return mnt; 1233b3e19d92SNick Piggin } 1234b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1235b3e19d92SNick Piggin 1236c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current 1237c6609c0aSIan Kent * namespace. 1238c6609c0aSIan Kent * 1239c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1240c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1241c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1242c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1243c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1244c6609c0aSIan Kent * alone. 1245c6609c0aSIan Kent */ 1246c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1247c6609c0aSIan Kent { 1248c6609c0aSIan Kent unsigned seq; 1249c6609c0aSIan Kent bool res; 1250c6609c0aSIan Kent 1251c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1252c6609c0aSIan Kent return false; 1253c6609c0aSIan Kent 1254c6609c0aSIan Kent rcu_read_lock(); 1255c6609c0aSIan Kent do { 1256c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1257c6609c0aSIan Kent res = __path_is_mountpoint(path); 1258c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1259c6609c0aSIan Kent rcu_read_unlock(); 1260c6609c0aSIan Kent 1261c6609c0aSIan Kent return res; 1262c6609c0aSIan Kent } 1263c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1264c6609c0aSIan Kent 1265ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 12667b7b1aceSAl Viro { 12673064c356SAl Viro struct mount *p; 12683064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 12693064c356SAl Viro if (IS_ERR(p)) 12703064c356SAl Viro return ERR_CAST(p); 12713064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 12723064c356SAl Viro return &p->mnt; 12737b7b1aceSAl Viro } 12741da177e4SLinus Torvalds 1275a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 12769f6c61f9SMiklos Szeredi static struct mount *mnt_list_next(struct mnt_namespace *ns, 12779f6c61f9SMiklos Szeredi struct list_head *p) 12789f6c61f9SMiklos Szeredi { 12799f6c61f9SMiklos Szeredi struct mount *mnt, *ret = NULL; 12809f6c61f9SMiklos Szeredi 12819f6c61f9SMiklos Szeredi lock_ns_list(ns); 12829f6c61f9SMiklos Szeredi list_for_each_continue(p, &ns->list) { 12839f6c61f9SMiklos Szeredi mnt = list_entry(p, typeof(*mnt), mnt_list); 12849f6c61f9SMiklos Szeredi if (!mnt_is_cursor(mnt)) { 12859f6c61f9SMiklos Szeredi ret = mnt; 12869f6c61f9SMiklos Szeredi break; 12879f6c61f9SMiklos Szeredi } 12889f6c61f9SMiklos Szeredi } 12899f6c61f9SMiklos Szeredi unlock_ns_list(ns); 12909f6c61f9SMiklos Szeredi 12919f6c61f9SMiklos Szeredi return ret; 12929f6c61f9SMiklos Szeredi } 12939f6c61f9SMiklos Szeredi 12940226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 12951da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 12961da177e4SLinus Torvalds { 1297ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12989f6c61f9SMiklos Szeredi struct list_head *prev; 12991da177e4SLinus Torvalds 1300390c6843SRam Pai down_read(&namespace_sem); 13019f6c61f9SMiklos Szeredi if (!*pos) { 13029f6c61f9SMiklos Szeredi prev = &p->ns->list; 13039f6c61f9SMiklos Szeredi } else { 13049f6c61f9SMiklos Szeredi prev = &p->cursor.mnt_list; 13059f6c61f9SMiklos Szeredi 13069f6c61f9SMiklos Szeredi /* Read after we'd reached the end? */ 13079f6c61f9SMiklos Szeredi if (list_empty(prev)) 13089f6c61f9SMiklos Szeredi return NULL; 1309c7999c36SAl Viro } 1310c7999c36SAl Viro 13119f6c61f9SMiklos Szeredi return mnt_list_next(p->ns, prev); 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 13151da177e4SLinus Torvalds { 1316ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13179f6c61f9SMiklos Szeredi struct mount *mnt = v; 1318b0765fb8SPavel Emelianov 13199f6c61f9SMiklos Szeredi ++*pos; 13209f6c61f9SMiklos Szeredi return mnt_list_next(p->ns, &mnt->mnt_list); 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 13241da177e4SLinus Torvalds { 13259f6c61f9SMiklos Szeredi struct proc_mounts *p = m->private; 13269f6c61f9SMiklos Szeredi struct mount *mnt = v; 13279f6c61f9SMiklos Szeredi 13289f6c61f9SMiklos Szeredi lock_ns_list(p->ns); 13299f6c61f9SMiklos Szeredi if (mnt) 13309f6c61f9SMiklos Szeredi list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list); 13319f6c61f9SMiklos Szeredi else 13329f6c61f9SMiklos Szeredi list_del_init(&p->cursor.mnt_list); 13339f6c61f9SMiklos Szeredi unlock_ns_list(p->ns); 1334390c6843SRam Pai up_read(&namespace_sem); 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 13370226f492SAl Viro static int m_show(struct seq_file *m, void *v) 13389f5596afSAl Viro { 1339ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13409f6c61f9SMiklos Szeredi struct mount *r = v; 13410226f492SAl Viro return p->show(m, &r->mnt); 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 1344a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 13451da177e4SLinus Torvalds .start = m_start, 13461da177e4SLinus Torvalds .next = m_next, 13471da177e4SLinus Torvalds .stop = m_stop, 13480226f492SAl Viro .show = m_show, 1349b4629fe2SChuck Lever }; 13509f6c61f9SMiklos Szeredi 13519f6c61f9SMiklos Szeredi void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor) 13529f6c61f9SMiklos Szeredi { 13539f6c61f9SMiklos Szeredi down_read(&namespace_sem); 13549f6c61f9SMiklos Szeredi lock_ns_list(ns); 13559f6c61f9SMiklos Szeredi list_del(&cursor->mnt_list); 13569f6c61f9SMiklos Szeredi unlock_ns_list(ns); 13579f6c61f9SMiklos Szeredi up_read(&namespace_sem); 13589f6c61f9SMiklos Szeredi } 1359a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1360b4629fe2SChuck Lever 13611da177e4SLinus Torvalds /** 13621da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 13631da177e4SLinus Torvalds * @mnt: root of mount tree 13641da177e4SLinus Torvalds * 13651da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 13661da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 13671da177e4SLinus Torvalds * busy. 13681da177e4SLinus Torvalds */ 1369909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13701da177e4SLinus Torvalds { 1371909b0a88SAl Viro struct mount *mnt = real_mount(m); 137236341f64SRam Pai int actual_refs = 0; 137336341f64SRam Pai int minimum_refs = 0; 1374315fc83eSAl Viro struct mount *p; 1375909b0a88SAl Viro BUG_ON(!m); 13761da177e4SLinus Torvalds 1377b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1378719ea2fbSAl Viro lock_mount_hash(); 1379909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 138083adc753SAl Viro actual_refs += mnt_get_count(p); 13811da177e4SLinus Torvalds minimum_refs += 2; 13821da177e4SLinus Torvalds } 1383719ea2fbSAl Viro unlock_mount_hash(); 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds if (actual_refs > minimum_refs) 13861da177e4SLinus Torvalds return 0; 1387e3474a8eSIan Kent 1388e3474a8eSIan Kent return 1; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds /** 13941da177e4SLinus Torvalds * may_umount - check if a mount point is busy 13951da177e4SLinus Torvalds * @mnt: root of mount 13961da177e4SLinus Torvalds * 13971da177e4SLinus Torvalds * This is called to check if a mount point has any 13981da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 13991da177e4SLinus Torvalds * mount has sub mounts this will return busy 14001da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 14011da177e4SLinus Torvalds * 14021da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 14031da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 14041da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 14051da177e4SLinus Torvalds */ 14061da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 14071da177e4SLinus Torvalds { 1408e3474a8eSIan Kent int ret = 1; 14098ad08d8aSAl Viro down_read(&namespace_sem); 1410719ea2fbSAl Viro lock_mount_hash(); 14111ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1412e3474a8eSIan Kent ret = 0; 1413719ea2fbSAl Viro unlock_mount_hash(); 14148ad08d8aSAl Viro up_read(&namespace_sem); 1415a05964f3SRam Pai return ret; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 14191da177e4SLinus Torvalds 142097216be0SAl Viro static void namespace_unlock(void) 14211da177e4SLinus Torvalds { 1422a3b3c562SEric W. Biederman struct hlist_head head; 142356cbb429SAl Viro struct hlist_node *p; 142456cbb429SAl Viro struct mount *m; 14254edbe133SAl Viro LIST_HEAD(list); 142697216be0SAl Viro 1427a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 14284edbe133SAl Viro list_splice_init(&ex_mountpoints, &list); 1429a3b3c562SEric W. Biederman 143097216be0SAl Viro up_write(&namespace_sem); 1431a3b3c562SEric W. Biederman 14324edbe133SAl Viro shrink_dentry_list(&list); 14334edbe133SAl Viro 1434a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 143597216be0SAl Viro return; 143697216be0SAl Viro 143722cb7405SNeilBrown synchronize_rcu_expedited(); 143848a066e7SAl Viro 143956cbb429SAl Viro hlist_for_each_entry_safe(m, p, &head, mnt_umount) { 144056cbb429SAl Viro hlist_del(&m->mnt_umount); 144156cbb429SAl Viro mntput(&m->mnt); 144256cbb429SAl Viro } 144370fbcdf4SRam Pai } 144470fbcdf4SRam Pai 144597216be0SAl Viro static inline void namespace_lock(void) 1446e3197d83SAl Viro { 144797216be0SAl Viro down_write(&namespace_sem); 1448e3197d83SAl Viro } 1449e3197d83SAl Viro 1450e819f152SEric W. Biederman enum umount_tree_flags { 1451e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1452e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1453e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1454e819f152SEric W. Biederman }; 1455f2d0a123SEric W. Biederman 1456f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1457f2d0a123SEric W. Biederman { 1458f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1459f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1460f2d0a123SEric W. Biederman return true; 1461f2d0a123SEric W. Biederman 1462f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1463f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1464f2d0a123SEric W. Biederman return true; 1465f2d0a123SEric W. Biederman 1466f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1467f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1468f2d0a123SEric W. Biederman * connected to mounted mounts. 1469f2d0a123SEric W. Biederman */ 1470f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1471f2d0a123SEric W. Biederman return true; 1472f2d0a123SEric W. Biederman 1473f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1474f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1475f2d0a123SEric W. Biederman return false; 1476f2d0a123SEric W. Biederman 1477f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1478f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1479f2d0a123SEric W. Biederman return false; 1480f2d0a123SEric W. Biederman 1481f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1482f2d0a123SEric W. Biederman return true; 1483f2d0a123SEric W. Biederman } 1484f2d0a123SEric W. Biederman 148599b7db7bSNick Piggin /* 148648a066e7SAl Viro * mount_lock must be held 148799b7db7bSNick Piggin * namespace_sem must be held for write 148899b7db7bSNick Piggin */ 1489e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 149070fbcdf4SRam Pai { 1491c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1492315fc83eSAl Viro struct mount *p; 149370fbcdf4SRam Pai 14945d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 14955d88457eSEric W. Biederman propagate_mount_unlock(mnt); 14965d88457eSEric W. Biederman 1497c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1498590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1499590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1500c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1501590ce4bcSEric W. Biederman } 1502c003b26fSEric W. Biederman 1503411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1504c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1505c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 150638129a13SAl Viro } 150770fbcdf4SRam Pai 1508c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1509e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 15107b8a53fdSAl Viro propagate_umount(&tmp_list); 1511a05964f3SRam Pai 1512c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1513d2921684SEric W. Biederman struct mnt_namespace *ns; 1514ce07d891SEric W. Biederman bool disconnect; 1515c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 15166776db3dSAl Viro list_del_init(&p->mnt_expire); 15171a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1518d2921684SEric W. Biederman ns = p->mnt_ns; 1519d2921684SEric W. Biederman if (ns) { 1520d2921684SEric W. Biederman ns->mounts--; 1521d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1522d2921684SEric W. Biederman } 152363d37a84SAl Viro p->mnt_ns = NULL; 1524e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 152548a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 152687b95ce0SAl Viro 1527f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1528676da58dSAl Viro if (mnt_has_parent(p)) { 152981b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1530ce07d891SEric W. Biederman if (!disconnect) { 1531ce07d891SEric W. Biederman /* Don't forget about p */ 1532ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1533ce07d891SEric W. Biederman } else { 15346a46c573SEric W. Biederman umount_mnt(p); 15357c4b93d8SAl Viro } 1536ce07d891SEric W. Biederman } 15370f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 153819a1c409SAl Viro if (disconnect) 153919a1c409SAl Viro hlist_add_head(&p->mnt_umount, &unmounted); 154038129a13SAl Viro } 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 1543b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1544c35038beSAl Viro 15458d0347f6SDavid Howells static int do_umount_root(struct super_block *sb) 15468d0347f6SDavid Howells { 15478d0347f6SDavid Howells int ret = 0; 15488d0347f6SDavid Howells 15498d0347f6SDavid Howells down_write(&sb->s_umount); 15508d0347f6SDavid Howells if (!sb_rdonly(sb)) { 15518d0347f6SDavid Howells struct fs_context *fc; 15528d0347f6SDavid Howells 15538d0347f6SDavid Howells fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY, 15548d0347f6SDavid Howells SB_RDONLY); 15558d0347f6SDavid Howells if (IS_ERR(fc)) { 15568d0347f6SDavid Howells ret = PTR_ERR(fc); 15578d0347f6SDavid Howells } else { 15588d0347f6SDavid Howells ret = parse_monolithic_mount_data(fc, NULL); 15598d0347f6SDavid Howells if (!ret) 15608d0347f6SDavid Howells ret = reconfigure_super(fc); 15618d0347f6SDavid Howells put_fs_context(fc); 15628d0347f6SDavid Howells } 15638d0347f6SDavid Howells } 15648d0347f6SDavid Howells up_write(&sb->s_umount); 15658d0347f6SDavid Howells return ret; 15668d0347f6SDavid Howells } 15678d0347f6SDavid Howells 15681ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 15691da177e4SLinus Torvalds { 15701ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 15711da177e4SLinus Torvalds int retval; 15721da177e4SLinus Torvalds 15731ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 15741da177e4SLinus Torvalds if (retval) 15751da177e4SLinus Torvalds return retval; 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds /* 15781da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 15791da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 15801da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 15811da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 15821da177e4SLinus Torvalds */ 15831da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 15841ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 15851da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 15861da177e4SLinus Torvalds return -EINVAL; 15871da177e4SLinus Torvalds 1588b3e19d92SNick Piggin /* 1589b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1590b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1591b3e19d92SNick Piggin */ 1592719ea2fbSAl Viro lock_mount_hash(); 159383adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1594719ea2fbSAl Viro unlock_mount_hash(); 15951da177e4SLinus Torvalds return -EBUSY; 1596b3e19d92SNick Piggin } 1597719ea2fbSAl Viro unlock_mount_hash(); 15981da177e4SLinus Torvalds 1599863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 16001da177e4SLinus Torvalds return -EAGAIN; 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds /* 16041da177e4SLinus Torvalds * If we may have to abort operations to get out of this 16051da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 16061da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 16071da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 16081da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 16091da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 16101da177e4SLinus Torvalds * about for the moment. 16111da177e4SLinus Torvalds */ 16121da177e4SLinus Torvalds 161342faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 161442faad99SAl Viro sb->s_op->umount_begin(sb); 161542faad99SAl Viro } 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds /* 16181da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 16191da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 16201da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 16211da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 16221da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 16231da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 16241da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 16251da177e4SLinus Torvalds */ 16261ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 16271da177e4SLinus Torvalds /* 16281da177e4SLinus Torvalds * Special case for "unmounting" root ... 16291da177e4SLinus Torvalds * we just try to remount it readonly. 16301da177e4SLinus Torvalds */ 1631bc6155d1SEric W. Biederman if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 1632a1480dccSAndy Lutomirski return -EPERM; 16338d0347f6SDavid Howells return do_umount_root(sb); 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds 163697216be0SAl Viro namespace_lock(); 1637719ea2fbSAl Viro lock_mount_hash(); 16381da177e4SLinus Torvalds 163925d202edSEric W. Biederman /* Recheck MNT_LOCKED with the locks held */ 164025d202edSEric W. Biederman retval = -EINVAL; 164125d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 164225d202edSEric W. Biederman goto out; 164325d202edSEric W. Biederman 164425d202edSEric W. Biederman event++; 164548a066e7SAl Viro if (flags & MNT_DETACH) { 164648a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1647e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 164848a066e7SAl Viro retval = 0; 164948a066e7SAl Viro } else { 1650b54b9be7SAl Viro shrink_submounts(mnt); 16511da177e4SLinus Torvalds retval = -EBUSY; 165248a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 16531a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1654e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 16551da177e4SLinus Torvalds retval = 0; 16561da177e4SLinus Torvalds } 165748a066e7SAl Viro } 165825d202edSEric W. Biederman out: 1659719ea2fbSAl Viro unlock_mount_hash(); 1660e3197d83SAl Viro namespace_unlock(); 16611da177e4SLinus Torvalds return retval; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds /* 166580b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 166680b5dce8SEric W. Biederman * 166780b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 166880b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 166980b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 167080b5dce8SEric W. Biederman * leaking them. 167180b5dce8SEric W. Biederman * 167280b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 167380b5dce8SEric W. Biederman */ 167480b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 167580b5dce8SEric W. Biederman { 167680b5dce8SEric W. Biederman struct mountpoint *mp; 167780b5dce8SEric W. Biederman struct mount *mnt; 167880b5dce8SEric W. Biederman 167980b5dce8SEric W. Biederman namespace_lock(); 16803895dbf8SEric W. Biederman lock_mount_hash(); 168180b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1682adc9b5c0SAl Viro if (!mp) 168380b5dce8SEric W. Biederman goto out_unlock; 168480b5dce8SEric W. Biederman 1685e06b933eSAndrey Ulanov event++; 168680b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 168780b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1688ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1689fe78fcc8SEric W. Biederman umount_mnt(mnt); 169056cbb429SAl Viro hlist_add_head(&mnt->mnt_umount, &unmounted); 1691ce07d891SEric W. Biederman } 1692e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 169380b5dce8SEric W. Biederman } 169480b5dce8SEric W. Biederman put_mountpoint(mp); 169580b5dce8SEric W. Biederman out_unlock: 16963895dbf8SEric W. Biederman unlock_mount_hash(); 169780b5dce8SEric W. Biederman namespace_unlock(); 169880b5dce8SEric W. Biederman } 169980b5dce8SEric W. Biederman 170080b5dce8SEric W. Biederman /* 17019b40bc90SAl Viro * Is the caller allowed to modify his namespace? 17029b40bc90SAl Viro */ 17039b40bc90SAl Viro static inline bool may_mount(void) 17049b40bc90SAl Viro { 17059b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 17069b40bc90SAl Viro } 17079b40bc90SAl Viro 1708df2474a2SJeff Layton #ifdef CONFIG_MANDATORY_FILE_LOCKING 17099e8925b6SJeff Layton static inline bool may_mandlock(void) 17109e8925b6SJeff Layton { 171195ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 17129e8925b6SJeff Layton } 1713df2474a2SJeff Layton #else 1714df2474a2SJeff Layton static inline bool may_mandlock(void) 1715df2474a2SJeff Layton { 1716df2474a2SJeff Layton pr_warn("VFS: \"mand\" mount option not supported"); 1717df2474a2SJeff Layton return false; 1718df2474a2SJeff Layton } 1719df2474a2SJeff Layton #endif 17209e8925b6SJeff Layton 172125ccd24fSChristoph Hellwig static int can_umount(const struct path *path, int flags) 17221da177e4SLinus Torvalds { 172325ccd24fSChristoph Hellwig struct mount *mnt = real_mount(path->mnt); 17241da177e4SLinus Torvalds 17259b40bc90SAl Viro if (!may_mount()) 17269b40bc90SAl Viro return -EPERM; 172741525f56SChristoph Hellwig if (path->dentry != path->mnt->mnt_root) 172825ccd24fSChristoph Hellwig return -EINVAL; 1729143c8c91SAl Viro if (!check_mnt(mnt)) 173025ccd24fSChristoph Hellwig return -EINVAL; 173125d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */ 173225ccd24fSChristoph Hellwig return -EINVAL; 1733b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 173425ccd24fSChristoph Hellwig return -EPERM; 173525ccd24fSChristoph Hellwig return 0; 173625ccd24fSChristoph Hellwig } 17371da177e4SLinus Torvalds 1738a0a6df9aSAl Viro // caller is responsible for flags being sane 173925ccd24fSChristoph Hellwig int path_umount(struct path *path, int flags) 174025ccd24fSChristoph Hellwig { 174125ccd24fSChristoph Hellwig struct mount *mnt = real_mount(path->mnt); 174225ccd24fSChristoph Hellwig int ret; 174325ccd24fSChristoph Hellwig 174425ccd24fSChristoph Hellwig ret = can_umount(path, flags); 174525ccd24fSChristoph Hellwig if (!ret) 174625ccd24fSChristoph Hellwig ret = do_umount(mnt, flags); 174725ccd24fSChristoph Hellwig 1748429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 174941525f56SChristoph Hellwig dput(path->dentry); 1750900148dcSAl Viro mntput_no_expire(mnt); 175125ccd24fSChristoph Hellwig return ret; 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 175409267defSChristoph Hellwig static int ksys_umount(char __user *name, int flags) 175541525f56SChristoph Hellwig { 175641525f56SChristoph Hellwig int lookup_flags = LOOKUP_MOUNTPOINT; 175741525f56SChristoph Hellwig struct path path; 175841525f56SChristoph Hellwig int ret; 175941525f56SChristoph Hellwig 1760a0a6df9aSAl Viro // basic validity checks done first 1761a0a6df9aSAl Viro if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1762a0a6df9aSAl Viro return -EINVAL; 1763a0a6df9aSAl Viro 176441525f56SChristoph Hellwig if (!(flags & UMOUNT_NOFOLLOW)) 176541525f56SChristoph Hellwig lookup_flags |= LOOKUP_FOLLOW; 176641525f56SChristoph Hellwig ret = user_path_at(AT_FDCWD, name, lookup_flags, &path); 176741525f56SChristoph Hellwig if (ret) 176841525f56SChristoph Hellwig return ret; 176941525f56SChristoph Hellwig return path_umount(&path, flags); 177041525f56SChristoph Hellwig } 177141525f56SChristoph Hellwig 17723a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 17733a18ef5cSDominik Brodowski { 17743a18ef5cSDominik Brodowski return ksys_umount(name, flags); 17753a18ef5cSDominik Brodowski } 17763a18ef5cSDominik Brodowski 17771da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds /* 17801da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 17811da177e4SLinus Torvalds */ 1782bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 17831da177e4SLinus Torvalds { 17843a18ef5cSDominik Brodowski return ksys_umount(name, 0); 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds #endif 17881da177e4SLinus Torvalds 17894ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 17908823c079SEric W. Biederman { 17914ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1792e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1793e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 17944ce5d2b1SEric W. Biederman } 17954ce5d2b1SEric W. Biederman 1796213921f9SEric Biggers static struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 179758be2825SAl Viro { 179858be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 179958be2825SAl Viro } 180058be2825SAl Viro 1801303cc571SChristian Brauner struct ns_common *from_mnt_ns(struct mnt_namespace *mnt) 1802303cc571SChristian Brauner { 1803303cc571SChristian Brauner return &mnt->ns; 1804303cc571SChristian Brauner } 1805303cc571SChristian Brauner 18064ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 18074ce5d2b1SEric W. Biederman { 18084ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 18094ce5d2b1SEric W. Biederman * mount namespace loop? 18104ce5d2b1SEric W. Biederman */ 18114ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 18124ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 18134ce5d2b1SEric W. Biederman return false; 18144ce5d2b1SEric W. Biederman 1815f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 18168823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 18178823c079SEric W. Biederman } 18188823c079SEric W. Biederman 181987129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 182036341f64SRam Pai int flag) 18211da177e4SLinus Torvalds { 182284d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 18231da177e4SLinus Torvalds 18244ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 18254ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 18264ce5d2b1SEric W. Biederman 18274ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1828be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 18299676f0c6SRam Pai 183036341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1831be34d1a3SDavid Howells if (IS_ERR(q)) 1832be34d1a3SDavid Howells return q; 1833be34d1a3SDavid Howells 1834a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds p = mnt; 18376b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1838315fc83eSAl Viro struct mount *s; 18397ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 18401da177e4SLinus Torvalds continue; 18411da177e4SLinus Torvalds 1842909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 18434ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 18444ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 1845df7342b2SEric W. Biederman if (s->mnt.mnt_flags & MNT_LOCKED) { 1846df7342b2SEric W. Biederman /* Both unbindable and locked. */ 1847df7342b2SEric W. Biederman q = ERR_PTR(-EPERM); 1848df7342b2SEric W. Biederman goto out; 1849df7342b2SEric W. Biederman } else { 18504ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 18514ce5d2b1SEric W. Biederman continue; 18524ce5d2b1SEric W. Biederman } 1853df7342b2SEric W. Biederman } 18544ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 18554ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 18569676f0c6SRam Pai s = skip_mnt_tree(s); 18579676f0c6SRam Pai continue; 18589676f0c6SRam Pai } 18590714a533SAl Viro while (p != s->mnt_parent) { 18600714a533SAl Viro p = p->mnt_parent; 18610714a533SAl Viro q = q->mnt_parent; 18621da177e4SLinus Torvalds } 186387129cc0SAl Viro p = s; 186484d17192SAl Viro parent = q; 186587129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1866be34d1a3SDavid Howells if (IS_ERR(q)) 1867be34d1a3SDavid Howells goto out; 1868719ea2fbSAl Viro lock_mount_hash(); 18691a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 18701064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1871719ea2fbSAl Viro unlock_mount_hash(); 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds return res; 1875be34d1a3SDavid Howells out: 18761da177e4SLinus Torvalds if (res) { 1877719ea2fbSAl Viro lock_mount_hash(); 1878e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1879719ea2fbSAl Viro unlock_mount_hash(); 18801da177e4SLinus Torvalds } 1881be34d1a3SDavid Howells return q; 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds 1884be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1885be34d1a3SDavid Howells 1886ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 18878aec0809SAl Viro { 1888cb338d06SAl Viro struct mount *tree; 188997216be0SAl Viro namespace_lock(); 1890cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1891cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1892cd4a4017SEric W. Biederman else 189387129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 189487129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1895328e6d90SAl Viro namespace_unlock(); 1896be34d1a3SDavid Howells if (IS_ERR(tree)) 189752e220d3SDan Carpenter return ERR_CAST(tree); 1898be34d1a3SDavid Howells return &tree->mnt; 18998aec0809SAl Viro } 19008aec0809SAl Viro 1901a07b2000SAl Viro static void free_mnt_ns(struct mnt_namespace *); 1902a07b2000SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool); 1903a07b2000SAl Viro 1904a07b2000SAl Viro void dissolve_on_fput(struct vfsmount *mnt) 1905a07b2000SAl Viro { 1906a07b2000SAl Viro struct mnt_namespace *ns; 1907a07b2000SAl Viro namespace_lock(); 1908a07b2000SAl Viro lock_mount_hash(); 1909a07b2000SAl Viro ns = real_mount(mnt)->mnt_ns; 191044dfd84aSDavid Howells if (ns) { 191144dfd84aSDavid Howells if (is_anon_ns(ns)) 1912a07b2000SAl Viro umount_tree(real_mount(mnt), UMOUNT_CONNECTED); 191344dfd84aSDavid Howells else 191444dfd84aSDavid Howells ns = NULL; 191544dfd84aSDavid Howells } 1916a07b2000SAl Viro unlock_mount_hash(); 1917a07b2000SAl Viro namespace_unlock(); 191844dfd84aSDavid Howells if (ns) 1919a07b2000SAl Viro free_mnt_ns(ns); 1920a07b2000SAl Viro } 1921a07b2000SAl Viro 19228aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 19238aec0809SAl Viro { 192497216be0SAl Viro namespace_lock(); 1925719ea2fbSAl Viro lock_mount_hash(); 19269c8e0a1bSEric W. Biederman umount_tree(real_mount(mnt), 0); 1927719ea2fbSAl Viro unlock_mount_hash(); 19283ab6abeeSAl Viro namespace_unlock(); 19298aec0809SAl Viro } 19308aec0809SAl Viro 1931c771d683SMiklos Szeredi /** 1932c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 1933c771d683SMiklos Szeredi * 1934c771d683SMiklos Szeredi * This creates a new vfsmount, which will be the clone of @path. The new will 1935c771d683SMiklos Szeredi * not be attached anywhere in the namespace and will be private (i.e. changes 1936c771d683SMiklos Szeredi * to the originating mount won't be propagated into this). 1937c771d683SMiklos Szeredi * 1938c771d683SMiklos Szeredi * Release with mntput(). 1939c771d683SMiklos Szeredi */ 1940ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1941c771d683SMiklos Szeredi { 1942c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1943c771d683SMiklos Szeredi struct mount *new_mnt; 1944c771d683SMiklos Szeredi 1945c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1946c771d683SMiklos Szeredi return ERR_PTR(-EINVAL); 1947c771d683SMiklos Szeredi 1948c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1949c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1950c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1951c771d683SMiklos Szeredi 1952df820f8dSMiklos Szeredi /* Longterm mount to be removed by kern_unmount*() */ 1953df820f8dSMiklos Szeredi new_mnt->mnt_ns = MNT_NS_INTERNAL; 1954df820f8dSMiklos Szeredi 1955c771d683SMiklos Szeredi return &new_mnt->mnt; 1956c771d683SMiklos Szeredi } 1957c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 1958c771d683SMiklos Szeredi 19591f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 19601f707137SAl Viro struct vfsmount *root) 19611f707137SAl Viro { 19621a4eeaf2SAl Viro struct mount *mnt; 19631f707137SAl Viro int res = f(root, arg); 19641f707137SAl Viro if (res) 19651f707137SAl Viro return res; 19661a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 19671a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 19681f707137SAl Viro if (res) 19691f707137SAl Viro return res; 19701f707137SAl Viro } 19711f707137SAl Viro return 0; 19721f707137SAl Viro } 19731f707137SAl Viro 19743bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt) 19753bd045ccSAl Viro { 19763bd045ccSAl Viro struct mount *p; 19773bd045ccSAl Viro 19783bd045ccSAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 19793bd045ccSAl Viro int flags = p->mnt.mnt_flags; 19803bd045ccSAl Viro /* Don't allow unprivileged users to change mount flags */ 19813bd045ccSAl Viro flags |= MNT_LOCK_ATIME; 19823bd045ccSAl Viro 19833bd045ccSAl Viro if (flags & MNT_READONLY) 19843bd045ccSAl Viro flags |= MNT_LOCK_READONLY; 19853bd045ccSAl Viro 19863bd045ccSAl Viro if (flags & MNT_NODEV) 19873bd045ccSAl Viro flags |= MNT_LOCK_NODEV; 19883bd045ccSAl Viro 19893bd045ccSAl Viro if (flags & MNT_NOSUID) 19903bd045ccSAl Viro flags |= MNT_LOCK_NOSUID; 19913bd045ccSAl Viro 19923bd045ccSAl Viro if (flags & MNT_NOEXEC) 19933bd045ccSAl Viro flags |= MNT_LOCK_NOEXEC; 19943bd045ccSAl Viro /* Don't allow unprivileged users to reveal what is under a mount */ 19953bd045ccSAl Viro if (list_empty(&p->mnt_expire)) 19963bd045ccSAl Viro flags |= MNT_LOCKED; 19973bd045ccSAl Viro p->mnt.mnt_flags = flags; 19983bd045ccSAl Viro } 19993bd045ccSAl Viro } 20003bd045ccSAl Viro 20014b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 2002719f5d7fSMiklos Szeredi { 2003315fc83eSAl Viro struct mount *p; 2004719f5d7fSMiklos Szeredi 2005909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 2006fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 20074b8b21f4SAl Viro mnt_release_group_id(p); 2008719f5d7fSMiklos Szeredi } 2009719f5d7fSMiklos Szeredi } 2010719f5d7fSMiklos Szeredi 20114b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 2012719f5d7fSMiklos Szeredi { 2013315fc83eSAl Viro struct mount *p; 2014719f5d7fSMiklos Szeredi 2015909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 2016fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 20174b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 2018719f5d7fSMiklos Szeredi if (err) { 20194b8b21f4SAl Viro cleanup_group_ids(mnt, p); 2020719f5d7fSMiklos Szeredi return err; 2021719f5d7fSMiklos Szeredi } 2022719f5d7fSMiklos Szeredi } 2023719f5d7fSMiklos Szeredi } 2024719f5d7fSMiklos Szeredi 2025719f5d7fSMiklos Szeredi return 0; 2026719f5d7fSMiklos Szeredi } 2027719f5d7fSMiklos Szeredi 2028d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 2029d2921684SEric W. Biederman { 2030d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 2031d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 2032d2921684SEric W. Biederman struct mount *p; 2033d2921684SEric W. Biederman 2034d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 2035d2921684SEric W. Biederman mounts++; 2036d2921684SEric W. Biederman 2037d2921684SEric W. Biederman old = ns->mounts; 2038d2921684SEric W. Biederman pending = ns->pending_mounts; 2039d2921684SEric W. Biederman sum = old + pending; 2040d2921684SEric W. Biederman if ((old > sum) || 2041d2921684SEric W. Biederman (pending > sum) || 2042d2921684SEric W. Biederman (max < sum) || 2043d2921684SEric W. Biederman (mounts > (max - sum))) 2044d2921684SEric W. Biederman return -ENOSPC; 2045d2921684SEric W. Biederman 2046d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 2047d2921684SEric W. Biederman return 0; 2048d2921684SEric W. Biederman } 2049d2921684SEric W. Biederman 2050b90fa9aeSRam Pai /* 2051b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 2052b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 205321444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 205421444403SRam Pai * store the parent mount and mountpoint dentry. 205521444403SRam Pai * (done when source_mnt is moved) 2056b90fa9aeSRam Pai * 2057b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 2058b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 20599676f0c6SRam Pai * --------------------------------------------------------------------------- 2060b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 20619676f0c6SRam Pai * |************************************************************************** 20629676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 20639676f0c6SRam Pai * | dest | | | | | 20649676f0c6SRam Pai * | | | | | | | 20659676f0c6SRam Pai * | v | | | | | 20669676f0c6SRam Pai * |************************************************************************** 20679676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 20685afe0022SRam Pai * | | | | | | 20699676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 20709676f0c6SRam Pai * *************************************************************************** 2071b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 2072b90fa9aeSRam Pai * destination mount. 2073b90fa9aeSRam Pai * 2074b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 2075b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 2076b90fa9aeSRam Pai * the peer group of the source mount. 2077b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 2078b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 2079b90fa9aeSRam Pai * mount. 20805afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 20815afe0022SRam Pai * of the destination mount and the cloned mount is made slave 20825afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 20835afe0022SRam Pai * is marked as 'shared and slave'. 20845afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 20855afe0022SRam Pai * source mount. 20865afe0022SRam Pai * 20879676f0c6SRam Pai * --------------------------------------------------------------------------- 208821444403SRam Pai * | MOVE MOUNT OPERATION | 20899676f0c6SRam Pai * |************************************************************************** 20909676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 20919676f0c6SRam Pai * | dest | | | | | 20929676f0c6SRam Pai * | | | | | | | 20939676f0c6SRam Pai * | v | | | | | 20949676f0c6SRam Pai * |************************************************************************** 20959676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 20965afe0022SRam Pai * | | | | | | 20979676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 20989676f0c6SRam Pai * *************************************************************************** 20995afe0022SRam Pai * 21005afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 21015afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 210221444403SRam Pai * (+*) the mount is moved to the destination. 21035afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 21045afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 21055afe0022SRam Pai * the mount is marked as 'shared and slave'. 21065afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 2107b90fa9aeSRam Pai * 2108b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 2109b90fa9aeSRam Pai * applied to each mount in the tree. 2110b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 2111b90fa9aeSRam Pai * in allocations. 2112b90fa9aeSRam Pai */ 21130fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 211484d17192SAl Viro struct mount *dest_mnt, 211584d17192SAl Viro struct mountpoint *dest_mp, 21162763d119SAl Viro bool moving) 2117b90fa9aeSRam Pai { 21183bd045ccSAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 211938129a13SAl Viro HLIST_HEAD(tree_list); 2120d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 21211064f874SEric W. Biederman struct mountpoint *smp; 2122315fc83eSAl Viro struct mount *child, *p; 212338129a13SAl Viro struct hlist_node *n; 2124719f5d7fSMiklos Szeredi int err; 2125b90fa9aeSRam Pai 21261064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 21271064f874SEric W. Biederman * to be tucked under other mounts. 21281064f874SEric W. Biederman */ 21291064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 21301064f874SEric W. Biederman if (IS_ERR(smp)) 21311064f874SEric W. Biederman return PTR_ERR(smp); 21321064f874SEric W. Biederman 2133d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 21342763d119SAl Viro if (!moving) { 2135d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 2136d2921684SEric W. Biederman if (err) 2137d2921684SEric W. Biederman goto out; 2138d2921684SEric W. Biederman } 2139d2921684SEric W. Biederman 2140fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 21410fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 2142719f5d7fSMiklos Szeredi if (err) 2143719f5d7fSMiklos Szeredi goto out; 214484d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 2145f2ebb3a9SAl Viro lock_mount_hash(); 2146719f5d7fSMiklos Szeredi if (err) 2147719f5d7fSMiklos Szeredi goto out_cleanup_ids; 2148909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 21490f0afb1dSAl Viro set_mnt_shared(p); 21500b1b901bSAl Viro } else { 21510b1b901bSAl Viro lock_mount_hash(); 2152b90fa9aeSRam Pai } 21532763d119SAl Viro if (moving) { 21542763d119SAl Viro unhash_mnt(source_mnt); 215584d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 2156143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 215721444403SRam Pai } else { 215844dfd84aSDavid Howells if (source_mnt->mnt_ns) { 215944dfd84aSDavid Howells /* move from anon - the caller will destroy */ 216044dfd84aSDavid Howells list_del_init(&source_mnt->mnt_ns->list); 216144dfd84aSDavid Howells } 216284d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 21631064f874SEric W. Biederman commit_tree(source_mnt); 216421444403SRam Pai } 2165b90fa9aeSRam Pai 216638129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 21671d6a32acSAl Viro struct mount *q; 216838129a13SAl Viro hlist_del_init(&child->mnt_hash); 21691064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 21701d6a32acSAl Viro child->mnt_mountpoint); 21711064f874SEric W. Biederman if (q) 21721064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 21733bd045ccSAl Viro /* Notice when we are propagating across user namespaces */ 21743bd045ccSAl Viro if (child->mnt_parent->mnt_ns->user_ns != user_ns) 21753bd045ccSAl Viro lock_mnt_tree(child); 2176d728cf79SChristian Brauner child->mnt.mnt_flags &= ~MNT_LOCKED; 21771064f874SEric W. Biederman commit_tree(child); 2178b90fa9aeSRam Pai } 21791064f874SEric W. Biederman put_mountpoint(smp); 2180719ea2fbSAl Viro unlock_mount_hash(); 218199b7db7bSNick Piggin 2182b90fa9aeSRam Pai return 0; 2183719f5d7fSMiklos Szeredi 2184719f5d7fSMiklos Szeredi out_cleanup_ids: 2185f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2186f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2187d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2188e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2189f2ebb3a9SAl Viro } 2190f2ebb3a9SAl Viro unlock_mount_hash(); 21910fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2192719f5d7fSMiklos Szeredi out: 2193d2921684SEric W. Biederman ns->pending_mounts = 0; 21941064f874SEric W. Biederman 21951064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 21961064f874SEric W. Biederman put_mountpoint(smp); 21971064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 21981064f874SEric W. Biederman 2199719f5d7fSMiklos Szeredi return err; 2200b90fa9aeSRam Pai } 2201b90fa9aeSRam Pai 220284d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2203b12cea91SAl Viro { 2204b12cea91SAl Viro struct vfsmount *mnt; 220584d17192SAl Viro struct dentry *dentry = path->dentry; 2206b12cea91SAl Viro retry: 22075955102cSAl Viro inode_lock(dentry->d_inode); 220884d17192SAl Viro if (unlikely(cant_mount(dentry))) { 22095955102cSAl Viro inode_unlock(dentry->d_inode); 221084d17192SAl Viro return ERR_PTR(-ENOENT); 2211b12cea91SAl Viro } 221297216be0SAl Viro namespace_lock(); 2213b12cea91SAl Viro mnt = lookup_mnt(path); 221484d17192SAl Viro if (likely(!mnt)) { 22153895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 221684d17192SAl Viro if (IS_ERR(mp)) { 221797216be0SAl Viro namespace_unlock(); 22185955102cSAl Viro inode_unlock(dentry->d_inode); 221984d17192SAl Viro return mp; 222084d17192SAl Viro } 222184d17192SAl Viro return mp; 222284d17192SAl Viro } 222397216be0SAl Viro namespace_unlock(); 22245955102cSAl Viro inode_unlock(path->dentry->d_inode); 2225b12cea91SAl Viro path_put(path); 2226b12cea91SAl Viro path->mnt = mnt; 222784d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2228b12cea91SAl Viro goto retry; 2229b12cea91SAl Viro } 2230b12cea91SAl Viro 223184d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2232b12cea91SAl Viro { 223384d17192SAl Viro struct dentry *dentry = where->m_dentry; 22343895dbf8SEric W. Biederman 22353895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 223684d17192SAl Viro put_mountpoint(where); 22373895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 22383895dbf8SEric W. Biederman 2239328e6d90SAl Viro namespace_unlock(); 22405955102cSAl Viro inode_unlock(dentry->d_inode); 2241b12cea91SAl Viro } 2242b12cea91SAl Viro 224384d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 22441da177e4SLinus Torvalds { 2245e462ec50SDavid Howells if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER) 22461da177e4SLinus Torvalds return -EINVAL; 22471da177e4SLinus Torvalds 2248e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2249e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 22501da177e4SLinus Torvalds return -ENOTDIR; 22511da177e4SLinus Torvalds 22522763d119SAl Viro return attach_recursive_mnt(mnt, p, mp, false); 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds /* 22567a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 22577a2e8a8fSValerie Aurora */ 22587a2e8a8fSValerie Aurora 2259e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags) 22607a2e8a8fSValerie Aurora { 2261e462ec50SDavid Howells int type = ms_flags & ~(MS_REC | MS_SILENT); 22627a2e8a8fSValerie Aurora 22637a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 22647a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 22657a2e8a8fSValerie Aurora return 0; 22667a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 22677a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 22687a2e8a8fSValerie Aurora return 0; 22697a2e8a8fSValerie Aurora return type; 22707a2e8a8fSValerie Aurora } 22717a2e8a8fSValerie Aurora 22727a2e8a8fSValerie Aurora /* 227307b20889SRam Pai * recursively change the type of the mountpoint. 227407b20889SRam Pai */ 2275e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags) 227607b20889SRam Pai { 2277315fc83eSAl Viro struct mount *m; 22784b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 2279e462ec50SDavid Howells int recurse = ms_flags & MS_REC; 22807a2e8a8fSValerie Aurora int type; 2281719f5d7fSMiklos Szeredi int err = 0; 228207b20889SRam Pai 22832d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 228407b20889SRam Pai return -EINVAL; 228507b20889SRam Pai 2286e462ec50SDavid Howells type = flags_to_propagation_type(ms_flags); 22877a2e8a8fSValerie Aurora if (!type) 22887a2e8a8fSValerie Aurora return -EINVAL; 22897a2e8a8fSValerie Aurora 229097216be0SAl Viro namespace_lock(); 2291719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2292719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2293719f5d7fSMiklos Szeredi if (err) 2294719f5d7fSMiklos Szeredi goto out_unlock; 2295719f5d7fSMiklos Szeredi } 2296719f5d7fSMiklos Szeredi 2297719ea2fbSAl Viro lock_mount_hash(); 2298909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 22990f0afb1dSAl Viro change_mnt_propagation(m, type); 2300719ea2fbSAl Viro unlock_mount_hash(); 2301719f5d7fSMiklos Szeredi 2302719f5d7fSMiklos Szeredi out_unlock: 230397216be0SAl Viro namespace_unlock(); 2304719f5d7fSMiklos Szeredi return err; 230507b20889SRam Pai } 230607b20889SRam Pai 23075ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 23085ff9d8a6SEric W. Biederman { 23095ff9d8a6SEric W. Biederman struct mount *child; 23105ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 23115ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 23125ff9d8a6SEric W. Biederman continue; 23135ff9d8a6SEric W. Biederman 23145ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 23155ff9d8a6SEric W. Biederman return true; 23165ff9d8a6SEric W. Biederman } 23175ff9d8a6SEric W. Biederman return false; 23185ff9d8a6SEric W. Biederman } 23195ff9d8a6SEric W. Biederman 2320a07b2000SAl Viro static struct mount *__do_loopback(struct path *old_path, int recurse) 2321a07b2000SAl Viro { 2322a07b2000SAl Viro struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt); 2323a07b2000SAl Viro 2324a07b2000SAl Viro if (IS_MNT_UNBINDABLE(old)) 2325a07b2000SAl Viro return mnt; 2326a07b2000SAl Viro 2327a07b2000SAl Viro if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations) 2328a07b2000SAl Viro return mnt; 2329a07b2000SAl Viro 2330a07b2000SAl Viro if (!recurse && has_locked_children(old, old_path->dentry)) 2331a07b2000SAl Viro return mnt; 2332a07b2000SAl Viro 2333a07b2000SAl Viro if (recurse) 2334a07b2000SAl Viro mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE); 2335a07b2000SAl Viro else 2336a07b2000SAl Viro mnt = clone_mnt(old, old_path->dentry, 0); 2337a07b2000SAl Viro 2338a07b2000SAl Viro if (!IS_ERR(mnt)) 2339a07b2000SAl Viro mnt->mnt.mnt_flags &= ~MNT_LOCKED; 2340a07b2000SAl Viro 2341a07b2000SAl Viro return mnt; 2342a07b2000SAl Viro } 2343a07b2000SAl Viro 234407b20889SRam Pai /* 23451da177e4SLinus Torvalds * do loopback mount. 23461da177e4SLinus Torvalds */ 2347808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 23482dafe1c4SEric Sandeen int recurse) 23491da177e4SLinus Torvalds { 23502d92ab3cSAl Viro struct path old_path; 2351a07b2000SAl Viro struct mount *mnt = NULL, *parent; 235284d17192SAl Viro struct mountpoint *mp; 235357eccb83SAl Viro int err; 23541da177e4SLinus Torvalds if (!old_name || !*old_name) 23551da177e4SLinus Torvalds return -EINVAL; 2356815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 23571da177e4SLinus Torvalds if (err) 23581da177e4SLinus Torvalds return err; 23591da177e4SLinus Torvalds 23608823c079SEric W. Biederman err = -EINVAL; 23614ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 23628823c079SEric W. Biederman goto out; 23638823c079SEric W. Biederman 236484d17192SAl Viro mp = lock_mount(path); 2365a07b2000SAl Viro if (IS_ERR(mp)) { 236684d17192SAl Viro err = PTR_ERR(mp); 23679676f0c6SRam Pai goto out; 2368a07b2000SAl Viro } 23699676f0c6SRam Pai 237084d17192SAl Viro parent = real_mount(path->mnt); 2371e149ed2bSAl Viro if (!check_mnt(parent)) 2372e149ed2bSAl Viro goto out2; 2373e149ed2bSAl Viro 2374a07b2000SAl Viro mnt = __do_loopback(&old_path, recurse); 2375be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2376be34d1a3SDavid Howells err = PTR_ERR(mnt); 2377e9c5d8a5SAndrey Vagin goto out2; 2378be34d1a3SDavid Howells } 2379ccd48bc7SAl Viro 238084d17192SAl Viro err = graft_tree(mnt, parent, mp); 23811da177e4SLinus Torvalds if (err) { 2382719ea2fbSAl Viro lock_mount_hash(); 2383e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2384719ea2fbSAl Viro unlock_mount_hash(); 23855b83d2c5SRam Pai } 2386b12cea91SAl Viro out2: 238784d17192SAl Viro unlock_mount(mp); 2388ccd48bc7SAl Viro out: 23892d92ab3cSAl Viro path_put(&old_path); 23901da177e4SLinus Torvalds return err; 23911da177e4SLinus Torvalds } 23921da177e4SLinus Torvalds 2393a07b2000SAl Viro static struct file *open_detached_copy(struct path *path, bool recursive) 2394a07b2000SAl Viro { 2395a07b2000SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 2396a07b2000SAl Viro struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true); 2397a07b2000SAl Viro struct mount *mnt, *p; 2398a07b2000SAl Viro struct file *file; 2399a07b2000SAl Viro 2400a07b2000SAl Viro if (IS_ERR(ns)) 2401a07b2000SAl Viro return ERR_CAST(ns); 2402a07b2000SAl Viro 2403a07b2000SAl Viro namespace_lock(); 2404a07b2000SAl Viro mnt = __do_loopback(path, recursive); 2405a07b2000SAl Viro if (IS_ERR(mnt)) { 2406a07b2000SAl Viro namespace_unlock(); 2407a07b2000SAl Viro free_mnt_ns(ns); 2408a07b2000SAl Viro return ERR_CAST(mnt); 2409a07b2000SAl Viro } 2410a07b2000SAl Viro 2411a07b2000SAl Viro lock_mount_hash(); 2412a07b2000SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2413a07b2000SAl Viro p->mnt_ns = ns; 2414a07b2000SAl Viro ns->mounts++; 2415a07b2000SAl Viro } 2416a07b2000SAl Viro ns->root = mnt; 2417a07b2000SAl Viro list_add_tail(&ns->list, &mnt->mnt_list); 2418a07b2000SAl Viro mntget(&mnt->mnt); 2419a07b2000SAl Viro unlock_mount_hash(); 2420a07b2000SAl Viro namespace_unlock(); 2421a07b2000SAl Viro 2422a07b2000SAl Viro mntput(path->mnt); 2423a07b2000SAl Viro path->mnt = &mnt->mnt; 2424a07b2000SAl Viro file = dentry_open(path, O_PATH, current_cred()); 2425a07b2000SAl Viro if (IS_ERR(file)) 2426a07b2000SAl Viro dissolve_on_fput(path->mnt); 2427a07b2000SAl Viro else 2428a07b2000SAl Viro file->f_mode |= FMODE_NEED_UNMOUNT; 2429a07b2000SAl Viro return file; 2430a07b2000SAl Viro } 2431a07b2000SAl Viro 24322658ce09SBen Dooks SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags) 2433a07b2000SAl Viro { 2434a07b2000SAl Viro struct file *file; 2435a07b2000SAl Viro struct path path; 2436a07b2000SAl Viro int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW; 2437a07b2000SAl Viro bool detached = flags & OPEN_TREE_CLONE; 2438a07b2000SAl Viro int error; 2439a07b2000SAl Viro int fd; 2440a07b2000SAl Viro 2441a07b2000SAl Viro BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC); 2442a07b2000SAl Viro 2443a07b2000SAl Viro if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE | 2444a07b2000SAl Viro AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE | 2445a07b2000SAl Viro OPEN_TREE_CLOEXEC)) 2446a07b2000SAl Viro return -EINVAL; 2447a07b2000SAl Viro 2448a07b2000SAl Viro if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE) 2449a07b2000SAl Viro return -EINVAL; 2450a07b2000SAl Viro 2451a07b2000SAl Viro if (flags & AT_NO_AUTOMOUNT) 2452a07b2000SAl Viro lookup_flags &= ~LOOKUP_AUTOMOUNT; 2453a07b2000SAl Viro if (flags & AT_SYMLINK_NOFOLLOW) 2454a07b2000SAl Viro lookup_flags &= ~LOOKUP_FOLLOW; 2455a07b2000SAl Viro if (flags & AT_EMPTY_PATH) 2456a07b2000SAl Viro lookup_flags |= LOOKUP_EMPTY; 2457a07b2000SAl Viro 2458a07b2000SAl Viro if (detached && !may_mount()) 2459a07b2000SAl Viro return -EPERM; 2460a07b2000SAl Viro 2461a07b2000SAl Viro fd = get_unused_fd_flags(flags & O_CLOEXEC); 2462a07b2000SAl Viro if (fd < 0) 2463a07b2000SAl Viro return fd; 2464a07b2000SAl Viro 2465a07b2000SAl Viro error = user_path_at(dfd, filename, lookup_flags, &path); 2466a07b2000SAl Viro if (unlikely(error)) { 2467a07b2000SAl Viro file = ERR_PTR(error); 2468a07b2000SAl Viro } else { 2469a07b2000SAl Viro if (detached) 2470a07b2000SAl Viro file = open_detached_copy(&path, flags & AT_RECURSIVE); 2471a07b2000SAl Viro else 2472a07b2000SAl Viro file = dentry_open(&path, O_PATH, current_cred()); 2473a07b2000SAl Viro path_put(&path); 2474a07b2000SAl Viro } 2475a07b2000SAl Viro if (IS_ERR(file)) { 2476a07b2000SAl Viro put_unused_fd(fd); 2477a07b2000SAl Viro return PTR_ERR(file); 2478a07b2000SAl Viro } 2479a07b2000SAl Viro fd_install(fd, file); 2480a07b2000SAl Viro return fd; 2481a07b2000SAl Viro } 2482a07b2000SAl Viro 248343f5e655SDavid Howells /* 248443f5e655SDavid Howells * Don't allow locked mount flags to be cleared. 248543f5e655SDavid Howells * 248643f5e655SDavid Howells * No locks need to be held here while testing the various MNT_LOCK 248743f5e655SDavid Howells * flags because those flags can never be cleared once they are set. 248843f5e655SDavid Howells */ 248943f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags) 24902e4b7fcdSDave Hansen { 249143f5e655SDavid Howells unsigned int fl = mnt->mnt.mnt_flags; 24922e4b7fcdSDave Hansen 249343f5e655SDavid Howells if ((fl & MNT_LOCK_READONLY) && 249443f5e655SDavid Howells !(mnt_flags & MNT_READONLY)) 249543f5e655SDavid Howells return false; 249643f5e655SDavid Howells 249743f5e655SDavid Howells if ((fl & MNT_LOCK_NODEV) && 249843f5e655SDavid Howells !(mnt_flags & MNT_NODEV)) 249943f5e655SDavid Howells return false; 250043f5e655SDavid Howells 250143f5e655SDavid Howells if ((fl & MNT_LOCK_NOSUID) && 250243f5e655SDavid Howells !(mnt_flags & MNT_NOSUID)) 250343f5e655SDavid Howells return false; 250443f5e655SDavid Howells 250543f5e655SDavid Howells if ((fl & MNT_LOCK_NOEXEC) && 250643f5e655SDavid Howells !(mnt_flags & MNT_NOEXEC)) 250743f5e655SDavid Howells return false; 250843f5e655SDavid Howells 250943f5e655SDavid Howells if ((fl & MNT_LOCK_ATIME) && 251043f5e655SDavid Howells ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) 251143f5e655SDavid Howells return false; 251243f5e655SDavid Howells 251343f5e655SDavid Howells return true; 251443f5e655SDavid Howells } 251543f5e655SDavid Howells 251643f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags) 251743f5e655SDavid Howells { 251843f5e655SDavid Howells bool readonly_request = (mnt_flags & MNT_READONLY); 251943f5e655SDavid Howells 252043f5e655SDavid Howells if (readonly_request == __mnt_is_readonly(&mnt->mnt)) 25212e4b7fcdSDave Hansen return 0; 25222e4b7fcdSDave Hansen 25232e4b7fcdSDave Hansen if (readonly_request) 252443f5e655SDavid Howells return mnt_make_readonly(mnt); 252543f5e655SDavid Howells 252668847c94SChristian Brauner mnt->mnt.mnt_flags &= ~MNT_READONLY; 252768847c94SChristian Brauner return 0; 252843f5e655SDavid Howells } 252943f5e655SDavid Howells 253043f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags) 253143f5e655SDavid Howells { 253243f5e655SDavid Howells mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 253343f5e655SDavid Howells mnt->mnt.mnt_flags = mnt_flags; 253443f5e655SDavid Howells touch_mnt_namespace(mnt->mnt_ns); 253543f5e655SDavid Howells } 253643f5e655SDavid Howells 2537f8b92ba6SDeepa Dinamani static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt) 2538f8b92ba6SDeepa Dinamani { 2539f8b92ba6SDeepa Dinamani struct super_block *sb = mnt->mnt_sb; 2540f8b92ba6SDeepa Dinamani 2541f8b92ba6SDeepa Dinamani if (!__mnt_is_readonly(mnt) && 2542f8b92ba6SDeepa Dinamani (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) { 2543f8b92ba6SDeepa Dinamani char *buf = (char *)__get_free_page(GFP_KERNEL); 2544f8b92ba6SDeepa Dinamani char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM); 2545f8b92ba6SDeepa Dinamani struct tm tm; 2546f8b92ba6SDeepa Dinamani 2547f8b92ba6SDeepa Dinamani time64_to_tm(sb->s_time_max, 0, &tm); 2548f8b92ba6SDeepa Dinamani 25490ecee669SEric Biggers pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n", 25500ecee669SEric Biggers sb->s_type->name, 25510ecee669SEric Biggers is_mounted(mnt) ? "remounted" : "mounted", 25520ecee669SEric Biggers mntpath, 2553f8b92ba6SDeepa Dinamani tm.tm_year+1900, (unsigned long long)sb->s_time_max); 2554f8b92ba6SDeepa Dinamani 2555f8b92ba6SDeepa Dinamani free_page((unsigned long)buf); 2556f8b92ba6SDeepa Dinamani } 2557f8b92ba6SDeepa Dinamani } 2558f8b92ba6SDeepa Dinamani 255943f5e655SDavid Howells /* 256043f5e655SDavid Howells * Handle reconfiguration of the mountpoint only without alteration of the 256143f5e655SDavid Howells * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND 256243f5e655SDavid Howells * to mount(2). 256343f5e655SDavid Howells */ 256443f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags) 256543f5e655SDavid Howells { 256643f5e655SDavid Howells struct super_block *sb = path->mnt->mnt_sb; 256743f5e655SDavid Howells struct mount *mnt = real_mount(path->mnt); 256843f5e655SDavid Howells int ret; 256943f5e655SDavid Howells 257043f5e655SDavid Howells if (!check_mnt(mnt)) 257143f5e655SDavid Howells return -EINVAL; 257243f5e655SDavid Howells 257343f5e655SDavid Howells if (path->dentry != mnt->mnt.mnt_root) 257443f5e655SDavid Howells return -EINVAL; 257543f5e655SDavid Howells 257643f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 257743f5e655SDavid Howells return -EPERM; 257843f5e655SDavid Howells 2579*e58ace1aSChristian Brauner /* 2580*e58ace1aSChristian Brauner * We're only checking whether the superblock is read-only not 2581*e58ace1aSChristian Brauner * changing it, so only take down_read(&sb->s_umount). 2582*e58ace1aSChristian Brauner */ 2583*e58ace1aSChristian Brauner down_read(&sb->s_umount); 258468847c94SChristian Brauner lock_mount_hash(); 258543f5e655SDavid Howells ret = change_mount_ro_state(mnt, mnt_flags); 258643f5e655SDavid Howells if (ret == 0) 258743f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 258868847c94SChristian Brauner unlock_mount_hash(); 2589*e58ace1aSChristian Brauner up_read(&sb->s_umount); 2590f8b92ba6SDeepa Dinamani 2591f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(path, &mnt->mnt); 2592f8b92ba6SDeepa Dinamani 259343f5e655SDavid Howells return ret; 25942e4b7fcdSDave Hansen } 25952e4b7fcdSDave Hansen 25961da177e4SLinus Torvalds /* 25971da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 25981da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 25991da177e4SLinus Torvalds * on it - tough luck. 26001da177e4SLinus Torvalds */ 2601e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags, 2602e462ec50SDavid Howells int mnt_flags, void *data) 26031da177e4SLinus Torvalds { 26041da177e4SLinus Torvalds int err; 26052d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2606143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 26078d0347f6SDavid Howells struct fs_context *fc; 26081da177e4SLinus Torvalds 2609143c8c91SAl Viro if (!check_mnt(mnt)) 26101da177e4SLinus Torvalds return -EINVAL; 26111da177e4SLinus Torvalds 26122d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 26131da177e4SLinus Torvalds return -EINVAL; 26141da177e4SLinus Torvalds 261543f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 261607b64558SEric W. Biederman return -EPERM; 26179566d674SEric W. Biederman 26188d0347f6SDavid Howells fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK); 26198d0347f6SDavid Howells if (IS_ERR(fc)) 26208d0347f6SDavid Howells return PTR_ERR(fc); 2621ff36fe2cSEric Paris 2622b330966fSMiklos Szeredi fc->oldapi = true; 26238d0347f6SDavid Howells err = parse_monolithic_mount_data(fc, data); 26248d0347f6SDavid Howells if (!err) { 26251da177e4SLinus Torvalds down_write(&sb->s_umount); 262657eccb83SAl Viro err = -EPERM; 262743f5e655SDavid Howells if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) { 26288d0347f6SDavid Howells err = reconfigure_super(fc); 262968847c94SChristian Brauner if (!err) { 263068847c94SChristian Brauner lock_mount_hash(); 263143f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 263268847c94SChristian Brauner unlock_mount_hash(); 263368847c94SChristian Brauner } 26340e55a7ccSDan Williams } 26356339dab8SAl Viro up_write(&sb->s_umount); 26368d0347f6SDavid Howells } 2637f8b92ba6SDeepa Dinamani 2638f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(path, &mnt->mnt); 2639f8b92ba6SDeepa Dinamani 26408d0347f6SDavid Howells put_fs_context(fc); 26411da177e4SLinus Torvalds return err; 26421da177e4SLinus Torvalds } 26431da177e4SLinus Torvalds 2644cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 26459676f0c6SRam Pai { 2646315fc83eSAl Viro struct mount *p; 2647909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2648fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 26499676f0c6SRam Pai return 1; 26509676f0c6SRam Pai } 26519676f0c6SRam Pai return 0; 26529676f0c6SRam Pai } 26539676f0c6SRam Pai 265444dfd84aSDavid Howells /* 265544dfd84aSDavid Howells * Check that there aren't references to earlier/same mount namespaces in the 265644dfd84aSDavid Howells * specified subtree. Such references can act as pins for mount namespaces 265744dfd84aSDavid Howells * that aren't checked by the mount-cycle checking code, thereby allowing 265844dfd84aSDavid Howells * cycles to be made. 265944dfd84aSDavid Howells */ 266044dfd84aSDavid Howells static bool check_for_nsfs_mounts(struct mount *subtree) 266144dfd84aSDavid Howells { 266244dfd84aSDavid Howells struct mount *p; 266344dfd84aSDavid Howells bool ret = false; 266444dfd84aSDavid Howells 266544dfd84aSDavid Howells lock_mount_hash(); 266644dfd84aSDavid Howells for (p = subtree; p; p = next_mnt(p, subtree)) 266744dfd84aSDavid Howells if (mnt_ns_loop(p->mnt.mnt_root)) 266844dfd84aSDavid Howells goto out; 266944dfd84aSDavid Howells 267044dfd84aSDavid Howells ret = true; 267144dfd84aSDavid Howells out: 267244dfd84aSDavid Howells unlock_mount_hash(); 267344dfd84aSDavid Howells return ret; 267444dfd84aSDavid Howells } 267544dfd84aSDavid Howells 26762db154b3SDavid Howells static int do_move_mount(struct path *old_path, struct path *new_path) 26771da177e4SLinus Torvalds { 267844dfd84aSDavid Howells struct mnt_namespace *ns; 2679676da58dSAl Viro struct mount *p; 26800fb54e50SAl Viro struct mount *old; 26812763d119SAl Viro struct mount *parent; 26822763d119SAl Viro struct mountpoint *mp, *old_mp; 268357eccb83SAl Viro int err; 268444dfd84aSDavid Howells bool attached; 26851da177e4SLinus Torvalds 26862db154b3SDavid Howells mp = lock_mount(new_path); 268784d17192SAl Viro if (IS_ERR(mp)) 26882db154b3SDavid Howells return PTR_ERR(mp); 2689cc53ce53SDavid Howells 26902db154b3SDavid Howells old = real_mount(old_path->mnt); 26912db154b3SDavid Howells p = real_mount(new_path->mnt); 26922763d119SAl Viro parent = old->mnt_parent; 269344dfd84aSDavid Howells attached = mnt_has_parent(old); 26942763d119SAl Viro old_mp = old->mnt_mp; 269544dfd84aSDavid Howells ns = old->mnt_ns; 2696143c8c91SAl Viro 26971da177e4SLinus Torvalds err = -EINVAL; 269844dfd84aSDavid Howells /* The mountpoint must be in our namespace. */ 269944dfd84aSDavid Howells if (!check_mnt(p)) 27002db154b3SDavid Howells goto out; 27011da177e4SLinus Torvalds 2702570d7a98SEric Biggers /* The thing moved must be mounted... */ 2703570d7a98SEric Biggers if (!is_mounted(&old->mnt)) 270444dfd84aSDavid Howells goto out; 270544dfd84aSDavid Howells 2706570d7a98SEric Biggers /* ... and either ours or the root of anon namespace */ 2707570d7a98SEric Biggers if (!(attached ? check_mnt(old) : is_anon_ns(ns))) 27082db154b3SDavid Howells goto out; 27091da177e4SLinus Torvalds 27102db154b3SDavid Howells if (old->mnt.mnt_flags & MNT_LOCKED) 27112db154b3SDavid Howells goto out; 27122db154b3SDavid Howells 27132db154b3SDavid Howells if (old_path->dentry != old_path->mnt->mnt_root) 27142db154b3SDavid Howells goto out; 27152db154b3SDavid Howells 27162db154b3SDavid Howells if (d_is_dir(new_path->dentry) != 27172db154b3SDavid Howells d_is_dir(old_path->dentry)) 27182db154b3SDavid Howells goto out; 271921444403SRam Pai /* 272021444403SRam Pai * Don't move a mount residing in a shared parent. 272121444403SRam Pai */ 27222763d119SAl Viro if (attached && IS_MNT_SHARED(parent)) 27232db154b3SDavid Howells goto out; 27249676f0c6SRam Pai /* 27259676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 27269676f0c6SRam Pai * mount which is shared. 27279676f0c6SRam Pai */ 2728fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 27292db154b3SDavid Howells goto out; 27301da177e4SLinus Torvalds err = -ELOOP; 273144dfd84aSDavid Howells if (!check_for_nsfs_mounts(old)) 273244dfd84aSDavid Howells goto out; 2733fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2734676da58dSAl Viro if (p == old) 27352db154b3SDavid Howells goto out; 27361da177e4SLinus Torvalds 27372db154b3SDavid Howells err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp, 27382763d119SAl Viro attached); 27394ac91378SJan Blunck if (err) 27402db154b3SDavid Howells goto out; 27411da177e4SLinus Torvalds 27421da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 27431da177e4SLinus Torvalds * automatically */ 27446776db3dSAl Viro list_del_init(&old->mnt_expire); 27452763d119SAl Viro if (attached) 27462763d119SAl Viro put_mountpoint(old_mp); 27471da177e4SLinus Torvalds out: 27482db154b3SDavid Howells unlock_mount(mp); 274944dfd84aSDavid Howells if (!err) { 27502763d119SAl Viro if (attached) 27512763d119SAl Viro mntput_no_expire(parent); 27522763d119SAl Viro else 275344dfd84aSDavid Howells free_mnt_ns(ns); 275444dfd84aSDavid Howells } 27552db154b3SDavid Howells return err; 27562db154b3SDavid Howells } 27572db154b3SDavid Howells 27582db154b3SDavid Howells static int do_move_mount_old(struct path *path, const char *old_name) 27592db154b3SDavid Howells { 27602db154b3SDavid Howells struct path old_path; 27612db154b3SDavid Howells int err; 27622db154b3SDavid Howells 27632db154b3SDavid Howells if (!old_name || !*old_name) 27642db154b3SDavid Howells return -EINVAL; 27652db154b3SDavid Howells 27662db154b3SDavid Howells err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 27672db154b3SDavid Howells if (err) 27682db154b3SDavid Howells return err; 27692db154b3SDavid Howells 27702db154b3SDavid Howells err = do_move_mount(&old_path, path); 27712d92ab3cSAl Viro path_put(&old_path); 27721da177e4SLinus Torvalds return err; 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds 27759d412a43SAl Viro /* 27769d412a43SAl Viro * add a mount into a namespace's mount tree 27779d412a43SAl Viro */ 27788f11538eSAl Viro static int do_add_mount(struct mount *newmnt, struct mountpoint *mp, 27798f11538eSAl Viro struct path *path, int mnt_flags) 27809d412a43SAl Viro { 27818f11538eSAl Viro struct mount *parent = real_mount(path->mnt); 27829d412a43SAl Viro 2783f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 27849d412a43SAl Viro 278584d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2786156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2787156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 27888f11538eSAl Viro return -EINVAL; 2789156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 279084d17192SAl Viro if (!parent->mnt_ns) 27918f11538eSAl Viro return -EINVAL; 2792156cacb1SAl Viro } 27939d412a43SAl Viro 27949d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 279595bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 27969d412a43SAl Viro path->mnt->mnt_root == path->dentry) 27978f11538eSAl Viro return -EBUSY; 27989d412a43SAl Viro 2799e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 28008f11538eSAl Viro return -EINVAL; 28019d412a43SAl Viro 280295bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 28038f11538eSAl Viro return graft_tree(newmnt, parent, mp); 28049d412a43SAl Viro } 2805b1e75df4SAl Viro 2806132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags); 2807132e4608SDavid Howells 2808132e4608SDavid Howells /* 2809132e4608SDavid Howells * Create a new mount using a superblock configuration and request it 2810132e4608SDavid Howells * be added to the namespace tree. 2811132e4608SDavid Howells */ 2812132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint, 2813132e4608SDavid Howells unsigned int mnt_flags) 2814132e4608SDavid Howells { 2815132e4608SDavid Howells struct vfsmount *mnt; 28168f11538eSAl Viro struct mountpoint *mp; 2817132e4608SDavid Howells struct super_block *sb = fc->root->d_sb; 2818132e4608SDavid Howells int error; 2819132e4608SDavid Howells 2820c9ce29edSAl Viro error = security_sb_kern_mount(sb); 2821c9ce29edSAl Viro if (!error && mount_too_revealing(sb, &mnt_flags)) 2822c9ce29edSAl Viro error = -EPERM; 2823c9ce29edSAl Viro 2824c9ce29edSAl Viro if (unlikely(error)) { 2825c9ce29edSAl Viro fc_drop_locked(fc); 2826c9ce29edSAl Viro return error; 2827132e4608SDavid Howells } 2828132e4608SDavid Howells 2829132e4608SDavid Howells up_write(&sb->s_umount); 2830132e4608SDavid Howells 2831132e4608SDavid Howells mnt = vfs_create_mount(fc); 2832132e4608SDavid Howells if (IS_ERR(mnt)) 2833132e4608SDavid Howells return PTR_ERR(mnt); 2834132e4608SDavid Howells 2835f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(mountpoint, mnt); 2836f8b92ba6SDeepa Dinamani 28378f11538eSAl Viro mp = lock_mount(mountpoint); 28388f11538eSAl Viro if (IS_ERR(mp)) { 28398f11538eSAl Viro mntput(mnt); 28408f11538eSAl Viro return PTR_ERR(mp); 28418f11538eSAl Viro } 28428f11538eSAl Viro error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags); 28438f11538eSAl Viro unlock_mount(mp); 28440ecee669SEric Biggers if (error < 0) 28450ecee669SEric Biggers mntput(mnt); 2846f8b92ba6SDeepa Dinamani return error; 2847f8b92ba6SDeepa Dinamani } 2848f8b92ba6SDeepa Dinamani 28491da177e4SLinus Torvalds /* 28501da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 28511da177e4SLinus Torvalds * namespace's tree 28521da177e4SLinus Torvalds */ 2853e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags, 2854808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 28551da177e4SLinus Torvalds { 28560c55cfc4SEric W. Biederman struct file_system_type *type; 2857a0c9a8b8SAl Viro struct fs_context *fc; 2858a0c9a8b8SAl Viro const char *subtype = NULL; 2859a0c9a8b8SAl Viro int err = 0; 28601da177e4SLinus Torvalds 28610c55cfc4SEric W. Biederman if (!fstype) 28621da177e4SLinus Torvalds return -EINVAL; 28631da177e4SLinus Torvalds 28640c55cfc4SEric W. Biederman type = get_fs_type(fstype); 28650c55cfc4SEric W. Biederman if (!type) 28660c55cfc4SEric W. Biederman return -ENODEV; 28670c55cfc4SEric W. Biederman 2868a0c9a8b8SAl Viro if (type->fs_flags & FS_HAS_SUBTYPE) { 2869a0c9a8b8SAl Viro subtype = strchr(fstype, '.'); 2870a0c9a8b8SAl Viro if (subtype) { 2871a0c9a8b8SAl Viro subtype++; 2872a0c9a8b8SAl Viro if (!*subtype) { 28730c55cfc4SEric W. Biederman put_filesystem(type); 2874a0c9a8b8SAl Viro return -EINVAL; 2875a0c9a8b8SAl Viro } 2876a0c9a8b8SAl Viro } 28778654df4eSEric W. Biederman } 28788654df4eSEric W. Biederman 2879a0c9a8b8SAl Viro fc = fs_context_for_mount(type, sb_flags); 2880a0c9a8b8SAl Viro put_filesystem(type); 2881a0c9a8b8SAl Viro if (IS_ERR(fc)) 2882a0c9a8b8SAl Viro return PTR_ERR(fc); 2883a0c9a8b8SAl Viro 28843e1aeb00SDavid Howells if (subtype) 28853e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "subtype", 28863e1aeb00SDavid Howells subtype, strlen(subtype)); 28873e1aeb00SDavid Howells if (!err && name) 28883e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "source", name, strlen(name)); 2889a0c9a8b8SAl Viro if (!err) 2890a0c9a8b8SAl Viro err = parse_monolithic_mount_data(fc, data); 2891c3aabf07SAl Viro if (!err && !mount_capable(fc)) 2892c3aabf07SAl Viro err = -EPERM; 2893a0c9a8b8SAl Viro if (!err) 2894a0c9a8b8SAl Viro err = vfs_get_tree(fc); 2895132e4608SDavid Howells if (!err) 2896132e4608SDavid Howells err = do_new_mount_fc(fc, path, mnt_flags); 2897a0c9a8b8SAl Viro 2898a0c9a8b8SAl Viro put_fs_context(fc); 289915f9a3f3SAl Viro return err; 29001da177e4SLinus Torvalds } 29011da177e4SLinus Torvalds 290219a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 290319a167afSAl Viro { 290426df6034SAl Viro struct dentry *dentry = path->dentry; 29058f11538eSAl Viro struct mountpoint *mp; 290625e195aaSAl Viro struct mount *mnt; 290719a167afSAl Viro int err; 290825e195aaSAl Viro 290925e195aaSAl Viro if (!m) 291025e195aaSAl Viro return 0; 291125e195aaSAl Viro if (IS_ERR(m)) 291225e195aaSAl Viro return PTR_ERR(m); 291325e195aaSAl Viro 291425e195aaSAl Viro mnt = real_mount(m); 291519a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 291619a167afSAl Viro * expired before we get a chance to add it 291719a167afSAl Viro */ 29186776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 291919a167afSAl Viro 292019a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 292126df6034SAl Viro m->mnt_root == dentry) { 2922b1e75df4SAl Viro err = -ELOOP; 292326df6034SAl Viro goto discard; 292419a167afSAl Viro } 292519a167afSAl Viro 292626df6034SAl Viro /* 292726df6034SAl Viro * we don't want to use lock_mount() - in this case finding something 292826df6034SAl Viro * that overmounts our mountpoint to be means "quitely drop what we've 292926df6034SAl Viro * got", not "try to mount it on top". 293026df6034SAl Viro */ 293126df6034SAl Viro inode_lock(dentry->d_inode); 293226df6034SAl Viro namespace_lock(); 293326df6034SAl Viro if (unlikely(cant_mount(dentry))) { 293426df6034SAl Viro err = -ENOENT; 293526df6034SAl Viro goto discard_locked; 293626df6034SAl Viro } 293726df6034SAl Viro rcu_read_lock(); 293826df6034SAl Viro if (unlikely(__lookup_mnt(path->mnt, dentry))) { 293926df6034SAl Viro rcu_read_unlock(); 294026df6034SAl Viro err = 0; 294126df6034SAl Viro goto discard_locked; 294226df6034SAl Viro } 294326df6034SAl Viro rcu_read_unlock(); 294426df6034SAl Viro mp = get_mountpoint(dentry); 29458f11538eSAl Viro if (IS_ERR(mp)) { 29468f11538eSAl Viro err = PTR_ERR(mp); 294726df6034SAl Viro goto discard_locked; 29488f11538eSAl Viro } 294926df6034SAl Viro 29508f11538eSAl Viro err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 29518f11538eSAl Viro unlock_mount(mp); 295226df6034SAl Viro if (unlikely(err)) 295326df6034SAl Viro goto discard; 295426df6034SAl Viro mntput(m); 2955b1e75df4SAl Viro return 0; 295626df6034SAl Viro 295726df6034SAl Viro discard_locked: 295826df6034SAl Viro namespace_unlock(); 295926df6034SAl Viro inode_unlock(dentry->d_inode); 296026df6034SAl Viro discard: 2961b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 29626776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 296397216be0SAl Viro namespace_lock(); 29646776db3dSAl Viro list_del_init(&mnt->mnt_expire); 296597216be0SAl Viro namespace_unlock(); 296619a167afSAl Viro } 2967b1e75df4SAl Viro mntput(m); 2968b1e75df4SAl Viro mntput(m); 296919a167afSAl Viro return err; 297019a167afSAl Viro } 297119a167afSAl Viro 2972ea5b778aSDavid Howells /** 2973ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2974ea5b778aSDavid Howells * @mnt: The mount to list. 2975ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2976ea5b778aSDavid Howells */ 2977ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2978ea5b778aSDavid Howells { 297997216be0SAl Viro namespace_lock(); 2980ea5b778aSDavid Howells 29816776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2982ea5b778aSDavid Howells 298397216be0SAl Viro namespace_unlock(); 2984ea5b778aSDavid Howells } 2985ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2986ea5b778aSDavid Howells 2987ea5b778aSDavid Howells /* 29881da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 29891da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 29901da177e4SLinus Torvalds * here 29911da177e4SLinus Torvalds */ 29921da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 29931da177e4SLinus Torvalds { 2994761d5c38SAl Viro struct mount *mnt, *next; 29951da177e4SLinus Torvalds LIST_HEAD(graveyard); 29961da177e4SLinus Torvalds 29971da177e4SLinus Torvalds if (list_empty(mounts)) 29981da177e4SLinus Torvalds return; 29991da177e4SLinus Torvalds 300097216be0SAl Viro namespace_lock(); 3001719ea2fbSAl Viro lock_mount_hash(); 30021da177e4SLinus Torvalds 30031da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 30041da177e4SLinus Torvalds * following criteria: 30051da177e4SLinus Torvalds * - only referenced by its parent vfsmount 30061da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 30071da177e4SLinus Torvalds * cleared by mntput()) 30081da177e4SLinus Torvalds */ 30096776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 3010863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 30111ab59738SAl Viro propagate_mount_busy(mnt, 1)) 30121da177e4SLinus Torvalds continue; 30136776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 30141da177e4SLinus Torvalds } 3015bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 30166776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 3017143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 3018e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 3019bcc5c7d2SAl Viro } 3020719ea2fbSAl Viro unlock_mount_hash(); 30213ab6abeeSAl Viro namespace_unlock(); 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds 30241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 30251da177e4SLinus Torvalds 30261da177e4SLinus Torvalds /* 30275528f911STrond Myklebust * Ripoff of 'select_parent()' 30285528f911STrond Myklebust * 30295528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 30305528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 30315528f911STrond Myklebust */ 3032692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 30335528f911STrond Myklebust { 3034692afc31SAl Viro struct mount *this_parent = parent; 30355528f911STrond Myklebust struct list_head *next; 30365528f911STrond Myklebust int found = 0; 30375528f911STrond Myklebust 30385528f911STrond Myklebust repeat: 30396b41d536SAl Viro next = this_parent->mnt_mounts.next; 30405528f911STrond Myklebust resume: 30416b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 30425528f911STrond Myklebust struct list_head *tmp = next; 30436b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 30445528f911STrond Myklebust 30455528f911STrond Myklebust next = tmp->next; 3046692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 30475528f911STrond Myklebust continue; 30485528f911STrond Myklebust /* 30495528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 30505528f911STrond Myklebust */ 30516b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 30525528f911STrond Myklebust this_parent = mnt; 30535528f911STrond Myklebust goto repeat; 30545528f911STrond Myklebust } 30555528f911STrond Myklebust 30561ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 30576776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 30585528f911STrond Myklebust found++; 30595528f911STrond Myklebust } 30605528f911STrond Myklebust } 30615528f911STrond Myklebust /* 30625528f911STrond Myklebust * All done at this level ... ascend and resume the search 30635528f911STrond Myklebust */ 30645528f911STrond Myklebust if (this_parent != parent) { 30656b41d536SAl Viro next = this_parent->mnt_child.next; 30660714a533SAl Viro this_parent = this_parent->mnt_parent; 30675528f911STrond Myklebust goto resume; 30685528f911STrond Myklebust } 30695528f911STrond Myklebust return found; 30705528f911STrond Myklebust } 30715528f911STrond Myklebust 30725528f911STrond Myklebust /* 30735528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 30745528f911STrond Myklebust * submounts of a specific parent mountpoint 307599b7db7bSNick Piggin * 307648a066e7SAl Viro * mount_lock must be held for write 30775528f911STrond Myklebust */ 3078b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 30795528f911STrond Myklebust { 30805528f911STrond Myklebust LIST_HEAD(graveyard); 3081761d5c38SAl Viro struct mount *m; 30825528f911STrond Myklebust 30835528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 3084c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 3085bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 3086761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 30876776db3dSAl Viro mnt_expire); 3088143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 3089e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 3090bcc5c7d2SAl Viro } 3091bcc5c7d2SAl Viro } 30925528f911STrond Myklebust } 30935528f911STrond Myklebust 3094028abd92SChristoph Hellwig static void *copy_mount_options(const void __user * data) 30951da177e4SLinus Torvalds { 3096b40ef869SAl Viro char *copy; 3097d563d678SCatalin Marinas unsigned left, offset; 30981da177e4SLinus Torvalds 30991da177e4SLinus Torvalds if (!data) 3100b40ef869SAl Viro return NULL; 31011da177e4SLinus Torvalds 3102b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 3103b40ef869SAl Viro if (!copy) 3104b40ef869SAl Viro return ERR_PTR(-ENOMEM); 31051da177e4SLinus Torvalds 3106d563d678SCatalin Marinas left = copy_from_user(copy, data, PAGE_SIZE); 31071da177e4SLinus Torvalds 3108d563d678SCatalin Marinas /* 3109d563d678SCatalin Marinas * Not all architectures have an exact copy_from_user(). Resort to 3110d563d678SCatalin Marinas * byte at a time. 3111d563d678SCatalin Marinas */ 3112d563d678SCatalin Marinas offset = PAGE_SIZE - left; 3113d563d678SCatalin Marinas while (left) { 3114d563d678SCatalin Marinas char c; 3115d563d678SCatalin Marinas if (get_user(c, (const char __user *)data + offset)) 3116d563d678SCatalin Marinas break; 3117d563d678SCatalin Marinas copy[offset] = c; 3118d563d678SCatalin Marinas left--; 3119d563d678SCatalin Marinas offset++; 3120d563d678SCatalin Marinas } 3121d563d678SCatalin Marinas 3122d563d678SCatalin Marinas if (left == PAGE_SIZE) { 3123b40ef869SAl Viro kfree(copy); 3124b40ef869SAl Viro return ERR_PTR(-EFAULT); 31251da177e4SLinus Torvalds } 3126d563d678SCatalin Marinas 3127b40ef869SAl Viro return copy; 31281da177e4SLinus Torvalds } 31291da177e4SLinus Torvalds 3130028abd92SChristoph Hellwig static char *copy_mount_string(const void __user *data) 3131eca6f534SVegard Nossum { 3132fbdb4401SChandan Rajendra return data ? strndup_user(data, PATH_MAX) : NULL; 3133eca6f534SVegard Nossum } 3134eca6f534SVegard Nossum 31351da177e4SLinus Torvalds /* 31361da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 31371da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 31381da177e4SLinus Torvalds * 31391da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 31401da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 31411da177e4SLinus Torvalds * information (or be NULL). 31421da177e4SLinus Torvalds * 31431da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 31441da177e4SLinus Torvalds * When the flags word was introduced its top half was required 31451da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 31461da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 31471da177e4SLinus Torvalds * and must be discarded. 31481da177e4SLinus Torvalds */ 3149c60166f0SChristoph Hellwig int path_mount(const char *dev_name, struct path *path, 3150808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 31511da177e4SLinus Torvalds { 3152e462ec50SDavid Howells unsigned int mnt_flags = 0, sb_flags; 3153a1e6aaa3SChristoph Hellwig int ret; 31541da177e4SLinus Torvalds 31551da177e4SLinus Torvalds /* Discard magic */ 31561da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 31571da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds /* Basic sanity checks */ 31601da177e4SLinus Torvalds if (data_page) 31611da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 31621da177e4SLinus Torvalds 3163e462ec50SDavid Howells if (flags & MS_NOUSER) 3164e462ec50SDavid Howells return -EINVAL; 3165e462ec50SDavid Howells 3166a1e6aaa3SChristoph Hellwig ret = security_sb_mount(dev_name, path, type_page, flags, data_page); 3167a1e6aaa3SChristoph Hellwig if (ret) 3168a1e6aaa3SChristoph Hellwig return ret; 3169a1e6aaa3SChristoph Hellwig if (!may_mount()) 3170a1e6aaa3SChristoph Hellwig return -EPERM; 3171a1e6aaa3SChristoph Hellwig if ((flags & SB_MANDLOCK) && !may_mandlock()) 3172a1e6aaa3SChristoph Hellwig return -EPERM; 3173a27ab9f2STetsuo Handa 3174613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 3175613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 31760a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 31770a1c01c9SMatthew Garrett 31781da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 31791da177e4SLinus Torvalds if (flags & MS_NOSUID) 31801da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 31811da177e4SLinus Torvalds if (flags & MS_NODEV) 31821da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 31831da177e4SLinus Torvalds if (flags & MS_NOEXEC) 31841da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 3185fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 3186fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 3187fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 3188fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 3189d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 3190d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 3191a9e5b732SDavid Howells if (flags & MS_RDONLY) 31922e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 3193dab741e0SMattias Nissler if (flags & MS_NOSYMFOLLOW) 3194dab741e0SMattias Nissler mnt_flags |= MNT_NOSYMFOLLOW; 3195fc33a7bbSChristoph Hellwig 3196ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 3197ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 3198ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 3199ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 3200ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 3201a1e6aaa3SChristoph Hellwig mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK; 3202ffbc6f0eSEric W. Biederman } 3203ffbc6f0eSEric W. Biederman 3204e462ec50SDavid Howells sb_flags = flags & (SB_RDONLY | 3205e462ec50SDavid Howells SB_SYNCHRONOUS | 3206e462ec50SDavid Howells SB_MANDLOCK | 3207e462ec50SDavid Howells SB_DIRSYNC | 3208e462ec50SDavid Howells SB_SILENT | 3209917086ffSMimi Zohar SB_POSIXACL | 3210d7ee9469SMarkus Trippelsdorf SB_LAZYTIME | 3211917086ffSMimi Zohar SB_I_VERSION); 32121da177e4SLinus Torvalds 321343f5e655SDavid Howells if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND)) 3214a1e6aaa3SChristoph Hellwig return do_reconfigure_mnt(path, mnt_flags); 3215a1e6aaa3SChristoph Hellwig if (flags & MS_REMOUNT) 3216a1e6aaa3SChristoph Hellwig return do_remount(path, flags, sb_flags, mnt_flags, data_page); 3217a1e6aaa3SChristoph Hellwig if (flags & MS_BIND) 3218a1e6aaa3SChristoph Hellwig return do_loopback(path, dev_name, flags & MS_REC); 3219a1e6aaa3SChristoph Hellwig if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 3220a1e6aaa3SChristoph Hellwig return do_change_type(path, flags); 3221a1e6aaa3SChristoph Hellwig if (flags & MS_MOVE) 3222a1e6aaa3SChristoph Hellwig return do_move_mount_old(path, dev_name); 3223a1e6aaa3SChristoph Hellwig 3224a1e6aaa3SChristoph Hellwig return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name, 32251da177e4SLinus Torvalds data_page); 3226a1e6aaa3SChristoph Hellwig } 3227a1e6aaa3SChristoph Hellwig 3228a1e6aaa3SChristoph Hellwig long do_mount(const char *dev_name, const char __user *dir_name, 3229a1e6aaa3SChristoph Hellwig const char *type_page, unsigned long flags, void *data_page) 3230a1e6aaa3SChristoph Hellwig { 3231a1e6aaa3SChristoph Hellwig struct path path; 3232a1e6aaa3SChristoph Hellwig int ret; 3233a1e6aaa3SChristoph Hellwig 3234a1e6aaa3SChristoph Hellwig ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path); 3235a1e6aaa3SChristoph Hellwig if (ret) 3236a1e6aaa3SChristoph Hellwig return ret; 3237a1e6aaa3SChristoph Hellwig ret = path_mount(dev_name, &path, type_page, flags, data_page); 32382d92ab3cSAl Viro path_put(&path); 3239a1e6aaa3SChristoph Hellwig return ret; 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds 3242537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 3243537f7ccbSEric W. Biederman { 3244537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 3245537f7ccbSEric W. Biederman } 3246537f7ccbSEric W. Biederman 3247537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 3248537f7ccbSEric W. Biederman { 3249537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 3250537f7ccbSEric W. Biederman } 3251537f7ccbSEric W. Biederman 3252771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 3253771b1371SEric W. Biederman { 325474e83122SAl Viro if (!is_anon_ns(ns)) 32556344c433SAl Viro ns_free_inum(&ns->ns); 3256537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 3257771b1371SEric W. Biederman put_user_ns(ns->user_ns); 3258771b1371SEric W. Biederman kfree(ns); 3259771b1371SEric W. Biederman } 3260771b1371SEric W. Biederman 32618823c079SEric W. Biederman /* 32628823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 32638823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 32648823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 32658823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 32668823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 32678823c079SEric W. Biederman */ 32688823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 32698823c079SEric W. Biederman 327074e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon) 3271cf8d2c11STrond Myklebust { 3272cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 3273537f7ccbSEric W. Biederman struct ucounts *ucounts; 327498f842e6SEric W. Biederman int ret; 3275cf8d2c11STrond Myklebust 3276537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 3277537f7ccbSEric W. Biederman if (!ucounts) 3278df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 3279537f7ccbSEric W. Biederman 328074e83122SAl Viro new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 3281537f7ccbSEric W. Biederman if (!new_ns) { 3282537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 3283cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 3284537f7ccbSEric W. Biederman } 328574e83122SAl Viro if (!anon) { 32866344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 328798f842e6SEric W. Biederman if (ret) { 328898f842e6SEric W. Biederman kfree(new_ns); 3289537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 329098f842e6SEric W. Biederman return ERR_PTR(ret); 329198f842e6SEric W. Biederman } 329274e83122SAl Viro } 329333c42940SAl Viro new_ns->ns.ops = &mntns_operations; 329474e83122SAl Viro if (!anon) 32958823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 32961a7b8969SKirill Tkhai refcount_set(&new_ns->ns.count, 1); 3297cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 3298cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 32999f6c61f9SMiklos Szeredi spin_lock_init(&new_ns->ns_lock); 3300771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 3301537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 3302cf8d2c11STrond Myklebust return new_ns; 3303cf8d2c11STrond Myklebust } 3304cf8d2c11STrond Myklebust 33050766f788SEmese Revfy __latent_entropy 33069559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 33079559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 33081da177e4SLinus Torvalds { 33096b3286edSKirill Korotaev struct mnt_namespace *new_ns; 33107f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 3311315fc83eSAl Viro struct mount *p, *q; 33129559f689SAl Viro struct mount *old; 3313cb338d06SAl Viro struct mount *new; 33147a472ef4SEric W. Biederman int copy_flags; 33151da177e4SLinus Torvalds 33169559f689SAl Viro BUG_ON(!ns); 33179559f689SAl Viro 33189559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 33199559f689SAl Viro get_mnt_ns(ns); 33209559f689SAl Viro return ns; 33219559f689SAl Viro } 33229559f689SAl Viro 33239559f689SAl Viro old = ns->root; 33249559f689SAl Viro 332574e83122SAl Viro new_ns = alloc_mnt_ns(user_ns, false); 3326cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 3327cf8d2c11STrond Myklebust return new_ns; 33281da177e4SLinus Torvalds 332997216be0SAl Viro namespace_lock(); 33301da177e4SLinus Torvalds /* First pass: copy the tree topology */ 33314ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 33329559f689SAl Viro if (user_ns != ns->user_ns) 33333bd045ccSAl Viro copy_flags |= CL_SHARED_TO_SLAVE; 33347a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 3335be34d1a3SDavid Howells if (IS_ERR(new)) { 3336328e6d90SAl Viro namespace_unlock(); 3337771b1371SEric W. Biederman free_mnt_ns(new_ns); 3338be34d1a3SDavid Howells return ERR_CAST(new); 33391da177e4SLinus Torvalds } 33403bd045ccSAl Viro if (user_ns != ns->user_ns) { 33413bd045ccSAl Viro lock_mount_hash(); 33423bd045ccSAl Viro lock_mnt_tree(new); 33433bd045ccSAl Viro unlock_mount_hash(); 33443bd045ccSAl Viro } 3345be08d6d2SAl Viro new_ns->root = new; 33461a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 33471da177e4SLinus Torvalds 33481da177e4SLinus Torvalds /* 33491da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 33501da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 33511da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 33521da177e4SLinus Torvalds */ 3353909b0a88SAl Viro p = old; 3354cb338d06SAl Viro q = new; 33551da177e4SLinus Torvalds while (p) { 3356143c8c91SAl Viro q->mnt_ns = new_ns; 3357d2921684SEric W. Biederman new_ns->mounts++; 33589559f689SAl Viro if (new_fs) { 33599559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 33609559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 3361315fc83eSAl Viro rootmnt = &p->mnt; 33621da177e4SLinus Torvalds } 33639559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 33649559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 3365315fc83eSAl Viro pwdmnt = &p->mnt; 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds } 3368909b0a88SAl Viro p = next_mnt(p, old); 3369909b0a88SAl Viro q = next_mnt(q, new); 33704ce5d2b1SEric W. Biederman if (!q) 33714ce5d2b1SEric W. Biederman break; 33724ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 33734ce5d2b1SEric W. Biederman p = next_mnt(p, old); 33741da177e4SLinus Torvalds } 3375328e6d90SAl Viro namespace_unlock(); 33761da177e4SLinus Torvalds 33771da177e4SLinus Torvalds if (rootmnt) 3378f03c6599SAl Viro mntput(rootmnt); 33791da177e4SLinus Torvalds if (pwdmnt) 3380f03c6599SAl Viro mntput(pwdmnt); 33811da177e4SLinus Torvalds 3382741a2951SJANAK DESAI return new_ns; 3383741a2951SJANAK DESAI } 3384741a2951SJANAK DESAI 338574e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name) 3386cf8d2c11STrond Myklebust { 33871a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 3388ea441d11SAl Viro struct mnt_namespace *ns; 3389d31da0f0SAl Viro struct super_block *s; 3390ea441d11SAl Viro struct path path; 3391ea441d11SAl Viro int err; 3392ea441d11SAl Viro 339374e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, true); 339474e83122SAl Viro if (IS_ERR(ns)) { 339574e83122SAl Viro mntput(m); 3396ea441d11SAl Viro return ERR_CAST(ns); 339774e83122SAl Viro } 339874e83122SAl Viro mnt->mnt_ns = ns; 339974e83122SAl Viro ns->root = mnt; 340074e83122SAl Viro ns->mounts++; 340174e83122SAl Viro list_add(&mnt->mnt_list, &ns->list); 3402ea441d11SAl Viro 340374e83122SAl Viro err = vfs_path_lookup(m->mnt_root, m, 3404ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 3405ea441d11SAl Viro 3406ea441d11SAl Viro put_mnt_ns(ns); 3407ea441d11SAl Viro 3408ea441d11SAl Viro if (err) 3409ea441d11SAl Viro return ERR_PTR(err); 3410ea441d11SAl Viro 3411ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 3412d31da0f0SAl Viro s = path.mnt->mnt_sb; 3413d31da0f0SAl Viro atomic_inc(&s->s_active); 3414ea441d11SAl Viro mntput(path.mnt); 3415ea441d11SAl Viro /* lock the sucker */ 3416d31da0f0SAl Viro down_write(&s->s_umount); 3417ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 3418ea441d11SAl Viro return path.dentry; 3419ea441d11SAl Viro } 3420ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 3421ea441d11SAl Viro 3422cccaa5e3SDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 3423cccaa5e3SDominik Brodowski char __user *, type, unsigned long, flags, void __user *, data) 34241da177e4SLinus Torvalds { 3425eca6f534SVegard Nossum int ret; 3426eca6f534SVegard Nossum char *kernel_type; 3427eca6f534SVegard Nossum char *kernel_dev; 3428b40ef869SAl Viro void *options; 34291da177e4SLinus Torvalds 3430b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 3431b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 3432b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 3433eca6f534SVegard Nossum goto out_type; 34341da177e4SLinus Torvalds 3435b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 3436b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 3437b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 3438eca6f534SVegard Nossum goto out_dev; 34391da177e4SLinus Torvalds 3440b40ef869SAl Viro options = copy_mount_options(data); 3441b40ef869SAl Viro ret = PTR_ERR(options); 3442b40ef869SAl Viro if (IS_ERR(options)) 3443eca6f534SVegard Nossum goto out_data; 34441da177e4SLinus Torvalds 3445b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 3446eca6f534SVegard Nossum 3447b40ef869SAl Viro kfree(options); 3448eca6f534SVegard Nossum out_data: 3449eca6f534SVegard Nossum kfree(kernel_dev); 3450eca6f534SVegard Nossum out_dev: 3451eca6f534SVegard Nossum kfree(kernel_type); 3452eca6f534SVegard Nossum out_type: 3453eca6f534SVegard Nossum return ret; 34541da177e4SLinus Torvalds } 34551da177e4SLinus Torvalds 34561da177e4SLinus Torvalds /* 345793766fbdSDavid Howells * Create a kernel mount representation for a new, prepared superblock 345893766fbdSDavid Howells * (specified by fs_fd) and attach to an open_tree-like file descriptor. 345993766fbdSDavid Howells */ 346093766fbdSDavid Howells SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags, 346193766fbdSDavid Howells unsigned int, attr_flags) 346293766fbdSDavid Howells { 346393766fbdSDavid Howells struct mnt_namespace *ns; 346493766fbdSDavid Howells struct fs_context *fc; 346593766fbdSDavid Howells struct file *file; 346693766fbdSDavid Howells struct path newmount; 346793766fbdSDavid Howells struct mount *mnt; 346893766fbdSDavid Howells struct fd f; 346993766fbdSDavid Howells unsigned int mnt_flags = 0; 347093766fbdSDavid Howells long ret; 347193766fbdSDavid Howells 347293766fbdSDavid Howells if (!may_mount()) 347393766fbdSDavid Howells return -EPERM; 347493766fbdSDavid Howells 347593766fbdSDavid Howells if ((flags & ~(FSMOUNT_CLOEXEC)) != 0) 347693766fbdSDavid Howells return -EINVAL; 347793766fbdSDavid Howells 347893766fbdSDavid Howells if (attr_flags & ~(MOUNT_ATTR_RDONLY | 347993766fbdSDavid Howells MOUNT_ATTR_NOSUID | 348093766fbdSDavid Howells MOUNT_ATTR_NODEV | 348193766fbdSDavid Howells MOUNT_ATTR_NOEXEC | 348293766fbdSDavid Howells MOUNT_ATTR__ATIME | 348393766fbdSDavid Howells MOUNT_ATTR_NODIRATIME)) 348493766fbdSDavid Howells return -EINVAL; 348593766fbdSDavid Howells 348693766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_RDONLY) 348793766fbdSDavid Howells mnt_flags |= MNT_READONLY; 348893766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NOSUID) 348993766fbdSDavid Howells mnt_flags |= MNT_NOSUID; 349093766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NODEV) 349193766fbdSDavid Howells mnt_flags |= MNT_NODEV; 349293766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NOEXEC) 349393766fbdSDavid Howells mnt_flags |= MNT_NOEXEC; 349493766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NODIRATIME) 349593766fbdSDavid Howells mnt_flags |= MNT_NODIRATIME; 349693766fbdSDavid Howells 349793766fbdSDavid Howells switch (attr_flags & MOUNT_ATTR__ATIME) { 349893766fbdSDavid Howells case MOUNT_ATTR_STRICTATIME: 349993766fbdSDavid Howells break; 350093766fbdSDavid Howells case MOUNT_ATTR_NOATIME: 350193766fbdSDavid Howells mnt_flags |= MNT_NOATIME; 350293766fbdSDavid Howells break; 350393766fbdSDavid Howells case MOUNT_ATTR_RELATIME: 350493766fbdSDavid Howells mnt_flags |= MNT_RELATIME; 350593766fbdSDavid Howells break; 350693766fbdSDavid Howells default: 350793766fbdSDavid Howells return -EINVAL; 350893766fbdSDavid Howells } 350993766fbdSDavid Howells 351093766fbdSDavid Howells f = fdget(fs_fd); 351193766fbdSDavid Howells if (!f.file) 351293766fbdSDavid Howells return -EBADF; 351393766fbdSDavid Howells 351493766fbdSDavid Howells ret = -EINVAL; 351593766fbdSDavid Howells if (f.file->f_op != &fscontext_fops) 351693766fbdSDavid Howells goto err_fsfd; 351793766fbdSDavid Howells 351893766fbdSDavid Howells fc = f.file->private_data; 351993766fbdSDavid Howells 352093766fbdSDavid Howells ret = mutex_lock_interruptible(&fc->uapi_mutex); 352193766fbdSDavid Howells if (ret < 0) 352293766fbdSDavid Howells goto err_fsfd; 352393766fbdSDavid Howells 352493766fbdSDavid Howells /* There must be a valid superblock or we can't mount it */ 352593766fbdSDavid Howells ret = -EINVAL; 352693766fbdSDavid Howells if (!fc->root) 352793766fbdSDavid Howells goto err_unlock; 352893766fbdSDavid Howells 352993766fbdSDavid Howells ret = -EPERM; 353093766fbdSDavid Howells if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) { 353193766fbdSDavid Howells pr_warn("VFS: Mount too revealing\n"); 353293766fbdSDavid Howells goto err_unlock; 353393766fbdSDavid Howells } 353493766fbdSDavid Howells 353593766fbdSDavid Howells ret = -EBUSY; 353693766fbdSDavid Howells if (fc->phase != FS_CONTEXT_AWAITING_MOUNT) 353793766fbdSDavid Howells goto err_unlock; 353893766fbdSDavid Howells 353993766fbdSDavid Howells ret = -EPERM; 354093766fbdSDavid Howells if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock()) 354193766fbdSDavid Howells goto err_unlock; 354293766fbdSDavid Howells 354393766fbdSDavid Howells newmount.mnt = vfs_create_mount(fc); 354493766fbdSDavid Howells if (IS_ERR(newmount.mnt)) { 354593766fbdSDavid Howells ret = PTR_ERR(newmount.mnt); 354693766fbdSDavid Howells goto err_unlock; 354793766fbdSDavid Howells } 354893766fbdSDavid Howells newmount.dentry = dget(fc->root); 354993766fbdSDavid Howells newmount.mnt->mnt_flags = mnt_flags; 355093766fbdSDavid Howells 355193766fbdSDavid Howells /* We've done the mount bit - now move the file context into more or 355293766fbdSDavid Howells * less the same state as if we'd done an fspick(). We don't want to 355393766fbdSDavid Howells * do any memory allocation or anything like that at this point as we 355493766fbdSDavid Howells * don't want to have to handle any errors incurred. 355593766fbdSDavid Howells */ 355693766fbdSDavid Howells vfs_clean_context(fc); 355793766fbdSDavid Howells 355893766fbdSDavid Howells ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true); 355993766fbdSDavid Howells if (IS_ERR(ns)) { 356093766fbdSDavid Howells ret = PTR_ERR(ns); 356193766fbdSDavid Howells goto err_path; 356293766fbdSDavid Howells } 356393766fbdSDavid Howells mnt = real_mount(newmount.mnt); 356493766fbdSDavid Howells mnt->mnt_ns = ns; 356593766fbdSDavid Howells ns->root = mnt; 356693766fbdSDavid Howells ns->mounts = 1; 356793766fbdSDavid Howells list_add(&mnt->mnt_list, &ns->list); 35681b0b9cc8SEric Biggers mntget(newmount.mnt); 356993766fbdSDavid Howells 357093766fbdSDavid Howells /* Attach to an apparent O_PATH fd with a note that we need to unmount 357193766fbdSDavid Howells * it, not just simply put it. 357293766fbdSDavid Howells */ 357393766fbdSDavid Howells file = dentry_open(&newmount, O_PATH, fc->cred); 357493766fbdSDavid Howells if (IS_ERR(file)) { 357593766fbdSDavid Howells dissolve_on_fput(newmount.mnt); 357693766fbdSDavid Howells ret = PTR_ERR(file); 357793766fbdSDavid Howells goto err_path; 357893766fbdSDavid Howells } 357993766fbdSDavid Howells file->f_mode |= FMODE_NEED_UNMOUNT; 358093766fbdSDavid Howells 358193766fbdSDavid Howells ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0); 358293766fbdSDavid Howells if (ret >= 0) 358393766fbdSDavid Howells fd_install(ret, file); 358493766fbdSDavid Howells else 358593766fbdSDavid Howells fput(file); 358693766fbdSDavid Howells 358793766fbdSDavid Howells err_path: 358893766fbdSDavid Howells path_put(&newmount); 358993766fbdSDavid Howells err_unlock: 359093766fbdSDavid Howells mutex_unlock(&fc->uapi_mutex); 359193766fbdSDavid Howells err_fsfd: 359293766fbdSDavid Howells fdput(f); 359393766fbdSDavid Howells return ret; 359493766fbdSDavid Howells } 359593766fbdSDavid Howells 359693766fbdSDavid Howells /* 359793766fbdSDavid Howells * Move a mount from one place to another. In combination with 359893766fbdSDavid Howells * fsopen()/fsmount() this is used to install a new mount and in combination 359993766fbdSDavid Howells * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy 360093766fbdSDavid Howells * a mount subtree. 36012db154b3SDavid Howells * 36022db154b3SDavid Howells * Note the flags value is a combination of MOVE_MOUNT_* flags. 36032db154b3SDavid Howells */ 36042db154b3SDavid Howells SYSCALL_DEFINE5(move_mount, 36052658ce09SBen Dooks int, from_dfd, const char __user *, from_pathname, 36062658ce09SBen Dooks int, to_dfd, const char __user *, to_pathname, 36072db154b3SDavid Howells unsigned int, flags) 36082db154b3SDavid Howells { 36092db154b3SDavid Howells struct path from_path, to_path; 36102db154b3SDavid Howells unsigned int lflags; 36112db154b3SDavid Howells int ret = 0; 36122db154b3SDavid Howells 36132db154b3SDavid Howells if (!may_mount()) 36142db154b3SDavid Howells return -EPERM; 36152db154b3SDavid Howells 36162db154b3SDavid Howells if (flags & ~MOVE_MOUNT__MASK) 36172db154b3SDavid Howells return -EINVAL; 36182db154b3SDavid Howells 36192db154b3SDavid Howells /* If someone gives a pathname, they aren't permitted to move 36202db154b3SDavid Howells * from an fd that requires unmount as we can't get at the flag 36212db154b3SDavid Howells * to clear it afterwards. 36222db154b3SDavid Howells */ 36232db154b3SDavid Howells lflags = 0; 36242db154b3SDavid Howells if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW; 36252db154b3SDavid Howells if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT; 36262db154b3SDavid Howells if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY; 36272db154b3SDavid Howells 36282db154b3SDavid Howells ret = user_path_at(from_dfd, from_pathname, lflags, &from_path); 36292db154b3SDavid Howells if (ret < 0) 36302db154b3SDavid Howells return ret; 36312db154b3SDavid Howells 36322db154b3SDavid Howells lflags = 0; 36332db154b3SDavid Howells if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW; 36342db154b3SDavid Howells if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT; 36352db154b3SDavid Howells if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY; 36362db154b3SDavid Howells 36372db154b3SDavid Howells ret = user_path_at(to_dfd, to_pathname, lflags, &to_path); 36382db154b3SDavid Howells if (ret < 0) 36392db154b3SDavid Howells goto out_from; 36402db154b3SDavid Howells 36412db154b3SDavid Howells ret = security_move_mount(&from_path, &to_path); 36422db154b3SDavid Howells if (ret < 0) 36432db154b3SDavid Howells goto out_to; 36442db154b3SDavid Howells 36452db154b3SDavid Howells ret = do_move_mount(&from_path, &to_path); 36462db154b3SDavid Howells 36472db154b3SDavid Howells out_to: 36482db154b3SDavid Howells path_put(&to_path); 36492db154b3SDavid Howells out_from: 36502db154b3SDavid Howells path_put(&from_path); 36512db154b3SDavid Howells return ret; 36522db154b3SDavid Howells } 36532db154b3SDavid Howells 36542db154b3SDavid Howells /* 3655afac7cbaSAl Viro * Return true if path is reachable from root 3656afac7cbaSAl Viro * 365748a066e7SAl Viro * namespace_sem or mount_lock is held 3658afac7cbaSAl Viro */ 3659643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3660afac7cbaSAl Viro const struct path *root) 3661afac7cbaSAl Viro { 3662643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3663a73324daSAl Viro dentry = mnt->mnt_mountpoint; 36640714a533SAl Viro mnt = mnt->mnt_parent; 3665afac7cbaSAl Viro } 3666643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3667afac7cbaSAl Viro } 3668afac7cbaSAl Viro 3669640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3670afac7cbaSAl Viro { 367125ab4c9bSYaowei Bai bool res; 367248a066e7SAl Viro read_seqlock_excl(&mount_lock); 3673643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 367448a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3675afac7cbaSAl Viro return res; 3676afac7cbaSAl Viro } 3677afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3678afac7cbaSAl Viro 3679afac7cbaSAl Viro /* 36801da177e4SLinus Torvalds * pivot_root Semantics: 36811da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 36821da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 36831da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 36841da177e4SLinus Torvalds * 36851da177e4SLinus Torvalds * Restrictions: 36861da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 36871da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 36881da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 36891da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 36901da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 36911da177e4SLinus Torvalds * 36924a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 36930c1bc6b8SMauro Carvalho Chehab * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives 36944a0d11faSNeil Brown * in this situation. 36954a0d11faSNeil Brown * 36961da177e4SLinus Torvalds * Notes: 36971da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 36981da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 36991da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 37001da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 37011da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 37021da177e4SLinus Torvalds * first. 37031da177e4SLinus Torvalds */ 37043480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 37053480b257SHeiko Carstens const char __user *, put_old) 37061da177e4SLinus Torvalds { 37072763d119SAl Viro struct path new, old, root; 37082763d119SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent; 370984d17192SAl Viro struct mountpoint *old_mp, *root_mp; 37101da177e4SLinus Torvalds int error; 37111da177e4SLinus Torvalds 37129b40bc90SAl Viro if (!may_mount()) 37131da177e4SLinus Torvalds return -EPERM; 37141da177e4SLinus Torvalds 3715ce6595a2SAl Viro error = user_path_at(AT_FDCWD, new_root, 3716ce6595a2SAl Viro LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new); 37171da177e4SLinus Torvalds if (error) 37181da177e4SLinus Torvalds goto out0; 37191da177e4SLinus Torvalds 3720ce6595a2SAl Viro error = user_path_at(AT_FDCWD, put_old, 3721ce6595a2SAl Viro LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old); 37221da177e4SLinus Torvalds if (error) 37231da177e4SLinus Torvalds goto out1; 37241da177e4SLinus Torvalds 37252d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3726b12cea91SAl Viro if (error) 3727b12cea91SAl Viro goto out2; 37281da177e4SLinus Torvalds 3729f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 373084d17192SAl Viro old_mp = lock_mount(&old); 373184d17192SAl Viro error = PTR_ERR(old_mp); 373284d17192SAl Viro if (IS_ERR(old_mp)) 3733b12cea91SAl Viro goto out3; 3734b12cea91SAl Viro 37351da177e4SLinus Torvalds error = -EINVAL; 3736419148daSAl Viro new_mnt = real_mount(new.mnt); 3737419148daSAl Viro root_mnt = real_mount(root.mnt); 373884d17192SAl Viro old_mnt = real_mount(old.mnt); 37392763d119SAl Viro ex_parent = new_mnt->mnt_parent; 37402763d119SAl Viro root_parent = root_mnt->mnt_parent; 374184d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 37422763d119SAl Viro IS_MNT_SHARED(ex_parent) || 37432763d119SAl Viro IS_MNT_SHARED(root_parent)) 3744b12cea91SAl Viro goto out4; 3745143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3746b12cea91SAl Viro goto out4; 37475ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 37485ff9d8a6SEric W. Biederman goto out4; 37491da177e4SLinus Torvalds error = -ENOENT; 3750f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3751b12cea91SAl Viro goto out4; 37521da177e4SLinus Torvalds error = -EBUSY; 375384d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3754b12cea91SAl Viro goto out4; /* loop, on the same file system */ 37551da177e4SLinus Torvalds error = -EINVAL; 37568c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3757b12cea91SAl Viro goto out4; /* not a mountpoint */ 3758676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3759b12cea91SAl Viro goto out4; /* not attached */ 37602d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3761b12cea91SAl Viro goto out4; /* not a mountpoint */ 3762676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3763b12cea91SAl Viro goto out4; /* not attached */ 37644ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 376584d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3766b12cea91SAl Viro goto out4; 37670d082601SEric W. Biederman /* make certain new is below the root */ 37680d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 37690d082601SEric W. Biederman goto out4; 3770719ea2fbSAl Viro lock_mount_hash(); 37712763d119SAl Viro umount_mnt(new_mnt); 37722763d119SAl Viro root_mp = unhash_mnt(root_mnt); /* we'll need its mountpoint */ 37735ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 37745ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 37755ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 37765ff9d8a6SEric W. Biederman } 37774ac91378SJan Blunck /* mount old root on put_old */ 377884d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 37794ac91378SJan Blunck /* mount new_root on / */ 37802763d119SAl Viro attach_mnt(new_mnt, root_parent, root_mp); 37812763d119SAl Viro mnt_add_count(root_parent, -1); 37826b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 37834fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 37844fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 37853895dbf8SEric W. Biederman put_mountpoint(root_mp); 3786719ea2fbSAl Viro unlock_mount_hash(); 37872d8f3038SAl Viro chroot_fs_refs(&root, &new); 37881da177e4SLinus Torvalds error = 0; 3789b12cea91SAl Viro out4: 379084d17192SAl Viro unlock_mount(old_mp); 37912763d119SAl Viro if (!error) 37922763d119SAl Viro mntput_no_expire(ex_parent); 3793b12cea91SAl Viro out3: 37948c3ee42eSAl Viro path_put(&root); 3795b12cea91SAl Viro out2: 37962d8f3038SAl Viro path_put(&old); 37971da177e4SLinus Torvalds out1: 37982d8f3038SAl Viro path_put(&new); 37991da177e4SLinus Torvalds out0: 38001da177e4SLinus Torvalds return error; 38011da177e4SLinus Torvalds } 38021da177e4SLinus Torvalds 38031da177e4SLinus Torvalds static void __init init_mount_tree(void) 38041da177e4SLinus Torvalds { 38051da177e4SLinus Torvalds struct vfsmount *mnt; 380674e83122SAl Viro struct mount *m; 38076b3286edSKirill Korotaev struct mnt_namespace *ns; 3808ac748a09SJan Blunck struct path root; 38091da177e4SLinus Torvalds 3810fd3e007fSAl Viro mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL); 38111da177e4SLinus Torvalds if (IS_ERR(mnt)) 38121da177e4SLinus Torvalds panic("Can't create rootfs"); 3813b3e19d92SNick Piggin 381474e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, false); 38153b22edc5STrond Myklebust if (IS_ERR(ns)) 38161da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 381774e83122SAl Viro m = real_mount(mnt); 381874e83122SAl Viro m->mnt_ns = ns; 381974e83122SAl Viro ns->root = m; 382074e83122SAl Viro ns->mounts = 1; 382174e83122SAl Viro list_add(&m->mnt_list, &ns->list); 38226b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 38236b3286edSKirill Korotaev get_mnt_ns(ns); 38241da177e4SLinus Torvalds 3825be08d6d2SAl Viro root.mnt = mnt; 3826be08d6d2SAl Viro root.dentry = mnt->mnt_root; 3827da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 3828ac748a09SJan Blunck 3829ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 3830ac748a09SJan Blunck set_fs_root(current->fs, &root); 38311da177e4SLinus Torvalds } 38321da177e4SLinus Torvalds 383374bf17cfSDenis Cheng void __init mnt_init(void) 38341da177e4SLinus Torvalds { 383515a67dd8SRandy Dunlap int err; 38361da177e4SLinus Torvalds 38377d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 383820c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 38391da177e4SLinus Torvalds 38400818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 384138129a13SAl Viro sizeof(struct hlist_head), 38420818bf27SAl Viro mhash_entries, 19, 38433d375d78SPavel Tatashin HASH_ZERO, 38440818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 38450818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 38460818bf27SAl Viro sizeof(struct hlist_head), 38470818bf27SAl Viro mphash_entries, 19, 38483d375d78SPavel Tatashin HASH_ZERO, 38490818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 38501da177e4SLinus Torvalds 385184d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 38521da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 38531da177e4SLinus Torvalds 38544b93dc9bSTejun Heo kernfs_init(); 38554b93dc9bSTejun Heo 385615a67dd8SRandy Dunlap err = sysfs_init(); 385715a67dd8SRandy Dunlap if (err) 385815a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 38598e24eea7SHarvey Harrison __func__, err); 386000d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 386100d26666SGreg Kroah-Hartman if (!fs_kobj) 38628e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 3863037f11b4SAl Viro shmem_init(); 38641da177e4SLinus Torvalds init_rootfs(); 38651da177e4SLinus Torvalds init_mount_tree(); 38661da177e4SLinus Torvalds } 38671da177e4SLinus Torvalds 3868616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 38691da177e4SLinus Torvalds { 38701a7b8969SKirill Tkhai if (!refcount_dec_and_test(&ns->ns.count)) 3871616511d0STrond Myklebust return; 38727b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3873771b1371SEric W. Biederman free_mnt_ns(ns); 38741da177e4SLinus Torvalds } 38759d412a43SAl Viro 3876d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type) 38779d412a43SAl Viro { 3878423e0ab0STim Chen struct vfsmount *mnt; 3879d911b458SDavid Howells mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 3880423e0ab0STim Chen if (!IS_ERR(mnt)) { 3881423e0ab0STim Chen /* 3882423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3883423e0ab0STim Chen * we unmount before file sys is unregistered 3884423e0ab0STim Chen */ 3885f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3886423e0ab0STim Chen } 3887423e0ab0STim Chen return mnt; 38889d412a43SAl Viro } 3889d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount); 3890423e0ab0STim Chen 3891423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3892423e0ab0STim Chen { 3893423e0ab0STim Chen /* release long term mount so mount point can be released */ 3894423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3895f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 389648a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3897423e0ab0STim Chen mntput(mnt); 3898423e0ab0STim Chen } 3899423e0ab0STim Chen } 3900423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 390102125a82SAl Viro 3902df820f8dSMiklos Szeredi void kern_unmount_array(struct vfsmount *mnt[], unsigned int num) 3903df820f8dSMiklos Szeredi { 3904df820f8dSMiklos Szeredi unsigned int i; 3905df820f8dSMiklos Szeredi 3906df820f8dSMiklos Szeredi for (i = 0; i < num; i++) 3907df820f8dSMiklos Szeredi if (mnt[i]) 3908df820f8dSMiklos Szeredi real_mount(mnt[i])->mnt_ns = NULL; 3909df820f8dSMiklos Szeredi synchronize_rcu_expedited(); 3910df820f8dSMiklos Szeredi for (i = 0; i < num; i++) 3911df820f8dSMiklos Szeredi mntput(mnt[i]); 3912df820f8dSMiklos Szeredi } 3913df820f8dSMiklos Szeredi EXPORT_SYMBOL(kern_unmount_array); 3914df820f8dSMiklos Szeredi 391502125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 391602125a82SAl Viro { 3917143c8c91SAl Viro return check_mnt(real_mount(mnt)); 391802125a82SAl Viro } 39198823c079SEric W. Biederman 39203151527eSEric W. Biederman bool current_chrooted(void) 39213151527eSEric W. Biederman { 39223151527eSEric W. Biederman /* Does the current process have a non-standard root */ 39233151527eSEric W. Biederman struct path ns_root; 39243151527eSEric W. Biederman struct path fs_root; 39253151527eSEric W. Biederman bool chrooted; 39263151527eSEric W. Biederman 39273151527eSEric W. Biederman /* Find the namespace root */ 39283151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 39293151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 39303151527eSEric W. Biederman path_get(&ns_root); 39313151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 39323151527eSEric W. Biederman ; 39333151527eSEric W. Biederman 39343151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 39353151527eSEric W. Biederman 39363151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 39373151527eSEric W. Biederman 39383151527eSEric W. Biederman path_put(&fs_root); 39393151527eSEric W. Biederman path_put(&ns_root); 39403151527eSEric W. Biederman 39413151527eSEric W. Biederman return chrooted; 39423151527eSEric W. Biederman } 39433151527eSEric W. Biederman 3944132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns, 3945132e4608SDavid Howells const struct super_block *sb, 39468654df4eSEric W. Biederman int *new_mnt_flags) 394787a8ebd6SEric W. Biederman { 39488c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 394987a8ebd6SEric W. Biederman struct mount *mnt; 3950e51db735SEric W. Biederman bool visible = false; 395187a8ebd6SEric W. Biederman 395244bb4385SAl Viro down_read(&namespace_sem); 39539f6c61f9SMiklos Szeredi lock_ns_list(ns); 395487a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3955e51db735SEric W. Biederman struct mount *child; 395677b1a97dSEric W. Biederman int mnt_flags; 395777b1a97dSEric W. Biederman 39589f6c61f9SMiklos Szeredi if (mnt_is_cursor(mnt)) 39599f6c61f9SMiklos Szeredi continue; 39609f6c61f9SMiklos Szeredi 3961132e4608SDavid Howells if (mnt->mnt.mnt_sb->s_type != sb->s_type) 3962e51db735SEric W. Biederman continue; 3963e51db735SEric W. Biederman 39647e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 39657e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 39667e96c1b0SEric W. Biederman */ 39677e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 39687e96c1b0SEric W. Biederman continue; 39697e96c1b0SEric W. Biederman 3970a1935c17SEric W. Biederman /* A local view of the mount flags */ 397177b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 397277b1a97dSEric W. Biederman 3973695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 3974bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt.mnt_sb)) 3975695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 3976695e9df0SEric W. Biederman 39778c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 39788c6cf9ccSEric W. Biederman * than the proposed new mount. 39798c6cf9ccSEric W. Biederman */ 398077b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 39818c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 39828c6cf9ccSEric W. Biederman continue; 398377b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 398477b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 39858c6cf9ccSEric W. Biederman continue; 39868c6cf9ccSEric W. Biederman 3987ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 3988ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 3989ceeb0e5dSEric W. Biederman * empty directories. 3990e51db735SEric W. Biederman */ 3991e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3992e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3993ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 3994d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 3995ceeb0e5dSEric W. Biederman continue; 39967236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 39977236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 3998e51db735SEric W. Biederman goto next; 399987a8ebd6SEric W. Biederman } 40008c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 400177b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 40028c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 4003e51db735SEric W. Biederman visible = true; 4004e51db735SEric W. Biederman goto found; 4005e51db735SEric W. Biederman next: ; 400687a8ebd6SEric W. Biederman } 4007e51db735SEric W. Biederman found: 40089f6c61f9SMiklos Szeredi unlock_ns_list(ns); 400944bb4385SAl Viro up_read(&namespace_sem); 4010e51db735SEric W. Biederman return visible; 401187a8ebd6SEric W. Biederman } 401287a8ebd6SEric W. Biederman 4013132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags) 40148654df4eSEric W. Biederman { 4015a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 40168654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 40178654df4eSEric W. Biederman unsigned long s_iflags; 40188654df4eSEric W. Biederman 40198654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 40208654df4eSEric W. Biederman return false; 40218654df4eSEric W. Biederman 40228654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 4023132e4608SDavid Howells s_iflags = sb->s_iflags; 40248654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 40258654df4eSEric W. Biederman return false; 40268654df4eSEric W. Biederman 4027a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 4028a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 4029a1935c17SEric W. Biederman required_iflags); 4030a1935c17SEric W. Biederman return true; 4031a1935c17SEric W. Biederman } 4032a1935c17SEric W. Biederman 4033132e4608SDavid Howells return !mnt_already_visible(ns, sb, new_mnt_flags); 40348654df4eSEric W. Biederman } 40358654df4eSEric W. Biederman 4036380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 4037380cf5baSAndy Lutomirski { 4038380cf5baSAndy Lutomirski /* 4039380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 4040380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 4041380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 4042380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 4043380cf5baSAndy Lutomirski * in other namespaces. 4044380cf5baSAndy Lutomirski */ 4045380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 4046380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 4047380cf5baSAndy Lutomirski } 4048380cf5baSAndy Lutomirski 404964964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 40508823c079SEric W. Biederman { 405158be2825SAl Viro struct ns_common *ns = NULL; 40528823c079SEric W. Biederman struct nsproxy *nsproxy; 40538823c079SEric W. Biederman 4054728dba3aSEric W. Biederman task_lock(task); 4055728dba3aSEric W. Biederman nsproxy = task->nsproxy; 40568823c079SEric W. Biederman if (nsproxy) { 405758be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 405858be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 40598823c079SEric W. Biederman } 4060728dba3aSEric W. Biederman task_unlock(task); 40618823c079SEric W. Biederman 40628823c079SEric W. Biederman return ns; 40638823c079SEric W. Biederman } 40648823c079SEric W. Biederman 406564964528SAl Viro static void mntns_put(struct ns_common *ns) 40668823c079SEric W. Biederman { 406758be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 40688823c079SEric W. Biederman } 40698823c079SEric W. Biederman 4070f2a8d52eSChristian Brauner static int mntns_install(struct nsset *nsset, struct ns_common *ns) 40718823c079SEric W. Biederman { 4072f2a8d52eSChristian Brauner struct nsproxy *nsproxy = nsset->nsproxy; 4073f2a8d52eSChristian Brauner struct fs_struct *fs = nsset->fs; 40744f757f3cSAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 4075f2a8d52eSChristian Brauner struct user_namespace *user_ns = nsset->cred->user_ns; 40768823c079SEric W. Biederman struct path root; 40774f757f3cSAl Viro int err; 40788823c079SEric W. Biederman 40790c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 4080f2a8d52eSChristian Brauner !ns_capable(user_ns, CAP_SYS_CHROOT) || 4081f2a8d52eSChristian Brauner !ns_capable(user_ns, CAP_SYS_ADMIN)) 4082ae11e0f1SZhao Hongjiang return -EPERM; 40838823c079SEric W. Biederman 408474e83122SAl Viro if (is_anon_ns(mnt_ns)) 408574e83122SAl Viro return -EINVAL; 408674e83122SAl Viro 40878823c079SEric W. Biederman if (fs->users != 1) 40888823c079SEric W. Biederman return -EINVAL; 40898823c079SEric W. Biederman 40908823c079SEric W. Biederman get_mnt_ns(mnt_ns); 40914f757f3cSAl Viro old_mnt_ns = nsproxy->mnt_ns; 40928823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 40938823c079SEric W. Biederman 40948823c079SEric W. Biederman /* Find the root */ 40954f757f3cSAl Viro err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 40964f757f3cSAl Viro "/", LOOKUP_DOWN, &root); 40974f757f3cSAl Viro if (err) { 40984f757f3cSAl Viro /* revert to old namespace */ 40994f757f3cSAl Viro nsproxy->mnt_ns = old_mnt_ns; 41004f757f3cSAl Viro put_mnt_ns(mnt_ns); 41014f757f3cSAl Viro return err; 41024f757f3cSAl Viro } 41038823c079SEric W. Biederman 41044068367cSAndrei Vagin put_mnt_ns(old_mnt_ns); 41054068367cSAndrei Vagin 41068823c079SEric W. Biederman /* Update the pwd and root */ 41078823c079SEric W. Biederman set_fs_pwd(fs, &root); 41088823c079SEric W. Biederman set_fs_root(fs, &root); 41098823c079SEric W. Biederman 41108823c079SEric W. Biederman path_put(&root); 41118823c079SEric W. Biederman return 0; 41128823c079SEric W. Biederman } 41138823c079SEric W. Biederman 4114bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 4115bcac25a5SAndrey Vagin { 4116bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 4117bcac25a5SAndrey Vagin } 4118bcac25a5SAndrey Vagin 41198823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 41208823c079SEric W. Biederman .name = "mnt", 41218823c079SEric W. Biederman .type = CLONE_NEWNS, 41228823c079SEric W. Biederman .get = mntns_get, 41238823c079SEric W. Biederman .put = mntns_put, 41248823c079SEric W. Biederman .install = mntns_install, 4125bcac25a5SAndrey Vagin .owner = mntns_owner, 41268823c079SEric W. Biederman }; 4127