11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namespace.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright Al Viro 2000, 2001 51da177e4SLinus Torvalds * Released under GPL v2. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Based on code from fs/super.c, copyright Linus Torvalds and others. 81da177e4SLinus Torvalds * Heavily rewritten. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/syscalls.h> 12d10577a8SAl Viro #include <linux/export.h> 1316f7e0feSRandy Dunlap #include <linux/capability.h> 146b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 15771b1371SEric W. Biederman #include <linux/user_namespace.h> 161da177e4SLinus Torvalds #include <linux/namei.h> 171da177e4SLinus Torvalds #include <linux/security.h> 185b825c3aSIngo Molnar #include <linux/cred.h> 1973cd49ecSMiklos Szeredi #include <linux/idr.h> 2057f150a5SRob Landley #include <linux/init.h> /* init_rootfs */ 21d10577a8SAl Viro #include <linux/fs_struct.h> /* get_fs_root et.al. */ 22d10577a8SAl Viro #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */ 23d10577a8SAl Viro #include <linux/uaccess.h> 240bb80f24SDavid Howells #include <linux/proc_ns.h> 2520b4fb48SLinus Torvalds #include <linux/magic.h> 260818bf27SAl Viro #include <linux/bootmem.h> 279ea459e1SAl Viro #include <linux/task_work.h> 289164bb4aSIngo Molnar #include <linux/sched/task.h> 299164bb4aSIngo Molnar 3007b20889SRam Pai #include "pnode.h" 31948730b0SAdrian Bunk #include "internal.h" 321da177e4SLinus Torvalds 33d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */ 34d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000; 35d2921684SEric W. Biederman 360818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 370818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 380818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 390818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 400818bf27SAl Viro 410818bf27SAl Viro static __initdata unsigned long mhash_entries; 420818bf27SAl Viro static int __init set_mhash_entries(char *str) 430818bf27SAl Viro { 440818bf27SAl Viro if (!str) 450818bf27SAl Viro return 0; 460818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 470818bf27SAl Viro return 1; 480818bf27SAl Viro } 490818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 500818bf27SAl Viro 510818bf27SAl Viro static __initdata unsigned long mphash_entries; 520818bf27SAl Viro static int __init set_mphash_entries(char *str) 530818bf27SAl Viro { 540818bf27SAl Viro if (!str) 550818bf27SAl Viro return 0; 560818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 570818bf27SAl Viro return 1; 580818bf27SAl Viro } 590818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 6013f14b4dSEric Dumazet 61c7999c36SAl Viro static u64 event; 6273cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 63719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 6499b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock); 65f21f6220SAl Viro static int mnt_id_start = 0; 66f21f6220SAl Viro static int mnt_group_start = 1; 675addc5ddSAl Viro 6838129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 690818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 70e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 7159aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 721da177e4SLinus Torvalds 73f87fd4c2SMiklos Szeredi /* /sys/fs */ 7400d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 7500d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 76f87fd4c2SMiklos Szeredi 7799b7db7bSNick Piggin /* 7899b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 7999b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 8099b7db7bSNick Piggin * up the tree. 8199b7db7bSNick Piggin * 8299b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 8399b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 8499b7db7bSNick Piggin */ 8548a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 8699b7db7bSNick Piggin 8738129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 901da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 910818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 920818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 930818bf27SAl Viro } 940818bf27SAl Viro 950818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 960818bf27SAl Viro { 970818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 980818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 990818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 102b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 10373cd49ecSMiklos Szeredi { 10473cd49ecSMiklos Szeredi int res; 10573cd49ecSMiklos Szeredi 10673cd49ecSMiklos Szeredi retry: 10773cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 10899b7db7bSNick Piggin spin_lock(&mnt_id_lock); 10915169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 110f21f6220SAl Viro if (!res) 11115169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 11299b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 11373cd49ecSMiklos Szeredi if (res == -EAGAIN) 11473cd49ecSMiklos Szeredi goto retry; 11573cd49ecSMiklos Szeredi 11673cd49ecSMiklos Szeredi return res; 11773cd49ecSMiklos Szeredi } 11873cd49ecSMiklos Szeredi 119b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 12073cd49ecSMiklos Szeredi { 12115169fe7SAl Viro int id = mnt->mnt_id; 12299b7db7bSNick Piggin spin_lock(&mnt_id_lock); 123f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 124f21f6220SAl Viro if (mnt_id_start > id) 125f21f6220SAl Viro mnt_id_start = id; 12699b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 12773cd49ecSMiklos Szeredi } 12873cd49ecSMiklos Szeredi 129719f5d7fSMiklos Szeredi /* 130719f5d7fSMiklos Szeredi * Allocate a new peer group ID 131719f5d7fSMiklos Szeredi * 132719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 133719f5d7fSMiklos Szeredi */ 1344b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 135719f5d7fSMiklos Szeredi { 136f21f6220SAl Viro int res; 137f21f6220SAl Viro 138719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 139719f5d7fSMiklos Szeredi return -ENOMEM; 140719f5d7fSMiklos Szeredi 141f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 142f21f6220SAl Viro mnt_group_start, 14315169fe7SAl Viro &mnt->mnt_group_id); 144f21f6220SAl Viro if (!res) 14515169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 146f21f6220SAl Viro 147f21f6220SAl Viro return res; 148719f5d7fSMiklos Szeredi } 149719f5d7fSMiklos Szeredi 150719f5d7fSMiklos Szeredi /* 151719f5d7fSMiklos Szeredi * Release a peer group ID 152719f5d7fSMiklos Szeredi */ 1534b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 154719f5d7fSMiklos Szeredi { 15515169fe7SAl Viro int id = mnt->mnt_group_id; 156f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 157f21f6220SAl Viro if (mnt_group_start > id) 158f21f6220SAl Viro mnt_group_start = id; 15915169fe7SAl Viro mnt->mnt_group_id = 0; 160719f5d7fSMiklos Szeredi } 161719f5d7fSMiklos Szeredi 162b3e19d92SNick Piggin /* 163b3e19d92SNick Piggin * vfsmount lock must be held for read 164b3e19d92SNick Piggin */ 16583adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 166b3e19d92SNick Piggin { 167b3e19d92SNick Piggin #ifdef CONFIG_SMP 16868e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 169b3e19d92SNick Piggin #else 170b3e19d92SNick Piggin preempt_disable(); 17168e8a9feSAl Viro mnt->mnt_count += n; 172b3e19d92SNick Piggin preempt_enable(); 173b3e19d92SNick Piggin #endif 174b3e19d92SNick Piggin } 175b3e19d92SNick Piggin 176b3e19d92SNick Piggin /* 177b3e19d92SNick Piggin * vfsmount lock must be held for write 178b3e19d92SNick Piggin */ 17983adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 180b3e19d92SNick Piggin { 181b3e19d92SNick Piggin #ifdef CONFIG_SMP 182f03c6599SAl Viro unsigned int count = 0; 183b3e19d92SNick Piggin int cpu; 184b3e19d92SNick Piggin 185b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 18668e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 187b3e19d92SNick Piggin } 188b3e19d92SNick Piggin 189b3e19d92SNick Piggin return count; 190b3e19d92SNick Piggin #else 19168e8a9feSAl Viro return mnt->mnt_count; 192b3e19d92SNick Piggin #endif 193b3e19d92SNick Piggin } 194b3e19d92SNick Piggin 19587b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p) 19687b95ce0SAl Viro { 19787b95ce0SAl Viro struct mount *m = container_of(p, struct mount, mnt_umount); 19887b95ce0SAl Viro dput(m->mnt_ex_mountpoint); 19987b95ce0SAl Viro pin_remove(p); 20087b95ce0SAl Viro mntput(&m->mnt); 20187b95ce0SAl Viro } 20287b95ce0SAl Viro 203b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 2041da177e4SLinus Torvalds { 205c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 206c63181e6SAl Viro if (mnt) { 20773cd49ecSMiklos Szeredi int err; 20873cd49ecSMiklos Szeredi 209c63181e6SAl Viro err = mnt_alloc_id(mnt); 21088b38782SLi Zefan if (err) 21188b38782SLi Zefan goto out_free_cache; 21288b38782SLi Zefan 21388b38782SLi Zefan if (name) { 214fcc139aeSAndrzej Hajda mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL); 215c63181e6SAl Viro if (!mnt->mnt_devname) 21688b38782SLi Zefan goto out_free_id; 21773cd49ecSMiklos Szeredi } 21873cd49ecSMiklos Szeredi 219b3e19d92SNick Piggin #ifdef CONFIG_SMP 220c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 221c63181e6SAl Viro if (!mnt->mnt_pcp) 222b3e19d92SNick Piggin goto out_free_devname; 223b3e19d92SNick Piggin 224c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 225b3e19d92SNick Piggin #else 226c63181e6SAl Viro mnt->mnt_count = 1; 227c63181e6SAl Viro mnt->mnt_writers = 0; 228b3e19d92SNick Piggin #endif 229b3e19d92SNick Piggin 23038129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 231c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 232c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 233c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 234c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 235c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 236c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 237c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2380a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 23999b19d16SEric W. Biederman INIT_LIST_HEAD(&mnt->mnt_umounting); 24087b95ce0SAl Viro init_fs_pin(&mnt->mnt_umount, drop_mountpoint); 2411da177e4SLinus Torvalds } 242c63181e6SAl Viro return mnt; 24388b38782SLi Zefan 244d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 245d3ef3d73Snpiggin@suse.de out_free_devname: 246fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 247d3ef3d73Snpiggin@suse.de #endif 24888b38782SLi Zefan out_free_id: 249c63181e6SAl Viro mnt_free_id(mnt); 25088b38782SLi Zefan out_free_cache: 251c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 25288b38782SLi Zefan return NULL; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2558366025eSDave Hansen /* 2568366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2578366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2588366025eSDave Hansen * We must keep track of when those operations start 2598366025eSDave Hansen * (for permission checks) and when they end, so that 2608366025eSDave Hansen * we can determine when writes are able to occur to 2618366025eSDave Hansen * a filesystem. 2628366025eSDave Hansen */ 2633d733633SDave Hansen /* 2643d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2653d733633SDave Hansen * @mnt: the mount to check for its write status 2663d733633SDave Hansen * 2673d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2683d733633SDave Hansen * It does not guarantee that the filesystem will stay 2693d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2703d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2713d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2723d733633SDave Hansen * r/w. 2733d733633SDave Hansen */ 2743d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2753d733633SDave Hansen { 2762e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2772e4b7fcdSDave Hansen return 1; 278bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt_sb)) 2792e4b7fcdSDave Hansen return 1; 2802e4b7fcdSDave Hansen return 0; 2813d733633SDave Hansen } 2823d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2833d733633SDave Hansen 28483adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2853d733633SDave Hansen { 286d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 28768e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 288d3ef3d73Snpiggin@suse.de #else 28968e8a9feSAl Viro mnt->mnt_writers++; 290d3ef3d73Snpiggin@suse.de #endif 2913d733633SDave Hansen } 2923d733633SDave Hansen 29383adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2943d733633SDave Hansen { 295d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 29668e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 297d3ef3d73Snpiggin@suse.de #else 29868e8a9feSAl Viro mnt->mnt_writers--; 299d3ef3d73Snpiggin@suse.de #endif 300d3ef3d73Snpiggin@suse.de } 301d3ef3d73Snpiggin@suse.de 30283adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 303d3ef3d73Snpiggin@suse.de { 304d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 305d3ef3d73Snpiggin@suse.de unsigned int count = 0; 3063d733633SDave Hansen int cpu; 3073d733633SDave Hansen 3083d733633SDave Hansen for_each_possible_cpu(cpu) { 30968e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 3103d733633SDave Hansen } 3113d733633SDave Hansen 312d3ef3d73Snpiggin@suse.de return count; 313d3ef3d73Snpiggin@suse.de #else 314d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 315d3ef3d73Snpiggin@suse.de #endif 3163d733633SDave Hansen } 3173d733633SDave Hansen 3184ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 3194ed5e82fSMiklos Szeredi { 3204ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 3214ed5e82fSMiklos Szeredi return 1; 3224ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 3234ed5e82fSMiklos Szeredi smp_rmb(); 3244ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 3254ed5e82fSMiklos Szeredi } 3264ed5e82fSMiklos Szeredi 3273d733633SDave Hansen /* 328eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 329eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 330eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 331eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3323d733633SDave Hansen */ 3338366025eSDave Hansen /** 334eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 33583adc753SAl Viro * @m: the mount on which to take a write 3368366025eSDave Hansen * 337eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 338eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 339eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 340eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 341eb04c282SJan Kara * called. This is effectively a refcount. 3428366025eSDave Hansen */ 343eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3448366025eSDave Hansen { 34583adc753SAl Viro struct mount *mnt = real_mount(m); 3463d733633SDave Hansen int ret = 0; 3473d733633SDave Hansen 348d3ef3d73Snpiggin@suse.de preempt_disable(); 349c6653a83SNick Piggin mnt_inc_writers(mnt); 350d3ef3d73Snpiggin@suse.de /* 351c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 352d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 353d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 354d3ef3d73Snpiggin@suse.de */ 355d3ef3d73Snpiggin@suse.de smp_mb(); 3561e75529eSMiao Xie while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 357d3ef3d73Snpiggin@suse.de cpu_relax(); 358d3ef3d73Snpiggin@suse.de /* 359d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 360d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 361d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 362d3ef3d73Snpiggin@suse.de */ 363d3ef3d73Snpiggin@suse.de smp_rmb(); 3644ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 365c6653a83SNick Piggin mnt_dec_writers(mnt); 3663d733633SDave Hansen ret = -EROFS; 3673d733633SDave Hansen } 368d3ef3d73Snpiggin@suse.de preempt_enable(); 369eb04c282SJan Kara 370eb04c282SJan Kara return ret; 371eb04c282SJan Kara } 372eb04c282SJan Kara 373eb04c282SJan Kara /** 374eb04c282SJan Kara * mnt_want_write - get write access to a mount 375eb04c282SJan Kara * @m: the mount on which to take a write 376eb04c282SJan Kara * 377eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 378eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 379eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 380eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 381eb04c282SJan Kara */ 382eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 383eb04c282SJan Kara { 384eb04c282SJan Kara int ret; 385eb04c282SJan Kara 386eb04c282SJan Kara sb_start_write(m->mnt_sb); 387eb04c282SJan Kara ret = __mnt_want_write(m); 388eb04c282SJan Kara if (ret) 389eb04c282SJan Kara sb_end_write(m->mnt_sb); 3903d733633SDave Hansen return ret; 3918366025eSDave Hansen } 3928366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3938366025eSDave Hansen 3948366025eSDave Hansen /** 39596029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 39696029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 39796029c4eSnpiggin@suse.de * 39896029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 39996029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 40096029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 40196029c4eSnpiggin@suse.de * on it. This allows some optimisation. 40296029c4eSnpiggin@suse.de * 40396029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 40496029c4eSnpiggin@suse.de * drop the reference. 40596029c4eSnpiggin@suse.de */ 40696029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 40796029c4eSnpiggin@suse.de { 40896029c4eSnpiggin@suse.de /* superblock may be r/o */ 40996029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 41096029c4eSnpiggin@suse.de return -EROFS; 41196029c4eSnpiggin@suse.de preempt_disable(); 41283adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 41396029c4eSnpiggin@suse.de preempt_enable(); 41496029c4eSnpiggin@suse.de return 0; 41596029c4eSnpiggin@suse.de } 41696029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 41796029c4eSnpiggin@suse.de 41896029c4eSnpiggin@suse.de /** 419eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 420eb04c282SJan Kara * @file: the file who's mount on which to take a write 421eb04c282SJan Kara * 422eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 423eb04c282SJan Kara * do some optimisations if the file is open for write already 424eb04c282SJan Kara */ 425eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 426eb04c282SJan Kara { 42783f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 428eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 429eb04c282SJan Kara else 430eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 431eb04c282SJan Kara } 432eb04c282SJan Kara 433eb04c282SJan Kara /** 43496029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 43596029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 43696029c4eSnpiggin@suse.de * 43796029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 43896029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 43996029c4eSnpiggin@suse.de */ 44096029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 44196029c4eSnpiggin@suse.de { 442eb04c282SJan Kara int ret; 443eb04c282SJan Kara 444eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 445eb04c282SJan Kara ret = __mnt_want_write_file(file); 446eb04c282SJan Kara if (ret) 447eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 448eb04c282SJan Kara return ret; 44996029c4eSnpiggin@suse.de } 45096029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 45196029c4eSnpiggin@suse.de 45296029c4eSnpiggin@suse.de /** 453eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4548366025eSDave Hansen * @mnt: the mount on which to give up write access 4558366025eSDave Hansen * 4568366025eSDave Hansen * Tells the low-level filesystem that we are done 4578366025eSDave Hansen * performing writes to it. Must be matched with 458eb04c282SJan Kara * __mnt_want_write() call above. 4598366025eSDave Hansen */ 460eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4618366025eSDave Hansen { 462d3ef3d73Snpiggin@suse.de preempt_disable(); 46383adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 464d3ef3d73Snpiggin@suse.de preempt_enable(); 4658366025eSDave Hansen } 466eb04c282SJan Kara 467eb04c282SJan Kara /** 468eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 469eb04c282SJan Kara * @mnt: the mount on which to give up write access 470eb04c282SJan Kara * 471eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 472eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 473eb04c282SJan Kara * mnt_want_write() call above. 474eb04c282SJan Kara */ 475eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 476eb04c282SJan Kara { 477eb04c282SJan Kara __mnt_drop_write(mnt); 478eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 479eb04c282SJan Kara } 4808366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4818366025eSDave Hansen 482eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 483eb04c282SJan Kara { 484eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 485eb04c282SJan Kara } 486eb04c282SJan Kara 4872a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4882a79f17eSAl Viro { 4892a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4902a79f17eSAl Viro } 4912a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4922a79f17eSAl Viro 49383adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4948366025eSDave Hansen { 4953d733633SDave Hansen int ret = 0; 4963d733633SDave Hansen 497719ea2fbSAl Viro lock_mount_hash(); 49883adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 499d3ef3d73Snpiggin@suse.de /* 500d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 501d3ef3d73Snpiggin@suse.de * should be visible before we do. 502d3ef3d73Snpiggin@suse.de */ 503d3ef3d73Snpiggin@suse.de smp_mb(); 504d3ef3d73Snpiggin@suse.de 505d3ef3d73Snpiggin@suse.de /* 506d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 507d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 508d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 509d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 510d3ef3d73Snpiggin@suse.de * 511d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 512d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 513d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 514d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 515d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 516d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 517d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 518d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 519d3ef3d73Snpiggin@suse.de * we're counting up here. 520d3ef3d73Snpiggin@suse.de */ 521c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 522d3ef3d73Snpiggin@suse.de ret = -EBUSY; 523d3ef3d73Snpiggin@suse.de else 52483adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 525d3ef3d73Snpiggin@suse.de /* 526d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 527d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 528d3ef3d73Snpiggin@suse.de */ 529d3ef3d73Snpiggin@suse.de smp_wmb(); 53083adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 531719ea2fbSAl Viro unlock_mount_hash(); 5323d733633SDave Hansen return ret; 5333d733633SDave Hansen } 5348366025eSDave Hansen 53583adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 5362e4b7fcdSDave Hansen { 537719ea2fbSAl Viro lock_mount_hash(); 53883adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 539719ea2fbSAl Viro unlock_mount_hash(); 5402e4b7fcdSDave Hansen } 5412e4b7fcdSDave Hansen 5424ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5434ed5e82fSMiklos Szeredi { 5444ed5e82fSMiklos Szeredi struct mount *mnt; 5454ed5e82fSMiklos Szeredi int err = 0; 5464ed5e82fSMiklos Szeredi 5478e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5488e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5498e8b8796SMiklos Szeredi return -EBUSY; 5508e8b8796SMiklos Szeredi 551719ea2fbSAl Viro lock_mount_hash(); 5524ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5534ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5544ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5554ed5e82fSMiklos Szeredi smp_mb(); 5564ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5574ed5e82fSMiklos Szeredi err = -EBUSY; 5584ed5e82fSMiklos Szeredi break; 5594ed5e82fSMiklos Szeredi } 5604ed5e82fSMiklos Szeredi } 5614ed5e82fSMiklos Szeredi } 5628e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5638e8b8796SMiklos Szeredi err = -EBUSY; 5648e8b8796SMiklos Szeredi 5654ed5e82fSMiklos Szeredi if (!err) { 5664ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5674ed5e82fSMiklos Szeredi smp_wmb(); 5684ed5e82fSMiklos Szeredi } 5694ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5704ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5714ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5724ed5e82fSMiklos Szeredi } 573719ea2fbSAl Viro unlock_mount_hash(); 5744ed5e82fSMiklos Szeredi 5754ed5e82fSMiklos Szeredi return err; 5764ed5e82fSMiklos Szeredi } 5774ed5e82fSMiklos Szeredi 578b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5791da177e4SLinus Torvalds { 580fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 581d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 58268e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 583d3ef3d73Snpiggin@suse.de #endif 584b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5878ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5888ffcb32eSDavid Howells { 5898ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5908ffcb32eSDavid Howells } 5918ffcb32eSDavid Howells 59248a066e7SAl Viro /* call under rcu_read_lock */ 593294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 59448a066e7SAl Viro { 59548a066e7SAl Viro struct mount *mnt; 59648a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 597294d71ffSAl Viro return 1; 59848a066e7SAl Viro if (bastard == NULL) 599294d71ffSAl Viro return 0; 60048a066e7SAl Viro mnt = real_mount(bastard); 60148a066e7SAl Viro mnt_add_count(mnt, 1); 60248a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 603294d71ffSAl Viro return 0; 60448a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 60548a066e7SAl Viro mnt_add_count(mnt, -1); 606294d71ffSAl Viro return 1; 60748a066e7SAl Viro } 608294d71ffSAl Viro return -1; 609294d71ffSAl Viro } 610294d71ffSAl Viro 611294d71ffSAl Viro /* call under rcu_read_lock */ 612294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 613294d71ffSAl Viro { 614294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 615294d71ffSAl Viro if (likely(!res)) 616294d71ffSAl Viro return true; 617294d71ffSAl Viro if (unlikely(res < 0)) { 61848a066e7SAl Viro rcu_read_unlock(); 61948a066e7SAl Viro mntput(bastard); 62048a066e7SAl Viro rcu_read_lock(); 621294d71ffSAl Viro } 62248a066e7SAl Viro return false; 62348a066e7SAl Viro } 62448a066e7SAl Viro 6251da177e4SLinus Torvalds /* 626474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 62748a066e7SAl Viro * call under rcu_read_lock() 6281da177e4SLinus Torvalds */ 629474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6301da177e4SLinus Torvalds { 63138129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 632474279dcSAl Viro struct mount *p; 6331da177e4SLinus Torvalds 63438129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 635474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 636474279dcSAl Viro return p; 637474279dcSAl Viro return NULL; 6381da177e4SLinus Torvalds } 639474279dcSAl Viro 640474279dcSAl Viro /* 641f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 642f015f126SDavid Howells * 643f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 644f015f126SDavid Howells * following mounts: 645f015f126SDavid Howells * 646f015f126SDavid Howells * mount /dev/sda1 /mnt 647f015f126SDavid Howells * mount /dev/sda2 /mnt 648f015f126SDavid Howells * mount /dev/sda3 /mnt 649f015f126SDavid Howells * 650f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 651f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 652f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 653f015f126SDavid Howells * 654f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 655a05964f3SRam Pai */ 656ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 657a05964f3SRam Pai { 658c7105365SAl Viro struct mount *child_mnt; 65948a066e7SAl Viro struct vfsmount *m; 66048a066e7SAl Viro unsigned seq; 66199b7db7bSNick Piggin 66248a066e7SAl Viro rcu_read_lock(); 66348a066e7SAl Viro do { 66448a066e7SAl Viro seq = read_seqbegin(&mount_lock); 665474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 66648a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 66748a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 66848a066e7SAl Viro rcu_read_unlock(); 66948a066e7SAl Viro return m; 670a05964f3SRam Pai } 671a05964f3SRam Pai 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 if (!d_mountpoint(dentry)) 6947af1364fSEric W. Biederman goto out; 6957af1364fSEric W. Biederman 6967af1364fSEric W. Biederman down_read(&namespace_sem); 6977af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 6987af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 6997af1364fSEric W. Biederman if (is_covered) 7007af1364fSEric W. Biederman break; 7017af1364fSEric W. Biederman } 7027af1364fSEric W. Biederman up_read(&namespace_sem); 7037af1364fSEric W. Biederman out: 7047af1364fSEric W. Biederman return is_covered; 7057af1364fSEric W. Biederman } 7067af1364fSEric W. Biederman 707e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 70884d17192SAl Viro { 7090818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 71084d17192SAl Viro struct mountpoint *mp; 71184d17192SAl Viro 7120818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 71384d17192SAl Viro if (mp->m_dentry == dentry) { 71484d17192SAl Viro /* might be worth a WARN_ON() */ 71584d17192SAl Viro if (d_unlinked(dentry)) 71684d17192SAl Viro return ERR_PTR(-ENOENT); 71784d17192SAl Viro mp->m_count++; 71884d17192SAl Viro return mp; 71984d17192SAl Viro } 72084d17192SAl Viro } 721e2dfa935SEric W. Biederman return NULL; 722e2dfa935SEric W. Biederman } 723e2dfa935SEric W. Biederman 7243895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 725e2dfa935SEric W. Biederman { 7263895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 727e2dfa935SEric W. Biederman int ret; 72884d17192SAl Viro 7293895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7303895dbf8SEric W. Biederman mountpoint: 7313895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7323895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 7333895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7343895dbf8SEric W. Biederman if (mp) 7353895dbf8SEric W. Biederman goto done; 73684d17192SAl Viro } 737eed81007SMiklos Szeredi 7383895dbf8SEric W. Biederman if (!new) 7393895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 7403895dbf8SEric W. Biederman if (!new) 7413895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 7423895dbf8SEric W. Biederman 7433895dbf8SEric W. Biederman 7443895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 7453895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 7463895dbf8SEric W. Biederman 7473895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 7483895dbf8SEric W. Biederman if (ret == -EBUSY) 7493895dbf8SEric W. Biederman goto mountpoint; 7503895dbf8SEric W. Biederman 7513895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 7523895dbf8SEric W. Biederman mp = ERR_PTR(ret); 7533895dbf8SEric W. Biederman if (ret) 7543895dbf8SEric W. Biederman goto done; 7553895dbf8SEric W. Biederman 7563895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 7573895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7583895dbf8SEric W. Biederman new->m_dentry = dentry; 7593895dbf8SEric W. Biederman new->m_count = 1; 7603895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 7613895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 7623895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7633895dbf8SEric W. Biederman 7643895dbf8SEric W. Biederman mp = new; 7653895dbf8SEric W. Biederman new = NULL; 7663895dbf8SEric W. Biederman done: 7673895dbf8SEric W. Biederman kfree(new); 76884d17192SAl Viro return mp; 76984d17192SAl Viro } 77084d17192SAl Viro 77184d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 77284d17192SAl Viro { 77384d17192SAl Viro if (!--mp->m_count) { 77484d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7750a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 77684d17192SAl Viro spin_lock(&dentry->d_lock); 77784d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 77884d17192SAl Viro spin_unlock(&dentry->d_lock); 7790818bf27SAl Viro hlist_del(&mp->m_hash); 78084d17192SAl Viro kfree(mp); 78184d17192SAl Viro } 78284d17192SAl Viro } 78384d17192SAl Viro 784143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7851da177e4SLinus Torvalds { 7866b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 78999b7db7bSNick Piggin /* 79099b7db7bSNick Piggin * vfsmount lock must be held for write 79199b7db7bSNick Piggin */ 7926b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7935addc5ddSAl Viro { 7945addc5ddSAl Viro if (ns) { 7955addc5ddSAl Viro ns->event = ++event; 7965addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7975addc5ddSAl Viro } 7985addc5ddSAl Viro } 7995addc5ddSAl Viro 80099b7db7bSNick Piggin /* 80199b7db7bSNick Piggin * vfsmount lock must be held for write 80299b7db7bSNick Piggin */ 8036b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 8045addc5ddSAl Viro { 8055addc5ddSAl Viro if (ns && ns->event != event) { 8065addc5ddSAl Viro ns->event = event; 8075addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8085addc5ddSAl Viro } 8095addc5ddSAl Viro } 8105addc5ddSAl Viro 81199b7db7bSNick Piggin /* 81299b7db7bSNick Piggin * vfsmount lock must be held for write 81399b7db7bSNick Piggin */ 8147bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt) 8151da177e4SLinus Torvalds { 8160714a533SAl Viro mnt->mnt_parent = mnt; 817a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8186b41d536SAl Viro list_del_init(&mnt->mnt_child); 81938129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8200a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 82184d17192SAl Viro put_mountpoint(mnt->mnt_mp); 82284d17192SAl Viro mnt->mnt_mp = NULL; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 82599b7db7bSNick Piggin /* 82699b7db7bSNick Piggin * vfsmount lock must be held for write 82799b7db7bSNick Piggin */ 8287bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path) 8297bdb11deSEric W. Biederman { 8307bdb11deSEric W. Biederman old_path->dentry = mnt->mnt_mountpoint; 8317bdb11deSEric W. Biederman old_path->mnt = &mnt->mnt_parent->mnt; 8327bdb11deSEric W. Biederman unhash_mnt(mnt); 8337bdb11deSEric W. Biederman } 8347bdb11deSEric W. Biederman 8357bdb11deSEric W. Biederman /* 8367bdb11deSEric W. Biederman * vfsmount lock must be held for write 8377bdb11deSEric W. Biederman */ 8386a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 8396a46c573SEric W. Biederman { 8406a46c573SEric W. Biederman /* old mountpoint will be dropped when we can do that */ 8416a46c573SEric W. Biederman mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint; 8426a46c573SEric W. Biederman unhash_mnt(mnt); 8436a46c573SEric W. Biederman } 8446a46c573SEric W. Biederman 8456a46c573SEric W. Biederman /* 8466a46c573SEric W. Biederman * vfsmount lock must be held for write 8476a46c573SEric W. Biederman */ 84884d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 84984d17192SAl Viro struct mountpoint *mp, 85044d964d6SAl Viro struct mount *child_mnt) 851b90fa9aeSRam Pai { 85284d17192SAl Viro mp->m_count++; 8533a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 85484d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 8553a2393d7SAl Viro child_mnt->mnt_parent = mnt; 85684d17192SAl Viro child_mnt->mnt_mp = mp; 8570a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 858b90fa9aeSRam Pai } 859b90fa9aeSRam Pai 8601064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 8611064f874SEric W. Biederman { 8621064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 8631064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8641064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8651064f874SEric W. Biederman } 8661064f874SEric W. Biederman 86799b7db7bSNick Piggin /* 86899b7db7bSNick Piggin * vfsmount lock must be held for write 86999b7db7bSNick Piggin */ 87084d17192SAl Viro static void attach_mnt(struct mount *mnt, 87184d17192SAl Viro struct mount *parent, 87284d17192SAl Viro struct mountpoint *mp) 8731da177e4SLinus Torvalds { 87484d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 8751064f874SEric W. Biederman __attach_mnt(mnt, parent); 876b90fa9aeSRam Pai } 877b90fa9aeSRam Pai 8781064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 87912a5b529SAl Viro { 8801064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 8811064f874SEric W. Biederman struct dentry *old_mountpoint = mnt->mnt_mountpoint; 8821064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 8831064f874SEric W. Biederman 8841064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 8851064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 8861064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 8871064f874SEric W. Biederman 8881064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 8891064f874SEric W. Biederman 8901064f874SEric W. Biederman put_mountpoint(old_mp); 8911064f874SEric W. Biederman 8921064f874SEric W. Biederman /* 8931064f874SEric W. Biederman * Safely avoid even the suggestion this code might sleep or 8941064f874SEric W. Biederman * lock the mount hash by taking advantage of the knowledge that 8951064f874SEric W. Biederman * mnt_change_mountpoint will not release the final reference 8961064f874SEric W. Biederman * to a mountpoint. 8971064f874SEric W. Biederman * 8981064f874SEric W. Biederman * During mounting, the mount passed in as the parent mount will 8991064f874SEric W. Biederman * continue to use the old mountpoint and during unmounting, the 9001064f874SEric W. Biederman * old mountpoint will continue to exist until namespace_unlock, 9011064f874SEric W. Biederman * which happens well after mnt_change_mountpoint. 9021064f874SEric W. Biederman */ 9031064f874SEric W. Biederman spin_lock(&old_mountpoint->d_lock); 9041064f874SEric W. Biederman old_mountpoint->d_lockref.count--; 9051064f874SEric W. Biederman spin_unlock(&old_mountpoint->d_lock); 9061064f874SEric W. Biederman 9071064f874SEric W. Biederman mnt_add_count(old_parent, -1); 90812a5b529SAl Viro } 90912a5b529SAl Viro 910b90fa9aeSRam Pai /* 91199b7db7bSNick Piggin * vfsmount lock must be held for write 912b90fa9aeSRam Pai */ 9131064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 914b90fa9aeSRam Pai { 9150714a533SAl Viro struct mount *parent = mnt->mnt_parent; 91683adc753SAl Viro struct mount *m; 917b90fa9aeSRam Pai LIST_HEAD(head); 918143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 919b90fa9aeSRam Pai 9200714a533SAl Viro BUG_ON(parent == mnt); 921b90fa9aeSRam Pai 9221a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 923f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 924143c8c91SAl Viro m->mnt_ns = n; 925f03c6599SAl Viro 926b90fa9aeSRam Pai list_splice(&head, n->list.prev); 927b90fa9aeSRam Pai 928d2921684SEric W. Biederman n->mounts += n->pending_mounts; 929d2921684SEric W. Biederman n->pending_mounts = 0; 930d2921684SEric W. Biederman 9311064f874SEric W. Biederman __attach_mnt(mnt, parent); 9326b3286edSKirill Korotaev touch_mnt_namespace(n); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 9361da177e4SLinus Torvalds { 9376b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 9386b41d536SAl Viro if (next == &p->mnt_mounts) { 9391da177e4SLinus Torvalds while (1) { 940909b0a88SAl Viro if (p == root) 9411da177e4SLinus Torvalds return NULL; 9426b41d536SAl Viro next = p->mnt_child.next; 9436b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 9441da177e4SLinus Torvalds break; 9450714a533SAl Viro p = p->mnt_parent; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds } 9486b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 951315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 9529676f0c6SRam Pai { 9536b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 9546b41d536SAl Viro while (prev != &p->mnt_mounts) { 9556b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 9566b41d536SAl Viro prev = p->mnt_mounts.prev; 9579676f0c6SRam Pai } 9589676f0c6SRam Pai return p; 9599676f0c6SRam Pai } 9609676f0c6SRam Pai 9619d412a43SAl Viro struct vfsmount * 9629d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 9639d412a43SAl Viro { 964b105e270SAl Viro struct mount *mnt; 9659d412a43SAl Viro struct dentry *root; 9669d412a43SAl Viro 9679d412a43SAl Viro if (!type) 9689d412a43SAl Viro return ERR_PTR(-ENODEV); 9699d412a43SAl Viro 9709d412a43SAl Viro mnt = alloc_vfsmnt(name); 9719d412a43SAl Viro if (!mnt) 9729d412a43SAl Viro return ERR_PTR(-ENOMEM); 9739d412a43SAl Viro 974e462ec50SDavid Howells if (flags & SB_KERNMOUNT) 975b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9769d412a43SAl Viro 9779d412a43SAl Viro root = mount_fs(type, flags, name, data); 9789d412a43SAl Viro if (IS_ERR(root)) { 9798ffcb32eSDavid Howells mnt_free_id(mnt); 9809d412a43SAl Viro free_vfsmnt(mnt); 9819d412a43SAl Viro return ERR_CAST(root); 9829d412a43SAl Viro } 9839d412a43SAl Viro 984b105e270SAl Viro mnt->mnt.mnt_root = root; 985b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 986a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9870714a533SAl Viro mnt->mnt_parent = mnt; 988719ea2fbSAl Viro lock_mount_hash(); 98939f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 990719ea2fbSAl Viro unlock_mount_hash(); 991b105e270SAl Viro return &mnt->mnt; 9929d412a43SAl Viro } 9939d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 9949d412a43SAl Viro 99593faccbbSEric W. Biederman struct vfsmount * 99693faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 99793faccbbSEric W. Biederman const char *name, void *data) 99893faccbbSEric W. Biederman { 99993faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 100093faccbbSEric W. Biederman * through from the parent mount to the submount don't support 100193faccbbSEric W. Biederman * unprivileged mounts with submounts. 100293faccbbSEric W. Biederman */ 100393faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 100493faccbbSEric W. Biederman return ERR_PTR(-EPERM); 100593faccbbSEric W. Biederman 1006e462ec50SDavid Howells return vfs_kern_mount(type, SB_SUBMOUNT, name, data); 100793faccbbSEric W. Biederman } 100893faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 100993faccbbSEric W. Biederman 101087129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 101136341f64SRam Pai int flag) 10121da177e4SLinus Torvalds { 101387129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 1014be34d1a3SDavid Howells struct mount *mnt; 1015be34d1a3SDavid Howells int err; 10161da177e4SLinus Torvalds 1017be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1018be34d1a3SDavid Howells if (!mnt) 1019be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1020be34d1a3SDavid Howells 10217a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 102215169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1023719f5d7fSMiklos Szeredi else 102415169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1025719f5d7fSMiklos Szeredi 102615169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1027be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1028719f5d7fSMiklos Szeredi if (err) 1029719f5d7fSMiklos Szeredi goto out_free; 1030719f5d7fSMiklos Szeredi } 1031719f5d7fSMiklos Szeredi 1032f2ebb3a9SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED); 1033132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 10349566d674SEric W. Biederman if (flag & CL_UNPRIVILEGED) { 10359566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_ATIME; 10369566d674SEric W. Biederman 10379566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_READONLY) 1038132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 1039132c94e3SEric W. Biederman 10409566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NODEV) 10419566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NODEV; 10429566d674SEric W. Biederman 10439566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOSUID) 10449566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID; 10459566d674SEric W. Biederman 10469566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOEXEC) 10479566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC; 10489566d674SEric W. Biederman } 10499566d674SEric W. Biederman 10505ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 1051381cacb1SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && 1052381cacb1SEric W. Biederman (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire))) 10535ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 10545ff9d8a6SEric W. Biederman 10551da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1056b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1057b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1058a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10590714a533SAl Viro mnt->mnt_parent = mnt; 1060719ea2fbSAl Viro lock_mount_hash(); 106139f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1062719ea2fbSAl Viro unlock_mount_hash(); 1063b90fa9aeSRam Pai 10647a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 10657a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 10666776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 106732301920SAl Viro mnt->mnt_master = old; 1068fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 10698aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1070fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 10716776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1072d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 10736776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1074d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 10755235d448SAl Viro } else { 10765235d448SAl Viro CLEAR_MNT_SHARED(mnt); 10775afe0022SRam Pai } 1078b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 10790f0afb1dSAl Viro set_mnt_shared(mnt); 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 10821da177e4SLinus Torvalds * as the original if that was on one */ 108336341f64SRam Pai if (flag & CL_EXPIRE) { 10846776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 10856776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 10861da177e4SLinus Torvalds } 1087be34d1a3SDavid Howells 1088cb338d06SAl Viro return mnt; 1089719f5d7fSMiklos Szeredi 1090719f5d7fSMiklos Szeredi out_free: 10918ffcb32eSDavid Howells mnt_free_id(mnt); 1092719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1093be34d1a3SDavid Howells return ERR_PTR(err); 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10969ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 10979ea459e1SAl Viro { 10989ea459e1SAl Viro /* 10999ea459e1SAl Viro * This probably indicates that somebody messed 11009ea459e1SAl Viro * up a mnt_want/drop_write() pair. If this 11019ea459e1SAl Viro * happens, the filesystem was probably unable 11029ea459e1SAl Viro * to make r/w->r/o transitions. 11039ea459e1SAl Viro */ 11049ea459e1SAl Viro /* 11059ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 11069ea459e1SAl Viro * so mnt_get_writers() below is safe. 11079ea459e1SAl Viro */ 11089ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 11099ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 11109ea459e1SAl Viro mnt_pin_kill(mnt); 11119ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 11129ea459e1SAl Viro dput(mnt->mnt.mnt_root); 11139ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 11149ea459e1SAl Viro mnt_free_id(mnt); 11159ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11169ea459e1SAl Viro } 11179ea459e1SAl Viro 11189ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11199ea459e1SAl Viro { 11209ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11219ea459e1SAl Viro } 11229ea459e1SAl Viro 11239ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11249ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11259ea459e1SAl Viro { 11269ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 11279ea459e1SAl Viro struct llist_node *next; 11289ea459e1SAl Viro 11299ea459e1SAl Viro for (; node; node = next) { 11309ea459e1SAl Viro next = llist_next(node); 11319ea459e1SAl Viro cleanup_mnt(llist_entry(node, struct mount, mnt_llist)); 11329ea459e1SAl Viro } 11339ea459e1SAl Viro } 11349ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 11359ea459e1SAl Viro 1136900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 11377b7b1aceSAl Viro { 113848a066e7SAl Viro rcu_read_lock(); 1139aa9c0e07SAl Viro mnt_add_count(mnt, -1); 114048a066e7SAl Viro if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */ 114148a066e7SAl Viro rcu_read_unlock(); 114299b7db7bSNick Piggin return; 1143b3e19d92SNick Piggin } 1144719ea2fbSAl Viro lock_mount_hash(); 1145b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 114648a066e7SAl Viro rcu_read_unlock(); 1147719ea2fbSAl Viro unlock_mount_hash(); 114899b7db7bSNick Piggin return; 114999b7db7bSNick Piggin } 115048a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 115148a066e7SAl Viro rcu_read_unlock(); 115248a066e7SAl Viro unlock_mount_hash(); 115348a066e7SAl Viro return; 115448a066e7SAl Viro } 115548a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 115648a066e7SAl Viro rcu_read_unlock(); 1157962830dfSAndi Kleen 115839f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1159ce07d891SEric W. Biederman 1160ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1161ce07d891SEric W. Biederman struct mount *p, *tmp; 1162ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 1163ce07d891SEric W. Biederman umount_mnt(p); 1164ce07d891SEric W. Biederman } 1165ce07d891SEric W. Biederman } 1166719ea2fbSAl Viro unlock_mount_hash(); 1167649a795aSAl Viro 11689ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 11699ea459e1SAl Viro struct task_struct *task = current; 11709ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 11719ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 11729ea459e1SAl Viro if (!task_work_add(task, &mnt->mnt_rcu, true)) 11739ea459e1SAl Viro return; 11749ea459e1SAl Viro } 11759ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 11769ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 11779ea459e1SAl Viro return; 11789ea459e1SAl Viro } 11799ea459e1SAl Viro cleanup_mnt(mnt); 1180b3e19d92SNick Piggin } 1181b3e19d92SNick Piggin 1182b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1183b3e19d92SNick Piggin { 1184b3e19d92SNick Piggin if (mnt) { 1185863d684fSAl Viro struct mount *m = real_mount(mnt); 1186b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1187863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1188863d684fSAl Viro m->mnt_expiry_mark = 0; 1189863d684fSAl Viro mntput_no_expire(m); 1190b3e19d92SNick Piggin } 1191b3e19d92SNick Piggin } 1192b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1193b3e19d92SNick Piggin 1194b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1195b3e19d92SNick Piggin { 1196b3e19d92SNick Piggin if (mnt) 119783adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1198b3e19d92SNick Piggin return mnt; 1199b3e19d92SNick Piggin } 1200b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1201b3e19d92SNick Piggin 1202c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current 1203c6609c0aSIan Kent * namespace. 1204c6609c0aSIan Kent * 1205c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1206c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1207c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1208c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1209c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1210c6609c0aSIan Kent * alone. 1211c6609c0aSIan Kent */ 1212c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1213c6609c0aSIan Kent { 1214c6609c0aSIan Kent unsigned seq; 1215c6609c0aSIan Kent bool res; 1216c6609c0aSIan Kent 1217c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1218c6609c0aSIan Kent return false; 1219c6609c0aSIan Kent 1220c6609c0aSIan Kent rcu_read_lock(); 1221c6609c0aSIan Kent do { 1222c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1223c6609c0aSIan Kent res = __path_is_mountpoint(path); 1224c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1225c6609c0aSIan Kent rcu_read_unlock(); 1226c6609c0aSIan Kent 1227c6609c0aSIan Kent return res; 1228c6609c0aSIan Kent } 1229c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1230c6609c0aSIan Kent 1231ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 12327b7b1aceSAl Viro { 12333064c356SAl Viro struct mount *p; 12343064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 12353064c356SAl Viro if (IS_ERR(p)) 12363064c356SAl Viro return ERR_CAST(p); 12373064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 12383064c356SAl Viro return &p->mnt; 12397b7b1aceSAl Viro } 12401da177e4SLinus Torvalds 1241a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 12420226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 12431da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 12441da177e4SLinus Torvalds { 1245ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12461da177e4SLinus Torvalds 1247390c6843SRam Pai down_read(&namespace_sem); 1248c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1249c7999c36SAl Viro void *v = p->cached_mount; 1250c7999c36SAl Viro if (*pos == p->cached_index) 1251c7999c36SAl Viro return v; 1252c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1253c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1254c7999c36SAl Viro return p->cached_mount = v; 1255c7999c36SAl Viro } 1256c7999c36SAl Viro } 1257c7999c36SAl Viro 1258c7999c36SAl Viro p->cached_event = p->ns->event; 1259c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1260c7999c36SAl Viro p->cached_index = *pos; 1261c7999c36SAl Viro return p->cached_mount; 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 12651da177e4SLinus Torvalds { 1266ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 1267b0765fb8SPavel Emelianov 1268c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1269c7999c36SAl Viro p->cached_index = *pos; 1270c7999c36SAl Viro return p->cached_mount; 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 12741da177e4SLinus Torvalds { 1275390c6843SRam Pai up_read(&namespace_sem); 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 12780226f492SAl Viro static int m_show(struct seq_file *m, void *v) 12799f5596afSAl Viro { 1280ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12811a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 12820226f492SAl Viro return p->show(m, &r->mnt); 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 1285a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 12861da177e4SLinus Torvalds .start = m_start, 12871da177e4SLinus Torvalds .next = m_next, 12881da177e4SLinus Torvalds .stop = m_stop, 12890226f492SAl Viro .show = m_show, 1290b4629fe2SChuck Lever }; 1291a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1292b4629fe2SChuck Lever 12931da177e4SLinus Torvalds /** 12941da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 12951da177e4SLinus Torvalds * @mnt: root of mount tree 12961da177e4SLinus Torvalds * 12971da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 12981da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 12991da177e4SLinus Torvalds * busy. 13001da177e4SLinus Torvalds */ 1301909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13021da177e4SLinus Torvalds { 1303909b0a88SAl Viro struct mount *mnt = real_mount(m); 130436341f64SRam Pai int actual_refs = 0; 130536341f64SRam Pai int minimum_refs = 0; 1306315fc83eSAl Viro struct mount *p; 1307909b0a88SAl Viro BUG_ON(!m); 13081da177e4SLinus Torvalds 1309b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1310719ea2fbSAl Viro lock_mount_hash(); 1311909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 131283adc753SAl Viro actual_refs += mnt_get_count(p); 13131da177e4SLinus Torvalds minimum_refs += 2; 13141da177e4SLinus Torvalds } 1315719ea2fbSAl Viro unlock_mount_hash(); 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds if (actual_refs > minimum_refs) 13181da177e4SLinus Torvalds return 0; 1319e3474a8eSIan Kent 1320e3474a8eSIan Kent return 1; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds /** 13261da177e4SLinus Torvalds * may_umount - check if a mount point is busy 13271da177e4SLinus Torvalds * @mnt: root of mount 13281da177e4SLinus Torvalds * 13291da177e4SLinus Torvalds * This is called to check if a mount point has any 13301da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 13311da177e4SLinus Torvalds * mount has sub mounts this will return busy 13321da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 13331da177e4SLinus Torvalds * 13341da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 13351da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 13361da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 13371da177e4SLinus Torvalds */ 13381da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 13391da177e4SLinus Torvalds { 1340e3474a8eSIan Kent int ret = 1; 13418ad08d8aSAl Viro down_read(&namespace_sem); 1342719ea2fbSAl Viro lock_mount_hash(); 13431ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1344e3474a8eSIan Kent ret = 0; 1345719ea2fbSAl Viro unlock_mount_hash(); 13468ad08d8aSAl Viro up_read(&namespace_sem); 1347a05964f3SRam Pai return ret; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 13511da177e4SLinus Torvalds 135238129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1353e3197d83SAl Viro 135497216be0SAl Viro static void namespace_unlock(void) 13551da177e4SLinus Torvalds { 1356a3b3c562SEric W. Biederman struct hlist_head head; 135797216be0SAl Viro 1358a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 1359a3b3c562SEric W. Biederman 136097216be0SAl Viro up_write(&namespace_sem); 1361a3b3c562SEric W. Biederman 1362a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 136397216be0SAl Viro return; 136497216be0SAl Viro 136548a066e7SAl Viro synchronize_rcu(); 136648a066e7SAl Viro 136787b95ce0SAl Viro group_pin_kill(&head); 136870fbcdf4SRam Pai } 136970fbcdf4SRam Pai 137097216be0SAl Viro static inline void namespace_lock(void) 1371e3197d83SAl Viro { 137297216be0SAl Viro down_write(&namespace_sem); 1373e3197d83SAl Viro } 1374e3197d83SAl Viro 1375e819f152SEric W. Biederman enum umount_tree_flags { 1376e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1377e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1378e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1379e819f152SEric W. Biederman }; 1380f2d0a123SEric W. Biederman 1381f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1382f2d0a123SEric W. Biederman { 1383f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1384f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1385f2d0a123SEric W. Biederman return true; 1386f2d0a123SEric W. Biederman 1387f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1388f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1389f2d0a123SEric W. Biederman return true; 1390f2d0a123SEric W. Biederman 1391f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1392f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1393f2d0a123SEric W. Biederman * connected to mounted mounts. 1394f2d0a123SEric W. Biederman */ 1395f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1396f2d0a123SEric W. Biederman return true; 1397f2d0a123SEric W. Biederman 1398f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1399f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1400f2d0a123SEric W. Biederman return false; 1401f2d0a123SEric W. Biederman 1402f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1403f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1404f2d0a123SEric W. Biederman return false; 1405f2d0a123SEric W. Biederman 1406f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1407f2d0a123SEric W. Biederman return true; 1408f2d0a123SEric W. Biederman } 1409f2d0a123SEric W. Biederman 141099b7db7bSNick Piggin /* 141148a066e7SAl Viro * mount_lock must be held 141299b7db7bSNick Piggin * namespace_sem must be held for write 141399b7db7bSNick Piggin */ 1414e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 141570fbcdf4SRam Pai { 1416c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1417315fc83eSAl Viro struct mount *p; 141870fbcdf4SRam Pai 14195d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 14205d88457eSEric W. Biederman propagate_mount_unlock(mnt); 14215d88457eSEric W. Biederman 1422c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1423590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1424590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1425c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1426590ce4bcSEric W. Biederman } 1427c003b26fSEric W. Biederman 1428411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1429c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1430c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 143138129a13SAl Viro } 143270fbcdf4SRam Pai 1433c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1434e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 14357b8a53fdSAl Viro propagate_umount(&tmp_list); 1436a05964f3SRam Pai 1437c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1438d2921684SEric W. Biederman struct mnt_namespace *ns; 1439ce07d891SEric W. Biederman bool disconnect; 1440c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 14416776db3dSAl Viro list_del_init(&p->mnt_expire); 14421a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1443d2921684SEric W. Biederman ns = p->mnt_ns; 1444d2921684SEric W. Biederman if (ns) { 1445d2921684SEric W. Biederman ns->mounts--; 1446d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1447d2921684SEric W. Biederman } 144863d37a84SAl Viro p->mnt_ns = NULL; 1449e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 145048a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 145187b95ce0SAl Viro 1452f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1453ce07d891SEric W. Biederman 1454ce07d891SEric W. Biederman pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt, 1455ce07d891SEric W. Biederman disconnect ? &unmounted : NULL); 1456676da58dSAl Viro if (mnt_has_parent(p)) { 145781b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1458ce07d891SEric W. Biederman if (!disconnect) { 1459ce07d891SEric W. Biederman /* Don't forget about p */ 1460ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1461ce07d891SEric W. Biederman } else { 14626a46c573SEric W. Biederman umount_mnt(p); 14637c4b93d8SAl Viro } 1464ce07d891SEric W. Biederman } 14650f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 146638129a13SAl Viro } 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 1469b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1470c35038beSAl Viro 14711ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 14721da177e4SLinus Torvalds { 14731ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 14741da177e4SLinus Torvalds int retval; 14751da177e4SLinus Torvalds 14761ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 14771da177e4SLinus Torvalds if (retval) 14781da177e4SLinus Torvalds return retval; 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds /* 14811da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 14821da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 14831da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 14841da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 14851da177e4SLinus Torvalds */ 14861da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 14871ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 14881da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 14891da177e4SLinus Torvalds return -EINVAL; 14901da177e4SLinus Torvalds 1491b3e19d92SNick Piggin /* 1492b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1493b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1494b3e19d92SNick Piggin */ 1495719ea2fbSAl Viro lock_mount_hash(); 149683adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1497719ea2fbSAl Viro unlock_mount_hash(); 14981da177e4SLinus Torvalds return -EBUSY; 1499b3e19d92SNick Piggin } 1500719ea2fbSAl Viro unlock_mount_hash(); 15011da177e4SLinus Torvalds 1502863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 15031da177e4SLinus Torvalds return -EAGAIN; 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds /* 15071da177e4SLinus Torvalds * If we may have to abort operations to get out of this 15081da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 15091da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 15101da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 15111da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 15121da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 15131da177e4SLinus Torvalds * about for the moment. 15141da177e4SLinus Torvalds */ 15151da177e4SLinus Torvalds 151642faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 151742faad99SAl Viro sb->s_op->umount_begin(sb); 151842faad99SAl Viro } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds /* 15211da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 15221da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 15231da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 15241da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 15251da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 15261da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 15271da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 15281da177e4SLinus Torvalds */ 15291ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 15301da177e4SLinus Torvalds /* 15311da177e4SLinus Torvalds * Special case for "unmounting" root ... 15321da177e4SLinus Torvalds * we just try to remount it readonly. 15331da177e4SLinus Torvalds */ 1534a1480dccSAndy Lutomirski if (!capable(CAP_SYS_ADMIN)) 1535a1480dccSAndy Lutomirski return -EPERM; 15361da177e4SLinus Torvalds down_write(&sb->s_umount); 1537bc98a42cSDavid Howells if (!sb_rdonly(sb)) 1538e462ec50SDavid Howells retval = do_remount_sb(sb, SB_RDONLY, NULL, 0); 15391da177e4SLinus Torvalds up_write(&sb->s_umount); 15401da177e4SLinus Torvalds return retval; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 154397216be0SAl Viro namespace_lock(); 1544719ea2fbSAl Viro lock_mount_hash(); 15455addc5ddSAl Viro event++; 15461da177e4SLinus Torvalds 154748a066e7SAl Viro if (flags & MNT_DETACH) { 154848a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1549e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 155048a066e7SAl Viro retval = 0; 155148a066e7SAl Viro } else { 1552b54b9be7SAl Viro shrink_submounts(mnt); 15531da177e4SLinus Torvalds retval = -EBUSY; 155448a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 15551a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1556e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 15571da177e4SLinus Torvalds retval = 0; 15581da177e4SLinus Torvalds } 155948a066e7SAl Viro } 1560719ea2fbSAl Viro unlock_mount_hash(); 1561e3197d83SAl Viro namespace_unlock(); 15621da177e4SLinus Torvalds return retval; 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds /* 156680b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 156780b5dce8SEric W. Biederman * 156880b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 156980b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 157080b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 157180b5dce8SEric W. Biederman * leaking them. 157280b5dce8SEric W. Biederman * 157380b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 157480b5dce8SEric W. Biederman */ 157580b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 157680b5dce8SEric W. Biederman { 157780b5dce8SEric W. Biederman struct mountpoint *mp; 157880b5dce8SEric W. Biederman struct mount *mnt; 157980b5dce8SEric W. Biederman 158080b5dce8SEric W. Biederman namespace_lock(); 15813895dbf8SEric W. Biederman lock_mount_hash(); 158280b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1583f53e5797SEric W. Biederman if (IS_ERR_OR_NULL(mp)) 158480b5dce8SEric W. Biederman goto out_unlock; 158580b5dce8SEric W. Biederman 1586e06b933eSAndrey Ulanov event++; 158780b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 158880b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1589ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1590fe78fcc8SEric W. Biederman hlist_add_head(&mnt->mnt_umount.s_list, &unmounted); 1591fe78fcc8SEric W. Biederman umount_mnt(mnt); 1592ce07d891SEric W. Biederman } 1593e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 159480b5dce8SEric W. Biederman } 159580b5dce8SEric W. Biederman put_mountpoint(mp); 159680b5dce8SEric W. Biederman out_unlock: 15973895dbf8SEric W. Biederman unlock_mount_hash(); 159880b5dce8SEric W. Biederman namespace_unlock(); 159980b5dce8SEric W. Biederman } 160080b5dce8SEric W. Biederman 160180b5dce8SEric W. Biederman /* 16029b40bc90SAl Viro * Is the caller allowed to modify his namespace? 16039b40bc90SAl Viro */ 16049b40bc90SAl Viro static inline bool may_mount(void) 16059b40bc90SAl Viro { 16069b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 16079b40bc90SAl Viro } 16089b40bc90SAl Viro 16099e8925b6SJeff Layton static inline bool may_mandlock(void) 16109e8925b6SJeff Layton { 16119e8925b6SJeff Layton #ifndef CONFIG_MANDATORY_FILE_LOCKING 16129e8925b6SJeff Layton return false; 16139e8925b6SJeff Layton #endif 161495ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 16159e8925b6SJeff Layton } 16169e8925b6SJeff Layton 16179b40bc90SAl Viro /* 16181da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 16191da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 16201da177e4SLinus Torvalds * 16211da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 16221da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 16231da177e4SLinus Torvalds */ 16241da177e4SLinus Torvalds 1625bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 16261da177e4SLinus Torvalds { 16272d8f3038SAl Viro struct path path; 1628900148dcSAl Viro struct mount *mnt; 16291da177e4SLinus Torvalds int retval; 1630db1f05bbSMiklos Szeredi int lookup_flags = 0; 16311da177e4SLinus Torvalds 1632db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1633db1f05bbSMiklos Szeredi return -EINVAL; 1634db1f05bbSMiklos Szeredi 16359b40bc90SAl Viro if (!may_mount()) 16369b40bc90SAl Viro return -EPERM; 16379b40bc90SAl Viro 1638db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1639db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1640db1f05bbSMiklos Szeredi 1641197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 16421da177e4SLinus Torvalds if (retval) 16431da177e4SLinus Torvalds goto out; 1644900148dcSAl Viro mnt = real_mount(path.mnt); 16451da177e4SLinus Torvalds retval = -EINVAL; 16462d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 16471da177e4SLinus Torvalds goto dput_and_out; 1648143c8c91SAl Viro if (!check_mnt(mnt)) 16491da177e4SLinus Torvalds goto dput_and_out; 16505ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 16515ff9d8a6SEric W. Biederman goto dput_and_out; 1652b2f5d4dcSEric W. Biederman retval = -EPERM; 1653b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 1654b2f5d4dcSEric W. Biederman goto dput_and_out; 16551da177e4SLinus Torvalds 1656900148dcSAl Viro retval = do_umount(mnt, flags); 16571da177e4SLinus Torvalds dput_and_out: 1658429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 16592d8f3038SAl Viro dput(path.dentry); 1660900148dcSAl Viro mntput_no_expire(mnt); 16611da177e4SLinus Torvalds out: 16621da177e4SLinus Torvalds return retval; 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds /* 16681da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 16691da177e4SLinus Torvalds */ 1670bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 16711da177e4SLinus Torvalds { 16721da177e4SLinus Torvalds return sys_umount(name, 0); 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds #endif 16761da177e4SLinus Torvalds 16774ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 16788823c079SEric W. Biederman { 16794ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1680e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1681e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 16824ce5d2b1SEric W. Biederman } 16834ce5d2b1SEric W. Biederman 168458be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 168558be2825SAl Viro { 168658be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 168758be2825SAl Viro } 168858be2825SAl Viro 16894ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 16904ce5d2b1SEric W. Biederman { 16914ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 16924ce5d2b1SEric W. Biederman * mount namespace loop? 16934ce5d2b1SEric W. Biederman */ 16944ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 16954ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 16964ce5d2b1SEric W. Biederman return false; 16974ce5d2b1SEric W. Biederman 1698f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 16998823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 17008823c079SEric W. Biederman } 17018823c079SEric W. Biederman 170287129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 170336341f64SRam Pai int flag) 17041da177e4SLinus Torvalds { 170584d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 17061da177e4SLinus Torvalds 17074ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 17084ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 17094ce5d2b1SEric W. Biederman 17104ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1711be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 17129676f0c6SRam Pai 171336341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1714be34d1a3SDavid Howells if (IS_ERR(q)) 1715be34d1a3SDavid Howells return q; 1716be34d1a3SDavid Howells 1717a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds p = mnt; 17206b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1721315fc83eSAl Viro struct mount *s; 17227ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 17231da177e4SLinus Torvalds continue; 17241da177e4SLinus Torvalds 1725909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 17264ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 17274ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 17284ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 17294ce5d2b1SEric W. Biederman continue; 17304ce5d2b1SEric W. Biederman } 17314ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 17324ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 17339676f0c6SRam Pai s = skip_mnt_tree(s); 17349676f0c6SRam Pai continue; 17359676f0c6SRam Pai } 17360714a533SAl Viro while (p != s->mnt_parent) { 17370714a533SAl Viro p = p->mnt_parent; 17380714a533SAl Viro q = q->mnt_parent; 17391da177e4SLinus Torvalds } 174087129cc0SAl Viro p = s; 174184d17192SAl Viro parent = q; 174287129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1743be34d1a3SDavid Howells if (IS_ERR(q)) 1744be34d1a3SDavid Howells goto out; 1745719ea2fbSAl Viro lock_mount_hash(); 17461a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 17471064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1748719ea2fbSAl Viro unlock_mount_hash(); 17491da177e4SLinus Torvalds } 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds return res; 1752be34d1a3SDavid Howells out: 17531da177e4SLinus Torvalds if (res) { 1754719ea2fbSAl Viro lock_mount_hash(); 1755e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1756719ea2fbSAl Viro unlock_mount_hash(); 17571da177e4SLinus Torvalds } 1758be34d1a3SDavid Howells return q; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 1761be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1762be34d1a3SDavid Howells 1763ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 17648aec0809SAl Viro { 1765cb338d06SAl Viro struct mount *tree; 176697216be0SAl Viro namespace_lock(); 1767cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1768cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1769cd4a4017SEric W. Biederman else 177087129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 177187129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1772328e6d90SAl Viro namespace_unlock(); 1773be34d1a3SDavid Howells if (IS_ERR(tree)) 177452e220d3SDan Carpenter return ERR_CAST(tree); 1775be34d1a3SDavid Howells return &tree->mnt; 17768aec0809SAl Viro } 17778aec0809SAl Viro 17788aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 17798aec0809SAl Viro { 178097216be0SAl Viro namespace_lock(); 1781719ea2fbSAl Viro lock_mount_hash(); 1782e819f152SEric W. Biederman umount_tree(real_mount(mnt), UMOUNT_SYNC); 1783719ea2fbSAl Viro unlock_mount_hash(); 17843ab6abeeSAl Viro namespace_unlock(); 17858aec0809SAl Viro } 17868aec0809SAl Viro 1787c771d683SMiklos Szeredi /** 1788c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 1789c771d683SMiklos Szeredi * 1790c771d683SMiklos Szeredi * This creates a new vfsmount, which will be the clone of @path. The new will 1791c771d683SMiklos Szeredi * not be attached anywhere in the namespace and will be private (i.e. changes 1792c771d683SMiklos Szeredi * to the originating mount won't be propagated into this). 1793c771d683SMiklos Szeredi * 1794c771d683SMiklos Szeredi * Release with mntput(). 1795c771d683SMiklos Szeredi */ 1796ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1797c771d683SMiklos Szeredi { 1798c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1799c771d683SMiklos Szeredi struct mount *new_mnt; 1800c771d683SMiklos Szeredi 1801c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1802c771d683SMiklos Szeredi return ERR_PTR(-EINVAL); 1803c771d683SMiklos Szeredi 1804c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1805c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1806c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1807c771d683SMiklos Szeredi 1808c771d683SMiklos Szeredi return &new_mnt->mnt; 1809c771d683SMiklos Szeredi } 1810c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 1811c771d683SMiklos Szeredi 18121f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 18131f707137SAl Viro struct vfsmount *root) 18141f707137SAl Viro { 18151a4eeaf2SAl Viro struct mount *mnt; 18161f707137SAl Viro int res = f(root, arg); 18171f707137SAl Viro if (res) 18181f707137SAl Viro return res; 18191a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 18201a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 18211f707137SAl Viro if (res) 18221f707137SAl Viro return res; 18231f707137SAl Viro } 18241f707137SAl Viro return 0; 18251f707137SAl Viro } 18261f707137SAl Viro 18274b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1828719f5d7fSMiklos Szeredi { 1829315fc83eSAl Viro struct mount *p; 1830719f5d7fSMiklos Szeredi 1831909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1832fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 18334b8b21f4SAl Viro mnt_release_group_id(p); 1834719f5d7fSMiklos Szeredi } 1835719f5d7fSMiklos Szeredi } 1836719f5d7fSMiklos Szeredi 18374b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1838719f5d7fSMiklos Szeredi { 1839315fc83eSAl Viro struct mount *p; 1840719f5d7fSMiklos Szeredi 1841909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1842fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 18434b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1844719f5d7fSMiklos Szeredi if (err) { 18454b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1846719f5d7fSMiklos Szeredi return err; 1847719f5d7fSMiklos Szeredi } 1848719f5d7fSMiklos Szeredi } 1849719f5d7fSMiklos Szeredi } 1850719f5d7fSMiklos Szeredi 1851719f5d7fSMiklos Szeredi return 0; 1852719f5d7fSMiklos Szeredi } 1853719f5d7fSMiklos Szeredi 1854d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 1855d2921684SEric W. Biederman { 1856d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 1857d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 1858d2921684SEric W. Biederman struct mount *p; 1859d2921684SEric W. Biederman 1860d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 1861d2921684SEric W. Biederman mounts++; 1862d2921684SEric W. Biederman 1863d2921684SEric W. Biederman old = ns->mounts; 1864d2921684SEric W. Biederman pending = ns->pending_mounts; 1865d2921684SEric W. Biederman sum = old + pending; 1866d2921684SEric W. Biederman if ((old > sum) || 1867d2921684SEric W. Biederman (pending > sum) || 1868d2921684SEric W. Biederman (max < sum) || 1869d2921684SEric W. Biederman (mounts > (max - sum))) 1870d2921684SEric W. Biederman return -ENOSPC; 1871d2921684SEric W. Biederman 1872d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 1873d2921684SEric W. Biederman return 0; 1874d2921684SEric W. Biederman } 1875d2921684SEric W. Biederman 1876b90fa9aeSRam Pai /* 1877b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1878b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 187921444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 188021444403SRam Pai * store the parent mount and mountpoint dentry. 188121444403SRam Pai * (done when source_mnt is moved) 1882b90fa9aeSRam Pai * 1883b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1884b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 18859676f0c6SRam Pai * --------------------------------------------------------------------------- 1886b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 18879676f0c6SRam Pai * |************************************************************************** 18889676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 18899676f0c6SRam Pai * | dest | | | | | 18909676f0c6SRam Pai * | | | | | | | 18919676f0c6SRam Pai * | v | | | | | 18929676f0c6SRam Pai * |************************************************************************** 18939676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 18945afe0022SRam Pai * | | | | | | 18959676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 18969676f0c6SRam Pai * *************************************************************************** 1897b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1898b90fa9aeSRam Pai * destination mount. 1899b90fa9aeSRam Pai * 1900b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1901b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1902b90fa9aeSRam Pai * the peer group of the source mount. 1903b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1904b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1905b90fa9aeSRam Pai * mount. 19065afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 19075afe0022SRam Pai * of the destination mount and the cloned mount is made slave 19085afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 19095afe0022SRam Pai * is marked as 'shared and slave'. 19105afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 19115afe0022SRam Pai * source mount. 19125afe0022SRam Pai * 19139676f0c6SRam Pai * --------------------------------------------------------------------------- 191421444403SRam Pai * | MOVE MOUNT OPERATION | 19159676f0c6SRam Pai * |************************************************************************** 19169676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 19179676f0c6SRam Pai * | dest | | | | | 19189676f0c6SRam Pai * | | | | | | | 19199676f0c6SRam Pai * | v | | | | | 19209676f0c6SRam Pai * |************************************************************************** 19219676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 19225afe0022SRam Pai * | | | | | | 19239676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 19249676f0c6SRam Pai * *************************************************************************** 19255afe0022SRam Pai * 19265afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 19275afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 192821444403SRam Pai * (+*) the mount is moved to the destination. 19295afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 19305afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 19315afe0022SRam Pai * the mount is marked as 'shared and slave'. 19325afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1933b90fa9aeSRam Pai * 1934b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1935b90fa9aeSRam Pai * applied to each mount in the tree. 1936b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1937b90fa9aeSRam Pai * in allocations. 1938b90fa9aeSRam Pai */ 19390fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 194084d17192SAl Viro struct mount *dest_mnt, 194184d17192SAl Viro struct mountpoint *dest_mp, 194284d17192SAl Viro struct path *parent_path) 1943b90fa9aeSRam Pai { 194438129a13SAl Viro HLIST_HEAD(tree_list); 1945d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 19461064f874SEric W. Biederman struct mountpoint *smp; 1947315fc83eSAl Viro struct mount *child, *p; 194838129a13SAl Viro struct hlist_node *n; 1949719f5d7fSMiklos Szeredi int err; 1950b90fa9aeSRam Pai 19511064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 19521064f874SEric W. Biederman * to be tucked under other mounts. 19531064f874SEric W. Biederman */ 19541064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 19551064f874SEric W. Biederman if (IS_ERR(smp)) 19561064f874SEric W. Biederman return PTR_ERR(smp); 19571064f874SEric W. Biederman 1958d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 1959d2921684SEric W. Biederman if (!parent_path) { 1960d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 1961d2921684SEric W. Biederman if (err) 1962d2921684SEric W. Biederman goto out; 1963d2921684SEric W. Biederman } 1964d2921684SEric W. Biederman 1965fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 19660fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1967719f5d7fSMiklos Szeredi if (err) 1968719f5d7fSMiklos Szeredi goto out; 196984d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 1970f2ebb3a9SAl Viro lock_mount_hash(); 1971719f5d7fSMiklos Szeredi if (err) 1972719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1973909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 19740f0afb1dSAl Viro set_mnt_shared(p); 19750b1b901bSAl Viro } else { 19760b1b901bSAl Viro lock_mount_hash(); 1977b90fa9aeSRam Pai } 19781a390689SAl Viro if (parent_path) { 19790fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 198084d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 1981143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 198221444403SRam Pai } else { 198384d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 19841064f874SEric W. Biederman commit_tree(source_mnt); 198521444403SRam Pai } 1986b90fa9aeSRam Pai 198738129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 19881d6a32acSAl Viro struct mount *q; 198938129a13SAl Viro hlist_del_init(&child->mnt_hash); 19901064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 19911d6a32acSAl Viro child->mnt_mountpoint); 19921064f874SEric W. Biederman if (q) 19931064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 19941064f874SEric W. Biederman commit_tree(child); 1995b90fa9aeSRam Pai } 19961064f874SEric W. Biederman put_mountpoint(smp); 1997719ea2fbSAl Viro unlock_mount_hash(); 199899b7db7bSNick Piggin 1999b90fa9aeSRam Pai return 0; 2000719f5d7fSMiklos Szeredi 2001719f5d7fSMiklos Szeredi out_cleanup_ids: 2002f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2003f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2004d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2005e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2006f2ebb3a9SAl Viro } 2007f2ebb3a9SAl Viro unlock_mount_hash(); 20080fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2009719f5d7fSMiklos Szeredi out: 2010d2921684SEric W. Biederman ns->pending_mounts = 0; 20111064f874SEric W. Biederman 20121064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 20131064f874SEric W. Biederman put_mountpoint(smp); 20141064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 20151064f874SEric W. Biederman 2016719f5d7fSMiklos Szeredi return err; 2017b90fa9aeSRam Pai } 2018b90fa9aeSRam Pai 201984d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2020b12cea91SAl Viro { 2021b12cea91SAl Viro struct vfsmount *mnt; 202284d17192SAl Viro struct dentry *dentry = path->dentry; 2023b12cea91SAl Viro retry: 20245955102cSAl Viro inode_lock(dentry->d_inode); 202584d17192SAl Viro if (unlikely(cant_mount(dentry))) { 20265955102cSAl Viro inode_unlock(dentry->d_inode); 202784d17192SAl Viro return ERR_PTR(-ENOENT); 2028b12cea91SAl Viro } 202997216be0SAl Viro namespace_lock(); 2030b12cea91SAl Viro mnt = lookup_mnt(path); 203184d17192SAl Viro if (likely(!mnt)) { 20323895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 203384d17192SAl Viro if (IS_ERR(mp)) { 203497216be0SAl Viro namespace_unlock(); 20355955102cSAl Viro inode_unlock(dentry->d_inode); 203684d17192SAl Viro return mp; 203784d17192SAl Viro } 203884d17192SAl Viro return mp; 203984d17192SAl Viro } 204097216be0SAl Viro namespace_unlock(); 20415955102cSAl Viro inode_unlock(path->dentry->d_inode); 2042b12cea91SAl Viro path_put(path); 2043b12cea91SAl Viro path->mnt = mnt; 204484d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2045b12cea91SAl Viro goto retry; 2046b12cea91SAl Viro } 2047b12cea91SAl Viro 204884d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2049b12cea91SAl Viro { 205084d17192SAl Viro struct dentry *dentry = where->m_dentry; 20513895dbf8SEric W. Biederman 20523895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 205384d17192SAl Viro put_mountpoint(where); 20543895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 20553895dbf8SEric W. Biederman 2056328e6d90SAl Viro namespace_unlock(); 20575955102cSAl Viro inode_unlock(dentry->d_inode); 2058b12cea91SAl Viro } 2059b12cea91SAl Viro 206084d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 20611da177e4SLinus Torvalds { 2062e462ec50SDavid Howells if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER) 20631da177e4SLinus Torvalds return -EINVAL; 20641da177e4SLinus Torvalds 2065e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2066e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 20671da177e4SLinus Torvalds return -ENOTDIR; 20681da177e4SLinus Torvalds 206984d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds /* 20737a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 20747a2e8a8fSValerie Aurora */ 20757a2e8a8fSValerie Aurora 2076e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags) 20777a2e8a8fSValerie Aurora { 2078e462ec50SDavid Howells int type = ms_flags & ~(MS_REC | MS_SILENT); 20797a2e8a8fSValerie Aurora 20807a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 20817a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 20827a2e8a8fSValerie Aurora return 0; 20837a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 20847a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 20857a2e8a8fSValerie Aurora return 0; 20867a2e8a8fSValerie Aurora return type; 20877a2e8a8fSValerie Aurora } 20887a2e8a8fSValerie Aurora 20897a2e8a8fSValerie Aurora /* 209007b20889SRam Pai * recursively change the type of the mountpoint. 209107b20889SRam Pai */ 2092e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags) 209307b20889SRam Pai { 2094315fc83eSAl Viro struct mount *m; 20954b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 2096e462ec50SDavid Howells int recurse = ms_flags & MS_REC; 20977a2e8a8fSValerie Aurora int type; 2098719f5d7fSMiklos Szeredi int err = 0; 209907b20889SRam Pai 21002d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 210107b20889SRam Pai return -EINVAL; 210207b20889SRam Pai 2103e462ec50SDavid Howells type = flags_to_propagation_type(ms_flags); 21047a2e8a8fSValerie Aurora if (!type) 21057a2e8a8fSValerie Aurora return -EINVAL; 21067a2e8a8fSValerie Aurora 210797216be0SAl Viro namespace_lock(); 2108719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2109719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2110719f5d7fSMiklos Szeredi if (err) 2111719f5d7fSMiklos Szeredi goto out_unlock; 2112719f5d7fSMiklos Szeredi } 2113719f5d7fSMiklos Szeredi 2114719ea2fbSAl Viro lock_mount_hash(); 2115909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 21160f0afb1dSAl Viro change_mnt_propagation(m, type); 2117719ea2fbSAl Viro unlock_mount_hash(); 2118719f5d7fSMiklos Szeredi 2119719f5d7fSMiklos Szeredi out_unlock: 212097216be0SAl Viro namespace_unlock(); 2121719f5d7fSMiklos Szeredi return err; 212207b20889SRam Pai } 212307b20889SRam Pai 21245ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 21255ff9d8a6SEric W. Biederman { 21265ff9d8a6SEric W. Biederman struct mount *child; 21275ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 21285ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 21295ff9d8a6SEric W. Biederman continue; 21305ff9d8a6SEric W. Biederman 21315ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 21325ff9d8a6SEric W. Biederman return true; 21335ff9d8a6SEric W. Biederman } 21345ff9d8a6SEric W. Biederman return false; 21355ff9d8a6SEric W. Biederman } 21365ff9d8a6SEric W. Biederman 213707b20889SRam Pai /* 21381da177e4SLinus Torvalds * do loopback mount. 21391da177e4SLinus Torvalds */ 2140808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 21412dafe1c4SEric Sandeen int recurse) 21421da177e4SLinus Torvalds { 21432d92ab3cSAl Viro struct path old_path; 214484d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 214584d17192SAl Viro struct mountpoint *mp; 214657eccb83SAl Viro int err; 21471da177e4SLinus Torvalds if (!old_name || !*old_name) 21481da177e4SLinus Torvalds return -EINVAL; 2149815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 21501da177e4SLinus Torvalds if (err) 21511da177e4SLinus Torvalds return err; 21521da177e4SLinus Torvalds 21538823c079SEric W. Biederman err = -EINVAL; 21544ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 21558823c079SEric W. Biederman goto out; 21568823c079SEric W. Biederman 215784d17192SAl Viro mp = lock_mount(path); 215884d17192SAl Viro err = PTR_ERR(mp); 215984d17192SAl Viro if (IS_ERR(mp)) 21609676f0c6SRam Pai goto out; 21619676f0c6SRam Pai 216287129cc0SAl Viro old = real_mount(old_path.mnt); 216384d17192SAl Viro parent = real_mount(path->mnt); 216487129cc0SAl Viro 2165b12cea91SAl Viro err = -EINVAL; 2166fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 2167b12cea91SAl Viro goto out2; 2168b12cea91SAl Viro 2169e149ed2bSAl Viro if (!check_mnt(parent)) 2170e149ed2bSAl Viro goto out2; 2171e149ed2bSAl Viro 2172e149ed2bSAl Viro if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations) 2173b12cea91SAl Viro goto out2; 2174ccd48bc7SAl Viro 21755ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 21765ff9d8a6SEric W. Biederman goto out2; 21775ff9d8a6SEric W. Biederman 21781da177e4SLinus Torvalds if (recurse) 21794ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 21801da177e4SLinus Torvalds else 218187129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 21821da177e4SLinus Torvalds 2183be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2184be34d1a3SDavid Howells err = PTR_ERR(mnt); 2185e9c5d8a5SAndrey Vagin goto out2; 2186be34d1a3SDavid Howells } 2187ccd48bc7SAl Viro 21885ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 21895ff9d8a6SEric W. Biederman 219084d17192SAl Viro err = graft_tree(mnt, parent, mp); 21911da177e4SLinus Torvalds if (err) { 2192719ea2fbSAl Viro lock_mount_hash(); 2193e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2194719ea2fbSAl Viro unlock_mount_hash(); 21955b83d2c5SRam Pai } 2196b12cea91SAl Viro out2: 219784d17192SAl Viro unlock_mount(mp); 2198ccd48bc7SAl Viro out: 21992d92ab3cSAl Viro path_put(&old_path); 22001da177e4SLinus Torvalds return err; 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 22032e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 22042e4b7fcdSDave Hansen { 22052e4b7fcdSDave Hansen int error = 0; 22062e4b7fcdSDave Hansen int readonly_request = 0; 22072e4b7fcdSDave Hansen 22082e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 22092e4b7fcdSDave Hansen readonly_request = 1; 22102e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 22112e4b7fcdSDave Hansen return 0; 22122e4b7fcdSDave Hansen 22132e4b7fcdSDave Hansen if (readonly_request) 221483adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 22152e4b7fcdSDave Hansen else 221683adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 22172e4b7fcdSDave Hansen return error; 22182e4b7fcdSDave Hansen } 22192e4b7fcdSDave Hansen 22201da177e4SLinus Torvalds /* 22211da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 22221da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 22231da177e4SLinus Torvalds * on it - tough luck. 22241da177e4SLinus Torvalds */ 2225e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags, 2226e462ec50SDavid Howells int mnt_flags, void *data) 22271da177e4SLinus Torvalds { 22281da177e4SLinus Torvalds int err; 22292d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2230143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 22311da177e4SLinus Torvalds 2232143c8c91SAl Viro if (!check_mnt(mnt)) 22331da177e4SLinus Torvalds return -EINVAL; 22341da177e4SLinus Torvalds 22352d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 22361da177e4SLinus Torvalds return -EINVAL; 22371da177e4SLinus Torvalds 223807b64558SEric W. Biederman /* Don't allow changing of locked mnt flags. 223907b64558SEric W. Biederman * 224007b64558SEric W. Biederman * No locks need to be held here while testing the various 224107b64558SEric W. Biederman * MNT_LOCK flags because those flags can never be cleared 224207b64558SEric W. Biederman * once they are set. 224307b64558SEric W. Biederman */ 224407b64558SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) && 224507b64558SEric W. Biederman !(mnt_flags & MNT_READONLY)) { 224607b64558SEric W. Biederman return -EPERM; 224707b64558SEric W. Biederman } 22489566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) && 22499566d674SEric W. Biederman !(mnt_flags & MNT_NODEV)) { 22509566d674SEric W. Biederman return -EPERM; 22519566d674SEric W. Biederman } 22529566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) && 22539566d674SEric W. Biederman !(mnt_flags & MNT_NOSUID)) { 22549566d674SEric W. Biederman return -EPERM; 22559566d674SEric W. Biederman } 22569566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) && 22579566d674SEric W. Biederman !(mnt_flags & MNT_NOEXEC)) { 22589566d674SEric W. Biederman return -EPERM; 22599566d674SEric W. Biederman } 22609566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) && 22619566d674SEric W. Biederman ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) { 22629566d674SEric W. Biederman return -EPERM; 22639566d674SEric W. Biederman } 22649566d674SEric W. Biederman 2265ff36fe2cSEric Paris err = security_sb_remount(sb, data); 2266ff36fe2cSEric Paris if (err) 2267ff36fe2cSEric Paris return err; 2268ff36fe2cSEric Paris 22691da177e4SLinus Torvalds down_write(&sb->s_umount); 2270e462ec50SDavid Howells if (ms_flags & MS_BIND) 2271e462ec50SDavid Howells err = change_mount_flags(path->mnt, ms_flags); 227257eccb83SAl Viro else if (!capable(CAP_SYS_ADMIN)) 227357eccb83SAl Viro err = -EPERM; 22744aa98cf7SAl Viro else 2275e462ec50SDavid Howells err = do_remount_sb(sb, sb_flags, data, 0); 22767b43a79fSAl Viro if (!err) { 2277719ea2fbSAl Viro lock_mount_hash(); 2278a6138db8SEric W. Biederman mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 2279143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 2280143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2281719ea2fbSAl Viro unlock_mount_hash(); 22820e55a7ccSDan Williams } 22836339dab8SAl Viro up_write(&sb->s_umount); 22841da177e4SLinus Torvalds return err; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 2287cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 22889676f0c6SRam Pai { 2289315fc83eSAl Viro struct mount *p; 2290909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2291fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 22929676f0c6SRam Pai return 1; 22939676f0c6SRam Pai } 22949676f0c6SRam Pai return 0; 22959676f0c6SRam Pai } 22969676f0c6SRam Pai 2297808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 22981da177e4SLinus Torvalds { 22992d92ab3cSAl Viro struct path old_path, parent_path; 2300676da58dSAl Viro struct mount *p; 23010fb54e50SAl Viro struct mount *old; 230284d17192SAl Viro struct mountpoint *mp; 230357eccb83SAl Viro int err; 23041da177e4SLinus Torvalds if (!old_name || !*old_name) 23051da177e4SLinus Torvalds return -EINVAL; 23062d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 23071da177e4SLinus Torvalds if (err) 23081da177e4SLinus Torvalds return err; 23091da177e4SLinus Torvalds 231084d17192SAl Viro mp = lock_mount(path); 231184d17192SAl Viro err = PTR_ERR(mp); 231284d17192SAl Viro if (IS_ERR(mp)) 2313cc53ce53SDavid Howells goto out; 2314cc53ce53SDavid Howells 2315143c8c91SAl Viro old = real_mount(old_path.mnt); 2316fc7be130SAl Viro p = real_mount(path->mnt); 2317143c8c91SAl Viro 23181da177e4SLinus Torvalds err = -EINVAL; 2319fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 23201da177e4SLinus Torvalds goto out1; 23211da177e4SLinus Torvalds 23225ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 23235ff9d8a6SEric W. Biederman goto out1; 23245ff9d8a6SEric W. Biederman 23251da177e4SLinus Torvalds err = -EINVAL; 23262d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 232721444403SRam Pai goto out1; 23281da177e4SLinus Torvalds 2329676da58dSAl Viro if (!mnt_has_parent(old)) 233021444403SRam Pai goto out1; 23311da177e4SLinus Torvalds 2332e36cb0b8SDavid Howells if (d_is_dir(path->dentry) != 2333e36cb0b8SDavid Howells d_is_dir(old_path.dentry)) 233421444403SRam Pai goto out1; 233521444403SRam Pai /* 233621444403SRam Pai * Don't move a mount residing in a shared parent. 233721444403SRam Pai */ 2338fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 233921444403SRam Pai goto out1; 23409676f0c6SRam Pai /* 23419676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 23429676f0c6SRam Pai * mount which is shared. 23439676f0c6SRam Pai */ 2344fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 23459676f0c6SRam Pai goto out1; 23461da177e4SLinus Torvalds err = -ELOOP; 2347fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2348676da58dSAl Viro if (p == old) 234921444403SRam Pai goto out1; 23501da177e4SLinus Torvalds 235184d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 23524ac91378SJan Blunck if (err) 235321444403SRam Pai goto out1; 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 23561da177e4SLinus Torvalds * automatically */ 23576776db3dSAl Viro list_del_init(&old->mnt_expire); 23581da177e4SLinus Torvalds out1: 235984d17192SAl Viro unlock_mount(mp); 23601da177e4SLinus Torvalds out: 23611da177e4SLinus Torvalds if (!err) 23621a390689SAl Viro path_put(&parent_path); 23632d92ab3cSAl Viro path_put(&old_path); 23641da177e4SLinus Torvalds return err; 23651da177e4SLinus Torvalds } 23661da177e4SLinus Torvalds 23679d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 23689d412a43SAl Viro { 23699d412a43SAl Viro int err; 23709d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 23719d412a43SAl Viro if (subtype) { 23729d412a43SAl Viro subtype++; 23739d412a43SAl Viro err = -EINVAL; 23749d412a43SAl Viro if (!subtype[0]) 23759d412a43SAl Viro goto err; 23769d412a43SAl Viro } else 23779d412a43SAl Viro subtype = ""; 23789d412a43SAl Viro 23799d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 23809d412a43SAl Viro err = -ENOMEM; 23819d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 23829d412a43SAl Viro goto err; 23839d412a43SAl Viro return mnt; 23849d412a43SAl Viro 23859d412a43SAl Viro err: 23869d412a43SAl Viro mntput(mnt); 23879d412a43SAl Viro return ERR_PTR(err); 23889d412a43SAl Viro } 23899d412a43SAl Viro 23909d412a43SAl Viro /* 23919d412a43SAl Viro * add a mount into a namespace's mount tree 23929d412a43SAl Viro */ 239395bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 23949d412a43SAl Viro { 239584d17192SAl Viro struct mountpoint *mp; 239684d17192SAl Viro struct mount *parent; 23979d412a43SAl Viro int err; 23989d412a43SAl Viro 2399f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 24009d412a43SAl Viro 240184d17192SAl Viro mp = lock_mount(path); 240284d17192SAl Viro if (IS_ERR(mp)) 240384d17192SAl Viro return PTR_ERR(mp); 24049d412a43SAl Viro 240584d17192SAl Viro parent = real_mount(path->mnt); 24069d412a43SAl Viro err = -EINVAL; 240784d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2408156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2409156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 24109d412a43SAl Viro goto unlock; 2411156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 241284d17192SAl Viro if (!parent->mnt_ns) 2413156cacb1SAl Viro goto unlock; 2414156cacb1SAl Viro } 24159d412a43SAl Viro 24169d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 24179d412a43SAl Viro err = -EBUSY; 241895bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 24199d412a43SAl Viro path->mnt->mnt_root == path->dentry) 24209d412a43SAl Viro goto unlock; 24219d412a43SAl Viro 24229d412a43SAl Viro err = -EINVAL; 2423e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 24249d412a43SAl Viro goto unlock; 24259d412a43SAl Viro 242695bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 242784d17192SAl Viro err = graft_tree(newmnt, parent, mp); 24289d412a43SAl Viro 24299d412a43SAl Viro unlock: 243084d17192SAl Viro unlock_mount(mp); 24319d412a43SAl Viro return err; 24329d412a43SAl Viro } 2433b1e75df4SAl Viro 24348654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags); 24351b852bceSEric W. Biederman 24361da177e4SLinus Torvalds /* 24371da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 24381da177e4SLinus Torvalds * namespace's tree 24391da177e4SLinus Torvalds */ 2440e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags, 2441808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 24421da177e4SLinus Torvalds { 24430c55cfc4SEric W. Biederman struct file_system_type *type; 24441da177e4SLinus Torvalds struct vfsmount *mnt; 244515f9a3f3SAl Viro int err; 24461da177e4SLinus Torvalds 24470c55cfc4SEric W. Biederman if (!fstype) 24481da177e4SLinus Torvalds return -EINVAL; 24491da177e4SLinus Torvalds 24500c55cfc4SEric W. Biederman type = get_fs_type(fstype); 24510c55cfc4SEric W. Biederman if (!type) 24520c55cfc4SEric W. Biederman return -ENODEV; 24530c55cfc4SEric W. Biederman 2454e462ec50SDavid Howells mnt = vfs_kern_mount(type, sb_flags, name, data); 24550c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 24560c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 24570c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 24580c55cfc4SEric W. Biederman 24590c55cfc4SEric W. Biederman put_filesystem(type); 24601da177e4SLinus Torvalds if (IS_ERR(mnt)) 24611da177e4SLinus Torvalds return PTR_ERR(mnt); 24621da177e4SLinus Torvalds 24638654df4eSEric W. Biederman if (mount_too_revealing(mnt, &mnt_flags)) { 24648654df4eSEric W. Biederman mntput(mnt); 24658654df4eSEric W. Biederman return -EPERM; 24668654df4eSEric W. Biederman } 24678654df4eSEric W. Biederman 246895bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 246915f9a3f3SAl Viro if (err) 247015f9a3f3SAl Viro mntput(mnt); 247115f9a3f3SAl Viro return err; 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds 247419a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 247519a167afSAl Viro { 24766776db3dSAl Viro struct mount *mnt = real_mount(m); 247719a167afSAl Viro int err; 247819a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 247919a167afSAl Viro * expired before we get a chance to add it 248019a167afSAl Viro */ 24816776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 248219a167afSAl Viro 248319a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 248419a167afSAl Viro m->mnt_root == path->dentry) { 2485b1e75df4SAl Viro err = -ELOOP; 2486b1e75df4SAl Viro goto fail; 248719a167afSAl Viro } 248819a167afSAl Viro 248995bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2490b1e75df4SAl Viro if (!err) 2491b1e75df4SAl Viro return 0; 2492b1e75df4SAl Viro fail: 2493b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 24946776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 249597216be0SAl Viro namespace_lock(); 24966776db3dSAl Viro list_del_init(&mnt->mnt_expire); 249797216be0SAl Viro namespace_unlock(); 249819a167afSAl Viro } 2499b1e75df4SAl Viro mntput(m); 2500b1e75df4SAl Viro mntput(m); 250119a167afSAl Viro return err; 250219a167afSAl Viro } 250319a167afSAl Viro 2504ea5b778aSDavid Howells /** 2505ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2506ea5b778aSDavid Howells * @mnt: The mount to list. 2507ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2508ea5b778aSDavid Howells */ 2509ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2510ea5b778aSDavid Howells { 251197216be0SAl Viro namespace_lock(); 2512ea5b778aSDavid Howells 25136776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2514ea5b778aSDavid Howells 251597216be0SAl Viro namespace_unlock(); 2516ea5b778aSDavid Howells } 2517ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2518ea5b778aSDavid Howells 2519ea5b778aSDavid Howells /* 25201da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 25211da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 25221da177e4SLinus Torvalds * here 25231da177e4SLinus Torvalds */ 25241da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 25251da177e4SLinus Torvalds { 2526761d5c38SAl Viro struct mount *mnt, *next; 25271da177e4SLinus Torvalds LIST_HEAD(graveyard); 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds if (list_empty(mounts)) 25301da177e4SLinus Torvalds return; 25311da177e4SLinus Torvalds 253297216be0SAl Viro namespace_lock(); 2533719ea2fbSAl Viro lock_mount_hash(); 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 25361da177e4SLinus Torvalds * following criteria: 25371da177e4SLinus Torvalds * - only referenced by its parent vfsmount 25381da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 25391da177e4SLinus Torvalds * cleared by mntput()) 25401da177e4SLinus Torvalds */ 25416776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2542863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 25431ab59738SAl Viro propagate_mount_busy(mnt, 1)) 25441da177e4SLinus Torvalds continue; 25456776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 25461da177e4SLinus Torvalds } 2547bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 25486776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2549143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2550e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2551bcc5c7d2SAl Viro } 2552719ea2fbSAl Viro unlock_mount_hash(); 25533ab6abeeSAl Viro namespace_unlock(); 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds /* 25595528f911STrond Myklebust * Ripoff of 'select_parent()' 25605528f911STrond Myklebust * 25615528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 25625528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 25635528f911STrond Myklebust */ 2564692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 25655528f911STrond Myklebust { 2566692afc31SAl Viro struct mount *this_parent = parent; 25675528f911STrond Myklebust struct list_head *next; 25685528f911STrond Myklebust int found = 0; 25695528f911STrond Myklebust 25705528f911STrond Myklebust repeat: 25716b41d536SAl Viro next = this_parent->mnt_mounts.next; 25725528f911STrond Myklebust resume: 25736b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 25745528f911STrond Myklebust struct list_head *tmp = next; 25756b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 25765528f911STrond Myklebust 25775528f911STrond Myklebust next = tmp->next; 2578692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 25795528f911STrond Myklebust continue; 25805528f911STrond Myklebust /* 25815528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 25825528f911STrond Myklebust */ 25836b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 25845528f911STrond Myklebust this_parent = mnt; 25855528f911STrond Myklebust goto repeat; 25865528f911STrond Myklebust } 25875528f911STrond Myklebust 25881ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 25896776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 25905528f911STrond Myklebust found++; 25915528f911STrond Myklebust } 25925528f911STrond Myklebust } 25935528f911STrond Myklebust /* 25945528f911STrond Myklebust * All done at this level ... ascend and resume the search 25955528f911STrond Myklebust */ 25965528f911STrond Myklebust if (this_parent != parent) { 25976b41d536SAl Viro next = this_parent->mnt_child.next; 25980714a533SAl Viro this_parent = this_parent->mnt_parent; 25995528f911STrond Myklebust goto resume; 26005528f911STrond Myklebust } 26015528f911STrond Myklebust return found; 26025528f911STrond Myklebust } 26035528f911STrond Myklebust 26045528f911STrond Myklebust /* 26055528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 26065528f911STrond Myklebust * submounts of a specific parent mountpoint 260799b7db7bSNick Piggin * 260848a066e7SAl Viro * mount_lock must be held for write 26095528f911STrond Myklebust */ 2610b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 26115528f911STrond Myklebust { 26125528f911STrond Myklebust LIST_HEAD(graveyard); 2613761d5c38SAl Viro struct mount *m; 26145528f911STrond Myklebust 26155528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2616c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2617bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2618761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 26196776db3dSAl Viro mnt_expire); 2620143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2621e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2622bcc5c7d2SAl Viro } 2623bcc5c7d2SAl Viro } 26245528f911STrond Myklebust } 26255528f911STrond Myklebust 26265528f911STrond Myklebust /* 26271da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 26281da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 26291da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 26301da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 26311da177e4SLinus Torvalds */ 2632b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2633b58fed8bSRam Pai unsigned long n) 26341da177e4SLinus Torvalds { 26351da177e4SLinus Torvalds char *t = to; 26361da177e4SLinus Torvalds const char __user *f = from; 26371da177e4SLinus Torvalds char c; 26381da177e4SLinus Torvalds 26391da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 26401da177e4SLinus Torvalds return n; 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds while (n) { 26431da177e4SLinus Torvalds if (__get_user(c, f)) { 26441da177e4SLinus Torvalds memset(t, 0, n); 26451da177e4SLinus Torvalds break; 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds *t++ = c; 26481da177e4SLinus Torvalds f++; 26491da177e4SLinus Torvalds n--; 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds return n; 26521da177e4SLinus Torvalds } 26531da177e4SLinus Torvalds 2654b40ef869SAl Viro void *copy_mount_options(const void __user * data) 26551da177e4SLinus Torvalds { 26561da177e4SLinus Torvalds int i; 26571da177e4SLinus Torvalds unsigned long size; 2658b40ef869SAl Viro char *copy; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds if (!data) 2661b40ef869SAl Viro return NULL; 26621da177e4SLinus Torvalds 2663b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 2664b40ef869SAl Viro if (!copy) 2665b40ef869SAl Viro return ERR_PTR(-ENOMEM); 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds /* We only care that *some* data at the address the user 26681da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 26691da177e4SLinus Torvalds * the remainder of the page. 26701da177e4SLinus Torvalds */ 26711da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 26721da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 26731da177e4SLinus Torvalds if (size > PAGE_SIZE) 26741da177e4SLinus Torvalds size = PAGE_SIZE; 26751da177e4SLinus Torvalds 2676b40ef869SAl Viro i = size - exact_copy_from_user(copy, data, size); 26771da177e4SLinus Torvalds if (!i) { 2678b40ef869SAl Viro kfree(copy); 2679b40ef869SAl Viro return ERR_PTR(-EFAULT); 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds if (i != PAGE_SIZE) 2682b40ef869SAl Viro memset(copy + i, 0, PAGE_SIZE - i); 2683b40ef869SAl Viro return copy; 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds 2686b8850d1fSTim Gardner char *copy_mount_string(const void __user *data) 2687eca6f534SVegard Nossum { 2688b8850d1fSTim Gardner return data ? strndup_user(data, PAGE_SIZE) : NULL; 2689eca6f534SVegard Nossum } 2690eca6f534SVegard Nossum 26911da177e4SLinus Torvalds /* 26921da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 26931da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 26941da177e4SLinus Torvalds * 26951da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 26961da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 26971da177e4SLinus Torvalds * information (or be NULL). 26981da177e4SLinus Torvalds * 26991da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 27001da177e4SLinus Torvalds * When the flags word was introduced its top half was required 27011da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 27021da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 27031da177e4SLinus Torvalds * and must be discarded. 27041da177e4SLinus Torvalds */ 27055e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name, 2706808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 27071da177e4SLinus Torvalds { 27082d92ab3cSAl Viro struct path path; 2709e462ec50SDavid Howells unsigned int mnt_flags = 0, sb_flags; 27101da177e4SLinus Torvalds int retval = 0; 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds /* Discard magic */ 27131da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 27141da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 27151da177e4SLinus Torvalds 27161da177e4SLinus Torvalds /* Basic sanity checks */ 27171da177e4SLinus Torvalds if (data_page) 27181da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 27191da177e4SLinus Torvalds 2720e462ec50SDavid Howells if (flags & MS_NOUSER) 2721e462ec50SDavid Howells return -EINVAL; 2722e462ec50SDavid Howells 2723a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 27245e6123f3SSeunghun Lee retval = user_path(dir_name, &path); 2725a27ab9f2STetsuo Handa if (retval) 2726a27ab9f2STetsuo Handa return retval; 2727a27ab9f2STetsuo Handa 2728a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2729a27ab9f2STetsuo Handa type_page, flags, data_page); 27300d5cadb8SAl Viro if (!retval && !may_mount()) 27310d5cadb8SAl Viro retval = -EPERM; 2732e462ec50SDavid Howells if (!retval && (flags & SB_MANDLOCK) && !may_mandlock()) 27339e8925b6SJeff Layton retval = -EPERM; 2734a27ab9f2STetsuo Handa if (retval) 2735a27ab9f2STetsuo Handa goto dput_out; 2736a27ab9f2STetsuo Handa 2737613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2738613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 27390a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 27400a1c01c9SMatthew Garrett 27411da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 27421da177e4SLinus Torvalds if (flags & MS_NOSUID) 27431da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 27441da177e4SLinus Torvalds if (flags & MS_NODEV) 27451da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 27461da177e4SLinus Torvalds if (flags & MS_NOEXEC) 27471da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2748fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2749fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2750fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2751fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2752d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2753d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 2754e462ec50SDavid Howells if (flags & SB_RDONLY) 27552e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2756fc33a7bbSChristoph Hellwig 2757ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 2758ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 2759ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 2760ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 2761ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 2762ffbc6f0eSEric W. Biederman mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; 2763ffbc6f0eSEric W. Biederman } 2764ffbc6f0eSEric W. Biederman 2765e462ec50SDavid Howells sb_flags = flags & (SB_RDONLY | 2766e462ec50SDavid Howells SB_SYNCHRONOUS | 2767e462ec50SDavid Howells SB_MANDLOCK | 2768e462ec50SDavid Howells SB_DIRSYNC | 2769e462ec50SDavid Howells SB_SILENT | 2770e462ec50SDavid Howells SB_POSIXACL); 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2773e462ec50SDavid Howells retval = do_remount(&path, flags, sb_flags, mnt_flags, 27741da177e4SLinus Torvalds data_page); 27751da177e4SLinus Torvalds else if (flags & MS_BIND) 27762d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 27779676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 27782d92ab3cSAl Viro retval = do_change_type(&path, flags); 27791da177e4SLinus Torvalds else if (flags & MS_MOVE) 27802d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 27811da177e4SLinus Torvalds else 2782e462ec50SDavid Howells retval = do_new_mount(&path, type_page, sb_flags, mnt_flags, 27831da177e4SLinus Torvalds dev_name, data_page); 27841da177e4SLinus Torvalds dput_out: 27852d92ab3cSAl Viro path_put(&path); 27861da177e4SLinus Torvalds return retval; 27871da177e4SLinus Torvalds } 27881da177e4SLinus Torvalds 2789537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 2790537f7ccbSEric W. Biederman { 2791537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 2792537f7ccbSEric W. Biederman } 2793537f7ccbSEric W. Biederman 2794537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 2795537f7ccbSEric W. Biederman { 2796537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 2797537f7ccbSEric W. Biederman } 2798537f7ccbSEric W. Biederman 2799771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2800771b1371SEric W. Biederman { 28016344c433SAl Viro ns_free_inum(&ns->ns); 2802537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 2803771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2804771b1371SEric W. Biederman kfree(ns); 2805771b1371SEric W. Biederman } 2806771b1371SEric W. Biederman 28078823c079SEric W. Biederman /* 28088823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 28098823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 28108823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 28118823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 28128823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 28138823c079SEric W. Biederman */ 28148823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 28158823c079SEric W. Biederman 2816771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2817cf8d2c11STrond Myklebust { 2818cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2819537f7ccbSEric W. Biederman struct ucounts *ucounts; 282098f842e6SEric W. Biederman int ret; 2821cf8d2c11STrond Myklebust 2822537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 2823537f7ccbSEric W. Biederman if (!ucounts) 2824df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 2825537f7ccbSEric W. Biederman 2826cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2827537f7ccbSEric W. Biederman if (!new_ns) { 2828537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 2829cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2830537f7ccbSEric W. Biederman } 28316344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 283298f842e6SEric W. Biederman if (ret) { 283398f842e6SEric W. Biederman kfree(new_ns); 2834537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 283598f842e6SEric W. Biederman return ERR_PTR(ret); 283698f842e6SEric W. Biederman } 283733c42940SAl Viro new_ns->ns.ops = &mntns_operations; 28388823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2839cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2840cf8d2c11STrond Myklebust new_ns->root = NULL; 2841cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2842cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2843cf8d2c11STrond Myklebust new_ns->event = 0; 2844771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2845537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 2846d2921684SEric W. Biederman new_ns->mounts = 0; 2847d2921684SEric W. Biederman new_ns->pending_mounts = 0; 2848cf8d2c11STrond Myklebust return new_ns; 2849cf8d2c11STrond Myklebust } 2850cf8d2c11STrond Myklebust 28510766f788SEmese Revfy __latent_entropy 28529559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 28539559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 28541da177e4SLinus Torvalds { 28556b3286edSKirill Korotaev struct mnt_namespace *new_ns; 28567f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2857315fc83eSAl Viro struct mount *p, *q; 28589559f689SAl Viro struct mount *old; 2859cb338d06SAl Viro struct mount *new; 28607a472ef4SEric W. Biederman int copy_flags; 28611da177e4SLinus Torvalds 28629559f689SAl Viro BUG_ON(!ns); 28639559f689SAl Viro 28649559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 28659559f689SAl Viro get_mnt_ns(ns); 28669559f689SAl Viro return ns; 28679559f689SAl Viro } 28689559f689SAl Viro 28699559f689SAl Viro old = ns->root; 28709559f689SAl Viro 2871771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2872cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2873cf8d2c11STrond Myklebust return new_ns; 28741da177e4SLinus Torvalds 287597216be0SAl Viro namespace_lock(); 28761da177e4SLinus Torvalds /* First pass: copy the tree topology */ 28774ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 28789559f689SAl Viro if (user_ns != ns->user_ns) 2879132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 28807a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2881be34d1a3SDavid Howells if (IS_ERR(new)) { 2882328e6d90SAl Viro namespace_unlock(); 2883771b1371SEric W. Biederman free_mnt_ns(new_ns); 2884be34d1a3SDavid Howells return ERR_CAST(new); 28851da177e4SLinus Torvalds } 2886be08d6d2SAl Viro new_ns->root = new; 28871a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 28881da177e4SLinus Torvalds 28891da177e4SLinus Torvalds /* 28901da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 28911da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 28921da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 28931da177e4SLinus Torvalds */ 2894909b0a88SAl Viro p = old; 2895cb338d06SAl Viro q = new; 28961da177e4SLinus Torvalds while (p) { 2897143c8c91SAl Viro q->mnt_ns = new_ns; 2898d2921684SEric W. Biederman new_ns->mounts++; 28999559f689SAl Viro if (new_fs) { 29009559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 29019559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2902315fc83eSAl Viro rootmnt = &p->mnt; 29031da177e4SLinus Torvalds } 29049559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 29059559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2906315fc83eSAl Viro pwdmnt = &p->mnt; 29071da177e4SLinus Torvalds } 29081da177e4SLinus Torvalds } 2909909b0a88SAl Viro p = next_mnt(p, old); 2910909b0a88SAl Viro q = next_mnt(q, new); 29114ce5d2b1SEric W. Biederman if (!q) 29124ce5d2b1SEric W. Biederman break; 29134ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 29144ce5d2b1SEric W. Biederman p = next_mnt(p, old); 29151da177e4SLinus Torvalds } 2916328e6d90SAl Viro namespace_unlock(); 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds if (rootmnt) 2919f03c6599SAl Viro mntput(rootmnt); 29201da177e4SLinus Torvalds if (pwdmnt) 2921f03c6599SAl Viro mntput(pwdmnt); 29221da177e4SLinus Torvalds 2923741a2951SJANAK DESAI return new_ns; 2924741a2951SJANAK DESAI } 2925741a2951SJANAK DESAI 2926cf8d2c11STrond Myklebust /** 2927cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2928cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2929cf8d2c11STrond Myklebust */ 29301a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2931cf8d2c11STrond Myklebust { 2932771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2933cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 29341a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 29351a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2936be08d6d2SAl Viro new_ns->root = mnt; 2937d2921684SEric W. Biederman new_ns->mounts++; 2938b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 2939c1334495SAl Viro } else { 29401a4eeaf2SAl Viro mntput(m); 2941cf8d2c11STrond Myklebust } 2942cf8d2c11STrond Myklebust return new_ns; 2943cf8d2c11STrond Myklebust } 2944cf8d2c11STrond Myklebust 2945ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2946ea441d11SAl Viro { 2947ea441d11SAl Viro struct mnt_namespace *ns; 2948d31da0f0SAl Viro struct super_block *s; 2949ea441d11SAl Viro struct path path; 2950ea441d11SAl Viro int err; 2951ea441d11SAl Viro 2952ea441d11SAl Viro ns = create_mnt_ns(mnt); 2953ea441d11SAl Viro if (IS_ERR(ns)) 2954ea441d11SAl Viro return ERR_CAST(ns); 2955ea441d11SAl Viro 2956ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2957ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2958ea441d11SAl Viro 2959ea441d11SAl Viro put_mnt_ns(ns); 2960ea441d11SAl Viro 2961ea441d11SAl Viro if (err) 2962ea441d11SAl Viro return ERR_PTR(err); 2963ea441d11SAl Viro 2964ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2965d31da0f0SAl Viro s = path.mnt->mnt_sb; 2966d31da0f0SAl Viro atomic_inc(&s->s_active); 2967ea441d11SAl Viro mntput(path.mnt); 2968ea441d11SAl Viro /* lock the sucker */ 2969d31da0f0SAl Viro down_write(&s->s_umount); 2970ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2971ea441d11SAl Viro return path.dentry; 2972ea441d11SAl Viro } 2973ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2974ea441d11SAl Viro 2975bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2976bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 29771da177e4SLinus Torvalds { 2978eca6f534SVegard Nossum int ret; 2979eca6f534SVegard Nossum char *kernel_type; 2980eca6f534SVegard Nossum char *kernel_dev; 2981b40ef869SAl Viro void *options; 29821da177e4SLinus Torvalds 2983b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 2984b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 2985b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 2986eca6f534SVegard Nossum goto out_type; 29871da177e4SLinus Torvalds 2988b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 2989b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 2990b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 2991eca6f534SVegard Nossum goto out_dev; 29921da177e4SLinus Torvalds 2993b40ef869SAl Viro options = copy_mount_options(data); 2994b40ef869SAl Viro ret = PTR_ERR(options); 2995b40ef869SAl Viro if (IS_ERR(options)) 2996eca6f534SVegard Nossum goto out_data; 29971da177e4SLinus Torvalds 2998b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 2999eca6f534SVegard Nossum 3000b40ef869SAl Viro kfree(options); 3001eca6f534SVegard Nossum out_data: 3002eca6f534SVegard Nossum kfree(kernel_dev); 3003eca6f534SVegard Nossum out_dev: 3004eca6f534SVegard Nossum kfree(kernel_type); 3005eca6f534SVegard Nossum out_type: 3006eca6f534SVegard Nossum return ret; 30071da177e4SLinus Torvalds } 30081da177e4SLinus Torvalds 30091da177e4SLinus Torvalds /* 3010afac7cbaSAl Viro * Return true if path is reachable from root 3011afac7cbaSAl Viro * 301248a066e7SAl Viro * namespace_sem or mount_lock is held 3013afac7cbaSAl Viro */ 3014643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3015afac7cbaSAl Viro const struct path *root) 3016afac7cbaSAl Viro { 3017643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3018a73324daSAl Viro dentry = mnt->mnt_mountpoint; 30190714a533SAl Viro mnt = mnt->mnt_parent; 3020afac7cbaSAl Viro } 3021643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3022afac7cbaSAl Viro } 3023afac7cbaSAl Viro 3024640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3025afac7cbaSAl Viro { 302625ab4c9bSYaowei Bai bool res; 302748a066e7SAl Viro read_seqlock_excl(&mount_lock); 3028643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 302948a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3030afac7cbaSAl Viro return res; 3031afac7cbaSAl Viro } 3032afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3033afac7cbaSAl Viro 3034afac7cbaSAl Viro /* 30351da177e4SLinus Torvalds * pivot_root Semantics: 30361da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 30371da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 30381da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 30391da177e4SLinus Torvalds * 30401da177e4SLinus Torvalds * Restrictions: 30411da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 30421da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 30431da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 30441da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 30451da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 30461da177e4SLinus Torvalds * 30474a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 30484a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 30494a0d11faSNeil Brown * in this situation. 30504a0d11faSNeil Brown * 30511da177e4SLinus Torvalds * Notes: 30521da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 30531da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 30541da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 30551da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 30561da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 30571da177e4SLinus Torvalds * first. 30581da177e4SLinus Torvalds */ 30593480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 30603480b257SHeiko Carstens const char __user *, put_old) 30611da177e4SLinus Torvalds { 30622d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 306384d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 306484d17192SAl Viro struct mountpoint *old_mp, *root_mp; 30651da177e4SLinus Torvalds int error; 30661da177e4SLinus Torvalds 30679b40bc90SAl Viro if (!may_mount()) 30681da177e4SLinus Torvalds return -EPERM; 30691da177e4SLinus Torvalds 30702d8f3038SAl Viro error = user_path_dir(new_root, &new); 30711da177e4SLinus Torvalds if (error) 30721da177e4SLinus Torvalds goto out0; 30731da177e4SLinus Torvalds 30742d8f3038SAl Viro error = user_path_dir(put_old, &old); 30751da177e4SLinus Torvalds if (error) 30761da177e4SLinus Torvalds goto out1; 30771da177e4SLinus Torvalds 30782d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3079b12cea91SAl Viro if (error) 3080b12cea91SAl Viro goto out2; 30811da177e4SLinus Torvalds 3082f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 308384d17192SAl Viro old_mp = lock_mount(&old); 308484d17192SAl Viro error = PTR_ERR(old_mp); 308584d17192SAl Viro if (IS_ERR(old_mp)) 3086b12cea91SAl Viro goto out3; 3087b12cea91SAl Viro 30881da177e4SLinus Torvalds error = -EINVAL; 3089419148daSAl Viro new_mnt = real_mount(new.mnt); 3090419148daSAl Viro root_mnt = real_mount(root.mnt); 309184d17192SAl Viro old_mnt = real_mount(old.mnt); 309284d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 3093fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 3094fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 3095b12cea91SAl Viro goto out4; 3096143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3097b12cea91SAl Viro goto out4; 30985ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 30995ff9d8a6SEric W. Biederman goto out4; 31001da177e4SLinus Torvalds error = -ENOENT; 3101f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3102b12cea91SAl Viro goto out4; 31031da177e4SLinus Torvalds error = -EBUSY; 310484d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3105b12cea91SAl Viro goto out4; /* loop, on the same file system */ 31061da177e4SLinus Torvalds error = -EINVAL; 31078c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3108b12cea91SAl Viro goto out4; /* not a mountpoint */ 3109676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3110b12cea91SAl Viro goto out4; /* not attached */ 311184d17192SAl Viro root_mp = root_mnt->mnt_mp; 31122d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3113b12cea91SAl Viro goto out4; /* not a mountpoint */ 3114676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3115b12cea91SAl Viro goto out4; /* not attached */ 31164ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 311784d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3118b12cea91SAl Viro goto out4; 31190d082601SEric W. Biederman /* make certain new is below the root */ 31200d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 31210d082601SEric W. Biederman goto out4; 312284d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 3123719ea2fbSAl Viro lock_mount_hash(); 3124419148daSAl Viro detach_mnt(new_mnt, &parent_path); 3125419148daSAl Viro detach_mnt(root_mnt, &root_parent); 31265ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 31275ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 31285ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 31295ff9d8a6SEric W. Biederman } 31304ac91378SJan Blunck /* mount old root on put_old */ 313184d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 31324ac91378SJan Blunck /* mount new_root on / */ 313384d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 31346b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 31354fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 31364fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 31373895dbf8SEric W. Biederman put_mountpoint(root_mp); 3138719ea2fbSAl Viro unlock_mount_hash(); 31392d8f3038SAl Viro chroot_fs_refs(&root, &new); 31401da177e4SLinus Torvalds error = 0; 3141b12cea91SAl Viro out4: 314284d17192SAl Viro unlock_mount(old_mp); 3143b12cea91SAl Viro if (!error) { 31441a390689SAl Viro path_put(&root_parent); 31451a390689SAl Viro path_put(&parent_path); 3146b12cea91SAl Viro } 3147b12cea91SAl Viro out3: 31488c3ee42eSAl Viro path_put(&root); 3149b12cea91SAl Viro out2: 31502d8f3038SAl Viro path_put(&old); 31511da177e4SLinus Torvalds out1: 31522d8f3038SAl Viro path_put(&new); 31531da177e4SLinus Torvalds out0: 31541da177e4SLinus Torvalds return error; 31551da177e4SLinus Torvalds } 31561da177e4SLinus Torvalds 31571da177e4SLinus Torvalds static void __init init_mount_tree(void) 31581da177e4SLinus Torvalds { 31591da177e4SLinus Torvalds struct vfsmount *mnt; 31606b3286edSKirill Korotaev struct mnt_namespace *ns; 3161ac748a09SJan Blunck struct path root; 31620c55cfc4SEric W. Biederman struct file_system_type *type; 31631da177e4SLinus Torvalds 31640c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 31650c55cfc4SEric W. Biederman if (!type) 31660c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 31670c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 31680c55cfc4SEric W. Biederman put_filesystem(type); 31691da177e4SLinus Torvalds if (IS_ERR(mnt)) 31701da177e4SLinus Torvalds panic("Can't create rootfs"); 3171b3e19d92SNick Piggin 31723b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 31733b22edc5STrond Myklebust if (IS_ERR(ns)) 31741da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 31751da177e4SLinus Torvalds 31766b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 31776b3286edSKirill Korotaev get_mnt_ns(ns); 31781da177e4SLinus Torvalds 3179be08d6d2SAl Viro root.mnt = mnt; 3180be08d6d2SAl Viro root.dentry = mnt->mnt_root; 3181da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 3182ac748a09SJan Blunck 3183ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 3184ac748a09SJan Blunck set_fs_root(current->fs, &root); 31851da177e4SLinus Torvalds } 31861da177e4SLinus Torvalds 318774bf17cfSDenis Cheng void __init mnt_init(void) 31881da177e4SLinus Torvalds { 318915a67dd8SRandy Dunlap int err; 31901da177e4SLinus Torvalds 31917d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 319220c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 31931da177e4SLinus Torvalds 31940818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 319538129a13SAl Viro sizeof(struct hlist_head), 31960818bf27SAl Viro mhash_entries, 19, 31973d375d78SPavel Tatashin HASH_ZERO, 31980818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 31990818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 32000818bf27SAl Viro sizeof(struct hlist_head), 32010818bf27SAl Viro mphash_entries, 19, 32023d375d78SPavel Tatashin HASH_ZERO, 32030818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 32041da177e4SLinus Torvalds 320584d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 32061da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 32071da177e4SLinus Torvalds 32084b93dc9bSTejun Heo kernfs_init(); 32094b93dc9bSTejun Heo 321015a67dd8SRandy Dunlap err = sysfs_init(); 321115a67dd8SRandy Dunlap if (err) 321215a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 32138e24eea7SHarvey Harrison __func__, err); 321400d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 321500d26666SGreg Kroah-Hartman if (!fs_kobj) 32168e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 32171da177e4SLinus Torvalds init_rootfs(); 32181da177e4SLinus Torvalds init_mount_tree(); 32191da177e4SLinus Torvalds } 32201da177e4SLinus Torvalds 3221616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 32221da177e4SLinus Torvalds { 3223d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 3224616511d0STrond Myklebust return; 32257b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3226771b1371SEric W. Biederman free_mnt_ns(ns); 32271da177e4SLinus Torvalds } 32289d412a43SAl Viro 32299d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 32309d412a43SAl Viro { 3231423e0ab0STim Chen struct vfsmount *mnt; 3232e462ec50SDavid Howells mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, data); 3233423e0ab0STim Chen if (!IS_ERR(mnt)) { 3234423e0ab0STim Chen /* 3235423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3236423e0ab0STim Chen * we unmount before file sys is unregistered 3237423e0ab0STim Chen */ 3238f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3239423e0ab0STim Chen } 3240423e0ab0STim Chen return mnt; 32419d412a43SAl Viro } 32429d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 3243423e0ab0STim Chen 3244423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3245423e0ab0STim Chen { 3246423e0ab0STim Chen /* release long term mount so mount point can be released */ 3247423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3248f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 324948a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3250423e0ab0STim Chen mntput(mnt); 3251423e0ab0STim Chen } 3252423e0ab0STim Chen } 3253423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 325402125a82SAl Viro 325502125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 325602125a82SAl Viro { 3257143c8c91SAl Viro return check_mnt(real_mount(mnt)); 325802125a82SAl Viro } 32598823c079SEric W. Biederman 32603151527eSEric W. Biederman bool current_chrooted(void) 32613151527eSEric W. Biederman { 32623151527eSEric W. Biederman /* Does the current process have a non-standard root */ 32633151527eSEric W. Biederman struct path ns_root; 32643151527eSEric W. Biederman struct path fs_root; 32653151527eSEric W. Biederman bool chrooted; 32663151527eSEric W. Biederman 32673151527eSEric W. Biederman /* Find the namespace root */ 32683151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 32693151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 32703151527eSEric W. Biederman path_get(&ns_root); 32713151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 32723151527eSEric W. Biederman ; 32733151527eSEric W. Biederman 32743151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 32753151527eSEric W. Biederman 32763151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 32773151527eSEric W. Biederman 32783151527eSEric W. Biederman path_put(&fs_root); 32793151527eSEric W. Biederman path_put(&ns_root); 32803151527eSEric W. Biederman 32813151527eSEric W. Biederman return chrooted; 32823151527eSEric W. Biederman } 32833151527eSEric W. Biederman 32848654df4eSEric W. Biederman static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new, 32858654df4eSEric W. Biederman int *new_mnt_flags) 328687a8ebd6SEric W. Biederman { 32878c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 328887a8ebd6SEric W. Biederman struct mount *mnt; 3289e51db735SEric W. Biederman bool visible = false; 329087a8ebd6SEric W. Biederman 329144bb4385SAl Viro down_read(&namespace_sem); 329287a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3293e51db735SEric W. Biederman struct mount *child; 329477b1a97dSEric W. Biederman int mnt_flags; 329577b1a97dSEric W. Biederman 32968654df4eSEric W. Biederman if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type) 3297e51db735SEric W. Biederman continue; 3298e51db735SEric W. Biederman 32997e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 33007e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 33017e96c1b0SEric W. Biederman */ 33027e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 33037e96c1b0SEric W. Biederman continue; 33047e96c1b0SEric W. Biederman 3305a1935c17SEric W. Biederman /* A local view of the mount flags */ 330677b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 330777b1a97dSEric W. Biederman 3308695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 3309bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt.mnt_sb)) 3310695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 3311695e9df0SEric W. Biederman 33128c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 33138c6cf9ccSEric W. Biederman * than the proposed new mount. 33148c6cf9ccSEric W. Biederman */ 331577b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 33168c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 33178c6cf9ccSEric W. Biederman continue; 331877b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 331977b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 33208c6cf9ccSEric W. Biederman continue; 33218c6cf9ccSEric W. Biederman 3322ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 3323ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 3324ceeb0e5dSEric W. Biederman * empty directories. 3325e51db735SEric W. Biederman */ 3326e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3327e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3328ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 3329d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 3330ceeb0e5dSEric W. Biederman continue; 33317236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 33327236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 3333e51db735SEric W. Biederman goto next; 333487a8ebd6SEric W. Biederman } 33358c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 333677b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 33378c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 3338e51db735SEric W. Biederman visible = true; 3339e51db735SEric W. Biederman goto found; 3340e51db735SEric W. Biederman next: ; 334187a8ebd6SEric W. Biederman } 3342e51db735SEric W. Biederman found: 334344bb4385SAl Viro up_read(&namespace_sem); 3344e51db735SEric W. Biederman return visible; 334587a8ebd6SEric W. Biederman } 334687a8ebd6SEric W. Biederman 33478654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags) 33488654df4eSEric W. Biederman { 3349a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 33508654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 33518654df4eSEric W. Biederman unsigned long s_iflags; 33528654df4eSEric W. Biederman 33538654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 33548654df4eSEric W. Biederman return false; 33558654df4eSEric W. Biederman 33568654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 33578654df4eSEric W. Biederman s_iflags = mnt->mnt_sb->s_iflags; 33588654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 33598654df4eSEric W. Biederman return false; 33608654df4eSEric W. Biederman 3361a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 3362a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 3363a1935c17SEric W. Biederman required_iflags); 3364a1935c17SEric W. Biederman return true; 3365a1935c17SEric W. Biederman } 3366a1935c17SEric W. Biederman 33678654df4eSEric W. Biederman return !mnt_already_visible(ns, mnt, new_mnt_flags); 33688654df4eSEric W. Biederman } 33698654df4eSEric W. Biederman 3370380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 3371380cf5baSAndy Lutomirski { 3372380cf5baSAndy Lutomirski /* 3373380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 3374380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 3375380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 3376380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 3377380cf5baSAndy Lutomirski * in other namespaces. 3378380cf5baSAndy Lutomirski */ 3379380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 3380380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 3381380cf5baSAndy Lutomirski } 3382380cf5baSAndy Lutomirski 338364964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 33848823c079SEric W. Biederman { 338558be2825SAl Viro struct ns_common *ns = NULL; 33868823c079SEric W. Biederman struct nsproxy *nsproxy; 33878823c079SEric W. Biederman 3388728dba3aSEric W. Biederman task_lock(task); 3389728dba3aSEric W. Biederman nsproxy = task->nsproxy; 33908823c079SEric W. Biederman if (nsproxy) { 339158be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 339258be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 33938823c079SEric W. Biederman } 3394728dba3aSEric W. Biederman task_unlock(task); 33958823c079SEric W. Biederman 33968823c079SEric W. Biederman return ns; 33978823c079SEric W. Biederman } 33988823c079SEric W. Biederman 339964964528SAl Viro static void mntns_put(struct ns_common *ns) 34008823c079SEric W. Biederman { 340158be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 34028823c079SEric W. Biederman } 34038823c079SEric W. Biederman 340464964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns) 34058823c079SEric W. Biederman { 34068823c079SEric W. Biederman struct fs_struct *fs = current->fs; 34074f757f3cSAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 34088823c079SEric W. Biederman struct path root; 34094f757f3cSAl Viro int err; 34108823c079SEric W. Biederman 34110c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3412c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3413c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3414ae11e0f1SZhao Hongjiang return -EPERM; 34158823c079SEric W. Biederman 34168823c079SEric W. Biederman if (fs->users != 1) 34178823c079SEric W. Biederman return -EINVAL; 34188823c079SEric W. Biederman 34198823c079SEric W. Biederman get_mnt_ns(mnt_ns); 34204f757f3cSAl Viro old_mnt_ns = nsproxy->mnt_ns; 34218823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 34228823c079SEric W. Biederman 34238823c079SEric W. Biederman /* Find the root */ 34244f757f3cSAl Viro err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 34254f757f3cSAl Viro "/", LOOKUP_DOWN, &root); 34264f757f3cSAl Viro if (err) { 34274f757f3cSAl Viro /* revert to old namespace */ 34284f757f3cSAl Viro nsproxy->mnt_ns = old_mnt_ns; 34294f757f3cSAl Viro put_mnt_ns(mnt_ns); 34304f757f3cSAl Viro return err; 34314f757f3cSAl Viro } 34328823c079SEric W. Biederman 34334068367cSAndrei Vagin put_mnt_ns(old_mnt_ns); 34344068367cSAndrei Vagin 34358823c079SEric W. Biederman /* Update the pwd and root */ 34368823c079SEric W. Biederman set_fs_pwd(fs, &root); 34378823c079SEric W. Biederman set_fs_root(fs, &root); 34388823c079SEric W. Biederman 34398823c079SEric W. Biederman path_put(&root); 34408823c079SEric W. Biederman return 0; 34418823c079SEric W. Biederman } 34428823c079SEric W. Biederman 3443bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 3444bcac25a5SAndrey Vagin { 3445bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 3446bcac25a5SAndrey Vagin } 3447bcac25a5SAndrey Vagin 34488823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 34498823c079SEric W. Biederman .name = "mnt", 34508823c079SEric W. Biederman .type = CLONE_NEWNS, 34518823c079SEric W. Biederman .get = mntns_get, 34528823c079SEric W. Biederman .put = mntns_put, 34538823c079SEric W. Biederman .install = mntns_install, 3454bcac25a5SAndrey Vagin .owner = mntns_owner, 34558823c079SEric W. Biederman }; 3456