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> 1873cd49ecSMiklos Szeredi #include <linux/idr.h> 1957f150a5SRob Landley #include <linux/init.h> /* init_rootfs */ 20d10577a8SAl Viro #include <linux/fs_struct.h> /* get_fs_root et.al. */ 21d10577a8SAl Viro #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */ 22d10577a8SAl Viro #include <linux/uaccess.h> 230bb80f24SDavid Howells #include <linux/proc_ns.h> 2420b4fb48SLinus Torvalds #include <linux/magic.h> 250818bf27SAl Viro #include <linux/bootmem.h> 269ea459e1SAl Viro #include <linux/task_work.h> 2707b20889SRam Pai #include "pnode.h" 28948730b0SAdrian Bunk #include "internal.h" 291da177e4SLinus Torvalds 300818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 310818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 320818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 330818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 340818bf27SAl Viro 350818bf27SAl Viro static __initdata unsigned long mhash_entries; 360818bf27SAl Viro static int __init set_mhash_entries(char *str) 370818bf27SAl Viro { 380818bf27SAl Viro if (!str) 390818bf27SAl Viro return 0; 400818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 410818bf27SAl Viro return 1; 420818bf27SAl Viro } 430818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 440818bf27SAl Viro 450818bf27SAl Viro static __initdata unsigned long mphash_entries; 460818bf27SAl Viro static int __init set_mphash_entries(char *str) 470818bf27SAl Viro { 480818bf27SAl Viro if (!str) 490818bf27SAl Viro return 0; 500818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 510818bf27SAl Viro return 1; 520818bf27SAl Viro } 530818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 5413f14b4dSEric Dumazet 55c7999c36SAl Viro static u64 event; 5673cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 57719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 5899b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock); 59f21f6220SAl Viro static int mnt_id_start = 0; 60f21f6220SAl Viro static int mnt_group_start = 1; 615addc5ddSAl Viro 6238129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 630818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 64e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 6559aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 661da177e4SLinus Torvalds 67f87fd4c2SMiklos Szeredi /* /sys/fs */ 6800d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 6900d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 70f87fd4c2SMiklos Szeredi 7199b7db7bSNick Piggin /* 7299b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 7399b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 7499b7db7bSNick Piggin * up the tree. 7599b7db7bSNick Piggin * 7699b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 7799b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 7899b7db7bSNick Piggin */ 7948a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 8099b7db7bSNick Piggin 8138129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 821da177e4SLinus Torvalds { 831da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 841da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 850818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 860818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 870818bf27SAl Viro } 880818bf27SAl Viro 890818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 900818bf27SAl Viro { 910818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 920818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 930818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 9699b7db7bSNick Piggin /* 9799b7db7bSNick Piggin * allocation is serialized by namespace_sem, but we need the spinlock to 9899b7db7bSNick Piggin * serialize with freeing. 9999b7db7bSNick Piggin */ 100b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 10173cd49ecSMiklos Szeredi { 10273cd49ecSMiklos Szeredi int res; 10373cd49ecSMiklos Szeredi 10473cd49ecSMiklos Szeredi retry: 10573cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 10699b7db7bSNick Piggin spin_lock(&mnt_id_lock); 10715169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 108f21f6220SAl Viro if (!res) 10915169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 11099b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 11173cd49ecSMiklos Szeredi if (res == -EAGAIN) 11273cd49ecSMiklos Szeredi goto retry; 11373cd49ecSMiklos Szeredi 11473cd49ecSMiklos Szeredi return res; 11573cd49ecSMiklos Szeredi } 11673cd49ecSMiklos Szeredi 117b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 11873cd49ecSMiklos Szeredi { 11915169fe7SAl Viro int id = mnt->mnt_id; 12099b7db7bSNick Piggin spin_lock(&mnt_id_lock); 121f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 122f21f6220SAl Viro if (mnt_id_start > id) 123f21f6220SAl Viro mnt_id_start = id; 12499b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 12573cd49ecSMiklos Szeredi } 12673cd49ecSMiklos Szeredi 127719f5d7fSMiklos Szeredi /* 128719f5d7fSMiklos Szeredi * Allocate a new peer group ID 129719f5d7fSMiklos Szeredi * 130719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 131719f5d7fSMiklos Szeredi */ 1324b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 133719f5d7fSMiklos Szeredi { 134f21f6220SAl Viro int res; 135f21f6220SAl Viro 136719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 137719f5d7fSMiklos Szeredi return -ENOMEM; 138719f5d7fSMiklos Szeredi 139f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 140f21f6220SAl Viro mnt_group_start, 14115169fe7SAl Viro &mnt->mnt_group_id); 142f21f6220SAl Viro if (!res) 14315169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 144f21f6220SAl Viro 145f21f6220SAl Viro return res; 146719f5d7fSMiklos Szeredi } 147719f5d7fSMiklos Szeredi 148719f5d7fSMiklos Szeredi /* 149719f5d7fSMiklos Szeredi * Release a peer group ID 150719f5d7fSMiklos Szeredi */ 1514b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 152719f5d7fSMiklos Szeredi { 15315169fe7SAl Viro int id = mnt->mnt_group_id; 154f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 155f21f6220SAl Viro if (mnt_group_start > id) 156f21f6220SAl Viro mnt_group_start = id; 15715169fe7SAl Viro mnt->mnt_group_id = 0; 158719f5d7fSMiklos Szeredi } 159719f5d7fSMiklos Szeredi 160b3e19d92SNick Piggin /* 161b3e19d92SNick Piggin * vfsmount lock must be held for read 162b3e19d92SNick Piggin */ 16383adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 164b3e19d92SNick Piggin { 165b3e19d92SNick Piggin #ifdef CONFIG_SMP 16668e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 167b3e19d92SNick Piggin #else 168b3e19d92SNick Piggin preempt_disable(); 16968e8a9feSAl Viro mnt->mnt_count += n; 170b3e19d92SNick Piggin preempt_enable(); 171b3e19d92SNick Piggin #endif 172b3e19d92SNick Piggin } 173b3e19d92SNick Piggin 174b3e19d92SNick Piggin /* 175b3e19d92SNick Piggin * vfsmount lock must be held for write 176b3e19d92SNick Piggin */ 17783adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 178b3e19d92SNick Piggin { 179b3e19d92SNick Piggin #ifdef CONFIG_SMP 180f03c6599SAl Viro unsigned int count = 0; 181b3e19d92SNick Piggin int cpu; 182b3e19d92SNick Piggin 183b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 18468e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 185b3e19d92SNick Piggin } 186b3e19d92SNick Piggin 187b3e19d92SNick Piggin return count; 188b3e19d92SNick Piggin #else 18968e8a9feSAl Viro return mnt->mnt_count; 190b3e19d92SNick Piggin #endif 191b3e19d92SNick Piggin } 192b3e19d92SNick Piggin 193b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1941da177e4SLinus Torvalds { 195c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 196c63181e6SAl Viro if (mnt) { 19773cd49ecSMiklos Szeredi int err; 19873cd49ecSMiklos Szeredi 199c63181e6SAl Viro err = mnt_alloc_id(mnt); 20088b38782SLi Zefan if (err) 20188b38782SLi Zefan goto out_free_cache; 20288b38782SLi Zefan 20388b38782SLi Zefan if (name) { 204c63181e6SAl Viro mnt->mnt_devname = kstrdup(name, GFP_KERNEL); 205c63181e6SAl Viro if (!mnt->mnt_devname) 20688b38782SLi Zefan goto out_free_id; 20773cd49ecSMiklos Szeredi } 20873cd49ecSMiklos Szeredi 209b3e19d92SNick Piggin #ifdef CONFIG_SMP 210c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 211c63181e6SAl Viro if (!mnt->mnt_pcp) 212b3e19d92SNick Piggin goto out_free_devname; 213b3e19d92SNick Piggin 214c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 215b3e19d92SNick Piggin #else 216c63181e6SAl Viro mnt->mnt_count = 1; 217c63181e6SAl Viro mnt->mnt_writers = 0; 218b3e19d92SNick Piggin #endif 219b3e19d92SNick Piggin 22038129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 221c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 222c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 223c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 224c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 225c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 226c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 227c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2280a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 2292504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 2302504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 2312504c5d6SAndreas Gruenbacher #endif 2321da177e4SLinus Torvalds } 233c63181e6SAl Viro return mnt; 23488b38782SLi Zefan 235d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 236d3ef3d73Snpiggin@suse.de out_free_devname: 237c63181e6SAl Viro kfree(mnt->mnt_devname); 238d3ef3d73Snpiggin@suse.de #endif 23988b38782SLi Zefan out_free_id: 240c63181e6SAl Viro mnt_free_id(mnt); 24188b38782SLi Zefan out_free_cache: 242c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 24388b38782SLi Zefan return NULL; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2468366025eSDave Hansen /* 2478366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2488366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2498366025eSDave Hansen * We must keep track of when those operations start 2508366025eSDave Hansen * (for permission checks) and when they end, so that 2518366025eSDave Hansen * we can determine when writes are able to occur to 2528366025eSDave Hansen * a filesystem. 2538366025eSDave Hansen */ 2543d733633SDave Hansen /* 2553d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2563d733633SDave Hansen * @mnt: the mount to check for its write status 2573d733633SDave Hansen * 2583d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2593d733633SDave Hansen * It does not guarantee that the filesystem will stay 2603d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2613d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2623d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2633d733633SDave Hansen * r/w. 2643d733633SDave Hansen */ 2653d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2663d733633SDave Hansen { 2672e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2682e4b7fcdSDave Hansen return 1; 2692e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2702e4b7fcdSDave Hansen return 1; 2712e4b7fcdSDave Hansen return 0; 2723d733633SDave Hansen } 2733d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2743d733633SDave Hansen 27583adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2763d733633SDave Hansen { 277d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 27868e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 279d3ef3d73Snpiggin@suse.de #else 28068e8a9feSAl Viro mnt->mnt_writers++; 281d3ef3d73Snpiggin@suse.de #endif 2823d733633SDave Hansen } 2833d733633SDave Hansen 28483adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2853d733633SDave Hansen { 286d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 28768e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 288d3ef3d73Snpiggin@suse.de #else 28968e8a9feSAl Viro mnt->mnt_writers--; 290d3ef3d73Snpiggin@suse.de #endif 291d3ef3d73Snpiggin@suse.de } 292d3ef3d73Snpiggin@suse.de 29383adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 294d3ef3d73Snpiggin@suse.de { 295d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 296d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2973d733633SDave Hansen int cpu; 2983d733633SDave Hansen 2993d733633SDave Hansen for_each_possible_cpu(cpu) { 30068e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 3013d733633SDave Hansen } 3023d733633SDave Hansen 303d3ef3d73Snpiggin@suse.de return count; 304d3ef3d73Snpiggin@suse.de #else 305d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 306d3ef3d73Snpiggin@suse.de #endif 3073d733633SDave Hansen } 3083d733633SDave Hansen 3094ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 3104ed5e82fSMiklos Szeredi { 3114ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 3124ed5e82fSMiklos Szeredi return 1; 3134ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 3144ed5e82fSMiklos Szeredi smp_rmb(); 3154ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 3164ed5e82fSMiklos Szeredi } 3174ed5e82fSMiklos Szeredi 3183d733633SDave Hansen /* 319eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 320eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 321eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 322eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3233d733633SDave Hansen */ 3248366025eSDave Hansen /** 325eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 32683adc753SAl Viro * @m: the mount on which to take a write 3278366025eSDave Hansen * 328eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 329eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 330eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 331eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 332eb04c282SJan Kara * called. This is effectively a refcount. 3338366025eSDave Hansen */ 334eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3358366025eSDave Hansen { 33683adc753SAl Viro struct mount *mnt = real_mount(m); 3373d733633SDave Hansen int ret = 0; 3383d733633SDave Hansen 339d3ef3d73Snpiggin@suse.de preempt_disable(); 340c6653a83SNick Piggin mnt_inc_writers(mnt); 341d3ef3d73Snpiggin@suse.de /* 342c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 343d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 344d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 345d3ef3d73Snpiggin@suse.de */ 346d3ef3d73Snpiggin@suse.de smp_mb(); 3471e75529eSMiao Xie while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 348d3ef3d73Snpiggin@suse.de cpu_relax(); 349d3ef3d73Snpiggin@suse.de /* 350d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 351d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 352d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 353d3ef3d73Snpiggin@suse.de */ 354d3ef3d73Snpiggin@suse.de smp_rmb(); 3554ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 356c6653a83SNick Piggin mnt_dec_writers(mnt); 3573d733633SDave Hansen ret = -EROFS; 3583d733633SDave Hansen } 359d3ef3d73Snpiggin@suse.de preempt_enable(); 360eb04c282SJan Kara 361eb04c282SJan Kara return ret; 362eb04c282SJan Kara } 363eb04c282SJan Kara 364eb04c282SJan Kara /** 365eb04c282SJan Kara * mnt_want_write - get write access to a mount 366eb04c282SJan Kara * @m: the mount on which to take a write 367eb04c282SJan Kara * 368eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 369eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 370eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 371eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 372eb04c282SJan Kara */ 373eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 374eb04c282SJan Kara { 375eb04c282SJan Kara int ret; 376eb04c282SJan Kara 377eb04c282SJan Kara sb_start_write(m->mnt_sb); 378eb04c282SJan Kara ret = __mnt_want_write(m); 379eb04c282SJan Kara if (ret) 380eb04c282SJan Kara sb_end_write(m->mnt_sb); 3813d733633SDave Hansen return ret; 3828366025eSDave Hansen } 3838366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3848366025eSDave Hansen 3858366025eSDave Hansen /** 38696029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 38796029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 38896029c4eSnpiggin@suse.de * 38996029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 39096029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 39196029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 39296029c4eSnpiggin@suse.de * on it. This allows some optimisation. 39396029c4eSnpiggin@suse.de * 39496029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 39596029c4eSnpiggin@suse.de * drop the reference. 39696029c4eSnpiggin@suse.de */ 39796029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 39896029c4eSnpiggin@suse.de { 39996029c4eSnpiggin@suse.de /* superblock may be r/o */ 40096029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 40196029c4eSnpiggin@suse.de return -EROFS; 40296029c4eSnpiggin@suse.de preempt_disable(); 40383adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 40496029c4eSnpiggin@suse.de preempt_enable(); 40596029c4eSnpiggin@suse.de return 0; 40696029c4eSnpiggin@suse.de } 40796029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 40896029c4eSnpiggin@suse.de 40996029c4eSnpiggin@suse.de /** 410eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 411eb04c282SJan Kara * @file: the file who's mount on which to take a write 412eb04c282SJan Kara * 413eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 414eb04c282SJan Kara * do some optimisations if the file is open for write already 415eb04c282SJan Kara */ 416eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 417eb04c282SJan Kara { 41883f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 419eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 420eb04c282SJan Kara else 421eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 422eb04c282SJan Kara } 423eb04c282SJan Kara 424eb04c282SJan Kara /** 42596029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 42696029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 42796029c4eSnpiggin@suse.de * 42896029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 42996029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 43096029c4eSnpiggin@suse.de */ 43196029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 43296029c4eSnpiggin@suse.de { 433eb04c282SJan Kara int ret; 434eb04c282SJan Kara 435eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 436eb04c282SJan Kara ret = __mnt_want_write_file(file); 437eb04c282SJan Kara if (ret) 438eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 439eb04c282SJan Kara return ret; 44096029c4eSnpiggin@suse.de } 44196029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 44296029c4eSnpiggin@suse.de 44396029c4eSnpiggin@suse.de /** 444eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4458366025eSDave Hansen * @mnt: the mount on which to give up write access 4468366025eSDave Hansen * 4478366025eSDave Hansen * Tells the low-level filesystem that we are done 4488366025eSDave Hansen * performing writes to it. Must be matched with 449eb04c282SJan Kara * __mnt_want_write() call above. 4508366025eSDave Hansen */ 451eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4528366025eSDave Hansen { 453d3ef3d73Snpiggin@suse.de preempt_disable(); 45483adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 455d3ef3d73Snpiggin@suse.de preempt_enable(); 4568366025eSDave Hansen } 457eb04c282SJan Kara 458eb04c282SJan Kara /** 459eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 460eb04c282SJan Kara * @mnt: the mount on which to give up write access 461eb04c282SJan Kara * 462eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 463eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 464eb04c282SJan Kara * mnt_want_write() call above. 465eb04c282SJan Kara */ 466eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 467eb04c282SJan Kara { 468eb04c282SJan Kara __mnt_drop_write(mnt); 469eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 470eb04c282SJan Kara } 4718366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4728366025eSDave Hansen 473eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 474eb04c282SJan Kara { 475eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 476eb04c282SJan Kara } 477eb04c282SJan Kara 4782a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4792a79f17eSAl Viro { 4802a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4812a79f17eSAl Viro } 4822a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4832a79f17eSAl Viro 48483adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4858366025eSDave Hansen { 4863d733633SDave Hansen int ret = 0; 4873d733633SDave Hansen 488719ea2fbSAl Viro lock_mount_hash(); 48983adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 490d3ef3d73Snpiggin@suse.de /* 491d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 492d3ef3d73Snpiggin@suse.de * should be visible before we do. 493d3ef3d73Snpiggin@suse.de */ 494d3ef3d73Snpiggin@suse.de smp_mb(); 495d3ef3d73Snpiggin@suse.de 496d3ef3d73Snpiggin@suse.de /* 497d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 498d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 499d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 500d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 501d3ef3d73Snpiggin@suse.de * 502d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 503d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 504d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 505d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 506d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 507d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 508d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 509d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 510d3ef3d73Snpiggin@suse.de * we're counting up here. 511d3ef3d73Snpiggin@suse.de */ 512c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 513d3ef3d73Snpiggin@suse.de ret = -EBUSY; 514d3ef3d73Snpiggin@suse.de else 51583adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 516d3ef3d73Snpiggin@suse.de /* 517d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 518d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 519d3ef3d73Snpiggin@suse.de */ 520d3ef3d73Snpiggin@suse.de smp_wmb(); 52183adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 522719ea2fbSAl Viro unlock_mount_hash(); 5233d733633SDave Hansen return ret; 5243d733633SDave Hansen } 5258366025eSDave Hansen 52683adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 5272e4b7fcdSDave Hansen { 528719ea2fbSAl Viro lock_mount_hash(); 52983adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 530719ea2fbSAl Viro unlock_mount_hash(); 5312e4b7fcdSDave Hansen } 5322e4b7fcdSDave Hansen 5334ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5344ed5e82fSMiklos Szeredi { 5354ed5e82fSMiklos Szeredi struct mount *mnt; 5364ed5e82fSMiklos Szeredi int err = 0; 5374ed5e82fSMiklos Szeredi 5388e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5398e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5408e8b8796SMiklos Szeredi return -EBUSY; 5418e8b8796SMiklos Szeredi 542719ea2fbSAl Viro lock_mount_hash(); 5434ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5444ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5454ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5464ed5e82fSMiklos Szeredi smp_mb(); 5474ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5484ed5e82fSMiklos Szeredi err = -EBUSY; 5494ed5e82fSMiklos Szeredi break; 5504ed5e82fSMiklos Szeredi } 5514ed5e82fSMiklos Szeredi } 5524ed5e82fSMiklos Szeredi } 5538e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5548e8b8796SMiklos Szeredi err = -EBUSY; 5558e8b8796SMiklos Szeredi 5564ed5e82fSMiklos Szeredi if (!err) { 5574ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5584ed5e82fSMiklos Szeredi smp_wmb(); 5594ed5e82fSMiklos Szeredi } 5604ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5614ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5624ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5634ed5e82fSMiklos Szeredi } 564719ea2fbSAl Viro unlock_mount_hash(); 5654ed5e82fSMiklos Szeredi 5664ed5e82fSMiklos Szeredi return err; 5674ed5e82fSMiklos Szeredi } 5684ed5e82fSMiklos Szeredi 569b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5701da177e4SLinus Torvalds { 57152ba1621SAl Viro kfree(mnt->mnt_devname); 572d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 57368e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 574d3ef3d73Snpiggin@suse.de #endif 575b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5788ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5798ffcb32eSDavid Howells { 5808ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5818ffcb32eSDavid Howells } 5828ffcb32eSDavid Howells 58348a066e7SAl Viro /* call under rcu_read_lock */ 58448a066e7SAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 58548a066e7SAl Viro { 58648a066e7SAl Viro struct mount *mnt; 58748a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 58848a066e7SAl Viro return false; 58948a066e7SAl Viro if (bastard == NULL) 59048a066e7SAl Viro return true; 59148a066e7SAl Viro mnt = real_mount(bastard); 59248a066e7SAl Viro mnt_add_count(mnt, 1); 59348a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 59448a066e7SAl Viro return true; 59548a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 59648a066e7SAl Viro mnt_add_count(mnt, -1); 59748a066e7SAl Viro return false; 59848a066e7SAl Viro } 59948a066e7SAl Viro rcu_read_unlock(); 60048a066e7SAl Viro mntput(bastard); 60148a066e7SAl Viro rcu_read_lock(); 60248a066e7SAl Viro return false; 60348a066e7SAl Viro } 60448a066e7SAl Viro 6051da177e4SLinus Torvalds /* 606474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 60748a066e7SAl Viro * call under rcu_read_lock() 6081da177e4SLinus Torvalds */ 609474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6101da177e4SLinus Torvalds { 61138129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 612474279dcSAl Viro struct mount *p; 6131da177e4SLinus Torvalds 61438129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 615474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 616474279dcSAl Viro return p; 617474279dcSAl Viro return NULL; 6181da177e4SLinus Torvalds } 619474279dcSAl Viro 620474279dcSAl Viro /* 621474279dcSAl Viro * find the last mount at @dentry on vfsmount @mnt. 62248a066e7SAl Viro * mount_lock must be held. 623474279dcSAl Viro */ 624474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry) 625474279dcSAl Viro { 62638129a13SAl Viro struct mount *p, *res; 62738129a13SAl Viro res = p = __lookup_mnt(mnt, dentry); 62838129a13SAl Viro if (!p) 62938129a13SAl Viro goto out; 63038129a13SAl Viro hlist_for_each_entry_continue(p, mnt_hash) { 6311d6a32acSAl Viro if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry) 6321d6a32acSAl Viro break; 6331d6a32acSAl Viro res = p; 6341d6a32acSAl Viro } 63538129a13SAl Viro out: 6361d6a32acSAl Viro return res; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 639a05964f3SRam Pai /* 640f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 641f015f126SDavid Howells * 642f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 643f015f126SDavid Howells * following mounts: 644f015f126SDavid Howells * 645f015f126SDavid Howells * mount /dev/sda1 /mnt 646f015f126SDavid Howells * mount /dev/sda2 /mnt 647f015f126SDavid Howells * mount /dev/sda3 /mnt 648f015f126SDavid Howells * 649f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 650f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 651f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 652f015f126SDavid Howells * 653f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 654a05964f3SRam Pai */ 6551c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 656a05964f3SRam Pai { 657c7105365SAl Viro struct mount *child_mnt; 65848a066e7SAl Viro struct vfsmount *m; 65948a066e7SAl Viro unsigned seq; 66099b7db7bSNick Piggin 66148a066e7SAl Viro rcu_read_lock(); 66248a066e7SAl Viro do { 66348a066e7SAl Viro seq = read_seqbegin(&mount_lock); 664474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 66548a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 66648a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 66748a066e7SAl Viro rcu_read_unlock(); 66848a066e7SAl Viro return m; 669a05964f3SRam Pai } 670a05964f3SRam Pai 6717af1364fSEric W. Biederman /* 6727af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 6737af1364fSEric W. Biederman * current mount namespace. 6747af1364fSEric W. Biederman * 6757af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 6767af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 6777af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 6787af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 6797af1364fSEric W. Biederman * is a mountpoint. 6807af1364fSEric W. Biederman * 6817af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 6827af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 6837af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 6847af1364fSEric W. Biederman * parent mount. 6857af1364fSEric W. Biederman */ 6867af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 6877af1364fSEric W. Biederman { 6887af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 6897af1364fSEric W. Biederman struct mount *mnt; 6907af1364fSEric W. Biederman bool is_covered = false; 6917af1364fSEric W. Biederman 6927af1364fSEric W. Biederman if (!d_mountpoint(dentry)) 6937af1364fSEric W. Biederman goto out; 6947af1364fSEric W. Biederman 6957af1364fSEric W. Biederman down_read(&namespace_sem); 6967af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 6977af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 6987af1364fSEric W. Biederman if (is_covered) 6997af1364fSEric W. Biederman break; 7007af1364fSEric W. Biederman } 7017af1364fSEric W. Biederman up_read(&namespace_sem); 7027af1364fSEric W. Biederman out: 7037af1364fSEric W. Biederman return is_covered; 7047af1364fSEric W. Biederman } 7057af1364fSEric W. Biederman 706e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 70784d17192SAl Viro { 7080818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 70984d17192SAl Viro struct mountpoint *mp; 71084d17192SAl Viro 7110818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 71284d17192SAl Viro if (mp->m_dentry == dentry) { 71384d17192SAl Viro /* might be worth a WARN_ON() */ 71484d17192SAl Viro if (d_unlinked(dentry)) 71584d17192SAl Viro return ERR_PTR(-ENOENT); 71684d17192SAl Viro mp->m_count++; 71784d17192SAl Viro return mp; 71884d17192SAl Viro } 71984d17192SAl Viro } 720e2dfa935SEric W. Biederman return NULL; 721e2dfa935SEric W. Biederman } 722e2dfa935SEric W. Biederman 723e2dfa935SEric W. Biederman static struct mountpoint *new_mountpoint(struct dentry *dentry) 724e2dfa935SEric W. Biederman { 725e2dfa935SEric W. Biederman struct hlist_head *chain = mp_hash(dentry); 726e2dfa935SEric W. Biederman struct mountpoint *mp; 727e2dfa935SEric W. Biederman int ret; 72884d17192SAl Viro 72984d17192SAl Viro mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 73084d17192SAl Viro if (!mp) 73184d17192SAl Viro return ERR_PTR(-ENOMEM); 73284d17192SAl Viro 733eed81007SMiklos Szeredi ret = d_set_mounted(dentry); 734eed81007SMiklos Szeredi if (ret) { 73584d17192SAl Viro kfree(mp); 736eed81007SMiklos Szeredi return ERR_PTR(ret); 73784d17192SAl Viro } 738eed81007SMiklos Szeredi 73984d17192SAl Viro mp->m_dentry = dentry; 74084d17192SAl Viro mp->m_count = 1; 7410818bf27SAl Viro hlist_add_head(&mp->m_hash, chain); 7420a5eb7c8SEric W. Biederman INIT_HLIST_HEAD(&mp->m_list); 74384d17192SAl Viro return mp; 74484d17192SAl Viro } 74584d17192SAl Viro 74684d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 74784d17192SAl Viro { 74884d17192SAl Viro if (!--mp->m_count) { 74984d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7500a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 75184d17192SAl Viro spin_lock(&dentry->d_lock); 75284d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 75384d17192SAl Viro spin_unlock(&dentry->d_lock); 7540818bf27SAl Viro hlist_del(&mp->m_hash); 75584d17192SAl Viro kfree(mp); 75684d17192SAl Viro } 75784d17192SAl Viro } 75884d17192SAl Viro 759143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7601da177e4SLinus Torvalds { 7616b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 76499b7db7bSNick Piggin /* 76599b7db7bSNick Piggin * vfsmount lock must be held for write 76699b7db7bSNick Piggin */ 7676b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7685addc5ddSAl Viro { 7695addc5ddSAl Viro if (ns) { 7705addc5ddSAl Viro ns->event = ++event; 7715addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7725addc5ddSAl Viro } 7735addc5ddSAl Viro } 7745addc5ddSAl Viro 77599b7db7bSNick Piggin /* 77699b7db7bSNick Piggin * vfsmount lock must be held for write 77799b7db7bSNick Piggin */ 7786b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7795addc5ddSAl Viro { 7805addc5ddSAl Viro if (ns && ns->event != event) { 7815addc5ddSAl Viro ns->event = event; 7825addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7835addc5ddSAl Viro } 7845addc5ddSAl Viro } 7855addc5ddSAl Viro 78699b7db7bSNick Piggin /* 78799b7db7bSNick Piggin * vfsmount lock must be held for write 78899b7db7bSNick Piggin */ 789419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 7901da177e4SLinus Torvalds { 791a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 7920714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 7930714a533SAl Viro mnt->mnt_parent = mnt; 794a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7956b41d536SAl Viro list_del_init(&mnt->mnt_child); 79638129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 7970a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 79884d17192SAl Viro put_mountpoint(mnt->mnt_mp); 79984d17192SAl Viro mnt->mnt_mp = NULL; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 80299b7db7bSNick Piggin /* 80399b7db7bSNick Piggin * vfsmount lock must be held for write 80499b7db7bSNick Piggin */ 80584d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 80684d17192SAl Viro struct mountpoint *mp, 80744d964d6SAl Viro struct mount *child_mnt) 808b90fa9aeSRam Pai { 80984d17192SAl Viro mp->m_count++; 8103a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 81184d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 8123a2393d7SAl Viro child_mnt->mnt_parent = mnt; 81384d17192SAl Viro child_mnt->mnt_mp = mp; 8140a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 815b90fa9aeSRam Pai } 816b90fa9aeSRam Pai 81799b7db7bSNick Piggin /* 81899b7db7bSNick Piggin * vfsmount lock must be held for write 81999b7db7bSNick Piggin */ 82084d17192SAl Viro static void attach_mnt(struct mount *mnt, 82184d17192SAl Viro struct mount *parent, 82284d17192SAl Viro struct mountpoint *mp) 8231da177e4SLinus Torvalds { 82484d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 82538129a13SAl Viro hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry)); 82684d17192SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 827b90fa9aeSRam Pai } 828b90fa9aeSRam Pai 82912a5b529SAl Viro static void attach_shadowed(struct mount *mnt, 83012a5b529SAl Viro struct mount *parent, 83112a5b529SAl Viro struct mount *shadows) 83212a5b529SAl Viro { 83312a5b529SAl Viro if (shadows) { 834f6f99332SLinus Torvalds hlist_add_behind_rcu(&mnt->mnt_hash, &shadows->mnt_hash); 83512a5b529SAl Viro list_add(&mnt->mnt_child, &shadows->mnt_child); 83612a5b529SAl Viro } else { 83712a5b529SAl Viro hlist_add_head_rcu(&mnt->mnt_hash, 83812a5b529SAl Viro m_hash(&parent->mnt, mnt->mnt_mountpoint)); 83912a5b529SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 84012a5b529SAl Viro } 84112a5b529SAl Viro } 84212a5b529SAl Viro 843b90fa9aeSRam Pai /* 84499b7db7bSNick Piggin * vfsmount lock must be held for write 845b90fa9aeSRam Pai */ 8461d6a32acSAl Viro static void commit_tree(struct mount *mnt, struct mount *shadows) 847b90fa9aeSRam Pai { 8480714a533SAl Viro struct mount *parent = mnt->mnt_parent; 84983adc753SAl Viro struct mount *m; 850b90fa9aeSRam Pai LIST_HEAD(head); 851143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 852b90fa9aeSRam Pai 8530714a533SAl Viro BUG_ON(parent == mnt); 854b90fa9aeSRam Pai 8551a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 856f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 857143c8c91SAl Viro m->mnt_ns = n; 858f03c6599SAl Viro 859b90fa9aeSRam Pai list_splice(&head, n->list.prev); 860b90fa9aeSRam Pai 86112a5b529SAl Viro attach_shadowed(mnt, parent, shadows); 8626b3286edSKirill Korotaev touch_mnt_namespace(n); 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 865909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 8661da177e4SLinus Torvalds { 8676b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 8686b41d536SAl Viro if (next == &p->mnt_mounts) { 8691da177e4SLinus Torvalds while (1) { 870909b0a88SAl Viro if (p == root) 8711da177e4SLinus Torvalds return NULL; 8726b41d536SAl Viro next = p->mnt_child.next; 8736b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 8741da177e4SLinus Torvalds break; 8750714a533SAl Viro p = p->mnt_parent; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds } 8786b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 881315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 8829676f0c6SRam Pai { 8836b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 8846b41d536SAl Viro while (prev != &p->mnt_mounts) { 8856b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 8866b41d536SAl Viro prev = p->mnt_mounts.prev; 8879676f0c6SRam Pai } 8889676f0c6SRam Pai return p; 8899676f0c6SRam Pai } 8909676f0c6SRam Pai 8919d412a43SAl Viro struct vfsmount * 8929d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 8939d412a43SAl Viro { 894b105e270SAl Viro struct mount *mnt; 8959d412a43SAl Viro struct dentry *root; 8969d412a43SAl Viro 8979d412a43SAl Viro if (!type) 8989d412a43SAl Viro return ERR_PTR(-ENODEV); 8999d412a43SAl Viro 9009d412a43SAl Viro mnt = alloc_vfsmnt(name); 9019d412a43SAl Viro if (!mnt) 9029d412a43SAl Viro return ERR_PTR(-ENOMEM); 9039d412a43SAl Viro 9049d412a43SAl Viro if (flags & MS_KERNMOUNT) 905b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9069d412a43SAl Viro 9079d412a43SAl Viro root = mount_fs(type, flags, name, data); 9089d412a43SAl Viro if (IS_ERR(root)) { 9098ffcb32eSDavid Howells mnt_free_id(mnt); 9109d412a43SAl Viro free_vfsmnt(mnt); 9119d412a43SAl Viro return ERR_CAST(root); 9129d412a43SAl Viro } 9139d412a43SAl Viro 914b105e270SAl Viro mnt->mnt.mnt_root = root; 915b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 916a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9170714a533SAl Viro mnt->mnt_parent = mnt; 918719ea2fbSAl Viro lock_mount_hash(); 91939f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 920719ea2fbSAl Viro unlock_mount_hash(); 921b105e270SAl Viro return &mnt->mnt; 9229d412a43SAl Viro } 9239d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 9249d412a43SAl Viro 92587129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 92636341f64SRam Pai int flag) 9271da177e4SLinus Torvalds { 92887129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 929be34d1a3SDavid Howells struct mount *mnt; 930be34d1a3SDavid Howells int err; 9311da177e4SLinus Torvalds 932be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 933be34d1a3SDavid Howells if (!mnt) 934be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 935be34d1a3SDavid Howells 9367a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 93715169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 938719f5d7fSMiklos Szeredi else 93915169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 940719f5d7fSMiklos Szeredi 94115169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 942be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 943719f5d7fSMiklos Szeredi if (err) 944719f5d7fSMiklos Szeredi goto out_free; 945719f5d7fSMiklos Szeredi } 946719f5d7fSMiklos Szeredi 947f2ebb3a9SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED); 948132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 9499566d674SEric W. Biederman if (flag & CL_UNPRIVILEGED) { 9509566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_ATIME; 9519566d674SEric W. Biederman 9529566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_READONLY) 953132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 954132c94e3SEric W. Biederman 9559566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NODEV) 9569566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NODEV; 9579566d674SEric W. Biederman 9589566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOSUID) 9599566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID; 9609566d674SEric W. Biederman 9619566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOEXEC) 9629566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC; 9639566d674SEric W. Biederman } 9649566d674SEric W. Biederman 9655ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 9665ff9d8a6SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire)) 9675ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 9685ff9d8a6SEric W. Biederman 9691da177e4SLinus Torvalds atomic_inc(&sb->s_active); 970b105e270SAl Viro mnt->mnt.mnt_sb = sb; 971b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 972a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9730714a533SAl Viro mnt->mnt_parent = mnt; 974719ea2fbSAl Viro lock_mount_hash(); 97539f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 976719ea2fbSAl Viro unlock_mount_hash(); 977b90fa9aeSRam Pai 9787a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 9797a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 9806776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 98132301920SAl Viro mnt->mnt_master = old; 982fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 9838aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 984fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 9856776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 986d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 9876776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 988d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 9895afe0022SRam Pai } 990b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 9910f0afb1dSAl Viro set_mnt_shared(mnt); 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 9941da177e4SLinus Torvalds * as the original if that was on one */ 99536341f64SRam Pai if (flag & CL_EXPIRE) { 9966776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 9976776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 9981da177e4SLinus Torvalds } 999be34d1a3SDavid Howells 1000cb338d06SAl Viro return mnt; 1001719f5d7fSMiklos Szeredi 1002719f5d7fSMiklos Szeredi out_free: 10038ffcb32eSDavid Howells mnt_free_id(mnt); 1004719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1005be34d1a3SDavid Howells return ERR_PTR(err); 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 10089ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 10099ea459e1SAl Viro { 10109ea459e1SAl Viro /* 10119ea459e1SAl Viro * This probably indicates that somebody messed 10129ea459e1SAl Viro * up a mnt_want/drop_write() pair. If this 10139ea459e1SAl Viro * happens, the filesystem was probably unable 10149ea459e1SAl Viro * to make r/w->r/o transitions. 10159ea459e1SAl Viro */ 10169ea459e1SAl Viro /* 10179ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 10189ea459e1SAl Viro * so mnt_get_writers() below is safe. 10199ea459e1SAl Viro */ 10209ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 10219ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 10229ea459e1SAl Viro mnt_pin_kill(mnt); 10239ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 10249ea459e1SAl Viro dput(mnt->mnt.mnt_root); 10259ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 10269ea459e1SAl Viro mnt_free_id(mnt); 10279ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 10289ea459e1SAl Viro } 10299ea459e1SAl Viro 10309ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 10319ea459e1SAl Viro { 10329ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 10339ea459e1SAl Viro } 10349ea459e1SAl Viro 10359ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 10369ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 10379ea459e1SAl Viro { 10389ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 10399ea459e1SAl Viro struct llist_node *next; 10409ea459e1SAl Viro 10419ea459e1SAl Viro for (; node; node = next) { 10429ea459e1SAl Viro next = llist_next(node); 10439ea459e1SAl Viro cleanup_mnt(llist_entry(node, struct mount, mnt_llist)); 10449ea459e1SAl Viro } 10459ea459e1SAl Viro } 10469ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 10479ea459e1SAl Viro 1048900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 10497b7b1aceSAl Viro { 105048a066e7SAl Viro rcu_read_lock(); 1051aa9c0e07SAl Viro mnt_add_count(mnt, -1); 105248a066e7SAl Viro if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */ 105348a066e7SAl Viro rcu_read_unlock(); 105499b7db7bSNick Piggin return; 1055b3e19d92SNick Piggin } 1056719ea2fbSAl Viro lock_mount_hash(); 1057b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 105848a066e7SAl Viro rcu_read_unlock(); 1059719ea2fbSAl Viro unlock_mount_hash(); 106099b7db7bSNick Piggin return; 106199b7db7bSNick Piggin } 106248a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 106348a066e7SAl Viro rcu_read_unlock(); 106448a066e7SAl Viro unlock_mount_hash(); 106548a066e7SAl Viro return; 106648a066e7SAl Viro } 106748a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 106848a066e7SAl Viro rcu_read_unlock(); 1069962830dfSAndi Kleen 107039f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1071719ea2fbSAl Viro unlock_mount_hash(); 1072649a795aSAl Viro 10739ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 10749ea459e1SAl Viro struct task_struct *task = current; 10759ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 10769ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 10779ea459e1SAl Viro if (!task_work_add(task, &mnt->mnt_rcu, true)) 10789ea459e1SAl Viro return; 10799ea459e1SAl Viro } 10809ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 10819ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 10829ea459e1SAl Viro return; 10839ea459e1SAl Viro } 10849ea459e1SAl Viro cleanup_mnt(mnt); 1085b3e19d92SNick Piggin } 1086b3e19d92SNick Piggin 1087b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1088b3e19d92SNick Piggin { 1089b3e19d92SNick Piggin if (mnt) { 1090863d684fSAl Viro struct mount *m = real_mount(mnt); 1091b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1092863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1093863d684fSAl Viro m->mnt_expiry_mark = 0; 1094863d684fSAl Viro mntput_no_expire(m); 1095b3e19d92SNick Piggin } 1096b3e19d92SNick Piggin } 1097b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1098b3e19d92SNick Piggin 1099b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1100b3e19d92SNick Piggin { 1101b3e19d92SNick Piggin if (mnt) 110283adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1103b3e19d92SNick Piggin return mnt; 1104b3e19d92SNick Piggin } 1105b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1106b3e19d92SNick Piggin 11073064c356SAl Viro struct vfsmount *mnt_clone_internal(struct path *path) 11087b7b1aceSAl Viro { 11093064c356SAl Viro struct mount *p; 11103064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 11113064c356SAl Viro if (IS_ERR(p)) 11123064c356SAl Viro return ERR_CAST(p); 11133064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 11143064c356SAl Viro return &p->mnt; 11157b7b1aceSAl Viro } 11161da177e4SLinus Torvalds 1117b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 1118b3b304a2SMiklos Szeredi { 1119b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 1120b3b304a2SMiklos Szeredi } 1121b3b304a2SMiklos Szeredi 1122b3b304a2SMiklos Szeredi /* 1123b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 1124b3b304a2SMiklos Szeredi * implement more complex mount option showing. 1125b3b304a2SMiklos Szeredi * 1126b3b304a2SMiklos Szeredi * See also save_mount_options(). 1127b3b304a2SMiklos Szeredi */ 112834c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 1129b3b304a2SMiklos Szeredi { 11302a32cebdSAl Viro const char *options; 11312a32cebdSAl Viro 11322a32cebdSAl Viro rcu_read_lock(); 113334c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 1134b3b304a2SMiklos Szeredi 1135b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 1136b3b304a2SMiklos Szeredi seq_putc(m, ','); 1137b3b304a2SMiklos Szeredi mangle(m, options); 1138b3b304a2SMiklos Szeredi } 11392a32cebdSAl Viro rcu_read_unlock(); 1140b3b304a2SMiklos Szeredi 1141b3b304a2SMiklos Szeredi return 0; 1142b3b304a2SMiklos Szeredi } 1143b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 1144b3b304a2SMiklos Szeredi 1145b3b304a2SMiklos Szeredi /* 1146b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 1147b3b304a2SMiklos Szeredi * called from the fill_super() callback. 1148b3b304a2SMiklos Szeredi * 1149b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 1150b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 1151b3b304a2SMiklos Szeredi * remount fails. 1152b3b304a2SMiklos Szeredi * 1153b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 1154b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 1155b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 1156b3b304a2SMiklos Szeredi * any more. 1157b3b304a2SMiklos Szeredi */ 1158b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 1159b3b304a2SMiklos Szeredi { 11602a32cebdSAl Viro BUG_ON(sb->s_options); 11612a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 1162b3b304a2SMiklos Szeredi } 1163b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 1164b3b304a2SMiklos Szeredi 11652a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 11662a32cebdSAl Viro { 11672a32cebdSAl Viro char *old = sb->s_options; 11682a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 11692a32cebdSAl Viro if (old) { 11702a32cebdSAl Viro synchronize_rcu(); 11712a32cebdSAl Viro kfree(old); 11722a32cebdSAl Viro } 11732a32cebdSAl Viro } 11742a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 11752a32cebdSAl Viro 1176a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 11770226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 11781da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 11791da177e4SLinus Torvalds { 11806ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 11811da177e4SLinus Torvalds 1182390c6843SRam Pai down_read(&namespace_sem); 1183c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1184c7999c36SAl Viro void *v = p->cached_mount; 1185c7999c36SAl Viro if (*pos == p->cached_index) 1186c7999c36SAl Viro return v; 1187c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1188c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1189c7999c36SAl Viro return p->cached_mount = v; 1190c7999c36SAl Viro } 1191c7999c36SAl Viro } 1192c7999c36SAl Viro 1193c7999c36SAl Viro p->cached_event = p->ns->event; 1194c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1195c7999c36SAl Viro p->cached_index = *pos; 1196c7999c36SAl Viro return p->cached_mount; 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 12001da177e4SLinus Torvalds { 12016ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 1202b0765fb8SPavel Emelianov 1203c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1204c7999c36SAl Viro p->cached_index = *pos; 1205c7999c36SAl Viro return p->cached_mount; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 12091da177e4SLinus Torvalds { 1210390c6843SRam Pai up_read(&namespace_sem); 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 12130226f492SAl Viro static int m_show(struct seq_file *m, void *v) 12149f5596afSAl Viro { 12156ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 12161a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 12170226f492SAl Viro return p->show(m, &r->mnt); 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 1220a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 12211da177e4SLinus Torvalds .start = m_start, 12221da177e4SLinus Torvalds .next = m_next, 12231da177e4SLinus Torvalds .stop = m_stop, 12240226f492SAl Viro .show = m_show, 1225b4629fe2SChuck Lever }; 1226a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1227b4629fe2SChuck Lever 12281da177e4SLinus Torvalds /** 12291da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 12301da177e4SLinus Torvalds * @mnt: root of mount tree 12311da177e4SLinus Torvalds * 12321da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 12331da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 12341da177e4SLinus Torvalds * busy. 12351da177e4SLinus Torvalds */ 1236909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 12371da177e4SLinus Torvalds { 1238909b0a88SAl Viro struct mount *mnt = real_mount(m); 123936341f64SRam Pai int actual_refs = 0; 124036341f64SRam Pai int minimum_refs = 0; 1241315fc83eSAl Viro struct mount *p; 1242909b0a88SAl Viro BUG_ON(!m); 12431da177e4SLinus Torvalds 1244b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1245719ea2fbSAl Viro lock_mount_hash(); 1246909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 124783adc753SAl Viro actual_refs += mnt_get_count(p); 12481da177e4SLinus Torvalds minimum_refs += 2; 12491da177e4SLinus Torvalds } 1250719ea2fbSAl Viro unlock_mount_hash(); 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds if (actual_refs > minimum_refs) 12531da177e4SLinus Torvalds return 0; 1254e3474a8eSIan Kent 1255e3474a8eSIan Kent return 1; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds /** 12611da177e4SLinus Torvalds * may_umount - check if a mount point is busy 12621da177e4SLinus Torvalds * @mnt: root of mount 12631da177e4SLinus Torvalds * 12641da177e4SLinus Torvalds * This is called to check if a mount point has any 12651da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 12661da177e4SLinus Torvalds * mount has sub mounts this will return busy 12671da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 12681da177e4SLinus Torvalds * 12691da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 12701da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 12711da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 12721da177e4SLinus Torvalds */ 12731da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 12741da177e4SLinus Torvalds { 1275e3474a8eSIan Kent int ret = 1; 12768ad08d8aSAl Viro down_read(&namespace_sem); 1277719ea2fbSAl Viro lock_mount_hash(); 12781ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1279e3474a8eSIan Kent ret = 0; 1280719ea2fbSAl Viro unlock_mount_hash(); 12818ad08d8aSAl Viro up_read(&namespace_sem); 1282a05964f3SRam Pai return ret; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 12861da177e4SLinus Torvalds 128738129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1288e3197d83SAl Viro 128997216be0SAl Viro static void namespace_unlock(void) 12901da177e4SLinus Torvalds { 1291d5e50f74SAl Viro struct mount *mnt; 129238129a13SAl Viro struct hlist_head head = unmounted; 129397216be0SAl Viro 129438129a13SAl Viro if (likely(hlist_empty(&head))) { 129597216be0SAl Viro up_write(&namespace_sem); 129697216be0SAl Viro return; 129797216be0SAl Viro } 129897216be0SAl Viro 129938129a13SAl Viro head.first->pprev = &head.first; 130038129a13SAl Viro INIT_HLIST_HEAD(&unmounted); 130138129a13SAl Viro 130281b6b061SAl Viro /* undo decrements we'd done in umount_tree() */ 130381b6b061SAl Viro hlist_for_each_entry(mnt, &head, mnt_hash) 130481b6b061SAl Viro if (mnt->mnt_ex_mountpoint.mnt) 130581b6b061SAl Viro mntget(mnt->mnt_ex_mountpoint.mnt); 130681b6b061SAl Viro 130797216be0SAl Viro up_write(&namespace_sem); 130897216be0SAl Viro 130948a066e7SAl Viro synchronize_rcu(); 131048a066e7SAl Viro 131138129a13SAl Viro while (!hlist_empty(&head)) { 131238129a13SAl Viro mnt = hlist_entry(head.first, struct mount, mnt_hash); 131338129a13SAl Viro hlist_del_init(&mnt->mnt_hash); 1314aba809cfSAl Viro if (mnt->mnt_ex_mountpoint.mnt) 1315aba809cfSAl Viro path_put(&mnt->mnt_ex_mountpoint); 1316d5e50f74SAl Viro mntput(&mnt->mnt); 131770fbcdf4SRam Pai } 131870fbcdf4SRam Pai } 131970fbcdf4SRam Pai 132097216be0SAl Viro static inline void namespace_lock(void) 1321e3197d83SAl Viro { 132297216be0SAl Viro down_write(&namespace_sem); 1323e3197d83SAl Viro } 1324e3197d83SAl Viro 132599b7db7bSNick Piggin /* 132648a066e7SAl Viro * mount_lock must be held 132799b7db7bSNick Piggin * namespace_sem must be held for write 132848a066e7SAl Viro * how = 0 => just this tree, don't propagate 132948a066e7SAl Viro * how = 1 => propagate; we know that nobody else has reference to any victims 133048a066e7SAl Viro * how = 2 => lazy umount 133199b7db7bSNick Piggin */ 133248a066e7SAl Viro void umount_tree(struct mount *mnt, int how) 133370fbcdf4SRam Pai { 133438129a13SAl Viro HLIST_HEAD(tmp_list); 1335315fc83eSAl Viro struct mount *p; 133638129a13SAl Viro struct mount *last = NULL; 133770fbcdf4SRam Pai 133838129a13SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 133938129a13SAl Viro hlist_del_init_rcu(&p->mnt_hash); 134038129a13SAl Viro hlist_add_head(&p->mnt_hash, &tmp_list); 134138129a13SAl Viro } 134270fbcdf4SRam Pai 134388b368f2SAl Viro hlist_for_each_entry(p, &tmp_list, mnt_hash) 134488b368f2SAl Viro list_del_init(&p->mnt_child); 134588b368f2SAl Viro 134648a066e7SAl Viro if (how) 13477b8a53fdSAl Viro propagate_umount(&tmp_list); 1348a05964f3SRam Pai 134938129a13SAl Viro hlist_for_each_entry(p, &tmp_list, mnt_hash) { 13506776db3dSAl Viro list_del_init(&p->mnt_expire); 13511a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1352143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 135363d37a84SAl Viro p->mnt_ns = NULL; 135448a066e7SAl Viro if (how < 2) 135548a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 1356676da58dSAl Viro if (mnt_has_parent(p)) { 13570a5eb7c8SEric W. Biederman hlist_del_init(&p->mnt_mp_list); 135884d17192SAl Viro put_mountpoint(p->mnt_mp); 135981b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1360aba809cfSAl Viro /* move the reference to mountpoint into ->mnt_ex_mountpoint */ 1361aba809cfSAl Viro p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint; 1362aba809cfSAl Viro p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt; 1363aba809cfSAl Viro p->mnt_mountpoint = p->mnt.mnt_root; 1364aba809cfSAl Viro p->mnt_parent = p; 136584d17192SAl Viro p->mnt_mp = NULL; 13667c4b93d8SAl Viro } 13670f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 136838129a13SAl Viro last = p; 13691da177e4SLinus Torvalds } 137038129a13SAl Viro if (last) { 137138129a13SAl Viro last->mnt_hash.next = unmounted.first; 137238129a13SAl Viro unmounted.first = tmp_list.first; 137338129a13SAl Viro unmounted.first->pprev = &unmounted.first; 137438129a13SAl Viro } 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 1377b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1378c35038beSAl Viro 13791ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 13801da177e4SLinus Torvalds { 13811ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 13821da177e4SLinus Torvalds int retval; 13831da177e4SLinus Torvalds 13841ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 13851da177e4SLinus Torvalds if (retval) 13861da177e4SLinus Torvalds return retval; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds /* 13891da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 13901da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 13911da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 13921da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 13931da177e4SLinus Torvalds */ 13941da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 13951ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 13961da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 13971da177e4SLinus Torvalds return -EINVAL; 13981da177e4SLinus Torvalds 1399b3e19d92SNick Piggin /* 1400b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1401b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1402b3e19d92SNick Piggin */ 1403719ea2fbSAl Viro lock_mount_hash(); 140483adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1405719ea2fbSAl Viro unlock_mount_hash(); 14061da177e4SLinus Torvalds return -EBUSY; 1407b3e19d92SNick Piggin } 1408719ea2fbSAl Viro unlock_mount_hash(); 14091da177e4SLinus Torvalds 1410863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 14111da177e4SLinus Torvalds return -EAGAIN; 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds /* 14151da177e4SLinus Torvalds * If we may have to abort operations to get out of this 14161da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 14171da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 14181da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 14191da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 14201da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 14211da177e4SLinus Torvalds * about for the moment. 14221da177e4SLinus Torvalds */ 14231da177e4SLinus Torvalds 142442faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 142542faad99SAl Viro sb->s_op->umount_begin(sb); 142642faad99SAl Viro } 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds /* 14291da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 14301da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 14311da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 14321da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 14331da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 14341da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 14351da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 14361da177e4SLinus Torvalds */ 14371ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 14381da177e4SLinus Torvalds /* 14391da177e4SLinus Torvalds * Special case for "unmounting" root ... 14401da177e4SLinus Torvalds * we just try to remount it readonly. 14411da177e4SLinus Torvalds */ 14421da177e4SLinus Torvalds down_write(&sb->s_umount); 14434aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 14441da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 14451da177e4SLinus Torvalds up_write(&sb->s_umount); 14461da177e4SLinus Torvalds return retval; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 144997216be0SAl Viro namespace_lock(); 1450719ea2fbSAl Viro lock_mount_hash(); 14515addc5ddSAl Viro event++; 14521da177e4SLinus Torvalds 145348a066e7SAl Viro if (flags & MNT_DETACH) { 145448a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 145548a066e7SAl Viro umount_tree(mnt, 2); 145648a066e7SAl Viro retval = 0; 145748a066e7SAl Viro } else { 1458b54b9be7SAl Viro shrink_submounts(mnt); 14591da177e4SLinus Torvalds retval = -EBUSY; 146048a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 14611a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1462328e6d90SAl Viro umount_tree(mnt, 1); 14631da177e4SLinus Torvalds retval = 0; 14641da177e4SLinus Torvalds } 146548a066e7SAl Viro } 1466719ea2fbSAl Viro unlock_mount_hash(); 1467e3197d83SAl Viro namespace_unlock(); 14681da177e4SLinus Torvalds return retval; 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds /* 147280b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 147380b5dce8SEric W. Biederman * 147480b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 147580b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 147680b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 147780b5dce8SEric W. Biederman * leaking them. 147880b5dce8SEric W. Biederman * 147980b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 148080b5dce8SEric W. Biederman */ 148180b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 148280b5dce8SEric W. Biederman { 148380b5dce8SEric W. Biederman struct mountpoint *mp; 148480b5dce8SEric W. Biederman struct mount *mnt; 148580b5dce8SEric W. Biederman 148680b5dce8SEric W. Biederman namespace_lock(); 148780b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 148880b5dce8SEric W. Biederman if (!mp) 148980b5dce8SEric W. Biederman goto out_unlock; 149080b5dce8SEric W. Biederman 149180b5dce8SEric W. Biederman lock_mount_hash(); 149280b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 149380b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 149480b5dce8SEric W. Biederman umount_tree(mnt, 2); 149580b5dce8SEric W. Biederman } 149680b5dce8SEric W. Biederman unlock_mount_hash(); 149780b5dce8SEric W. Biederman put_mountpoint(mp); 149880b5dce8SEric W. Biederman out_unlock: 149980b5dce8SEric W. Biederman namespace_unlock(); 150080b5dce8SEric W. Biederman } 150180b5dce8SEric W. Biederman 150280b5dce8SEric W. Biederman /* 15039b40bc90SAl Viro * Is the caller allowed to modify his namespace? 15049b40bc90SAl Viro */ 15059b40bc90SAl Viro static inline bool may_mount(void) 15069b40bc90SAl Viro { 15079b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 15089b40bc90SAl Viro } 15099b40bc90SAl Viro 15109b40bc90SAl Viro /* 15111da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 15121da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 15131da177e4SLinus Torvalds * 15141da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 15151da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 15161da177e4SLinus Torvalds */ 15171da177e4SLinus Torvalds 1518bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 15191da177e4SLinus Torvalds { 15202d8f3038SAl Viro struct path path; 1521900148dcSAl Viro struct mount *mnt; 15221da177e4SLinus Torvalds int retval; 1523db1f05bbSMiklos Szeredi int lookup_flags = 0; 15241da177e4SLinus Torvalds 1525db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1526db1f05bbSMiklos Szeredi return -EINVAL; 1527db1f05bbSMiklos Szeredi 15289b40bc90SAl Viro if (!may_mount()) 15299b40bc90SAl Viro return -EPERM; 15309b40bc90SAl Viro 1531db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1532db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1533db1f05bbSMiklos Szeredi 1534197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 15351da177e4SLinus Torvalds if (retval) 15361da177e4SLinus Torvalds goto out; 1537900148dcSAl Viro mnt = real_mount(path.mnt); 15381da177e4SLinus Torvalds retval = -EINVAL; 15392d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 15401da177e4SLinus Torvalds goto dput_and_out; 1541143c8c91SAl Viro if (!check_mnt(mnt)) 15421da177e4SLinus Torvalds goto dput_and_out; 15435ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 15445ff9d8a6SEric W. Biederman goto dput_and_out; 15451da177e4SLinus Torvalds 1546900148dcSAl Viro retval = do_umount(mnt, flags); 15471da177e4SLinus Torvalds dput_and_out: 1548429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 15492d8f3038SAl Viro dput(path.dentry); 1550900148dcSAl Viro mntput_no_expire(mnt); 15511da177e4SLinus Torvalds out: 15521da177e4SLinus Torvalds return retval; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds /* 15581da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 15591da177e4SLinus Torvalds */ 1560bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 15611da177e4SLinus Torvalds { 15621da177e4SLinus Torvalds return sys_umount(name, 0); 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds #endif 15661da177e4SLinus Torvalds 15674ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 15688823c079SEric W. Biederman { 15694ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 15704ce5d2b1SEric W. Biederman struct inode *inode = dentry->d_inode; 15710bb80f24SDavid Howells struct proc_ns *ei; 15728823c079SEric W. Biederman 15738823c079SEric W. Biederman if (!proc_ns_inode(inode)) 15748823c079SEric W. Biederman return false; 15758823c079SEric W. Biederman 15760bb80f24SDavid Howells ei = get_proc_ns(inode); 15778823c079SEric W. Biederman if (ei->ns_ops != &mntns_operations) 15788823c079SEric W. Biederman return false; 15798823c079SEric W. Biederman 15804ce5d2b1SEric W. Biederman return true; 15814ce5d2b1SEric W. Biederman } 15824ce5d2b1SEric W. Biederman 15834ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 15844ce5d2b1SEric W. Biederman { 15854ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 15864ce5d2b1SEric W. Biederman * mount namespace loop? 15874ce5d2b1SEric W. Biederman */ 15884ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 15894ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 15904ce5d2b1SEric W. Biederman return false; 15914ce5d2b1SEric W. Biederman 15924ce5d2b1SEric W. Biederman mnt_ns = get_proc_ns(dentry->d_inode)->ns; 15938823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 15948823c079SEric W. Biederman } 15958823c079SEric W. Biederman 159687129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 159736341f64SRam Pai int flag) 15981da177e4SLinus Torvalds { 159984d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 16001da177e4SLinus Torvalds 16014ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 16024ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 16034ce5d2b1SEric W. Biederman 16044ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1605be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 16069676f0c6SRam Pai 160736341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1608be34d1a3SDavid Howells if (IS_ERR(q)) 1609be34d1a3SDavid Howells return q; 1610be34d1a3SDavid Howells 16115ff9d8a6SEric W. Biederman q->mnt.mnt_flags &= ~MNT_LOCKED; 1612a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds p = mnt; 16156b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1616315fc83eSAl Viro struct mount *s; 16177ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 16181da177e4SLinus Torvalds continue; 16191da177e4SLinus Torvalds 1620909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 162112a5b529SAl Viro struct mount *t = NULL; 16224ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 16234ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 16244ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 16254ce5d2b1SEric W. Biederman continue; 16264ce5d2b1SEric W. Biederman } 16274ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 16284ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 16299676f0c6SRam Pai s = skip_mnt_tree(s); 16309676f0c6SRam Pai continue; 16319676f0c6SRam Pai } 16320714a533SAl Viro while (p != s->mnt_parent) { 16330714a533SAl Viro p = p->mnt_parent; 16340714a533SAl Viro q = q->mnt_parent; 16351da177e4SLinus Torvalds } 163687129cc0SAl Viro p = s; 163784d17192SAl Viro parent = q; 163887129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1639be34d1a3SDavid Howells if (IS_ERR(q)) 1640be34d1a3SDavid Howells goto out; 1641719ea2fbSAl Viro lock_mount_hash(); 16421a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 164312a5b529SAl Viro mnt_set_mountpoint(parent, p->mnt_mp, q); 164412a5b529SAl Viro if (!list_empty(&parent->mnt_mounts)) { 164512a5b529SAl Viro t = list_last_entry(&parent->mnt_mounts, 164612a5b529SAl Viro struct mount, mnt_child); 164712a5b529SAl Viro if (t->mnt_mp != p->mnt_mp) 164812a5b529SAl Viro t = NULL; 164912a5b529SAl Viro } 165012a5b529SAl Viro attach_shadowed(q, parent, t); 1651719ea2fbSAl Viro unlock_mount_hash(); 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds return res; 1655be34d1a3SDavid Howells out: 16561da177e4SLinus Torvalds if (res) { 1657719ea2fbSAl Viro lock_mount_hash(); 1658328e6d90SAl Viro umount_tree(res, 0); 1659719ea2fbSAl Viro unlock_mount_hash(); 16601da177e4SLinus Torvalds } 1661be34d1a3SDavid Howells return q; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 1664be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1665be34d1a3SDavid Howells 1666589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 16678aec0809SAl Viro { 1668cb338d06SAl Viro struct mount *tree; 166997216be0SAl Viro namespace_lock(); 167087129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 167187129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1672328e6d90SAl Viro namespace_unlock(); 1673be34d1a3SDavid Howells if (IS_ERR(tree)) 167452e220d3SDan Carpenter return ERR_CAST(tree); 1675be34d1a3SDavid Howells return &tree->mnt; 16768aec0809SAl Viro } 16778aec0809SAl Viro 16788aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 16798aec0809SAl Viro { 168097216be0SAl Viro namespace_lock(); 1681719ea2fbSAl Viro lock_mount_hash(); 1682328e6d90SAl Viro umount_tree(real_mount(mnt), 0); 1683719ea2fbSAl Viro unlock_mount_hash(); 16843ab6abeeSAl Viro namespace_unlock(); 16858aec0809SAl Viro } 16868aec0809SAl Viro 16871f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 16881f707137SAl Viro struct vfsmount *root) 16891f707137SAl Viro { 16901a4eeaf2SAl Viro struct mount *mnt; 16911f707137SAl Viro int res = f(root, arg); 16921f707137SAl Viro if (res) 16931f707137SAl Viro return res; 16941a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 16951a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 16961f707137SAl Viro if (res) 16971f707137SAl Viro return res; 16981f707137SAl Viro } 16991f707137SAl Viro return 0; 17001f707137SAl Viro } 17011f707137SAl Viro 17024b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1703719f5d7fSMiklos Szeredi { 1704315fc83eSAl Viro struct mount *p; 1705719f5d7fSMiklos Szeredi 1706909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1707fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 17084b8b21f4SAl Viro mnt_release_group_id(p); 1709719f5d7fSMiklos Szeredi } 1710719f5d7fSMiklos Szeredi } 1711719f5d7fSMiklos Szeredi 17124b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1713719f5d7fSMiklos Szeredi { 1714315fc83eSAl Viro struct mount *p; 1715719f5d7fSMiklos Szeredi 1716909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1717fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 17184b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1719719f5d7fSMiklos Szeredi if (err) { 17204b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1721719f5d7fSMiklos Szeredi return err; 1722719f5d7fSMiklos Szeredi } 1723719f5d7fSMiklos Szeredi } 1724719f5d7fSMiklos Szeredi } 1725719f5d7fSMiklos Szeredi 1726719f5d7fSMiklos Szeredi return 0; 1727719f5d7fSMiklos Szeredi } 1728719f5d7fSMiklos Szeredi 1729b90fa9aeSRam Pai /* 1730b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1731b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 173221444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 173321444403SRam Pai * store the parent mount and mountpoint dentry. 173421444403SRam Pai * (done when source_mnt is moved) 1735b90fa9aeSRam Pai * 1736b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1737b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 17389676f0c6SRam Pai * --------------------------------------------------------------------------- 1739b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 17409676f0c6SRam Pai * |************************************************************************** 17419676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 17429676f0c6SRam Pai * | dest | | | | | 17439676f0c6SRam Pai * | | | | | | | 17449676f0c6SRam Pai * | v | | | | | 17459676f0c6SRam Pai * |************************************************************************** 17469676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 17475afe0022SRam Pai * | | | | | | 17489676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 17499676f0c6SRam Pai * *************************************************************************** 1750b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1751b90fa9aeSRam Pai * destination mount. 1752b90fa9aeSRam Pai * 1753b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1754b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1755b90fa9aeSRam Pai * the peer group of the source mount. 1756b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1757b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1758b90fa9aeSRam Pai * mount. 17595afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 17605afe0022SRam Pai * of the destination mount and the cloned mount is made slave 17615afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 17625afe0022SRam Pai * is marked as 'shared and slave'. 17635afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 17645afe0022SRam Pai * source mount. 17655afe0022SRam Pai * 17669676f0c6SRam Pai * --------------------------------------------------------------------------- 176721444403SRam Pai * | MOVE MOUNT OPERATION | 17689676f0c6SRam Pai * |************************************************************************** 17699676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 17709676f0c6SRam Pai * | dest | | | | | 17719676f0c6SRam Pai * | | | | | | | 17729676f0c6SRam Pai * | v | | | | | 17739676f0c6SRam Pai * |************************************************************************** 17749676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 17755afe0022SRam Pai * | | | | | | 17769676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 17779676f0c6SRam Pai * *************************************************************************** 17785afe0022SRam Pai * 17795afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 17805afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 178121444403SRam Pai * (+*) the mount is moved to the destination. 17825afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 17835afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 17845afe0022SRam Pai * the mount is marked as 'shared and slave'. 17855afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1786b90fa9aeSRam Pai * 1787b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1788b90fa9aeSRam Pai * applied to each mount in the tree. 1789b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1790b90fa9aeSRam Pai * in allocations. 1791b90fa9aeSRam Pai */ 17920fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 179384d17192SAl Viro struct mount *dest_mnt, 179484d17192SAl Viro struct mountpoint *dest_mp, 179584d17192SAl Viro struct path *parent_path) 1796b90fa9aeSRam Pai { 179738129a13SAl Viro HLIST_HEAD(tree_list); 1798315fc83eSAl Viro struct mount *child, *p; 179938129a13SAl Viro struct hlist_node *n; 1800719f5d7fSMiklos Szeredi int err; 1801b90fa9aeSRam Pai 1802fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 18030fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1804719f5d7fSMiklos Szeredi if (err) 1805719f5d7fSMiklos Szeredi goto out; 180684d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 1807f2ebb3a9SAl Viro lock_mount_hash(); 1808719f5d7fSMiklos Szeredi if (err) 1809719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1810909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 18110f0afb1dSAl Viro set_mnt_shared(p); 18120b1b901bSAl Viro } else { 18130b1b901bSAl Viro lock_mount_hash(); 1814b90fa9aeSRam Pai } 18151a390689SAl Viro if (parent_path) { 18160fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 181784d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 1818143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 181921444403SRam Pai } else { 182084d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 18211d6a32acSAl Viro commit_tree(source_mnt, NULL); 182221444403SRam Pai } 1823b90fa9aeSRam Pai 182438129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 18251d6a32acSAl Viro struct mount *q; 182638129a13SAl Viro hlist_del_init(&child->mnt_hash); 18271d6a32acSAl Viro q = __lookup_mnt_last(&child->mnt_parent->mnt, 18281d6a32acSAl Viro child->mnt_mountpoint); 18291d6a32acSAl Viro commit_tree(child, q); 1830b90fa9aeSRam Pai } 1831719ea2fbSAl Viro unlock_mount_hash(); 183299b7db7bSNick Piggin 1833b90fa9aeSRam Pai return 0; 1834719f5d7fSMiklos Szeredi 1835719f5d7fSMiklos Szeredi out_cleanup_ids: 1836f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 1837f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 1838f2ebb3a9SAl Viro umount_tree(child, 0); 1839f2ebb3a9SAl Viro } 1840f2ebb3a9SAl Viro unlock_mount_hash(); 18410fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1842719f5d7fSMiklos Szeredi out: 1843719f5d7fSMiklos Szeredi return err; 1844b90fa9aeSRam Pai } 1845b90fa9aeSRam Pai 184684d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 1847b12cea91SAl Viro { 1848b12cea91SAl Viro struct vfsmount *mnt; 184984d17192SAl Viro struct dentry *dentry = path->dentry; 1850b12cea91SAl Viro retry: 185184d17192SAl Viro mutex_lock(&dentry->d_inode->i_mutex); 185284d17192SAl Viro if (unlikely(cant_mount(dentry))) { 185384d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 185484d17192SAl Viro return ERR_PTR(-ENOENT); 1855b12cea91SAl Viro } 185697216be0SAl Viro namespace_lock(); 1857b12cea91SAl Viro mnt = lookup_mnt(path); 185884d17192SAl Viro if (likely(!mnt)) { 1859e2dfa935SEric W. Biederman struct mountpoint *mp = lookup_mountpoint(dentry); 1860e2dfa935SEric W. Biederman if (!mp) 1861e2dfa935SEric W. Biederman mp = new_mountpoint(dentry); 186284d17192SAl Viro if (IS_ERR(mp)) { 186397216be0SAl Viro namespace_unlock(); 186484d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 186584d17192SAl Viro return mp; 186684d17192SAl Viro } 186784d17192SAl Viro return mp; 186884d17192SAl Viro } 186997216be0SAl Viro namespace_unlock(); 1870b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1871b12cea91SAl Viro path_put(path); 1872b12cea91SAl Viro path->mnt = mnt; 187384d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 1874b12cea91SAl Viro goto retry; 1875b12cea91SAl Viro } 1876b12cea91SAl Viro 187784d17192SAl Viro static void unlock_mount(struct mountpoint *where) 1878b12cea91SAl Viro { 187984d17192SAl Viro struct dentry *dentry = where->m_dentry; 188084d17192SAl Viro put_mountpoint(where); 1881328e6d90SAl Viro namespace_unlock(); 188284d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 1883b12cea91SAl Viro } 1884b12cea91SAl Viro 188584d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 18861da177e4SLinus Torvalds { 188795bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 18881da177e4SLinus Torvalds return -EINVAL; 18891da177e4SLinus Torvalds 189084d17192SAl Viro if (S_ISDIR(mp->m_dentry->d_inode->i_mode) != 189195bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 18921da177e4SLinus Torvalds return -ENOTDIR; 18931da177e4SLinus Torvalds 189484d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds /* 18987a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 18997a2e8a8fSValerie Aurora */ 19007a2e8a8fSValerie Aurora 19017a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 19027a2e8a8fSValerie Aurora { 19037c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 19047a2e8a8fSValerie Aurora 19057a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 19067a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 19077a2e8a8fSValerie Aurora return 0; 19087a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 19097a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 19107a2e8a8fSValerie Aurora return 0; 19117a2e8a8fSValerie Aurora return type; 19127a2e8a8fSValerie Aurora } 19137a2e8a8fSValerie Aurora 19147a2e8a8fSValerie Aurora /* 191507b20889SRam Pai * recursively change the type of the mountpoint. 191607b20889SRam Pai */ 19170a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 191807b20889SRam Pai { 1919315fc83eSAl Viro struct mount *m; 19204b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 192107b20889SRam Pai int recurse = flag & MS_REC; 19227a2e8a8fSValerie Aurora int type; 1923719f5d7fSMiklos Szeredi int err = 0; 192407b20889SRam Pai 19252d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 192607b20889SRam Pai return -EINVAL; 192707b20889SRam Pai 19287a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 19297a2e8a8fSValerie Aurora if (!type) 19307a2e8a8fSValerie Aurora return -EINVAL; 19317a2e8a8fSValerie Aurora 193297216be0SAl Viro namespace_lock(); 1933719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1934719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1935719f5d7fSMiklos Szeredi if (err) 1936719f5d7fSMiklos Szeredi goto out_unlock; 1937719f5d7fSMiklos Szeredi } 1938719f5d7fSMiklos Szeredi 1939719ea2fbSAl Viro lock_mount_hash(); 1940909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 19410f0afb1dSAl Viro change_mnt_propagation(m, type); 1942719ea2fbSAl Viro unlock_mount_hash(); 1943719f5d7fSMiklos Szeredi 1944719f5d7fSMiklos Szeredi out_unlock: 194597216be0SAl Viro namespace_unlock(); 1946719f5d7fSMiklos Szeredi return err; 194707b20889SRam Pai } 194807b20889SRam Pai 19495ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 19505ff9d8a6SEric W. Biederman { 19515ff9d8a6SEric W. Biederman struct mount *child; 19525ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 19535ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 19545ff9d8a6SEric W. Biederman continue; 19555ff9d8a6SEric W. Biederman 19565ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 19575ff9d8a6SEric W. Biederman return true; 19585ff9d8a6SEric W. Biederman } 19595ff9d8a6SEric W. Biederman return false; 19605ff9d8a6SEric W. Biederman } 19615ff9d8a6SEric W. Biederman 196207b20889SRam Pai /* 19631da177e4SLinus Torvalds * do loopback mount. 19641da177e4SLinus Torvalds */ 1965808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 19662dafe1c4SEric Sandeen int recurse) 19671da177e4SLinus Torvalds { 19682d92ab3cSAl Viro struct path old_path; 196984d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 197084d17192SAl Viro struct mountpoint *mp; 197157eccb83SAl Viro int err; 19721da177e4SLinus Torvalds if (!old_name || !*old_name) 19731da177e4SLinus Torvalds return -EINVAL; 1974815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 19751da177e4SLinus Torvalds if (err) 19761da177e4SLinus Torvalds return err; 19771da177e4SLinus Torvalds 19788823c079SEric W. Biederman err = -EINVAL; 19794ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 19808823c079SEric W. Biederman goto out; 19818823c079SEric W. Biederman 198284d17192SAl Viro mp = lock_mount(path); 198384d17192SAl Viro err = PTR_ERR(mp); 198484d17192SAl Viro if (IS_ERR(mp)) 19859676f0c6SRam Pai goto out; 19869676f0c6SRam Pai 198787129cc0SAl Viro old = real_mount(old_path.mnt); 198884d17192SAl Viro parent = real_mount(path->mnt); 198987129cc0SAl Viro 1990b12cea91SAl Viro err = -EINVAL; 1991fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1992b12cea91SAl Viro goto out2; 1993b12cea91SAl Viro 199484d17192SAl Viro if (!check_mnt(parent) || !check_mnt(old)) 1995b12cea91SAl Viro goto out2; 1996ccd48bc7SAl Viro 19975ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 19985ff9d8a6SEric W. Biederman goto out2; 19995ff9d8a6SEric W. Biederman 20001da177e4SLinus Torvalds if (recurse) 20014ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 20021da177e4SLinus Torvalds else 200387129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 20041da177e4SLinus Torvalds 2005be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2006be34d1a3SDavid Howells err = PTR_ERR(mnt); 2007e9c5d8a5SAndrey Vagin goto out2; 2008be34d1a3SDavid Howells } 2009ccd48bc7SAl Viro 20105ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 20115ff9d8a6SEric W. Biederman 201284d17192SAl Viro err = graft_tree(mnt, parent, mp); 20131da177e4SLinus Torvalds if (err) { 2014719ea2fbSAl Viro lock_mount_hash(); 2015328e6d90SAl Viro umount_tree(mnt, 0); 2016719ea2fbSAl Viro unlock_mount_hash(); 20175b83d2c5SRam Pai } 2018b12cea91SAl Viro out2: 201984d17192SAl Viro unlock_mount(mp); 2020ccd48bc7SAl Viro out: 20212d92ab3cSAl Viro path_put(&old_path); 20221da177e4SLinus Torvalds return err; 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 20252e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 20262e4b7fcdSDave Hansen { 20272e4b7fcdSDave Hansen int error = 0; 20282e4b7fcdSDave Hansen int readonly_request = 0; 20292e4b7fcdSDave Hansen 20302e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 20312e4b7fcdSDave Hansen readonly_request = 1; 20322e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 20332e4b7fcdSDave Hansen return 0; 20342e4b7fcdSDave Hansen 20352e4b7fcdSDave Hansen if (readonly_request) 203683adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 20372e4b7fcdSDave Hansen else 203883adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 20392e4b7fcdSDave Hansen return error; 20402e4b7fcdSDave Hansen } 20412e4b7fcdSDave Hansen 20421da177e4SLinus Torvalds /* 20431da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 20441da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 20451da177e4SLinus Torvalds * on it - tough luck. 20461da177e4SLinus Torvalds */ 20470a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 20481da177e4SLinus Torvalds void *data) 20491da177e4SLinus Torvalds { 20501da177e4SLinus Torvalds int err; 20512d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2052143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 20531da177e4SLinus Torvalds 2054143c8c91SAl Viro if (!check_mnt(mnt)) 20551da177e4SLinus Torvalds return -EINVAL; 20561da177e4SLinus Torvalds 20572d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 20581da177e4SLinus Torvalds return -EINVAL; 20591da177e4SLinus Torvalds 206007b64558SEric W. Biederman /* Don't allow changing of locked mnt flags. 206107b64558SEric W. Biederman * 206207b64558SEric W. Biederman * No locks need to be held here while testing the various 206307b64558SEric W. Biederman * MNT_LOCK flags because those flags can never be cleared 206407b64558SEric W. Biederman * once they are set. 206507b64558SEric W. Biederman */ 206607b64558SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) && 206707b64558SEric W. Biederman !(mnt_flags & MNT_READONLY)) { 206807b64558SEric W. Biederman return -EPERM; 206907b64558SEric W. Biederman } 20709566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) && 20719566d674SEric W. Biederman !(mnt_flags & MNT_NODEV)) { 20729566d674SEric W. Biederman return -EPERM; 20739566d674SEric W. Biederman } 20749566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) && 20759566d674SEric W. Biederman !(mnt_flags & MNT_NOSUID)) { 20769566d674SEric W. Biederman return -EPERM; 20779566d674SEric W. Biederman } 20789566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) && 20799566d674SEric W. Biederman !(mnt_flags & MNT_NOEXEC)) { 20809566d674SEric W. Biederman return -EPERM; 20819566d674SEric W. Biederman } 20829566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) && 20839566d674SEric W. Biederman ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) { 20849566d674SEric W. Biederman return -EPERM; 20859566d674SEric W. Biederman } 20869566d674SEric W. Biederman 2087ff36fe2cSEric Paris err = security_sb_remount(sb, data); 2088ff36fe2cSEric Paris if (err) 2089ff36fe2cSEric Paris return err; 2090ff36fe2cSEric Paris 20911da177e4SLinus Torvalds down_write(&sb->s_umount); 20922e4b7fcdSDave Hansen if (flags & MS_BIND) 20932d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 209457eccb83SAl Viro else if (!capable(CAP_SYS_ADMIN)) 209557eccb83SAl Viro err = -EPERM; 20964aa98cf7SAl Viro else 20971da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 20987b43a79fSAl Viro if (!err) { 2099719ea2fbSAl Viro lock_mount_hash(); 2100a6138db8SEric W. Biederman mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 2101143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 2102143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2103719ea2fbSAl Viro unlock_mount_hash(); 21040e55a7ccSDan Williams } 21056339dab8SAl Viro up_write(&sb->s_umount); 21061da177e4SLinus Torvalds return err; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds 2109cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 21109676f0c6SRam Pai { 2111315fc83eSAl Viro struct mount *p; 2112909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2113fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 21149676f0c6SRam Pai return 1; 21159676f0c6SRam Pai } 21169676f0c6SRam Pai return 0; 21179676f0c6SRam Pai } 21189676f0c6SRam Pai 2119808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 21201da177e4SLinus Torvalds { 21212d92ab3cSAl Viro struct path old_path, parent_path; 2122676da58dSAl Viro struct mount *p; 21230fb54e50SAl Viro struct mount *old; 212484d17192SAl Viro struct mountpoint *mp; 212557eccb83SAl Viro int err; 21261da177e4SLinus Torvalds if (!old_name || !*old_name) 21271da177e4SLinus Torvalds return -EINVAL; 21282d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 21291da177e4SLinus Torvalds if (err) 21301da177e4SLinus Torvalds return err; 21311da177e4SLinus Torvalds 213284d17192SAl Viro mp = lock_mount(path); 213384d17192SAl Viro err = PTR_ERR(mp); 213484d17192SAl Viro if (IS_ERR(mp)) 2135cc53ce53SDavid Howells goto out; 2136cc53ce53SDavid Howells 2137143c8c91SAl Viro old = real_mount(old_path.mnt); 2138fc7be130SAl Viro p = real_mount(path->mnt); 2139143c8c91SAl Viro 21401da177e4SLinus Torvalds err = -EINVAL; 2141fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 21421da177e4SLinus Torvalds goto out1; 21431da177e4SLinus Torvalds 21445ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 21455ff9d8a6SEric W. Biederman goto out1; 21465ff9d8a6SEric W. Biederman 21471da177e4SLinus Torvalds err = -EINVAL; 21482d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 214921444403SRam Pai goto out1; 21501da177e4SLinus Torvalds 2151676da58dSAl Viro if (!mnt_has_parent(old)) 215221444403SRam Pai goto out1; 21531da177e4SLinus Torvalds 21542d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 21552d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 215621444403SRam Pai goto out1; 215721444403SRam Pai /* 215821444403SRam Pai * Don't move a mount residing in a shared parent. 215921444403SRam Pai */ 2160fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 216121444403SRam Pai goto out1; 21629676f0c6SRam Pai /* 21639676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 21649676f0c6SRam Pai * mount which is shared. 21659676f0c6SRam Pai */ 2166fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 21679676f0c6SRam Pai goto out1; 21681da177e4SLinus Torvalds err = -ELOOP; 2169fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2170676da58dSAl Viro if (p == old) 217121444403SRam Pai goto out1; 21721da177e4SLinus Torvalds 217384d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 21744ac91378SJan Blunck if (err) 217521444403SRam Pai goto out1; 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 21781da177e4SLinus Torvalds * automatically */ 21796776db3dSAl Viro list_del_init(&old->mnt_expire); 21801da177e4SLinus Torvalds out1: 218184d17192SAl Viro unlock_mount(mp); 21821da177e4SLinus Torvalds out: 21831da177e4SLinus Torvalds if (!err) 21841a390689SAl Viro path_put(&parent_path); 21852d92ab3cSAl Viro path_put(&old_path); 21861da177e4SLinus Torvalds return err; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds 21899d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 21909d412a43SAl Viro { 21919d412a43SAl Viro int err; 21929d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 21939d412a43SAl Viro if (subtype) { 21949d412a43SAl Viro subtype++; 21959d412a43SAl Viro err = -EINVAL; 21969d412a43SAl Viro if (!subtype[0]) 21979d412a43SAl Viro goto err; 21989d412a43SAl Viro } else 21999d412a43SAl Viro subtype = ""; 22009d412a43SAl Viro 22019d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 22029d412a43SAl Viro err = -ENOMEM; 22039d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 22049d412a43SAl Viro goto err; 22059d412a43SAl Viro return mnt; 22069d412a43SAl Viro 22079d412a43SAl Viro err: 22089d412a43SAl Viro mntput(mnt); 22099d412a43SAl Viro return ERR_PTR(err); 22109d412a43SAl Viro } 22119d412a43SAl Viro 22129d412a43SAl Viro /* 22139d412a43SAl Viro * add a mount into a namespace's mount tree 22149d412a43SAl Viro */ 221595bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 22169d412a43SAl Viro { 221784d17192SAl Viro struct mountpoint *mp; 221884d17192SAl Viro struct mount *parent; 22199d412a43SAl Viro int err; 22209d412a43SAl Viro 2221f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 22229d412a43SAl Viro 222384d17192SAl Viro mp = lock_mount(path); 222484d17192SAl Viro if (IS_ERR(mp)) 222584d17192SAl Viro return PTR_ERR(mp); 22269d412a43SAl Viro 222784d17192SAl Viro parent = real_mount(path->mnt); 22289d412a43SAl Viro err = -EINVAL; 222984d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2230156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2231156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 22329d412a43SAl Viro goto unlock; 2233156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 223484d17192SAl Viro if (!parent->mnt_ns) 2235156cacb1SAl Viro goto unlock; 2236156cacb1SAl Viro } 22379d412a43SAl Viro 22389d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 22399d412a43SAl Viro err = -EBUSY; 224095bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 22419d412a43SAl Viro path->mnt->mnt_root == path->dentry) 22429d412a43SAl Viro goto unlock; 22439d412a43SAl Viro 22449d412a43SAl Viro err = -EINVAL; 224595bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 22469d412a43SAl Viro goto unlock; 22479d412a43SAl Viro 224895bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 224984d17192SAl Viro err = graft_tree(newmnt, parent, mp); 22509d412a43SAl Viro 22519d412a43SAl Viro unlock: 225284d17192SAl Viro unlock_mount(mp); 22539d412a43SAl Viro return err; 22549d412a43SAl Viro } 2255b1e75df4SAl Viro 22561da177e4SLinus Torvalds /* 22571da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 22581da177e4SLinus Torvalds * namespace's tree 22591da177e4SLinus Torvalds */ 22600c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags, 2261808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 22621da177e4SLinus Torvalds { 22630c55cfc4SEric W. Biederman struct file_system_type *type; 22649b40bc90SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 22651da177e4SLinus Torvalds struct vfsmount *mnt; 226615f9a3f3SAl Viro int err; 22671da177e4SLinus Torvalds 22680c55cfc4SEric W. Biederman if (!fstype) 22691da177e4SLinus Torvalds return -EINVAL; 22701da177e4SLinus Torvalds 22710c55cfc4SEric W. Biederman type = get_fs_type(fstype); 22720c55cfc4SEric W. Biederman if (!type) 22730c55cfc4SEric W. Biederman return -ENODEV; 22740c55cfc4SEric W. Biederman 22750c55cfc4SEric W. Biederman if (user_ns != &init_user_ns) { 22760c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_MOUNT)) { 22770c55cfc4SEric W. Biederman put_filesystem(type); 22780c55cfc4SEric W. Biederman return -EPERM; 22790c55cfc4SEric W. Biederman } 22800c55cfc4SEric W. Biederman /* Only in special cases allow devices from mounts 22810c55cfc4SEric W. Biederman * created outside the initial user namespace. 22820c55cfc4SEric W. Biederman */ 22830c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) { 22840c55cfc4SEric W. Biederman flags |= MS_NODEV; 22859566d674SEric W. Biederman mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV; 22860c55cfc4SEric W. Biederman } 22870c55cfc4SEric W. Biederman } 22880c55cfc4SEric W. Biederman 22890c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, flags, name, data); 22900c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 22910c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 22920c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 22930c55cfc4SEric W. Biederman 22940c55cfc4SEric W. Biederman put_filesystem(type); 22951da177e4SLinus Torvalds if (IS_ERR(mnt)) 22961da177e4SLinus Torvalds return PTR_ERR(mnt); 22971da177e4SLinus Torvalds 229895bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 229915f9a3f3SAl Viro if (err) 230015f9a3f3SAl Viro mntput(mnt); 230115f9a3f3SAl Viro return err; 23021da177e4SLinus Torvalds } 23031da177e4SLinus Torvalds 230419a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 230519a167afSAl Viro { 23066776db3dSAl Viro struct mount *mnt = real_mount(m); 230719a167afSAl Viro int err; 230819a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 230919a167afSAl Viro * expired before we get a chance to add it 231019a167afSAl Viro */ 23116776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 231219a167afSAl Viro 231319a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 231419a167afSAl Viro m->mnt_root == path->dentry) { 2315b1e75df4SAl Viro err = -ELOOP; 2316b1e75df4SAl Viro goto fail; 231719a167afSAl Viro } 231819a167afSAl Viro 231995bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2320b1e75df4SAl Viro if (!err) 2321b1e75df4SAl Viro return 0; 2322b1e75df4SAl Viro fail: 2323b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 23246776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 232597216be0SAl Viro namespace_lock(); 23266776db3dSAl Viro list_del_init(&mnt->mnt_expire); 232797216be0SAl Viro namespace_unlock(); 232819a167afSAl Viro } 2329b1e75df4SAl Viro mntput(m); 2330b1e75df4SAl Viro mntput(m); 233119a167afSAl Viro return err; 233219a167afSAl Viro } 233319a167afSAl Viro 2334ea5b778aSDavid Howells /** 2335ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2336ea5b778aSDavid Howells * @mnt: The mount to list. 2337ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2338ea5b778aSDavid Howells */ 2339ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2340ea5b778aSDavid Howells { 234197216be0SAl Viro namespace_lock(); 2342ea5b778aSDavid Howells 23436776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2344ea5b778aSDavid Howells 234597216be0SAl Viro namespace_unlock(); 2346ea5b778aSDavid Howells } 2347ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2348ea5b778aSDavid Howells 2349ea5b778aSDavid Howells /* 23501da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 23511da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 23521da177e4SLinus Torvalds * here 23531da177e4SLinus Torvalds */ 23541da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 23551da177e4SLinus Torvalds { 2356761d5c38SAl Viro struct mount *mnt, *next; 23571da177e4SLinus Torvalds LIST_HEAD(graveyard); 23581da177e4SLinus Torvalds 23591da177e4SLinus Torvalds if (list_empty(mounts)) 23601da177e4SLinus Torvalds return; 23611da177e4SLinus Torvalds 236297216be0SAl Viro namespace_lock(); 2363719ea2fbSAl Viro lock_mount_hash(); 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 23661da177e4SLinus Torvalds * following criteria: 23671da177e4SLinus Torvalds * - only referenced by its parent vfsmount 23681da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 23691da177e4SLinus Torvalds * cleared by mntput()) 23701da177e4SLinus Torvalds */ 23716776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2372863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 23731ab59738SAl Viro propagate_mount_busy(mnt, 1)) 23741da177e4SLinus Torvalds continue; 23756776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 23761da177e4SLinus Torvalds } 2377bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 23786776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2379143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2380328e6d90SAl Viro umount_tree(mnt, 1); 2381bcc5c7d2SAl Viro } 2382719ea2fbSAl Viro unlock_mount_hash(); 23833ab6abeeSAl Viro namespace_unlock(); 23841da177e4SLinus Torvalds } 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 23871da177e4SLinus Torvalds 23881da177e4SLinus Torvalds /* 23895528f911STrond Myklebust * Ripoff of 'select_parent()' 23905528f911STrond Myklebust * 23915528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 23925528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 23935528f911STrond Myklebust */ 2394692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 23955528f911STrond Myklebust { 2396692afc31SAl Viro struct mount *this_parent = parent; 23975528f911STrond Myklebust struct list_head *next; 23985528f911STrond Myklebust int found = 0; 23995528f911STrond Myklebust 24005528f911STrond Myklebust repeat: 24016b41d536SAl Viro next = this_parent->mnt_mounts.next; 24025528f911STrond Myklebust resume: 24036b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 24045528f911STrond Myklebust struct list_head *tmp = next; 24056b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 24065528f911STrond Myklebust 24075528f911STrond Myklebust next = tmp->next; 2408692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 24095528f911STrond Myklebust continue; 24105528f911STrond Myklebust /* 24115528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 24125528f911STrond Myklebust */ 24136b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 24145528f911STrond Myklebust this_parent = mnt; 24155528f911STrond Myklebust goto repeat; 24165528f911STrond Myklebust } 24175528f911STrond Myklebust 24181ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 24196776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 24205528f911STrond Myklebust found++; 24215528f911STrond Myklebust } 24225528f911STrond Myklebust } 24235528f911STrond Myklebust /* 24245528f911STrond Myklebust * All done at this level ... ascend and resume the search 24255528f911STrond Myklebust */ 24265528f911STrond Myklebust if (this_parent != parent) { 24276b41d536SAl Viro next = this_parent->mnt_child.next; 24280714a533SAl Viro this_parent = this_parent->mnt_parent; 24295528f911STrond Myklebust goto resume; 24305528f911STrond Myklebust } 24315528f911STrond Myklebust return found; 24325528f911STrond Myklebust } 24335528f911STrond Myklebust 24345528f911STrond Myklebust /* 24355528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 24365528f911STrond Myklebust * submounts of a specific parent mountpoint 243799b7db7bSNick Piggin * 243848a066e7SAl Viro * mount_lock must be held for write 24395528f911STrond Myklebust */ 2440b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 24415528f911STrond Myklebust { 24425528f911STrond Myklebust LIST_HEAD(graveyard); 2443761d5c38SAl Viro struct mount *m; 24445528f911STrond Myklebust 24455528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2446c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2447bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2448761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 24496776db3dSAl Viro mnt_expire); 2450143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2451328e6d90SAl Viro umount_tree(m, 1); 2452bcc5c7d2SAl Viro } 2453bcc5c7d2SAl Viro } 24545528f911STrond Myklebust } 24555528f911STrond Myklebust 24565528f911STrond Myklebust /* 24571da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 24581da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 24591da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 24601da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 24611da177e4SLinus Torvalds */ 2462b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2463b58fed8bSRam Pai unsigned long n) 24641da177e4SLinus Torvalds { 24651da177e4SLinus Torvalds char *t = to; 24661da177e4SLinus Torvalds const char __user *f = from; 24671da177e4SLinus Torvalds char c; 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 24701da177e4SLinus Torvalds return n; 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds while (n) { 24731da177e4SLinus Torvalds if (__get_user(c, f)) { 24741da177e4SLinus Torvalds memset(t, 0, n); 24751da177e4SLinus Torvalds break; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds *t++ = c; 24781da177e4SLinus Torvalds f++; 24791da177e4SLinus Torvalds n--; 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds return n; 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds 24841da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 24851da177e4SLinus Torvalds { 24861da177e4SLinus Torvalds int i; 24871da177e4SLinus Torvalds unsigned long page; 24881da177e4SLinus Torvalds unsigned long size; 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds *where = 0; 24911da177e4SLinus Torvalds if (!data) 24921da177e4SLinus Torvalds return 0; 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 24951da177e4SLinus Torvalds return -ENOMEM; 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds /* We only care that *some* data at the address the user 24981da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 24991da177e4SLinus Torvalds * the remainder of the page. 25001da177e4SLinus Torvalds */ 25011da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 25021da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 25031da177e4SLinus Torvalds if (size > PAGE_SIZE) 25041da177e4SLinus Torvalds size = PAGE_SIZE; 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 25071da177e4SLinus Torvalds if (!i) { 25081da177e4SLinus Torvalds free_page(page); 25091da177e4SLinus Torvalds return -EFAULT; 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds if (i != PAGE_SIZE) 25121da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 25131da177e4SLinus Torvalds *where = page; 25141da177e4SLinus Torvalds return 0; 25151da177e4SLinus Torvalds } 25161da177e4SLinus Torvalds 2517eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2518eca6f534SVegard Nossum { 2519eca6f534SVegard Nossum char *tmp; 2520eca6f534SVegard Nossum 2521eca6f534SVegard Nossum if (!data) { 2522eca6f534SVegard Nossum *where = NULL; 2523eca6f534SVegard Nossum return 0; 2524eca6f534SVegard Nossum } 2525eca6f534SVegard Nossum 2526eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2527eca6f534SVegard Nossum if (IS_ERR(tmp)) 2528eca6f534SVegard Nossum return PTR_ERR(tmp); 2529eca6f534SVegard Nossum 2530eca6f534SVegard Nossum *where = tmp; 2531eca6f534SVegard Nossum return 0; 2532eca6f534SVegard Nossum } 2533eca6f534SVegard Nossum 25341da177e4SLinus Torvalds /* 25351da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 25361da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 25371da177e4SLinus Torvalds * 25381da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 25391da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 25401da177e4SLinus Torvalds * information (or be NULL). 25411da177e4SLinus Torvalds * 25421da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 25431da177e4SLinus Torvalds * When the flags word was introduced its top half was required 25441da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 25451da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 25461da177e4SLinus Torvalds * and must be discarded. 25471da177e4SLinus Torvalds */ 2548808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name, 2549808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 25501da177e4SLinus Torvalds { 25512d92ab3cSAl Viro struct path path; 25521da177e4SLinus Torvalds int retval = 0; 25531da177e4SLinus Torvalds int mnt_flags = 0; 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds /* Discard magic */ 25561da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 25571da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds /* Basic sanity checks */ 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 25621da177e4SLinus Torvalds return -EINVAL; 25631da177e4SLinus Torvalds 25641da177e4SLinus Torvalds if (data_page) 25651da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 25661da177e4SLinus Torvalds 2567a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2568a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2569a27ab9f2STetsuo Handa if (retval) 2570a27ab9f2STetsuo Handa return retval; 2571a27ab9f2STetsuo Handa 2572a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2573a27ab9f2STetsuo Handa type_page, flags, data_page); 25740d5cadb8SAl Viro if (!retval && !may_mount()) 25750d5cadb8SAl Viro retval = -EPERM; 2576a27ab9f2STetsuo Handa if (retval) 2577a27ab9f2STetsuo Handa goto dput_out; 2578a27ab9f2STetsuo Handa 2579613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2580613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 25810a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 25820a1c01c9SMatthew Garrett 25831da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 25841da177e4SLinus Torvalds if (flags & MS_NOSUID) 25851da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 25861da177e4SLinus Torvalds if (flags & MS_NODEV) 25871da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 25881da177e4SLinus Torvalds if (flags & MS_NOEXEC) 25891da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2590fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2591fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2592fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2593fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2594d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2595d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 25962e4b7fcdSDave Hansen if (flags & MS_RDONLY) 25972e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2598fc33a7bbSChristoph Hellwig 2599ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 2600ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 2601ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 2602ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 2603ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 2604ffbc6f0eSEric W. Biederman mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; 2605ffbc6f0eSEric W. Biederman } 2606ffbc6f0eSEric W. Biederman 26077a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2608d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2609d0adde57SMatthew Garrett MS_STRICTATIME); 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds if (flags & MS_REMOUNT) 26122d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 26131da177e4SLinus Torvalds data_page); 26141da177e4SLinus Torvalds else if (flags & MS_BIND) 26152d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 26169676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 26172d92ab3cSAl Viro retval = do_change_type(&path, flags); 26181da177e4SLinus Torvalds else if (flags & MS_MOVE) 26192d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 26201da177e4SLinus Torvalds else 26212d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 26221da177e4SLinus Torvalds dev_name, data_page); 26231da177e4SLinus Torvalds dput_out: 26242d92ab3cSAl Viro path_put(&path); 26251da177e4SLinus Torvalds return retval; 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds 2628771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2629771b1371SEric W. Biederman { 263098f842e6SEric W. Biederman proc_free_inum(ns->proc_inum); 2631771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2632771b1371SEric W. Biederman kfree(ns); 2633771b1371SEric W. Biederman } 2634771b1371SEric W. Biederman 26358823c079SEric W. Biederman /* 26368823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 26378823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 26388823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 26398823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 26408823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 26418823c079SEric W. Biederman */ 26428823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 26438823c079SEric W. Biederman 2644771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2645cf8d2c11STrond Myklebust { 2646cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 264798f842e6SEric W. Biederman int ret; 2648cf8d2c11STrond Myklebust 2649cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2650cf8d2c11STrond Myklebust if (!new_ns) 2651cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 265298f842e6SEric W. Biederman ret = proc_alloc_inum(&new_ns->proc_inum); 265398f842e6SEric W. Biederman if (ret) { 265498f842e6SEric W. Biederman kfree(new_ns); 265598f842e6SEric W. Biederman return ERR_PTR(ret); 265698f842e6SEric W. Biederman } 26578823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2658cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2659cf8d2c11STrond Myklebust new_ns->root = NULL; 2660cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2661cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2662cf8d2c11STrond Myklebust new_ns->event = 0; 2663771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2664cf8d2c11STrond Myklebust return new_ns; 2665cf8d2c11STrond Myklebust } 2666cf8d2c11STrond Myklebust 26679559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 26689559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 26691da177e4SLinus Torvalds { 26706b3286edSKirill Korotaev struct mnt_namespace *new_ns; 26717f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2672315fc83eSAl Viro struct mount *p, *q; 26739559f689SAl Viro struct mount *old; 2674cb338d06SAl Viro struct mount *new; 26757a472ef4SEric W. Biederman int copy_flags; 26761da177e4SLinus Torvalds 26779559f689SAl Viro BUG_ON(!ns); 26789559f689SAl Viro 26799559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 26809559f689SAl Viro get_mnt_ns(ns); 26819559f689SAl Viro return ns; 26829559f689SAl Viro } 26839559f689SAl Viro 26849559f689SAl Viro old = ns->root; 26859559f689SAl Viro 2686771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2687cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2688cf8d2c11STrond Myklebust return new_ns; 26891da177e4SLinus Torvalds 269097216be0SAl Viro namespace_lock(); 26911da177e4SLinus Torvalds /* First pass: copy the tree topology */ 26924ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 26939559f689SAl Viro if (user_ns != ns->user_ns) 2694132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 26957a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2696be34d1a3SDavid Howells if (IS_ERR(new)) { 2697328e6d90SAl Viro namespace_unlock(); 2698771b1371SEric W. Biederman free_mnt_ns(new_ns); 2699be34d1a3SDavid Howells return ERR_CAST(new); 27001da177e4SLinus Torvalds } 2701be08d6d2SAl Viro new_ns->root = new; 27021a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds /* 27051da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 27061da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 27071da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 27081da177e4SLinus Torvalds */ 2709909b0a88SAl Viro p = old; 2710cb338d06SAl Viro q = new; 27111da177e4SLinus Torvalds while (p) { 2712143c8c91SAl Viro q->mnt_ns = new_ns; 27139559f689SAl Viro if (new_fs) { 27149559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 27159559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2716315fc83eSAl Viro rootmnt = &p->mnt; 27171da177e4SLinus Torvalds } 27189559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 27199559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2720315fc83eSAl Viro pwdmnt = &p->mnt; 27211da177e4SLinus Torvalds } 27221da177e4SLinus Torvalds } 2723909b0a88SAl Viro p = next_mnt(p, old); 2724909b0a88SAl Viro q = next_mnt(q, new); 27254ce5d2b1SEric W. Biederman if (!q) 27264ce5d2b1SEric W. Biederman break; 27274ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 27284ce5d2b1SEric W. Biederman p = next_mnt(p, old); 27291da177e4SLinus Torvalds } 2730328e6d90SAl Viro namespace_unlock(); 27311da177e4SLinus Torvalds 27321da177e4SLinus Torvalds if (rootmnt) 2733f03c6599SAl Viro mntput(rootmnt); 27341da177e4SLinus Torvalds if (pwdmnt) 2735f03c6599SAl Viro mntput(pwdmnt); 27361da177e4SLinus Torvalds 2737741a2951SJANAK DESAI return new_ns; 2738741a2951SJANAK DESAI } 2739741a2951SJANAK DESAI 2740cf8d2c11STrond Myklebust /** 2741cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2742cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2743cf8d2c11STrond Myklebust */ 27441a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2745cf8d2c11STrond Myklebust { 2746771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2747cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 27481a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 27491a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2750be08d6d2SAl Viro new_ns->root = mnt; 2751b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 2752c1334495SAl Viro } else { 27531a4eeaf2SAl Viro mntput(m); 2754cf8d2c11STrond Myklebust } 2755cf8d2c11STrond Myklebust return new_ns; 2756cf8d2c11STrond Myklebust } 2757cf8d2c11STrond Myklebust 2758ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2759ea441d11SAl Viro { 2760ea441d11SAl Viro struct mnt_namespace *ns; 2761d31da0f0SAl Viro struct super_block *s; 2762ea441d11SAl Viro struct path path; 2763ea441d11SAl Viro int err; 2764ea441d11SAl Viro 2765ea441d11SAl Viro ns = create_mnt_ns(mnt); 2766ea441d11SAl Viro if (IS_ERR(ns)) 2767ea441d11SAl Viro return ERR_CAST(ns); 2768ea441d11SAl Viro 2769ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2770ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2771ea441d11SAl Viro 2772ea441d11SAl Viro put_mnt_ns(ns); 2773ea441d11SAl Viro 2774ea441d11SAl Viro if (err) 2775ea441d11SAl Viro return ERR_PTR(err); 2776ea441d11SAl Viro 2777ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2778d31da0f0SAl Viro s = path.mnt->mnt_sb; 2779d31da0f0SAl Viro atomic_inc(&s->s_active); 2780ea441d11SAl Viro mntput(path.mnt); 2781ea441d11SAl Viro /* lock the sucker */ 2782d31da0f0SAl Viro down_write(&s->s_umount); 2783ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2784ea441d11SAl Viro return path.dentry; 2785ea441d11SAl Viro } 2786ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2787ea441d11SAl Viro 2788bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2789bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 27901da177e4SLinus Torvalds { 2791eca6f534SVegard Nossum int ret; 2792eca6f534SVegard Nossum char *kernel_type; 279391a27b2aSJeff Layton struct filename *kernel_dir; 2794eca6f534SVegard Nossum char *kernel_dev; 27951da177e4SLinus Torvalds unsigned long data_page; 27961da177e4SLinus Torvalds 2797eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2798eca6f534SVegard Nossum if (ret < 0) 2799eca6f534SVegard Nossum goto out_type; 28001da177e4SLinus Torvalds 2801eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2802eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2803eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2804eca6f534SVegard Nossum goto out_dir; 2805eca6f534SVegard Nossum } 28061da177e4SLinus Torvalds 2807eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2808eca6f534SVegard Nossum if (ret < 0) 2809eca6f534SVegard Nossum goto out_dev; 28101da177e4SLinus Torvalds 2811eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2812eca6f534SVegard Nossum if (ret < 0) 2813eca6f534SVegard Nossum goto out_data; 28141da177e4SLinus Torvalds 281591a27b2aSJeff Layton ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags, 2816eca6f534SVegard Nossum (void *) data_page); 2817eca6f534SVegard Nossum 28181da177e4SLinus Torvalds free_page(data_page); 2819eca6f534SVegard Nossum out_data: 2820eca6f534SVegard Nossum kfree(kernel_dev); 2821eca6f534SVegard Nossum out_dev: 2822eca6f534SVegard Nossum putname(kernel_dir); 2823eca6f534SVegard Nossum out_dir: 2824eca6f534SVegard Nossum kfree(kernel_type); 2825eca6f534SVegard Nossum out_type: 2826eca6f534SVegard Nossum return ret; 28271da177e4SLinus Torvalds } 28281da177e4SLinus Torvalds 28291da177e4SLinus Torvalds /* 2830afac7cbaSAl Viro * Return true if path is reachable from root 2831afac7cbaSAl Viro * 283248a066e7SAl Viro * namespace_sem or mount_lock is held 2833afac7cbaSAl Viro */ 2834643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2835afac7cbaSAl Viro const struct path *root) 2836afac7cbaSAl Viro { 2837643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2838a73324daSAl Viro dentry = mnt->mnt_mountpoint; 28390714a533SAl Viro mnt = mnt->mnt_parent; 2840afac7cbaSAl Viro } 2841643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2842afac7cbaSAl Viro } 2843afac7cbaSAl Viro 2844afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2845afac7cbaSAl Viro { 2846afac7cbaSAl Viro int res; 284748a066e7SAl Viro read_seqlock_excl(&mount_lock); 2848643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 284948a066e7SAl Viro read_sequnlock_excl(&mount_lock); 2850afac7cbaSAl Viro return res; 2851afac7cbaSAl Viro } 2852afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2853afac7cbaSAl Viro 2854afac7cbaSAl Viro /* 28551da177e4SLinus Torvalds * pivot_root Semantics: 28561da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 28571da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 28581da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 28591da177e4SLinus Torvalds * 28601da177e4SLinus Torvalds * Restrictions: 28611da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 28621da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 28631da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 28641da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 28651da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 28661da177e4SLinus Torvalds * 28674a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 28684a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 28694a0d11faSNeil Brown * in this situation. 28704a0d11faSNeil Brown * 28711da177e4SLinus Torvalds * Notes: 28721da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 28731da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 28741da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 28751da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 28761da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 28771da177e4SLinus Torvalds * first. 28781da177e4SLinus Torvalds */ 28793480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 28803480b257SHeiko Carstens const char __user *, put_old) 28811da177e4SLinus Torvalds { 28822d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 288384d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 288484d17192SAl Viro struct mountpoint *old_mp, *root_mp; 28851da177e4SLinus Torvalds int error; 28861da177e4SLinus Torvalds 28879b40bc90SAl Viro if (!may_mount()) 28881da177e4SLinus Torvalds return -EPERM; 28891da177e4SLinus Torvalds 28902d8f3038SAl Viro error = user_path_dir(new_root, &new); 28911da177e4SLinus Torvalds if (error) 28921da177e4SLinus Torvalds goto out0; 28931da177e4SLinus Torvalds 28942d8f3038SAl Viro error = user_path_dir(put_old, &old); 28951da177e4SLinus Torvalds if (error) 28961da177e4SLinus Torvalds goto out1; 28971da177e4SLinus Torvalds 28982d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2899b12cea91SAl Viro if (error) 2900b12cea91SAl Viro goto out2; 29011da177e4SLinus Torvalds 2902f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 290384d17192SAl Viro old_mp = lock_mount(&old); 290484d17192SAl Viro error = PTR_ERR(old_mp); 290584d17192SAl Viro if (IS_ERR(old_mp)) 2906b12cea91SAl Viro goto out3; 2907b12cea91SAl Viro 29081da177e4SLinus Torvalds error = -EINVAL; 2909419148daSAl Viro new_mnt = real_mount(new.mnt); 2910419148daSAl Viro root_mnt = real_mount(root.mnt); 291184d17192SAl Viro old_mnt = real_mount(old.mnt); 291284d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 2913fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2914fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2915b12cea91SAl Viro goto out4; 2916143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2917b12cea91SAl Viro goto out4; 29185ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 29195ff9d8a6SEric W. Biederman goto out4; 29201da177e4SLinus Torvalds error = -ENOENT; 2921f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2922b12cea91SAl Viro goto out4; 29231da177e4SLinus Torvalds error = -EBUSY; 292484d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 2925b12cea91SAl Viro goto out4; /* loop, on the same file system */ 29261da177e4SLinus Torvalds error = -EINVAL; 29278c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2928b12cea91SAl Viro goto out4; /* not a mountpoint */ 2929676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2930b12cea91SAl Viro goto out4; /* not attached */ 293184d17192SAl Viro root_mp = root_mnt->mnt_mp; 29322d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2933b12cea91SAl Viro goto out4; /* not a mountpoint */ 2934676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2935b12cea91SAl Viro goto out4; /* not attached */ 29364ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 293784d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 2938b12cea91SAl Viro goto out4; 293984d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 2940719ea2fbSAl Viro lock_mount_hash(); 2941419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2942419148daSAl Viro detach_mnt(root_mnt, &root_parent); 29435ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 29445ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 29455ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 29465ff9d8a6SEric W. Biederman } 29474ac91378SJan Blunck /* mount old root on put_old */ 294884d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 29494ac91378SJan Blunck /* mount new_root on / */ 295084d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 29516b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2952719ea2fbSAl Viro unlock_mount_hash(); 29532d8f3038SAl Viro chroot_fs_refs(&root, &new); 295484d17192SAl Viro put_mountpoint(root_mp); 29551da177e4SLinus Torvalds error = 0; 2956b12cea91SAl Viro out4: 295784d17192SAl Viro unlock_mount(old_mp); 2958b12cea91SAl Viro if (!error) { 29591a390689SAl Viro path_put(&root_parent); 29601a390689SAl Viro path_put(&parent_path); 2961b12cea91SAl Viro } 2962b12cea91SAl Viro out3: 29638c3ee42eSAl Viro path_put(&root); 2964b12cea91SAl Viro out2: 29652d8f3038SAl Viro path_put(&old); 29661da177e4SLinus Torvalds out1: 29672d8f3038SAl Viro path_put(&new); 29681da177e4SLinus Torvalds out0: 29691da177e4SLinus Torvalds return error; 29701da177e4SLinus Torvalds } 29711da177e4SLinus Torvalds 29721da177e4SLinus Torvalds static void __init init_mount_tree(void) 29731da177e4SLinus Torvalds { 29741da177e4SLinus Torvalds struct vfsmount *mnt; 29756b3286edSKirill Korotaev struct mnt_namespace *ns; 2976ac748a09SJan Blunck struct path root; 29770c55cfc4SEric W. Biederman struct file_system_type *type; 29781da177e4SLinus Torvalds 29790c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 29800c55cfc4SEric W. Biederman if (!type) 29810c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 29820c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 29830c55cfc4SEric W. Biederman put_filesystem(type); 29841da177e4SLinus Torvalds if (IS_ERR(mnt)) 29851da177e4SLinus Torvalds panic("Can't create rootfs"); 2986b3e19d92SNick Piggin 29873b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 29883b22edc5STrond Myklebust if (IS_ERR(ns)) 29891da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 29901da177e4SLinus Torvalds 29916b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 29926b3286edSKirill Korotaev get_mnt_ns(ns); 29931da177e4SLinus Torvalds 2994be08d6d2SAl Viro root.mnt = mnt; 2995be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2996ac748a09SJan Blunck 2997ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2998ac748a09SJan Blunck set_fs_root(current->fs, &root); 29991da177e4SLinus Torvalds } 30001da177e4SLinus Torvalds 300174bf17cfSDenis Cheng void __init mnt_init(void) 30021da177e4SLinus Torvalds { 300313f14b4dSEric Dumazet unsigned u; 300415a67dd8SRandy Dunlap int err; 30051da177e4SLinus Torvalds 30067d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 300720c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 30081da177e4SLinus Torvalds 30090818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 301038129a13SAl Viro sizeof(struct hlist_head), 30110818bf27SAl Viro mhash_entries, 19, 30120818bf27SAl Viro 0, 30130818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 30140818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 30150818bf27SAl Viro sizeof(struct hlist_head), 30160818bf27SAl Viro mphash_entries, 19, 30170818bf27SAl Viro 0, 30180818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 30191da177e4SLinus Torvalds 302084d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 30211da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 30221da177e4SLinus Torvalds 30230818bf27SAl Viro for (u = 0; u <= m_hash_mask; u++) 302438129a13SAl Viro INIT_HLIST_HEAD(&mount_hashtable[u]); 30250818bf27SAl Viro for (u = 0; u <= mp_hash_mask; u++) 30260818bf27SAl Viro INIT_HLIST_HEAD(&mountpoint_hashtable[u]); 30271da177e4SLinus Torvalds 30284b93dc9bSTejun Heo kernfs_init(); 30294b93dc9bSTejun Heo 303015a67dd8SRandy Dunlap err = sysfs_init(); 303115a67dd8SRandy Dunlap if (err) 303215a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 30338e24eea7SHarvey Harrison __func__, err); 303400d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 303500d26666SGreg Kroah-Hartman if (!fs_kobj) 30368e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 30371da177e4SLinus Torvalds init_rootfs(); 30381da177e4SLinus Torvalds init_mount_tree(); 30391da177e4SLinus Torvalds } 30401da177e4SLinus Torvalds 3041616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 30421da177e4SLinus Torvalds { 3043d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 3044616511d0STrond Myklebust return; 30457b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3046771b1371SEric W. Biederman free_mnt_ns(ns); 30471da177e4SLinus Torvalds } 30489d412a43SAl Viro 30499d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 30509d412a43SAl Viro { 3051423e0ab0STim Chen struct vfsmount *mnt; 3052423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 3053423e0ab0STim Chen if (!IS_ERR(mnt)) { 3054423e0ab0STim Chen /* 3055423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3056423e0ab0STim Chen * we unmount before file sys is unregistered 3057423e0ab0STim Chen */ 3058f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3059423e0ab0STim Chen } 3060423e0ab0STim Chen return mnt; 30619d412a43SAl Viro } 30629d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 3063423e0ab0STim Chen 3064423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3065423e0ab0STim Chen { 3066423e0ab0STim Chen /* release long term mount so mount point can be released */ 3067423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3068f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 306948a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3070423e0ab0STim Chen mntput(mnt); 3071423e0ab0STim Chen } 3072423e0ab0STim Chen } 3073423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 307402125a82SAl Viro 307502125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 307602125a82SAl Viro { 3077143c8c91SAl Viro return check_mnt(real_mount(mnt)); 307802125a82SAl Viro } 30798823c079SEric W. Biederman 30803151527eSEric W. Biederman bool current_chrooted(void) 30813151527eSEric W. Biederman { 30823151527eSEric W. Biederman /* Does the current process have a non-standard root */ 30833151527eSEric W. Biederman struct path ns_root; 30843151527eSEric W. Biederman struct path fs_root; 30853151527eSEric W. Biederman bool chrooted; 30863151527eSEric W. Biederman 30873151527eSEric W. Biederman /* Find the namespace root */ 30883151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 30893151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 30903151527eSEric W. Biederman path_get(&ns_root); 30913151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 30923151527eSEric W. Biederman ; 30933151527eSEric W. Biederman 30943151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 30953151527eSEric W. Biederman 30963151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 30973151527eSEric W. Biederman 30983151527eSEric W. Biederman path_put(&fs_root); 30993151527eSEric W. Biederman path_put(&ns_root); 31003151527eSEric W. Biederman 31013151527eSEric W. Biederman return chrooted; 31023151527eSEric W. Biederman } 31033151527eSEric W. Biederman 3104e51db735SEric W. Biederman bool fs_fully_visible(struct file_system_type *type) 310587a8ebd6SEric W. Biederman { 310687a8ebd6SEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 310787a8ebd6SEric W. Biederman struct mount *mnt; 3108e51db735SEric W. Biederman bool visible = false; 310987a8ebd6SEric W. Biederman 3110e51db735SEric W. Biederman if (unlikely(!ns)) 3111e51db735SEric W. Biederman return false; 3112e51db735SEric W. Biederman 311344bb4385SAl Viro down_read(&namespace_sem); 311487a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3115e51db735SEric W. Biederman struct mount *child; 3116e51db735SEric W. Biederman if (mnt->mnt.mnt_sb->s_type != type) 3117e51db735SEric W. Biederman continue; 3118e51db735SEric W. Biederman 3119e51db735SEric W. Biederman /* This mount is not fully visible if there are any child mounts 3120e51db735SEric W. Biederman * that cover anything except for empty directories. 3121e51db735SEric W. Biederman */ 3122e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3123e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3124e51db735SEric W. Biederman if (!S_ISDIR(inode->i_mode)) 3125e51db735SEric W. Biederman goto next; 312641301ae7SEric W. Biederman if (inode->i_nlink > 2) 3127e51db735SEric W. Biederman goto next; 312887a8ebd6SEric W. Biederman } 3129e51db735SEric W. Biederman visible = true; 3130e51db735SEric W. Biederman goto found; 3131e51db735SEric W. Biederman next: ; 313287a8ebd6SEric W. Biederman } 3133e51db735SEric W. Biederman found: 313444bb4385SAl Viro up_read(&namespace_sem); 3135e51db735SEric W. Biederman return visible; 313687a8ebd6SEric W. Biederman } 313787a8ebd6SEric W. Biederman 31388823c079SEric W. Biederman static void *mntns_get(struct task_struct *task) 31398823c079SEric W. Biederman { 31408823c079SEric W. Biederman struct mnt_namespace *ns = NULL; 31418823c079SEric W. Biederman struct nsproxy *nsproxy; 31428823c079SEric W. Biederman 3143728dba3aSEric W. Biederman task_lock(task); 3144728dba3aSEric W. Biederman nsproxy = task->nsproxy; 31458823c079SEric W. Biederman if (nsproxy) { 31468823c079SEric W. Biederman ns = nsproxy->mnt_ns; 31478823c079SEric W. Biederman get_mnt_ns(ns); 31488823c079SEric W. Biederman } 3149728dba3aSEric W. Biederman task_unlock(task); 31508823c079SEric W. Biederman 31518823c079SEric W. Biederman return ns; 31528823c079SEric W. Biederman } 31538823c079SEric W. Biederman 31548823c079SEric W. Biederman static void mntns_put(void *ns) 31558823c079SEric W. Biederman { 31568823c079SEric W. Biederman put_mnt_ns(ns); 31578823c079SEric W. Biederman } 31588823c079SEric W. Biederman 31598823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns) 31608823c079SEric W. Biederman { 31618823c079SEric W. Biederman struct fs_struct *fs = current->fs; 31628823c079SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 31638823c079SEric W. Biederman struct path root; 31648823c079SEric W. Biederman 31650c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3166c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3167c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3168ae11e0f1SZhao Hongjiang return -EPERM; 31698823c079SEric W. Biederman 31708823c079SEric W. Biederman if (fs->users != 1) 31718823c079SEric W. Biederman return -EINVAL; 31728823c079SEric W. Biederman 31738823c079SEric W. Biederman get_mnt_ns(mnt_ns); 31748823c079SEric W. Biederman put_mnt_ns(nsproxy->mnt_ns); 31758823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 31768823c079SEric W. Biederman 31778823c079SEric W. Biederman /* Find the root */ 31788823c079SEric W. Biederman root.mnt = &mnt_ns->root->mnt; 31798823c079SEric W. Biederman root.dentry = mnt_ns->root->mnt.mnt_root; 31808823c079SEric W. Biederman path_get(&root); 31818823c079SEric W. Biederman while(d_mountpoint(root.dentry) && follow_down_one(&root)) 31828823c079SEric W. Biederman ; 31838823c079SEric W. Biederman 31848823c079SEric W. Biederman /* Update the pwd and root */ 31858823c079SEric W. Biederman set_fs_pwd(fs, &root); 31868823c079SEric W. Biederman set_fs_root(fs, &root); 31878823c079SEric W. Biederman 31888823c079SEric W. Biederman path_put(&root); 31898823c079SEric W. Biederman return 0; 31908823c079SEric W. Biederman } 31918823c079SEric W. Biederman 319298f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns) 319398f842e6SEric W. Biederman { 319498f842e6SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 319598f842e6SEric W. Biederman return mnt_ns->proc_inum; 319698f842e6SEric W. Biederman } 319798f842e6SEric W. Biederman 31988823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 31998823c079SEric W. Biederman .name = "mnt", 32008823c079SEric W. Biederman .type = CLONE_NEWNS, 32018823c079SEric W. Biederman .get = mntns_get, 32028823c079SEric W. Biederman .put = mntns_put, 32038823c079SEric W. Biederman .install = mntns_install, 320498f842e6SEric W. Biederman .inum = mntns_inum, 32058823c079SEric W. Biederman }; 3206