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); 2392504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 2402504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 2412504c5d6SAndreas Gruenbacher #endif 24287b95ce0SAl Viro init_fs_pin(&mnt->mnt_umount, drop_mountpoint); 2431da177e4SLinus Torvalds } 244c63181e6SAl Viro return mnt; 24588b38782SLi Zefan 246d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 247d3ef3d73Snpiggin@suse.de out_free_devname: 248fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 249d3ef3d73Snpiggin@suse.de #endif 25088b38782SLi Zefan out_free_id: 251c63181e6SAl Viro mnt_free_id(mnt); 25288b38782SLi Zefan out_free_cache: 253c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 25488b38782SLi Zefan return NULL; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2578366025eSDave Hansen /* 2588366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2598366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2608366025eSDave Hansen * We must keep track of when those operations start 2618366025eSDave Hansen * (for permission checks) and when they end, so that 2628366025eSDave Hansen * we can determine when writes are able to occur to 2638366025eSDave Hansen * a filesystem. 2648366025eSDave Hansen */ 2653d733633SDave Hansen /* 2663d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2673d733633SDave Hansen * @mnt: the mount to check for its write status 2683d733633SDave Hansen * 2693d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2703d733633SDave Hansen * It does not guarantee that the filesystem will stay 2713d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2723d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2733d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2743d733633SDave Hansen * r/w. 2753d733633SDave Hansen */ 2763d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2773d733633SDave Hansen { 2782e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2792e4b7fcdSDave Hansen return 1; 2802e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2812e4b7fcdSDave Hansen return 1; 2822e4b7fcdSDave Hansen return 0; 2833d733633SDave Hansen } 2843d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2853d733633SDave Hansen 28683adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2873d733633SDave Hansen { 288d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 28968e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 290d3ef3d73Snpiggin@suse.de #else 29168e8a9feSAl Viro mnt->mnt_writers++; 292d3ef3d73Snpiggin@suse.de #endif 2933d733633SDave Hansen } 2943d733633SDave Hansen 29583adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2963d733633SDave Hansen { 297d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 29868e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 299d3ef3d73Snpiggin@suse.de #else 30068e8a9feSAl Viro mnt->mnt_writers--; 301d3ef3d73Snpiggin@suse.de #endif 302d3ef3d73Snpiggin@suse.de } 303d3ef3d73Snpiggin@suse.de 30483adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 305d3ef3d73Snpiggin@suse.de { 306d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 307d3ef3d73Snpiggin@suse.de unsigned int count = 0; 3083d733633SDave Hansen int cpu; 3093d733633SDave Hansen 3103d733633SDave Hansen for_each_possible_cpu(cpu) { 31168e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 3123d733633SDave Hansen } 3133d733633SDave Hansen 314d3ef3d73Snpiggin@suse.de return count; 315d3ef3d73Snpiggin@suse.de #else 316d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 317d3ef3d73Snpiggin@suse.de #endif 3183d733633SDave Hansen } 3193d733633SDave Hansen 3204ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 3214ed5e82fSMiklos Szeredi { 3224ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 3234ed5e82fSMiklos Szeredi return 1; 3244ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 3254ed5e82fSMiklos Szeredi smp_rmb(); 3264ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 3274ed5e82fSMiklos Szeredi } 3284ed5e82fSMiklos Szeredi 3293d733633SDave Hansen /* 330eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 331eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 332eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 333eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3343d733633SDave Hansen */ 3358366025eSDave Hansen /** 336eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 33783adc753SAl Viro * @m: the mount on which to take a write 3388366025eSDave Hansen * 339eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 340eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 341eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 342eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 343eb04c282SJan Kara * called. This is effectively a refcount. 3448366025eSDave Hansen */ 345eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3468366025eSDave Hansen { 34783adc753SAl Viro struct mount *mnt = real_mount(m); 3483d733633SDave Hansen int ret = 0; 3493d733633SDave Hansen 350d3ef3d73Snpiggin@suse.de preempt_disable(); 351c6653a83SNick Piggin mnt_inc_writers(mnt); 352d3ef3d73Snpiggin@suse.de /* 353c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 354d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 355d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 356d3ef3d73Snpiggin@suse.de */ 357d3ef3d73Snpiggin@suse.de smp_mb(); 3581e75529eSMiao Xie while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 359d3ef3d73Snpiggin@suse.de cpu_relax(); 360d3ef3d73Snpiggin@suse.de /* 361d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 362d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 363d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 364d3ef3d73Snpiggin@suse.de */ 365d3ef3d73Snpiggin@suse.de smp_rmb(); 3664ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 367c6653a83SNick Piggin mnt_dec_writers(mnt); 3683d733633SDave Hansen ret = -EROFS; 3693d733633SDave Hansen } 370d3ef3d73Snpiggin@suse.de preempt_enable(); 371eb04c282SJan Kara 372eb04c282SJan Kara return ret; 373eb04c282SJan Kara } 374eb04c282SJan Kara 375eb04c282SJan Kara /** 376eb04c282SJan Kara * mnt_want_write - get write access to a mount 377eb04c282SJan Kara * @m: the mount on which to take a write 378eb04c282SJan Kara * 379eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 380eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 381eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 382eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 383eb04c282SJan Kara */ 384eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 385eb04c282SJan Kara { 386eb04c282SJan Kara int ret; 387eb04c282SJan Kara 388eb04c282SJan Kara sb_start_write(m->mnt_sb); 389eb04c282SJan Kara ret = __mnt_want_write(m); 390eb04c282SJan Kara if (ret) 391eb04c282SJan Kara sb_end_write(m->mnt_sb); 3923d733633SDave Hansen return ret; 3938366025eSDave Hansen } 3948366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3958366025eSDave Hansen 3968366025eSDave Hansen /** 39796029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 39896029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 39996029c4eSnpiggin@suse.de * 40096029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 40196029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 40296029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 40396029c4eSnpiggin@suse.de * on it. This allows some optimisation. 40496029c4eSnpiggin@suse.de * 40596029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 40696029c4eSnpiggin@suse.de * drop the reference. 40796029c4eSnpiggin@suse.de */ 40896029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 40996029c4eSnpiggin@suse.de { 41096029c4eSnpiggin@suse.de /* superblock may be r/o */ 41196029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 41296029c4eSnpiggin@suse.de return -EROFS; 41396029c4eSnpiggin@suse.de preempt_disable(); 41483adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 41596029c4eSnpiggin@suse.de preempt_enable(); 41696029c4eSnpiggin@suse.de return 0; 41796029c4eSnpiggin@suse.de } 41896029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 41996029c4eSnpiggin@suse.de 42096029c4eSnpiggin@suse.de /** 421eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 422eb04c282SJan Kara * @file: the file who's mount on which to take a write 423eb04c282SJan Kara * 424eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 425eb04c282SJan Kara * do some optimisations if the file is open for write already 426eb04c282SJan Kara */ 427eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 428eb04c282SJan Kara { 42983f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 430eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 431eb04c282SJan Kara else 432eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 433eb04c282SJan Kara } 434eb04c282SJan Kara 435eb04c282SJan Kara /** 43696029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 43796029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 43896029c4eSnpiggin@suse.de * 43996029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 44096029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 44196029c4eSnpiggin@suse.de */ 44296029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 44396029c4eSnpiggin@suse.de { 444eb04c282SJan Kara int ret; 445eb04c282SJan Kara 446eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 447eb04c282SJan Kara ret = __mnt_want_write_file(file); 448eb04c282SJan Kara if (ret) 449eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 450eb04c282SJan Kara return ret; 45196029c4eSnpiggin@suse.de } 45296029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 45396029c4eSnpiggin@suse.de 45496029c4eSnpiggin@suse.de /** 455eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4568366025eSDave Hansen * @mnt: the mount on which to give up write access 4578366025eSDave Hansen * 4588366025eSDave Hansen * Tells the low-level filesystem that we are done 4598366025eSDave Hansen * performing writes to it. Must be matched with 460eb04c282SJan Kara * __mnt_want_write() call above. 4618366025eSDave Hansen */ 462eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4638366025eSDave Hansen { 464d3ef3d73Snpiggin@suse.de preempt_disable(); 46583adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 466d3ef3d73Snpiggin@suse.de preempt_enable(); 4678366025eSDave Hansen } 468eb04c282SJan Kara 469eb04c282SJan Kara /** 470eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 471eb04c282SJan Kara * @mnt: the mount on which to give up write access 472eb04c282SJan Kara * 473eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 474eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 475eb04c282SJan Kara * mnt_want_write() call above. 476eb04c282SJan Kara */ 477eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 478eb04c282SJan Kara { 479eb04c282SJan Kara __mnt_drop_write(mnt); 480eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 481eb04c282SJan Kara } 4828366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4838366025eSDave Hansen 484eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 485eb04c282SJan Kara { 486eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 487eb04c282SJan Kara } 488eb04c282SJan Kara 4892a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4902a79f17eSAl Viro { 4912a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4922a79f17eSAl Viro } 4932a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4942a79f17eSAl Viro 49583adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4968366025eSDave Hansen { 4973d733633SDave Hansen int ret = 0; 4983d733633SDave Hansen 499719ea2fbSAl Viro lock_mount_hash(); 50083adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 501d3ef3d73Snpiggin@suse.de /* 502d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 503d3ef3d73Snpiggin@suse.de * should be visible before we do. 504d3ef3d73Snpiggin@suse.de */ 505d3ef3d73Snpiggin@suse.de smp_mb(); 506d3ef3d73Snpiggin@suse.de 507d3ef3d73Snpiggin@suse.de /* 508d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 509d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 510d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 511d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 512d3ef3d73Snpiggin@suse.de * 513d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 514d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 515d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 516d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 517d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 518d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 519d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 520d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 521d3ef3d73Snpiggin@suse.de * we're counting up here. 522d3ef3d73Snpiggin@suse.de */ 523c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 524d3ef3d73Snpiggin@suse.de ret = -EBUSY; 525d3ef3d73Snpiggin@suse.de else 52683adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 527d3ef3d73Snpiggin@suse.de /* 528d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 529d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 530d3ef3d73Snpiggin@suse.de */ 531d3ef3d73Snpiggin@suse.de smp_wmb(); 53283adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 533719ea2fbSAl Viro unlock_mount_hash(); 5343d733633SDave Hansen return ret; 5353d733633SDave Hansen } 5368366025eSDave Hansen 53783adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 5382e4b7fcdSDave Hansen { 539719ea2fbSAl Viro lock_mount_hash(); 54083adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 541719ea2fbSAl Viro unlock_mount_hash(); 5422e4b7fcdSDave Hansen } 5432e4b7fcdSDave Hansen 5444ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5454ed5e82fSMiklos Szeredi { 5464ed5e82fSMiklos Szeredi struct mount *mnt; 5474ed5e82fSMiklos Szeredi int err = 0; 5484ed5e82fSMiklos Szeredi 5498e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5508e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5518e8b8796SMiklos Szeredi return -EBUSY; 5528e8b8796SMiklos Szeredi 553719ea2fbSAl Viro lock_mount_hash(); 5544ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5554ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5564ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5574ed5e82fSMiklos Szeredi smp_mb(); 5584ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5594ed5e82fSMiklos Szeredi err = -EBUSY; 5604ed5e82fSMiklos Szeredi break; 5614ed5e82fSMiklos Szeredi } 5624ed5e82fSMiklos Szeredi } 5634ed5e82fSMiklos Szeredi } 5648e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5658e8b8796SMiklos Szeredi err = -EBUSY; 5668e8b8796SMiklos Szeredi 5674ed5e82fSMiklos Szeredi if (!err) { 5684ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5694ed5e82fSMiklos Szeredi smp_wmb(); 5704ed5e82fSMiklos Szeredi } 5714ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5724ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5734ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5744ed5e82fSMiklos Szeredi } 575719ea2fbSAl Viro unlock_mount_hash(); 5764ed5e82fSMiklos Szeredi 5774ed5e82fSMiklos Szeredi return err; 5784ed5e82fSMiklos Szeredi } 5794ed5e82fSMiklos Szeredi 580b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5811da177e4SLinus Torvalds { 582fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 583d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 58468e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 585d3ef3d73Snpiggin@suse.de #endif 586b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5898ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5908ffcb32eSDavid Howells { 5918ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5928ffcb32eSDavid Howells } 5938ffcb32eSDavid Howells 59448a066e7SAl Viro /* call under rcu_read_lock */ 595294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 59648a066e7SAl Viro { 59748a066e7SAl Viro struct mount *mnt; 59848a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 599294d71ffSAl Viro return 1; 60048a066e7SAl Viro if (bastard == NULL) 601294d71ffSAl Viro return 0; 60248a066e7SAl Viro mnt = real_mount(bastard); 60348a066e7SAl Viro mnt_add_count(mnt, 1); 60448a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 605294d71ffSAl Viro return 0; 60648a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 60748a066e7SAl Viro mnt_add_count(mnt, -1); 608294d71ffSAl Viro return 1; 60948a066e7SAl Viro } 610294d71ffSAl Viro return -1; 611294d71ffSAl Viro } 612294d71ffSAl Viro 613294d71ffSAl Viro /* call under rcu_read_lock */ 614294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 615294d71ffSAl Viro { 616294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 617294d71ffSAl Viro if (likely(!res)) 618294d71ffSAl Viro return true; 619294d71ffSAl Viro if (unlikely(res < 0)) { 62048a066e7SAl Viro rcu_read_unlock(); 62148a066e7SAl Viro mntput(bastard); 62248a066e7SAl Viro rcu_read_lock(); 623294d71ffSAl Viro } 62448a066e7SAl Viro return false; 62548a066e7SAl Viro } 62648a066e7SAl Viro 6271da177e4SLinus Torvalds /* 628474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 62948a066e7SAl Viro * call under rcu_read_lock() 6301da177e4SLinus Torvalds */ 631474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6321da177e4SLinus Torvalds { 63338129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 634474279dcSAl Viro struct mount *p; 6351da177e4SLinus Torvalds 63638129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 637474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 638474279dcSAl Viro return p; 639474279dcSAl Viro return NULL; 6401da177e4SLinus Torvalds } 641474279dcSAl Viro 642474279dcSAl Viro /* 643f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 644f015f126SDavid Howells * 645f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 646f015f126SDavid Howells * following mounts: 647f015f126SDavid Howells * 648f015f126SDavid Howells * mount /dev/sda1 /mnt 649f015f126SDavid Howells * mount /dev/sda2 /mnt 650f015f126SDavid Howells * mount /dev/sda3 /mnt 651f015f126SDavid Howells * 652f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 653f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 654f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 655f015f126SDavid Howells * 656f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 657a05964f3SRam Pai */ 658ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 659a05964f3SRam Pai { 660c7105365SAl Viro struct mount *child_mnt; 66148a066e7SAl Viro struct vfsmount *m; 66248a066e7SAl Viro unsigned seq; 66399b7db7bSNick Piggin 66448a066e7SAl Viro rcu_read_lock(); 66548a066e7SAl Viro do { 66648a066e7SAl Viro seq = read_seqbegin(&mount_lock); 667474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 66848a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 66948a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 67048a066e7SAl Viro rcu_read_unlock(); 67148a066e7SAl Viro return m; 672a05964f3SRam Pai } 673a05964f3SRam Pai 6747af1364fSEric W. Biederman /* 6757af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 6767af1364fSEric W. Biederman * current mount namespace. 6777af1364fSEric W. Biederman * 6787af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 6797af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 6807af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 6817af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 6827af1364fSEric W. Biederman * is a mountpoint. 6837af1364fSEric W. Biederman * 6847af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 6857af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 6867af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 6877af1364fSEric W. Biederman * parent mount. 6887af1364fSEric W. Biederman */ 6897af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 6907af1364fSEric W. Biederman { 6917af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 6927af1364fSEric W. Biederman struct mount *mnt; 6937af1364fSEric W. Biederman bool is_covered = false; 6947af1364fSEric W. Biederman 6957af1364fSEric W. Biederman if (!d_mountpoint(dentry)) 6967af1364fSEric W. Biederman goto out; 6977af1364fSEric W. Biederman 6987af1364fSEric W. Biederman down_read(&namespace_sem); 6997af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 7007af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 7017af1364fSEric W. Biederman if (is_covered) 7027af1364fSEric W. Biederman break; 7037af1364fSEric W. Biederman } 7047af1364fSEric W. Biederman up_read(&namespace_sem); 7057af1364fSEric W. Biederman out: 7067af1364fSEric W. Biederman return is_covered; 7077af1364fSEric W. Biederman } 7087af1364fSEric W. Biederman 709e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 71084d17192SAl Viro { 7110818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 71284d17192SAl Viro struct mountpoint *mp; 71384d17192SAl Viro 7140818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 71584d17192SAl Viro if (mp->m_dentry == dentry) { 71684d17192SAl Viro /* might be worth a WARN_ON() */ 71784d17192SAl Viro if (d_unlinked(dentry)) 71884d17192SAl Viro return ERR_PTR(-ENOENT); 71984d17192SAl Viro mp->m_count++; 72084d17192SAl Viro return mp; 72184d17192SAl Viro } 72284d17192SAl Viro } 723e2dfa935SEric W. Biederman return NULL; 724e2dfa935SEric W. Biederman } 725e2dfa935SEric W. Biederman 7263895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 727e2dfa935SEric W. Biederman { 7283895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 729e2dfa935SEric W. Biederman int ret; 73084d17192SAl Viro 7313895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7323895dbf8SEric W. Biederman mountpoint: 7333895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7343895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 7353895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7363895dbf8SEric W. Biederman if (mp) 7373895dbf8SEric W. Biederman goto done; 73884d17192SAl Viro } 739eed81007SMiklos Szeredi 7403895dbf8SEric W. Biederman if (!new) 7413895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 7423895dbf8SEric W. Biederman if (!new) 7433895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 7443895dbf8SEric W. Biederman 7453895dbf8SEric W. Biederman 7463895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 7473895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 7483895dbf8SEric W. Biederman 7493895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 7503895dbf8SEric W. Biederman if (ret == -EBUSY) 7513895dbf8SEric W. Biederman goto mountpoint; 7523895dbf8SEric W. Biederman 7533895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 7543895dbf8SEric W. Biederman mp = ERR_PTR(ret); 7553895dbf8SEric W. Biederman if (ret) 7563895dbf8SEric W. Biederman goto done; 7573895dbf8SEric W. Biederman 7583895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 7593895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7603895dbf8SEric W. Biederman new->m_dentry = dentry; 7613895dbf8SEric W. Biederman new->m_count = 1; 7623895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 7633895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 7643895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7653895dbf8SEric W. Biederman 7663895dbf8SEric W. Biederman mp = new; 7673895dbf8SEric W. Biederman new = NULL; 7683895dbf8SEric W. Biederman done: 7693895dbf8SEric W. Biederman kfree(new); 77084d17192SAl Viro return mp; 77184d17192SAl Viro } 77284d17192SAl Viro 77384d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 77484d17192SAl Viro { 77584d17192SAl Viro if (!--mp->m_count) { 77684d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7770a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 77884d17192SAl Viro spin_lock(&dentry->d_lock); 77984d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 78084d17192SAl Viro spin_unlock(&dentry->d_lock); 7810818bf27SAl Viro hlist_del(&mp->m_hash); 78284d17192SAl Viro kfree(mp); 78384d17192SAl Viro } 78484d17192SAl Viro } 78584d17192SAl Viro 786143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7871da177e4SLinus Torvalds { 7886b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 79199b7db7bSNick Piggin /* 79299b7db7bSNick Piggin * vfsmount lock must be held for write 79399b7db7bSNick Piggin */ 7946b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7955addc5ddSAl Viro { 7965addc5ddSAl Viro if (ns) { 7975addc5ddSAl Viro ns->event = ++event; 7985addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7995addc5ddSAl Viro } 8005addc5ddSAl Viro } 8015addc5ddSAl Viro 80299b7db7bSNick Piggin /* 80399b7db7bSNick Piggin * vfsmount lock must be held for write 80499b7db7bSNick Piggin */ 8056b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 8065addc5ddSAl Viro { 8075addc5ddSAl Viro if (ns && ns->event != event) { 8085addc5ddSAl Viro ns->event = event; 8095addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8105addc5ddSAl Viro } 8115addc5ddSAl Viro } 8125addc5ddSAl Viro 81399b7db7bSNick Piggin /* 81499b7db7bSNick Piggin * vfsmount lock must be held for write 81599b7db7bSNick Piggin */ 8167bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt) 8171da177e4SLinus Torvalds { 8180714a533SAl Viro mnt->mnt_parent = mnt; 819a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8206b41d536SAl Viro list_del_init(&mnt->mnt_child); 82138129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8220a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 82384d17192SAl Viro put_mountpoint(mnt->mnt_mp); 82484d17192SAl Viro mnt->mnt_mp = NULL; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 82799b7db7bSNick Piggin /* 82899b7db7bSNick Piggin * vfsmount lock must be held for write 82999b7db7bSNick Piggin */ 8307bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path) 8317bdb11deSEric W. Biederman { 8327bdb11deSEric W. Biederman old_path->dentry = mnt->mnt_mountpoint; 8337bdb11deSEric W. Biederman old_path->mnt = &mnt->mnt_parent->mnt; 8347bdb11deSEric W. Biederman unhash_mnt(mnt); 8357bdb11deSEric W. Biederman } 8367bdb11deSEric W. Biederman 8377bdb11deSEric W. Biederman /* 8387bdb11deSEric W. Biederman * vfsmount lock must be held for write 8397bdb11deSEric W. Biederman */ 8406a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 8416a46c573SEric W. Biederman { 8426a46c573SEric W. Biederman /* old mountpoint will be dropped when we can do that */ 8436a46c573SEric W. Biederman mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint; 8446a46c573SEric W. Biederman unhash_mnt(mnt); 8456a46c573SEric W. Biederman } 8466a46c573SEric W. Biederman 8476a46c573SEric W. Biederman /* 8486a46c573SEric W. Biederman * vfsmount lock must be held for write 8496a46c573SEric W. Biederman */ 85084d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 85184d17192SAl Viro struct mountpoint *mp, 85244d964d6SAl Viro struct mount *child_mnt) 853b90fa9aeSRam Pai { 85484d17192SAl Viro mp->m_count++; 8553a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 85684d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 8573a2393d7SAl Viro child_mnt->mnt_parent = mnt; 85884d17192SAl Viro child_mnt->mnt_mp = mp; 8590a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 860b90fa9aeSRam Pai } 861b90fa9aeSRam Pai 8621064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 8631064f874SEric W. Biederman { 8641064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 8651064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8661064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8671064f874SEric W. Biederman } 8681064f874SEric W. Biederman 86999b7db7bSNick Piggin /* 87099b7db7bSNick Piggin * vfsmount lock must be held for write 87199b7db7bSNick Piggin */ 87284d17192SAl Viro static void attach_mnt(struct mount *mnt, 87384d17192SAl Viro struct mount *parent, 87484d17192SAl Viro struct mountpoint *mp) 8751da177e4SLinus Torvalds { 87684d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 8771064f874SEric W. Biederman __attach_mnt(mnt, parent); 878b90fa9aeSRam Pai } 879b90fa9aeSRam Pai 8801064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 88112a5b529SAl Viro { 8821064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 8831064f874SEric W. Biederman struct dentry *old_mountpoint = mnt->mnt_mountpoint; 8841064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 8851064f874SEric W. Biederman 8861064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 8871064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 8881064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 8891064f874SEric W. Biederman 8901064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 8911064f874SEric W. Biederman 8921064f874SEric W. Biederman put_mountpoint(old_mp); 8931064f874SEric W. Biederman 8941064f874SEric W. Biederman /* 8951064f874SEric W. Biederman * Safely avoid even the suggestion this code might sleep or 8961064f874SEric W. Biederman * lock the mount hash by taking advantage of the knowledge that 8971064f874SEric W. Biederman * mnt_change_mountpoint will not release the final reference 8981064f874SEric W. Biederman * to a mountpoint. 8991064f874SEric W. Biederman * 9001064f874SEric W. Biederman * During mounting, the mount passed in as the parent mount will 9011064f874SEric W. Biederman * continue to use the old mountpoint and during unmounting, the 9021064f874SEric W. Biederman * old mountpoint will continue to exist until namespace_unlock, 9031064f874SEric W. Biederman * which happens well after mnt_change_mountpoint. 9041064f874SEric W. Biederman */ 9051064f874SEric W. Biederman spin_lock(&old_mountpoint->d_lock); 9061064f874SEric W. Biederman old_mountpoint->d_lockref.count--; 9071064f874SEric W. Biederman spin_unlock(&old_mountpoint->d_lock); 9081064f874SEric W. Biederman 9091064f874SEric W. Biederman mnt_add_count(old_parent, -1); 91012a5b529SAl Viro } 91112a5b529SAl Viro 912b90fa9aeSRam Pai /* 91399b7db7bSNick Piggin * vfsmount lock must be held for write 914b90fa9aeSRam Pai */ 9151064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 916b90fa9aeSRam Pai { 9170714a533SAl Viro struct mount *parent = mnt->mnt_parent; 91883adc753SAl Viro struct mount *m; 919b90fa9aeSRam Pai LIST_HEAD(head); 920143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 921b90fa9aeSRam Pai 9220714a533SAl Viro BUG_ON(parent == mnt); 923b90fa9aeSRam Pai 9241a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 925f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 926143c8c91SAl Viro m->mnt_ns = n; 927f03c6599SAl Viro 928b90fa9aeSRam Pai list_splice(&head, n->list.prev); 929b90fa9aeSRam Pai 930d2921684SEric W. Biederman n->mounts += n->pending_mounts; 931d2921684SEric W. Biederman n->pending_mounts = 0; 932d2921684SEric W. Biederman 9331064f874SEric W. Biederman __attach_mnt(mnt, parent); 9346b3286edSKirill Korotaev touch_mnt_namespace(n); 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 937909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 9381da177e4SLinus Torvalds { 9396b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 9406b41d536SAl Viro if (next == &p->mnt_mounts) { 9411da177e4SLinus Torvalds while (1) { 942909b0a88SAl Viro if (p == root) 9431da177e4SLinus Torvalds return NULL; 9446b41d536SAl Viro next = p->mnt_child.next; 9456b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 9461da177e4SLinus Torvalds break; 9470714a533SAl Viro p = p->mnt_parent; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds } 9506b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 953315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 9549676f0c6SRam Pai { 9556b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 9566b41d536SAl Viro while (prev != &p->mnt_mounts) { 9576b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 9586b41d536SAl Viro prev = p->mnt_mounts.prev; 9599676f0c6SRam Pai } 9609676f0c6SRam Pai return p; 9619676f0c6SRam Pai } 9629676f0c6SRam Pai 9639d412a43SAl Viro struct vfsmount * 9649d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 9659d412a43SAl Viro { 966b105e270SAl Viro struct mount *mnt; 9679d412a43SAl Viro struct dentry *root; 9689d412a43SAl Viro 9699d412a43SAl Viro if (!type) 9709d412a43SAl Viro return ERR_PTR(-ENODEV); 9719d412a43SAl Viro 9729d412a43SAl Viro mnt = alloc_vfsmnt(name); 9739d412a43SAl Viro if (!mnt) 9749d412a43SAl Viro return ERR_PTR(-ENOMEM); 9759d412a43SAl Viro 9769d412a43SAl Viro if (flags & MS_KERNMOUNT) 977b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9789d412a43SAl Viro 9799d412a43SAl Viro root = mount_fs(type, flags, name, data); 9809d412a43SAl Viro if (IS_ERR(root)) { 9818ffcb32eSDavid Howells mnt_free_id(mnt); 9829d412a43SAl Viro free_vfsmnt(mnt); 9839d412a43SAl Viro return ERR_CAST(root); 9849d412a43SAl Viro } 9859d412a43SAl Viro 986b105e270SAl Viro mnt->mnt.mnt_root = root; 987b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 988a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9890714a533SAl Viro mnt->mnt_parent = mnt; 990719ea2fbSAl Viro lock_mount_hash(); 99139f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 992719ea2fbSAl Viro unlock_mount_hash(); 993b105e270SAl Viro return &mnt->mnt; 9949d412a43SAl Viro } 9959d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 9969d412a43SAl Viro 99793faccbbSEric W. Biederman struct vfsmount * 99893faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 99993faccbbSEric W. Biederman const char *name, void *data) 100093faccbbSEric W. Biederman { 100193faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 100293faccbbSEric W. Biederman * through from the parent mount to the submount don't support 100393faccbbSEric W. Biederman * unprivileged mounts with submounts. 100493faccbbSEric W. Biederman */ 100593faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 100693faccbbSEric W. Biederman return ERR_PTR(-EPERM); 100793faccbbSEric W. Biederman 100893faccbbSEric W. Biederman return vfs_kern_mount(type, MS_SUBMOUNT, name, data); 100993faccbbSEric W. Biederman } 101093faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 101193faccbbSEric W. Biederman 101287129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 101336341f64SRam Pai int flag) 10141da177e4SLinus Torvalds { 101587129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 1016be34d1a3SDavid Howells struct mount *mnt; 1017be34d1a3SDavid Howells int err; 10181da177e4SLinus Torvalds 1019be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1020be34d1a3SDavid Howells if (!mnt) 1021be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1022be34d1a3SDavid Howells 10237a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 102415169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1025719f5d7fSMiklos Szeredi else 102615169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1027719f5d7fSMiklos Szeredi 102815169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1029be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1030719f5d7fSMiklos Szeredi if (err) 1031719f5d7fSMiklos Szeredi goto out_free; 1032719f5d7fSMiklos Szeredi } 1033719f5d7fSMiklos Szeredi 1034f2ebb3a9SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED); 1035132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 10369566d674SEric W. Biederman if (flag & CL_UNPRIVILEGED) { 10379566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_ATIME; 10389566d674SEric W. Biederman 10399566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_READONLY) 1040132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 1041132c94e3SEric W. Biederman 10429566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NODEV) 10439566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NODEV; 10449566d674SEric W. Biederman 10459566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOSUID) 10469566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID; 10479566d674SEric W. Biederman 10489566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOEXEC) 10499566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC; 10509566d674SEric W. Biederman } 10519566d674SEric W. Biederman 10525ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 1053381cacb1SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && 1054381cacb1SEric W. Biederman (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire))) 10555ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 10565ff9d8a6SEric W. Biederman 10571da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1058b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1059b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1060a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10610714a533SAl Viro mnt->mnt_parent = mnt; 1062719ea2fbSAl Viro lock_mount_hash(); 106339f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1064719ea2fbSAl Viro unlock_mount_hash(); 1065b90fa9aeSRam Pai 10667a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 10677a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 10686776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 106932301920SAl Viro mnt->mnt_master = old; 1070fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 10718aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1072fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 10736776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1074d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 10756776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1076d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 10775235d448SAl Viro } else { 10785235d448SAl Viro CLEAR_MNT_SHARED(mnt); 10795afe0022SRam Pai } 1080b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 10810f0afb1dSAl Viro set_mnt_shared(mnt); 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 10841da177e4SLinus Torvalds * as the original if that was on one */ 108536341f64SRam Pai if (flag & CL_EXPIRE) { 10866776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 10876776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 10881da177e4SLinus Torvalds } 1089be34d1a3SDavid Howells 1090cb338d06SAl Viro return mnt; 1091719f5d7fSMiklos Szeredi 1092719f5d7fSMiklos Szeredi out_free: 10938ffcb32eSDavid Howells mnt_free_id(mnt); 1094719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1095be34d1a3SDavid Howells return ERR_PTR(err); 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 10989ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 10999ea459e1SAl Viro { 11009ea459e1SAl Viro /* 11019ea459e1SAl Viro * This probably indicates that somebody messed 11029ea459e1SAl Viro * up a mnt_want/drop_write() pair. If this 11039ea459e1SAl Viro * happens, the filesystem was probably unable 11049ea459e1SAl Viro * to make r/w->r/o transitions. 11059ea459e1SAl Viro */ 11069ea459e1SAl Viro /* 11079ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 11089ea459e1SAl Viro * so mnt_get_writers() below is safe. 11099ea459e1SAl Viro */ 11109ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 11119ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 11129ea459e1SAl Viro mnt_pin_kill(mnt); 11139ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 11149ea459e1SAl Viro dput(mnt->mnt.mnt_root); 11159ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 11169ea459e1SAl Viro mnt_free_id(mnt); 11179ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11189ea459e1SAl Viro } 11199ea459e1SAl Viro 11209ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11219ea459e1SAl Viro { 11229ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11239ea459e1SAl Viro } 11249ea459e1SAl Viro 11259ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11269ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11279ea459e1SAl Viro { 11289ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 11299ea459e1SAl Viro struct llist_node *next; 11309ea459e1SAl Viro 11319ea459e1SAl Viro for (; node; node = next) { 11329ea459e1SAl Viro next = llist_next(node); 11339ea459e1SAl Viro cleanup_mnt(llist_entry(node, struct mount, mnt_llist)); 11349ea459e1SAl Viro } 11359ea459e1SAl Viro } 11369ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 11379ea459e1SAl Viro 1138900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 11397b7b1aceSAl Viro { 114048a066e7SAl Viro rcu_read_lock(); 1141aa9c0e07SAl Viro mnt_add_count(mnt, -1); 114248a066e7SAl Viro if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */ 114348a066e7SAl Viro rcu_read_unlock(); 114499b7db7bSNick Piggin return; 1145b3e19d92SNick Piggin } 1146719ea2fbSAl Viro lock_mount_hash(); 1147b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 114848a066e7SAl Viro rcu_read_unlock(); 1149719ea2fbSAl Viro unlock_mount_hash(); 115099b7db7bSNick Piggin return; 115199b7db7bSNick Piggin } 115248a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 115348a066e7SAl Viro rcu_read_unlock(); 115448a066e7SAl Viro unlock_mount_hash(); 115548a066e7SAl Viro return; 115648a066e7SAl Viro } 115748a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 115848a066e7SAl Viro rcu_read_unlock(); 1159962830dfSAndi Kleen 116039f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1161ce07d891SEric W. Biederman 1162ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1163ce07d891SEric W. Biederman struct mount *p, *tmp; 1164ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 1165ce07d891SEric W. Biederman umount_mnt(p); 1166ce07d891SEric W. Biederman } 1167ce07d891SEric W. Biederman } 1168719ea2fbSAl Viro unlock_mount_hash(); 1169649a795aSAl Viro 11709ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 11719ea459e1SAl Viro struct task_struct *task = current; 11729ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 11739ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 11749ea459e1SAl Viro if (!task_work_add(task, &mnt->mnt_rcu, true)) 11759ea459e1SAl Viro return; 11769ea459e1SAl Viro } 11779ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 11789ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 11799ea459e1SAl Viro return; 11809ea459e1SAl Viro } 11819ea459e1SAl Viro cleanup_mnt(mnt); 1182b3e19d92SNick Piggin } 1183b3e19d92SNick Piggin 1184b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1185b3e19d92SNick Piggin { 1186b3e19d92SNick Piggin if (mnt) { 1187863d684fSAl Viro struct mount *m = real_mount(mnt); 1188b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1189863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1190863d684fSAl Viro m->mnt_expiry_mark = 0; 1191863d684fSAl Viro mntput_no_expire(m); 1192b3e19d92SNick Piggin } 1193b3e19d92SNick Piggin } 1194b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1195b3e19d92SNick Piggin 1196b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1197b3e19d92SNick Piggin { 1198b3e19d92SNick Piggin if (mnt) 119983adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1200b3e19d92SNick Piggin return mnt; 1201b3e19d92SNick Piggin } 1202b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1203b3e19d92SNick Piggin 1204c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current 1205c6609c0aSIan Kent * namespace. 1206c6609c0aSIan Kent * 1207c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1208c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1209c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1210c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1211c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1212c6609c0aSIan Kent * alone. 1213c6609c0aSIan Kent */ 1214c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1215c6609c0aSIan Kent { 1216c6609c0aSIan Kent unsigned seq; 1217c6609c0aSIan Kent bool res; 1218c6609c0aSIan Kent 1219c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1220c6609c0aSIan Kent return false; 1221c6609c0aSIan Kent 1222c6609c0aSIan Kent rcu_read_lock(); 1223c6609c0aSIan Kent do { 1224c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1225c6609c0aSIan Kent res = __path_is_mountpoint(path); 1226c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1227c6609c0aSIan Kent rcu_read_unlock(); 1228c6609c0aSIan Kent 1229c6609c0aSIan Kent return res; 1230c6609c0aSIan Kent } 1231c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1232c6609c0aSIan Kent 1233ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 12347b7b1aceSAl Viro { 12353064c356SAl Viro struct mount *p; 12363064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 12373064c356SAl Viro if (IS_ERR(p)) 12383064c356SAl Viro return ERR_CAST(p); 12393064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 12403064c356SAl Viro return &p->mnt; 12417b7b1aceSAl Viro } 12421da177e4SLinus Torvalds 1243b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 1244b3b304a2SMiklos Szeredi { 1245b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 1246b3b304a2SMiklos Szeredi } 1247b3b304a2SMiklos Szeredi 1248b3b304a2SMiklos Szeredi /* 1249b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 1250b3b304a2SMiklos Szeredi * implement more complex mount option showing. 1251b3b304a2SMiklos Szeredi * 1252b3b304a2SMiklos Szeredi * See also save_mount_options(). 1253b3b304a2SMiklos Szeredi */ 125434c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 1255b3b304a2SMiklos Szeredi { 12562a32cebdSAl Viro const char *options; 12572a32cebdSAl Viro 12582a32cebdSAl Viro rcu_read_lock(); 125934c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 1260b3b304a2SMiklos Szeredi 1261b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 1262b3b304a2SMiklos Szeredi seq_putc(m, ','); 1263b3b304a2SMiklos Szeredi mangle(m, options); 1264b3b304a2SMiklos Szeredi } 12652a32cebdSAl Viro rcu_read_unlock(); 1266b3b304a2SMiklos Szeredi 1267b3b304a2SMiklos Szeredi return 0; 1268b3b304a2SMiklos Szeredi } 1269b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 1270b3b304a2SMiklos Szeredi 1271b3b304a2SMiklos Szeredi /* 1272b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 1273b3b304a2SMiklos Szeredi * called from the fill_super() callback. 1274b3b304a2SMiklos Szeredi * 1275b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 1276b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 1277b3b304a2SMiklos Szeredi * remount fails. 1278b3b304a2SMiklos Szeredi * 1279b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 1280b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 1281b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 1282b3b304a2SMiklos Szeredi * any more. 1283b3b304a2SMiklos Szeredi */ 1284b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 1285b3b304a2SMiklos Szeredi { 12862a32cebdSAl Viro BUG_ON(sb->s_options); 12872a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 1288b3b304a2SMiklos Szeredi } 1289b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 1290b3b304a2SMiklos Szeredi 12912a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 12922a32cebdSAl Viro { 12932a32cebdSAl Viro char *old = sb->s_options; 12942a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 12952a32cebdSAl Viro if (old) { 12962a32cebdSAl Viro synchronize_rcu(); 12972a32cebdSAl Viro kfree(old); 12982a32cebdSAl Viro } 12992a32cebdSAl Viro } 13002a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 13012a32cebdSAl Viro 1302a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 13030226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 13041da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 13051da177e4SLinus Torvalds { 1306ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13071da177e4SLinus Torvalds 1308390c6843SRam Pai down_read(&namespace_sem); 1309c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1310c7999c36SAl Viro void *v = p->cached_mount; 1311c7999c36SAl Viro if (*pos == p->cached_index) 1312c7999c36SAl Viro return v; 1313c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1314c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1315c7999c36SAl Viro return p->cached_mount = v; 1316c7999c36SAl Viro } 1317c7999c36SAl Viro } 1318c7999c36SAl Viro 1319c7999c36SAl Viro p->cached_event = p->ns->event; 1320c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1321c7999c36SAl Viro p->cached_index = *pos; 1322c7999c36SAl Viro return p->cached_mount; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 13261da177e4SLinus Torvalds { 1327ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 1328b0765fb8SPavel Emelianov 1329c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1330c7999c36SAl Viro p->cached_index = *pos; 1331c7999c36SAl Viro return p->cached_mount; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 13351da177e4SLinus Torvalds { 1336390c6843SRam Pai up_read(&namespace_sem); 13371da177e4SLinus Torvalds } 13381da177e4SLinus Torvalds 13390226f492SAl Viro static int m_show(struct seq_file *m, void *v) 13409f5596afSAl Viro { 1341ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13421a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 13430226f492SAl Viro return p->show(m, &r->mnt); 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 1346a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 13471da177e4SLinus Torvalds .start = m_start, 13481da177e4SLinus Torvalds .next = m_next, 13491da177e4SLinus Torvalds .stop = m_stop, 13500226f492SAl Viro .show = m_show, 1351b4629fe2SChuck Lever }; 1352a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1353b4629fe2SChuck Lever 13541da177e4SLinus Torvalds /** 13551da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 13561da177e4SLinus Torvalds * @mnt: root of mount tree 13571da177e4SLinus Torvalds * 13581da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 13591da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 13601da177e4SLinus Torvalds * busy. 13611da177e4SLinus Torvalds */ 1362909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13631da177e4SLinus Torvalds { 1364909b0a88SAl Viro struct mount *mnt = real_mount(m); 136536341f64SRam Pai int actual_refs = 0; 136636341f64SRam Pai int minimum_refs = 0; 1367315fc83eSAl Viro struct mount *p; 1368909b0a88SAl Viro BUG_ON(!m); 13691da177e4SLinus Torvalds 1370b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1371719ea2fbSAl Viro lock_mount_hash(); 1372909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 137383adc753SAl Viro actual_refs += mnt_get_count(p); 13741da177e4SLinus Torvalds minimum_refs += 2; 13751da177e4SLinus Torvalds } 1376719ea2fbSAl Viro unlock_mount_hash(); 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds if (actual_refs > minimum_refs) 13791da177e4SLinus Torvalds return 0; 1380e3474a8eSIan Kent 1381e3474a8eSIan Kent return 1; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds /** 13871da177e4SLinus Torvalds * may_umount - check if a mount point is busy 13881da177e4SLinus Torvalds * @mnt: root of mount 13891da177e4SLinus Torvalds * 13901da177e4SLinus Torvalds * This is called to check if a mount point has any 13911da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 13921da177e4SLinus Torvalds * mount has sub mounts this will return busy 13931da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 13941da177e4SLinus Torvalds * 13951da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 13961da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 13971da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 13981da177e4SLinus Torvalds */ 13991da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 14001da177e4SLinus Torvalds { 1401e3474a8eSIan Kent int ret = 1; 14028ad08d8aSAl Viro down_read(&namespace_sem); 1403719ea2fbSAl Viro lock_mount_hash(); 14041ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1405e3474a8eSIan Kent ret = 0; 1406719ea2fbSAl Viro unlock_mount_hash(); 14078ad08d8aSAl Viro up_read(&namespace_sem); 1408a05964f3SRam Pai return ret; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 14121da177e4SLinus Torvalds 141338129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1414e3197d83SAl Viro 141597216be0SAl Viro static void namespace_unlock(void) 14161da177e4SLinus Torvalds { 1417a3b3c562SEric W. Biederman struct hlist_head head; 141897216be0SAl Viro 1419a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 1420a3b3c562SEric W. Biederman 142197216be0SAl Viro up_write(&namespace_sem); 1422a3b3c562SEric W. Biederman 1423a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 142497216be0SAl Viro return; 142597216be0SAl Viro 142648a066e7SAl Viro synchronize_rcu(); 142748a066e7SAl Viro 142887b95ce0SAl Viro group_pin_kill(&head); 142970fbcdf4SRam Pai } 143070fbcdf4SRam Pai 143197216be0SAl Viro static inline void namespace_lock(void) 1432e3197d83SAl Viro { 143397216be0SAl Viro down_write(&namespace_sem); 1434e3197d83SAl Viro } 1435e3197d83SAl Viro 1436e819f152SEric W. Biederman enum umount_tree_flags { 1437e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1438e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1439e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1440e819f152SEric W. Biederman }; 1441f2d0a123SEric W. Biederman 1442f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1443f2d0a123SEric W. Biederman { 1444f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1445f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1446f2d0a123SEric W. Biederman return true; 1447f2d0a123SEric W. Biederman 1448f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1449f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1450f2d0a123SEric W. Biederman return true; 1451f2d0a123SEric W. Biederman 1452f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1453f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1454f2d0a123SEric W. Biederman * connected to mounted mounts. 1455f2d0a123SEric W. Biederman */ 1456f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1457f2d0a123SEric W. Biederman return true; 1458f2d0a123SEric W. Biederman 1459f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1460f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1461f2d0a123SEric W. Biederman return false; 1462f2d0a123SEric W. Biederman 1463f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1464f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1465f2d0a123SEric W. Biederman return false; 1466f2d0a123SEric W. Biederman 1467f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1468f2d0a123SEric W. Biederman return true; 1469f2d0a123SEric W. Biederman } 1470f2d0a123SEric W. Biederman 147199b7db7bSNick Piggin /* 147248a066e7SAl Viro * mount_lock must be held 147399b7db7bSNick Piggin * namespace_sem must be held for write 147499b7db7bSNick Piggin */ 1475e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 147670fbcdf4SRam Pai { 1477c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1478315fc83eSAl Viro struct mount *p; 147970fbcdf4SRam Pai 14805d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 14815d88457eSEric W. Biederman propagate_mount_unlock(mnt); 14825d88457eSEric W. Biederman 1483c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1484590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1485590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1486c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1487590ce4bcSEric W. Biederman } 1488c003b26fSEric W. Biederman 1489411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1490c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1491c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 149238129a13SAl Viro } 149370fbcdf4SRam Pai 1494c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1495e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 14967b8a53fdSAl Viro propagate_umount(&tmp_list); 1497a05964f3SRam Pai 1498c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1499d2921684SEric W. Biederman struct mnt_namespace *ns; 1500ce07d891SEric W. Biederman bool disconnect; 1501c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 15026776db3dSAl Viro list_del_init(&p->mnt_expire); 15031a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1504d2921684SEric W. Biederman ns = p->mnt_ns; 1505d2921684SEric W. Biederman if (ns) { 1506d2921684SEric W. Biederman ns->mounts--; 1507d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1508d2921684SEric W. Biederman } 150963d37a84SAl Viro p->mnt_ns = NULL; 1510e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 151148a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 151287b95ce0SAl Viro 1513f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1514ce07d891SEric W. Biederman 1515ce07d891SEric W. Biederman pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt, 1516ce07d891SEric W. Biederman disconnect ? &unmounted : NULL); 1517676da58dSAl Viro if (mnt_has_parent(p)) { 151881b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1519ce07d891SEric W. Biederman if (!disconnect) { 1520ce07d891SEric W. Biederman /* Don't forget about p */ 1521ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1522ce07d891SEric W. Biederman } else { 15236a46c573SEric W. Biederman umount_mnt(p); 15247c4b93d8SAl Viro } 1525ce07d891SEric W. Biederman } 15260f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 152738129a13SAl Viro } 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 1530b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1531c35038beSAl Viro 15321ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 15331da177e4SLinus Torvalds { 15341ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 15351da177e4SLinus Torvalds int retval; 15361da177e4SLinus Torvalds 15371ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 15381da177e4SLinus Torvalds if (retval) 15391da177e4SLinus Torvalds return retval; 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds /* 15421da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 15431da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 15441da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 15451da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 15461da177e4SLinus Torvalds */ 15471da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 15481ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 15491da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 15501da177e4SLinus Torvalds return -EINVAL; 15511da177e4SLinus Torvalds 1552b3e19d92SNick Piggin /* 1553b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1554b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1555b3e19d92SNick Piggin */ 1556719ea2fbSAl Viro lock_mount_hash(); 155783adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1558719ea2fbSAl Viro unlock_mount_hash(); 15591da177e4SLinus Torvalds return -EBUSY; 1560b3e19d92SNick Piggin } 1561719ea2fbSAl Viro unlock_mount_hash(); 15621da177e4SLinus Torvalds 1563863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 15641da177e4SLinus Torvalds return -EAGAIN; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds /* 15681da177e4SLinus Torvalds * If we may have to abort operations to get out of this 15691da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 15701da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 15711da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 15721da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 15731da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 15741da177e4SLinus Torvalds * about for the moment. 15751da177e4SLinus Torvalds */ 15761da177e4SLinus Torvalds 157742faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 157842faad99SAl Viro sb->s_op->umount_begin(sb); 157942faad99SAl Viro } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* 15821da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 15831da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 15841da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 15851da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 15861da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 15871da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 15881da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 15891da177e4SLinus Torvalds */ 15901ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 15911da177e4SLinus Torvalds /* 15921da177e4SLinus Torvalds * Special case for "unmounting" root ... 15931da177e4SLinus Torvalds * we just try to remount it readonly. 15941da177e4SLinus Torvalds */ 1595a1480dccSAndy Lutomirski if (!capable(CAP_SYS_ADMIN)) 1596a1480dccSAndy Lutomirski return -EPERM; 15971da177e4SLinus Torvalds down_write(&sb->s_umount); 15984aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 15991da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 16001da177e4SLinus Torvalds up_write(&sb->s_umount); 16011da177e4SLinus Torvalds return retval; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 160497216be0SAl Viro namespace_lock(); 1605719ea2fbSAl Viro lock_mount_hash(); 16065addc5ddSAl Viro event++; 16071da177e4SLinus Torvalds 160848a066e7SAl Viro if (flags & MNT_DETACH) { 160948a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1610e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 161148a066e7SAl Viro retval = 0; 161248a066e7SAl Viro } else { 1613b54b9be7SAl Viro shrink_submounts(mnt); 16141da177e4SLinus Torvalds retval = -EBUSY; 161548a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 16161a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1617e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 16181da177e4SLinus Torvalds retval = 0; 16191da177e4SLinus Torvalds } 162048a066e7SAl Viro } 1621719ea2fbSAl Viro unlock_mount_hash(); 1622e3197d83SAl Viro namespace_unlock(); 16231da177e4SLinus Torvalds return retval; 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds /* 162780b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 162880b5dce8SEric W. Biederman * 162980b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 163080b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 163180b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 163280b5dce8SEric W. Biederman * leaking them. 163380b5dce8SEric W. Biederman * 163480b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 163580b5dce8SEric W. Biederman */ 163680b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 163780b5dce8SEric W. Biederman { 163880b5dce8SEric W. Biederman struct mountpoint *mp; 163980b5dce8SEric W. Biederman struct mount *mnt; 164080b5dce8SEric W. Biederman 164180b5dce8SEric W. Biederman namespace_lock(); 16423895dbf8SEric W. Biederman lock_mount_hash(); 164380b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1644f53e5797SEric W. Biederman if (IS_ERR_OR_NULL(mp)) 164580b5dce8SEric W. Biederman goto out_unlock; 164680b5dce8SEric W. Biederman 1647e06b933eSAndrey Ulanov event++; 164880b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 164980b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1650ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1651fe78fcc8SEric W. Biederman hlist_add_head(&mnt->mnt_umount.s_list, &unmounted); 1652fe78fcc8SEric W. Biederman umount_mnt(mnt); 1653ce07d891SEric W. Biederman } 1654e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 165580b5dce8SEric W. Biederman } 165680b5dce8SEric W. Biederman put_mountpoint(mp); 165780b5dce8SEric W. Biederman out_unlock: 16583895dbf8SEric W. Biederman unlock_mount_hash(); 165980b5dce8SEric W. Biederman namespace_unlock(); 166080b5dce8SEric W. Biederman } 166180b5dce8SEric W. Biederman 166280b5dce8SEric W. Biederman /* 16639b40bc90SAl Viro * Is the caller allowed to modify his namespace? 16649b40bc90SAl Viro */ 16659b40bc90SAl Viro static inline bool may_mount(void) 16669b40bc90SAl Viro { 16679b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 16689b40bc90SAl Viro } 16699b40bc90SAl Viro 16709e8925b6SJeff Layton static inline bool may_mandlock(void) 16719e8925b6SJeff Layton { 16729e8925b6SJeff Layton #ifndef CONFIG_MANDATORY_FILE_LOCKING 16739e8925b6SJeff Layton return false; 16749e8925b6SJeff Layton #endif 167595ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 16769e8925b6SJeff Layton } 16779e8925b6SJeff Layton 16789b40bc90SAl Viro /* 16791da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 16801da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 16811da177e4SLinus Torvalds * 16821da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 16831da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 16841da177e4SLinus Torvalds */ 16851da177e4SLinus Torvalds 1686bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 16871da177e4SLinus Torvalds { 16882d8f3038SAl Viro struct path path; 1689900148dcSAl Viro struct mount *mnt; 16901da177e4SLinus Torvalds int retval; 1691db1f05bbSMiklos Szeredi int lookup_flags = 0; 16921da177e4SLinus Torvalds 1693db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1694db1f05bbSMiklos Szeredi return -EINVAL; 1695db1f05bbSMiklos Szeredi 16969b40bc90SAl Viro if (!may_mount()) 16979b40bc90SAl Viro return -EPERM; 16989b40bc90SAl Viro 1699db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1700db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1701db1f05bbSMiklos Szeredi 1702197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 17031da177e4SLinus Torvalds if (retval) 17041da177e4SLinus Torvalds goto out; 1705900148dcSAl Viro mnt = real_mount(path.mnt); 17061da177e4SLinus Torvalds retval = -EINVAL; 17072d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 17081da177e4SLinus Torvalds goto dput_and_out; 1709143c8c91SAl Viro if (!check_mnt(mnt)) 17101da177e4SLinus Torvalds goto dput_and_out; 17115ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 17125ff9d8a6SEric W. Biederman goto dput_and_out; 1713b2f5d4dcSEric W. Biederman retval = -EPERM; 1714b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 1715b2f5d4dcSEric W. Biederman goto dput_and_out; 17161da177e4SLinus Torvalds 1717900148dcSAl Viro retval = do_umount(mnt, flags); 17181da177e4SLinus Torvalds dput_and_out: 1719429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 17202d8f3038SAl Viro dput(path.dentry); 1721900148dcSAl Viro mntput_no_expire(mnt); 17221da177e4SLinus Torvalds out: 17231da177e4SLinus Torvalds return retval; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds /* 17291da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 17301da177e4SLinus Torvalds */ 1731bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 17321da177e4SLinus Torvalds { 17331da177e4SLinus Torvalds return sys_umount(name, 0); 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds #endif 17371da177e4SLinus Torvalds 17384ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 17398823c079SEric W. Biederman { 17404ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1741e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1742e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 17434ce5d2b1SEric W. Biederman } 17444ce5d2b1SEric W. Biederman 174558be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 174658be2825SAl Viro { 174758be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 174858be2825SAl Viro } 174958be2825SAl Viro 17504ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 17514ce5d2b1SEric W. Biederman { 17524ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 17534ce5d2b1SEric W. Biederman * mount namespace loop? 17544ce5d2b1SEric W. Biederman */ 17554ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 17564ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 17574ce5d2b1SEric W. Biederman return false; 17584ce5d2b1SEric W. Biederman 1759f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 17608823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 17618823c079SEric W. Biederman } 17628823c079SEric W. Biederman 176387129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 176436341f64SRam Pai int flag) 17651da177e4SLinus Torvalds { 176684d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 17671da177e4SLinus Torvalds 17684ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 17694ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 17704ce5d2b1SEric W. Biederman 17714ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1772be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 17739676f0c6SRam Pai 177436341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1775be34d1a3SDavid Howells if (IS_ERR(q)) 1776be34d1a3SDavid Howells return q; 1777be34d1a3SDavid Howells 1778a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds p = mnt; 17816b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1782315fc83eSAl Viro struct mount *s; 17837ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 17841da177e4SLinus Torvalds continue; 17851da177e4SLinus Torvalds 1786909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 17874ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 17884ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 17894ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 17904ce5d2b1SEric W. Biederman continue; 17914ce5d2b1SEric W. Biederman } 17924ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 17934ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 17949676f0c6SRam Pai s = skip_mnt_tree(s); 17959676f0c6SRam Pai continue; 17969676f0c6SRam Pai } 17970714a533SAl Viro while (p != s->mnt_parent) { 17980714a533SAl Viro p = p->mnt_parent; 17990714a533SAl Viro q = q->mnt_parent; 18001da177e4SLinus Torvalds } 180187129cc0SAl Viro p = s; 180284d17192SAl Viro parent = q; 180387129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1804be34d1a3SDavid Howells if (IS_ERR(q)) 1805be34d1a3SDavid Howells goto out; 1806719ea2fbSAl Viro lock_mount_hash(); 18071a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 18081064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1809719ea2fbSAl Viro unlock_mount_hash(); 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds return res; 1813be34d1a3SDavid Howells out: 18141da177e4SLinus Torvalds if (res) { 1815719ea2fbSAl Viro lock_mount_hash(); 1816e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1817719ea2fbSAl Viro unlock_mount_hash(); 18181da177e4SLinus Torvalds } 1819be34d1a3SDavid Howells return q; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 1822be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1823be34d1a3SDavid Howells 1824ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 18258aec0809SAl Viro { 1826cb338d06SAl Viro struct mount *tree; 182797216be0SAl Viro namespace_lock(); 1828cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1829cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1830cd4a4017SEric W. Biederman else 183187129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 183287129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1833328e6d90SAl Viro namespace_unlock(); 1834be34d1a3SDavid Howells if (IS_ERR(tree)) 183552e220d3SDan Carpenter return ERR_CAST(tree); 1836be34d1a3SDavid Howells return &tree->mnt; 18378aec0809SAl Viro } 18388aec0809SAl Viro 18398aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 18408aec0809SAl Viro { 184197216be0SAl Viro namespace_lock(); 1842719ea2fbSAl Viro lock_mount_hash(); 1843e819f152SEric W. Biederman umount_tree(real_mount(mnt), UMOUNT_SYNC); 1844719ea2fbSAl Viro unlock_mount_hash(); 18453ab6abeeSAl Viro namespace_unlock(); 18468aec0809SAl Viro } 18478aec0809SAl Viro 1848c771d683SMiklos Szeredi /** 1849c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 1850c771d683SMiklos Szeredi * 1851c771d683SMiklos Szeredi * This creates a new vfsmount, which will be the clone of @path. The new will 1852c771d683SMiklos Szeredi * not be attached anywhere in the namespace and will be private (i.e. changes 1853c771d683SMiklos Szeredi * to the originating mount won't be propagated into this). 1854c771d683SMiklos Szeredi * 1855c771d683SMiklos Szeredi * Release with mntput(). 1856c771d683SMiklos Szeredi */ 1857ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1858c771d683SMiklos Szeredi { 1859c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1860c771d683SMiklos Szeredi struct mount *new_mnt; 1861c771d683SMiklos Szeredi 1862c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1863c771d683SMiklos Szeredi return ERR_PTR(-EINVAL); 1864c771d683SMiklos Szeredi 1865c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1866c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1867c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1868c771d683SMiklos Szeredi 1869c771d683SMiklos Szeredi return &new_mnt->mnt; 1870c771d683SMiklos Szeredi } 1871c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 1872c771d683SMiklos Szeredi 18731f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 18741f707137SAl Viro struct vfsmount *root) 18751f707137SAl Viro { 18761a4eeaf2SAl Viro struct mount *mnt; 18771f707137SAl Viro int res = f(root, arg); 18781f707137SAl Viro if (res) 18791f707137SAl Viro return res; 18801a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 18811a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 18821f707137SAl Viro if (res) 18831f707137SAl Viro return res; 18841f707137SAl Viro } 18851f707137SAl Viro return 0; 18861f707137SAl Viro } 18871f707137SAl Viro 18884b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1889719f5d7fSMiklos Szeredi { 1890315fc83eSAl Viro struct mount *p; 1891719f5d7fSMiklos Szeredi 1892909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1893fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 18944b8b21f4SAl Viro mnt_release_group_id(p); 1895719f5d7fSMiklos Szeredi } 1896719f5d7fSMiklos Szeredi } 1897719f5d7fSMiklos Szeredi 18984b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1899719f5d7fSMiklos Szeredi { 1900315fc83eSAl Viro struct mount *p; 1901719f5d7fSMiklos Szeredi 1902909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1903fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 19044b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1905719f5d7fSMiklos Szeredi if (err) { 19064b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1907719f5d7fSMiklos Szeredi return err; 1908719f5d7fSMiklos Szeredi } 1909719f5d7fSMiklos Szeredi } 1910719f5d7fSMiklos Szeredi } 1911719f5d7fSMiklos Szeredi 1912719f5d7fSMiklos Szeredi return 0; 1913719f5d7fSMiklos Szeredi } 1914719f5d7fSMiklos Szeredi 1915d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 1916d2921684SEric W. Biederman { 1917d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 1918d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 1919d2921684SEric W. Biederman struct mount *p; 1920d2921684SEric W. Biederman 1921d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 1922d2921684SEric W. Biederman mounts++; 1923d2921684SEric W. Biederman 1924d2921684SEric W. Biederman old = ns->mounts; 1925d2921684SEric W. Biederman pending = ns->pending_mounts; 1926d2921684SEric W. Biederman sum = old + pending; 1927d2921684SEric W. Biederman if ((old > sum) || 1928d2921684SEric W. Biederman (pending > sum) || 1929d2921684SEric W. Biederman (max < sum) || 1930d2921684SEric W. Biederman (mounts > (max - sum))) 1931d2921684SEric W. Biederman return -ENOSPC; 1932d2921684SEric W. Biederman 1933d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 1934d2921684SEric W. Biederman return 0; 1935d2921684SEric W. Biederman } 1936d2921684SEric W. Biederman 1937b90fa9aeSRam Pai /* 1938b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1939b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 194021444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 194121444403SRam Pai * store the parent mount and mountpoint dentry. 194221444403SRam Pai * (done when source_mnt is moved) 1943b90fa9aeSRam Pai * 1944b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1945b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 19469676f0c6SRam Pai * --------------------------------------------------------------------------- 1947b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 19489676f0c6SRam Pai * |************************************************************************** 19499676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 19509676f0c6SRam Pai * | dest | | | | | 19519676f0c6SRam Pai * | | | | | | | 19529676f0c6SRam Pai * | v | | | | | 19539676f0c6SRam Pai * |************************************************************************** 19549676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 19555afe0022SRam Pai * | | | | | | 19569676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 19579676f0c6SRam Pai * *************************************************************************** 1958b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1959b90fa9aeSRam Pai * destination mount. 1960b90fa9aeSRam Pai * 1961b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1962b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1963b90fa9aeSRam Pai * the peer group of the source mount. 1964b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1965b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1966b90fa9aeSRam Pai * mount. 19675afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 19685afe0022SRam Pai * of the destination mount and the cloned mount is made slave 19695afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 19705afe0022SRam Pai * is marked as 'shared and slave'. 19715afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 19725afe0022SRam Pai * source mount. 19735afe0022SRam Pai * 19749676f0c6SRam Pai * --------------------------------------------------------------------------- 197521444403SRam Pai * | MOVE MOUNT OPERATION | 19769676f0c6SRam Pai * |************************************************************************** 19779676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 19789676f0c6SRam Pai * | dest | | | | | 19799676f0c6SRam Pai * | | | | | | | 19809676f0c6SRam Pai * | v | | | | | 19819676f0c6SRam Pai * |************************************************************************** 19829676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 19835afe0022SRam Pai * | | | | | | 19849676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 19859676f0c6SRam Pai * *************************************************************************** 19865afe0022SRam Pai * 19875afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 19885afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 198921444403SRam Pai * (+*) the mount is moved to the destination. 19905afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 19915afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 19925afe0022SRam Pai * the mount is marked as 'shared and slave'. 19935afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1994b90fa9aeSRam Pai * 1995b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1996b90fa9aeSRam Pai * applied to each mount in the tree. 1997b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1998b90fa9aeSRam Pai * in allocations. 1999b90fa9aeSRam Pai */ 20000fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 200184d17192SAl Viro struct mount *dest_mnt, 200284d17192SAl Viro struct mountpoint *dest_mp, 200384d17192SAl Viro struct path *parent_path) 2004b90fa9aeSRam Pai { 200538129a13SAl Viro HLIST_HEAD(tree_list); 2006d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 20071064f874SEric W. Biederman struct mountpoint *smp; 2008315fc83eSAl Viro struct mount *child, *p; 200938129a13SAl Viro struct hlist_node *n; 2010719f5d7fSMiklos Szeredi int err; 2011b90fa9aeSRam Pai 20121064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 20131064f874SEric W. Biederman * to be tucked under other mounts. 20141064f874SEric W. Biederman */ 20151064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 20161064f874SEric W. Biederman if (IS_ERR(smp)) 20171064f874SEric W. Biederman return PTR_ERR(smp); 20181064f874SEric W. Biederman 2019d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 2020d2921684SEric W. Biederman if (!parent_path) { 2021d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 2022d2921684SEric W. Biederman if (err) 2023d2921684SEric W. Biederman goto out; 2024d2921684SEric W. Biederman } 2025d2921684SEric W. Biederman 2026fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 20270fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 2028719f5d7fSMiklos Szeredi if (err) 2029719f5d7fSMiklos Szeredi goto out; 203084d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 2031f2ebb3a9SAl Viro lock_mount_hash(); 2032719f5d7fSMiklos Szeredi if (err) 2033719f5d7fSMiklos Szeredi goto out_cleanup_ids; 2034909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 20350f0afb1dSAl Viro set_mnt_shared(p); 20360b1b901bSAl Viro } else { 20370b1b901bSAl Viro lock_mount_hash(); 2038b90fa9aeSRam Pai } 20391a390689SAl Viro if (parent_path) { 20400fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 204184d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 2042143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 204321444403SRam Pai } else { 204484d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 20451064f874SEric W. Biederman commit_tree(source_mnt); 204621444403SRam Pai } 2047b90fa9aeSRam Pai 204838129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 20491d6a32acSAl Viro struct mount *q; 205038129a13SAl Viro hlist_del_init(&child->mnt_hash); 20511064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 20521d6a32acSAl Viro child->mnt_mountpoint); 20531064f874SEric W. Biederman if (q) 20541064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 20551064f874SEric W. Biederman commit_tree(child); 2056b90fa9aeSRam Pai } 20571064f874SEric W. Biederman put_mountpoint(smp); 2058719ea2fbSAl Viro unlock_mount_hash(); 205999b7db7bSNick Piggin 2060b90fa9aeSRam Pai return 0; 2061719f5d7fSMiklos Szeredi 2062719f5d7fSMiklos Szeredi out_cleanup_ids: 2063f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2064f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2065d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2066e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2067f2ebb3a9SAl Viro } 2068f2ebb3a9SAl Viro unlock_mount_hash(); 20690fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2070719f5d7fSMiklos Szeredi out: 2071d2921684SEric W. Biederman ns->pending_mounts = 0; 20721064f874SEric W. Biederman 20731064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 20741064f874SEric W. Biederman put_mountpoint(smp); 20751064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 20761064f874SEric W. Biederman 2077719f5d7fSMiklos Szeredi return err; 2078b90fa9aeSRam Pai } 2079b90fa9aeSRam Pai 208084d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2081b12cea91SAl Viro { 2082b12cea91SAl Viro struct vfsmount *mnt; 208384d17192SAl Viro struct dentry *dentry = path->dentry; 2084b12cea91SAl Viro retry: 20855955102cSAl Viro inode_lock(dentry->d_inode); 208684d17192SAl Viro if (unlikely(cant_mount(dentry))) { 20875955102cSAl Viro inode_unlock(dentry->d_inode); 208884d17192SAl Viro return ERR_PTR(-ENOENT); 2089b12cea91SAl Viro } 209097216be0SAl Viro namespace_lock(); 2091b12cea91SAl Viro mnt = lookup_mnt(path); 209284d17192SAl Viro if (likely(!mnt)) { 20933895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 209484d17192SAl Viro if (IS_ERR(mp)) { 209597216be0SAl Viro namespace_unlock(); 20965955102cSAl Viro inode_unlock(dentry->d_inode); 209784d17192SAl Viro return mp; 209884d17192SAl Viro } 209984d17192SAl Viro return mp; 210084d17192SAl Viro } 210197216be0SAl Viro namespace_unlock(); 21025955102cSAl Viro inode_unlock(path->dentry->d_inode); 2103b12cea91SAl Viro path_put(path); 2104b12cea91SAl Viro path->mnt = mnt; 210584d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2106b12cea91SAl Viro goto retry; 2107b12cea91SAl Viro } 2108b12cea91SAl Viro 210984d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2110b12cea91SAl Viro { 211184d17192SAl Viro struct dentry *dentry = where->m_dentry; 21123895dbf8SEric W. Biederman 21133895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 211484d17192SAl Viro put_mountpoint(where); 21153895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 21163895dbf8SEric W. Biederman 2117328e6d90SAl Viro namespace_unlock(); 21185955102cSAl Viro inode_unlock(dentry->d_inode); 2119b12cea91SAl Viro } 2120b12cea91SAl Viro 212184d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 21221da177e4SLinus Torvalds { 212395bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 21241da177e4SLinus Torvalds return -EINVAL; 21251da177e4SLinus Torvalds 2126e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2127e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 21281da177e4SLinus Torvalds return -ENOTDIR; 21291da177e4SLinus Torvalds 213084d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 21311da177e4SLinus Torvalds } 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds /* 21347a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 21357a2e8a8fSValerie Aurora */ 21367a2e8a8fSValerie Aurora 21377a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 21387a2e8a8fSValerie Aurora { 21397c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 21407a2e8a8fSValerie Aurora 21417a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 21427a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21437a2e8a8fSValerie Aurora return 0; 21447a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 21457a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 21467a2e8a8fSValerie Aurora return 0; 21477a2e8a8fSValerie Aurora return type; 21487a2e8a8fSValerie Aurora } 21497a2e8a8fSValerie Aurora 21507a2e8a8fSValerie Aurora /* 215107b20889SRam Pai * recursively change the type of the mountpoint. 215207b20889SRam Pai */ 21530a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 215407b20889SRam Pai { 2155315fc83eSAl Viro struct mount *m; 21564b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 215707b20889SRam Pai int recurse = flag & MS_REC; 21587a2e8a8fSValerie Aurora int type; 2159719f5d7fSMiklos Szeredi int err = 0; 216007b20889SRam Pai 21612d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 216207b20889SRam Pai return -EINVAL; 216307b20889SRam Pai 21647a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 21657a2e8a8fSValerie Aurora if (!type) 21667a2e8a8fSValerie Aurora return -EINVAL; 21677a2e8a8fSValerie Aurora 216897216be0SAl Viro namespace_lock(); 2169719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2170719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2171719f5d7fSMiklos Szeredi if (err) 2172719f5d7fSMiklos Szeredi goto out_unlock; 2173719f5d7fSMiklos Szeredi } 2174719f5d7fSMiklos Szeredi 2175719ea2fbSAl Viro lock_mount_hash(); 2176909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 21770f0afb1dSAl Viro change_mnt_propagation(m, type); 2178719ea2fbSAl Viro unlock_mount_hash(); 2179719f5d7fSMiklos Szeredi 2180719f5d7fSMiklos Szeredi out_unlock: 218197216be0SAl Viro namespace_unlock(); 2182719f5d7fSMiklos Szeredi return err; 218307b20889SRam Pai } 218407b20889SRam Pai 21855ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 21865ff9d8a6SEric W. Biederman { 21875ff9d8a6SEric W. Biederman struct mount *child; 21885ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 21895ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 21905ff9d8a6SEric W. Biederman continue; 21915ff9d8a6SEric W. Biederman 21925ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 21935ff9d8a6SEric W. Biederman return true; 21945ff9d8a6SEric W. Biederman } 21955ff9d8a6SEric W. Biederman return false; 21965ff9d8a6SEric W. Biederman } 21975ff9d8a6SEric W. Biederman 219807b20889SRam Pai /* 21991da177e4SLinus Torvalds * do loopback mount. 22001da177e4SLinus Torvalds */ 2201808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 22022dafe1c4SEric Sandeen int recurse) 22031da177e4SLinus Torvalds { 22042d92ab3cSAl Viro struct path old_path; 220584d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 220684d17192SAl Viro struct mountpoint *mp; 220757eccb83SAl Viro int err; 22081da177e4SLinus Torvalds if (!old_name || !*old_name) 22091da177e4SLinus Torvalds return -EINVAL; 2210815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 22111da177e4SLinus Torvalds if (err) 22121da177e4SLinus Torvalds return err; 22131da177e4SLinus Torvalds 22148823c079SEric W. Biederman err = -EINVAL; 22154ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 22168823c079SEric W. Biederman goto out; 22178823c079SEric W. Biederman 221884d17192SAl Viro mp = lock_mount(path); 221984d17192SAl Viro err = PTR_ERR(mp); 222084d17192SAl Viro if (IS_ERR(mp)) 22219676f0c6SRam Pai goto out; 22229676f0c6SRam Pai 222387129cc0SAl Viro old = real_mount(old_path.mnt); 222484d17192SAl Viro parent = real_mount(path->mnt); 222587129cc0SAl Viro 2226b12cea91SAl Viro err = -EINVAL; 2227fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 2228b12cea91SAl Viro goto out2; 2229b12cea91SAl Viro 2230e149ed2bSAl Viro if (!check_mnt(parent)) 2231e149ed2bSAl Viro goto out2; 2232e149ed2bSAl Viro 2233e149ed2bSAl Viro if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations) 2234b12cea91SAl Viro goto out2; 2235ccd48bc7SAl Viro 22365ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 22375ff9d8a6SEric W. Biederman goto out2; 22385ff9d8a6SEric W. Biederman 22391da177e4SLinus Torvalds if (recurse) 22404ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 22411da177e4SLinus Torvalds else 224287129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 22431da177e4SLinus Torvalds 2244be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2245be34d1a3SDavid Howells err = PTR_ERR(mnt); 2246e9c5d8a5SAndrey Vagin goto out2; 2247be34d1a3SDavid Howells } 2248ccd48bc7SAl Viro 22495ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 22505ff9d8a6SEric W. Biederman 225184d17192SAl Viro err = graft_tree(mnt, parent, mp); 22521da177e4SLinus Torvalds if (err) { 2253719ea2fbSAl Viro lock_mount_hash(); 2254e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2255719ea2fbSAl Viro unlock_mount_hash(); 22565b83d2c5SRam Pai } 2257b12cea91SAl Viro out2: 225884d17192SAl Viro unlock_mount(mp); 2259ccd48bc7SAl Viro out: 22602d92ab3cSAl Viro path_put(&old_path); 22611da177e4SLinus Torvalds return err; 22621da177e4SLinus Torvalds } 22631da177e4SLinus Torvalds 22642e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 22652e4b7fcdSDave Hansen { 22662e4b7fcdSDave Hansen int error = 0; 22672e4b7fcdSDave Hansen int readonly_request = 0; 22682e4b7fcdSDave Hansen 22692e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 22702e4b7fcdSDave Hansen readonly_request = 1; 22712e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 22722e4b7fcdSDave Hansen return 0; 22732e4b7fcdSDave Hansen 22742e4b7fcdSDave Hansen if (readonly_request) 227583adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 22762e4b7fcdSDave Hansen else 227783adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 22782e4b7fcdSDave Hansen return error; 22792e4b7fcdSDave Hansen } 22802e4b7fcdSDave Hansen 22811da177e4SLinus Torvalds /* 22821da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 22831da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 22841da177e4SLinus Torvalds * on it - tough luck. 22851da177e4SLinus Torvalds */ 22860a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 22871da177e4SLinus Torvalds void *data) 22881da177e4SLinus Torvalds { 22891da177e4SLinus Torvalds int err; 22902d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2291143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 22921da177e4SLinus Torvalds 2293143c8c91SAl Viro if (!check_mnt(mnt)) 22941da177e4SLinus Torvalds return -EINVAL; 22951da177e4SLinus Torvalds 22962d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 22971da177e4SLinus Torvalds return -EINVAL; 22981da177e4SLinus Torvalds 229907b64558SEric W. Biederman /* Don't allow changing of locked mnt flags. 230007b64558SEric W. Biederman * 230107b64558SEric W. Biederman * No locks need to be held here while testing the various 230207b64558SEric W. Biederman * MNT_LOCK flags because those flags can never be cleared 230307b64558SEric W. Biederman * once they are set. 230407b64558SEric W. Biederman */ 230507b64558SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) && 230607b64558SEric W. Biederman !(mnt_flags & MNT_READONLY)) { 230707b64558SEric W. Biederman return -EPERM; 230807b64558SEric W. Biederman } 23099566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) && 23109566d674SEric W. Biederman !(mnt_flags & MNT_NODEV)) { 23119566d674SEric W. Biederman return -EPERM; 23129566d674SEric W. Biederman } 23139566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) && 23149566d674SEric W. Biederman !(mnt_flags & MNT_NOSUID)) { 23159566d674SEric W. Biederman return -EPERM; 23169566d674SEric W. Biederman } 23179566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) && 23189566d674SEric W. Biederman !(mnt_flags & MNT_NOEXEC)) { 23199566d674SEric W. Biederman return -EPERM; 23209566d674SEric W. Biederman } 23219566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) && 23229566d674SEric W. Biederman ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) { 23239566d674SEric W. Biederman return -EPERM; 23249566d674SEric W. Biederman } 23259566d674SEric W. Biederman 2326ff36fe2cSEric Paris err = security_sb_remount(sb, data); 2327ff36fe2cSEric Paris if (err) 2328ff36fe2cSEric Paris return err; 2329ff36fe2cSEric Paris 23301da177e4SLinus Torvalds down_write(&sb->s_umount); 23312e4b7fcdSDave Hansen if (flags & MS_BIND) 23322d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 233357eccb83SAl Viro else if (!capable(CAP_SYS_ADMIN)) 233457eccb83SAl Viro err = -EPERM; 23354aa98cf7SAl Viro else 23361da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 23377b43a79fSAl Viro if (!err) { 2338719ea2fbSAl Viro lock_mount_hash(); 2339a6138db8SEric W. Biederman mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 2340143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 2341143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2342719ea2fbSAl Viro unlock_mount_hash(); 23430e55a7ccSDan Williams } 23446339dab8SAl Viro up_write(&sb->s_umount); 23451da177e4SLinus Torvalds return err; 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds 2348cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 23499676f0c6SRam Pai { 2350315fc83eSAl Viro struct mount *p; 2351909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2352fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 23539676f0c6SRam Pai return 1; 23549676f0c6SRam Pai } 23559676f0c6SRam Pai return 0; 23569676f0c6SRam Pai } 23579676f0c6SRam Pai 2358808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 23591da177e4SLinus Torvalds { 23602d92ab3cSAl Viro struct path old_path, parent_path; 2361676da58dSAl Viro struct mount *p; 23620fb54e50SAl Viro struct mount *old; 236384d17192SAl Viro struct mountpoint *mp; 236457eccb83SAl Viro int err; 23651da177e4SLinus Torvalds if (!old_name || !*old_name) 23661da177e4SLinus Torvalds return -EINVAL; 23672d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 23681da177e4SLinus Torvalds if (err) 23691da177e4SLinus Torvalds return err; 23701da177e4SLinus Torvalds 237184d17192SAl Viro mp = lock_mount(path); 237284d17192SAl Viro err = PTR_ERR(mp); 237384d17192SAl Viro if (IS_ERR(mp)) 2374cc53ce53SDavid Howells goto out; 2375cc53ce53SDavid Howells 2376143c8c91SAl Viro old = real_mount(old_path.mnt); 2377fc7be130SAl Viro p = real_mount(path->mnt); 2378143c8c91SAl Viro 23791da177e4SLinus Torvalds err = -EINVAL; 2380fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 23811da177e4SLinus Torvalds goto out1; 23821da177e4SLinus Torvalds 23835ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 23845ff9d8a6SEric W. Biederman goto out1; 23855ff9d8a6SEric W. Biederman 23861da177e4SLinus Torvalds err = -EINVAL; 23872d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 238821444403SRam Pai goto out1; 23891da177e4SLinus Torvalds 2390676da58dSAl Viro if (!mnt_has_parent(old)) 239121444403SRam Pai goto out1; 23921da177e4SLinus Torvalds 2393e36cb0b8SDavid Howells if (d_is_dir(path->dentry) != 2394e36cb0b8SDavid Howells d_is_dir(old_path.dentry)) 239521444403SRam Pai goto out1; 239621444403SRam Pai /* 239721444403SRam Pai * Don't move a mount residing in a shared parent. 239821444403SRam Pai */ 2399fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 240021444403SRam Pai goto out1; 24019676f0c6SRam Pai /* 24029676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 24039676f0c6SRam Pai * mount which is shared. 24049676f0c6SRam Pai */ 2405fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 24069676f0c6SRam Pai goto out1; 24071da177e4SLinus Torvalds err = -ELOOP; 2408fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2409676da58dSAl Viro if (p == old) 241021444403SRam Pai goto out1; 24111da177e4SLinus Torvalds 241284d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 24134ac91378SJan Blunck if (err) 241421444403SRam Pai goto out1; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 24171da177e4SLinus Torvalds * automatically */ 24186776db3dSAl Viro list_del_init(&old->mnt_expire); 24191da177e4SLinus Torvalds out1: 242084d17192SAl Viro unlock_mount(mp); 24211da177e4SLinus Torvalds out: 24221da177e4SLinus Torvalds if (!err) 24231a390689SAl Viro path_put(&parent_path); 24242d92ab3cSAl Viro path_put(&old_path); 24251da177e4SLinus Torvalds return err; 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds 24289d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 24299d412a43SAl Viro { 24309d412a43SAl Viro int err; 24319d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 24329d412a43SAl Viro if (subtype) { 24339d412a43SAl Viro subtype++; 24349d412a43SAl Viro err = -EINVAL; 24359d412a43SAl Viro if (!subtype[0]) 24369d412a43SAl Viro goto err; 24379d412a43SAl Viro } else 24389d412a43SAl Viro subtype = ""; 24399d412a43SAl Viro 24409d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 24419d412a43SAl Viro err = -ENOMEM; 24429d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 24439d412a43SAl Viro goto err; 24449d412a43SAl Viro return mnt; 24459d412a43SAl Viro 24469d412a43SAl Viro err: 24479d412a43SAl Viro mntput(mnt); 24489d412a43SAl Viro return ERR_PTR(err); 24499d412a43SAl Viro } 24509d412a43SAl Viro 24519d412a43SAl Viro /* 24529d412a43SAl Viro * add a mount into a namespace's mount tree 24539d412a43SAl Viro */ 245495bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 24559d412a43SAl Viro { 245684d17192SAl Viro struct mountpoint *mp; 245784d17192SAl Viro struct mount *parent; 24589d412a43SAl Viro int err; 24599d412a43SAl Viro 2460f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 24619d412a43SAl Viro 246284d17192SAl Viro mp = lock_mount(path); 246384d17192SAl Viro if (IS_ERR(mp)) 246484d17192SAl Viro return PTR_ERR(mp); 24659d412a43SAl Viro 246684d17192SAl Viro parent = real_mount(path->mnt); 24679d412a43SAl Viro err = -EINVAL; 246884d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2469156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2470156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 24719d412a43SAl Viro goto unlock; 2472156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 247384d17192SAl Viro if (!parent->mnt_ns) 2474156cacb1SAl Viro goto unlock; 2475156cacb1SAl Viro } 24769d412a43SAl Viro 24779d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 24789d412a43SAl Viro err = -EBUSY; 247995bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 24809d412a43SAl Viro path->mnt->mnt_root == path->dentry) 24819d412a43SAl Viro goto unlock; 24829d412a43SAl Viro 24839d412a43SAl Viro err = -EINVAL; 2484e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 24859d412a43SAl Viro goto unlock; 24869d412a43SAl Viro 248795bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 248884d17192SAl Viro err = graft_tree(newmnt, parent, mp); 24899d412a43SAl Viro 24909d412a43SAl Viro unlock: 249184d17192SAl Viro unlock_mount(mp); 24929d412a43SAl Viro return err; 24939d412a43SAl Viro } 2494b1e75df4SAl Viro 24958654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags); 24961b852bceSEric W. Biederman 24971da177e4SLinus Torvalds /* 24981da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 24991da177e4SLinus Torvalds * namespace's tree 25001da177e4SLinus Torvalds */ 25010c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags, 2502808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 25031da177e4SLinus Torvalds { 25040c55cfc4SEric W. Biederman struct file_system_type *type; 25051da177e4SLinus Torvalds struct vfsmount *mnt; 250615f9a3f3SAl Viro int err; 25071da177e4SLinus Torvalds 25080c55cfc4SEric W. Biederman if (!fstype) 25091da177e4SLinus Torvalds return -EINVAL; 25101da177e4SLinus Torvalds 25110c55cfc4SEric W. Biederman type = get_fs_type(fstype); 25120c55cfc4SEric W. Biederman if (!type) 25130c55cfc4SEric W. Biederman return -ENODEV; 25140c55cfc4SEric W. Biederman 25150c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, flags, name, data); 25160c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 25170c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 25180c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 25190c55cfc4SEric W. Biederman 25200c55cfc4SEric W. Biederman put_filesystem(type); 25211da177e4SLinus Torvalds if (IS_ERR(mnt)) 25221da177e4SLinus Torvalds return PTR_ERR(mnt); 25231da177e4SLinus Torvalds 25248654df4eSEric W. Biederman if (mount_too_revealing(mnt, &mnt_flags)) { 25258654df4eSEric W. Biederman mntput(mnt); 25268654df4eSEric W. Biederman return -EPERM; 25278654df4eSEric W. Biederman } 25288654df4eSEric W. Biederman 252995bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 253015f9a3f3SAl Viro if (err) 253115f9a3f3SAl Viro mntput(mnt); 253215f9a3f3SAl Viro return err; 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds 253519a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 253619a167afSAl Viro { 25376776db3dSAl Viro struct mount *mnt = real_mount(m); 253819a167afSAl Viro int err; 253919a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 254019a167afSAl Viro * expired before we get a chance to add it 254119a167afSAl Viro */ 25426776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 254319a167afSAl Viro 254419a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 254519a167afSAl Viro m->mnt_root == path->dentry) { 2546b1e75df4SAl Viro err = -ELOOP; 2547b1e75df4SAl Viro goto fail; 254819a167afSAl Viro } 254919a167afSAl Viro 255095bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2551b1e75df4SAl Viro if (!err) 2552b1e75df4SAl Viro return 0; 2553b1e75df4SAl Viro fail: 2554b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 25556776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 255697216be0SAl Viro namespace_lock(); 25576776db3dSAl Viro list_del_init(&mnt->mnt_expire); 255897216be0SAl Viro namespace_unlock(); 255919a167afSAl Viro } 2560b1e75df4SAl Viro mntput(m); 2561b1e75df4SAl Viro mntput(m); 256219a167afSAl Viro return err; 256319a167afSAl Viro } 256419a167afSAl Viro 2565ea5b778aSDavid Howells /** 2566ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2567ea5b778aSDavid Howells * @mnt: The mount to list. 2568ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2569ea5b778aSDavid Howells */ 2570ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2571ea5b778aSDavid Howells { 257297216be0SAl Viro namespace_lock(); 2573ea5b778aSDavid Howells 25746776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2575ea5b778aSDavid Howells 257697216be0SAl Viro namespace_unlock(); 2577ea5b778aSDavid Howells } 2578ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2579ea5b778aSDavid Howells 2580ea5b778aSDavid Howells /* 25811da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 25821da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 25831da177e4SLinus Torvalds * here 25841da177e4SLinus Torvalds */ 25851da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 25861da177e4SLinus Torvalds { 2587761d5c38SAl Viro struct mount *mnt, *next; 25881da177e4SLinus Torvalds LIST_HEAD(graveyard); 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds if (list_empty(mounts)) 25911da177e4SLinus Torvalds return; 25921da177e4SLinus Torvalds 259397216be0SAl Viro namespace_lock(); 2594719ea2fbSAl Viro lock_mount_hash(); 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 25971da177e4SLinus Torvalds * following criteria: 25981da177e4SLinus Torvalds * - only referenced by its parent vfsmount 25991da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 26001da177e4SLinus Torvalds * cleared by mntput()) 26011da177e4SLinus Torvalds */ 26026776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2603863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 26041ab59738SAl Viro propagate_mount_busy(mnt, 1)) 26051da177e4SLinus Torvalds continue; 26066776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 26071da177e4SLinus Torvalds } 2608bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 26096776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2610143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2611e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2612bcc5c7d2SAl Viro } 2613719ea2fbSAl Viro unlock_mount_hash(); 26143ab6abeeSAl Viro namespace_unlock(); 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 26181da177e4SLinus Torvalds 26191da177e4SLinus Torvalds /* 26205528f911STrond Myklebust * Ripoff of 'select_parent()' 26215528f911STrond Myklebust * 26225528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 26235528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 26245528f911STrond Myklebust */ 2625692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 26265528f911STrond Myklebust { 2627692afc31SAl Viro struct mount *this_parent = parent; 26285528f911STrond Myklebust struct list_head *next; 26295528f911STrond Myklebust int found = 0; 26305528f911STrond Myklebust 26315528f911STrond Myklebust repeat: 26326b41d536SAl Viro next = this_parent->mnt_mounts.next; 26335528f911STrond Myklebust resume: 26346b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 26355528f911STrond Myklebust struct list_head *tmp = next; 26366b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 26375528f911STrond Myklebust 26385528f911STrond Myklebust next = tmp->next; 2639692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 26405528f911STrond Myklebust continue; 26415528f911STrond Myklebust /* 26425528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 26435528f911STrond Myklebust */ 26446b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 26455528f911STrond Myklebust this_parent = mnt; 26465528f911STrond Myklebust goto repeat; 26475528f911STrond Myklebust } 26485528f911STrond Myklebust 26491ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 26506776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 26515528f911STrond Myklebust found++; 26525528f911STrond Myklebust } 26535528f911STrond Myklebust } 26545528f911STrond Myklebust /* 26555528f911STrond Myklebust * All done at this level ... ascend and resume the search 26565528f911STrond Myklebust */ 26575528f911STrond Myklebust if (this_parent != parent) { 26586b41d536SAl Viro next = this_parent->mnt_child.next; 26590714a533SAl Viro this_parent = this_parent->mnt_parent; 26605528f911STrond Myklebust goto resume; 26615528f911STrond Myklebust } 26625528f911STrond Myklebust return found; 26635528f911STrond Myklebust } 26645528f911STrond Myklebust 26655528f911STrond Myklebust /* 26665528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 26675528f911STrond Myklebust * submounts of a specific parent mountpoint 266899b7db7bSNick Piggin * 266948a066e7SAl Viro * mount_lock must be held for write 26705528f911STrond Myklebust */ 2671b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 26725528f911STrond Myklebust { 26735528f911STrond Myklebust LIST_HEAD(graveyard); 2674761d5c38SAl Viro struct mount *m; 26755528f911STrond Myklebust 26765528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2677c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2678bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2679761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 26806776db3dSAl Viro mnt_expire); 2681143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2682e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2683bcc5c7d2SAl Viro } 2684bcc5c7d2SAl Viro } 26855528f911STrond Myklebust } 26865528f911STrond Myklebust 26875528f911STrond Myklebust /* 26881da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 26891da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 26901da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 26911da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 26921da177e4SLinus Torvalds */ 2693b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2694b58fed8bSRam Pai unsigned long n) 26951da177e4SLinus Torvalds { 26961da177e4SLinus Torvalds char *t = to; 26971da177e4SLinus Torvalds const char __user *f = from; 26981da177e4SLinus Torvalds char c; 26991da177e4SLinus Torvalds 27001da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 27011da177e4SLinus Torvalds return n; 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds while (n) { 27041da177e4SLinus Torvalds if (__get_user(c, f)) { 27051da177e4SLinus Torvalds memset(t, 0, n); 27061da177e4SLinus Torvalds break; 27071da177e4SLinus Torvalds } 27081da177e4SLinus Torvalds *t++ = c; 27091da177e4SLinus Torvalds f++; 27101da177e4SLinus Torvalds n--; 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds return n; 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds 2715b40ef869SAl Viro void *copy_mount_options(const void __user * data) 27161da177e4SLinus Torvalds { 27171da177e4SLinus Torvalds int i; 27181da177e4SLinus Torvalds unsigned long size; 2719b40ef869SAl Viro char *copy; 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds if (!data) 2722b40ef869SAl Viro return NULL; 27231da177e4SLinus Torvalds 2724b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 2725b40ef869SAl Viro if (!copy) 2726b40ef869SAl Viro return ERR_PTR(-ENOMEM); 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds /* We only care that *some* data at the address the user 27291da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 27301da177e4SLinus Torvalds * the remainder of the page. 27311da177e4SLinus Torvalds */ 27321da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 27331da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 27341da177e4SLinus Torvalds if (size > PAGE_SIZE) 27351da177e4SLinus Torvalds size = PAGE_SIZE; 27361da177e4SLinus Torvalds 2737b40ef869SAl Viro i = size - exact_copy_from_user(copy, data, size); 27381da177e4SLinus Torvalds if (!i) { 2739b40ef869SAl Viro kfree(copy); 2740b40ef869SAl Viro return ERR_PTR(-EFAULT); 27411da177e4SLinus Torvalds } 27421da177e4SLinus Torvalds if (i != PAGE_SIZE) 2743b40ef869SAl Viro memset(copy + i, 0, PAGE_SIZE - i); 2744b40ef869SAl Viro return copy; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 2747b8850d1fSTim Gardner char *copy_mount_string(const void __user *data) 2748eca6f534SVegard Nossum { 2749b8850d1fSTim Gardner return data ? strndup_user(data, PAGE_SIZE) : NULL; 2750eca6f534SVegard Nossum } 2751eca6f534SVegard Nossum 27521da177e4SLinus Torvalds /* 27531da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 27541da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 27551da177e4SLinus Torvalds * 27561da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 27571da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 27581da177e4SLinus Torvalds * information (or be NULL). 27591da177e4SLinus Torvalds * 27601da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 27611da177e4SLinus Torvalds * When the flags word was introduced its top half was required 27621da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 27631da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 27641da177e4SLinus Torvalds * and must be discarded. 27651da177e4SLinus Torvalds */ 27665e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name, 2767808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 27681da177e4SLinus Torvalds { 27692d92ab3cSAl Viro struct path path; 27701da177e4SLinus Torvalds int retval = 0; 27711da177e4SLinus Torvalds int mnt_flags = 0; 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds /* Discard magic */ 27741da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 27751da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds /* Basic sanity checks */ 27781da177e4SLinus Torvalds if (data_page) 27791da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 27801da177e4SLinus Torvalds 2781a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 27825e6123f3SSeunghun Lee retval = user_path(dir_name, &path); 2783a27ab9f2STetsuo Handa if (retval) 2784a27ab9f2STetsuo Handa return retval; 2785a27ab9f2STetsuo Handa 2786a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2787a27ab9f2STetsuo Handa type_page, flags, data_page); 27880d5cadb8SAl Viro if (!retval && !may_mount()) 27890d5cadb8SAl Viro retval = -EPERM; 27909e8925b6SJeff Layton if (!retval && (flags & MS_MANDLOCK) && !may_mandlock()) 27919e8925b6SJeff Layton retval = -EPERM; 2792a27ab9f2STetsuo Handa if (retval) 2793a27ab9f2STetsuo Handa goto dput_out; 2794a27ab9f2STetsuo Handa 2795613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2796613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 27970a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 27980a1c01c9SMatthew Garrett 27991da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 28001da177e4SLinus Torvalds if (flags & MS_NOSUID) 28011da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 28021da177e4SLinus Torvalds if (flags & MS_NODEV) 28031da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 28041da177e4SLinus Torvalds if (flags & MS_NOEXEC) 28051da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2806fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2807fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2808fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2809fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2810d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2811d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 28122e4b7fcdSDave Hansen if (flags & MS_RDONLY) 28132e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2814fc33a7bbSChristoph Hellwig 2815ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 2816ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 2817ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 2818ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 2819ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 2820ffbc6f0eSEric W. Biederman mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; 2821ffbc6f0eSEric W. Biederman } 2822ffbc6f0eSEric W. Biederman 28237a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2824d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 282593faccbbSEric W. Biederman MS_STRICTATIME | MS_NOREMOTELOCK | MS_SUBMOUNT); 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds if (flags & MS_REMOUNT) 28282d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 28291da177e4SLinus Torvalds data_page); 28301da177e4SLinus Torvalds else if (flags & MS_BIND) 28312d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 28329676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 28332d92ab3cSAl Viro retval = do_change_type(&path, flags); 28341da177e4SLinus Torvalds else if (flags & MS_MOVE) 28352d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 28361da177e4SLinus Torvalds else 28372d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 28381da177e4SLinus Torvalds dev_name, data_page); 28391da177e4SLinus Torvalds dput_out: 28402d92ab3cSAl Viro path_put(&path); 28411da177e4SLinus Torvalds return retval; 28421da177e4SLinus Torvalds } 28431da177e4SLinus Torvalds 2844537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 2845537f7ccbSEric W. Biederman { 2846537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 2847537f7ccbSEric W. Biederman } 2848537f7ccbSEric W. Biederman 2849537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 2850537f7ccbSEric W. Biederman { 2851537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 2852537f7ccbSEric W. Biederman } 2853537f7ccbSEric W. Biederman 2854771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2855771b1371SEric W. Biederman { 28566344c433SAl Viro ns_free_inum(&ns->ns); 2857537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 2858771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2859771b1371SEric W. Biederman kfree(ns); 2860771b1371SEric W. Biederman } 2861771b1371SEric W. Biederman 28628823c079SEric W. Biederman /* 28638823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 28648823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 28658823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 28668823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 28678823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 28688823c079SEric W. Biederman */ 28698823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 28708823c079SEric W. Biederman 2871771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2872cf8d2c11STrond Myklebust { 2873cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2874537f7ccbSEric W. Biederman struct ucounts *ucounts; 287598f842e6SEric W. Biederman int ret; 2876cf8d2c11STrond Myklebust 2877537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 2878537f7ccbSEric W. Biederman if (!ucounts) 2879df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 2880537f7ccbSEric W. Biederman 2881cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2882537f7ccbSEric W. Biederman if (!new_ns) { 2883537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 2884cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2885537f7ccbSEric W. Biederman } 28866344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 288798f842e6SEric W. Biederman if (ret) { 288898f842e6SEric W. Biederman kfree(new_ns); 2889537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 289098f842e6SEric W. Biederman return ERR_PTR(ret); 289198f842e6SEric W. Biederman } 289233c42940SAl Viro new_ns->ns.ops = &mntns_operations; 28938823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2894cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2895cf8d2c11STrond Myklebust new_ns->root = NULL; 2896cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2897cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2898cf8d2c11STrond Myklebust new_ns->event = 0; 2899771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2900537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 2901d2921684SEric W. Biederman new_ns->mounts = 0; 2902d2921684SEric W. Biederman new_ns->pending_mounts = 0; 2903cf8d2c11STrond Myklebust return new_ns; 2904cf8d2c11STrond Myklebust } 2905cf8d2c11STrond Myklebust 29060766f788SEmese Revfy __latent_entropy 29079559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 29089559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 29091da177e4SLinus Torvalds { 29106b3286edSKirill Korotaev struct mnt_namespace *new_ns; 29117f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2912315fc83eSAl Viro struct mount *p, *q; 29139559f689SAl Viro struct mount *old; 2914cb338d06SAl Viro struct mount *new; 29157a472ef4SEric W. Biederman int copy_flags; 29161da177e4SLinus Torvalds 29179559f689SAl Viro BUG_ON(!ns); 29189559f689SAl Viro 29199559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 29209559f689SAl Viro get_mnt_ns(ns); 29219559f689SAl Viro return ns; 29229559f689SAl Viro } 29239559f689SAl Viro 29249559f689SAl Viro old = ns->root; 29259559f689SAl Viro 2926771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2927cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2928cf8d2c11STrond Myklebust return new_ns; 29291da177e4SLinus Torvalds 293097216be0SAl Viro namespace_lock(); 29311da177e4SLinus Torvalds /* First pass: copy the tree topology */ 29324ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 29339559f689SAl Viro if (user_ns != ns->user_ns) 2934132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 29357a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2936be34d1a3SDavid Howells if (IS_ERR(new)) { 2937328e6d90SAl Viro namespace_unlock(); 2938771b1371SEric W. Biederman free_mnt_ns(new_ns); 2939be34d1a3SDavid Howells return ERR_CAST(new); 29401da177e4SLinus Torvalds } 2941be08d6d2SAl Viro new_ns->root = new; 29421a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds /* 29451da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 29461da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 29471da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 29481da177e4SLinus Torvalds */ 2949909b0a88SAl Viro p = old; 2950cb338d06SAl Viro q = new; 29511da177e4SLinus Torvalds while (p) { 2952143c8c91SAl Viro q->mnt_ns = new_ns; 2953d2921684SEric W. Biederman new_ns->mounts++; 29549559f689SAl Viro if (new_fs) { 29559559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 29569559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2957315fc83eSAl Viro rootmnt = &p->mnt; 29581da177e4SLinus Torvalds } 29599559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 29609559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2961315fc83eSAl Viro pwdmnt = &p->mnt; 29621da177e4SLinus Torvalds } 29631da177e4SLinus Torvalds } 2964909b0a88SAl Viro p = next_mnt(p, old); 2965909b0a88SAl Viro q = next_mnt(q, new); 29664ce5d2b1SEric W. Biederman if (!q) 29674ce5d2b1SEric W. Biederman break; 29684ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 29694ce5d2b1SEric W. Biederman p = next_mnt(p, old); 29701da177e4SLinus Torvalds } 2971328e6d90SAl Viro namespace_unlock(); 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds if (rootmnt) 2974f03c6599SAl Viro mntput(rootmnt); 29751da177e4SLinus Torvalds if (pwdmnt) 2976f03c6599SAl Viro mntput(pwdmnt); 29771da177e4SLinus Torvalds 2978741a2951SJANAK DESAI return new_ns; 2979741a2951SJANAK DESAI } 2980741a2951SJANAK DESAI 2981cf8d2c11STrond Myklebust /** 2982cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2983cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2984cf8d2c11STrond Myklebust */ 29851a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2986cf8d2c11STrond Myklebust { 2987771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2988cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 29891a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 29901a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2991be08d6d2SAl Viro new_ns->root = mnt; 2992d2921684SEric W. Biederman new_ns->mounts++; 2993b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 2994c1334495SAl Viro } else { 29951a4eeaf2SAl Viro mntput(m); 2996cf8d2c11STrond Myklebust } 2997cf8d2c11STrond Myklebust return new_ns; 2998cf8d2c11STrond Myklebust } 2999cf8d2c11STrond Myklebust 3000ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 3001ea441d11SAl Viro { 3002ea441d11SAl Viro struct mnt_namespace *ns; 3003d31da0f0SAl Viro struct super_block *s; 3004ea441d11SAl Viro struct path path; 3005ea441d11SAl Viro int err; 3006ea441d11SAl Viro 3007ea441d11SAl Viro ns = create_mnt_ns(mnt); 3008ea441d11SAl Viro if (IS_ERR(ns)) 3009ea441d11SAl Viro return ERR_CAST(ns); 3010ea441d11SAl Viro 3011ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 3012ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 3013ea441d11SAl Viro 3014ea441d11SAl Viro put_mnt_ns(ns); 3015ea441d11SAl Viro 3016ea441d11SAl Viro if (err) 3017ea441d11SAl Viro return ERR_PTR(err); 3018ea441d11SAl Viro 3019ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 3020d31da0f0SAl Viro s = path.mnt->mnt_sb; 3021d31da0f0SAl Viro atomic_inc(&s->s_active); 3022ea441d11SAl Viro mntput(path.mnt); 3023ea441d11SAl Viro /* lock the sucker */ 3024d31da0f0SAl Viro down_write(&s->s_umount); 3025ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 3026ea441d11SAl Viro return path.dentry; 3027ea441d11SAl Viro } 3028ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 3029ea441d11SAl Viro 3030bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 3031bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 30321da177e4SLinus Torvalds { 3033eca6f534SVegard Nossum int ret; 3034eca6f534SVegard Nossum char *kernel_type; 3035eca6f534SVegard Nossum char *kernel_dev; 3036b40ef869SAl Viro void *options; 30371da177e4SLinus Torvalds 3038b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 3039b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 3040b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 3041eca6f534SVegard Nossum goto out_type; 30421da177e4SLinus Torvalds 3043b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 3044b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 3045b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 3046eca6f534SVegard Nossum goto out_dev; 30471da177e4SLinus Torvalds 3048b40ef869SAl Viro options = copy_mount_options(data); 3049b40ef869SAl Viro ret = PTR_ERR(options); 3050b40ef869SAl Viro if (IS_ERR(options)) 3051eca6f534SVegard Nossum goto out_data; 30521da177e4SLinus Torvalds 3053b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 3054eca6f534SVegard Nossum 3055b40ef869SAl Viro kfree(options); 3056eca6f534SVegard Nossum out_data: 3057eca6f534SVegard Nossum kfree(kernel_dev); 3058eca6f534SVegard Nossum out_dev: 3059eca6f534SVegard Nossum kfree(kernel_type); 3060eca6f534SVegard Nossum out_type: 3061eca6f534SVegard Nossum return ret; 30621da177e4SLinus Torvalds } 30631da177e4SLinus Torvalds 30641da177e4SLinus Torvalds /* 3065afac7cbaSAl Viro * Return true if path is reachable from root 3066afac7cbaSAl Viro * 306748a066e7SAl Viro * namespace_sem or mount_lock is held 3068afac7cbaSAl Viro */ 3069643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3070afac7cbaSAl Viro const struct path *root) 3071afac7cbaSAl Viro { 3072643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3073a73324daSAl Viro dentry = mnt->mnt_mountpoint; 30740714a533SAl Viro mnt = mnt->mnt_parent; 3075afac7cbaSAl Viro } 3076643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3077afac7cbaSAl Viro } 3078afac7cbaSAl Viro 3079640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3080afac7cbaSAl Viro { 308125ab4c9bSYaowei Bai bool res; 308248a066e7SAl Viro read_seqlock_excl(&mount_lock); 3083643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 308448a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3085afac7cbaSAl Viro return res; 3086afac7cbaSAl Viro } 3087afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3088afac7cbaSAl Viro 3089afac7cbaSAl Viro /* 30901da177e4SLinus Torvalds * pivot_root Semantics: 30911da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 30921da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 30931da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 30941da177e4SLinus Torvalds * 30951da177e4SLinus Torvalds * Restrictions: 30961da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 30971da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 30981da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 30991da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 31001da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 31011da177e4SLinus Torvalds * 31024a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 31034a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 31044a0d11faSNeil Brown * in this situation. 31054a0d11faSNeil Brown * 31061da177e4SLinus Torvalds * Notes: 31071da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 31081da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 31091da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 31101da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 31111da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 31121da177e4SLinus Torvalds * first. 31131da177e4SLinus Torvalds */ 31143480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 31153480b257SHeiko Carstens const char __user *, put_old) 31161da177e4SLinus Torvalds { 31172d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 311884d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 311984d17192SAl Viro struct mountpoint *old_mp, *root_mp; 31201da177e4SLinus Torvalds int error; 31211da177e4SLinus Torvalds 31229b40bc90SAl Viro if (!may_mount()) 31231da177e4SLinus Torvalds return -EPERM; 31241da177e4SLinus Torvalds 31252d8f3038SAl Viro error = user_path_dir(new_root, &new); 31261da177e4SLinus Torvalds if (error) 31271da177e4SLinus Torvalds goto out0; 31281da177e4SLinus Torvalds 31292d8f3038SAl Viro error = user_path_dir(put_old, &old); 31301da177e4SLinus Torvalds if (error) 31311da177e4SLinus Torvalds goto out1; 31321da177e4SLinus Torvalds 31332d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3134b12cea91SAl Viro if (error) 3135b12cea91SAl Viro goto out2; 31361da177e4SLinus Torvalds 3137f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 313884d17192SAl Viro old_mp = lock_mount(&old); 313984d17192SAl Viro error = PTR_ERR(old_mp); 314084d17192SAl Viro if (IS_ERR(old_mp)) 3141b12cea91SAl Viro goto out3; 3142b12cea91SAl Viro 31431da177e4SLinus Torvalds error = -EINVAL; 3144419148daSAl Viro new_mnt = real_mount(new.mnt); 3145419148daSAl Viro root_mnt = real_mount(root.mnt); 314684d17192SAl Viro old_mnt = real_mount(old.mnt); 314784d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 3148fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 3149fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 3150b12cea91SAl Viro goto out4; 3151143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3152b12cea91SAl Viro goto out4; 31535ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 31545ff9d8a6SEric W. Biederman goto out4; 31551da177e4SLinus Torvalds error = -ENOENT; 3156f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3157b12cea91SAl Viro goto out4; 31581da177e4SLinus Torvalds error = -EBUSY; 315984d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3160b12cea91SAl Viro goto out4; /* loop, on the same file system */ 31611da177e4SLinus Torvalds error = -EINVAL; 31628c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3163b12cea91SAl Viro goto out4; /* not a mountpoint */ 3164676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3165b12cea91SAl Viro goto out4; /* not attached */ 316684d17192SAl Viro root_mp = root_mnt->mnt_mp; 31672d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3168b12cea91SAl Viro goto out4; /* not a mountpoint */ 3169676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3170b12cea91SAl Viro goto out4; /* not attached */ 31714ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 317284d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3173b12cea91SAl Viro goto out4; 31740d082601SEric W. Biederman /* make certain new is below the root */ 31750d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 31760d082601SEric W. Biederman goto out4; 317784d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 3178719ea2fbSAl Viro lock_mount_hash(); 3179419148daSAl Viro detach_mnt(new_mnt, &parent_path); 3180419148daSAl Viro detach_mnt(root_mnt, &root_parent); 31815ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 31825ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 31835ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 31845ff9d8a6SEric W. Biederman } 31854ac91378SJan Blunck /* mount old root on put_old */ 318684d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 31874ac91378SJan Blunck /* mount new_root on / */ 318884d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 31896b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 31904fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 31914fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 31923895dbf8SEric W. Biederman put_mountpoint(root_mp); 3193719ea2fbSAl Viro unlock_mount_hash(); 31942d8f3038SAl Viro chroot_fs_refs(&root, &new); 31951da177e4SLinus Torvalds error = 0; 3196b12cea91SAl Viro out4: 319784d17192SAl Viro unlock_mount(old_mp); 3198b12cea91SAl Viro if (!error) { 31991a390689SAl Viro path_put(&root_parent); 32001a390689SAl Viro path_put(&parent_path); 3201b12cea91SAl Viro } 3202b12cea91SAl Viro out3: 32038c3ee42eSAl Viro path_put(&root); 3204b12cea91SAl Viro out2: 32052d8f3038SAl Viro path_put(&old); 32061da177e4SLinus Torvalds out1: 32072d8f3038SAl Viro path_put(&new); 32081da177e4SLinus Torvalds out0: 32091da177e4SLinus Torvalds return error; 32101da177e4SLinus Torvalds } 32111da177e4SLinus Torvalds 32121da177e4SLinus Torvalds static void __init init_mount_tree(void) 32131da177e4SLinus Torvalds { 32141da177e4SLinus Torvalds struct vfsmount *mnt; 32156b3286edSKirill Korotaev struct mnt_namespace *ns; 3216ac748a09SJan Blunck struct path root; 32170c55cfc4SEric W. Biederman struct file_system_type *type; 32181da177e4SLinus Torvalds 32190c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 32200c55cfc4SEric W. Biederman if (!type) 32210c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 32220c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 32230c55cfc4SEric W. Biederman put_filesystem(type); 32241da177e4SLinus Torvalds if (IS_ERR(mnt)) 32251da177e4SLinus Torvalds panic("Can't create rootfs"); 3226b3e19d92SNick Piggin 32273b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 32283b22edc5STrond Myklebust if (IS_ERR(ns)) 32291da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 32301da177e4SLinus Torvalds 32316b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 32326b3286edSKirill Korotaev get_mnt_ns(ns); 32331da177e4SLinus Torvalds 3234be08d6d2SAl Viro root.mnt = mnt; 3235be08d6d2SAl Viro root.dentry = mnt->mnt_root; 3236da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 3237ac748a09SJan Blunck 3238ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 3239ac748a09SJan Blunck set_fs_root(current->fs, &root); 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds 324274bf17cfSDenis Cheng void __init mnt_init(void) 32431da177e4SLinus Torvalds { 324413f14b4dSEric Dumazet unsigned u; 324515a67dd8SRandy Dunlap int err; 32461da177e4SLinus Torvalds 32477d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 324820c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 32491da177e4SLinus Torvalds 32500818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 325138129a13SAl Viro sizeof(struct hlist_head), 32520818bf27SAl Viro mhash_entries, 19, 32530818bf27SAl Viro 0, 32540818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 32550818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 32560818bf27SAl Viro sizeof(struct hlist_head), 32570818bf27SAl Viro mphash_entries, 19, 32580818bf27SAl Viro 0, 32590818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 32601da177e4SLinus Torvalds 326184d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 32621da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 32631da177e4SLinus Torvalds 32640818bf27SAl Viro for (u = 0; u <= m_hash_mask; u++) 326538129a13SAl Viro INIT_HLIST_HEAD(&mount_hashtable[u]); 32660818bf27SAl Viro for (u = 0; u <= mp_hash_mask; u++) 32670818bf27SAl Viro INIT_HLIST_HEAD(&mountpoint_hashtable[u]); 32681da177e4SLinus Torvalds 32694b93dc9bSTejun Heo kernfs_init(); 32704b93dc9bSTejun Heo 327115a67dd8SRandy Dunlap err = sysfs_init(); 327215a67dd8SRandy Dunlap if (err) 327315a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 32748e24eea7SHarvey Harrison __func__, err); 327500d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 327600d26666SGreg Kroah-Hartman if (!fs_kobj) 32778e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 32781da177e4SLinus Torvalds init_rootfs(); 32791da177e4SLinus Torvalds init_mount_tree(); 32801da177e4SLinus Torvalds } 32811da177e4SLinus Torvalds 3282616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 32831da177e4SLinus Torvalds { 3284d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 3285616511d0STrond Myklebust return; 32867b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3287771b1371SEric W. Biederman free_mnt_ns(ns); 32881da177e4SLinus Torvalds } 32899d412a43SAl Viro 32909d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 32919d412a43SAl Viro { 3292423e0ab0STim Chen struct vfsmount *mnt; 3293423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 3294423e0ab0STim Chen if (!IS_ERR(mnt)) { 3295423e0ab0STim Chen /* 3296423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3297423e0ab0STim Chen * we unmount before file sys is unregistered 3298423e0ab0STim Chen */ 3299f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3300423e0ab0STim Chen } 3301423e0ab0STim Chen return mnt; 33029d412a43SAl Viro } 33039d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 3304423e0ab0STim Chen 3305423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3306423e0ab0STim Chen { 3307423e0ab0STim Chen /* release long term mount so mount point can be released */ 3308423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3309f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 331048a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3311423e0ab0STim Chen mntput(mnt); 3312423e0ab0STim Chen } 3313423e0ab0STim Chen } 3314423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 331502125a82SAl Viro 331602125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 331702125a82SAl Viro { 3318143c8c91SAl Viro return check_mnt(real_mount(mnt)); 331902125a82SAl Viro } 33208823c079SEric W. Biederman 33213151527eSEric W. Biederman bool current_chrooted(void) 33223151527eSEric W. Biederman { 33233151527eSEric W. Biederman /* Does the current process have a non-standard root */ 33243151527eSEric W. Biederman struct path ns_root; 33253151527eSEric W. Biederman struct path fs_root; 33263151527eSEric W. Biederman bool chrooted; 33273151527eSEric W. Biederman 33283151527eSEric W. Biederman /* Find the namespace root */ 33293151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 33303151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 33313151527eSEric W. Biederman path_get(&ns_root); 33323151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 33333151527eSEric W. Biederman ; 33343151527eSEric W. Biederman 33353151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 33363151527eSEric W. Biederman 33373151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 33383151527eSEric W. Biederman 33393151527eSEric W. Biederman path_put(&fs_root); 33403151527eSEric W. Biederman path_put(&ns_root); 33413151527eSEric W. Biederman 33423151527eSEric W. Biederman return chrooted; 33433151527eSEric W. Biederman } 33443151527eSEric W. Biederman 33458654df4eSEric W. Biederman static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new, 33468654df4eSEric W. Biederman int *new_mnt_flags) 334787a8ebd6SEric W. Biederman { 33488c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 334987a8ebd6SEric W. Biederman struct mount *mnt; 3350e51db735SEric W. Biederman bool visible = false; 335187a8ebd6SEric W. Biederman 335244bb4385SAl Viro down_read(&namespace_sem); 335387a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3354e51db735SEric W. Biederman struct mount *child; 335577b1a97dSEric W. Biederman int mnt_flags; 335677b1a97dSEric W. Biederman 33578654df4eSEric W. Biederman if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type) 3358e51db735SEric W. Biederman continue; 3359e51db735SEric W. Biederman 33607e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 33617e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 33627e96c1b0SEric W. Biederman */ 33637e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 33647e96c1b0SEric W. Biederman continue; 33657e96c1b0SEric W. Biederman 3366a1935c17SEric W. Biederman /* A local view of the mount flags */ 336777b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 336877b1a97dSEric W. Biederman 3369695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 3370695e9df0SEric W. Biederman if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY) 3371695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 3372695e9df0SEric W. Biederman 33738c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 33748c6cf9ccSEric W. Biederman * than the proposed new mount. 33758c6cf9ccSEric W. Biederman */ 337677b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 33778c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 33788c6cf9ccSEric W. Biederman continue; 337977b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 338077b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 33818c6cf9ccSEric W. Biederman continue; 33828c6cf9ccSEric W. Biederman 3383ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 3384ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 3385ceeb0e5dSEric W. Biederman * empty directories. 3386e51db735SEric W. Biederman */ 3387e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3388e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3389ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 3390d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 3391ceeb0e5dSEric W. Biederman continue; 33927236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 33937236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 3394e51db735SEric W. Biederman goto next; 339587a8ebd6SEric W. Biederman } 33968c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 339777b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 33988c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 3399e51db735SEric W. Biederman visible = true; 3400e51db735SEric W. Biederman goto found; 3401e51db735SEric W. Biederman next: ; 340287a8ebd6SEric W. Biederman } 3403e51db735SEric W. Biederman found: 340444bb4385SAl Viro up_read(&namespace_sem); 3405e51db735SEric W. Biederman return visible; 340687a8ebd6SEric W. Biederman } 340787a8ebd6SEric W. Biederman 34088654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags) 34098654df4eSEric W. Biederman { 3410a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 34118654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 34128654df4eSEric W. Biederman unsigned long s_iflags; 34138654df4eSEric W. Biederman 34148654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 34158654df4eSEric W. Biederman return false; 34168654df4eSEric W. Biederman 34178654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 34188654df4eSEric W. Biederman s_iflags = mnt->mnt_sb->s_iflags; 34198654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 34208654df4eSEric W. Biederman return false; 34218654df4eSEric W. Biederman 3422a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 3423a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 3424a1935c17SEric W. Biederman required_iflags); 3425a1935c17SEric W. Biederman return true; 3426a1935c17SEric W. Biederman } 3427a1935c17SEric W. Biederman 34288654df4eSEric W. Biederman return !mnt_already_visible(ns, mnt, new_mnt_flags); 34298654df4eSEric W. Biederman } 34308654df4eSEric W. Biederman 3431380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 3432380cf5baSAndy Lutomirski { 3433380cf5baSAndy Lutomirski /* 3434380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 3435380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 3436380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 3437380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 3438380cf5baSAndy Lutomirski * in other namespaces. 3439380cf5baSAndy Lutomirski */ 3440380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 3441380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 3442380cf5baSAndy Lutomirski } 3443380cf5baSAndy Lutomirski 344464964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 34458823c079SEric W. Biederman { 344658be2825SAl Viro struct ns_common *ns = NULL; 34478823c079SEric W. Biederman struct nsproxy *nsproxy; 34488823c079SEric W. Biederman 3449728dba3aSEric W. Biederman task_lock(task); 3450728dba3aSEric W. Biederman nsproxy = task->nsproxy; 34518823c079SEric W. Biederman if (nsproxy) { 345258be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 345358be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 34548823c079SEric W. Biederman } 3455728dba3aSEric W. Biederman task_unlock(task); 34568823c079SEric W. Biederman 34578823c079SEric W. Biederman return ns; 34588823c079SEric W. Biederman } 34598823c079SEric W. Biederman 346064964528SAl Viro static void mntns_put(struct ns_common *ns) 34618823c079SEric W. Biederman { 346258be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 34638823c079SEric W. Biederman } 34648823c079SEric W. Biederman 346564964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns) 34668823c079SEric W. Biederman { 34678823c079SEric W. Biederman struct fs_struct *fs = current->fs; 346858be2825SAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns); 34698823c079SEric W. Biederman struct path root; 34708823c079SEric W. Biederman 34710c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3472c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3473c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3474ae11e0f1SZhao Hongjiang return -EPERM; 34758823c079SEric W. Biederman 34768823c079SEric W. Biederman if (fs->users != 1) 34778823c079SEric W. Biederman return -EINVAL; 34788823c079SEric W. Biederman 34798823c079SEric W. Biederman get_mnt_ns(mnt_ns); 34808823c079SEric W. Biederman put_mnt_ns(nsproxy->mnt_ns); 34818823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 34828823c079SEric W. Biederman 34838823c079SEric W. Biederman /* Find the root */ 34848823c079SEric W. Biederman root.mnt = &mnt_ns->root->mnt; 34858823c079SEric W. Biederman root.dentry = mnt_ns->root->mnt.mnt_root; 34868823c079SEric W. Biederman path_get(&root); 34878823c079SEric W. Biederman while(d_mountpoint(root.dentry) && follow_down_one(&root)) 34888823c079SEric W. Biederman ; 34898823c079SEric W. Biederman 34908823c079SEric W. Biederman /* Update the pwd and root */ 34918823c079SEric W. Biederman set_fs_pwd(fs, &root); 34928823c079SEric W. Biederman set_fs_root(fs, &root); 34938823c079SEric W. Biederman 34948823c079SEric W. Biederman path_put(&root); 34958823c079SEric W. Biederman return 0; 34968823c079SEric W. Biederman } 34978823c079SEric W. Biederman 3498bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 3499bcac25a5SAndrey Vagin { 3500bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 3501bcac25a5SAndrey Vagin } 3502bcac25a5SAndrey Vagin 35038823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 35048823c079SEric W. Biederman .name = "mnt", 35058823c079SEric W. Biederman .type = CLONE_NEWNS, 35068823c079SEric W. Biederman .get = mntns_get, 35078823c079SEric W. Biederman .put = mntns_put, 35088823c079SEric W. Biederman .install = mntns_install, 3509bcac25a5SAndrey Vagin .owner = mntns_owner, 35108823c079SEric W. Biederman }; 3511