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> 19d10577a8SAl Viro #include <linux/acct.h> /* acct_auto_close_mnt */ 2057f150a5SRob Landley #include <linux/init.h> /* init_rootfs */ 21d10577a8SAl Viro #include <linux/fs_struct.h> /* get_fs_root et.al. */ 22d10577a8SAl Viro #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */ 23d10577a8SAl Viro #include <linux/uaccess.h> 240bb80f24SDavid Howells #include <linux/proc_ns.h> 2520b4fb48SLinus Torvalds #include <linux/magic.h> 260818bf27SAl Viro #include <linux/bootmem.h> 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); 2282504c5d6SAndreas Gruenbacher #ifdef CONFIG_FSNOTIFY 2292504c5d6SAndreas Gruenbacher INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks); 2302504c5d6SAndreas Gruenbacher #endif 2311da177e4SLinus Torvalds } 232c63181e6SAl Viro return mnt; 23388b38782SLi Zefan 234d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 235d3ef3d73Snpiggin@suse.de out_free_devname: 236c63181e6SAl Viro kfree(mnt->mnt_devname); 237d3ef3d73Snpiggin@suse.de #endif 23888b38782SLi Zefan out_free_id: 239c63181e6SAl Viro mnt_free_id(mnt); 24088b38782SLi Zefan out_free_cache: 241c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 24288b38782SLi Zefan return NULL; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2458366025eSDave Hansen /* 2468366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2478366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2488366025eSDave Hansen * We must keep track of when those operations start 2498366025eSDave Hansen * (for permission checks) and when they end, so that 2508366025eSDave Hansen * we can determine when writes are able to occur to 2518366025eSDave Hansen * a filesystem. 2528366025eSDave Hansen */ 2533d733633SDave Hansen /* 2543d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2553d733633SDave Hansen * @mnt: the mount to check for its write status 2563d733633SDave Hansen * 2573d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2583d733633SDave Hansen * It does not guarantee that the filesystem will stay 2593d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2603d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2613d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2623d733633SDave Hansen * r/w. 2633d733633SDave Hansen */ 2643d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2653d733633SDave Hansen { 2662e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2672e4b7fcdSDave Hansen return 1; 2682e4b7fcdSDave Hansen if (mnt->mnt_sb->s_flags & MS_RDONLY) 2692e4b7fcdSDave Hansen return 1; 2702e4b7fcdSDave Hansen return 0; 2713d733633SDave Hansen } 2723d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2733d733633SDave Hansen 27483adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2753d733633SDave Hansen { 276d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 27768e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 278d3ef3d73Snpiggin@suse.de #else 27968e8a9feSAl Viro mnt->mnt_writers++; 280d3ef3d73Snpiggin@suse.de #endif 2813d733633SDave Hansen } 2823d733633SDave Hansen 28383adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2843d733633SDave Hansen { 285d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 28668e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 287d3ef3d73Snpiggin@suse.de #else 28868e8a9feSAl Viro mnt->mnt_writers--; 289d3ef3d73Snpiggin@suse.de #endif 290d3ef3d73Snpiggin@suse.de } 291d3ef3d73Snpiggin@suse.de 29283adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 293d3ef3d73Snpiggin@suse.de { 294d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 295d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2963d733633SDave Hansen int cpu; 2973d733633SDave Hansen 2983d733633SDave Hansen for_each_possible_cpu(cpu) { 29968e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 3003d733633SDave Hansen } 3013d733633SDave Hansen 302d3ef3d73Snpiggin@suse.de return count; 303d3ef3d73Snpiggin@suse.de #else 304d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 305d3ef3d73Snpiggin@suse.de #endif 3063d733633SDave Hansen } 3073d733633SDave Hansen 3084ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 3094ed5e82fSMiklos Szeredi { 3104ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 3114ed5e82fSMiklos Szeredi return 1; 3124ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 3134ed5e82fSMiklos Szeredi smp_rmb(); 3144ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 3154ed5e82fSMiklos Szeredi } 3164ed5e82fSMiklos Szeredi 3173d733633SDave Hansen /* 318eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 319eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 320eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 321eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3223d733633SDave Hansen */ 3238366025eSDave Hansen /** 324eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 32583adc753SAl Viro * @m: the mount on which to take a write 3268366025eSDave Hansen * 327eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 328eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 329eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 330eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 331eb04c282SJan Kara * called. This is effectively a refcount. 3328366025eSDave Hansen */ 333eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3348366025eSDave Hansen { 33583adc753SAl Viro struct mount *mnt = real_mount(m); 3363d733633SDave Hansen int ret = 0; 3373d733633SDave Hansen 338d3ef3d73Snpiggin@suse.de preempt_disable(); 339c6653a83SNick Piggin mnt_inc_writers(mnt); 340d3ef3d73Snpiggin@suse.de /* 341c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 342d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 343d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 344d3ef3d73Snpiggin@suse.de */ 345d3ef3d73Snpiggin@suse.de smp_mb(); 3461e75529eSMiao Xie while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 347d3ef3d73Snpiggin@suse.de cpu_relax(); 348d3ef3d73Snpiggin@suse.de /* 349d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 350d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 351d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 352d3ef3d73Snpiggin@suse.de */ 353d3ef3d73Snpiggin@suse.de smp_rmb(); 3544ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 355c6653a83SNick Piggin mnt_dec_writers(mnt); 3563d733633SDave Hansen ret = -EROFS; 3573d733633SDave Hansen } 358d3ef3d73Snpiggin@suse.de preempt_enable(); 359eb04c282SJan Kara 360eb04c282SJan Kara return ret; 361eb04c282SJan Kara } 362eb04c282SJan Kara 363eb04c282SJan Kara /** 364eb04c282SJan Kara * mnt_want_write - get write access to a mount 365eb04c282SJan Kara * @m: the mount on which to take a write 366eb04c282SJan Kara * 367eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 368eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 369eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 370eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 371eb04c282SJan Kara */ 372eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 373eb04c282SJan Kara { 374eb04c282SJan Kara int ret; 375eb04c282SJan Kara 376eb04c282SJan Kara sb_start_write(m->mnt_sb); 377eb04c282SJan Kara ret = __mnt_want_write(m); 378eb04c282SJan Kara if (ret) 379eb04c282SJan Kara sb_end_write(m->mnt_sb); 3803d733633SDave Hansen return ret; 3818366025eSDave Hansen } 3828366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3838366025eSDave Hansen 3848366025eSDave Hansen /** 38596029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 38696029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 38796029c4eSnpiggin@suse.de * 38896029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 38996029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 39096029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 39196029c4eSnpiggin@suse.de * on it. This allows some optimisation. 39296029c4eSnpiggin@suse.de * 39396029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 39496029c4eSnpiggin@suse.de * drop the reference. 39596029c4eSnpiggin@suse.de */ 39696029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 39796029c4eSnpiggin@suse.de { 39896029c4eSnpiggin@suse.de /* superblock may be r/o */ 39996029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 40096029c4eSnpiggin@suse.de return -EROFS; 40196029c4eSnpiggin@suse.de preempt_disable(); 40283adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 40396029c4eSnpiggin@suse.de preempt_enable(); 40496029c4eSnpiggin@suse.de return 0; 40596029c4eSnpiggin@suse.de } 40696029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 40796029c4eSnpiggin@suse.de 40896029c4eSnpiggin@suse.de /** 409eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 410eb04c282SJan Kara * @file: the file who's mount on which to take a write 411eb04c282SJan Kara * 412eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 413eb04c282SJan Kara * do some optimisations if the file is open for write already 414eb04c282SJan Kara */ 415eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 416eb04c282SJan Kara { 41783f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 418eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 419eb04c282SJan Kara else 420eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 421eb04c282SJan Kara } 422eb04c282SJan Kara 423eb04c282SJan Kara /** 42496029c4eSnpiggin@suse.de * mnt_want_write_file - get write access to a file's mount 42596029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 42696029c4eSnpiggin@suse.de * 42796029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 42896029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 42996029c4eSnpiggin@suse.de */ 43096029c4eSnpiggin@suse.de int mnt_want_write_file(struct file *file) 43196029c4eSnpiggin@suse.de { 432eb04c282SJan Kara int ret; 433eb04c282SJan Kara 434eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 435eb04c282SJan Kara ret = __mnt_want_write_file(file); 436eb04c282SJan Kara if (ret) 437eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 438eb04c282SJan Kara return ret; 43996029c4eSnpiggin@suse.de } 44096029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 44196029c4eSnpiggin@suse.de 44296029c4eSnpiggin@suse.de /** 443eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4448366025eSDave Hansen * @mnt: the mount on which to give up write access 4458366025eSDave Hansen * 4468366025eSDave Hansen * Tells the low-level filesystem that we are done 4478366025eSDave Hansen * performing writes to it. Must be matched with 448eb04c282SJan Kara * __mnt_want_write() call above. 4498366025eSDave Hansen */ 450eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4518366025eSDave Hansen { 452d3ef3d73Snpiggin@suse.de preempt_disable(); 45383adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 454d3ef3d73Snpiggin@suse.de preempt_enable(); 4558366025eSDave Hansen } 456eb04c282SJan Kara 457eb04c282SJan Kara /** 458eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 459eb04c282SJan Kara * @mnt: the mount on which to give up write access 460eb04c282SJan Kara * 461eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 462eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 463eb04c282SJan Kara * mnt_want_write() call above. 464eb04c282SJan Kara */ 465eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 466eb04c282SJan Kara { 467eb04c282SJan Kara __mnt_drop_write(mnt); 468eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 469eb04c282SJan Kara } 4708366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4718366025eSDave Hansen 472eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 473eb04c282SJan Kara { 474eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 475eb04c282SJan Kara } 476eb04c282SJan Kara 4772a79f17eSAl Viro void mnt_drop_write_file(struct file *file) 4782a79f17eSAl Viro { 4792a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 4802a79f17eSAl Viro } 4812a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4822a79f17eSAl Viro 48383adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4848366025eSDave Hansen { 4853d733633SDave Hansen int ret = 0; 4863d733633SDave Hansen 487719ea2fbSAl Viro lock_mount_hash(); 48883adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 489d3ef3d73Snpiggin@suse.de /* 490d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 491d3ef3d73Snpiggin@suse.de * should be visible before we do. 492d3ef3d73Snpiggin@suse.de */ 493d3ef3d73Snpiggin@suse.de smp_mb(); 494d3ef3d73Snpiggin@suse.de 495d3ef3d73Snpiggin@suse.de /* 496d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 497d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 498d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 499d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 500d3ef3d73Snpiggin@suse.de * 501d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 502d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 503d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 504d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 505d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 506d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 507d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 508d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 509d3ef3d73Snpiggin@suse.de * we're counting up here. 510d3ef3d73Snpiggin@suse.de */ 511c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 512d3ef3d73Snpiggin@suse.de ret = -EBUSY; 513d3ef3d73Snpiggin@suse.de else 51483adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 515d3ef3d73Snpiggin@suse.de /* 516d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 517d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 518d3ef3d73Snpiggin@suse.de */ 519d3ef3d73Snpiggin@suse.de smp_wmb(); 52083adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 521719ea2fbSAl Viro unlock_mount_hash(); 5223d733633SDave Hansen return ret; 5233d733633SDave Hansen } 5248366025eSDave Hansen 52583adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 5262e4b7fcdSDave Hansen { 527719ea2fbSAl Viro lock_mount_hash(); 52883adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 529719ea2fbSAl Viro unlock_mount_hash(); 5302e4b7fcdSDave Hansen } 5312e4b7fcdSDave Hansen 5324ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5334ed5e82fSMiklos Szeredi { 5344ed5e82fSMiklos Szeredi struct mount *mnt; 5354ed5e82fSMiklos Szeredi int err = 0; 5364ed5e82fSMiklos Szeredi 5378e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5388e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5398e8b8796SMiklos Szeredi return -EBUSY; 5408e8b8796SMiklos Szeredi 541719ea2fbSAl Viro lock_mount_hash(); 5424ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5434ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5444ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5454ed5e82fSMiklos Szeredi smp_mb(); 5464ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5474ed5e82fSMiklos Szeredi err = -EBUSY; 5484ed5e82fSMiklos Szeredi break; 5494ed5e82fSMiklos Szeredi } 5504ed5e82fSMiklos Szeredi } 5514ed5e82fSMiklos Szeredi } 5528e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5538e8b8796SMiklos Szeredi err = -EBUSY; 5548e8b8796SMiklos Szeredi 5554ed5e82fSMiklos Szeredi if (!err) { 5564ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5574ed5e82fSMiklos Szeredi smp_wmb(); 5584ed5e82fSMiklos Szeredi } 5594ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5604ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5614ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5624ed5e82fSMiklos Szeredi } 563719ea2fbSAl Viro unlock_mount_hash(); 5644ed5e82fSMiklos Szeredi 5654ed5e82fSMiklos Szeredi return err; 5664ed5e82fSMiklos Szeredi } 5674ed5e82fSMiklos Szeredi 568b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5691da177e4SLinus Torvalds { 57052ba1621SAl Viro kfree(mnt->mnt_devname); 571d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 57268e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 573d3ef3d73Snpiggin@suse.de #endif 574b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5778ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5788ffcb32eSDavid Howells { 5798ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5808ffcb32eSDavid Howells } 5818ffcb32eSDavid Howells 58248a066e7SAl Viro /* call under rcu_read_lock */ 58348a066e7SAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 58448a066e7SAl Viro { 58548a066e7SAl Viro struct mount *mnt; 58648a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 58748a066e7SAl Viro return false; 58848a066e7SAl Viro if (bastard == NULL) 58948a066e7SAl Viro return true; 59048a066e7SAl Viro mnt = real_mount(bastard); 59148a066e7SAl Viro mnt_add_count(mnt, 1); 59248a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 59348a066e7SAl Viro return true; 59448a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 59548a066e7SAl Viro mnt_add_count(mnt, -1); 59648a066e7SAl Viro return false; 59748a066e7SAl Viro } 59848a066e7SAl Viro rcu_read_unlock(); 59948a066e7SAl Viro mntput(bastard); 60048a066e7SAl Viro rcu_read_lock(); 60148a066e7SAl Viro return false; 60248a066e7SAl Viro } 60348a066e7SAl Viro 6041da177e4SLinus Torvalds /* 605474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 60648a066e7SAl Viro * call under rcu_read_lock() 6071da177e4SLinus Torvalds */ 608474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6091da177e4SLinus Torvalds { 61038129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 611474279dcSAl Viro struct mount *p; 6121da177e4SLinus Torvalds 61338129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 614474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 615474279dcSAl Viro return p; 616474279dcSAl Viro return NULL; 6171da177e4SLinus Torvalds } 618474279dcSAl Viro 619474279dcSAl Viro /* 620474279dcSAl Viro * find the last mount at @dentry on vfsmount @mnt. 62148a066e7SAl Viro * mount_lock must be held. 622474279dcSAl Viro */ 623474279dcSAl Viro struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry) 624474279dcSAl Viro { 62538129a13SAl Viro struct mount *p, *res; 62638129a13SAl Viro res = p = __lookup_mnt(mnt, dentry); 62738129a13SAl Viro if (!p) 62838129a13SAl Viro goto out; 62938129a13SAl Viro hlist_for_each_entry_continue(p, mnt_hash) { 6301d6a32acSAl Viro if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry) 6311d6a32acSAl Viro break; 6321d6a32acSAl Viro res = p; 6331d6a32acSAl Viro } 63438129a13SAl Viro out: 6351d6a32acSAl Viro return res; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 638a05964f3SRam Pai /* 639f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 640f015f126SDavid Howells * 641f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 642f015f126SDavid Howells * following mounts: 643f015f126SDavid Howells * 644f015f126SDavid Howells * mount /dev/sda1 /mnt 645f015f126SDavid Howells * mount /dev/sda2 /mnt 646f015f126SDavid Howells * mount /dev/sda3 /mnt 647f015f126SDavid Howells * 648f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 649f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 650f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 651f015f126SDavid Howells * 652f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 653a05964f3SRam Pai */ 6541c755af4SAl Viro struct vfsmount *lookup_mnt(struct path *path) 655a05964f3SRam Pai { 656c7105365SAl Viro struct mount *child_mnt; 65748a066e7SAl Viro struct vfsmount *m; 65848a066e7SAl Viro unsigned seq; 65999b7db7bSNick Piggin 66048a066e7SAl Viro rcu_read_lock(); 66148a066e7SAl Viro do { 66248a066e7SAl Viro seq = read_seqbegin(&mount_lock); 663474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 66448a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 66548a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 66648a066e7SAl Viro rcu_read_unlock(); 66748a066e7SAl Viro return m; 668a05964f3SRam Pai } 669a05964f3SRam Pai 67084d17192SAl Viro static struct mountpoint *new_mountpoint(struct dentry *dentry) 67184d17192SAl Viro { 6720818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 67384d17192SAl Viro struct mountpoint *mp; 674eed81007SMiklos Szeredi int ret; 67584d17192SAl Viro 6760818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 67784d17192SAl Viro if (mp->m_dentry == dentry) { 67884d17192SAl Viro /* might be worth a WARN_ON() */ 67984d17192SAl Viro if (d_unlinked(dentry)) 68084d17192SAl Viro return ERR_PTR(-ENOENT); 68184d17192SAl Viro mp->m_count++; 68284d17192SAl Viro return mp; 68384d17192SAl Viro } 68484d17192SAl Viro } 68584d17192SAl Viro 68684d17192SAl Viro mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 68784d17192SAl Viro if (!mp) 68884d17192SAl Viro return ERR_PTR(-ENOMEM); 68984d17192SAl Viro 690eed81007SMiklos Szeredi ret = d_set_mounted(dentry); 691eed81007SMiklos Szeredi if (ret) { 69284d17192SAl Viro kfree(mp); 693eed81007SMiklos Szeredi return ERR_PTR(ret); 69484d17192SAl Viro } 695eed81007SMiklos Szeredi 69684d17192SAl Viro mp->m_dentry = dentry; 69784d17192SAl Viro mp->m_count = 1; 6980818bf27SAl Viro hlist_add_head(&mp->m_hash, chain); 69984d17192SAl Viro return mp; 70084d17192SAl Viro } 70184d17192SAl Viro 70284d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 70384d17192SAl Viro { 70484d17192SAl Viro if (!--mp->m_count) { 70584d17192SAl Viro struct dentry *dentry = mp->m_dentry; 70684d17192SAl Viro spin_lock(&dentry->d_lock); 70784d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 70884d17192SAl Viro spin_unlock(&dentry->d_lock); 7090818bf27SAl Viro hlist_del(&mp->m_hash); 71084d17192SAl Viro kfree(mp); 71184d17192SAl Viro } 71284d17192SAl Viro } 71384d17192SAl Viro 714143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7151da177e4SLinus Torvalds { 7166b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 71999b7db7bSNick Piggin /* 72099b7db7bSNick Piggin * vfsmount lock must be held for write 72199b7db7bSNick Piggin */ 7226b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7235addc5ddSAl Viro { 7245addc5ddSAl Viro if (ns) { 7255addc5ddSAl Viro ns->event = ++event; 7265addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7275addc5ddSAl Viro } 7285addc5ddSAl Viro } 7295addc5ddSAl Viro 73099b7db7bSNick Piggin /* 73199b7db7bSNick Piggin * vfsmount lock must be held for write 73299b7db7bSNick Piggin */ 7336b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7345addc5ddSAl Viro { 7355addc5ddSAl Viro if (ns && ns->event != event) { 7365addc5ddSAl Viro ns->event = event; 7375addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7385addc5ddSAl Viro } 7395addc5ddSAl Viro } 7405addc5ddSAl Viro 74199b7db7bSNick Piggin /* 74299b7db7bSNick Piggin * vfsmount lock must be held for write 74399b7db7bSNick Piggin */ 744419148daSAl Viro static void detach_mnt(struct mount *mnt, struct path *old_path) 7451da177e4SLinus Torvalds { 746a73324daSAl Viro old_path->dentry = mnt->mnt_mountpoint; 7470714a533SAl Viro old_path->mnt = &mnt->mnt_parent->mnt; 7480714a533SAl Viro mnt->mnt_parent = mnt; 749a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 7506b41d536SAl Viro list_del_init(&mnt->mnt_child); 75138129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 75284d17192SAl Viro put_mountpoint(mnt->mnt_mp); 75384d17192SAl Viro mnt->mnt_mp = NULL; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 75699b7db7bSNick Piggin /* 75799b7db7bSNick Piggin * vfsmount lock must be held for write 75899b7db7bSNick Piggin */ 75984d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 76084d17192SAl Viro struct mountpoint *mp, 76144d964d6SAl Viro struct mount *child_mnt) 762b90fa9aeSRam Pai { 76384d17192SAl Viro mp->m_count++; 7643a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 76584d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 7663a2393d7SAl Viro child_mnt->mnt_parent = mnt; 76784d17192SAl Viro child_mnt->mnt_mp = mp; 768b90fa9aeSRam Pai } 769b90fa9aeSRam Pai 77099b7db7bSNick Piggin /* 77199b7db7bSNick Piggin * vfsmount lock must be held for write 77299b7db7bSNick Piggin */ 77384d17192SAl Viro static void attach_mnt(struct mount *mnt, 77484d17192SAl Viro struct mount *parent, 77584d17192SAl Viro struct mountpoint *mp) 7761da177e4SLinus Torvalds { 77784d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 77838129a13SAl Viro hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry)); 77984d17192SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 780b90fa9aeSRam Pai } 781b90fa9aeSRam Pai 782b90fa9aeSRam Pai /* 78399b7db7bSNick Piggin * vfsmount lock must be held for write 784b90fa9aeSRam Pai */ 7851d6a32acSAl Viro static void commit_tree(struct mount *mnt, struct mount *shadows) 786b90fa9aeSRam Pai { 7870714a533SAl Viro struct mount *parent = mnt->mnt_parent; 78883adc753SAl Viro struct mount *m; 789b90fa9aeSRam Pai LIST_HEAD(head); 790143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 791b90fa9aeSRam Pai 7920714a533SAl Viro BUG_ON(parent == mnt); 793b90fa9aeSRam Pai 7941a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 795f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 796143c8c91SAl Viro m->mnt_ns = n; 797f03c6599SAl Viro 798b90fa9aeSRam Pai list_splice(&head, n->list.prev); 799b90fa9aeSRam Pai 8001d6a32acSAl Viro if (shadows) 80138129a13SAl Viro hlist_add_after_rcu(&shadows->mnt_hash, &mnt->mnt_hash); 8021d6a32acSAl Viro else 80338129a13SAl Viro hlist_add_head_rcu(&mnt->mnt_hash, 8040818bf27SAl Viro m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8056b41d536SAl Viro list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8066b3286edSKirill Korotaev touch_mnt_namespace(n); 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 809909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 8101da177e4SLinus Torvalds { 8116b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 8126b41d536SAl Viro if (next == &p->mnt_mounts) { 8131da177e4SLinus Torvalds while (1) { 814909b0a88SAl Viro if (p == root) 8151da177e4SLinus Torvalds return NULL; 8166b41d536SAl Viro next = p->mnt_child.next; 8176b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 8181da177e4SLinus Torvalds break; 8190714a533SAl Viro p = p->mnt_parent; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds } 8226b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 825315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 8269676f0c6SRam Pai { 8276b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 8286b41d536SAl Viro while (prev != &p->mnt_mounts) { 8296b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 8306b41d536SAl Viro prev = p->mnt_mounts.prev; 8319676f0c6SRam Pai } 8329676f0c6SRam Pai return p; 8339676f0c6SRam Pai } 8349676f0c6SRam Pai 8359d412a43SAl Viro struct vfsmount * 8369d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 8379d412a43SAl Viro { 838b105e270SAl Viro struct mount *mnt; 8399d412a43SAl Viro struct dentry *root; 8409d412a43SAl Viro 8419d412a43SAl Viro if (!type) 8429d412a43SAl Viro return ERR_PTR(-ENODEV); 8439d412a43SAl Viro 8449d412a43SAl Viro mnt = alloc_vfsmnt(name); 8459d412a43SAl Viro if (!mnt) 8469d412a43SAl Viro return ERR_PTR(-ENOMEM); 8479d412a43SAl Viro 8489d412a43SAl Viro if (flags & MS_KERNMOUNT) 849b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 8509d412a43SAl Viro 8519d412a43SAl Viro root = mount_fs(type, flags, name, data); 8529d412a43SAl Viro if (IS_ERR(root)) { 8538ffcb32eSDavid Howells mnt_free_id(mnt); 8549d412a43SAl Viro free_vfsmnt(mnt); 8559d412a43SAl Viro return ERR_CAST(root); 8569d412a43SAl Viro } 8579d412a43SAl Viro 858b105e270SAl Viro mnt->mnt.mnt_root = root; 859b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 860a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8610714a533SAl Viro mnt->mnt_parent = mnt; 862719ea2fbSAl Viro lock_mount_hash(); 86339f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 864719ea2fbSAl Viro unlock_mount_hash(); 865b105e270SAl Viro return &mnt->mnt; 8669d412a43SAl Viro } 8679d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 8689d412a43SAl Viro 86987129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 87036341f64SRam Pai int flag) 8711da177e4SLinus Torvalds { 87287129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 873be34d1a3SDavid Howells struct mount *mnt; 874be34d1a3SDavid Howells int err; 8751da177e4SLinus Torvalds 876be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 877be34d1a3SDavid Howells if (!mnt) 878be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 879be34d1a3SDavid Howells 8807a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 88115169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 882719f5d7fSMiklos Szeredi else 88315169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 884719f5d7fSMiklos Szeredi 88515169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 886be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 887719f5d7fSMiklos Szeredi if (err) 888719f5d7fSMiklos Szeredi goto out_free; 889719f5d7fSMiklos Szeredi } 890719f5d7fSMiklos Szeredi 891f2ebb3a9SAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED); 892132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 893132c94e3SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY)) 894132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 895132c94e3SEric W. Biederman 8965ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 8975ff9d8a6SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire)) 8985ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 8995ff9d8a6SEric W. Biederman 9001da177e4SLinus Torvalds atomic_inc(&sb->s_active); 901b105e270SAl Viro mnt->mnt.mnt_sb = sb; 902b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 903a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9040714a533SAl Viro mnt->mnt_parent = mnt; 905719ea2fbSAl Viro lock_mount_hash(); 90639f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 907719ea2fbSAl Viro unlock_mount_hash(); 908b90fa9aeSRam Pai 9097a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 9107a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 9116776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 91232301920SAl Viro mnt->mnt_master = old; 913fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 9148aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 915fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 9166776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 917d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 9186776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 919d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 9205afe0022SRam Pai } 921b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 9220f0afb1dSAl Viro set_mnt_shared(mnt); 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 9251da177e4SLinus Torvalds * as the original if that was on one */ 92636341f64SRam Pai if (flag & CL_EXPIRE) { 9276776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 9286776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 9291da177e4SLinus Torvalds } 930be34d1a3SDavid Howells 931cb338d06SAl Viro return mnt; 932719f5d7fSMiklos Szeredi 933719f5d7fSMiklos Szeredi out_free: 9348ffcb32eSDavid Howells mnt_free_id(mnt); 935719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 936be34d1a3SDavid Howells return ERR_PTR(err); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 939900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 9407b7b1aceSAl Viro { 941b3e19d92SNick Piggin put_again: 94248a066e7SAl Viro rcu_read_lock(); 943aa9c0e07SAl Viro mnt_add_count(mnt, -1); 94448a066e7SAl Viro if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */ 94548a066e7SAl Viro rcu_read_unlock(); 94699b7db7bSNick Piggin return; 947b3e19d92SNick Piggin } 948719ea2fbSAl Viro lock_mount_hash(); 949b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 95048a066e7SAl Viro rcu_read_unlock(); 951719ea2fbSAl Viro unlock_mount_hash(); 95299b7db7bSNick Piggin return; 95399b7db7bSNick Piggin } 954863d684fSAl Viro if (unlikely(mnt->mnt_pinned)) { 955863d684fSAl Viro mnt_add_count(mnt, mnt->mnt_pinned + 1); 956863d684fSAl Viro mnt->mnt_pinned = 0; 95748a066e7SAl Viro rcu_read_unlock(); 958719ea2fbSAl Viro unlock_mount_hash(); 959900148dcSAl Viro acct_auto_close_mnt(&mnt->mnt); 960b3e19d92SNick Piggin goto put_again; 961b3e19d92SNick Piggin } 96248a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 96348a066e7SAl Viro rcu_read_unlock(); 96448a066e7SAl Viro unlock_mount_hash(); 96548a066e7SAl Viro return; 96648a066e7SAl Viro } 96748a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 96848a066e7SAl Viro rcu_read_unlock(); 969962830dfSAndi Kleen 97039f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 971719ea2fbSAl Viro unlock_mount_hash(); 972649a795aSAl Viro 973649a795aSAl Viro /* 974649a795aSAl Viro * This probably indicates that somebody messed 975649a795aSAl Viro * up a mnt_want/drop_write() pair. If this 976649a795aSAl Viro * happens, the filesystem was probably unable 977649a795aSAl Viro * to make r/w->r/o transitions. 978649a795aSAl Viro */ 979649a795aSAl Viro /* 980649a795aSAl Viro * The locking used to deal with mnt_count decrement provides barriers, 981649a795aSAl Viro * so mnt_get_writers() below is safe. 982649a795aSAl Viro */ 983649a795aSAl Viro WARN_ON(mnt_get_writers(mnt)); 984649a795aSAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 985649a795aSAl Viro dput(mnt->mnt.mnt_root); 986649a795aSAl Viro deactivate_super(mnt->mnt.mnt_sb); 98748a066e7SAl Viro mnt_free_id(mnt); 9888ffcb32eSDavid Howells call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 989b3e19d92SNick Piggin } 990b3e19d92SNick Piggin 991b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 992b3e19d92SNick Piggin { 993b3e19d92SNick Piggin if (mnt) { 994863d684fSAl Viro struct mount *m = real_mount(mnt); 995b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 996863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 997863d684fSAl Viro m->mnt_expiry_mark = 0; 998863d684fSAl Viro mntput_no_expire(m); 999b3e19d92SNick Piggin } 1000b3e19d92SNick Piggin } 1001b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1002b3e19d92SNick Piggin 1003b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1004b3e19d92SNick Piggin { 1005b3e19d92SNick Piggin if (mnt) 100683adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1007b3e19d92SNick Piggin return mnt; 1008b3e19d92SNick Piggin } 1009b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1010b3e19d92SNick Piggin 10117b7b1aceSAl Viro void mnt_pin(struct vfsmount *mnt) 10127b7b1aceSAl Viro { 1013719ea2fbSAl Viro lock_mount_hash(); 1014863d684fSAl Viro real_mount(mnt)->mnt_pinned++; 1015719ea2fbSAl Viro unlock_mount_hash(); 10167b7b1aceSAl Viro } 10177b7b1aceSAl Viro EXPORT_SYMBOL(mnt_pin); 10187b7b1aceSAl Viro 1019863d684fSAl Viro void mnt_unpin(struct vfsmount *m) 10207b7b1aceSAl Viro { 1021863d684fSAl Viro struct mount *mnt = real_mount(m); 1022719ea2fbSAl Viro lock_mount_hash(); 10237b7b1aceSAl Viro if (mnt->mnt_pinned) { 1024863d684fSAl Viro mnt_add_count(mnt, 1); 10257b7b1aceSAl Viro mnt->mnt_pinned--; 10267b7b1aceSAl Viro } 1027719ea2fbSAl Viro unlock_mount_hash(); 10287b7b1aceSAl Viro } 10297b7b1aceSAl Viro EXPORT_SYMBOL(mnt_unpin); 10301da177e4SLinus Torvalds 1031b3b304a2SMiklos Szeredi static inline void mangle(struct seq_file *m, const char *s) 1032b3b304a2SMiklos Szeredi { 1033b3b304a2SMiklos Szeredi seq_escape(m, s, " \t\n\\"); 1034b3b304a2SMiklos Szeredi } 1035b3b304a2SMiklos Szeredi 1036b3b304a2SMiklos Szeredi /* 1037b3b304a2SMiklos Szeredi * Simple .show_options callback for filesystems which don't want to 1038b3b304a2SMiklos Szeredi * implement more complex mount option showing. 1039b3b304a2SMiklos Szeredi * 1040b3b304a2SMiklos Szeredi * See also save_mount_options(). 1041b3b304a2SMiklos Szeredi */ 104234c80b1dSAl Viro int generic_show_options(struct seq_file *m, struct dentry *root) 1043b3b304a2SMiklos Szeredi { 10442a32cebdSAl Viro const char *options; 10452a32cebdSAl Viro 10462a32cebdSAl Viro rcu_read_lock(); 104734c80b1dSAl Viro options = rcu_dereference(root->d_sb->s_options); 1048b3b304a2SMiklos Szeredi 1049b3b304a2SMiklos Szeredi if (options != NULL && options[0]) { 1050b3b304a2SMiklos Szeredi seq_putc(m, ','); 1051b3b304a2SMiklos Szeredi mangle(m, options); 1052b3b304a2SMiklos Szeredi } 10532a32cebdSAl Viro rcu_read_unlock(); 1054b3b304a2SMiklos Szeredi 1055b3b304a2SMiklos Szeredi return 0; 1056b3b304a2SMiklos Szeredi } 1057b3b304a2SMiklos Szeredi EXPORT_SYMBOL(generic_show_options); 1058b3b304a2SMiklos Szeredi 1059b3b304a2SMiklos Szeredi /* 1060b3b304a2SMiklos Szeredi * If filesystem uses generic_show_options(), this function should be 1061b3b304a2SMiklos Szeredi * called from the fill_super() callback. 1062b3b304a2SMiklos Szeredi * 1063b3b304a2SMiklos Szeredi * The .remount_fs callback usually needs to be handled in a special 1064b3b304a2SMiklos Szeredi * way, to make sure, that previous options are not overwritten if the 1065b3b304a2SMiklos Szeredi * remount fails. 1066b3b304a2SMiklos Szeredi * 1067b3b304a2SMiklos Szeredi * Also note, that if the filesystem's .remount_fs function doesn't 1068b3b304a2SMiklos Szeredi * reset all options to their default value, but changes only newly 1069b3b304a2SMiklos Szeredi * given options, then the displayed options will not reflect reality 1070b3b304a2SMiklos Szeredi * any more. 1071b3b304a2SMiklos Szeredi */ 1072b3b304a2SMiklos Szeredi void save_mount_options(struct super_block *sb, char *options) 1073b3b304a2SMiklos Szeredi { 10742a32cebdSAl Viro BUG_ON(sb->s_options); 10752a32cebdSAl Viro rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL)); 1076b3b304a2SMiklos Szeredi } 1077b3b304a2SMiklos Szeredi EXPORT_SYMBOL(save_mount_options); 1078b3b304a2SMiklos Szeredi 10792a32cebdSAl Viro void replace_mount_options(struct super_block *sb, char *options) 10802a32cebdSAl Viro { 10812a32cebdSAl Viro char *old = sb->s_options; 10822a32cebdSAl Viro rcu_assign_pointer(sb->s_options, options); 10832a32cebdSAl Viro if (old) { 10842a32cebdSAl Viro synchronize_rcu(); 10852a32cebdSAl Viro kfree(old); 10862a32cebdSAl Viro } 10872a32cebdSAl Viro } 10882a32cebdSAl Viro EXPORT_SYMBOL(replace_mount_options); 10892a32cebdSAl Viro 1090a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 10910226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 10921da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 10931da177e4SLinus Torvalds { 10946ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 10951da177e4SLinus Torvalds 1096390c6843SRam Pai down_read(&namespace_sem); 1097c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1098c7999c36SAl Viro void *v = p->cached_mount; 1099c7999c36SAl Viro if (*pos == p->cached_index) 1100c7999c36SAl Viro return v; 1101c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1102c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1103c7999c36SAl Viro return p->cached_mount = v; 1104c7999c36SAl Viro } 1105c7999c36SAl Viro } 1106c7999c36SAl Viro 1107c7999c36SAl Viro p->cached_event = p->ns->event; 1108c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1109c7999c36SAl Viro p->cached_index = *pos; 1110c7999c36SAl Viro return p->cached_mount; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 11141da177e4SLinus Torvalds { 11156ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 1116b0765fb8SPavel Emelianov 1117c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1118c7999c36SAl Viro p->cached_index = *pos; 1119c7999c36SAl Viro return p->cached_mount; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 11231da177e4SLinus Torvalds { 1124390c6843SRam Pai up_read(&namespace_sem); 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds 11270226f492SAl Viro static int m_show(struct seq_file *m, void *v) 11289f5596afSAl Viro { 11296ce6e24eSAl Viro struct proc_mounts *p = proc_mounts(m); 11301a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 11310226f492SAl Viro return p->show(m, &r->mnt); 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 1134a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 11351da177e4SLinus Torvalds .start = m_start, 11361da177e4SLinus Torvalds .next = m_next, 11371da177e4SLinus Torvalds .stop = m_stop, 11380226f492SAl Viro .show = m_show, 1139b4629fe2SChuck Lever }; 1140a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1141b4629fe2SChuck Lever 11421da177e4SLinus Torvalds /** 11431da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 11441da177e4SLinus Torvalds * @mnt: root of mount tree 11451da177e4SLinus Torvalds * 11461da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 11471da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 11481da177e4SLinus Torvalds * busy. 11491da177e4SLinus Torvalds */ 1150909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 11511da177e4SLinus Torvalds { 1152909b0a88SAl Viro struct mount *mnt = real_mount(m); 115336341f64SRam Pai int actual_refs = 0; 115436341f64SRam Pai int minimum_refs = 0; 1155315fc83eSAl Viro struct mount *p; 1156909b0a88SAl Viro BUG_ON(!m); 11571da177e4SLinus Torvalds 1158b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1159719ea2fbSAl Viro lock_mount_hash(); 1160909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 116183adc753SAl Viro actual_refs += mnt_get_count(p); 11621da177e4SLinus Torvalds minimum_refs += 2; 11631da177e4SLinus Torvalds } 1164719ea2fbSAl Viro unlock_mount_hash(); 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds if (actual_refs > minimum_refs) 11671da177e4SLinus Torvalds return 0; 1168e3474a8eSIan Kent 1169e3474a8eSIan Kent return 1; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds /** 11751da177e4SLinus Torvalds * may_umount - check if a mount point is busy 11761da177e4SLinus Torvalds * @mnt: root of mount 11771da177e4SLinus Torvalds * 11781da177e4SLinus Torvalds * This is called to check if a mount point has any 11791da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 11801da177e4SLinus Torvalds * mount has sub mounts this will return busy 11811da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 11821da177e4SLinus Torvalds * 11831da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 11841da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 11851da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 11861da177e4SLinus Torvalds */ 11871da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 11881da177e4SLinus Torvalds { 1189e3474a8eSIan Kent int ret = 1; 11908ad08d8aSAl Viro down_read(&namespace_sem); 1191719ea2fbSAl Viro lock_mount_hash(); 11921ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1193e3474a8eSIan Kent ret = 0; 1194719ea2fbSAl Viro unlock_mount_hash(); 11958ad08d8aSAl Viro up_read(&namespace_sem); 1196a05964f3SRam Pai return ret; 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 12001da177e4SLinus Torvalds 120138129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1202e3197d83SAl Viro 120397216be0SAl Viro static void namespace_unlock(void) 12041da177e4SLinus Torvalds { 1205d5e50f74SAl Viro struct mount *mnt; 120638129a13SAl Viro struct hlist_head head = unmounted; 120797216be0SAl Viro 120838129a13SAl Viro if (likely(hlist_empty(&head))) { 120997216be0SAl Viro up_write(&namespace_sem); 121097216be0SAl Viro return; 121197216be0SAl Viro } 121297216be0SAl Viro 121338129a13SAl Viro head.first->pprev = &head.first; 121438129a13SAl Viro INIT_HLIST_HEAD(&unmounted); 121538129a13SAl Viro 121697216be0SAl Viro up_write(&namespace_sem); 121797216be0SAl Viro 121848a066e7SAl Viro synchronize_rcu(); 121948a066e7SAl Viro 122038129a13SAl Viro while (!hlist_empty(&head)) { 122138129a13SAl Viro mnt = hlist_entry(head.first, struct mount, mnt_hash); 122238129a13SAl Viro hlist_del_init(&mnt->mnt_hash); 1223aba809cfSAl Viro if (mnt->mnt_ex_mountpoint.mnt) 1224aba809cfSAl Viro path_put(&mnt->mnt_ex_mountpoint); 1225d5e50f74SAl Viro mntput(&mnt->mnt); 122670fbcdf4SRam Pai } 122770fbcdf4SRam Pai } 122870fbcdf4SRam Pai 122997216be0SAl Viro static inline void namespace_lock(void) 1230e3197d83SAl Viro { 123197216be0SAl Viro down_write(&namespace_sem); 1232e3197d83SAl Viro } 1233e3197d83SAl Viro 123499b7db7bSNick Piggin /* 123548a066e7SAl Viro * mount_lock must be held 123699b7db7bSNick Piggin * namespace_sem must be held for write 123748a066e7SAl Viro * how = 0 => just this tree, don't propagate 123848a066e7SAl Viro * how = 1 => propagate; we know that nobody else has reference to any victims 123948a066e7SAl Viro * how = 2 => lazy umount 124099b7db7bSNick Piggin */ 124148a066e7SAl Viro void umount_tree(struct mount *mnt, int how) 124270fbcdf4SRam Pai { 124338129a13SAl Viro HLIST_HEAD(tmp_list); 1244315fc83eSAl Viro struct mount *p; 124538129a13SAl Viro struct mount *last = NULL; 124670fbcdf4SRam Pai 124738129a13SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 124838129a13SAl Viro hlist_del_init_rcu(&p->mnt_hash); 124938129a13SAl Viro hlist_add_head(&p->mnt_hash, &tmp_list); 125038129a13SAl Viro } 125170fbcdf4SRam Pai 125248a066e7SAl Viro if (how) 12537b8a53fdSAl Viro propagate_umount(&tmp_list); 1254a05964f3SRam Pai 125538129a13SAl Viro hlist_for_each_entry(p, &tmp_list, mnt_hash) { 12566776db3dSAl Viro list_del_init(&p->mnt_expire); 12571a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1258143c8c91SAl Viro __touch_mnt_namespace(p->mnt_ns); 125963d37a84SAl Viro p->mnt_ns = NULL; 126048a066e7SAl Viro if (how < 2) 126148a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 12626b41d536SAl Viro list_del_init(&p->mnt_child); 1263676da58dSAl Viro if (mnt_has_parent(p)) { 126484d17192SAl Viro put_mountpoint(p->mnt_mp); 1265aba809cfSAl Viro /* move the reference to mountpoint into ->mnt_ex_mountpoint */ 1266aba809cfSAl Viro p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint; 1267aba809cfSAl Viro p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt; 1268aba809cfSAl Viro p->mnt_mountpoint = p->mnt.mnt_root; 1269aba809cfSAl Viro p->mnt_parent = p; 127084d17192SAl Viro p->mnt_mp = NULL; 12717c4b93d8SAl Viro } 12720f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 127338129a13SAl Viro last = p; 12741da177e4SLinus Torvalds } 127538129a13SAl Viro if (last) { 127638129a13SAl Viro last->mnt_hash.next = unmounted.first; 127738129a13SAl Viro unmounted.first = tmp_list.first; 127838129a13SAl Viro unmounted.first->pprev = &unmounted.first; 127938129a13SAl Viro } 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 1282b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1283c35038beSAl Viro 12841ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 12851da177e4SLinus Torvalds { 12861ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 12871da177e4SLinus Torvalds int retval; 12881da177e4SLinus Torvalds 12891ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 12901da177e4SLinus Torvalds if (retval) 12911da177e4SLinus Torvalds return retval; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds /* 12941da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 12951da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 12961da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 12971da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 12981da177e4SLinus Torvalds */ 12991da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 13001ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 13011da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 13021da177e4SLinus Torvalds return -EINVAL; 13031da177e4SLinus Torvalds 1304b3e19d92SNick Piggin /* 1305b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1306b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1307b3e19d92SNick Piggin */ 1308719ea2fbSAl Viro lock_mount_hash(); 130983adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1310719ea2fbSAl Viro unlock_mount_hash(); 13111da177e4SLinus Torvalds return -EBUSY; 1312b3e19d92SNick Piggin } 1313719ea2fbSAl Viro unlock_mount_hash(); 13141da177e4SLinus Torvalds 1315863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 13161da177e4SLinus Torvalds return -EAGAIN; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds /* 13201da177e4SLinus Torvalds * If we may have to abort operations to get out of this 13211da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 13221da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 13231da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 13241da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 13251da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 13261da177e4SLinus Torvalds * about for the moment. 13271da177e4SLinus Torvalds */ 13281da177e4SLinus Torvalds 132942faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 133042faad99SAl Viro sb->s_op->umount_begin(sb); 133142faad99SAl Viro } 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds /* 13341da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 13351da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 13361da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 13371da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 13381da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 13391da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 13401da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 13411da177e4SLinus Torvalds */ 13421ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 13431da177e4SLinus Torvalds /* 13441da177e4SLinus Torvalds * Special case for "unmounting" root ... 13451da177e4SLinus Torvalds * we just try to remount it readonly. 13461da177e4SLinus Torvalds */ 13471da177e4SLinus Torvalds down_write(&sb->s_umount); 13484aa98cf7SAl Viro if (!(sb->s_flags & MS_RDONLY)) 13491da177e4SLinus Torvalds retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); 13501da177e4SLinus Torvalds up_write(&sb->s_umount); 13511da177e4SLinus Torvalds return retval; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 135497216be0SAl Viro namespace_lock(); 1355719ea2fbSAl Viro lock_mount_hash(); 13565addc5ddSAl Viro event++; 13571da177e4SLinus Torvalds 135848a066e7SAl Viro if (flags & MNT_DETACH) { 135948a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 136048a066e7SAl Viro umount_tree(mnt, 2); 136148a066e7SAl Viro retval = 0; 136248a066e7SAl Viro } else { 1363b54b9be7SAl Viro shrink_submounts(mnt); 13641da177e4SLinus Torvalds retval = -EBUSY; 136548a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 13661a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1367328e6d90SAl Viro umount_tree(mnt, 1); 13681da177e4SLinus Torvalds retval = 0; 13691da177e4SLinus Torvalds } 137048a066e7SAl Viro } 1371719ea2fbSAl Viro unlock_mount_hash(); 1372e3197d83SAl Viro namespace_unlock(); 13731da177e4SLinus Torvalds return retval; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /* 13779b40bc90SAl Viro * Is the caller allowed to modify his namespace? 13789b40bc90SAl Viro */ 13799b40bc90SAl Viro static inline bool may_mount(void) 13809b40bc90SAl Viro { 13819b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 13829b40bc90SAl Viro } 13839b40bc90SAl Viro 13849b40bc90SAl Viro /* 13851da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 13861da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 13871da177e4SLinus Torvalds * 13881da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 13891da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 13901da177e4SLinus Torvalds */ 13911da177e4SLinus Torvalds 1392bdc480e3SHeiko Carstens SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 13931da177e4SLinus Torvalds { 13942d8f3038SAl Viro struct path path; 1395900148dcSAl Viro struct mount *mnt; 13961da177e4SLinus Torvalds int retval; 1397db1f05bbSMiklos Szeredi int lookup_flags = 0; 13981da177e4SLinus Torvalds 1399db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1400db1f05bbSMiklos Szeredi return -EINVAL; 1401db1f05bbSMiklos Szeredi 14029b40bc90SAl Viro if (!may_mount()) 14039b40bc90SAl Viro return -EPERM; 14049b40bc90SAl Viro 1405db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1406db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1407db1f05bbSMiklos Szeredi 1408197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 14091da177e4SLinus Torvalds if (retval) 14101da177e4SLinus Torvalds goto out; 1411900148dcSAl Viro mnt = real_mount(path.mnt); 14121da177e4SLinus Torvalds retval = -EINVAL; 14132d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 14141da177e4SLinus Torvalds goto dput_and_out; 1415143c8c91SAl Viro if (!check_mnt(mnt)) 14161da177e4SLinus Torvalds goto dput_and_out; 14175ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 14185ff9d8a6SEric W. Biederman goto dput_and_out; 14191da177e4SLinus Torvalds 1420900148dcSAl Viro retval = do_umount(mnt, flags); 14211da177e4SLinus Torvalds dput_and_out: 1422429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 14232d8f3038SAl Viro dput(path.dentry); 1424900148dcSAl Viro mntput_no_expire(mnt); 14251da177e4SLinus Torvalds out: 14261da177e4SLinus Torvalds return retval; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds /* 14321da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 14331da177e4SLinus Torvalds */ 1434bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 14351da177e4SLinus Torvalds { 14361da177e4SLinus Torvalds return sys_umount(name, 0); 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds #endif 14401da177e4SLinus Torvalds 14414ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 14428823c079SEric W. Biederman { 14434ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 14444ce5d2b1SEric W. Biederman struct inode *inode = dentry->d_inode; 14450bb80f24SDavid Howells struct proc_ns *ei; 14468823c079SEric W. Biederman 14478823c079SEric W. Biederman if (!proc_ns_inode(inode)) 14488823c079SEric W. Biederman return false; 14498823c079SEric W. Biederman 14500bb80f24SDavid Howells ei = get_proc_ns(inode); 14518823c079SEric W. Biederman if (ei->ns_ops != &mntns_operations) 14528823c079SEric W. Biederman return false; 14538823c079SEric W. Biederman 14544ce5d2b1SEric W. Biederman return true; 14554ce5d2b1SEric W. Biederman } 14564ce5d2b1SEric W. Biederman 14574ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 14584ce5d2b1SEric W. Biederman { 14594ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 14604ce5d2b1SEric W. Biederman * mount namespace loop? 14614ce5d2b1SEric W. Biederman */ 14624ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 14634ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 14644ce5d2b1SEric W. Biederman return false; 14654ce5d2b1SEric W. Biederman 14664ce5d2b1SEric W. Biederman mnt_ns = get_proc_ns(dentry->d_inode)->ns; 14678823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 14688823c079SEric W. Biederman } 14698823c079SEric W. Biederman 147087129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 147136341f64SRam Pai int flag) 14721da177e4SLinus Torvalds { 147384d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 14741da177e4SLinus Torvalds 14754ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 14764ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 14774ce5d2b1SEric W. Biederman 14784ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1479be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 14809676f0c6SRam Pai 148136341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1482be34d1a3SDavid Howells if (IS_ERR(q)) 1483be34d1a3SDavid Howells return q; 1484be34d1a3SDavid Howells 14855ff9d8a6SEric W. Biederman q->mnt.mnt_flags &= ~MNT_LOCKED; 1486a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds p = mnt; 14896b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1490315fc83eSAl Viro struct mount *s; 14917ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 14921da177e4SLinus Torvalds continue; 14931da177e4SLinus Torvalds 1494909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 14954ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 14964ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 14974ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 14984ce5d2b1SEric W. Biederman continue; 14994ce5d2b1SEric W. Biederman } 15004ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 15014ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 15029676f0c6SRam Pai s = skip_mnt_tree(s); 15039676f0c6SRam Pai continue; 15049676f0c6SRam Pai } 15050714a533SAl Viro while (p != s->mnt_parent) { 15060714a533SAl Viro p = p->mnt_parent; 15070714a533SAl Viro q = q->mnt_parent; 15081da177e4SLinus Torvalds } 150987129cc0SAl Viro p = s; 151084d17192SAl Viro parent = q; 151187129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1512be34d1a3SDavid Howells if (IS_ERR(q)) 1513be34d1a3SDavid Howells goto out; 1514719ea2fbSAl Viro lock_mount_hash(); 15151a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 151684d17192SAl Viro attach_mnt(q, parent, p->mnt_mp); 1517719ea2fbSAl Viro unlock_mount_hash(); 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds return res; 1521be34d1a3SDavid Howells out: 15221da177e4SLinus Torvalds if (res) { 1523719ea2fbSAl Viro lock_mount_hash(); 1524328e6d90SAl Viro umount_tree(res, 0); 1525719ea2fbSAl Viro unlock_mount_hash(); 15261da177e4SLinus Torvalds } 1527be34d1a3SDavid Howells return q; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 1530be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1531be34d1a3SDavid Howells 1532589ff870SAl Viro struct vfsmount *collect_mounts(struct path *path) 15338aec0809SAl Viro { 1534cb338d06SAl Viro struct mount *tree; 153597216be0SAl Viro namespace_lock(); 153687129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 153787129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1538328e6d90SAl Viro namespace_unlock(); 1539be34d1a3SDavid Howells if (IS_ERR(tree)) 154052e220d3SDan Carpenter return ERR_CAST(tree); 1541be34d1a3SDavid Howells return &tree->mnt; 15428aec0809SAl Viro } 15438aec0809SAl Viro 15448aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 15458aec0809SAl Viro { 154697216be0SAl Viro namespace_lock(); 1547719ea2fbSAl Viro lock_mount_hash(); 1548328e6d90SAl Viro umount_tree(real_mount(mnt), 0); 1549719ea2fbSAl Viro unlock_mount_hash(); 15503ab6abeeSAl Viro namespace_unlock(); 15518aec0809SAl Viro } 15528aec0809SAl Viro 15531f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 15541f707137SAl Viro struct vfsmount *root) 15551f707137SAl Viro { 15561a4eeaf2SAl Viro struct mount *mnt; 15571f707137SAl Viro int res = f(root, arg); 15581f707137SAl Viro if (res) 15591f707137SAl Viro return res; 15601a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 15611a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 15621f707137SAl Viro if (res) 15631f707137SAl Viro return res; 15641f707137SAl Viro } 15651f707137SAl Viro return 0; 15661f707137SAl Viro } 15671f707137SAl Viro 15684b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1569719f5d7fSMiklos Szeredi { 1570315fc83eSAl Viro struct mount *p; 1571719f5d7fSMiklos Szeredi 1572909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1573fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 15744b8b21f4SAl Viro mnt_release_group_id(p); 1575719f5d7fSMiklos Szeredi } 1576719f5d7fSMiklos Szeredi } 1577719f5d7fSMiklos Szeredi 15784b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1579719f5d7fSMiklos Szeredi { 1580315fc83eSAl Viro struct mount *p; 1581719f5d7fSMiklos Szeredi 1582909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1583fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 15844b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1585719f5d7fSMiklos Szeredi if (err) { 15864b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1587719f5d7fSMiklos Szeredi return err; 1588719f5d7fSMiklos Szeredi } 1589719f5d7fSMiklos Szeredi } 1590719f5d7fSMiklos Szeredi } 1591719f5d7fSMiklos Szeredi 1592719f5d7fSMiklos Szeredi return 0; 1593719f5d7fSMiklos Szeredi } 1594719f5d7fSMiklos Szeredi 1595b90fa9aeSRam Pai /* 1596b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1597b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 159821444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 159921444403SRam Pai * store the parent mount and mountpoint dentry. 160021444403SRam Pai * (done when source_mnt is moved) 1601b90fa9aeSRam Pai * 1602b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1603b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 16049676f0c6SRam Pai * --------------------------------------------------------------------------- 1605b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 16069676f0c6SRam Pai * |************************************************************************** 16079676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 16089676f0c6SRam Pai * | dest | | | | | 16099676f0c6SRam Pai * | | | | | | | 16109676f0c6SRam Pai * | v | | | | | 16119676f0c6SRam Pai * |************************************************************************** 16129676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 16135afe0022SRam Pai * | | | | | | 16149676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 16159676f0c6SRam Pai * *************************************************************************** 1616b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1617b90fa9aeSRam Pai * destination mount. 1618b90fa9aeSRam Pai * 1619b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1620b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1621b90fa9aeSRam Pai * the peer group of the source mount. 1622b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1623b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1624b90fa9aeSRam Pai * mount. 16255afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 16265afe0022SRam Pai * of the destination mount and the cloned mount is made slave 16275afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 16285afe0022SRam Pai * is marked as 'shared and slave'. 16295afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 16305afe0022SRam Pai * source mount. 16315afe0022SRam Pai * 16329676f0c6SRam Pai * --------------------------------------------------------------------------- 163321444403SRam Pai * | MOVE MOUNT OPERATION | 16349676f0c6SRam Pai * |************************************************************************** 16359676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 16369676f0c6SRam Pai * | dest | | | | | 16379676f0c6SRam Pai * | | | | | | | 16389676f0c6SRam Pai * | v | | | | | 16399676f0c6SRam Pai * |************************************************************************** 16409676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 16415afe0022SRam Pai * | | | | | | 16429676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 16439676f0c6SRam Pai * *************************************************************************** 16445afe0022SRam Pai * 16455afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 16465afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 164721444403SRam Pai * (+*) the mount is moved to the destination. 16485afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 16495afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 16505afe0022SRam Pai * the mount is marked as 'shared and slave'. 16515afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 1652b90fa9aeSRam Pai * 1653b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 1654b90fa9aeSRam Pai * applied to each mount in the tree. 1655b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 1656b90fa9aeSRam Pai * in allocations. 1657b90fa9aeSRam Pai */ 16580fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 165984d17192SAl Viro struct mount *dest_mnt, 166084d17192SAl Viro struct mountpoint *dest_mp, 166184d17192SAl Viro struct path *parent_path) 1662b90fa9aeSRam Pai { 166338129a13SAl Viro HLIST_HEAD(tree_list); 1664315fc83eSAl Viro struct mount *child, *p; 166538129a13SAl Viro struct hlist_node *n; 1666719f5d7fSMiklos Szeredi int err; 1667b90fa9aeSRam Pai 1668fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 16690fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 1670719f5d7fSMiklos Szeredi if (err) 1671719f5d7fSMiklos Szeredi goto out; 167284d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 1673f2ebb3a9SAl Viro lock_mount_hash(); 1674719f5d7fSMiklos Szeredi if (err) 1675719f5d7fSMiklos Szeredi goto out_cleanup_ids; 1676909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 16770f0afb1dSAl Viro set_mnt_shared(p); 16780b1b901bSAl Viro } else { 16790b1b901bSAl Viro lock_mount_hash(); 1680b90fa9aeSRam Pai } 16811a390689SAl Viro if (parent_path) { 16820fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 168384d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 1684143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 168521444403SRam Pai } else { 168684d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 16871d6a32acSAl Viro commit_tree(source_mnt, NULL); 168821444403SRam Pai } 1689b90fa9aeSRam Pai 169038129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 16911d6a32acSAl Viro struct mount *q; 169238129a13SAl Viro hlist_del_init(&child->mnt_hash); 16931d6a32acSAl Viro q = __lookup_mnt_last(&child->mnt_parent->mnt, 16941d6a32acSAl Viro child->mnt_mountpoint); 16951d6a32acSAl Viro commit_tree(child, q); 1696b90fa9aeSRam Pai } 1697719ea2fbSAl Viro unlock_mount_hash(); 169899b7db7bSNick Piggin 1699b90fa9aeSRam Pai return 0; 1700719f5d7fSMiklos Szeredi 1701719f5d7fSMiklos Szeredi out_cleanup_ids: 1702f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 1703f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 1704f2ebb3a9SAl Viro umount_tree(child, 0); 1705f2ebb3a9SAl Viro } 1706f2ebb3a9SAl Viro unlock_mount_hash(); 17070fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 1708719f5d7fSMiklos Szeredi out: 1709719f5d7fSMiklos Szeredi return err; 1710b90fa9aeSRam Pai } 1711b90fa9aeSRam Pai 171284d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 1713b12cea91SAl Viro { 1714b12cea91SAl Viro struct vfsmount *mnt; 171584d17192SAl Viro struct dentry *dentry = path->dentry; 1716b12cea91SAl Viro retry: 171784d17192SAl Viro mutex_lock(&dentry->d_inode->i_mutex); 171884d17192SAl Viro if (unlikely(cant_mount(dentry))) { 171984d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 172084d17192SAl Viro return ERR_PTR(-ENOENT); 1721b12cea91SAl Viro } 172297216be0SAl Viro namespace_lock(); 1723b12cea91SAl Viro mnt = lookup_mnt(path); 172484d17192SAl Viro if (likely(!mnt)) { 172584d17192SAl Viro struct mountpoint *mp = new_mountpoint(dentry); 172684d17192SAl Viro if (IS_ERR(mp)) { 172797216be0SAl Viro namespace_unlock(); 172884d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 172984d17192SAl Viro return mp; 173084d17192SAl Viro } 173184d17192SAl Viro return mp; 173284d17192SAl Viro } 173397216be0SAl Viro namespace_unlock(); 1734b12cea91SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 1735b12cea91SAl Viro path_put(path); 1736b12cea91SAl Viro path->mnt = mnt; 173784d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 1738b12cea91SAl Viro goto retry; 1739b12cea91SAl Viro } 1740b12cea91SAl Viro 174184d17192SAl Viro static void unlock_mount(struct mountpoint *where) 1742b12cea91SAl Viro { 174384d17192SAl Viro struct dentry *dentry = where->m_dentry; 174484d17192SAl Viro put_mountpoint(where); 1745328e6d90SAl Viro namespace_unlock(); 174684d17192SAl Viro mutex_unlock(&dentry->d_inode->i_mutex); 1747b12cea91SAl Viro } 1748b12cea91SAl Viro 174984d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 17501da177e4SLinus Torvalds { 175195bc5f25SAl Viro if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER) 17521da177e4SLinus Torvalds return -EINVAL; 17531da177e4SLinus Torvalds 175484d17192SAl Viro if (S_ISDIR(mp->m_dentry->d_inode->i_mode) != 175595bc5f25SAl Viro S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode)) 17561da177e4SLinus Torvalds return -ENOTDIR; 17571da177e4SLinus Torvalds 175884d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /* 17627a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 17637a2e8a8fSValerie Aurora */ 17647a2e8a8fSValerie Aurora 17657a2e8a8fSValerie Aurora static int flags_to_propagation_type(int flags) 17667a2e8a8fSValerie Aurora { 17677c6e984dSRoman Borisov int type = flags & ~(MS_REC | MS_SILENT); 17687a2e8a8fSValerie Aurora 17697a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 17707a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 17717a2e8a8fSValerie Aurora return 0; 17727a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 17737a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 17747a2e8a8fSValerie Aurora return 0; 17757a2e8a8fSValerie Aurora return type; 17767a2e8a8fSValerie Aurora } 17777a2e8a8fSValerie Aurora 17787a2e8a8fSValerie Aurora /* 177907b20889SRam Pai * recursively change the type of the mountpoint. 178007b20889SRam Pai */ 17810a0d8a46SAl Viro static int do_change_type(struct path *path, int flag) 178207b20889SRam Pai { 1783315fc83eSAl Viro struct mount *m; 17844b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 178507b20889SRam Pai int recurse = flag & MS_REC; 17867a2e8a8fSValerie Aurora int type; 1787719f5d7fSMiklos Szeredi int err = 0; 178807b20889SRam Pai 17892d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 179007b20889SRam Pai return -EINVAL; 179107b20889SRam Pai 17927a2e8a8fSValerie Aurora type = flags_to_propagation_type(flag); 17937a2e8a8fSValerie Aurora if (!type) 17947a2e8a8fSValerie Aurora return -EINVAL; 17957a2e8a8fSValerie Aurora 179697216be0SAl Viro namespace_lock(); 1797719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 1798719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 1799719f5d7fSMiklos Szeredi if (err) 1800719f5d7fSMiklos Szeredi goto out_unlock; 1801719f5d7fSMiklos Szeredi } 1802719f5d7fSMiklos Szeredi 1803719ea2fbSAl Viro lock_mount_hash(); 1804909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 18050f0afb1dSAl Viro change_mnt_propagation(m, type); 1806719ea2fbSAl Viro unlock_mount_hash(); 1807719f5d7fSMiklos Szeredi 1808719f5d7fSMiklos Szeredi out_unlock: 180997216be0SAl Viro namespace_unlock(); 1810719f5d7fSMiklos Szeredi return err; 181107b20889SRam Pai } 181207b20889SRam Pai 18135ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 18145ff9d8a6SEric W. Biederman { 18155ff9d8a6SEric W. Biederman struct mount *child; 18165ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 18175ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 18185ff9d8a6SEric W. Biederman continue; 18195ff9d8a6SEric W. Biederman 18205ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 18215ff9d8a6SEric W. Biederman return true; 18225ff9d8a6SEric W. Biederman } 18235ff9d8a6SEric W. Biederman return false; 18245ff9d8a6SEric W. Biederman } 18255ff9d8a6SEric W. Biederman 182607b20889SRam Pai /* 18271da177e4SLinus Torvalds * do loopback mount. 18281da177e4SLinus Torvalds */ 1829808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 18302dafe1c4SEric Sandeen int recurse) 18311da177e4SLinus Torvalds { 18322d92ab3cSAl Viro struct path old_path; 183384d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 183484d17192SAl Viro struct mountpoint *mp; 183557eccb83SAl Viro int err; 18361da177e4SLinus Torvalds if (!old_name || !*old_name) 18371da177e4SLinus Torvalds return -EINVAL; 1838815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 18391da177e4SLinus Torvalds if (err) 18401da177e4SLinus Torvalds return err; 18411da177e4SLinus Torvalds 18428823c079SEric W. Biederman err = -EINVAL; 18434ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 18448823c079SEric W. Biederman goto out; 18458823c079SEric W. Biederman 184684d17192SAl Viro mp = lock_mount(path); 184784d17192SAl Viro err = PTR_ERR(mp); 184884d17192SAl Viro if (IS_ERR(mp)) 18499676f0c6SRam Pai goto out; 18509676f0c6SRam Pai 185187129cc0SAl Viro old = real_mount(old_path.mnt); 185284d17192SAl Viro parent = real_mount(path->mnt); 185387129cc0SAl Viro 1854b12cea91SAl Viro err = -EINVAL; 1855fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 1856b12cea91SAl Viro goto out2; 1857b12cea91SAl Viro 185884d17192SAl Viro if (!check_mnt(parent) || !check_mnt(old)) 1859b12cea91SAl Viro goto out2; 1860ccd48bc7SAl Viro 18615ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 18625ff9d8a6SEric W. Biederman goto out2; 18635ff9d8a6SEric W. Biederman 18641da177e4SLinus Torvalds if (recurse) 18654ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 18661da177e4SLinus Torvalds else 186787129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 18681da177e4SLinus Torvalds 1869be34d1a3SDavid Howells if (IS_ERR(mnt)) { 1870be34d1a3SDavid Howells err = PTR_ERR(mnt); 1871e9c5d8a5SAndrey Vagin goto out2; 1872be34d1a3SDavid Howells } 1873ccd48bc7SAl Viro 18745ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 18755ff9d8a6SEric W. Biederman 187684d17192SAl Viro err = graft_tree(mnt, parent, mp); 18771da177e4SLinus Torvalds if (err) { 1878719ea2fbSAl Viro lock_mount_hash(); 1879328e6d90SAl Viro umount_tree(mnt, 0); 1880719ea2fbSAl Viro unlock_mount_hash(); 18815b83d2c5SRam Pai } 1882b12cea91SAl Viro out2: 188384d17192SAl Viro unlock_mount(mp); 1884ccd48bc7SAl Viro out: 18852d92ab3cSAl Viro path_put(&old_path); 18861da177e4SLinus Torvalds return err; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 18892e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 18902e4b7fcdSDave Hansen { 18912e4b7fcdSDave Hansen int error = 0; 18922e4b7fcdSDave Hansen int readonly_request = 0; 18932e4b7fcdSDave Hansen 18942e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 18952e4b7fcdSDave Hansen readonly_request = 1; 18962e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 18972e4b7fcdSDave Hansen return 0; 18982e4b7fcdSDave Hansen 189990563b19SEric W. Biederman if (mnt->mnt_flags & MNT_LOCK_READONLY) 190090563b19SEric W. Biederman return -EPERM; 190190563b19SEric W. Biederman 19022e4b7fcdSDave Hansen if (readonly_request) 190383adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 19042e4b7fcdSDave Hansen else 190583adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 19062e4b7fcdSDave Hansen return error; 19072e4b7fcdSDave Hansen } 19082e4b7fcdSDave Hansen 19091da177e4SLinus Torvalds /* 19101da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 19111da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 19121da177e4SLinus Torvalds * on it - tough luck. 19131da177e4SLinus Torvalds */ 19140a0d8a46SAl Viro static int do_remount(struct path *path, int flags, int mnt_flags, 19151da177e4SLinus Torvalds void *data) 19161da177e4SLinus Torvalds { 19171da177e4SLinus Torvalds int err; 19182d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 1919143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 19201da177e4SLinus Torvalds 1921143c8c91SAl Viro if (!check_mnt(mnt)) 19221da177e4SLinus Torvalds return -EINVAL; 19231da177e4SLinus Torvalds 19242d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 19251da177e4SLinus Torvalds return -EINVAL; 19261da177e4SLinus Torvalds 1927ff36fe2cSEric Paris err = security_sb_remount(sb, data); 1928ff36fe2cSEric Paris if (err) 1929ff36fe2cSEric Paris return err; 1930ff36fe2cSEric Paris 19311da177e4SLinus Torvalds down_write(&sb->s_umount); 19322e4b7fcdSDave Hansen if (flags & MS_BIND) 19332d92ab3cSAl Viro err = change_mount_flags(path->mnt, flags); 193457eccb83SAl Viro else if (!capable(CAP_SYS_ADMIN)) 193557eccb83SAl Viro err = -EPERM; 19364aa98cf7SAl Viro else 19371da177e4SLinus Torvalds err = do_remount_sb(sb, flags, data, 0); 19387b43a79fSAl Viro if (!err) { 1939719ea2fbSAl Viro lock_mount_hash(); 1940143c8c91SAl Viro mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK; 1941143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 1942143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 1943719ea2fbSAl Viro unlock_mount_hash(); 19440e55a7ccSDan Williams } 19456339dab8SAl Viro up_write(&sb->s_umount); 19461da177e4SLinus Torvalds return err; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 1949cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 19509676f0c6SRam Pai { 1951315fc83eSAl Viro struct mount *p; 1952909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 1953fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 19549676f0c6SRam Pai return 1; 19559676f0c6SRam Pai } 19569676f0c6SRam Pai return 0; 19579676f0c6SRam Pai } 19589676f0c6SRam Pai 1959808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 19601da177e4SLinus Torvalds { 19612d92ab3cSAl Viro struct path old_path, parent_path; 1962676da58dSAl Viro struct mount *p; 19630fb54e50SAl Viro struct mount *old; 196484d17192SAl Viro struct mountpoint *mp; 196557eccb83SAl Viro int err; 19661da177e4SLinus Torvalds if (!old_name || !*old_name) 19671da177e4SLinus Torvalds return -EINVAL; 19682d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 19691da177e4SLinus Torvalds if (err) 19701da177e4SLinus Torvalds return err; 19711da177e4SLinus Torvalds 197284d17192SAl Viro mp = lock_mount(path); 197384d17192SAl Viro err = PTR_ERR(mp); 197484d17192SAl Viro if (IS_ERR(mp)) 1975cc53ce53SDavid Howells goto out; 1976cc53ce53SDavid Howells 1977143c8c91SAl Viro old = real_mount(old_path.mnt); 1978fc7be130SAl Viro p = real_mount(path->mnt); 1979143c8c91SAl Viro 19801da177e4SLinus Torvalds err = -EINVAL; 1981fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 19821da177e4SLinus Torvalds goto out1; 19831da177e4SLinus Torvalds 19845ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 19855ff9d8a6SEric W. Biederman goto out1; 19865ff9d8a6SEric W. Biederman 19871da177e4SLinus Torvalds err = -EINVAL; 19882d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 198921444403SRam Pai goto out1; 19901da177e4SLinus Torvalds 1991676da58dSAl Viro if (!mnt_has_parent(old)) 199221444403SRam Pai goto out1; 19931da177e4SLinus Torvalds 19942d92ab3cSAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode) != 19952d92ab3cSAl Viro S_ISDIR(old_path.dentry->d_inode->i_mode)) 199621444403SRam Pai goto out1; 199721444403SRam Pai /* 199821444403SRam Pai * Don't move a mount residing in a shared parent. 199921444403SRam Pai */ 2000fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 200121444403SRam Pai goto out1; 20029676f0c6SRam Pai /* 20039676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 20049676f0c6SRam Pai * mount which is shared. 20059676f0c6SRam Pai */ 2006fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 20079676f0c6SRam Pai goto out1; 20081da177e4SLinus Torvalds err = -ELOOP; 2009fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2010676da58dSAl Viro if (p == old) 201121444403SRam Pai goto out1; 20121da177e4SLinus Torvalds 201384d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 20144ac91378SJan Blunck if (err) 201521444403SRam Pai goto out1; 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 20181da177e4SLinus Torvalds * automatically */ 20196776db3dSAl Viro list_del_init(&old->mnt_expire); 20201da177e4SLinus Torvalds out1: 202184d17192SAl Viro unlock_mount(mp); 20221da177e4SLinus Torvalds out: 20231da177e4SLinus Torvalds if (!err) 20241a390689SAl Viro path_put(&parent_path); 20252d92ab3cSAl Viro path_put(&old_path); 20261da177e4SLinus Torvalds return err; 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds 20299d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 20309d412a43SAl Viro { 20319d412a43SAl Viro int err; 20329d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 20339d412a43SAl Viro if (subtype) { 20349d412a43SAl Viro subtype++; 20359d412a43SAl Viro err = -EINVAL; 20369d412a43SAl Viro if (!subtype[0]) 20379d412a43SAl Viro goto err; 20389d412a43SAl Viro } else 20399d412a43SAl Viro subtype = ""; 20409d412a43SAl Viro 20419d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 20429d412a43SAl Viro err = -ENOMEM; 20439d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 20449d412a43SAl Viro goto err; 20459d412a43SAl Viro return mnt; 20469d412a43SAl Viro 20479d412a43SAl Viro err: 20489d412a43SAl Viro mntput(mnt); 20499d412a43SAl Viro return ERR_PTR(err); 20509d412a43SAl Viro } 20519d412a43SAl Viro 20529d412a43SAl Viro /* 20539d412a43SAl Viro * add a mount into a namespace's mount tree 20549d412a43SAl Viro */ 205595bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 20569d412a43SAl Viro { 205784d17192SAl Viro struct mountpoint *mp; 205884d17192SAl Viro struct mount *parent; 20599d412a43SAl Viro int err; 20609d412a43SAl Viro 2061f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 20629d412a43SAl Viro 206384d17192SAl Viro mp = lock_mount(path); 206484d17192SAl Viro if (IS_ERR(mp)) 206584d17192SAl Viro return PTR_ERR(mp); 20669d412a43SAl Viro 206784d17192SAl Viro parent = real_mount(path->mnt); 20689d412a43SAl Viro err = -EINVAL; 206984d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2070156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2071156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 20729d412a43SAl Viro goto unlock; 2073156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 207484d17192SAl Viro if (!parent->mnt_ns) 2075156cacb1SAl Viro goto unlock; 2076156cacb1SAl Viro } 20779d412a43SAl Viro 20789d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 20799d412a43SAl Viro err = -EBUSY; 208095bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 20819d412a43SAl Viro path->mnt->mnt_root == path->dentry) 20829d412a43SAl Viro goto unlock; 20839d412a43SAl Viro 20849d412a43SAl Viro err = -EINVAL; 208595bc5f25SAl Viro if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode)) 20869d412a43SAl Viro goto unlock; 20879d412a43SAl Viro 208895bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 208984d17192SAl Viro err = graft_tree(newmnt, parent, mp); 20909d412a43SAl Viro 20919d412a43SAl Viro unlock: 209284d17192SAl Viro unlock_mount(mp); 20939d412a43SAl Viro return err; 20949d412a43SAl Viro } 2095b1e75df4SAl Viro 20961da177e4SLinus Torvalds /* 20971da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 20981da177e4SLinus Torvalds * namespace's tree 20991da177e4SLinus Torvalds */ 21000c55cfc4SEric W. Biederman static int do_new_mount(struct path *path, const char *fstype, int flags, 2101808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 21021da177e4SLinus Torvalds { 21030c55cfc4SEric W. Biederman struct file_system_type *type; 21049b40bc90SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 21051da177e4SLinus Torvalds struct vfsmount *mnt; 210615f9a3f3SAl Viro int err; 21071da177e4SLinus Torvalds 21080c55cfc4SEric W. Biederman if (!fstype) 21091da177e4SLinus Torvalds return -EINVAL; 21101da177e4SLinus Torvalds 21110c55cfc4SEric W. Biederman type = get_fs_type(fstype); 21120c55cfc4SEric W. Biederman if (!type) 21130c55cfc4SEric W. Biederman return -ENODEV; 21140c55cfc4SEric W. Biederman 21150c55cfc4SEric W. Biederman if (user_ns != &init_user_ns) { 21160c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_MOUNT)) { 21170c55cfc4SEric W. Biederman put_filesystem(type); 21180c55cfc4SEric W. Biederman return -EPERM; 21190c55cfc4SEric W. Biederman } 21200c55cfc4SEric W. Biederman /* Only in special cases allow devices from mounts 21210c55cfc4SEric W. Biederman * created outside the initial user namespace. 21220c55cfc4SEric W. Biederman */ 21230c55cfc4SEric W. Biederman if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) { 21240c55cfc4SEric W. Biederman flags |= MS_NODEV; 21250c55cfc4SEric W. Biederman mnt_flags |= MNT_NODEV; 21260c55cfc4SEric W. Biederman } 21270c55cfc4SEric W. Biederman } 21280c55cfc4SEric W. Biederman 21290c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, flags, name, data); 21300c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 21310c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 21320c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 21330c55cfc4SEric W. Biederman 21340c55cfc4SEric W. Biederman put_filesystem(type); 21351da177e4SLinus Torvalds if (IS_ERR(mnt)) 21361da177e4SLinus Torvalds return PTR_ERR(mnt); 21371da177e4SLinus Torvalds 213895bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 213915f9a3f3SAl Viro if (err) 214015f9a3f3SAl Viro mntput(mnt); 214115f9a3f3SAl Viro return err; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 214419a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 214519a167afSAl Viro { 21466776db3dSAl Viro struct mount *mnt = real_mount(m); 214719a167afSAl Viro int err; 214819a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 214919a167afSAl Viro * expired before we get a chance to add it 215019a167afSAl Viro */ 21516776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 215219a167afSAl Viro 215319a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 215419a167afSAl Viro m->mnt_root == path->dentry) { 2155b1e75df4SAl Viro err = -ELOOP; 2156b1e75df4SAl Viro goto fail; 215719a167afSAl Viro } 215819a167afSAl Viro 215995bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2160b1e75df4SAl Viro if (!err) 2161b1e75df4SAl Viro return 0; 2162b1e75df4SAl Viro fail: 2163b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 21646776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 216597216be0SAl Viro namespace_lock(); 21666776db3dSAl Viro list_del_init(&mnt->mnt_expire); 216797216be0SAl Viro namespace_unlock(); 216819a167afSAl Viro } 2169b1e75df4SAl Viro mntput(m); 2170b1e75df4SAl Viro mntput(m); 217119a167afSAl Viro return err; 217219a167afSAl Viro } 217319a167afSAl Viro 2174ea5b778aSDavid Howells /** 2175ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2176ea5b778aSDavid Howells * @mnt: The mount to list. 2177ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2178ea5b778aSDavid Howells */ 2179ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2180ea5b778aSDavid Howells { 218197216be0SAl Viro namespace_lock(); 2182ea5b778aSDavid Howells 21836776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2184ea5b778aSDavid Howells 218597216be0SAl Viro namespace_unlock(); 2186ea5b778aSDavid Howells } 2187ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2188ea5b778aSDavid Howells 2189ea5b778aSDavid Howells /* 21901da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 21911da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 21921da177e4SLinus Torvalds * here 21931da177e4SLinus Torvalds */ 21941da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 21951da177e4SLinus Torvalds { 2196761d5c38SAl Viro struct mount *mnt, *next; 21971da177e4SLinus Torvalds LIST_HEAD(graveyard); 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds if (list_empty(mounts)) 22001da177e4SLinus Torvalds return; 22011da177e4SLinus Torvalds 220297216be0SAl Viro namespace_lock(); 2203719ea2fbSAl Viro lock_mount_hash(); 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 22061da177e4SLinus Torvalds * following criteria: 22071da177e4SLinus Torvalds * - only referenced by its parent vfsmount 22081da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 22091da177e4SLinus Torvalds * cleared by mntput()) 22101da177e4SLinus Torvalds */ 22116776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2212863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 22131ab59738SAl Viro propagate_mount_busy(mnt, 1)) 22141da177e4SLinus Torvalds continue; 22156776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 22161da177e4SLinus Torvalds } 2217bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 22186776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2219143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2220328e6d90SAl Viro umount_tree(mnt, 1); 2221bcc5c7d2SAl Viro } 2222719ea2fbSAl Viro unlock_mount_hash(); 22233ab6abeeSAl Viro namespace_unlock(); 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds /* 22295528f911STrond Myklebust * Ripoff of 'select_parent()' 22305528f911STrond Myklebust * 22315528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 22325528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 22335528f911STrond Myklebust */ 2234692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 22355528f911STrond Myklebust { 2236692afc31SAl Viro struct mount *this_parent = parent; 22375528f911STrond Myklebust struct list_head *next; 22385528f911STrond Myklebust int found = 0; 22395528f911STrond Myklebust 22405528f911STrond Myklebust repeat: 22416b41d536SAl Viro next = this_parent->mnt_mounts.next; 22425528f911STrond Myklebust resume: 22436b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 22445528f911STrond Myklebust struct list_head *tmp = next; 22456b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 22465528f911STrond Myklebust 22475528f911STrond Myklebust next = tmp->next; 2248692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 22495528f911STrond Myklebust continue; 22505528f911STrond Myklebust /* 22515528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 22525528f911STrond Myklebust */ 22536b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 22545528f911STrond Myklebust this_parent = mnt; 22555528f911STrond Myklebust goto repeat; 22565528f911STrond Myklebust } 22575528f911STrond Myklebust 22581ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 22596776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 22605528f911STrond Myklebust found++; 22615528f911STrond Myklebust } 22625528f911STrond Myklebust } 22635528f911STrond Myklebust /* 22645528f911STrond Myklebust * All done at this level ... ascend and resume the search 22655528f911STrond Myklebust */ 22665528f911STrond Myklebust if (this_parent != parent) { 22676b41d536SAl Viro next = this_parent->mnt_child.next; 22680714a533SAl Viro this_parent = this_parent->mnt_parent; 22695528f911STrond Myklebust goto resume; 22705528f911STrond Myklebust } 22715528f911STrond Myklebust return found; 22725528f911STrond Myklebust } 22735528f911STrond Myklebust 22745528f911STrond Myklebust /* 22755528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 22765528f911STrond Myklebust * submounts of a specific parent mountpoint 227799b7db7bSNick Piggin * 227848a066e7SAl Viro * mount_lock must be held for write 22795528f911STrond Myklebust */ 2280b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 22815528f911STrond Myklebust { 22825528f911STrond Myklebust LIST_HEAD(graveyard); 2283761d5c38SAl Viro struct mount *m; 22845528f911STrond Myklebust 22855528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2286c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2287bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2288761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 22896776db3dSAl Viro mnt_expire); 2290143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2291328e6d90SAl Viro umount_tree(m, 1); 2292bcc5c7d2SAl Viro } 2293bcc5c7d2SAl Viro } 22945528f911STrond Myklebust } 22955528f911STrond Myklebust 22965528f911STrond Myklebust /* 22971da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 22981da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 22991da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 23001da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 23011da177e4SLinus Torvalds */ 2302b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2303b58fed8bSRam Pai unsigned long n) 23041da177e4SLinus Torvalds { 23051da177e4SLinus Torvalds char *t = to; 23061da177e4SLinus Torvalds const char __user *f = from; 23071da177e4SLinus Torvalds char c; 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 23101da177e4SLinus Torvalds return n; 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds while (n) { 23131da177e4SLinus Torvalds if (__get_user(c, f)) { 23141da177e4SLinus Torvalds memset(t, 0, n); 23151da177e4SLinus Torvalds break; 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds *t++ = c; 23181da177e4SLinus Torvalds f++; 23191da177e4SLinus Torvalds n--; 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds return n; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds int copy_mount_options(const void __user * data, unsigned long *where) 23251da177e4SLinus Torvalds { 23261da177e4SLinus Torvalds int i; 23271da177e4SLinus Torvalds unsigned long page; 23281da177e4SLinus Torvalds unsigned long size; 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds *where = 0; 23311da177e4SLinus Torvalds if (!data) 23321da177e4SLinus Torvalds return 0; 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 23351da177e4SLinus Torvalds return -ENOMEM; 23361da177e4SLinus Torvalds 23371da177e4SLinus Torvalds /* We only care that *some* data at the address the user 23381da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 23391da177e4SLinus Torvalds * the remainder of the page. 23401da177e4SLinus Torvalds */ 23411da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 23421da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 23431da177e4SLinus Torvalds if (size > PAGE_SIZE) 23441da177e4SLinus Torvalds size = PAGE_SIZE; 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds i = size - exact_copy_from_user((void *)page, data, size); 23471da177e4SLinus Torvalds if (!i) { 23481da177e4SLinus Torvalds free_page(page); 23491da177e4SLinus Torvalds return -EFAULT; 23501da177e4SLinus Torvalds } 23511da177e4SLinus Torvalds if (i != PAGE_SIZE) 23521da177e4SLinus Torvalds memset((char *)page + i, 0, PAGE_SIZE - i); 23531da177e4SLinus Torvalds *where = page; 23541da177e4SLinus Torvalds return 0; 23551da177e4SLinus Torvalds } 23561da177e4SLinus Torvalds 2357eca6f534SVegard Nossum int copy_mount_string(const void __user *data, char **where) 2358eca6f534SVegard Nossum { 2359eca6f534SVegard Nossum char *tmp; 2360eca6f534SVegard Nossum 2361eca6f534SVegard Nossum if (!data) { 2362eca6f534SVegard Nossum *where = NULL; 2363eca6f534SVegard Nossum return 0; 2364eca6f534SVegard Nossum } 2365eca6f534SVegard Nossum 2366eca6f534SVegard Nossum tmp = strndup_user(data, PAGE_SIZE); 2367eca6f534SVegard Nossum if (IS_ERR(tmp)) 2368eca6f534SVegard Nossum return PTR_ERR(tmp); 2369eca6f534SVegard Nossum 2370eca6f534SVegard Nossum *where = tmp; 2371eca6f534SVegard Nossum return 0; 2372eca6f534SVegard Nossum } 2373eca6f534SVegard Nossum 23741da177e4SLinus Torvalds /* 23751da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 23761da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 23771da177e4SLinus Torvalds * 23781da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 23791da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 23801da177e4SLinus Torvalds * information (or be NULL). 23811da177e4SLinus Torvalds * 23821da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 23831da177e4SLinus Torvalds * When the flags word was introduced its top half was required 23841da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 23851da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 23861da177e4SLinus Torvalds * and must be discarded. 23871da177e4SLinus Torvalds */ 2388808d4e3cSAl Viro long do_mount(const char *dev_name, const char *dir_name, 2389808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 23901da177e4SLinus Torvalds { 23912d92ab3cSAl Viro struct path path; 23921da177e4SLinus Torvalds int retval = 0; 23931da177e4SLinus Torvalds int mnt_flags = 0; 23941da177e4SLinus Torvalds 23951da177e4SLinus Torvalds /* Discard magic */ 23961da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 23971da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds /* Basic sanity checks */ 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) 24021da177e4SLinus Torvalds return -EINVAL; 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds if (data_page) 24051da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 24061da177e4SLinus Torvalds 2407a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 2408a27ab9f2STetsuo Handa retval = kern_path(dir_name, LOOKUP_FOLLOW, &path); 2409a27ab9f2STetsuo Handa if (retval) 2410a27ab9f2STetsuo Handa return retval; 2411a27ab9f2STetsuo Handa 2412a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2413a27ab9f2STetsuo Handa type_page, flags, data_page); 24140d5cadb8SAl Viro if (!retval && !may_mount()) 24150d5cadb8SAl Viro retval = -EPERM; 2416a27ab9f2STetsuo Handa if (retval) 2417a27ab9f2STetsuo Handa goto dput_out; 2418a27ab9f2STetsuo Handa 2419613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2420613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 24210a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 24220a1c01c9SMatthew Garrett 24231da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 24241da177e4SLinus Torvalds if (flags & MS_NOSUID) 24251da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 24261da177e4SLinus Torvalds if (flags & MS_NODEV) 24271da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 24281da177e4SLinus Torvalds if (flags & MS_NOEXEC) 24291da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2430fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2431fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2432fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2433fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2434d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2435d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 24362e4b7fcdSDave Hansen if (flags & MS_RDONLY) 24372e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2438fc33a7bbSChristoph Hellwig 24397a4dec53SAl Viro flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN | 2440d0adde57SMatthew Garrett MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT | 2441d0adde57SMatthew Garrett MS_STRICTATIME); 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds if (flags & MS_REMOUNT) 24442d92ab3cSAl Viro retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags, 24451da177e4SLinus Torvalds data_page); 24461da177e4SLinus Torvalds else if (flags & MS_BIND) 24472d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 24489676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 24492d92ab3cSAl Viro retval = do_change_type(&path, flags); 24501da177e4SLinus Torvalds else if (flags & MS_MOVE) 24512d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 24521da177e4SLinus Torvalds else 24532d92ab3cSAl Viro retval = do_new_mount(&path, type_page, flags, mnt_flags, 24541da177e4SLinus Torvalds dev_name, data_page); 24551da177e4SLinus Torvalds dput_out: 24562d92ab3cSAl Viro path_put(&path); 24571da177e4SLinus Torvalds return retval; 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds 2460771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2461771b1371SEric W. Biederman { 246298f842e6SEric W. Biederman proc_free_inum(ns->proc_inum); 2463771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2464771b1371SEric W. Biederman kfree(ns); 2465771b1371SEric W. Biederman } 2466771b1371SEric W. Biederman 24678823c079SEric W. Biederman /* 24688823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 24698823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 24708823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 24718823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 24728823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 24738823c079SEric W. Biederman */ 24748823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 24758823c079SEric W. Biederman 2476771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2477cf8d2c11STrond Myklebust { 2478cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 247998f842e6SEric W. Biederman int ret; 2480cf8d2c11STrond Myklebust 2481cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2482cf8d2c11STrond Myklebust if (!new_ns) 2483cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 248498f842e6SEric W. Biederman ret = proc_alloc_inum(&new_ns->proc_inum); 248598f842e6SEric W. Biederman if (ret) { 248698f842e6SEric W. Biederman kfree(new_ns); 248798f842e6SEric W. Biederman return ERR_PTR(ret); 248898f842e6SEric W. Biederman } 24898823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2490cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2491cf8d2c11STrond Myklebust new_ns->root = NULL; 2492cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2493cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2494cf8d2c11STrond Myklebust new_ns->event = 0; 2495771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2496cf8d2c11STrond Myklebust return new_ns; 2497cf8d2c11STrond Myklebust } 2498cf8d2c11STrond Myklebust 24999559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 25009559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 25011da177e4SLinus Torvalds { 25026b3286edSKirill Korotaev struct mnt_namespace *new_ns; 25037f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2504315fc83eSAl Viro struct mount *p, *q; 25059559f689SAl Viro struct mount *old; 2506cb338d06SAl Viro struct mount *new; 25077a472ef4SEric W. Biederman int copy_flags; 25081da177e4SLinus Torvalds 25099559f689SAl Viro BUG_ON(!ns); 25109559f689SAl Viro 25119559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 25129559f689SAl Viro get_mnt_ns(ns); 25139559f689SAl Viro return ns; 25149559f689SAl Viro } 25159559f689SAl Viro 25169559f689SAl Viro old = ns->root; 25179559f689SAl Viro 2518771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2519cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2520cf8d2c11STrond Myklebust return new_ns; 25211da177e4SLinus Torvalds 252297216be0SAl Viro namespace_lock(); 25231da177e4SLinus Torvalds /* First pass: copy the tree topology */ 25244ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 25259559f689SAl Viro if (user_ns != ns->user_ns) 2526132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 25277a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2528be34d1a3SDavid Howells if (IS_ERR(new)) { 2529328e6d90SAl Viro namespace_unlock(); 2530771b1371SEric W. Biederman free_mnt_ns(new_ns); 2531be34d1a3SDavid Howells return ERR_CAST(new); 25321da177e4SLinus Torvalds } 2533be08d6d2SAl Viro new_ns->root = new; 25341a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds /* 25371da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 25381da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 25391da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 25401da177e4SLinus Torvalds */ 2541909b0a88SAl Viro p = old; 2542cb338d06SAl Viro q = new; 25431da177e4SLinus Torvalds while (p) { 2544143c8c91SAl Viro q->mnt_ns = new_ns; 25459559f689SAl Viro if (new_fs) { 25469559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 25479559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2548315fc83eSAl Viro rootmnt = &p->mnt; 25491da177e4SLinus Torvalds } 25509559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 25519559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2552315fc83eSAl Viro pwdmnt = &p->mnt; 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds } 2555909b0a88SAl Viro p = next_mnt(p, old); 2556909b0a88SAl Viro q = next_mnt(q, new); 25574ce5d2b1SEric W. Biederman if (!q) 25584ce5d2b1SEric W. Biederman break; 25594ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 25604ce5d2b1SEric W. Biederman p = next_mnt(p, old); 25611da177e4SLinus Torvalds } 2562328e6d90SAl Viro namespace_unlock(); 25631da177e4SLinus Torvalds 25641da177e4SLinus Torvalds if (rootmnt) 2565f03c6599SAl Viro mntput(rootmnt); 25661da177e4SLinus Torvalds if (pwdmnt) 2567f03c6599SAl Viro mntput(pwdmnt); 25681da177e4SLinus Torvalds 2569741a2951SJANAK DESAI return new_ns; 2570741a2951SJANAK DESAI } 2571741a2951SJANAK DESAI 2572cf8d2c11STrond Myklebust /** 2573cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 2574cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 2575cf8d2c11STrond Myklebust */ 25761a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 2577cf8d2c11STrond Myklebust { 2578771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 2579cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 25801a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 25811a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 2582be08d6d2SAl Viro new_ns->root = mnt; 2583b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 2584c1334495SAl Viro } else { 25851a4eeaf2SAl Viro mntput(m); 2586cf8d2c11STrond Myklebust } 2587cf8d2c11STrond Myklebust return new_ns; 2588cf8d2c11STrond Myklebust } 2589cf8d2c11STrond Myklebust 2590ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 2591ea441d11SAl Viro { 2592ea441d11SAl Viro struct mnt_namespace *ns; 2593d31da0f0SAl Viro struct super_block *s; 2594ea441d11SAl Viro struct path path; 2595ea441d11SAl Viro int err; 2596ea441d11SAl Viro 2597ea441d11SAl Viro ns = create_mnt_ns(mnt); 2598ea441d11SAl Viro if (IS_ERR(ns)) 2599ea441d11SAl Viro return ERR_CAST(ns); 2600ea441d11SAl Viro 2601ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 2602ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 2603ea441d11SAl Viro 2604ea441d11SAl Viro put_mnt_ns(ns); 2605ea441d11SAl Viro 2606ea441d11SAl Viro if (err) 2607ea441d11SAl Viro return ERR_PTR(err); 2608ea441d11SAl Viro 2609ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 2610d31da0f0SAl Viro s = path.mnt->mnt_sb; 2611d31da0f0SAl Viro atomic_inc(&s->s_active); 2612ea441d11SAl Viro mntput(path.mnt); 2613ea441d11SAl Viro /* lock the sucker */ 2614d31da0f0SAl Viro down_write(&s->s_umount); 2615ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 2616ea441d11SAl Viro return path.dentry; 2617ea441d11SAl Viro } 2618ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 2619ea441d11SAl Viro 2620bdc480e3SHeiko Carstens SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 2621bdc480e3SHeiko Carstens char __user *, type, unsigned long, flags, void __user *, data) 26221da177e4SLinus Torvalds { 2623eca6f534SVegard Nossum int ret; 2624eca6f534SVegard Nossum char *kernel_type; 262591a27b2aSJeff Layton struct filename *kernel_dir; 2626eca6f534SVegard Nossum char *kernel_dev; 26271da177e4SLinus Torvalds unsigned long data_page; 26281da177e4SLinus Torvalds 2629eca6f534SVegard Nossum ret = copy_mount_string(type, &kernel_type); 2630eca6f534SVegard Nossum if (ret < 0) 2631eca6f534SVegard Nossum goto out_type; 26321da177e4SLinus Torvalds 2633eca6f534SVegard Nossum kernel_dir = getname(dir_name); 2634eca6f534SVegard Nossum if (IS_ERR(kernel_dir)) { 2635eca6f534SVegard Nossum ret = PTR_ERR(kernel_dir); 2636eca6f534SVegard Nossum goto out_dir; 2637eca6f534SVegard Nossum } 26381da177e4SLinus Torvalds 2639eca6f534SVegard Nossum ret = copy_mount_string(dev_name, &kernel_dev); 2640eca6f534SVegard Nossum if (ret < 0) 2641eca6f534SVegard Nossum goto out_dev; 26421da177e4SLinus Torvalds 2643eca6f534SVegard Nossum ret = copy_mount_options(data, &data_page); 2644eca6f534SVegard Nossum if (ret < 0) 2645eca6f534SVegard Nossum goto out_data; 26461da177e4SLinus Torvalds 264791a27b2aSJeff Layton ret = do_mount(kernel_dev, kernel_dir->name, kernel_type, flags, 2648eca6f534SVegard Nossum (void *) data_page); 2649eca6f534SVegard Nossum 26501da177e4SLinus Torvalds free_page(data_page); 2651eca6f534SVegard Nossum out_data: 2652eca6f534SVegard Nossum kfree(kernel_dev); 2653eca6f534SVegard Nossum out_dev: 2654eca6f534SVegard Nossum putname(kernel_dir); 2655eca6f534SVegard Nossum out_dir: 2656eca6f534SVegard Nossum kfree(kernel_type); 2657eca6f534SVegard Nossum out_type: 2658eca6f534SVegard Nossum return ret; 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds /* 2662afac7cbaSAl Viro * Return true if path is reachable from root 2663afac7cbaSAl Viro * 266448a066e7SAl Viro * namespace_sem or mount_lock is held 2665afac7cbaSAl Viro */ 2666643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 2667afac7cbaSAl Viro const struct path *root) 2668afac7cbaSAl Viro { 2669643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 2670a73324daSAl Viro dentry = mnt->mnt_mountpoint; 26710714a533SAl Viro mnt = mnt->mnt_parent; 2672afac7cbaSAl Viro } 2673643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 2674afac7cbaSAl Viro } 2675afac7cbaSAl Viro 2676afac7cbaSAl Viro int path_is_under(struct path *path1, struct path *path2) 2677afac7cbaSAl Viro { 2678afac7cbaSAl Viro int res; 267948a066e7SAl Viro read_seqlock_excl(&mount_lock); 2680643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 268148a066e7SAl Viro read_sequnlock_excl(&mount_lock); 2682afac7cbaSAl Viro return res; 2683afac7cbaSAl Viro } 2684afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 2685afac7cbaSAl Viro 2686afac7cbaSAl Viro /* 26871da177e4SLinus Torvalds * pivot_root Semantics: 26881da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 26891da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 26901da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 26911da177e4SLinus Torvalds * 26921da177e4SLinus Torvalds * Restrictions: 26931da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 26941da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 26951da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 26961da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 26971da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 26981da177e4SLinus Torvalds * 26994a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 27004a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 27014a0d11faSNeil Brown * in this situation. 27024a0d11faSNeil Brown * 27031da177e4SLinus Torvalds * Notes: 27041da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 27051da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 27061da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 27071da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 27081da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 27091da177e4SLinus Torvalds * first. 27101da177e4SLinus Torvalds */ 27113480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 27123480b257SHeiko Carstens const char __user *, put_old) 27131da177e4SLinus Torvalds { 27142d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 271584d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 271684d17192SAl Viro struct mountpoint *old_mp, *root_mp; 27171da177e4SLinus Torvalds int error; 27181da177e4SLinus Torvalds 27199b40bc90SAl Viro if (!may_mount()) 27201da177e4SLinus Torvalds return -EPERM; 27211da177e4SLinus Torvalds 27222d8f3038SAl Viro error = user_path_dir(new_root, &new); 27231da177e4SLinus Torvalds if (error) 27241da177e4SLinus Torvalds goto out0; 27251da177e4SLinus Torvalds 27262d8f3038SAl Viro error = user_path_dir(put_old, &old); 27271da177e4SLinus Torvalds if (error) 27281da177e4SLinus Torvalds goto out1; 27291da177e4SLinus Torvalds 27302d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 2731b12cea91SAl Viro if (error) 2732b12cea91SAl Viro goto out2; 27331da177e4SLinus Torvalds 2734f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 273584d17192SAl Viro old_mp = lock_mount(&old); 273684d17192SAl Viro error = PTR_ERR(old_mp); 273784d17192SAl Viro if (IS_ERR(old_mp)) 2738b12cea91SAl Viro goto out3; 2739b12cea91SAl Viro 27401da177e4SLinus Torvalds error = -EINVAL; 2741419148daSAl Viro new_mnt = real_mount(new.mnt); 2742419148daSAl Viro root_mnt = real_mount(root.mnt); 274384d17192SAl Viro old_mnt = real_mount(old.mnt); 274484d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 2745fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 2746fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 2747b12cea91SAl Viro goto out4; 2748143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 2749b12cea91SAl Viro goto out4; 27505ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 27515ff9d8a6SEric W. Biederman goto out4; 27521da177e4SLinus Torvalds error = -ENOENT; 2753f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 2754b12cea91SAl Viro goto out4; 27551da177e4SLinus Torvalds error = -EBUSY; 275684d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 2757b12cea91SAl Viro goto out4; /* loop, on the same file system */ 27581da177e4SLinus Torvalds error = -EINVAL; 27598c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 2760b12cea91SAl Viro goto out4; /* not a mountpoint */ 2761676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 2762b12cea91SAl Viro goto out4; /* not attached */ 276384d17192SAl Viro root_mp = root_mnt->mnt_mp; 27642d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 2765b12cea91SAl Viro goto out4; /* not a mountpoint */ 2766676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 2767b12cea91SAl Viro goto out4; /* not attached */ 27684ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 276984d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 2770b12cea91SAl Viro goto out4; 277184d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 2772719ea2fbSAl Viro lock_mount_hash(); 2773419148daSAl Viro detach_mnt(new_mnt, &parent_path); 2774419148daSAl Viro detach_mnt(root_mnt, &root_parent); 27755ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 27765ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 27775ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 27785ff9d8a6SEric W. Biederman } 27794ac91378SJan Blunck /* mount old root on put_old */ 278084d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 27814ac91378SJan Blunck /* mount new_root on / */ 278284d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 27836b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 2784719ea2fbSAl Viro unlock_mount_hash(); 27852d8f3038SAl Viro chroot_fs_refs(&root, &new); 278684d17192SAl Viro put_mountpoint(root_mp); 27871da177e4SLinus Torvalds error = 0; 2788b12cea91SAl Viro out4: 278984d17192SAl Viro unlock_mount(old_mp); 2790b12cea91SAl Viro if (!error) { 27911a390689SAl Viro path_put(&root_parent); 27921a390689SAl Viro path_put(&parent_path); 2793b12cea91SAl Viro } 2794b12cea91SAl Viro out3: 27958c3ee42eSAl Viro path_put(&root); 2796b12cea91SAl Viro out2: 27972d8f3038SAl Viro path_put(&old); 27981da177e4SLinus Torvalds out1: 27992d8f3038SAl Viro path_put(&new); 28001da177e4SLinus Torvalds out0: 28011da177e4SLinus Torvalds return error; 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 28041da177e4SLinus Torvalds static void __init init_mount_tree(void) 28051da177e4SLinus Torvalds { 28061da177e4SLinus Torvalds struct vfsmount *mnt; 28076b3286edSKirill Korotaev struct mnt_namespace *ns; 2808ac748a09SJan Blunck struct path root; 28090c55cfc4SEric W. Biederman struct file_system_type *type; 28101da177e4SLinus Torvalds 28110c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 28120c55cfc4SEric W. Biederman if (!type) 28130c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 28140c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 28150c55cfc4SEric W. Biederman put_filesystem(type); 28161da177e4SLinus Torvalds if (IS_ERR(mnt)) 28171da177e4SLinus Torvalds panic("Can't create rootfs"); 2818b3e19d92SNick Piggin 28193b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 28203b22edc5STrond Myklebust if (IS_ERR(ns)) 28211da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 28221da177e4SLinus Torvalds 28236b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 28246b3286edSKirill Korotaev get_mnt_ns(ns); 28251da177e4SLinus Torvalds 2826be08d6d2SAl Viro root.mnt = mnt; 2827be08d6d2SAl Viro root.dentry = mnt->mnt_root; 2828ac748a09SJan Blunck 2829ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 2830ac748a09SJan Blunck set_fs_root(current->fs, &root); 28311da177e4SLinus Torvalds } 28321da177e4SLinus Torvalds 283374bf17cfSDenis Cheng void __init mnt_init(void) 28341da177e4SLinus Torvalds { 283513f14b4dSEric Dumazet unsigned u; 283615a67dd8SRandy Dunlap int err; 28371da177e4SLinus Torvalds 28387d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 283920c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 28401da177e4SLinus Torvalds 28410818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 284238129a13SAl Viro sizeof(struct hlist_head), 28430818bf27SAl Viro mhash_entries, 19, 28440818bf27SAl Viro 0, 28450818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 28460818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 28470818bf27SAl Viro sizeof(struct hlist_head), 28480818bf27SAl Viro mphash_entries, 19, 28490818bf27SAl Viro 0, 28500818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 28511da177e4SLinus Torvalds 285284d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 28531da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 28541da177e4SLinus Torvalds 28550818bf27SAl Viro for (u = 0; u <= m_hash_mask; u++) 285638129a13SAl Viro INIT_HLIST_HEAD(&mount_hashtable[u]); 28570818bf27SAl Viro for (u = 0; u <= mp_hash_mask; u++) 28580818bf27SAl Viro INIT_HLIST_HEAD(&mountpoint_hashtable[u]); 28591da177e4SLinus Torvalds 28604b93dc9bSTejun Heo kernfs_init(); 28614b93dc9bSTejun Heo 286215a67dd8SRandy Dunlap err = sysfs_init(); 286315a67dd8SRandy Dunlap if (err) 286415a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 28658e24eea7SHarvey Harrison __func__, err); 286600d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 286700d26666SGreg Kroah-Hartman if (!fs_kobj) 28688e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 28691da177e4SLinus Torvalds init_rootfs(); 28701da177e4SLinus Torvalds init_mount_tree(); 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds 2873616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 28741da177e4SLinus Torvalds { 2875d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 2876616511d0STrond Myklebust return; 28777b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 2878771b1371SEric W. Biederman free_mnt_ns(ns); 28791da177e4SLinus Torvalds } 28809d412a43SAl Viro 28819d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 28829d412a43SAl Viro { 2883423e0ab0STim Chen struct vfsmount *mnt; 2884423e0ab0STim Chen mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); 2885423e0ab0STim Chen if (!IS_ERR(mnt)) { 2886423e0ab0STim Chen /* 2887423e0ab0STim Chen * it is a longterm mount, don't release mnt until 2888423e0ab0STim Chen * we unmount before file sys is unregistered 2889423e0ab0STim Chen */ 2890f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 2891423e0ab0STim Chen } 2892423e0ab0STim Chen return mnt; 28939d412a43SAl Viro } 28949d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 2895423e0ab0STim Chen 2896423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 2897423e0ab0STim Chen { 2898423e0ab0STim Chen /* release long term mount so mount point can be released */ 2899423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 2900f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 290148a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 2902423e0ab0STim Chen mntput(mnt); 2903423e0ab0STim Chen } 2904423e0ab0STim Chen } 2905423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 290602125a82SAl Viro 290702125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 290802125a82SAl Viro { 2909143c8c91SAl Viro return check_mnt(real_mount(mnt)); 291002125a82SAl Viro } 29118823c079SEric W. Biederman 29123151527eSEric W. Biederman bool current_chrooted(void) 29133151527eSEric W. Biederman { 29143151527eSEric W. Biederman /* Does the current process have a non-standard root */ 29153151527eSEric W. Biederman struct path ns_root; 29163151527eSEric W. Biederman struct path fs_root; 29173151527eSEric W. Biederman bool chrooted; 29183151527eSEric W. Biederman 29193151527eSEric W. Biederman /* Find the namespace root */ 29203151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 29213151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 29223151527eSEric W. Biederman path_get(&ns_root); 29233151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 29243151527eSEric W. Biederman ; 29253151527eSEric W. Biederman 29263151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 29273151527eSEric W. Biederman 29283151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 29293151527eSEric W. Biederman 29303151527eSEric W. Biederman path_put(&fs_root); 29313151527eSEric W. Biederman path_put(&ns_root); 29323151527eSEric W. Biederman 29333151527eSEric W. Biederman return chrooted; 29343151527eSEric W. Biederman } 29353151527eSEric W. Biederman 2936e51db735SEric W. Biederman bool fs_fully_visible(struct file_system_type *type) 293787a8ebd6SEric W. Biederman { 293887a8ebd6SEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 293987a8ebd6SEric W. Biederman struct mount *mnt; 2940e51db735SEric W. Biederman bool visible = false; 294187a8ebd6SEric W. Biederman 2942e51db735SEric W. Biederman if (unlikely(!ns)) 2943e51db735SEric W. Biederman return false; 2944e51db735SEric W. Biederman 294544bb4385SAl Viro down_read(&namespace_sem); 294687a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 2947e51db735SEric W. Biederman struct mount *child; 2948e51db735SEric W. Biederman if (mnt->mnt.mnt_sb->s_type != type) 2949e51db735SEric W. Biederman continue; 2950e51db735SEric W. Biederman 2951e51db735SEric W. Biederman /* This mount is not fully visible if there are any child mounts 2952e51db735SEric W. Biederman * that cover anything except for empty directories. 2953e51db735SEric W. Biederman */ 2954e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 2955e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 2956e51db735SEric W. Biederman if (!S_ISDIR(inode->i_mode)) 2957e51db735SEric W. Biederman goto next; 295841301ae7SEric W. Biederman if (inode->i_nlink > 2) 2959e51db735SEric W. Biederman goto next; 296087a8ebd6SEric W. Biederman } 2961e51db735SEric W. Biederman visible = true; 2962e51db735SEric W. Biederman goto found; 2963e51db735SEric W. Biederman next: ; 296487a8ebd6SEric W. Biederman } 2965e51db735SEric W. Biederman found: 296644bb4385SAl Viro up_read(&namespace_sem); 2967e51db735SEric W. Biederman return visible; 296887a8ebd6SEric W. Biederman } 296987a8ebd6SEric W. Biederman 29708823c079SEric W. Biederman static void *mntns_get(struct task_struct *task) 29718823c079SEric W. Biederman { 29728823c079SEric W. Biederman struct mnt_namespace *ns = NULL; 29738823c079SEric W. Biederman struct nsproxy *nsproxy; 29748823c079SEric W. Biederman 29758823c079SEric W. Biederman rcu_read_lock(); 29768823c079SEric W. Biederman nsproxy = task_nsproxy(task); 29778823c079SEric W. Biederman if (nsproxy) { 29788823c079SEric W. Biederman ns = nsproxy->mnt_ns; 29798823c079SEric W. Biederman get_mnt_ns(ns); 29808823c079SEric W. Biederman } 29818823c079SEric W. Biederman rcu_read_unlock(); 29828823c079SEric W. Biederman 29838823c079SEric W. Biederman return ns; 29848823c079SEric W. Biederman } 29858823c079SEric W. Biederman 29868823c079SEric W. Biederman static void mntns_put(void *ns) 29878823c079SEric W. Biederman { 29888823c079SEric W. Biederman put_mnt_ns(ns); 29898823c079SEric W. Biederman } 29908823c079SEric W. Biederman 29918823c079SEric W. Biederman static int mntns_install(struct nsproxy *nsproxy, void *ns) 29928823c079SEric W. Biederman { 29938823c079SEric W. Biederman struct fs_struct *fs = current->fs; 29948823c079SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 29958823c079SEric W. Biederman struct path root; 29968823c079SEric W. Biederman 29970c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 2998c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 2999c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3000ae11e0f1SZhao Hongjiang return -EPERM; 30018823c079SEric W. Biederman 30028823c079SEric W. Biederman if (fs->users != 1) 30038823c079SEric W. Biederman return -EINVAL; 30048823c079SEric W. Biederman 30058823c079SEric W. Biederman get_mnt_ns(mnt_ns); 30068823c079SEric W. Biederman put_mnt_ns(nsproxy->mnt_ns); 30078823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 30088823c079SEric W. Biederman 30098823c079SEric W. Biederman /* Find the root */ 30108823c079SEric W. Biederman root.mnt = &mnt_ns->root->mnt; 30118823c079SEric W. Biederman root.dentry = mnt_ns->root->mnt.mnt_root; 30128823c079SEric W. Biederman path_get(&root); 30138823c079SEric W. Biederman while(d_mountpoint(root.dentry) && follow_down_one(&root)) 30148823c079SEric W. Biederman ; 30158823c079SEric W. Biederman 30168823c079SEric W. Biederman /* Update the pwd and root */ 30178823c079SEric W. Biederman set_fs_pwd(fs, &root); 30188823c079SEric W. Biederman set_fs_root(fs, &root); 30198823c079SEric W. Biederman 30208823c079SEric W. Biederman path_put(&root); 30218823c079SEric W. Biederman return 0; 30228823c079SEric W. Biederman } 30238823c079SEric W. Biederman 302498f842e6SEric W. Biederman static unsigned int mntns_inum(void *ns) 302598f842e6SEric W. Biederman { 302698f842e6SEric W. Biederman struct mnt_namespace *mnt_ns = ns; 302798f842e6SEric W. Biederman return mnt_ns->proc_inum; 302898f842e6SEric W. Biederman } 302998f842e6SEric W. Biederman 30308823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 30318823c079SEric W. Biederman .name = "mnt", 30328823c079SEric W. Biederman .type = CLONE_NEWNS, 30338823c079SEric W. Biederman .get = mntns_get, 30348823c079SEric W. Biederman .put = mntns_put, 30358823c079SEric W. Biederman .install = mntns_install, 303698f842e6SEric W. Biederman .inum = mntns_inum, 30378823c079SEric W. Biederman }; 3038